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.
 
 
 
 
 

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