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.
 
 
 
 
 

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