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.
 
 
 
 
 

742 lines
21 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. #include "decaf_crypto.h"
  18. #include "shake.h"
  19. static __inline__ void
  20. ignore_result ( int result ) {
  21. (void)result;
  22. }
  23. static double now(void) {
  24. struct timeval tv;
  25. gettimeofday(&tv, NULL);
  26. return tv.tv_sec + tv.tv_usec/1000000.0;
  27. }
  28. static void field_randomize( struct crandom_state_t *crand, field_a_t a ) {
  29. crandom_generate(crand, (unsigned char *)a, sizeof(*a));
  30. field_strong_reduce(a);
  31. }
  32. static void q448_randomize( struct crandom_state_t *crand, word_t sk[SCALAR_WORDS] ) {
  33. crandom_generate(crand, (unsigned char *)sk, SCALAR_BYTES);
  34. }
  35. static void field_print( const char *descr, const field_a_t a ) {
  36. int j;
  37. unsigned char ser[FIELD_BYTES];
  38. field_serialize(ser,a);
  39. printf("%s = 0x", descr);
  40. for (j=FIELD_BYTES - 1; j>=0; j--) {
  41. printf("%02x", ser[j]);
  42. }
  43. printf("\n");
  44. }
  45. static void __attribute__((unused))
  46. field_print_full (
  47. const char *descr,
  48. const field_a_t a
  49. ) {
  50. int j;
  51. printf("%s = 0x", descr);
  52. for (j=15; j>=0; j--) {
  53. printf("%02" PRIxWORD "_" PRIxWORD56 " ",
  54. a->limb[j]>>28, a->limb[j]&((1<<28)-1));
  55. }
  56. printf("\n");
  57. }
  58. #ifndef N_TESTS_BASE
  59. #define N_TESTS_BASE 10000
  60. #endif
  61. int main(int argc, char **argv) {
  62. (void)argc;
  63. (void)argv;
  64. struct tw_extensible_t ext;
  65. struct extensible_t exta;
  66. struct tw_niels_t niels;
  67. struct tw_pniels_t pniels;
  68. struct affine_t affine;
  69. struct montgomery_t mb;
  70. struct montgomery_aux_t mba;
  71. field_a_t a,b,c,d;
  72. double when;
  73. int i;
  74. int nbase = N_TESTS_BASE;
  75. /* Bad randomness so we can debug. */
  76. char initial_seed[32];
  77. for (i=0; i<32; i++) initial_seed[i] = i;
  78. struct crandom_state_t crand;
  79. crandom_init_from_buffer(&crand, initial_seed);
  80. /* For testing the performance drop from the crandom debuffering change.
  81. ignore_result(crandom_init_from_file(&crand, "/dev/urandom", 10000, 1));
  82. */
  83. word_t sk[SCALAR_WORDS],tk[SCALAR_WORDS];
  84. q448_randomize(&crand, sk);
  85. memset(a,0,sizeof(a));
  86. memset(b,0,sizeof(b));
  87. memset(c,0,sizeof(c));
  88. memset(d,0,sizeof(d));
  89. when = now();
  90. for (i=0; i<nbase*5000; i++) {
  91. field_mul(c, b, a);
  92. }
  93. when = now() - when;
  94. printf("mul: %5.1fns\n", when * 1e9 / i);
  95. when = now();
  96. for (i=0; i<nbase*5000; i++) {
  97. field_sqr(c, a);
  98. }
  99. when = now() - when;
  100. printf("sqr: %5.1fns\n", when * 1e9 / i);
  101. when = now();
  102. for (i=0; i<nbase*5000; i++) {
  103. field_mulw(c, b, 1234562);
  104. }
  105. when = now() - when;
  106. printf("mulw: %5.1fns\n", when * 1e9 / i);
  107. when = now();
  108. for (i=0; i<nbase*500; i++) {
  109. field_mul(c, b, a);
  110. field_mul(a, b, c);
  111. }
  112. when = now() - when;
  113. printf("mul dep: %5.1fns\n", when * 1e9 / i / 2);
  114. when = now();
  115. for (i=0; i<nbase*10; i++) {
  116. field_randomize(&crand, a);
  117. }
  118. when = now() - when;
  119. printf("rand448: %5.1fns\n", when * 1e9 / i);
  120. sha512_ctx_a_t sha;
  121. uint8_t hashout[128];
  122. when = now();
  123. for (i=0; i<nbase; i++) {
  124. sha512_init(sha);
  125. sha512_final(sha, hashout);
  126. }
  127. when = now() - when;
  128. printf("sha512 1blk: %5.1fns\n", when * 1e9 / i);
  129. when = now();
  130. for (i=0; i<nbase; i++) {
  131. sha512_update(sha, hashout, 128);
  132. }
  133. when = now() - when;
  134. printf("sha512 blk: %5.1fns (%0.2f MB/s)\n", when * 1e9 / i, 128*i/when/1e6);
  135. when = now();
  136. for (i=0; i<nbase; i++) {
  137. shake256_hash(hashout,128,hashout,128);
  138. }
  139. when = now() - when;
  140. printf("shake 1blk: %5.1fns\n", when * 1e9 / i);
  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_448_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_448_scalar_mul(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_448_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_448_point_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_448_point_scalarmul(Da,Db,asc);
  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. decaf_448_point_double_scalarmul(Da,Db,bsc,Dc,asc);
  328. }
  329. when = now() - when;
  330. printf("decaf slo2: %5.1fµs\n", when * 1e6 / i);
  331. when = now();
  332. for (i=0; i<nbase/10; i++) {
  333. scalarmul(&ext,sk);
  334. }
  335. when = now() - when;
  336. printf("edwards smz: %5.1fµs\n", when * 1e6 / i);
  337. when = now();
  338. for (i=0; i<nbase/10; i++) {
  339. scalarmul_vlook(&ext,sk);
  340. }
  341. when = now() - when;
  342. printf("edwards svl: %5.1fµs\n", when * 1e6 / i);
  343. when = now();
  344. for (i=0; i<nbase/10; i++) {
  345. scalarmul_ed(ed,sk);
  346. }
  347. when = now() - when;
  348. printf("edwards txt: %5.1fµs\n", when * 1e6 / i);
  349. field_set_ui(a,0);
  350. when = now();
  351. for (i=0; i<nbase/10; i++) {
  352. ignore_result(decaf_deserialize_tw_extended(ed,a,-1));
  353. scalarmul_ed(ed,sk);
  354. decaf_serialize_tw_extended(a,ed);
  355. }
  356. when = now() - when;
  357. printf("simple ECDH: %5.1fµs\n", when * 1e6 / i);
  358. when = now();
  359. for (i=0; i<nbase/10; i++) {
  360. scalarmul(&ext,sk);
  361. untwist_and_double_and_serialize(a,&ext);
  362. }
  363. when = now() - when;
  364. printf("edwards smc: %5.1fµs\n", when * 1e6 / i);
  365. when = now();
  366. for (i=0; i<nbase/10; i++) {
  367. q448_randomize(&crand, sk);
  368. scalarmul_vt(&ext,sk,SCALAR_BITS);
  369. }
  370. when = now() - when;
  371. printf("edwards vtm: %5.1fµs\n", when * 1e6 / i);
  372. tw_niels_a_t wnaft[1<<6];
  373. when = now();
  374. for (i=0; i<nbase/10; i++) {
  375. ignore_result(precompute_fixed_base_wnaf(wnaft,&ext,6));
  376. }
  377. when = now() - when;
  378. printf("wnaf6 pre: %5.1fµs\n", when * 1e6 / i);
  379. when = now();
  380. for (i=0; i<nbase/10; i++) {
  381. q448_randomize(&crand, sk);
  382. scalarmul_fixed_base_wnaf_vt(&ext,sk,SCALAR_BITS,(const tw_niels_a_t*)wnaft,6);
  383. }
  384. when = now() - when;
  385. printf("edwards vt6: %5.1fµs\n", when * 1e6 / i);
  386. when = now();
  387. for (i=0; i<nbase/10; i++) {
  388. ignore_result(precompute_fixed_base_wnaf(wnaft,&ext,4));
  389. }
  390. when = now() - when;
  391. printf("wnaf4 pre: %5.1fµs\n", when * 1e6 / i);
  392. when = now();
  393. for (i=0; i<nbase/10; i++) {
  394. q448_randomize(&crand, sk);
  395. scalarmul_fixed_base_wnaf_vt(&ext,sk,SCALAR_BITS,(const tw_niels_a_t*)wnaft,4);
  396. }
  397. when = now() - when;
  398. printf("edwards vt4: %5.1fµs\n", when * 1e6 / i);
  399. when = now();
  400. for (i=0; i<nbase/10; i++) {
  401. ignore_result(precompute_fixed_base_wnaf(wnaft,&ext,5));
  402. }
  403. when = now() - when;
  404. printf("wnaf5 pre: %5.1fµs\n", when * 1e6 / i);
  405. when = now();
  406. for (i=0; i<nbase/10; i++) {
  407. q448_randomize(&crand, sk);
  408. scalarmul_fixed_base_wnaf_vt(&ext,sk,SCALAR_BITS,(const tw_niels_a_t*)wnaft,5);
  409. }
  410. when = now() - when;
  411. printf("edwards vt5: %5.1fµs\n", when * 1e6 / i);
  412. when = now();
  413. for (i=0; i<nbase/10; i++) {
  414. q448_randomize(&crand, sk);
  415. q448_randomize(&crand, tk);
  416. linear_combo_var_fixed_vt(&ext,sk,FIELD_BITS,tk,FIELD_BITS,(const tw_niels_a_t*)wnaft,5);
  417. }
  418. when = now() - when;
  419. printf("vt vf combo: %5.1fµs\n", when * 1e6 / i);
  420. when = now();
  421. for (i=0; i<nbase/10; i++) {
  422. deserialize_affine(&affine, a);
  423. convert_affine_to_extensible(&exta,&affine);
  424. twist_and_double(&ext,&exta);
  425. scalarmul(&ext,sk);
  426. untwist_and_double(&exta,&ext);
  427. serialize_extensible(b, &exta);
  428. }
  429. when = now() - when;
  430. printf("edwards sm: %5.1fµs\n", when * 1e6 / i);
  431. 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;
  432. while (1) {
  433. field_randomize(&crand, a);
  434. if (deserialize_affine(&affine, a)) break;
  435. }
  436. convert_affine_to_extensible(&exta,&affine);
  437. twist_and_double(&ext,&exta);
  438. when = now();
  439. for (i=0; i<nbase/10; i++) {
  440. if (i) destroy_fixed_base(&t_5_5_18);
  441. ignore_result(precompute_fixed_base(&t_5_5_18, &ext, 5, 5, 18, NULL));
  442. }
  443. when = now() - when;
  444. printf("pre(5,5,18): %5.1fµs\n", when * 1e6 / i);
  445. when = now();
  446. for (i=0; i<nbase/10; i++) {
  447. if (i) destroy_fixed_base(&t_3_5_30);
  448. ignore_result(precompute_fixed_base(&t_3_5_30, &ext, 3, 5, 30, NULL));
  449. }
  450. when = now() - when;
  451. printf("pre(3,5,30): %5.1fµs\n", when * 1e6 / i);
  452. when = now();
  453. for (i=0; i<nbase/10; i++) {
  454. if (i) destroy_fixed_base(&t_5_3_30);
  455. ignore_result(precompute_fixed_base(&t_5_3_30, &ext, 5, 3, 30, NULL));
  456. }
  457. when = now() - when;
  458. printf("pre(5,3,30): %5.1fµs\n", when * 1e6 / i);
  459. when = now();
  460. for (i=0; i<nbase/10; i++) {
  461. if (i) destroy_fixed_base(&t_15_3_10);
  462. ignore_result(precompute_fixed_base(&t_15_3_10, &ext, 15, 3, 10, NULL));
  463. }
  464. when = now() - when;
  465. printf("pre(15,3,10):%5.1fµs\n", when * 1e6 / i);
  466. when = now();
  467. for (i=0; i<nbase/10; i++) {
  468. if (i) destroy_fixed_base(&t_8_4_14);
  469. ignore_result(precompute_fixed_base(&t_8_4_14, &ext, 8, 4, 14, NULL));
  470. }
  471. when = now() - when;
  472. printf("pre(8,4,14): %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_5_5_18);
  476. }
  477. when = now() - when;
  478. printf("com(5,5,18): %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_3_5_30);
  482. }
  483. when = now() - when;
  484. printf("com(3,5,30): %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_8_4_14);
  488. }
  489. when = now() - when;
  490. printf("com(8,4,14): %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_5_3_30);
  494. }
  495. when = now() - when;
  496. printf("com(5,3,30): %5.1fµs\n", when * 1e6 / i);
  497. when = now();
  498. for (i=0; i<nbase; i++) {
  499. scalarmul_fixed_base(&ext, sk, FIELD_BITS, &t_15_3_10);
  500. }
  501. when = now() - when;
  502. printf("com(15,3,10):%5.1fµs\n", when * 1e6 / i);
  503. printf("\nGoldilocks:\n");
  504. int res = goldilocks_init();
  505. assert(!res);
  506. struct goldilocks_public_key_t gpk,hpk;
  507. struct goldilocks_private_key_t gsk,hsk;
  508. when = now();
  509. for (i=0; i<nbase; i++) {
  510. if (i&1) {
  511. res = goldilocks_keygen(&gsk,&gpk);
  512. } else {
  513. res = goldilocks_keygen(&hsk,&hpk);
  514. }
  515. assert(!res);
  516. }
  517. when = now() - when;
  518. printf("keygen: %5.1fµs\n", when * 1e6 / i);
  519. uint8_t ss1[64],ss2[64];
  520. int gres1=0,gres2=0;
  521. when = now();
  522. for (i=0; i<nbase; i++) {
  523. if (i&1) {
  524. gres1 = goldilocks_shared_secret(ss1,&gsk,&hpk);
  525. } else {
  526. gres2 = goldilocks_shared_secret(ss2,&hsk,&gpk);
  527. }
  528. }
  529. when = now() - when;
  530. printf("ecdh: %5.1fµs\n", when * 1e6 / i);
  531. if (gres1 || gres2 || memcmp(ss1,ss2,64)) {
  532. printf("[FAIL] %d %d\n",gres1,gres2);
  533. printf("sk1 = ");
  534. for (i=0; i<SCALAR_BYTES; i++) {
  535. printf("%02x", gsk.opaque[i]);
  536. }
  537. printf("\nsk2 = ");
  538. for (i=0; i<SCALAR_BYTES; i++) {
  539. printf("%02x", hsk.opaque[i]);
  540. }
  541. printf("\nss1 = ");
  542. for (i=0; i<64; i++) {
  543. printf("%02x", ss1[i]);
  544. }
  545. printf("\nss2 = ");
  546. for (i=0; i<64; i++) {
  547. printf("%02x", ss2[i]);
  548. }
  549. printf("\n");
  550. }
  551. uint8_t sout[FIELD_BYTES*2];
  552. const char *message = "hello world";
  553. size_t message_len = strlen(message);
  554. when = now();
  555. for (i=0; i<nbase; i++) {
  556. res = goldilocks_sign(sout,(const unsigned char *)message,message_len,&gsk);
  557. (void)res;
  558. assert(!res);
  559. }
  560. when = now() - when;
  561. printf("sign: %5.1fµs\n", when * 1e6 / i);
  562. when = now();
  563. for (i=0; i<nbase; i++) {
  564. int ver = goldilocks_verify(sout,(const unsigned char *)message,message_len,&gpk);
  565. (void)ver;
  566. assert(!ver);
  567. }
  568. when = now() - when;
  569. printf("verify: %5.1fµs\n", when * 1e6 / i);
  570. struct goldilocks_precomputed_public_key_t *pre = NULL;
  571. when = now();
  572. for (i=0; i<nbase; i++) {
  573. goldilocks_destroy_precomputed_public_key(pre);
  574. pre = goldilocks_precompute_public_key(&gpk);
  575. }
  576. when = now() - when;
  577. printf("precompute: %5.1fµs\n", when * 1e6 / i);
  578. when = now();
  579. for (i=0; i<nbase; i++) {
  580. int ver = goldilocks_verify_precomputed(sout,(const unsigned char *)message,message_len,pre);
  581. (void)ver;
  582. assert(!ver);
  583. }
  584. when = now() - when;
  585. printf("verify pre: %5.1fµs\n", when * 1e6 / i);
  586. when = now();
  587. for (i=0; i<nbase; i++) {
  588. int ret = goldilocks_shared_secret_precomputed(ss1,&gsk,pre);
  589. (void)ret;
  590. assert(!ret);
  591. }
  592. when = now() - when;
  593. printf("ecdh pre: %5.1fµs\n", when * 1e6 / i);
  594. printf("\nDecaf slow:\n");
  595. decaf_448_symmetric_key_t sym[2] = {{0},{1}};
  596. decaf_448_private_key_t dpriv[2];
  597. decaf_448_public_key_t dpub[2];
  598. unsigned char dshared[2][32];
  599. when = now();
  600. for (i=0; i<nbase/10; i++) {
  601. decaf_448_derive_private_key(dpriv[i&1], sym[i&1]);
  602. }
  603. when = now() - when;
  604. printf("derive priv: %5.1fµs\n", when * 1e6 / i);
  605. decaf_448_private_to_public(dpub[0], dpriv[0]);
  606. decaf_448_private_to_public(dpub[1], dpriv[1]);
  607. when = now();
  608. for (i=0; i<nbase/10; i++) {
  609. decaf_bool_t ret = decaf_448_shared_secret(dshared[i&1], 32, dpriv[i&1], dpub[(i+1)&1]);
  610. if (ret != DECAF_SUCCESS) {
  611. printf("BUG: shared secret returns failure on %d.\n", i);
  612. break;
  613. }
  614. }
  615. when = now() - when;
  616. printf("ecdh: %5.1fµs\n", when * 1e6 / i);
  617. if (memcmp(dshared[0], dshared[1], 32)) {
  618. printf("BUG: mismatched shared secrets\n");
  619. }
  620. decaf_448_signature_t dsig;
  621. const char *dmessage = "hello world";
  622. const char *dnessage = "Jello world";
  623. when = now();
  624. for (i=0; i<nbase/10; i++) {
  625. decaf_448_sign(dsig, dpriv[0], (const unsigned char *)dmessage, 11);
  626. }
  627. when = now() - when;
  628. printf("sign: %5.1fµs\n", when * 1e6 / i);
  629. when = now();
  630. for (i=0; i<nbase/10; i++) {
  631. decaf_bool_t ret = decaf_448_verify(dsig, dpub[0],
  632. (const unsigned char *)((i&1) ? dmessage : dnessage), 11);
  633. if ((i&1) && ~ret) {
  634. printf("BUG: verify failed\n");
  635. break;
  636. } else if (!(i&1) && ret) {
  637. printf("BUG: unverify succeeded\n");
  638. break;
  639. }
  640. }
  641. when = now() - when;
  642. printf("verify: %5.1fµs\n", when * 1e6 / i);
  643. return 0;
  644. }