keygen.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "keygen.hpp"
  2. extern const curvepoint_fp_t bn_curvegen;
  3. extern const twistpoint_fp2_t bn_twistgen;
  4. void keygen(PublicKey& public_key, PrivateKey& private_key)
  5. {
  6. Scalar a1, b1, c1, d1, a2, b2, c2, d2;
  7. while (true)
  8. {
  9. a1.set_random();
  10. b1.set_random();
  11. c1.set_random();
  12. if (a1 != 0)
  13. {
  14. d1 = (b1 * c1 + Scalar(1)) / a1;
  15. break;
  16. }
  17. }
  18. while (true)
  19. {
  20. a2.set_random();
  21. b2.set_random();
  22. c2.set_random();
  23. if (a2 != 0)
  24. {
  25. d2 = (b2 * c2 + Scalar(1)) / a2;
  26. break;
  27. }
  28. }
  29. private_key.set(a1, b1, c1, d1, a2, b2, c2, d2);
  30. Scalar r1, r2, r3, r4;
  31. r1.set_random();
  32. r2.set_random();
  33. r3.set_random();
  34. r4.set_random();
  35. curvepoint_fp_t g_part1, g_part2, g_a1, g_b1;
  36. g_part1 = r1 * bn_curvegen;
  37. g_part2 = r2 * bn_curvegen;
  38. g_a1 = a1 * bn_curvegen;
  39. g_b1 = b1 * bn_curvegen;
  40. Bipoint<curvepoint_fp_t> full_g(g_part1, g_part2);
  41. Bipoint<curvepoint_fp_t> full_g1(g_a1, g_b1);
  42. twistpoint_fp2_t h_part1, h_part2, h_a2, h_b2;
  43. h_part1 = r3 * bn_twistgen;
  44. h_part2 = r4 * bn_twistgen;
  45. h_a2 = a2 * bn_twistgen;
  46. h_b2 = b2 * bn_twistgen;
  47. Bipoint<twistpoint_fp2_t> full_h(h_part1, h_part2);
  48. Bipoint<twistpoint_fp2_t> full_h1(h_a2, h_b2);
  49. full_g.make_affine();
  50. full_g1.make_affine();
  51. full_h.make_affine();
  52. full_h1.make_affine();
  53. public_key.set(full_g, full_h, full_g1, full_h1);
  54. }