NFLLWE.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_NFLLWE
  18. #define DEF_NFLLWE
  19. #define SHOUP
  20. //#define TESTSHOUP
  21. #include <omp.h>
  22. #include <inttypes.h>
  23. #include <stdlib.h>
  24. #include <math.h>
  25. #include <iostream>
  26. #include "NFLParams.hpp"
  27. #include "NFLlib.hpp"
  28. #include "NFLLWEDatatypes.hpp"
  29. #include "LatticesBasedCryptosystem.hpp"
  30. #include "crypto/HomomorphicCrypto.hpp"
  31. #include "CryptographicSystem.hpp"
  32. #include "NFLLWEPublicParameters.hpp"
  33. #include <string>
  34. #include <cstddef>
  35. #include <gmp.h>
  36. class NFLLWE : public LatticesBasedCryptosystem
  37. {
  38. public:
  39. NFLLWEPublicParameters publicParams;
  40. NFLLWE();
  41. ~NFLLWE();
  42. std::string& toString();
  43. unsigned int getpolyDegree();
  44. poly64* getsecretKey();
  45. void recomputeNoiseAmplifiers();
  46. // Setters
  47. void setmodulus(uint64_t modulus);
  48. void setpolyDegree(unsigned int polyDegree);
  49. void setNewParameters(const std::string& crypto_param_descriptor);
  50. void setNewParameters(unsigned int polyDegree, unsigned int modulusBitsize, int absPCBitsize_);
  51. // Crypto related functions
  52. long setandgetAbsBitPerCiphertext(unsigned int elt_nbr);
  53. void enc(lwe_cipher *c, poly64 m);
  54. void dec(poly64 m, lwe_cipher *c);
  55. char* encrypt(unsigned int ui, unsigned int );
  56. char* encrypt(char* data, size_t, unsigned int exponent );
  57. char* encrypt_perftest();
  58. char* decrypt(char* cipheredData, unsigned int, size_t, size_t);
  59. // Data importation and exportation
  60. poly64* deserializeDataNFL(unsigned char **inArrayOfBuffers, uint64_t nbrOfBuffers,
  61. uint64_t dataBitsizePerBuffer, uint64_t &polyNumber);
  62. // Functions for PIROptimizer and PIRClient
  63. std::string getSerializedCryptoParams(bool shortversion);
  64. unsigned int getCryptoParams(unsigned int k, std::set<std::string>& crypto_params);
  65. unsigned int getAllCryptoParams(std::set<std::string>& crypto_params);
  66. AbstractPublicParameters& getPublicParameters();
  67. unsigned int findMaxModulusBitsize(unsigned int security_bits, unsigned int poly_degree);
  68. bool checkParamsSecure(unsigned int security_bits, unsigned int poly_degree, unsigned int p_size);
  69. double lllOutput(unsigned int n, double& p, double delta);
  70. double estimateAbsTime(std::string crypto_param);
  71. double estimatePrecomputeTime(std::string crypto_param);
  72. unsigned int estimateSecurity(unsigned int n, unsigned int p_size);
  73. unsigned int getmodulusBitsize();
  74. // **********************************
  75. // Modular ciphertext manipulation
  76. // **********************************
  77. // Additions
  78. void add(lwe_cipher rop, lwe_cipher op1, lwe_cipher op2, int d);
  79. // Fused Multiplications-Additions
  80. void mulandadd(lwe_cipher rop, lwe_in_data op1, lwe_query op2, int rec_lvl);
  81. void mulandadd(lwe_cipher rop, lwe_in_data op1, lwe_query op2, uint64_t current_poly,
  82. int rec_lvl);
  83. //Shoup version
  84. void mulandadd(lwe_cipher rop, lwe_in_data op1, lwe_query op2, lwe_query op2prime,
  85. uint64_t current_poly, int rec_lvl);
  86. void mul(lwe_cipher rop, lwe_in_data op1, lwe_query op2, lwe_query op2prime,
  87. uint64_t current_poly, int rec_lvl);
  88. void mulandaddCiphertextNTT(lwe_cipher rop, lwe_in_data op1, lwe_query op2);
  89. void mulandaddCiphertextNTT(lwe_cipher rop, lwe_in_data op1, lwe_query op2,
  90. uint64_t current_poly);
  91. private:
  92. // Attributes
  93. unsigned int oldNbModuli;
  94. unsigned int polyDegree;
  95. poly64 *secretKey; // The secret key
  96. poly64 *secretKeyShoup; // The secret key Shoupified
  97. uint64_t *Abit_mod,*Abit_mod_shoup;
  98. void clearSecretKeys();
  99. };
  100. #endif