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.
 
 
 
 
 

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