1 /*
2  * @file
3  * @author  TPOC: contact@palisade-crypto.org
4  *
5  * @copyright Copyright (c) 2019, New Jersey Institute of Technology (NJIT)
6  * All rights reserved.
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution. THIS SOFTWARE IS
14  * PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17  * EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 /*
27  *  This file contains google test code that exercises the big int
28  *  vector library of the PALISADE lattice encryption library.
29  *
30  */
31 
32 #include "include/gtest/gtest.h"
33 #include <iostream>
34 #include <fstream>
35 
36 #include "lattice/dcrtpoly.h"
37 #include "math/backend.h"
38 #include "utils/inttypes.h"
39 #include "math/nbtheory.h"
40 
41 #include "math/distrgen.h"
42 #include "utils/utilities.h"
43 #include "lattice/backend.h"
44 
45 using namespace std;
46 using namespace lbcrypto;
47 
48 /************************************************
49  *	TESTING Modular operations for 128-bit backend
50  ************************************************/
51 #if (NATIVEINT == 128)
TEST(UT128,modular_operations)52 TEST(UT128, modular_operations) {
53   bigintnat::NativeInteger modulus = ((bigintnat::NativeInteger(1) << 120) +
54                                       bigintnat::NativeInteger(123456789));
55   bigintnat::NativeInteger mu = modulus.ComputeMu();
56 
57   bigintnat::NativeInteger a =
58       (bigintnat::NativeInteger(1) << 110) + bigintnat::NativeInteger(1234);
59   bigintnat::NativeInteger b =
60       (bigintnat::NativeInteger(1) << 115) + bigintnat::NativeInteger(6789);
61 
62   bigintnat::NativeInteger c =
63       (bigintnat::NativeInteger(1) << 120) + bigintnat::NativeInteger(6);
64 
65   bigintnat::NativeInteger result = a;
66 
67   result.ModMulEq(b, modulus, mu);
68 
69   EXPECT_EQ(BigInteger("784483038650039308657432244878529921"),
70             BigInteger(result))
71       << "Failure in ModMulEq";
72 
73   result = a.ModMul(b, modulus, mu);
74 
75   EXPECT_EQ(BigInteger("784483038650039308657432244878529921"),
76             BigInteger(result))
77       << "Failure in ModMul";
78 
79   result = a.ModMulFast(b, modulus, mu);
80 
81   EXPECT_EQ(BigInteger("784483038650039308657432244878529921"),
82             BigInteger(result))
83       << "Failure in ModMulFast";
84 
85   result = a;
86 
87   result.ModMulFastEq(b, modulus, mu);
88 
89   EXPECT_EQ(BigInteger("784483038650039308657432244878529921"),
90             BigInteger(result))
91       << "Failure in ModMulFastEq";
92 
93   bigintnat::NativeInteger precon = b.PrepModMulConst(modulus);
94 
95   result = a.ModMulFastConst(b, modulus, precon);
96 
97   EXPECT_EQ(BigInteger("784483038650039308657432244878529921"),
98             BigInteger(result))
99       << "Failure in ModMulFastConst";
100 
101   result = a;
102 
103   result.ModMulFastConstEq(b, modulus, precon);
104 
105   EXPECT_EQ(BigInteger("784483038650039308657432244878529921"),
106             BigInteger(result))
107       << "Failure in ModMulFastConstEq";
108 
109   result = a.ModExp(b, modulus);
110 
111   EXPECT_EQ(BigInteger("420836984722658338771647831749821018"),
112             BigInteger(result))
113       << "Failure in ModExp";
114 
115   result = a;
116 
117   result.ModExpEq(b, modulus);
118 
119   EXPECT_EQ(BigInteger("420836984722658338771647831749821018"),
120             BigInteger(result))
121       << "Failure in ModExpEq";
122 
123   result = a.ModAddFast(c, modulus);
124 
125   EXPECT_EQ(BigInteger("1298074214633706907132623958849475"),
126             BigInteger(result))
127       << "Failure in ModAddFast";
128 
129   result = a.ModSubFast(c, modulus);
130 
131   EXPECT_EQ(BigInteger("1298074214633706907132624205763041"),
132             BigInteger(result))
133       << "Failure in ModSubFast";
134 
135   result = a.ModInverse(modulus);
136 
137   EXPECT_EQ(BigInteger("859455677183853192994953853474516202"),
138             BigInteger(result))
139       << "Failure in ModInverse";
140 }
141 
TEST(UT128,NTT_operations)142 TEST(UT128, NTT_operations) {
143   usint m1 = 16;
144   NativeInteger modulus = FirstPrime<NativeInteger>(100, m1);
145   NativeInteger rootOfUnity(RootOfUnity(m1, modulus));
146 
147   ILNativeParams params(m1, modulus, rootOfUnity);
148   ILNativeParams params2(m1 / 2, modulus, rootOfUnity);
149   shared_ptr<ILNativeParams> x1p(new ILNativeParams(params));
150   shared_ptr<ILNativeParams> x2p(new ILNativeParams(params2));
151 
152   NativePoly x1(x1p, Format::COEFFICIENT);
153   x1 = {431, 3414, 1234, 7845, 2145, 7415, 5471, 8452};
154 
155   NativePoly x2(x2p, Format::COEFFICIENT);
156   x2 = {4127, 9647, 1987, 5410};
157 
158   NativePoly x1Clone(x1);
159   NativePoly x2Clone(x2);
160 
161   x1.SwitchFormat();
162   x2.SwitchFormat();
163   x1.SwitchFormat();
164   x2.SwitchFormat();
165 
166   EXPECT_EQ(x1, x1Clone) << "Failure in NTT test #1";
167   EXPECT_EQ(x2, x2Clone) << "Failure in NTT test #2";
168 }
169 
170 #endif
171