keygen.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "keygen.hpp"
  2. extern const scalar_t bn_n;
  3. extern const curvepoint_fp_t bn_curvegen;
  4. extern const twistpoint_fp2_t bn_twistgen;
  5. void keygen(PublicKey& public_key, PrivateKey& private_key)
  6. {
  7. Fp i1, j1, k1, l1, i2, j2, k2, l2;
  8. while (true)
  9. {
  10. j1.set_random();
  11. k1.set_random();
  12. l1.set_random();
  13. if (!l1.is_zero())
  14. {
  15. i1 = (j1 * k1 + Fp(1)) / l1;
  16. break;
  17. }
  18. }
  19. while (true)
  20. {
  21. j2.set_random();
  22. k2.set_random();
  23. l2.set_random();
  24. if (!l2.is_zero())
  25. {
  26. i2 = (j2 * k2 + Fp(1)) / l2;
  27. break;
  28. }
  29. }
  30. private_key.set(i1, j1, k1, l1, i2, j2, k2, l2);
  31. curvepoint_fp_t c1, c2, c3, c4;
  32. curvepoint_fp_scalarmult_vartime(c1, bn_curvegen, i1.to_scalar());
  33. curvepoint_fp_makeaffine(c1);
  34. curvepoint_fp_scalarmult_vartime(c2, bn_curvegen, j1.to_scalar());
  35. curvepoint_fp_makeaffine(c2);
  36. Bipoint<curvepoint_fp_t> b1(c1, c2);
  37. twistpoint_fp2_t t1, t2, t3, t4;
  38. twistpoint_fp2_scalarmult_vartime(t1, bn_twistgen,i2.scalar());
  39. twistpoint_fp2_makeaffine(t1);
  40. twistpoint_fp2_scalarmult_vartime(t2, bn_twistgen,j2.scalar());
  41. twistpoint_fp2_makeaffine(t2);
  42. Bipoint<twistpoint_fp2_t> b2(t1, t2);
  43. scalar_t s1, s2, s3, s4;
  44. scalar_setrandom(s1, bn_n);
  45. scalar_setrandom(s2, bn_n);
  46. scalar_setrandom(s3, bn_n);
  47. scalar_setrandom(s4, bn_n);
  48. curvepoint_fp_scalarmult_vartime(c3, bn_curvegen, s1);
  49. curvepoint_fp_makeaffine(c3);
  50. curvepoint_fp_scalarmult_vartime(c4, bn_curvegen, s2);
  51. curvepoint_fp_makeaffine(c4);
  52. Bipoint<curvepoint_fp_t> b3(c3, c4);
  53. twistpoint_fp2_scalarmult_vartime(t3, bn_twistgen, s3);
  54. twistpoint_fp2_makeaffine(t3);
  55. twistpoint_fp2_scalarmult_vartime(t4, bn_twistgen, s4);
  56. twistpoint_fp2_makeaffine(t4);
  57. Bipoint<twistpoint_fp2_t> b4(t3, t4);
  58. public_key.set(b1, b2, b3, b4);
  59. }