PaillierAdapter.hpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright (C) 2014 Carlos Aguilar Melchor, Joris Barrier, Marc-Olivier Killijian
  2. * This file is part of XPIR.
  3. *
  4. * XPIR is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * XPIR is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with XPIR. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef DEF_PAILLIERADAPTER
  18. #define DEF_PAILLIERADAPTER
  19. static const unsigned int kNumSecurityBits = 80;
  20. #include <fstream>
  21. #include <omp.h>
  22. #include <math.h>
  23. #include <string>
  24. #include <unistd.h>
  25. #include <boost/algorithm/string.hpp>
  26. #include "HomomorphicCrypto.hpp"
  27. #include "PaillierPublicParameters.hpp"
  28. #include "PaillierPrivateParameters.hpp"
  29. using namespace std;
  30. class PaillierAdapter : public HomomorphicCrypto {
  31. private:
  32. /*Attributs*/
  33. gmp_randstate_t rand;
  34. int unsigned modulusbits;
  35. int security_bits;
  36. PaillierPrivateParameters privateParameters;
  37. /*Methods*/
  38. void keygen(unsigned int modulusbits,
  39. paillier_pubkey* pub,
  40. paillier_prvkey* prv,
  41. unsigned int s);
  42. void complete_prvkey( paillier_prvkey* prv,
  43. paillier_pubkey* pub,
  44. unsigned int s );
  45. void generatePrivateKey(void);
  46. void generatePublicKey(void);
  47. void enc(paillier_pubkey* pub,
  48. mpz_t m,
  49. unsigned int s,
  50. mpz_t c
  51. );
  52. void dec( paillier_pubkey* pub,
  53. paillier_prvkey* prv,
  54. mpz_t ct,
  55. unsigned int,
  56. mpz_t i);
  57. void getRandFromfile( int len, mpz_t* val );
  58. void getRandInteger(mpz_t rop, unsigned int length, gmp_randstate_t prng);
  59. void initRandomGenerator();
  60. public:
  61. void e_add(mpz_t res, mpz_t a, mpz_t b, int);
  62. void e_mul_const(mpz_t res, mpz_t a, mpz_t n, int);
  63. static unsigned int securityToModulus(int);
  64. /*Attributs*/
  65. PaillierPublicParameters publicParameters;
  66. /*Methods*/
  67. char* encrypt(unsigned int ui, unsigned int);
  68. char* encrypt(char* data, size_t, unsigned int exponent);
  69. char* encrypt_perftest();
  70. char* decrypt(char* cipheredData, unsigned int rec_lvl, size_t, size_t);
  71. unsigned int getCryptoParams(unsigned int k,set<std::string>& crypto_params);
  72. unsigned int getAllCryptoParams(set<std::string>& crypto_params);
  73. long setandgetAbsBitPerCiphertext(unsigned int elt_nbr);
  74. std::string getSerializedCryptoParams(bool shortversion);
  75. PaillierAdapter();
  76. PaillierAdapter(int security_bits, int recLvl);
  77. ~PaillierAdapter(void);
  78. double estimateAbsTime(std::string crypto_param);
  79. AbstractPublicParameters& getPublicParameters(void);
  80. double getDecCost(unsigned int length, unsigned int s);
  81. void setNewParameters(const std::string& crypto_params);
  82. void get_prime_of_size(mpz_t rop, unsigned int size);
  83. };
  84. #endif