rms.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "config.h"
  2. #include <assert.h>
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <gmp.h>
  7. #include "ddlog.h"
  8. #include "elgamal.h"
  9. #include "entropy.h"
  10. #include "group.h"
  11. #include "hss.h"
  12. #include "rms.h"
  13. #include "timeit.h"
  14. INIT_TIMEIT(CLOCK_PROCESS_CPUTIME_ID);
  15. static inline
  16. uint32_t mul_single(const elgamal_cipher_t c,
  17. const uint32_t x,
  18. const mpz_t cx)
  19. {
  20. mpz_t op1, op2;
  21. mpz_inits(op1, op2, NULL);
  22. /* first block */
  23. fb_powmp_ui(op1, c->fb_c1, cx->_mp_d[0]);
  24. /* second block */
  25. fb_powmp_ui(op2, c->fb_c1e64, cx->_mp_d[1]);
  26. mpz_mul_modp(op1, op2, op1);
  27. /* third block */
  28. fb_powmp_ui(op2, c->fb_c1e128, cx->_mp_d[2]);
  29. mpz_mul_modp(op1, op2, op1);
  30. fb_powmp_ui(op2, c->fb_c2, x);
  31. mpz_mul_modp(op2, op2, op1);
  32. const uint32_t converted = convert(PTR(op2));
  33. mpz_clears(op1, op2, NULL);
  34. return converted;
  35. }
  36. void hss_mul(ssl2_t rop, const ssl1_t sl1, const ssl2_t sl2)
  37. {
  38. uint32_t converted;
  39. rop->x = mul_single(sl1->w, sl2->x, sl2->cx);
  40. mpz_set_ui(rop->cx, 0);
  41. for (size_t t = 0; t < 160; t++) {
  42. mpz_mul_2exp(rop->cx, rop->cx, 1);
  43. converted = mul_single(sl1->cw[t], sl2->x, sl2->cx);
  44. mpz_add_ui(rop->cx, rop->cx, converted);
  45. }
  46. }
  47. int main()
  48. {
  49. group_init();
  50. mpz_entropy_init();
  51. dlog_precompute();
  52. mpz_t test;
  53. mpz_init(test);
  54. mpz_t x, y, xy;
  55. mpz_inits(x, y, xy, NULL);
  56. elgamal_key_t key;
  57. ELGAMAL_KEY(init, key);
  58. elgamal_keygen(key);
  59. mpz_t expected_mod, base;
  60. mpz_inits(expected_mod, base, NULL);
  61. mpz_urandomm(base, _rstate, p);
  62. mpz_powm_ui(expected_mod, base, 2, p);
  63. mpz_pow_ui(test, base, 2);
  64. remp(test);
  65. // gmp_printf("%Zx\n%Zx\n", test, expected_mod);
  66. assert(!mpz_cmp(test, expected_mod));
  67. mpz_clear(expected_mod);
  68. ssl1_t r1, r2;
  69. ssl2_t s1, s2;
  70. ssl2_t t1, t2;
  71. ssl1_init(r1);
  72. ssl1_init(r2);
  73. ssl2_init(s1);
  74. ssl2_init(s2);
  75. ssl2_init(t1);
  76. ssl2_init(t2);
  77. mpz_urandomb(y, _rstate, 1);
  78. mpz_urandomb(x, _rstate, 1);
  79. /* mpz_set_ui(x, 1); */
  80. /* mpz_set_ui(y, 1); */
  81. ssl2_share(s1, s2, x, key->sk);
  82. ssl2_open(test, s1, s2);
  83. assert(!mpz_cmp(test, x));
  84. ssl1_share(r1, r2, y, key);
  85. ssl1_open(test, r1, r2, key);
  86. assert(!mpz_cmp_ui(test, mpz_cmp_ui(y, 0) ? 2 : 1));
  87. for (int i = 0; i < (int) 1e2; i++) {
  88. START_TIMEIT();
  89. hss_mul(t1, r1, s1);
  90. END_TIMEIT();
  91. hss_mul(t2, r2, s2);
  92. #ifndef NDEBUG
  93. gmp_printf("%Zx %Zx\n", x, y);
  94. gmp_printf("%d %d\n", s1->x, s2->x);
  95. #endif
  96. mpz_mul(xy, x, y);
  97. ssl2_open(test, t2, t1);
  98. assert(!mpz_cmp(test, xy));
  99. mpz_sub(test, t2->cx, t1->cx);
  100. mpz_abs(test, test);
  101. assert(((!mpz_cmp_ui(xy, 1) && !mpz_cmp(test, key->sk))) ||
  102. ((!mpz_cmp_ui(xy, 0)) && !mpz_cmp_ui(test, 0)));
  103. }
  104. printf(TIMEIT_FORMAT "\n", GET_TIMEIT());
  105. ssl2_clear(s1);
  106. ssl2_clear(s2);
  107. ssl1_clear(r1);
  108. ssl1_clear(r2);
  109. ssl2_clear(t1);
  110. ssl2_clear(t2);
  111. mpz_clears(x, y, NULL);
  112. ELGAMAL_KEY(clear, key);
  113. group_clear();
  114. return 0;
  115. }