• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*############################################################################
2 # Copyright 2017 Intel Corporation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 ############################################################################*/
16 /// Unit tests of Fq2 implementation.
17 /*! \file */
18 
19 #include <gtest/gtest.h>
20 
21 #include "epid/member/tiny/math/unittests/cmp-testhelper.h"
22 
23 extern "C" {
24 #include "epid/member/tiny/math/fq2.h"
25 #include "epid/member/tiny/math/mathtypes.h"
26 }
27 
28 namespace {
29 
30 ////////////////////////////////////////////////////////////////////////
31 // Fq2Cp
32 
TEST(TinyFq2Test,Fq2CpWorks)33 TEST(TinyFq2Test, Fq2CpWorks) {
34   FqElem fq = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
35                 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
36   Fq2Elem a = {fq, fq};
37   Fq2Elem result = {0};
38   Fq2Cp(&result, &a);
39   EXPECT_EQ(a, result);
40 }
41 
42 ////////////////////////////////////////////////////////////////////////
43 // Fq2Set
44 
TEST(TinyFq2Test,Fq2SetWorks)45 TEST(TinyFq2Test, Fq2SetWorks) {
46   uint32_t small = 0xffffffff;
47   Fq2Elem expected = {0};
48   expected.x0.limbs.word[0] = small;
49   FqElem fq = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
50                 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
51   Fq2Elem result = {fq, fq};
52   Fq2Set(&result, small);
53   EXPECT_EQ(expected, result);
54 }
55 
56 ////////////////////////////////////////////////////////////////////////
57 // Fq2Clear
58 
TEST(TinyFq2Test,Fq2ClearWorks)59 TEST(TinyFq2Test, Fq2ClearWorks) {
60   Fq2Elem expected = {0};
61   FqElem fq = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
62                 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
63   Fq2Elem result = {fq, fq};
64   Fq2Clear(&result);
65   EXPECT_EQ(expected, result);
66 }
67 
68 ////////////////////////////////////////////////////////////////////////
69 // Fq2Add
70 
TEST(TinyFq2Test,Fq2AddWorks)71 TEST(TinyFq2Test, Fq2AddWorks) {
72   Fq2Elem result = {0};
73   Fq2Elem left = {{0, 0, 0, 0, 0, 5, 0, 0}, {0, 0, 0, 0, 0, 0, 9, 0}};
74   Fq2Elem right = {{0, 0, 0, 0, 0, 20, 0, 0}, {0, 0, 0, 0, 0, 0, 30, 0}};
75   Fq2Elem expected = {{0, 0, 0, 0, 0, 25, 0, 0}, {0, 0, 0, 0, 0, 0, 39, 0}};
76   Fq2Add(&result, &left, &right);
77   EXPECT_EQ(expected, result);
78 }
79 
80 ////////////////////////////////////////////////////////////////////////
81 // Fq2Exp
82 
TEST(TinyFq2Test,Fq2ExpWorks)83 TEST(TinyFq2Test, Fq2ExpWorks) {
84   Fq2Elem one = {1};
85   Fq2Elem in = {0};
86   VeryLargeInt exp = {0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB,
87                       0xEE71A49F, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF};
88   Fq2Elem result = {{0xb06f3418, 0x16043dfc, 0x7884a9ca, 0x0ab1a427, 0x309307b5,
89                      0x23133f6d, 0x250c3dc4, 0x005c4818},
90                     {0x3b6f652c, 0x6435c0fd, 0x509b8d30, 0x13fe4bef, 0x5f4ffc14,
91                      0x81dde5fd, 0x02b77b89, 0x5b38b2a5}};
92   Fq2Exp(&result, &in, &exp);
93   EXPECT_EQ(in, result);
94 
95   in = {exp, exp};
96   exp = {0};
97   Fq2Exp(&result, &in, &exp);
98   EXPECT_EQ(one, result);
99 
100   exp = {2};
101   in = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357, 0x4780716c,
102          0xffd94b0f, 0x5e643124},
103         {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651, 0x1bb8f3e0,
104          0x1e1f4181, 0x8aa45bf5}};
105   Fq2Elem expected = {{0xb06f3418, 0x16043dfc, 0x7884a9ca, 0x0ab1a427,
106                        0x309307b5, 0x23133f6d, 0x250c3dc4, 0x005c4818},
107                       {0x3b6f652c, 0x6435c0fd, 0x509b8d30, 0x13fe4bef,
108                        0x5f4ffc14, 0x81dde5fd, 0x02b77b89, 0x5b38b2a5}};
109   Fq2Exp(&result, &in, &exp);
110   EXPECT_EQ(expected, result);
111 }
112 
113 ////////////////////////////////////////////////////////////////////////
114 // Fq2Sub
115 
TEST(TinyFq2Test,Fq2SubWorks)116 TEST(TinyFq2Test, Fq2SubWorks) {
117   Fq2Elem result = {0};
118   Fq2Elem left = {{0, 0, 0, 0, 0, 20, 0, 0}, {0, 0, 0, 0, 0, 0, 30, 0}};
119   Fq2Elem right = {{0, 0, 0, 0, 0, 5, 0, 0}, {0, 0, 0, 0, 0, 0, 9, 0}};
120   Fq2Elem expected = {{0, 0, 0, 0, 0, 15, 0, 0}, {0, 0, 0, 0, 0, 0, 21, 0}};
121   Fq2Sub(&result, &left, &right);
122   EXPECT_EQ(expected, result);
123 }
124 
125 ////////////////////////////////////////////////////////////////////////
126 // Fq2Mul
127 
TEST(TinyFq2Test,Fq2MultWorks)128 TEST(TinyFq2Test, Fq2MultWorks) {
129   Fq2Elem expected = {{0x37861727, 0x52822db7, 0x8005ec64, 0xc0b0bc96,
130                        0xd60e07a4, 0x65eee0a2, 0x780dbc26, 0x7b36e4cb},
131                       {0x8201d4ed, 0xbf8ed473, 0xc5c09cbe, 0xba9d0095,
132                        0x3d91414a, 0xa8ebb728, 0x66bc029b, 0x5b6ca52b}};
133   Fq2Elem left = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
134                    0x4780716c, 0xffd94b0f, 0x5e643124},
135                   {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651,
136                    0x1bb8f3e0, 0x1e1f4181, 0x8aa45bf5}};
137   Fq2Elem right = {{0x848cdb73, 0x6399829e, 0xcaa20cc0, 0x1b02bff6, 0x2b477bd2,
138                     0xf9d48534, 0xff7929a0, 0xd4745161},
139                    {0xe323d956, 0xf8a05a85, 0xe02d5e1e, 0xfd533966, 0xe7d31209,
140                     0xc7786143, 0x91b441f6, 0x7409d67d}};
141   Fq2Elem actual = {0};
142   Fq2Mul(&actual, &left, &right);
143   EXPECT_EQ(expected, actual);
144 }
145 
146 ////////////////////////////////////////////////////////////////////////
147 // Fq2Inv
148 
TEST(TinyFq2Test,Fq2InvWorks)149 TEST(TinyFq2Test, Fq2InvWorks) {
150   Fq2Elem expected = {{0xffa2241d, 0x37481517, 0x4bc3ad3a, 0x3c57cefe,
151                        0xb7a57680, 0xaef8eb15, 0x6a1f7923, 0x2e6a7077},
152                       {0xdc8eb865, 0xe264dc4c, 0xa2a174dc, 0xa18b8fe8,
153                        0x31ee8433, 0xdea6fa81, 0x7ec16a0e, 0x1b0f8f81}};
154   Fq2Elem left = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
155                    0x4780716c, 0xffd94b0f, 0x5e643124},
156                   {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651,
157                    0x1bb8f3e0, 0x1e1f4181, 0x8aa45bf5}};
158   Fq2Elem actual = {0};
159   Fq2Inv(&actual, &left);
160   EXPECT_EQ(expected, actual);
161 }
162 
TEST(TinyFq2Test,Fq2InvWorksInPlace)163 TEST(TinyFq2Test, Fq2InvWorksInPlace) {
164   Fq2Elem expected = {{0xffa2241d, 0x37481517, 0x4bc3ad3a, 0x3c57cefe,
165                        0xb7a57680, 0xaef8eb15, 0x6a1f7923, 0x2e6a7077},
166                       {0xdc8eb865, 0xe264dc4c, 0xa2a174dc, 0xa18b8fe8,
167                        0x31ee8433, 0xdea6fa81, 0x7ec16a0e, 0x1b0f8f81}};
168   Fq2Elem left = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
169                    0x4780716c, 0xffd94b0f, 0x5e643124},
170                   {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651,
171                    0x1bb8f3e0, 0x1e1f4181, 0x8aa45bf5}};
172   Fq2Inv(&left, &left);
173   EXPECT_EQ(expected, left);
174 }
175 ////////////////////////////////////////////////////////////////////////
176 // Fq2Neg
177 
TEST(TinyFq2Test,Fq2NegWorks)178 TEST(TinyFq2Test, Fq2NegWorks) {
179   FqElem const q = {{0xAED33013, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
180                      0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
181   Fq2Elem neg_value = {0};
182   Fq2Elem one = {{1}, {1}};
183   Fq2Elem minus_one = {q, q};
184   --minus_one.x0.limbs.word[0];
185   --minus_one.x1.limbs.word[0];
186   Fq2Neg(&neg_value, &one);
187   EXPECT_EQ(minus_one, neg_value);
188 
189   Fq2Elem value = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
190                     0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF},
191                    {0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
192                     0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
193   Fq2Neg(&neg_value, &value);
194   Fq2Neg(&neg_value, &neg_value);
195   EXPECT_EQ(value, neg_value);
196   value = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
197             0x4780716c, 0xffd94b0f, 0x5e643124},
198            {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651,
199             0x1bb8f3e0, 0x1e1f4181, 0x8aa45bf5}};
200   Fq2Elem expected = {{0x8c035971, 0xaf40febd, 0x3d89f632, 0x24887d6e,
201                        0x3ecb5147, 0xff6580f2, 0x0023a5bd, 0xa19bcedb},
202                       {0x61afe694, 0xba8b7e8e, 0x07d2460a, 0xd758834b,
203                        0xb9944e4d, 0x2b2cfe7e, 0xe1ddaf4c, 0x755ba40a}};
204   Fq2Neg(&neg_value, &value);
205   EXPECT_EQ(expected, neg_value);
206 }
207 
208 ////////////////////////////////////////////////////////////////////////
209 // Fq2Conj
210 
TEST(TinyFq2Test,Fq2ConjWorks)211 TEST(TinyFq2Test, Fq2ConjWorks) {
212   Fq2Elem expected = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c,
213                        0xafa65357, 0x4780716c, 0xffd94b0f, 0x5e643124},
214                       {0x61afe694, 0xba8b7e8e, 0x07d2460a, 0xd758834b,
215                        0xb9944e4d, 0x2b2cfe7e, 0xe1ddaf4c, 0x755ba40a}};
216   Fq2Elem left = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
217                    0x4780716c, 0xffd94b0f, 0x5e643124},
218                   {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651,
219                    0x1bb8f3e0, 0x1e1f4181, 0x8aa45bf5}};
220   Fq2Elem actual = {0};
221   Fq2Conj(&actual, &left);
222   EXPECT_EQ(expected, actual);
223 }
224 
225 ////////////////////////////////////////////////////////////////////////
226 // Fq2Square
227 
TEST(TinyFq2Test,Fq2SquareWorks)228 TEST(TinyFq2Test, Fq2SquareWorks) {
229   Fq2Elem expected = {{0xb06f3418, 0x16043dfc, 0x7884a9ca, 0x0ab1a427,
230                        0x309307b5, 0x23133f6d, 0x250c3dc4, 0x005c4818},
231                       {0x3b6f652c, 0x6435c0fd, 0x509b8d30, 0x13fe4bef,
232                        0x5f4ffc14, 0x81dde5fd, 0x02b77b89, 0x5b38b2a5}};
233   Fq2Elem result = {0};
234   Fq2Elem in = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
235                  0x4780716c, 0xffd94b0f, 0x5e643124},
236                 {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651,
237                  0x1bb8f3e0, 0x1e1f4181, 0x8aa45bf5}};
238   Fq2Square(&result, &in);
239   EXPECT_EQ(expected, result);
240 }
241 
242 ////////////////////////////////////////////////////////////////////////
243 // Fq2MulScalar
244 
TEST(TinyFq2Test,Fq2MulScalarWorks)245 TEST(TinyFq2Test, Fq2MulScalarWorks) {
246   Fq2Elem expected = {{0x28f2f1dd, 0x2cb2b611, 0xa24767b3, 0x4e880c0e,
247                        0xed7f7b9e, 0x6ff4a7f2, 0x25fb15d0, 0x7b8c4fed},
248                       {0}};
249   Fq2Elem left = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
250                    0x4780716c, 0xffd94b0f, 0x5e643124},
251                   {0}};
252   FqElem scalar = {0x848cdb73, 0x6399829e, 0xcaa20cc0, 0x1b02bff6,
253                    0x2b477bd2, 0xf9d48534, 0xff7929a0, 0xd4745161};
254   Fq2Elem actual = {0};
255   Fq2MulScalar(&actual, &left, &scalar);
256   EXPECT_EQ(expected, actual);
257 }
258 
TEST(TinyFq2Test,Fq2MulScalarWorksInPlace)259 TEST(TinyFq2Test, Fq2MulScalarWorksInPlace) {
260   Fq2Elem expected = {{0x28f2f1dd, 0x2cb2b611, 0xa24767b3, 0x4e880c0e,
261                        0xed7f7b9e, 0x6ff4a7f2, 0x25fb15d0, 0x7b8c4fed},
262                       {0}};
263   Fq2Elem left = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
264                    0x4780716c, 0xffd94b0f, 0x5e643124},
265                   {0}};
266   FqElem scalar = {0x848cdb73, 0x6399829e, 0xcaa20cc0, 0x1b02bff6,
267                    0x2b477bd2, 0xf9d48534, 0xff7929a0, 0xd4745161};
268   Fq2MulScalar(&left, &left, &scalar);
269   EXPECT_EQ(expected, left);
270 }
271 ////////////////////////////////////////////////////////////////////////
272 // Fq2CondSet
273 
TEST(TinyFq2Test,Fq2CondSetWorksForTrue)274 TEST(TinyFq2Test, Fq2CondSetWorksForTrue) {
275   Fq2Elem a = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
276                 0x4780716c, 0xffd94b0f, 0x5e64364},
277                {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651,
278                 0x1bb8f3e0, 0x1e1f4181, 0x8aa45bf5}};
279   Fq2Elem b = {{0, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 1, 0}};
280   Fq2Elem result = {0};
281   Fq2CondSet(&result, &a, &b, true);
282   EXPECT_EQ(a, result);
283 }
284 
TEST(TinyFq2Test,Fq2CondSetWorksForFalse)285 TEST(TinyFq2Test, Fq2CondSetWorksForFalse) {
286   Fq2Elem a = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
287                 0x4780716c, 0xffd94b0f, 0x5e64364},
288                {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651,
289                 0x1bb8f3e0, 0x1e1f4181, 0x8aa45bf5}};
290   Fq2Elem b = {{0, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 1, 0}};
291   Fq2Elem result = {0};
292   Fq2CondSet(&result, &a, &b, false);
293   EXPECT_EQ(b, result);
294 }
295 
296 ////////////////////////////////////////////////////////////////////////
297 // Fq2Eq
298 
TEST(TinyFq2Test,Fq2EqPasses)299 TEST(TinyFq2Test, Fq2EqPasses) {
300   FqElem fq_a = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
301                   0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
302   FqElem fq_b = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
303                   0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
304   Fq2Elem a = {fq_a, fq_a};
305   Fq2Elem b = {fq_b, fq_b};
306   EXPECT_TRUE(Fq2Eq(&a, &b));
307 }
308 
TEST(TinyFq2Test,Fq2EqFails)309 TEST(TinyFq2Test, Fq2EqFails) {
310   FqElem fq_a = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
311                   0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
312   FqElem fq_b = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
313                   0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
314   Fq2Elem a = {fq_a, fq_a};
315   Fq2Elem b = {fq_b, fq_b};
316   --b.x1.limbs.word[5];
317   EXPECT_FALSE(Fq2Eq(&a, &b));
318 }
319 
320 ////////////////////////////////////////////////////////////////////////
321 // Fq2MulXi
322 
TEST(TinyFq2Test,Fq2MultXiWorks)323 TEST(TinyFq2Test, Fq2MultXiWorks) {
324   const Fq2Elem a = {{0xbca2b7aa, 0xc0e43294, 0x6199e561, 0xefdb7a39,
325                       0xd57bcbba, 0x03154f2a, 0xdf9e1797, 0xf52d29c1},
326                      {0x77cb909b, 0x906d8657, 0xfea2ffb3, 0x7810e964,
327                       0x022e47c1, 0x862bdbe6, 0xe4f5d59b, 0xa677247d}};
328   const Fq2Elem expected = {{0x52a6aea6, 0x1e31b0f6, 0xb1f8c08d, 0x5ac9a512,
329                              0xba57ab15, 0x3918d010, 0xda4968c5, 0x43e32f05},
330                             {0x4e9378ba, 0x3b6ce38c, 0x39afcfc3, 0xc644810d,
331                              0xfcf511ff, 0x81a12238, 0xa98fe133, 0x421b72bd}};
332 
333   Fq2Elem res;
334   Fq2MulXi(&res, &a);
335   EXPECT_EQ(expected, res);
336 }
337 
338 ////////////////////////////////////////////////////////////////////////
339 // Fq2IsZero
340 
TEST(TinyFq2Test,Fq2IsZeroPasses)341 TEST(TinyFq2Test, Fq2IsZeroPasses) {
342   Fq2Elem zero = {0};
343   EXPECT_TRUE(Fq2IsZero(&zero));
344 }
345 
TEST(TinyFq2Test,Fq2IsZeroFails)346 TEST(TinyFq2Test, Fq2IsZeroFails) {
347   Fq2Elem non_zero = {0};
348   ++non_zero.x0.limbs.word[6];
349   EXPECT_FALSE(Fq2IsZero(&non_zero));
350 }
351 
352 }  // namespace
353