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.
 
 
 
 
 

418 lines
13 KiB

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