rms.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 "elgamal.h"
  8. #include "entropy.h"
  9. #include "rms.h"
  10. #include "hss.h"
  11. #include "timeit.h"
  12. /** this function is only for testing purposes. */
  13. void ssl2_share(ssl2_t s1, ssl2_t s2, const mpz_t v, const mpz_t sk)
  14. {
  15. mpz_rrandomb(s1->x, _rstate, 192);
  16. mpz_add(s2->x, v, s1->x);
  17. mpz_rrandomb(s1->cx, _rstate, 192);
  18. mpz_mul(s2->cx, sk, v);
  19. mpz_add(s2->cx, s2->cx, s1->cx);
  20. }
  21. void ssl2_merge(mpz_t rop, const ssl2_t s1, const ssl2_t s2)
  22. {
  23. mpz_sub(rop, s2->x, s1->x);
  24. mpz_abs(rop, rop);
  25. }
  26. void ssl1_share(ssl1_t r1, ssl1_t r2, const mpz_t v, const elgamal_key_t key)
  27. {
  28. mpz_t zero;
  29. mpz_init_set_ui(zero, 0);
  30. r1->w = elgamal_encrypt(key, v);
  31. for (size_t t = 0; t < 160; t++) {
  32. if (mpz_tstbit(key.sk, t)) {
  33. r1->cw[t] = elgamal_encrypt(key, v);
  34. } else {
  35. r1->cw[t] = elgamal_encrypt(key, zero);
  36. }
  37. }
  38. mpz_set(r2->w.c1, r1->w.c1);
  39. mpz_set(r2->w.c2, r1->w.c2);
  40. //r2->w = elgamal_encrypt(key, v);
  41. for (size_t t = 0; t < 160; t++) {
  42. if (mpz_tstbit(key.sk, t)) {
  43. r2->cw[t] = elgamal_encrypt(key, v);
  44. } else {
  45. r2->cw[t] = elgamal_encrypt(key, zero);
  46. }
  47. }
  48. }
  49. void ssl1_merge(mpz_t rop, const ssl1_t r1, const ssl1_t r2, const elgamal_key_t key)
  50. {
  51. mpz_t rop1, rop2;
  52. mpz_inits(rop1, rop2, NULL);
  53. elgamal_decrypt(rop1, key, r1->w);
  54. elgamal_decrypt(rop2, key, r2->w);
  55. assert(!mpz_cmp(rop1, rop2));
  56. mpz_set(rop, rop1);
  57. mpz_clears(rop1, rop2, NULL);
  58. }
  59. #define strip_size 16
  60. uint32_t naif_convert(mpz_t n)
  61. {
  62. uint32_t i;
  63. mpz_t t;
  64. mpz_init_set_ui(t, 1);
  65. mpz_mul_2exp(t, t, 1536-strip_size);
  66. for (i = 0; mpz_cmp(n, t) > -1; i++) {
  67. mpz_mul_ui(n, n, 2);
  68. mpz_mod(n, n, p);
  69. }
  70. mpz_clear(t);
  71. return i;
  72. }
  73. elgamal_key_t key;
  74. void hss_mul(ssl2_t rop, const ssl1_t sl1, const ssl2_t sl2)
  75. {
  76. mpz_t tmp;
  77. mpz_init(tmp);
  78. mpz_powm(tmp, sl1->w.c1, sl2->cx, p);
  79. mpz_invert(tmp, tmp, p);
  80. mpz_powm(rop->x, sl1->w.c2, sl2->x, p);
  81. mpz_mul(rop->x, rop->x, tmp);
  82. mpz_mod(rop->x, rop->x, p);
  83. const uint32_t converted = naif_convert(rop->x);
  84. mpz_set_ui(rop->x, converted);
  85. mpz_clear(tmp);
  86. }
  87. int main()
  88. {
  89. /* set up entropy, prime modulus etc. */
  90. mpz_entropy_init();
  91. hss_init();
  92. mpz_t test;
  93. mpz_init(test);
  94. mpz_t x;
  95. mpz_t y;
  96. mpz_inits(x, y, NULL);
  97. uint64_t expected;
  98. /* test elgamal */
  99. key = elgamal_keygen();
  100. mpz_urandomb(x, _rstate, 128);
  101. //elgamal_cipher_t c = elgamal_encrypt(key, x);
  102. //elgamal_decrypt(test, key, c);
  103. //assert(!mpz_cmp(x, test));
  104. ssl1_t r1, r2;
  105. ssl2_t s1, s2;
  106. ssl2_t t1, t2;
  107. ssl1_init(r1);
  108. ssl1_init(r2);
  109. ssl2_init(s1);
  110. ssl2_init(s2);
  111. ssl2_init(t1);
  112. ssl2_init(t2);
  113. INIT_TIMEIT();
  114. for (int i = 0; i < (int) 1e1; i++) {
  115. mpz_rrandomb(x, _rstate, 1);
  116. mpz_urandomb(y, _rstate, 1);
  117. //mpz_set_ui(x, 0);
  118. //mpz_set_ui(y, 1);
  119. ssl2_share(s1, s2, x, key.sk);
  120. ssl2_merge(test, s1, s2);
  121. assert(!mpz_cmp(test, x));
  122. ssl1_share(r1, r2, y, key);
  123. ssl1_merge(test, r1, r2, key);
  124. expected = mpz_cmp_ui(x, 0) && mpz_cmp_ui(y, 0) ? 2 : 1;
  125. assert(!mpz_cmp_ui(test, expected));
  126. START_TIMEIT();
  127. hss_mul(t1, r1, s1);
  128. END_TIMEIT();
  129. hss_mul(t2, r2, s2);
  130. //gmp_printf("resulting shares: %Zx %Zx\n", t1->x, t2->x);
  131. //gmp_printf("x, y: %Zx %Zx\n", x, y);
  132. ssl2_merge(test, t2, t1);
  133. //gmp_printf("result: %Zx\n", test);
  134. expected = (!mpz_cmp_ui(x, 0) || !mpz_cmp_ui(y, 0)) ? 0 : 1;
  135. assert(!mpz_cmp_ui(test, expected));
  136. }
  137. printf(TIMEIT_FORMAT "\n", GET_TIMEIT());
  138. ssl2_clear(s1);
  139. ssl2_clear(s2);
  140. ssl1_clear(r1);
  141. ssl1_clear(r2);
  142. ssl2_clear(t1);
  143. ssl2_clear(t2);
  144. mpz_clears(x, y, NULL);
  145. mpz_clears(key.sk, key.pk, NULL);
  146. hss_del();
  147. return 0;
  148. }