gmp-pk-crypto.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. \file gmp-pk-crypto.h
  3. \author michael.zohner@ec-spride.de
  4. \copyright ABY - A Framework for Efficient Mixed-protocol Secure Two-party Computation
  5. Copyright (C) 2019 ENCRYPTO Group, TU Darmstadt
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. ABY is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. \brief Class with finite-field-cryptography operations (using the GMP library)
  17. */
  18. #ifndef GMP_PK_CRYPTO_H_
  19. #define GMP_PK_CRYPTO_H_
  20. #include "pk-crypto.h"
  21. #include "../utils.h"
  22. #include <gmp.h>
  23. class prime_field;
  24. class gmp_fe;
  25. class gmp_num;
  26. class gmp_brickexp;
  27. #define fe2mpz(fieldele) (((gmp_fe*) (fieldele))->get_val())
  28. #define num2mpz(number) (((gmp_num*) (number))->get_val())
  29. class prime_field: public pk_crypto {
  30. public:
  31. prime_field(seclvl sp, uint8_t* seed) :
  32. pk_crypto(sp) {
  33. init(sp, seed);
  34. }
  35. ;
  36. ~prime_field();
  37. num* get_num();
  38. num* get_rnd_num(uint32_t bitlen = 0);
  39. fe* get_fe();
  40. fe* get_rnd_fe();
  41. fe* get_generator();
  42. fe* get_rnd_generator();
  43. num* get_order();
  44. mpz_t* get_p();
  45. uint32_t get_size();
  46. brickexp* get_brick(fe* gen);
  47. uint32_t num_byte_size() {
  48. return ceil_divide(secparam.ifcbits, 8);
  49. }
  50. uint32_t get_field_size() {
  51. return secparam.ifcbits;
  52. }
  53. ;
  54. protected:
  55. void init(seclvl sp, uint8_t* seed);
  56. private:
  57. mpz_t p;
  58. mpz_t g;
  59. mpz_t q;
  60. };
  61. class gmp_fe: public fe {
  62. public:
  63. gmp_fe(prime_field* fld);
  64. gmp_fe(prime_field* fld, mpz_t src);
  65. ~gmp_fe();
  66. void set(fe* src);
  67. mpz_t* get_val();
  68. void set_mul(fe* a, fe* b);
  69. void set_pow(fe* b, num* e);
  70. void set_div(fe* a, fe* b);
  71. void set_double_pow_mul(fe* b1, num* e1, fe* b2, num* e2);
  72. void export_to_bytes(uint8_t* buf);
  73. void import_from_bytes(uint8_t* buf);
  74. void sample_fe_from_bytes(uint8_t* buf, uint32_t bytelen);
  75. bool eq(fe* a);
  76. void print();
  77. private:
  78. void init() {
  79. mpz_init(val);
  80. }
  81. ;
  82. mpz_t val;
  83. prime_field* field;
  84. };
  85. class gmp_num: public num {
  86. public:
  87. gmp_num(prime_field* fld);
  88. gmp_num(prime_field* fld, mpz_t src);
  89. ~gmp_num();
  90. void set(num* src);
  91. void set_si(int32_t src);
  92. void set_add(num* a, num* b);
  93. void set_sub(num* a, num* b);
  94. void set_mul(num* a, num* b);
  95. void mod(num* mod);
  96. void set_mul_mod(num* a, num* b, num* modulus) ;
  97. mpz_t* get_val();
  98. void export_to_bytes(uint8_t* buf, uint32_t field_size);
  99. void import_from_bytes(uint8_t* buf, uint32_t field_size);
  100. void set_rnd(uint32_t bits);
  101. void print();
  102. private:
  103. mpz_t val;
  104. prime_field* field;
  105. };
  106. class gmp_brickexp: public brickexp {
  107. public:
  108. gmp_brickexp(fe* g, prime_field* pfield) {
  109. init(g, pfield);
  110. }
  111. ;
  112. ~gmp_brickexp();
  113. void pow(fe* result, num* e);
  114. void init(fe* g, prime_field* pfield);
  115. private:
  116. uint32_t m_numberOfElements;
  117. mpz_t* m_table;
  118. prime_field* field;
  119. };
  120. // mpz_export does not fill leading zeros, thus a prepending of leading 0s is required
  121. void mpz_export_padded(uint8_t* pBufIdx, uint32_t field_size, mpz_t to_export);
  122. #endif /* GMP_PK_CRYPTO_H_ */