Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

4 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "./sidh_ref/P751_internal.h"
  5. #include "measurements.h"
  6. #ifndef PRIME_BITS
  7. #define PRIME_BITS 751
  8. #endif
  9. #define DIGITS_64 ((PRIME_BITS + 63) / 64)
  10. #define DIGITS_52 ((PRIME_BITS + 51) / 52)
  11. #define OALICE_BITS 372
  12. #define OBOB_BITS 379
  13. #define SECRETKEY_A_BYTES (OALICE_BITS + 7) / 8
  14. #define SECRETKEY_B_BYTES (OBOB_BITS + 7) / 8
  15. #define MASK_ALICE 0x0F
  16. #define MASK_BOB 0x03
  17. typedef uint64_t num52[DIGITS_52];
  18. typedef num52 felem[2];
  19. void fp2_mul_ifma(felem res, felem a, felem b);
  20. void fp2_sqr_ifma(felem res, felem a);
  21. void fp2_mul_ifma_x2(felem res1, const felem a1, const felem b1, felem res2, const felem a2, const felem b2);
  22. void fp_mul_ifma(uint64_t *rp, const uint64_t *ap, const uint64_t *bp);
  23. void to_mont_ifma(uint64_t *rp, const uint64_t *ap);
  24. void from_mont_ifma(uint64_t *rp, const uint64_t *ap);
  25. void red2norm(uint64_t out[12], const uint64_t in[15]);
  26. void norm2red(uint64_t *res, const uint64_t *a);
  27. int EphemeralKeyGeneration_A_ifma(const unsigned char *PrivateKeyA, unsigned char *PublicKeyA);
  28. int EphemeralKeyGeneration_B_ifma(const unsigned char *PrivateKeyB, unsigned char *PublicKeyB);
  29. int rdrand64_step(uint64_t *rand)
  30. {
  31. unsigned char ok;
  32. __asm__ volatile("rdrand %0; setc %1"
  33. : "=r"(*rand), "=qm"(ok));
  34. return (int)ok;
  35. }
  36. static void rand_750(uint64_t out[DIGITS_64])
  37. {
  38. for (int i = 0; i < DIGITS_64; i++)
  39. {
  40. while (!rdrand64_step((uint64_t *)&out[i]))
  41. ;
  42. }
  43. out[DIGITS_64 - 1] &= ((1ULL << (PRIME_BITS - 64 * (DIGITS_64 - 1))) - 1);
  44. }
  45. static void rand_bytes(uint8_t *out, size_t out_len)
  46. {
  47. uint64_t temp;
  48. for (int i = 0; i < out_len; i++)
  49. {
  50. while (!rdrand64_step((uint64_t *)&temp))
  51. ;
  52. out[i] = temp;
  53. }
  54. }
  55. int main()
  56. {
  57. int i;
  58. do
  59. {
  60. felm_t fa, fb, fr;
  61. num52 r, a, b;
  62. uint64_t res_ifma[DIGITS_64];
  63. rand_750(fa);
  64. rand_750(fb);
  65. norm2red(a, (uint64_t *)fa);
  66. norm2red(b, (uint64_t *)fb);
  67. to_mont_ifma(a, a);
  68. to_mont_ifma(b, b);
  69. MEASURE({ fp_mul_ifma(r, a, b); });
  70. from_mont_ifma(r, r);
  71. red2norm(res_ifma, r);
  72. printf("Mont mul IFMA Cycles/op: %.0f\n", RDTSC_total_clk);
  73. to_mont(fa, fa);
  74. to_mont(fb, fb);
  75. MEASURE({ fpmul751_mont(fa, fb, fr); });
  76. from_mont(fr, fr);
  77. printf("Mont mul ref Cycles/op: %.0f\n", RDTSC_total_clk);
  78. printf("%s\n", memcmp(fr, res_ifma, sizeof(res_ifma)) ? "FP MUL Fail"
  79. : "FP MUL Success");
  80. } while (0);
  81. do
  82. {
  83. felem a, b, r, r2;
  84. f2elm_t fa, fb, fr;
  85. uint64_t res_ifma[2][DIGITS_64];
  86. rand_750(fa[0]);
  87. rand_750(fa[1]);
  88. rand_750(fb[0]);
  89. rand_750(fb[1]);
  90. norm2red(a[0], (uint64_t *)fa[0]);
  91. norm2red(a[1], (uint64_t *)fa[1]);
  92. norm2red(b[0], (uint64_t *)fb[0]);
  93. norm2red(b[1], (uint64_t *)fb[1]);
  94. to_mont_ifma(a[0], a[0]);
  95. to_mont_ifma(a[1], a[1]);
  96. to_mont_ifma(b[0], b[0]);
  97. to_mont_ifma(b[1], b[1]);
  98. MEASURE({ fp2_mul_ifma(r, a, b); });
  99. from_mont_ifma(r[0], r[0]);
  100. from_mont_ifma(r[1], r[1]);
  101. red2norm(res_ifma[0], r[0]);
  102. red2norm(res_ifma[1], r[1]);
  103. printf("Mont FP2 mul IFMA Cycles/op: %.0f\n", RDTSC_total_clk);
  104. to_mont(fa[0], fa[0]);
  105. to_mont(fa[1], fa[1]);
  106. to_mont(fb[0], fb[0]);
  107. to_mont(fb[1], fb[1]);
  108. MEASURE({ fp2mul751_mont(fa, fb, fr); });
  109. from_mont(fr[0], fr[0]);
  110. from_mont(fr[1], fr[1]);
  111. printf("Mont FP2 mul ref Cycles/op: %.0f\n", RDTSC_total_clk);
  112. printf("%s\n", memcmp(fr, res_ifma, sizeof(res_ifma)) ? "FP2 MUL Fail"
  113. : "FP2 MUL Success");
  114. MEASURE({ fp2_mul_ifma_x2(r, a, b, r2, a, b); });
  115. from_mont_ifma(r[0], r[0]);
  116. from_mont_ifma(r[1], r[1]);
  117. red2norm(res_ifma[0], r[0]);
  118. red2norm(res_ifma[1], r[1]);
  119. printf("Dual Mont FP2 mul IFMA Cycles/op: %.0f\n", RDTSC_total_clk);
  120. printf("%s\n", memcmp(fr, res_ifma, sizeof(res_ifma)) ? "Dual FP2 MUL 1/2 Fail"
  121. : "Dual FP2 MUL 1/2 Success");
  122. from_mont_ifma(r2[0], r2[0]);
  123. from_mont_ifma(r2[1], r2[1]);
  124. red2norm(res_ifma[0], r2[0]);
  125. red2norm(res_ifma[1], r2[1]);
  126. printf("%s\n", memcmp(fr, res_ifma, sizeof(res_ifma)) ? "Dual FP2 MUL 2/2 Fail"
  127. : "Dual FP2 MUL 2/2 Success");
  128. MEASURE({ fp2_sqr_ifma(r, a); });
  129. from_mont_ifma(r[0], r[0]);
  130. from_mont_ifma(r[1], r[1]);
  131. red2norm(res_ifma[0], r[0]);
  132. red2norm(res_ifma[1], r[1]);
  133. printf("Mont FP2 sqr IFMA Cycles/op: %.0f\n", RDTSC_total_clk);
  134. MEASURE({ fp2sqr751_mont(fa, fr); });
  135. from_mont(fr[0], fr[0]);
  136. from_mont(fr[1], fr[1]);
  137. printf("Mont FP2 sqr ref Cycles/op: %.0f\n", RDTSC_total_clk);
  138. printf("%s\n", memcmp(fr, res_ifma, sizeof(res_ifma)) ? "FP2 SQR Fail"
  139. : "FP2 SQR Success");
  140. } while (0);
  141. do
  142. {
  143. unsigned char ephemeralsk_alice[SECRETKEY_A_BYTES];
  144. unsigned char ephemeralsk_bob[SECRETKEY_B_BYTES];
  145. unsigned char ct1[564] = {0};
  146. unsigned char ct2[564] = {0};
  147. rand_bytes(ephemeralsk_alice, sizeof(ephemeralsk_alice));
  148. rand_bytes(ephemeralsk_bob, sizeof(ephemeralsk_bob));
  149. ephemeralsk_alice[SECRETKEY_A_BYTES - 1] &= MASK_ALICE;
  150. ephemeralsk_bob[SECRETKEY_B_BYTES - 1] &= MASK_BOB;
  151. MEASURE({ EphemeralKeyGeneration_A(ephemeralsk_alice, ct1); });
  152. printf("Ref EphemeralKeyGeneration_A Cycles/op: %.0f\n", RDTSC_total_clk);
  153. MEASURE({ EphemeralKeyGeneration_A_ifma(ephemeralsk_alice, ct2); });
  154. printf("IFMA EphemeralKeyGeneration_A Cycles/op: %.0f\n", RDTSC_total_clk);
  155. printf("%s\n", memcmp(ct1, ct2, sizeof(ct1)) ? "EphemeralKeyGeneration_A Fail"
  156. : "EphemeralKeyGeneration_A Success");
  157. MEASURE({ EphemeralKeyGeneration_B(ephemeralsk_bob, ct1); });
  158. printf("Ref EphemeralKeyGeneration_B Cycles/op: %.0f\n", RDTSC_total_clk);
  159. MEASURE({ EphemeralKeyGeneration_B_ifma(ephemeralsk_bob, ct2); });
  160. printf("IFMA EphemeralKeyGeneration_B Cycles/op: %.0f\n", RDTSC_total_clk);
  161. printf("%s\n", memcmp(ct1, ct2, sizeof(ct1)) ? "EphemeralKeyGeneration_B Fail"
  162. : "EphemeralKeyGeneration_B Success");
  163. } while (0);
  164. }