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.
 
 
 
 
 

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