ecc-pk-crypto.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. \file ecc-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 ECC operations
  17. */
  18. #ifndef ECC_PK_CRYPTO_H_
  19. #define ECC_PK_CRYPTO_H_
  20. #include "pk-crypto.h"
  21. extern "C"
  22. {
  23. #include <relic.h>
  24. }
  25. #include <memory>
  26. #include <vector>
  27. #include <mutex>
  28. class ecc_num;
  29. class ecc_fe;
  30. class ecc_brickexp;
  31. static std::mutex relic_mutex;
  32. class ecc_field: public pk_crypto {
  33. public:
  34. ecc_field(seclvl sp, uint8_t* seed) :
  35. pk_crypto(sp) {
  36. init(sp, seed);
  37. };
  38. ~ecc_field();
  39. num* get_num();
  40. num* get_rnd_num(uint32_t bitlen = 0);
  41. fe* get_fe();
  42. fe* get_rnd_fe();
  43. fe* get_generator();
  44. fe* get_rnd_generator();
  45. uint32_t get_size();
  46. //fe* sample_fe_from_bytes(uint8_t* buf, uint32_t bytelen);
  47. num* get_order();
  48. uint32_t num_byte_size();
  49. uint32_t get_field_size();
  50. brickexp* get_brick(fe* gen);
  51. ctx_t* get_context();
  52. protected:
  53. void init(seclvl sp, uint8_t* seed);
  54. private:
  55. fe* sample_random_point();
  56. ecc_fe* generator;
  57. ctx_t* context;
  58. };
  59. class ecc_num: public num {
  60. public:
  61. ecc_num(ecc_field* fld);
  62. ecc_num(ecc_field* fld, bn_t src);
  63. ~ecc_num();
  64. void set(num* src);
  65. void set_si(int32_t src);
  66. void set_add(num* a, num* b);
  67. void set_sub(num* a, num* b);
  68. void set_mul(num* a, num* b);
  69. void mod(num* mod);
  70. void set_mul_mod(num* a, num* b, num* modulus) ;
  71. void get_val(bn_t res);
  72. void export_to_bytes(uint8_t* buf, uint32_t field_size_bytes);
  73. void import_from_bytes(uint8_t* buf, uint32_t field_size_bytes);
  74. //void set_rnd(uint32_t bits); Seems useless since not implemented
  75. void print();
  76. private:
  77. void shallow_copy(bn_t to, bn_t from);
  78. bn_t val;
  79. ecc_field* field;
  80. ctx_t* context;
  81. };
  82. class ecc_fe: public fe {
  83. public:
  84. ecc_fe(ecc_field* fld);
  85. ecc_fe(ecc_field* fld, eb_t src);
  86. ~ecc_fe();
  87. void set(fe* src);
  88. void get_val(eb_t res);
  89. void set_mul(fe* a, fe* b);
  90. void set_pow(fe* b, num* e);
  91. void set_div(fe* a, fe* b);
  92. void set_double_pow_mul(fe* b1, num* e1, fe* b2, num* e2);
  93. void export_to_bytes(uint8_t* buf);
  94. void import_from_bytes(uint8_t* buf);
  95. void sample_fe_from_bytes(uint8_t* buf, uint32_t bytelen);
  96. bool eq(fe* a);
  97. void print();
  98. private:
  99. void init();
  100. void shallow_copy(eb_t to, eb_t from);
  101. eb_t val;
  102. ecc_field* field;
  103. ctx_t* context;
  104. };
  105. class ecc_brickexp: public brickexp {
  106. public:
  107. ecc_brickexp(fe* generator, ecc_field* field);
  108. ~ecc_brickexp();
  109. void pow(fe* res, num* e);
  110. private:
  111. uint32_t eb_pre_size;
  112. eb_t* eb_table;
  113. ecc_field* field;
  114. ctx_t* context;
  115. };
  116. #endif /* ECC_PK_CRYPTO_H_ */