fq2-test.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #include <gtest/gtest.h>
  19. #include "epid/member/tiny/math/unittests/cmp-testhelper.h"
  20. extern "C" {
  21. #include "epid/member/tiny/math/fq2.h"
  22. #include "epid/member/tiny/math/mathtypes.h"
  23. }
  24. namespace {
  25. ////////////////////////////////////////////////////////////////////////
  26. // Fq2Cp
  27. TEST(TinyFq2Test, Fq2CpWorks) {
  28. FqElem fq = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  29. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  30. Fq2Elem a = {fq, fq};
  31. Fq2Elem result = {0};
  32. Fq2Cp(&result, &a);
  33. EXPECT_EQ(a, result);
  34. }
  35. ////////////////////////////////////////////////////////////////////////
  36. // Fq2Set
  37. TEST(TinyFq2Test, Fq2SetWorks) {
  38. uint32_t small = 0xffffffff;
  39. Fq2Elem expected = {0};
  40. expected.x0.limbs.word[0] = small;
  41. FqElem fq = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  42. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  43. Fq2Elem result = {fq, fq};
  44. Fq2Set(&result, small);
  45. EXPECT_EQ(expected, result);
  46. }
  47. ////////////////////////////////////////////////////////////////////////
  48. // Fq2Clear
  49. TEST(TinyFq2Test, Fq2ClearWorks) {
  50. Fq2Elem expected = {0};
  51. FqElem fq = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  52. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  53. Fq2Elem result = {fq, fq};
  54. Fq2Clear(&result);
  55. EXPECT_EQ(expected, result);
  56. }
  57. ////////////////////////////////////////////////////////////////////////
  58. // Fq2Add
  59. TEST(TinyFq2Test, Fq2AddWorks) {
  60. Fq2Elem result = {0};
  61. Fq2Elem left = {{0, 0, 0, 0, 0, 5, 0, 0}, {0, 0, 0, 0, 0, 0, 9, 0}};
  62. Fq2Elem right = {{0, 0, 0, 0, 0, 20, 0, 0}, {0, 0, 0, 0, 0, 0, 30, 0}};
  63. Fq2Elem expected = {{0, 0, 0, 0, 0, 25, 0, 0}, {0, 0, 0, 0, 0, 0, 39, 0}};
  64. Fq2Add(&result, &left, &right);
  65. EXPECT_EQ(expected, result);
  66. }
  67. ////////////////////////////////////////////////////////////////////////
  68. // Fq2Exp
  69. TEST(TinyFq2Test, Fq2ExpWorks) {
  70. Fq2Elem one = {1};
  71. Fq2Elem in = {0};
  72. VeryLargeInt exp = {0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB,
  73. 0xEE71A49F, 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF};
  74. Fq2Elem result = {{0xb06f3418, 0x16043dfc, 0x7884a9ca, 0x0ab1a427, 0x309307b5,
  75. 0x23133f6d, 0x250c3dc4, 0x005c4818},
  76. {0x3b6f652c, 0x6435c0fd, 0x509b8d30, 0x13fe4bef, 0x5f4ffc14,
  77. 0x81dde5fd, 0x02b77b89, 0x5b38b2a5}};
  78. Fq2Exp(&result, &in, &exp);
  79. EXPECT_EQ(in, result);
  80. in = {exp, exp};
  81. exp = {0};
  82. Fq2Exp(&result, &in, &exp);
  83. EXPECT_EQ(one, result);
  84. exp = {2};
  85. in = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357, 0x4780716c,
  86. 0xffd94b0f, 0x5e643124},
  87. {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651, 0x1bb8f3e0,
  88. 0x1e1f4181, 0x8aa45bf5}};
  89. Fq2Elem expected = {{0xb06f3418, 0x16043dfc, 0x7884a9ca, 0x0ab1a427,
  90. 0x309307b5, 0x23133f6d, 0x250c3dc4, 0x005c4818},
  91. {0x3b6f652c, 0x6435c0fd, 0x509b8d30, 0x13fe4bef,
  92. 0x5f4ffc14, 0x81dde5fd, 0x02b77b89, 0x5b38b2a5}};
  93. Fq2Exp(&result, &in, &exp);
  94. EXPECT_EQ(expected, result);
  95. }
  96. ////////////////////////////////////////////////////////////////////////
  97. // Fq2Sub
  98. TEST(TinyFq2Test, Fq2SubWorks) {
  99. Fq2Elem result = {0};
  100. Fq2Elem left = {{0, 0, 0, 0, 0, 20, 0, 0}, {0, 0, 0, 0, 0, 0, 30, 0}};
  101. Fq2Elem right = {{0, 0, 0, 0, 0, 5, 0, 0}, {0, 0, 0, 0, 0, 0, 9, 0}};
  102. Fq2Elem expected = {{0, 0, 0, 0, 0, 15, 0, 0}, {0, 0, 0, 0, 0, 0, 21, 0}};
  103. Fq2Sub(&result, &left, &right);
  104. EXPECT_EQ(expected, result);
  105. }
  106. ////////////////////////////////////////////////////////////////////////
  107. // Fq2Mul
  108. TEST(TinyFq2Test, Fq2MultWorks) {
  109. Fq2Elem expected = {{0x37861727, 0x52822db7, 0x8005ec64, 0xc0b0bc96,
  110. 0xd60e07a4, 0x65eee0a2, 0x780dbc26, 0x7b36e4cb},
  111. {0x8201d4ed, 0xbf8ed473, 0xc5c09cbe, 0xba9d0095,
  112. 0x3d91414a, 0xa8ebb728, 0x66bc029b, 0x5b6ca52b}};
  113. Fq2Elem left = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
  114. 0x4780716c, 0xffd94b0f, 0x5e643124},
  115. {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651,
  116. 0x1bb8f3e0, 0x1e1f4181, 0x8aa45bf5}};
  117. Fq2Elem right = {{0x848cdb73, 0x6399829e, 0xcaa20cc0, 0x1b02bff6, 0x2b477bd2,
  118. 0xf9d48534, 0xff7929a0, 0xd4745161},
  119. {0xe323d956, 0xf8a05a85, 0xe02d5e1e, 0xfd533966, 0xe7d31209,
  120. 0xc7786143, 0x91b441f6, 0x7409d67d}};
  121. Fq2Elem actual = {0};
  122. Fq2Mul(&actual, &left, &right);
  123. EXPECT_EQ(expected, actual);
  124. }
  125. ////////////////////////////////////////////////////////////////////////
  126. // Fq2Inv
  127. TEST(TinyFq2Test, Fq2InvWorks) {
  128. Fq2Elem expected = {{0xffa2241d, 0x37481517, 0x4bc3ad3a, 0x3c57cefe,
  129. 0xb7a57680, 0xaef8eb15, 0x6a1f7923, 0x2e6a7077},
  130. {0xdc8eb865, 0xe264dc4c, 0xa2a174dc, 0xa18b8fe8,
  131. 0x31ee8433, 0xdea6fa81, 0x7ec16a0e, 0x1b0f8f81}};
  132. Fq2Elem left = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
  133. 0x4780716c, 0xffd94b0f, 0x5e643124},
  134. {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651,
  135. 0x1bb8f3e0, 0x1e1f4181, 0x8aa45bf5}};
  136. Fq2Elem actual = {0};
  137. Fq2Inv(&actual, &left);
  138. EXPECT_EQ(expected, actual);
  139. }
  140. TEST(TinyFq2Test, Fq2InvWorksInPlace) {
  141. Fq2Elem expected = {{0xffa2241d, 0x37481517, 0x4bc3ad3a, 0x3c57cefe,
  142. 0xb7a57680, 0xaef8eb15, 0x6a1f7923, 0x2e6a7077},
  143. {0xdc8eb865, 0xe264dc4c, 0xa2a174dc, 0xa18b8fe8,
  144. 0x31ee8433, 0xdea6fa81, 0x7ec16a0e, 0x1b0f8f81}};
  145. Fq2Elem left = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
  146. 0x4780716c, 0xffd94b0f, 0x5e643124},
  147. {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651,
  148. 0x1bb8f3e0, 0x1e1f4181, 0x8aa45bf5}};
  149. Fq2Inv(&left, &left);
  150. EXPECT_EQ(expected, left);
  151. }
  152. ////////////////////////////////////////////////////////////////////////
  153. // Fq2Neg
  154. TEST(TinyFq2Test, Fq2NegWorks) {
  155. FqElem const q = {{0xAED33013, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  156. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  157. Fq2Elem neg_value = {0};
  158. Fq2Elem one = {{1}, {1}};
  159. Fq2Elem minus_one = {q, q};
  160. --minus_one.x0.limbs.word[0];
  161. --minus_one.x1.limbs.word[0];
  162. Fq2Neg(&neg_value, &one);
  163. EXPECT_EQ(minus_one, neg_value);
  164. Fq2Elem value = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  165. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF},
  166. {0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  167. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  168. Fq2Neg(&neg_value, &value);
  169. Fq2Neg(&neg_value, &neg_value);
  170. EXPECT_EQ(value, neg_value);
  171. value = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
  172. 0x4780716c, 0xffd94b0f, 0x5e643124},
  173. {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651,
  174. 0x1bb8f3e0, 0x1e1f4181, 0x8aa45bf5}};
  175. Fq2Elem expected = {{0x8c035971, 0xaf40febd, 0x3d89f632, 0x24887d6e,
  176. 0x3ecb5147, 0xff6580f2, 0x0023a5bd, 0xa19bcedb},
  177. {0x61afe694, 0xba8b7e8e, 0x07d2460a, 0xd758834b,
  178. 0xb9944e4d, 0x2b2cfe7e, 0xe1ddaf4c, 0x755ba40a}};
  179. Fq2Neg(&neg_value, &value);
  180. EXPECT_EQ(expected, neg_value);
  181. }
  182. ////////////////////////////////////////////////////////////////////////
  183. // Fq2Conj
  184. TEST(TinyFq2Test, Fq2ConjWorks) {
  185. Fq2Elem expected = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c,
  186. 0xafa65357, 0x4780716c, 0xffd94b0f, 0x5e643124},
  187. {0x61afe694, 0xba8b7e8e, 0x07d2460a, 0xd758834b,
  188. 0xb9944e4d, 0x2b2cfe7e, 0xe1ddaf4c, 0x755ba40a}};
  189. Fq2Elem left = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
  190. 0x4780716c, 0xffd94b0f, 0x5e643124},
  191. {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651,
  192. 0x1bb8f3e0, 0x1e1f4181, 0x8aa45bf5}};
  193. Fq2Elem actual = {0};
  194. Fq2Conj(&actual, &left);
  195. EXPECT_EQ(expected, actual);
  196. }
  197. ////////////////////////////////////////////////////////////////////////
  198. // Fq2Square
  199. TEST(TinyFq2Test, Fq2SquareWorks) {
  200. Fq2Elem expected = {{0xb06f3418, 0x16043dfc, 0x7884a9ca, 0x0ab1a427,
  201. 0x309307b5, 0x23133f6d, 0x250c3dc4, 0x005c4818},
  202. {0x3b6f652c, 0x6435c0fd, 0x509b8d30, 0x13fe4bef,
  203. 0x5f4ffc14, 0x81dde5fd, 0x02b77b89, 0x5b38b2a5}};
  204. Fq2Elem result = {0};
  205. Fq2Elem in = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
  206. 0x4780716c, 0xffd94b0f, 0x5e643124},
  207. {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651,
  208. 0x1bb8f3e0, 0x1e1f4181, 0x8aa45bf5}};
  209. Fq2Square(&result, &in);
  210. EXPECT_EQ(expected, result);
  211. }
  212. ////////////////////////////////////////////////////////////////////////
  213. // Fq2MulScalar
  214. TEST(TinyFq2Test, Fq2MulScalarWorks) {
  215. Fq2Elem expected = {{0x28f2f1dd, 0x2cb2b611, 0xa24767b3, 0x4e880c0e,
  216. 0xed7f7b9e, 0x6ff4a7f2, 0x25fb15d0, 0x7b8c4fed},
  217. {0}};
  218. Fq2Elem left = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
  219. 0x4780716c, 0xffd94b0f, 0x5e643124},
  220. {0}};
  221. FqElem scalar = {0x848cdb73, 0x6399829e, 0xcaa20cc0, 0x1b02bff6,
  222. 0x2b477bd2, 0xf9d48534, 0xff7929a0, 0xd4745161};
  223. Fq2Elem actual = {0};
  224. Fq2MulScalar(&actual, &left, &scalar);
  225. EXPECT_EQ(expected, actual);
  226. }
  227. TEST(TinyFq2Test, Fq2MulScalarWorksInPlace) {
  228. Fq2Elem expected = {{0x28f2f1dd, 0x2cb2b611, 0xa24767b3, 0x4e880c0e,
  229. 0xed7f7b9e, 0x6ff4a7f2, 0x25fb15d0, 0x7b8c4fed},
  230. {0}};
  231. Fq2Elem left = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
  232. 0x4780716c, 0xffd94b0f, 0x5e643124},
  233. {0}};
  234. FqElem scalar = {0x848cdb73, 0x6399829e, 0xcaa20cc0, 0x1b02bff6,
  235. 0x2b477bd2, 0xf9d48534, 0xff7929a0, 0xd4745161};
  236. Fq2MulScalar(&left, &left, &scalar);
  237. EXPECT_EQ(expected, left);
  238. }
  239. ////////////////////////////////////////////////////////////////////////
  240. // Fq2CondSet
  241. TEST(TinyFq2Test, Fq2CondSetWorksForTrue) {
  242. Fq2Elem a = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
  243. 0x4780716c, 0xffd94b0f, 0x5e64364},
  244. {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651,
  245. 0x1bb8f3e0, 0x1e1f4181, 0x8aa45bf5}};
  246. Fq2Elem b = {{0, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 1, 0}};
  247. Fq2Elem result = {0};
  248. Fq2CondSet(&result, &a, &b, true);
  249. EXPECT_EQ(a, result);
  250. }
  251. TEST(TinyFq2Test, Fq2CondSetWorksForFalse) {
  252. Fq2Elem a = {{0x22cfd6a2, 0x23e82f1e, 0xd50e1450, 0xe853e88c, 0xafa65357,
  253. 0x4780716c, 0xffd94b0f, 0x5e64364},
  254. {0x4d23497f, 0x189daf4d, 0x0ac5c478, 0x3583e2b0, 0x34dd5651,
  255. 0x1bb8f3e0, 0x1e1f4181, 0x8aa45bf5}};
  256. Fq2Elem b = {{0, 0, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 1, 0}};
  257. Fq2Elem result = {0};
  258. Fq2CondSet(&result, &a, &b, false);
  259. EXPECT_EQ(b, result);
  260. }
  261. ////////////////////////////////////////////////////////////////////////
  262. // Fq2Eq
  263. TEST(TinyFq2Test, Fq2EqPasses) {
  264. FqElem fq_a = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  265. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  266. FqElem fq_b = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  267. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  268. Fq2Elem a = {fq_a, fq_a};
  269. Fq2Elem b = {fq_b, fq_b};
  270. EXPECT_TRUE(Fq2Eq(&a, &b));
  271. }
  272. TEST(TinyFq2Test, Fq2EqFails) {
  273. FqElem fq_a = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  274. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  275. FqElem fq_b = {{0xAED33012, 0xD3292DDB, 0x12980A82, 0x0CDC65FB, 0xEE71A49F,
  276. 0x46E5F25E, 0xFFFCF0CD, 0xFFFFFFFF}};
  277. Fq2Elem a = {fq_a, fq_a};
  278. Fq2Elem b = {fq_b, fq_b};
  279. --b.x1.limbs.word[5];
  280. EXPECT_FALSE(Fq2Eq(&a, &b));
  281. }
  282. ////////////////////////////////////////////////////////////////////////
  283. // Fq2MulXi
  284. TEST(TinyFq2Test, Fq2MultXiWorks) {
  285. const Fq2Elem a = {{0xbca2b7aa, 0xc0e43294, 0x6199e561, 0xefdb7a39,
  286. 0xd57bcbba, 0x03154f2a, 0xdf9e1797, 0xf52d29c1},
  287. {0x77cb909b, 0x906d8657, 0xfea2ffb3, 0x7810e964,
  288. 0x022e47c1, 0x862bdbe6, 0xe4f5d59b, 0xa677247d}};
  289. const Fq2Elem expected = {{0x52a6aea6, 0x1e31b0f6, 0xb1f8c08d, 0x5ac9a512,
  290. 0xba57ab15, 0x3918d010, 0xda4968c5, 0x43e32f05},
  291. {0x4e9378ba, 0x3b6ce38c, 0x39afcfc3, 0xc644810d,
  292. 0xfcf511ff, 0x81a12238, 0xa98fe133, 0x421b72bd}};
  293. Fq2Elem res;
  294. Fq2MulXi(&res, &a);
  295. EXPECT_EQ(expected, res);
  296. }
  297. ////////////////////////////////////////////////////////////////////////
  298. // Fq2IsZero
  299. TEST(TinyFq2Test, Fq2IsZeroPasses) {
  300. Fq2Elem zero = {0};
  301. EXPECT_TRUE(Fq2IsZero(&zero));
  302. }
  303. TEST(TinyFq2Test, Fq2IsZeroFails) {
  304. Fq2Elem non_zero = {0};
  305. ++non_zero.x0.limbs.word[6];
  306. EXPECT_FALSE(Fq2IsZero(&non_zero));
  307. }
  308. } // namespace