NFLLWE.hpp 4.3 KB

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