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 "timeit.h"
  13. INIT_TIMEIT(CLOCK_PROCESS_CPUTIME_ID);
  14. static inline
  15. uint32_t mul_single(const elgamal_cipher_t c,
  16. const uint32_t x,
  17. const mpz_t cx)
  18. {
  19. mpz_t op1, op2;
  20. mpz_inits(op1, op2, NULL);
  21. START_TIMEIT();
  22. /* c1: first block */
  23. fb_powmp_ui(op1, c->fb_c1, cx->_mp_d[0]);
  24. /* c1: second block */
  25. fb_powmp_ui(op2, c->fb_c1e64, cx->_mp_d[1]);
  26. mpz_mul_modp(op1, op2, op1);
  27. /* c1: third block */
  28. fb_powmp_ui(op2, c->fb_c1e128, cx->_mp_d[2]);
  29. mpz_mul_modp(op1, op2, op1);
  30. /* c2 */
  31. fb_powmp_ui(op2, c->fb_c2, x);
  32. mpz_mul_modp(op2, op2, op1);
  33. END_TIMEIT();
  34. const uint32_t converted = convert(PTR(op2));
  35. mpz_clears(op1, op2, NULL);
  36. return converted;
  37. }
  38. void hss_mul(ssl2_t rop, const ssl1_t sl1, const ssl2_t sl2)
  39. {
  40. uint32_t converted;
  41. rop->x = mul_single(sl1->w, sl2->x, sl2->cx);
  42. mpz_set_ui(rop->cx, 0);
  43. for (size_t t = 0; t < SK_BLOCKS; t++) {
  44. mpz_mul_2exp(rop->cx, rop->cx, SS_BASE);
  45. converted = mul_single(sl1->cw[t], sl2->x, sl2->cx);
  46. mpz_add_ui(rop->cx, rop->cx, converted);
  47. }
  48. }
  49. int main()
  50. {
  51. group_init();
  52. mpz_entropy_init();
  53. dlog_precompute();
  54. mpz_t test;
  55. mpz_init(test);
  56. mpz_t x, y, xy;
  57. mpz_inits(x, y, xy, NULL);
  58. elgamal_key_t key;
  59. ELGAMAL_KEY(init, key);
  60. elgamal_keygen(key);
  61. mpz_t expected_mod, base;
  62. mpz_inits(expected_mod, base, NULL);
  63. mpz_urandomm(base, _rstate, p);
  64. mpz_powm_ui(expected_mod, base, 2, p);
  65. mpz_pow_ui(test, base, 2);
  66. remp(test);
  67. // gmp_printf("%Zx\n%Zx\n", test, expected_mod);
  68. assert(!mpz_cmp(test, expected_mod));
  69. mpz_clear(expected_mod);
  70. ssl1_t r1, r2;
  71. ssl2_t s1, s2;
  72. ssl2_t t1, t2;
  73. ssl1_init(r1);
  74. ssl1_init(r2);
  75. ssl2_init(s1);
  76. ssl2_init(s2);
  77. ssl2_init(t1);
  78. ssl2_init(t2);
  79. mpz_urandomb(y, _rstate, 1);
  80. mpz_urandomb(x, _rstate, 1);
  81. /* mpz_set_ui(x, 1); */
  82. /* mpz_set_ui(y, 1); */
  83. ssl2_share(s1, s2, x, key->sk);
  84. ssl2_open(test, s1, s2);
  85. assert(!mpz_cmp(test, x));
  86. ssl1_share(r1, r2, y, key);
  87. ssl1_open(test, r1, r2, key);
  88. assert(!mpz_cmp_ui(test, mpz_cmp_ui(y, 0) ? 2 : 1));
  89. for (int i = 0; i < (int) 1e2; i++) {
  90. hss_mul(t1, r1, s1);
  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. }