You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

476 lines
15 KiB

  1. #include "test.h"
  2. #include <stdio.h>
  3. #include "scalarmul.h"
  4. #include "decaf.h"
  5. #include "ec_point.h"
  6. #include "field.h"
  7. #include "crandom.h"
  8. #define STRIDE 7
  9. /* 0 = succeed, 1 = inval, -1 = fail */
  10. static int
  11. single_scalarmul_compatibility_test (
  12. const field_a_t base,
  13. const word_t *scalar,
  14. int nbits
  15. ) {
  16. struct tw_extensible_t text, work;
  17. field_a_t mont, ct, vl, vt, sced, decaf_s, decaf_m, decaf_te;
  18. int ret = 0, i;
  19. mask_t succ, succm;
  20. succ = deserialize_and_twist_approx(&text, base);
  21. succm = montgomery_ladder(mont,base,scalar,nbits,1);
  22. if (succ != succm) {
  23. youfail();
  24. printf(" Deserialize_and_twist_approx succ=%d, montgomery_ladder succ=%d\n",
  25. (int)-succ, (int)-succm);
  26. printf(" nbits = %d\n", nbits);
  27. field_print(" base", base);
  28. scalar_print(" scal", scalar, (nbits+WORD_BITS-1)/WORD_BITS);
  29. return -1;
  30. }
  31. if (!succ) {
  32. return 1;
  33. }
  34. #if FIELD_BITS == 448
  35. struct { int n,t,s; } params[] = {{5,5,18},{3,5,30},{4,4,28},{1,2,224}};
  36. #elif FIELD_BITS == 480
  37. struct { int n,t,s; } params[] = {{5,6,16},{6,5,16},{4,5,24},{4,4,30},{1,2,240}};
  38. #elif FIELD_BITS == 521
  39. struct { int n,t,s; } params[] = {{5,8,13},{4,5,26},{1,2,(SCALAR_BITS+1)/2}};
  40. #else
  41. struct { int n,t,s; } params[] = {{5,5,(SCALAR_BITS+24)/25},{1,2,(SCALAR_BITS+1)/2}};
  42. #endif
  43. const int nparams = sizeof(params)/sizeof(params[0]);
  44. struct fixed_base_table_t fbt;
  45. const int nsizes = 6;
  46. field_a_t fbout[nparams], wout[nsizes];
  47. memset(&fbt, 0, sizeof(fbt));
  48. memset(&fbout, 0, sizeof(fbout));
  49. memset(&wout, 0, sizeof(wout));
  50. /* compute using combs */
  51. for (i=0; i<nparams; i++) {
  52. int n=params[i].n, t=params[i].t, s=params[i].s;
  53. succ = precompute_fixed_base(&fbt, &text, n, t, s, NULL);
  54. if (!succ) {
  55. youfail();
  56. printf(" Failed to precompute_fixed_base(%d,%d,%d)\n", n, t, s);
  57. continue;
  58. }
  59. succ = scalarmul_fixed_base(&work, scalar, nbits, &fbt);
  60. destroy_fixed_base(&fbt);
  61. if (!succ) {
  62. youfail();
  63. printf(" Failed to scalarmul_fixed_base(%d,%d,%d)\n", n, t, s);
  64. continue;
  65. }
  66. untwist_and_double_and_serialize(fbout[i], &work);
  67. }
  68. /* compute using precomp wNAF */
  69. for (i=0; i<nsizes; i++) {
  70. tw_niels_a_t pre[1<<i];
  71. succ = precompute_fixed_base_wnaf(pre, &text, i);
  72. if (!succ) {
  73. youfail();
  74. printf(" Failed to precompute_fixed_base_wnaf(%d)\n", i);
  75. continue;
  76. }
  77. scalarmul_fixed_base_wnaf_vt(&work, scalar, nbits, (const tw_niels_a_t*)pre, i);
  78. untwist_and_double_and_serialize(wout[i], &work);
  79. }
  80. mask_t consistent = MASK_SUCCESS;
  81. if (nbits == FIELD_BITS) {
  82. /* window methods currently only work on FIELD_BITS bits. */
  83. copy_tw_extensible(&work, &text);
  84. scalarmul(&work, scalar);
  85. untwist_and_double_and_serialize(ct, &work);
  86. copy_tw_extensible(&work, &text);
  87. scalarmul_vlook(&work, scalar);
  88. untwist_and_double_and_serialize(vl, &work);
  89. copy_tw_extensible(&work, &text);
  90. scalarmul_vt(&work, scalar, nbits);
  91. untwist_and_double_and_serialize(vt, &work);
  92. decaf_448_point_t ed2, ed3;
  93. struct decaf_448_precomputed_s *dpre;
  94. posix_memalign((void**)&dpre, alignof_decaf_448_precomputed_s, sizeof_decaf_448_precomputed_s);
  95. tw_extended_a_t ed;
  96. convert_tw_extensible_to_tw_extended(ed, &text);
  97. uint8_t ser4[DECAF_448_SER_BYTES];
  98. decaf_448_point_encode(ser4, (struct decaf_448_point_s *)ed);
  99. decaf_448_point_scalarmul(
  100. ed2,
  101. (struct decaf_448_point_s *)ed,
  102. (struct decaf_448_scalar_s *)scalar
  103. );
  104. decaf_448_precompute(dpre, (struct decaf_448_point_s *)ed);
  105. decaf_448_precomputed_scalarmul(
  106. ed3,
  107. dpre,
  108. (struct decaf_448_scalar_s *)scalar
  109. );
  110. free(dpre);
  111. scalarmul_ed(ed, scalar);
  112. field_copy(work.x, ed->x);
  113. field_copy(work.y, ed->y);
  114. field_copy(work.z, ed->z);
  115. field_copy(work.t, ed->t);
  116. field_set_ui(work.u, 1);
  117. untwist_and_double_and_serialize(sced, &work);
  118. uint8_t ser1[DECAF_448_SER_BYTES], ser2[DECAF_448_SER_BYTES],
  119. ser3[DECAF_448_SER_BYTES];
  120. decaf_448_point_encode(ser1, (struct decaf_448_point_s *)ed);
  121. decaf_448_point_encode(ser2, ed2);
  122. decaf_448_point_encode(ser3, ed3);
  123. consistent &= decaf_448_direct_scalarmul(ser4, ser4, (struct decaf_448_scalar_s *)scalar, -1, -1);
  124. /* check consistency mont vs window */
  125. consistent &= field_eq(mont, ct);
  126. consistent &= field_eq(mont, vl);
  127. consistent &= field_eq(mont, vt);
  128. consistent &= field_eq(mont, sced);
  129. consistent &= memcmp(ser1,ser2,sizeof(ser1)) ? 0 : -1;
  130. consistent &= memcmp(ser1,ser3,sizeof(ser1)) ? 0 : -1;
  131. consistent &= memcmp(ser1,ser4,sizeof(ser1)) ? 0 : -1;
  132. }
  133. /* check consistency mont vs combs */
  134. for (i=0; i<nparams; i++) {
  135. consistent &= field_eq(mont,fbout[i]);
  136. }
  137. /* check consistency mont vs wNAF */
  138. for (i=0; i<nsizes; i++) {
  139. consistent &= field_eq(mont,wout[i]);
  140. }
  141. /* Do decaf */
  142. copy_tw_extensible(&work,&text);
  143. double_tw_extensible(&work);
  144. decaf_serialize_tw_extensible(decaf_s, &work);
  145. mask_t succ_dm, succ_dta;
  146. succ_dm = decaf_montgomery_ladder(decaf_m, decaf_s, scalar, nbits);
  147. succ_dta = deserialize_and_twist_approx(&work, mont);
  148. decaf_serialize_tw_extensible(decaf_te, &work);
  149. consistent &= field_eq(decaf_m, decaf_te);
  150. consistent &= succ_dm & succ_dta;
  151. /* If inconsistent, complain. */
  152. if (!consistent) {
  153. youfail();
  154. printf(" Failed scalarmul consistency test with nbits=%d.\n",nbits);
  155. field_print(" base", base);
  156. scalar_print(" scal", scalar, (nbits+WORD_BITS-1)/WORD_BITS);
  157. field_print(" mont", mont);
  158. for (i=0; i<nparams; i++) {
  159. printf(" With n=%d, t=%d, s=%d:\n", params[i].n, params[i].t, params[i].s);
  160. field_print(" out ", fbout[i]);
  161. }
  162. for (i=0; i<nsizes; i++) {
  163. printf(" With w=%d:\n",i);
  164. field_print(" wNAF", wout[i]);
  165. }
  166. if (nbits == FIELD_BITS) {
  167. field_print(" ct ", ct);
  168. field_print(" vl ", vl);
  169. field_print(" vt ", vt);
  170. field_print(" ed ", sced);
  171. }
  172. printf("decaf: succ = %d, %d\n", (int)succ_dm, (int)succ_dta);
  173. field_print(" s0", decaf_s);
  174. field_print(" dm", decaf_m);
  175. field_print(" dt", decaf_te);
  176. ret = -1;
  177. }
  178. return ret;
  179. }
  180. static int
  181. single_linear_combo_test (
  182. const field_a_t base1,
  183. const word_t *scalar1,
  184. int nbits1,
  185. const field_a_t base2,
  186. const word_t *scalar2,
  187. int nbits2
  188. ) {
  189. struct tw_extensible_t text1, text2, working;
  190. struct tw_pniels_t pn;
  191. field_a_t result_comb, result_combo, result_wnaf;
  192. mask_t succ =
  193. deserialize_and_twist_approx(&text1, base1)
  194. & deserialize_and_twist_approx(&text2, base2);
  195. if (!succ) return 1;
  196. struct fixed_base_table_t t1, t2;
  197. tw_niels_a_t wnaf[32];
  198. memset(&t1,0,sizeof(t1));
  199. memset(&t2,0,sizeof(t2));
  200. succ = precompute_fixed_base(&t1, &text1, 5, 5, 18, NULL); // FIELD_MAGIC
  201. succ &= precompute_fixed_base(&t2, &text2, 6, 3, 25, NULL); // FIELD_MAGIC
  202. succ &= precompute_fixed_base_wnaf(wnaf, &text2, 5);
  203. if (!succ) {
  204. destroy_fixed_base(&t1);
  205. destroy_fixed_base(&t2);
  206. return -1;
  207. }
  208. /* use the dedicated wNAF linear combo algorithm */
  209. copy_tw_extensible(&working, &text1);
  210. linear_combo_var_fixed_vt(&working, scalar1, nbits1, scalar2, nbits2, (const tw_niels_a_t*)wnaf, 5);
  211. untwist_and_double_and_serialize(result_wnaf, &working);
  212. /* use the dedicated combs algorithm */
  213. succ &= linear_combo_combs_vt(&working, scalar1, nbits1, &t1, scalar2, nbits2, &t2);
  214. untwist_and_double_and_serialize(result_combo, &working);
  215. /* use two combs */
  216. succ &= scalarmul_fixed_base(&working, scalar1, nbits1, &t1);
  217. convert_tw_extensible_to_tw_pniels(&pn, &working);
  218. succ &= scalarmul_fixed_base(&working, scalar2, nbits2, &t2);
  219. add_tw_pniels_to_tw_extensible(&working, &pn);
  220. untwist_and_double_and_serialize(result_comb, &working);
  221. mask_t consistent = MASK_SUCCESS;
  222. consistent &= field_eq(result_combo, result_wnaf);
  223. consistent &= field_eq(result_comb, result_wnaf);
  224. if (!succ || !consistent) {
  225. youfail();
  226. printf(" Failed linear combo consistency test with nbits=%d,%d.\n",nbits1,nbits2);
  227. field_print(" base1", base1);
  228. scalar_print(" scal1", scalar1, (nbits1+WORD_BITS-1)/WORD_BITS);
  229. field_print(" base2", base2);
  230. scalar_print(" scal2", scalar2, (nbits1+WORD_BITS-1)/WORD_BITS);
  231. field_print(" combs", result_comb);
  232. field_print(" combo", result_combo);
  233. field_print(" wNAFs", result_wnaf);
  234. return -1;
  235. }
  236. destroy_fixed_base(&t1);
  237. destroy_fixed_base(&t2);
  238. return 0;
  239. }
  240. /* 0 = succeed, 1 = inval, -1 = fail */
  241. static int
  242. single_scalarmul_commutativity_test (
  243. const field_a_t base,
  244. const word_t *scalar1,
  245. int nbits1,
  246. int ned1,
  247. const word_t *scalar2,
  248. int nbits2,
  249. int ned2
  250. ) {
  251. field_a_t m12, m21, tmp1, tmp2;
  252. mask_t succ12a = montgomery_ladder(tmp1,base,scalar1,nbits1,ned1);
  253. mask_t succ12b = montgomery_ladder(m12,tmp1,scalar2,nbits2,ned2);
  254. mask_t succ21a = montgomery_ladder(tmp2,base,scalar2,nbits2,ned2);
  255. mask_t succ21b = montgomery_ladder(m21,tmp2,scalar1,nbits1,ned1);
  256. mask_t succ12 = succ12a & succ12b, succ21 = succ21a & succ21b;
  257. if (succ12 != succ21) {
  258. youfail();
  259. printf(" Failed scalarmul commutativity test with (nbits,ned) = (%d,%d), (%d,%d).\n",
  260. nbits1,ned1,nbits2,ned2);
  261. field_print(" base", base);
  262. field_print(" tmp1", tmp1);
  263. field_print(" tmp2", tmp2);
  264. scalar_print(" sca1", scalar1, (nbits1+WORD_BITS-1)/WORD_BITS);
  265. scalar_print(" sca2", scalar2, (nbits1+WORD_BITS-1)/WORD_BITS);
  266. printf(" good = ((%d,%d),(%d,%d))\n", (int)-succ12a,
  267. (int)-succ12b, (int)-succ21a, (int)-succ21b);
  268. return -1;
  269. } else if (!succ12) {
  270. // printf(" (nbits,ned) = (%d,%d), (%d,%d).\n", nbits1,ned1,nbits2,ned2);
  271. // printf(" succ = (%d,%d), (%d,%d).\n", (int)-succ12a, (int)-succ12b, (int)-succ21a, (int)-succ21b);
  272. return 1;
  273. }
  274. mask_t consistent = field_eq(m12,m21);
  275. if (consistent) {
  276. return 0;
  277. } else {
  278. youfail();
  279. printf(" Failed scalarmul commutativity test with (nbits,ned) = (%d,%d), (%d,%d).\n",
  280. nbits1,ned1,nbits2,ned2);
  281. field_print(" base", base);
  282. scalar_print(" sca1", scalar1, (nbits1+WORD_BITS-1)/WORD_BITS);
  283. scalar_print(" sca2", scalar2, (nbits1+WORD_BITS-1)/WORD_BITS);
  284. field_print(" m12 ", m12);
  285. field_print(" m21 ", m21);
  286. return -1;
  287. }
  288. }
  289. static void crandom_generate_f(struct crandom_state_t *crand, uint8_t *scalar, int n) {
  290. crandom_generate(crand, scalar, n);
  291. int i;
  292. for (i = FIELD_BYTES; i<n; i++) {
  293. scalar[i] = 0;
  294. }
  295. #if (FIELD_BITS % 8)
  296. if (n >= FIELD_BYTES) {
  297. scalar[FIELD_BYTES-1] &= (1<<(FIELD_BITS%8)) - 1;
  298. }
  299. #endif
  300. }
  301. int test_scalarmul_commutativity (void) {
  302. int i,j,k,got;
  303. struct crandom_state_t crand;
  304. crandom_init_from_buffer(&crand, "scalarmul_commutativity_test RNG");
  305. for (i=0; i<=FIELD_BITS; i+=STRIDE) {
  306. for (j=0; j<=FIELD_BITS; j+=STRIDE) {
  307. got = 0;
  308. for (k=0; k<128 && !got; k++) {
  309. uint8_t ser[FIELD_BYTES];
  310. word_t scalar1[SCALAR_WORDS], scalar2[SCALAR_WORDS];
  311. crandom_generate_f(&crand, ser, sizeof(ser));
  312. crandom_generate(&crand, (uint8_t *)scalar1, sizeof(scalar1));
  313. crandom_generate(&crand, (uint8_t *)scalar2, sizeof(scalar2));
  314. field_t base;
  315. mask_t succ = field_deserialize(&base, ser);
  316. if (!succ) continue;
  317. int ret = single_scalarmul_commutativity_test (&base, scalar1, i, i%3, scalar2, j, j%3);
  318. got = !ret;
  319. if (ret == -1) return -1;
  320. }
  321. if (!got) {
  322. youfail();
  323. printf(" Unlikely: rejected 128 scalars in a row.\n");
  324. return -1;
  325. }
  326. }
  327. }
  328. return 0;
  329. }
  330. int test_linear_combo (void) {
  331. int i,j,k,got;
  332. struct crandom_state_t crand;
  333. crandom_init_from_buffer(&crand, "scalarmul_linear_combos_test RNG");
  334. for (i=0; i<=FIELD_BITS; i+=STRIDE) {
  335. for (j=0; j<=FIELD_BITS; j+=STRIDE) {
  336. got = 0;
  337. for (k=0; k<128 && !got; k++) {
  338. uint8_t ser[FIELD_BYTES];
  339. word_t scalar1[SCALAR_WORDS], scalar2[SCALAR_WORDS];
  340. crandom_generate(&crand, (uint8_t *)scalar1, sizeof(scalar1));
  341. crandom_generate(&crand, (uint8_t *)scalar2, sizeof(scalar2));
  342. field_t base1;
  343. crandom_generate_f(&crand, ser, sizeof(ser));
  344. mask_t succ = field_deserialize(&base1, ser);
  345. if (!succ) continue;
  346. field_t base2;
  347. crandom_generate(&crand, ser, sizeof(ser));
  348. succ = field_deserialize(&base2, ser);
  349. if (!succ) continue;
  350. int ret = single_linear_combo_test (&base1, scalar1, i, &base2, scalar2, j);
  351. got = !ret;
  352. if (ret == -1) return -1;
  353. }
  354. if (!got) {
  355. youfail();
  356. printf(" Unlikely: rejected 128 scalars in a row.\n");
  357. return -1;
  358. }
  359. }
  360. }
  361. return 0;
  362. }
  363. int test_scalarmul_compatibility (void) {
  364. int i,j,k,got;
  365. struct crandom_state_t crand;
  366. crandom_init_from_buffer(&crand, "scalarmul_compatibility_test RNG");
  367. for (i=0; i<=FIELD_BITS; i+=STRIDE) {
  368. for (j=0; j<=20; j++) {
  369. got = 0;
  370. for (k=0; k<128 && !got; k++) {
  371. uint8_t ser[FIELD_BYTES];
  372. word_t scalar[SCALAR_WORDS];
  373. crandom_generate_f(&crand, ser, sizeof(ser));
  374. crandom_generate(&crand, (uint8_t *)scalar, sizeof(scalar));
  375. field_t base;
  376. mask_t succ = field_deserialize(&base, ser);
  377. if (!succ) continue;
  378. int ret = single_scalarmul_compatibility_test (&base, scalar, i);
  379. got = !ret;
  380. if (ret == -1) return -1;
  381. }
  382. if (!got) {
  383. youfail();
  384. printf(" Unlikely: rejected 128 scalars in a row.\n");
  385. return -1;
  386. }
  387. }
  388. }
  389. return 0;
  390. }