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.
 
 
 
 
 

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