rms.c 2.9 KB

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