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.
 
 
 
 
 

751 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. uint8_t enc[DECAF_448_SER_BYTES];
  326. memset(enc,4,sizeof(enc));
  327. when = now();
  328. for (i=0; i<nbase/10; i++) {
  329. ignore_result(decaf_448_direct_scalarmul(enc,enc,asc,-1,0));
  330. }
  331. when = now() - when;
  332. printf("decaf dir: %5.1fµs\n", when * 1e6 / i);
  333. when = now();
  334. for (i=0; i<nbase/10; i++) {
  335. decaf_448_point_double_scalarmul(Da,Db,bsc,Dc,asc);
  336. }
  337. when = now() - when;
  338. printf("decaf slo2: %5.1fµs\n", when * 1e6 / i);
  339. when = now();
  340. for (i=0; i<nbase/10; i++) {
  341. scalarmul(&ext,sk);
  342. }
  343. when = now() - when;
  344. printf("edwards smz: %5.1fµs\n", when * 1e6 / i);
  345. when = now();
  346. for (i=0; i<nbase/10; i++) {
  347. scalarmul_vlook(&ext,sk);
  348. }
  349. when = now() - when;
  350. printf("edwards svl: %5.1fµs\n", when * 1e6 / i);
  351. when = now();
  352. for (i=0; i<nbase/10; i++) {
  353. scalarmul_ed(ed,sk);
  354. }
  355. when = now() - when;
  356. printf("edwards txt: %5.1fµs\n", when * 1e6 / i);
  357. field_set_ui(a,0);
  358. when = now();
  359. for (i=0; i<nbase/10; i++) {
  360. ignore_result(decaf_deserialize_tw_extended(ed,a,-1));
  361. scalarmul_ed(ed,sk);
  362. decaf_serialize_tw_extended(a,ed);
  363. }
  364. when = now() - when;
  365. printf("simple ECDH: %5.1fµs\n", when * 1e6 / i);
  366. when = now();
  367. for (i=0; i<nbase/10; i++) {
  368. scalarmul(&ext,sk);
  369. untwist_and_double_and_serialize(a,&ext);
  370. }
  371. when = now() - when;
  372. printf("edwards smc: %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_vt(&ext,sk,SCALAR_BITS);
  377. }
  378. when = now() - when;
  379. printf("edwards vtm: %5.1fµs\n", when * 1e6 / i);
  380. tw_niels_a_t wnaft[1<<6];
  381. when = now();
  382. for (i=0; i<nbase/10; i++) {
  383. ignore_result(precompute_fixed_base_wnaf(wnaft,&ext,6));
  384. }
  385. when = now() - when;
  386. printf("wnaf6 pre: %5.1fµs\n", when * 1e6 / i);
  387. when = now();
  388. for (i=0; i<nbase/10; i++) {
  389. q448_randomize(&crand, sk);
  390. scalarmul_fixed_base_wnaf_vt(&ext,sk,SCALAR_BITS,(const tw_niels_a_t*)wnaft,6);
  391. }
  392. when = now() - when;
  393. printf("edwards vt6: %5.1fµs\n", when * 1e6 / i);
  394. when = now();
  395. for (i=0; i<nbase/10; i++) {
  396. ignore_result(precompute_fixed_base_wnaf(wnaft,&ext,4));
  397. }
  398. when = now() - when;
  399. printf("wnaf4 pre: %5.1fµs\n", when * 1e6 / i);
  400. when = now();
  401. for (i=0; i<nbase/10; i++) {
  402. q448_randomize(&crand, sk);
  403. scalarmul_fixed_base_wnaf_vt(&ext,sk,SCALAR_BITS,(const tw_niels_a_t*)wnaft,4);
  404. }
  405. when = now() - when;
  406. printf("edwards vt4: %5.1fµs\n", when * 1e6 / i);
  407. when = now();
  408. for (i=0; i<nbase/10; i++) {
  409. ignore_result(precompute_fixed_base_wnaf(wnaft,&ext,5));
  410. }
  411. when = now() - when;
  412. printf("wnaf5 pre: %5.1fµs\n", when * 1e6 / i);
  413. when = now();
  414. for (i=0; i<nbase/10; i++) {
  415. q448_randomize(&crand, sk);
  416. scalarmul_fixed_base_wnaf_vt(&ext,sk,SCALAR_BITS,(const tw_niels_a_t*)wnaft,5);
  417. }
  418. when = now() - when;
  419. printf("edwards vt5: %5.1fµs\n", when * 1e6 / i);
  420. when = now();
  421. for (i=0; i<nbase/10; i++) {
  422. q448_randomize(&crand, sk);
  423. q448_randomize(&crand, tk);
  424. linear_combo_var_fixed_vt(&ext,sk,FIELD_BITS,tk,FIELD_BITS,(const tw_niels_a_t*)wnaft,5);
  425. }
  426. when = now() - when;
  427. printf("vt vf combo: %5.1fµs\n", when * 1e6 / i);
  428. when = now();
  429. for (i=0; i<nbase/10; i++) {
  430. deserialize_affine(&affine, a);
  431. convert_affine_to_extensible(&exta,&affine);
  432. twist_and_double(&ext,&exta);
  433. scalarmul(&ext,sk);
  434. untwist_and_double(&exta,&ext);
  435. serialize_extensible(b, &exta);
  436. }
  437. when = now() - when;
  438. printf("edwards sm: %5.1fµs\n", when * 1e6 / i);
  439. 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;
  440. while (1) {
  441. field_randomize(&crand, a);
  442. if (deserialize_affine(&affine, a)) break;
  443. }
  444. convert_affine_to_extensible(&exta,&affine);
  445. twist_and_double(&ext,&exta);
  446. when = now();
  447. for (i=0; i<nbase/10; i++) {
  448. if (i) destroy_fixed_base(&t_5_5_18);
  449. ignore_result(precompute_fixed_base(&t_5_5_18, &ext, 5, 5, 18, NULL));
  450. }
  451. when = now() - when;
  452. printf("pre(5,5,18): %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_3_5_30);
  456. ignore_result(precompute_fixed_base(&t_3_5_30, &ext, 3, 5, 30, NULL));
  457. }
  458. when = now() - when;
  459. printf("pre(3,5,30): %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_5_3_30);
  463. ignore_result(precompute_fixed_base(&t_5_3_30, &ext, 5, 3, 30, NULL));
  464. }
  465. when = now() - when;
  466. printf("pre(5,3,30): %5.1fµs\n", when * 1e6 / i);
  467. when = now();
  468. for (i=0; i<nbase/10; i++) {
  469. if (i) destroy_fixed_base(&t_15_3_10);
  470. ignore_result(precompute_fixed_base(&t_15_3_10, &ext, 15, 3, 10, NULL));
  471. }
  472. when = now() - when;
  473. printf("pre(15,3,10):%5.1fµs\n", when * 1e6 / i);
  474. when = now();
  475. for (i=0; i<nbase/10; i++) {
  476. if (i) destroy_fixed_base(&t_8_4_14);
  477. ignore_result(precompute_fixed_base(&t_8_4_14, &ext, 8, 4, 14, NULL));
  478. }
  479. when = now() - when;
  480. printf("pre(8,4,14): %5.1fµs\n", when * 1e6 / i);
  481. when = now();
  482. for (i=0; i<nbase; i++) {
  483. scalarmul_fixed_base(&ext, sk, FIELD_BITS, &t_5_5_18);
  484. }
  485. when = now() - when;
  486. printf("com(5,5,18): %5.1fµs\n", when * 1e6 / i);
  487. when = now();
  488. for (i=0; i<nbase; i++) {
  489. scalarmul_fixed_base(&ext, sk, FIELD_BITS, &t_3_5_30);
  490. }
  491. when = now() - when;
  492. printf("com(3,5,30): %5.1fµs\n", when * 1e6 / i);
  493. when = now();
  494. for (i=0; i<nbase; i++) {
  495. scalarmul_fixed_base(&ext, sk, FIELD_BITS, &t_8_4_14);
  496. }
  497. when = now() - when;
  498. printf("com(8,4,14): %5.1fµs\n", when * 1e6 / i);
  499. when = now();
  500. for (i=0; i<nbase; i++) {
  501. scalarmul_fixed_base(&ext, sk, FIELD_BITS, &t_5_3_30);
  502. }
  503. when = now() - when;
  504. printf("com(5,3,30): %5.1fµs\n", when * 1e6 / i);
  505. when = now();
  506. for (i=0; i<nbase; i++) {
  507. scalarmul_fixed_base(&ext, sk, FIELD_BITS, &t_15_3_10);
  508. }
  509. when = now() - when;
  510. printf("com(15,3,10):%5.1fµs\n", when * 1e6 / i);
  511. printf("\nGoldilocks:\n");
  512. int res = goldilocks_init();
  513. assert(!res);
  514. struct goldilocks_public_key_t gpk,hpk;
  515. struct goldilocks_private_key_t gsk,hsk;
  516. when = now();
  517. for (i=0; i<nbase; i++) {
  518. if (i&1) {
  519. res = goldilocks_keygen(&gsk,&gpk);
  520. } else {
  521. res = goldilocks_keygen(&hsk,&hpk);
  522. }
  523. assert(!res);
  524. }
  525. when = now() - when;
  526. printf("keygen: %5.1fµs\n", when * 1e6 / i);
  527. uint8_t ss1[64],ss2[64];
  528. int gres1=0,gres2=0;
  529. when = now();
  530. for (i=0; i<nbase; i++) {
  531. if (i&1) {
  532. gres1 = goldilocks_shared_secret(ss1,&gsk,&hpk);
  533. } else {
  534. gres2 = goldilocks_shared_secret(ss2,&hsk,&gpk);
  535. }
  536. }
  537. when = now() - when;
  538. printf("ecdh: %5.1fµs\n", when * 1e6 / i);
  539. if (gres1 || gres2 || memcmp(ss1,ss2,64)) {
  540. printf("[FAIL] %d %d\n",gres1,gres2);
  541. printf("sk1 = ");
  542. for (i=0; i<SCALAR_BYTES; i++) {
  543. printf("%02x", gsk.opaque[i]);
  544. }
  545. printf("\nsk2 = ");
  546. for (i=0; i<SCALAR_BYTES; i++) {
  547. printf("%02x", hsk.opaque[i]);
  548. }
  549. printf("\nss1 = ");
  550. for (i=0; i<64; i++) {
  551. printf("%02x", ss1[i]);
  552. }
  553. printf("\nss2 = ");
  554. for (i=0; i<64; i++) {
  555. printf("%02x", ss2[i]);
  556. }
  557. printf("\n");
  558. }
  559. uint8_t sout[FIELD_BYTES*2];
  560. const char *message = "hello world";
  561. size_t message_len = strlen(message);
  562. when = now();
  563. for (i=0; i<nbase; i++) {
  564. res = goldilocks_sign(sout,(const unsigned char *)message,message_len,&gsk);
  565. (void)res;
  566. assert(!res);
  567. }
  568. when = now() - when;
  569. printf("sign: %5.1fµs\n", when * 1e6 / i);
  570. when = now();
  571. for (i=0; i<nbase; i++) {
  572. int ver = goldilocks_verify(sout,(const unsigned char *)message,message_len,&gpk);
  573. (void)ver;
  574. assert(!ver);
  575. }
  576. when = now() - when;
  577. printf("verify: %5.1fµs\n", when * 1e6 / i);
  578. struct goldilocks_precomputed_public_key_t *pre = NULL;
  579. when = now();
  580. for (i=0; i<nbase; i++) {
  581. goldilocks_destroy_precomputed_public_key(pre);
  582. pre = goldilocks_precompute_public_key(&gpk);
  583. }
  584. when = now() - when;
  585. printf("precompute: %5.1fµs\n", when * 1e6 / i);
  586. when = now();
  587. for (i=0; i<nbase; i++) {
  588. int ver = goldilocks_verify_precomputed(sout,(const unsigned char *)message,message_len,pre);
  589. (void)ver;
  590. assert(!ver);
  591. }
  592. when = now() - when;
  593. printf("verify pre: %5.1fµs\n", when * 1e6 / i);
  594. when = now();
  595. for (i=0; i<nbase; i++) {
  596. int ret = goldilocks_shared_secret_precomputed(ss1,&gsk,pre);
  597. (void)ret;
  598. assert(!ret);
  599. }
  600. when = now() - when;
  601. printf("ecdh pre: %5.1fµs\n", when * 1e6 / i);
  602. printf("\nDecaf slow:\n");
  603. decaf_448_symmetric_key_t sym[2] = {{0},{1}};
  604. decaf_448_private_key_t dpriv[2];
  605. decaf_448_public_key_t dpub[2];
  606. unsigned char dshared[2][32];
  607. when = now();
  608. for (i=0; i<nbase/10; i++) {
  609. decaf_448_derive_private_key(dpriv[i&1], sym[i&1]);
  610. }
  611. when = now() - when;
  612. printf("derive priv: %5.1fµs\n", when * 1e6 / i);
  613. decaf_448_private_to_public(dpub[0], dpriv[0]);
  614. decaf_448_private_to_public(dpub[1], dpriv[1]);
  615. when = now();
  616. for (i=0; i<nbase/10; i++) {
  617. decaf_bool_t ret = decaf_448_shared_secret(dshared[i&1], 32, dpriv[i&1], dpub[(i+1)&1]);
  618. if (ret != DECAF_SUCCESS) {
  619. printf("BUG: shared secret returns failure on %d.\n", i);
  620. break;
  621. }
  622. }
  623. when = now() - when;
  624. printf("ecdh: %5.1fµs\n", when * 1e6 / i);
  625. if (memcmp(dshared[0], dshared[1], 32)) {
  626. printf("BUG: mismatched shared secrets\n");
  627. }
  628. decaf_448_signature_t dsig;
  629. const char *dmessage = "hello world";
  630. const char *dnessage = "Jello world";
  631. when = now();
  632. for (i=0; i<nbase/10; i++) {
  633. decaf_448_sign(dsig, dpriv[0], (const unsigned char *)dmessage, 11);
  634. }
  635. when = now() - when;
  636. printf("sign: %5.1fµs\n", when * 1e6 / i);
  637. when = now();
  638. for (i=0; i<nbase/10; i++) {
  639. decaf_bool_t ret = decaf_448_verify(dsig, dpub[0],
  640. (const unsigned char *)((i&1) ? dmessage : dnessage), 11);
  641. if ((i&1) && ~ret) {
  642. printf("BUG: verify failed\n");
  643. break;
  644. } else if (!(i&1) && ret) {
  645. printf("BUG: unverify succeeded\n");
  646. break;
  647. }
  648. }
  649. when = now() - when;
  650. printf("verify: %5.1fµs\n", when * 1e6 / i);
  651. return 0;
  652. }