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.
 
 
 
 
 

779 lines
22 KiB

  1. /* Copyright (c) 2014 Cryptography Research, Inc.
  2. * Released under the MIT License. See LICENSE.txt for license information.
  3. */
  4. #include "word.h"
  5. #include <sys/time.h>
  6. #include <sys/types.h>
  7. #include <stdio.h>
  8. #include <memory.h>
  9. #include "field.h"
  10. #include "ec_point.h"
  11. #include "scalarmul.h"
  12. #include "barrett_field.h"
  13. #include "crandom.h"
  14. #include "goldilocks.h"
  15. #include "sha512.h"
  16. static __inline__ void
  17. ignore_result ( int result ) {
  18. (void)result;
  19. }
  20. static double now(void) {
  21. struct timeval tv;
  22. gettimeofday(&tv, NULL);
  23. return tv.tv_sec + tv.tv_usec/1000000.0;
  24. }
  25. static void field_randomize( struct crandom_state_t *crand, field_a_t a ) {
  26. crandom_generate(crand, (unsigned char *)a, sizeof(*a));
  27. field_strong_reduce(a);
  28. }
  29. static void q448_randomize( struct crandom_state_t *crand, word_t sk[SCALAR_WORDS] ) {
  30. crandom_generate(crand, (unsigned char *)sk, SCALAR_BYTES);
  31. }
  32. static void field_print( const char *descr, const field_a_t a ) {
  33. int j;
  34. unsigned char ser[FIELD_BYTES];
  35. field_serialize(ser,a);
  36. printf("%s = 0x", descr);
  37. for (j=FIELD_BYTES - 1; j>=0; j--) {
  38. printf("%02x", ser[j]);
  39. }
  40. printf("\n");
  41. }
  42. static void __attribute__((unused))
  43. field_print_full (
  44. const char *descr,
  45. const field_a_t a
  46. ) {
  47. int j;
  48. printf("%s = 0x", descr);
  49. for (j=15; j>=0; j--) {
  50. printf("%02" PRIxWORD "_" PRIxWORD56 " ",
  51. a->limb[j]>>28, a->limb[j]&((1<<28)-1));
  52. }
  53. printf("\n");
  54. }
  55. static void q448_print( const char *descr, const word_t secret[SCALAR_WORDS] ) {
  56. int j;
  57. printf("%s = 0x", descr);
  58. for (j=SCALAR_WORDS-1; j>=0; j--) {
  59. printf(PRIxWORDfull, secret[j]);
  60. }
  61. printf("\n");
  62. }
  63. #ifndef N_TESTS_BASE
  64. #define N_TESTS_BASE 10000
  65. #endif
  66. int main(int argc, char **argv) {
  67. (void)argc;
  68. (void)argv;
  69. struct tw_extensible_t ext;
  70. struct extensible_t exta;
  71. struct tw_niels_t niels;
  72. struct tw_pniels_t pniels;
  73. struct affine_t affine;
  74. struct montgomery_t mb;
  75. struct montgomery_aux_t mba;
  76. field_a_t a,b,c,d;
  77. double when;
  78. int i;
  79. int nbase = N_TESTS_BASE;
  80. /* Bad randomness so we can debug. */
  81. char initial_seed[32];
  82. for (i=0; i<32; i++) initial_seed[i] = i;
  83. struct crandom_state_t crand;
  84. crandom_init_from_buffer(&crand, initial_seed);
  85. /* For testing the performance drop from the crandom debuffering change.
  86. ignore_result(crandom_init_from_file(&crand, "/dev/urandom", 10000, 1));
  87. */
  88. word_t sk[SCALAR_WORDS],tk[SCALAR_WORDS];
  89. q448_randomize(&crand, sk);
  90. memset(a,0,sizeof(a));
  91. memset(b,0,sizeof(b));
  92. memset(c,0,sizeof(c));
  93. memset(d,0,sizeof(d));
  94. when = now();
  95. for (i=0; i<nbase*5000; i++) {
  96. field_mul(c, b, a);
  97. }
  98. when = now() - when;
  99. printf("mul: %5.1fns\n", when * 1e9 / i);
  100. when = now();
  101. for (i=0; i<nbase*5000; i++) {
  102. field_sqr(c, a);
  103. }
  104. when = now() - when;
  105. printf("sqr: %5.1fns\n", when * 1e9 / i);
  106. when = now();
  107. for (i=0; i<nbase*5000; i++) {
  108. field_mulw(c, b, 1234562);
  109. }
  110. when = now() - when;
  111. printf("mulw: %5.1fns\n", when * 1e9 / i);
  112. when = now();
  113. for (i=0; i<nbase*500; i++) {
  114. field_mul(c, b, a);
  115. field_mul(a, b, c);
  116. }
  117. when = now() - when;
  118. printf("mul dep: %5.1fns\n", when * 1e9 / i / 2);
  119. when = now();
  120. for (i=0; i<nbase*10; i++) {
  121. field_randomize(&crand, a);
  122. }
  123. when = now() - when;
  124. printf("rand448: %5.1fns\n", when * 1e9 / i);
  125. sha512_ctx_a_t sha;
  126. uint8_t hashout[128];
  127. when = now();
  128. for (i=0; i<nbase; i++) {
  129. sha512_init(sha);
  130. sha512_final(sha, hashout);
  131. }
  132. when = now() - when;
  133. printf("sha512 1blk: %5.1fns\n", when * 1e9 / i);
  134. when = now();
  135. for (i=0; i<nbase; i++) {
  136. sha512_update(sha, hashout, 128);
  137. }
  138. when = now() - when;
  139. printf("sha512 blk: %5.1fns (%0.2f MB/s)\n", when * 1e9 / i, 128*i/when/1e6);
  140. when = now();
  141. for (i=0; i<nbase; i++) {
  142. field_isr(c, a);
  143. }
  144. when = now() - when;
  145. printf("isr auto: %5.1fµs\n", when * 1e6 / i);
  146. for (i=0; i<100; i++) {
  147. field_randomize(&crand, a);
  148. field_isr(d,a);
  149. field_sqr(b,d);
  150. field_mul(c,b,a);
  151. field_sqr(b,c);
  152. field_subw(b,1);
  153. if (!field_is_zero(b)) {
  154. printf("ISR validation failure!\n");
  155. field_print("a", a);
  156. field_print("s", d);
  157. }
  158. }
  159. when = now();
  160. for (i=0; i<nbase; i++) {
  161. elligator_2s_inject(&affine, a);
  162. }
  163. when = now() - when;
  164. printf("elligator: %5.1fµs\n", when * 1e6 / i);
  165. for (i=0; i<100; i++) {
  166. field_randomize(&crand, a);
  167. elligator_2s_inject(&affine, a);
  168. if (!validate_affine(&affine)) {
  169. printf("Elligator validation failure!\n");
  170. field_print("a", a);
  171. field_print("x", affine.x);
  172. field_print("y", affine.y);
  173. }
  174. }
  175. when = now();
  176. for (i=0; i<nbase; i++) {
  177. deserialize_affine(&affine, a);
  178. }
  179. when = now() - when;
  180. printf("decompress: %5.1fµs\n", when * 1e6 / i);
  181. convert_affine_to_extensible(&exta, &affine);
  182. when = now();
  183. for (i=0; i<nbase; i++) {
  184. serialize_extensible(a, &exta);
  185. }
  186. when = now() - when;
  187. printf("compress: %5.1fµs\n", when * 1e6 / i);
  188. int goods = 0;
  189. for (i=0; i<100; i++) {
  190. field_randomize(&crand, a);
  191. mask_t good = deserialize_affine(&affine, a);
  192. if (good & !validate_affine(&affine)) {
  193. printf("Deserialize validation failure!\n");
  194. field_print("a", a);
  195. field_print("x", affine.x);
  196. field_print("y", affine.y);
  197. } else if (good) {
  198. goods++;
  199. convert_affine_to_extensible(&exta,&affine);
  200. serialize_extensible(b, &exta);
  201. field_sub(c,b,a);
  202. if (!field_is_zero(c)) {
  203. printf("Reserialize validation failure!\n");
  204. field_print("a", a);
  205. field_print("x", affine.x);
  206. field_print("y", affine.y);
  207. deserialize_affine(&affine, b);
  208. field_print("b", b);
  209. field_print("x", affine.x);
  210. field_print("y", affine.y);
  211. printf("\n");
  212. }
  213. }
  214. }
  215. if (goods<i/3) {
  216. printf("Deserialization validation failure! Deserialized %d/%d points\n", goods, i);
  217. }
  218. word_t lsk[768/WORD_BITS];
  219. crandom_generate(&crand, (unsigned char *)lsk, sizeof(lsk));
  220. when = now();
  221. for (i=0; i<nbase*100; i++) {
  222. barrett_reduce(lsk,sizeof(lsk)/sizeof(word_t),0,&curve_prime_order);
  223. }
  224. when = now() - when;
  225. printf("barrett red: %5.1fns\n", when * 1e9 / i);
  226. when = now();
  227. for (i=0; i<nbase*10; i++) {
  228. barrett_mac(lsk,SCALAR_WORDS,lsk,SCALAR_WORDS,lsk,SCALAR_WORDS,&curve_prime_order);
  229. }
  230. when = now() - when;
  231. printf("barrett mac: %5.1fns\n", when * 1e9 / i);
  232. memset(&ext,0,sizeof(ext));
  233. memset(&niels,0,sizeof(niels)); /* avoid assertions in p521 even though this isn't a valid ext or niels */
  234. tw_extended_a_t ed;
  235. convert_tw_extensible_to_tw_extended(ed,&ext);
  236. when = now();
  237. for (i=0; i<nbase*100; i++) {
  238. add_tw_niels_to_tw_extensible(&ext, &niels);
  239. }
  240. when = now() - when;
  241. printf("exti+niels: %5.1fns\n", when * 1e9 / i);
  242. when = now();
  243. for (i=0; i<nbase*100; i++) {
  244. add_tw_extended(ed,ed);
  245. }
  246. when = now() - when;
  247. printf("txt + txt : %5.1fns\n", when * 1e9 / i);
  248. convert_tw_extensible_to_tw_pniels(&pniels, &ext);
  249. when = now();
  250. for (i=0; i<nbase*100; i++) {
  251. add_tw_pniels_to_tw_extensible(&ext, &pniels);
  252. }
  253. when = now() - when;
  254. printf("exti+pniels: %5.1fns\n", when * 1e9 / i);
  255. when = now();
  256. for (i=0; i<nbase*100; i++) {
  257. double_tw_extensible(&ext);
  258. }
  259. when = now() - when;
  260. printf("exti dbl: %5.1fns\n", when * 1e9 / i);
  261. when = now();
  262. for (i=0; i<nbase*100; i++) {
  263. untwist_and_double(&exta, &ext);
  264. }
  265. when = now() - when;
  266. printf("i->a isog: %5.1fns\n", when * 1e9 / i);
  267. when = now();
  268. for (i=0; i<nbase*100; i++) {
  269. twist_and_double(&ext, &exta);
  270. }
  271. when = now() - when;
  272. printf("a->i isog: %5.1fns\n", when * 1e9 / i);
  273. memset(&mb,0,sizeof(mb));
  274. when = now();
  275. for (i=0; i<nbase*100; i++) {
  276. montgomery_step(&mb);
  277. }
  278. when = now() - when;
  279. printf("monty step: %5.1fns\n", when * 1e9 / i);
  280. memset(&mba,0,sizeof(mba));
  281. when = now();
  282. for (i=0; i<nbase*100; i++) {
  283. montgomery_aux_step(&mba);
  284. }
  285. when = now() - when;
  286. printf("monty aux: %5.1fns\n", when * 1e9 / i);
  287. when = now();
  288. for (i=0; i<nbase/10; i++) {
  289. ignore_result(montgomery_ladder(a,b,sk,FIELD_BITS,0));
  290. }
  291. when = now() - when;
  292. printf("full ladder: %5.1fµs\n", when * 1e6 / i);
  293. when = now();
  294. for (i=0; i<nbase/10; i++) {
  295. ignore_result(decaf_montgomery_ladder(a,b,sk,FIELD_BITS));
  296. }
  297. when = now() - when;
  298. printf("decafladder: %5.1fµs\n", when * 1e6 / i);
  299. when = now();
  300. for (i=0; i<nbase/10; i++) {
  301. scalarmul(&ext,sk);
  302. }
  303. when = now() - when;
  304. printf("edwards smz: %5.1fµs\n", when * 1e6 / i);
  305. when = now();
  306. for (i=0; i<nbase/10; i++) {
  307. scalarmul_vlook(&ext,sk);
  308. }
  309. when = now() - when;
  310. printf("edwards svl: %5.1fµs\n", when * 1e6 / i);
  311. when = now();
  312. for (i=0; i<nbase/10; i++) {
  313. scalarmul_ed(ed,sk);
  314. }
  315. when = now() - when;
  316. printf("edwards txt: %5.1fµs\n", when * 1e6 / i);
  317. field_set_ui(a,0);
  318. when = now();
  319. for (i=0; i<nbase/10; i++) {
  320. ignore_result(decaf_deserialize_tw_extended(ed,a,-1));
  321. scalarmul_ed(ed,sk);
  322. decaf_serialize_tw_extended(a,ed);
  323. }
  324. when = now() - when;
  325. printf("simple ECDH: %5.1fµs\n", when * 1e6 / i);
  326. when = now();
  327. for (i=0; i<nbase/10; i++) {
  328. scalarmul(&ext,sk);
  329. untwist_and_double_and_serialize(a,&ext);
  330. }
  331. when = now() - when;
  332. printf("edwards smc: %5.1fµs\n", when * 1e6 / i);
  333. when = now();
  334. for (i=0; i<nbase/10; i++) {
  335. q448_randomize(&crand, sk);
  336. scalarmul_vt(&ext,sk,SCALAR_BITS);
  337. }
  338. when = now() - when;
  339. printf("edwards vtm: %5.1fµs\n", when * 1e6 / i);
  340. tw_niels_a_t wnaft[1<<6];
  341. when = now();
  342. for (i=0; i<nbase/10; i++) {
  343. ignore_result(precompute_fixed_base_wnaf(wnaft,&ext,6));
  344. }
  345. when = now() - when;
  346. printf("wnaf6 pre: %5.1fµs\n", when * 1e6 / i);
  347. when = now();
  348. for (i=0; i<nbase/10; i++) {
  349. q448_randomize(&crand, sk);
  350. scalarmul_fixed_base_wnaf_vt(&ext,sk,SCALAR_BITS,(const tw_niels_a_t*)wnaft,6);
  351. }
  352. when = now() - when;
  353. printf("edwards vt6: %5.1fµs\n", when * 1e6 / i);
  354. when = now();
  355. for (i=0; i<nbase/10; i++) {
  356. ignore_result(precompute_fixed_base_wnaf(wnaft,&ext,4));
  357. }
  358. when = now() - when;
  359. printf("wnaf4 pre: %5.1fµs\n", when * 1e6 / i);
  360. when = now();
  361. for (i=0; i<nbase/10; i++) {
  362. q448_randomize(&crand, sk);
  363. scalarmul_fixed_base_wnaf_vt(&ext,sk,SCALAR_BITS,(const tw_niels_a_t*)wnaft,4);
  364. }
  365. when = now() - when;
  366. printf("edwards vt4: %5.1fµs\n", when * 1e6 / i);
  367. when = now();
  368. for (i=0; i<nbase/10; i++) {
  369. ignore_result(precompute_fixed_base_wnaf(wnaft,&ext,5));
  370. }
  371. when = now() - when;
  372. printf("wnaf5 pre: %5.1fµs\n", when * 1e6 / i);
  373. when = now();
  374. for (i=0; i<nbase/10; i++) {
  375. q448_randomize(&crand, sk);
  376. scalarmul_fixed_base_wnaf_vt(&ext,sk,SCALAR_BITS,(const tw_niels_a_t*)wnaft,5);
  377. }
  378. when = now() - when;
  379. printf("edwards vt5: %5.1fµs\n", when * 1e6 / i);
  380. when = now();
  381. for (i=0; i<nbase/10; i++) {
  382. q448_randomize(&crand, sk);
  383. q448_randomize(&crand, tk);
  384. linear_combo_var_fixed_vt(&ext,sk,FIELD_BITS,tk,FIELD_BITS,(const tw_niels_a_t*)wnaft,5);
  385. }
  386. when = now() - when;
  387. printf("vt vf combo: %5.1fµs\n", when * 1e6 / i);
  388. when = now();
  389. for (i=0; i<nbase/10; i++) {
  390. deserialize_affine(&affine, a);
  391. convert_affine_to_extensible(&exta,&affine);
  392. twist_and_double(&ext,&exta);
  393. scalarmul(&ext,sk);
  394. untwist_and_double(&exta,&ext);
  395. serialize_extensible(b, &exta);
  396. }
  397. when = now() - when;
  398. printf("edwards sm: %5.1fµs\n", when * 1e6 / i);
  399. struct fixed_base_table_t t_5_5_18, t_3_5_30, t_8_4_14, t_5_3_30, t_15_3_10;
  400. while (1) {
  401. field_randomize(&crand, a);
  402. if (deserialize_affine(&affine, a)) break;
  403. }
  404. convert_affine_to_extensible(&exta,&affine);
  405. twist_and_double(&ext,&exta);
  406. when = now();
  407. for (i=0; i<nbase/10; i++) {
  408. if (i) destroy_fixed_base(&t_5_5_18);
  409. ignore_result(precompute_fixed_base(&t_5_5_18, &ext, 5, 5, 18, NULL));
  410. }
  411. when = now() - when;
  412. printf("pre(5,5,18): %5.1fµs\n", when * 1e6 / i);
  413. when = now();
  414. for (i=0; i<nbase/10; i++) {
  415. if (i) destroy_fixed_base(&t_3_5_30);
  416. ignore_result(precompute_fixed_base(&t_3_5_30, &ext, 3, 5, 30, NULL));
  417. }
  418. when = now() - when;
  419. printf("pre(3,5,30): %5.1fµs\n", when * 1e6 / i);
  420. when = now();
  421. for (i=0; i<nbase/10; i++) {
  422. if (i) destroy_fixed_base(&t_5_3_30);
  423. ignore_result(precompute_fixed_base(&t_5_3_30, &ext, 5, 3, 30, NULL));
  424. }
  425. when = now() - when;
  426. printf("pre(5,3,30): %5.1fµs\n", when * 1e6 / i);
  427. when = now();
  428. for (i=0; i<nbase/10; i++) {
  429. if (i) destroy_fixed_base(&t_15_3_10);
  430. ignore_result(precompute_fixed_base(&t_15_3_10, &ext, 15, 3, 10, NULL));
  431. }
  432. when = now() - when;
  433. printf("pre(15,3,10):%5.1fµs\n", when * 1e6 / i);
  434. when = now();
  435. for (i=0; i<nbase/10; i++) {
  436. if (i) destroy_fixed_base(&t_8_4_14);
  437. ignore_result(precompute_fixed_base(&t_8_4_14, &ext, 8, 4, 14, NULL));
  438. }
  439. when = now() - when;
  440. printf("pre(8,4,14): %5.1fµs\n", when * 1e6 / i);
  441. when = now();
  442. for (i=0; i<nbase; i++) {
  443. scalarmul_fixed_base(&ext, sk, FIELD_BITS, &t_5_5_18);
  444. }
  445. when = now() - when;
  446. printf("com(5,5,18): %5.1fµs\n", when * 1e6 / i);
  447. when = now();
  448. for (i=0; i<nbase; i++) {
  449. scalarmul_fixed_base(&ext, sk, FIELD_BITS, &t_3_5_30);
  450. }
  451. when = now() - when;
  452. printf("com(3,5,30): %5.1fµs\n", when * 1e6 / i);
  453. when = now();
  454. for (i=0; i<nbase; i++) {
  455. scalarmul_fixed_base(&ext, sk, FIELD_BITS, &t_8_4_14);
  456. }
  457. when = now() - when;
  458. printf("com(8,4,14): %5.1fµs\n", when * 1e6 / i);
  459. when = now();
  460. for (i=0; i<nbase; i++) {
  461. scalarmul_fixed_base(&ext, sk, FIELD_BITS, &t_5_3_30);
  462. }
  463. when = now() - when;
  464. printf("com(5,3,30): %5.1fµs\n", when * 1e6 / i);
  465. when = now();
  466. for (i=0; i<nbase; i++) {
  467. scalarmul_fixed_base(&ext, sk, FIELD_BITS, &t_15_3_10);
  468. }
  469. when = now() - when;
  470. printf("com(15,3,10):%5.1fµs\n", when * 1e6 / i);
  471. printf("\nGoldilocks:\n");
  472. int res = goldilocks_init();
  473. assert(!res);
  474. struct goldilocks_public_key_t gpk,hpk;
  475. struct goldilocks_private_key_t gsk,hsk;
  476. when = now();
  477. for (i=0; i<nbase; i++) {
  478. if (i&1) {
  479. res = goldilocks_keygen(&gsk,&gpk);
  480. } else {
  481. res = goldilocks_keygen(&hsk,&hpk);
  482. }
  483. assert(!res);
  484. }
  485. when = now() - when;
  486. printf("keygen: %5.1fµs\n", when * 1e6 / i);
  487. uint8_t ss1[64],ss2[64];
  488. int gres1=0,gres2=0;
  489. when = now();
  490. for (i=0; i<nbase; i++) {
  491. if (i&1) {
  492. gres1 = goldilocks_shared_secret(ss1,&gsk,&hpk);
  493. } else {
  494. gres2 = goldilocks_shared_secret(ss2,&hsk,&gpk);
  495. }
  496. }
  497. when = now() - when;
  498. printf("ecdh: %5.1fµs\n", when * 1e6 / i);
  499. if (gres1 || gres2 || memcmp(ss1,ss2,64)) {
  500. printf("[FAIL] %d %d\n",gres1,gres2);
  501. printf("sk1 = ");
  502. for (i=0; i<SCALAR_BYTES; i++) {
  503. printf("%02x", gsk.opaque[i]);
  504. }
  505. printf("\nsk2 = ");
  506. for (i=0; i<SCALAR_BYTES; i++) {
  507. printf("%02x", hsk.opaque[i]);
  508. }
  509. printf("\nss1 = ");
  510. for (i=0; i<64; i++) {
  511. printf("%02x", ss1[i]);
  512. }
  513. printf("\nss2 = ");
  514. for (i=0; i<64; i++) {
  515. printf("%02x", ss2[i]);
  516. }
  517. printf("\n");
  518. }
  519. uint8_t sout[FIELD_BYTES*2];
  520. const char *message = "hello world";
  521. size_t message_len = strlen(message);
  522. when = now();
  523. for (i=0; i<nbase; i++) {
  524. res = goldilocks_sign(sout,(const unsigned char *)message,message_len,&gsk);
  525. (void)res;
  526. assert(!res);
  527. }
  528. when = now() - when;
  529. printf("sign: %5.1fµs\n", when * 1e6 / i);
  530. when = now();
  531. for (i=0; i<nbase; i++) {
  532. int ver = goldilocks_verify(sout,(const unsigned char *)message,message_len,&gpk);
  533. (void)ver;
  534. assert(!ver);
  535. }
  536. when = now() - when;
  537. printf("verify: %5.1fµs\n", when * 1e6 / i);
  538. struct goldilocks_precomputed_public_key_t *pre = NULL;
  539. when = now();
  540. for (i=0; i<nbase; i++) {
  541. goldilocks_destroy_precomputed_public_key(pre);
  542. pre = goldilocks_precompute_public_key(&gpk);
  543. }
  544. when = now() - when;
  545. printf("precompute: %5.1fµs\n", when * 1e6 / i);
  546. when = now();
  547. for (i=0; i<nbase; i++) {
  548. int ver = goldilocks_verify_precomputed(sout,(const unsigned char *)message,message_len,pre);
  549. (void)ver;
  550. assert(!ver);
  551. }
  552. when = now() - when;
  553. printf("verify pre: %5.1fµs\n", when * 1e6 / i);
  554. when = now();
  555. for (i=0; i<nbase; i++) {
  556. int ret = goldilocks_shared_secret_precomputed(ss1,&gsk,pre);
  557. (void)ret;
  558. assert(!ret);
  559. }
  560. when = now() - when;
  561. printf("ecdh pre: %5.1fµs\n", when * 1e6 / i);
  562. printf("\nTesting...\n");
  563. int failures=0, successes = 0;
  564. for (i=0; i<nbase/10; i++) {
  565. ignore_result(goldilocks_keygen(&gsk,&gpk));
  566. goldilocks_sign(sout,(const unsigned char *)message,message_len,&gsk);
  567. res = goldilocks_verify(sout,(const unsigned char *)message,message_len,&gpk);
  568. if (res) failures++;
  569. }
  570. if (failures) {
  571. printf("FAIL %d/%d signature checks!\n", failures, i);
  572. }
  573. failures=0; successes = 0;
  574. for (i=0; i<nbase/10; i++) {
  575. field_randomize(&crand, a);
  576. word_t two = 2;
  577. mask_t good = montgomery_ladder(b,a,&two,2,0);
  578. if (!good) continue;
  579. word_t x,y;
  580. crandom_generate(&crand, (unsigned char *)&x, sizeof(x));
  581. crandom_generate(&crand, (unsigned char *)&y, sizeof(y));
  582. x = (hword_t)x;
  583. y = (hword_t)y;
  584. word_t z=x*y;
  585. ignore_result(montgomery_ladder(b,a,&x,WORD_BITS,0));
  586. ignore_result(montgomery_ladder(c,b,&y,WORD_BITS,0));
  587. ignore_result(montgomery_ladder(b,a,&z,WORD_BITS,0));
  588. field_sub(d,b,c);
  589. if (!field_is_zero(d)) {
  590. printf("Odd ladder validation failure %d!\n", ++failures);
  591. field_print("a", a);
  592. printf("x=%"PRIxWORD", y=%"PRIxWORD", z=%"PRIxWORD"\n", x,y,z);
  593. field_print("c", c);
  594. field_print("b", b);
  595. printf("\n");
  596. }
  597. }
  598. failures = 0;
  599. for (i=0; i<nbase/10; i++) {
  600. mask_t good;
  601. do {
  602. field_randomize(&crand, a);
  603. good = deserialize_affine(&affine, a);
  604. } while (!good);
  605. convert_affine_to_extensible(&exta,&affine);
  606. twist_and_double(&ext,&exta);
  607. untwist_and_double(&exta,&ext);
  608. serialize_extensible(b, &exta);
  609. untwist_and_double_and_serialize(c, &ext);
  610. field_sub(d,b,c);
  611. if (good && !field_is_zero(d)){
  612. printf("Iso+serial validation failure %d!\n", ++failures);
  613. field_print("a", a);
  614. field_print("b", b);
  615. field_print("c", c);
  616. printf("\n");
  617. } else if (good) {
  618. successes ++;
  619. }
  620. }
  621. if (successes < i/3) {
  622. printf("Iso+serial variation: only %d/%d successful.\n", successes, i);
  623. }
  624. successes = failures = 0;
  625. for (i=0; i<nbase/10; i++) {
  626. field_a_t aa;
  627. struct tw_extensible_t exu,exv,exw;
  628. mask_t good;
  629. do {
  630. field_randomize(&crand, a);
  631. good = deserialize_affine(&affine, a);
  632. convert_affine_to_extensible(&exta,&affine);
  633. twist_and_double(&ext,&exta);
  634. } while (!good);
  635. do {
  636. field_randomize(&crand, aa);
  637. good = deserialize_affine(&affine, aa);
  638. convert_affine_to_extensible(&exta,&affine);
  639. twist_and_double(&exu,&exta);
  640. } while (!good);
  641. field_randomize(&crand, aa);
  642. q448_randomize(&crand, sk);
  643. if (i==0 || i==2) memset(&sk, 0, sizeof(sk));
  644. q448_randomize(&crand, tk);
  645. if (i==0 || i==1) memset(&tk, 0, sizeof(tk));
  646. copy_tw_extensible(&exv, &ext);
  647. copy_tw_extensible(&exw, &exu);
  648. scalarmul(&exv,sk);
  649. scalarmul(&exw,tk);
  650. convert_tw_extensible_to_tw_pniels(&pniels, &exw);
  651. add_tw_pniels_to_tw_extensible(&exv,&pniels);
  652. untwist_and_double(&exta,&exv);
  653. serialize_extensible(b, &exta);
  654. ignore_result(precompute_fixed_base_wnaf(wnaft,&exu,5));
  655. linear_combo_var_fixed_vt(&ext,sk,FIELD_BITS,tk,FIELD_BITS,(const tw_niels_a_t*)wnaft,5);
  656. untwist_and_double(&exta,&exv);
  657. serialize_extensible(c, &exta);
  658. field_sub(d,b,c);
  659. if (!field_is_zero(d)){
  660. printf("PreWNAF combo validation failure %d!\n", ++failures);
  661. field_print("a", a);
  662. field_print("A", aa);
  663. q448_print("s", sk);
  664. q448_print("t", tk);
  665. field_print("c", c);
  666. field_print("b", b);
  667. printf("\n\n");
  668. } else if (good) {
  669. successes ++;
  670. }
  671. }
  672. if (successes < i) {
  673. printf("PreWNAF combo variation: only %d/%d successful.\n", successes, i);
  674. }
  675. return 0;
  676. }