NFLLWEPublicParameters.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #include "NFLLWEPublicParameters.hpp"
  18. #include "NFLLWE.hpp"
  19. #include <string.h>
  20. #include "NFLParams.hpp"
  21. using namespace std;
  22. NFLLWEPublicParameters::NFLLWEPublicParameters()//:
  23. //polyDegree(0)
  24. {
  25. cryptoName = "LWE";
  26. crypto_container=nullptr;
  27. // -1 means uninitialized, 0 means no absorption possible
  28. absPerCoordinateBitsize=-1;
  29. noise_ub=0;
  30. }
  31. NFLLWEPublicParameters::NFLLWEPublicParameters(unsigned int modulusBitsize_, unsigned int polyDegree_, int absPCBitsize_):
  32. absPerCoordinateBitsize(absPCBitsize_)
  33. {
  34. crypto_container=nullptr;
  35. cryptoName = "LWE";
  36. }
  37. // Function that sets the modulus and polyDegree from a raw description sent over the network
  38. // Expected structure of rawPubKey (uint modulusREPRESATIONBitsize):(uint modulus)
  39. void NFLLWEPublicParameters::setModulus(char* rawPubKey)
  40. {
  41. // MOK 05122013 there is no need for setting the public key in NFLLWE
  42. }
  43. void NFLLWEPublicParameters::setMockedPubKey()
  44. {
  45. // MOK 05122013 there is no need for setting the public key in NFLLWE
  46. }
  47. // Getters
  48. unsigned int NFLLWEPublicParameters::getmodulusBitsize() { return crypto_container->getmodulusBitsize(); }
  49. uint64_t* NFLLWEPublicParameters::getmoduli() { return crypto_container->getmoduli(); }
  50. uint64_t NFLLWEPublicParameters::getnoiseUB() { return noise_ub; }
  51. uint64_t NFLLWEPublicParameters::getsecurityBits() { return crypto_container->getsecurityBits(); }
  52. unsigned int NFLLWEPublicParameters::getpolyDegree() { return crypto_container->getpolyDegree(); }
  53. // Setters
  54. void NFLLWEPublicParameters::setnoiseUB(uint64_t noise_upper_bound) { noise_ub = noise_upper_bound;}
  55. void NFLLWEPublicParameters::setAbsPCBitsize(int bitSize_)
  56. {
  57. absPerCoordinateBitsize = bitSize_;
  58. }
  59. void NFLLWEPublicParameters::setsecurityBits(uint64_t security_bits_)
  60. {
  61. crypto_container->setsecurityBits(security_bits_);
  62. }
  63. void NFLLWEPublicParameters::setmodulus(uint64_t modulus_)
  64. {
  65. crypto_container->setmodulus(modulus_);
  66. }
  67. void NFLLWEPublicParameters::setpolyDegree(unsigned int polyDegree_)
  68. {
  69. crypto_container->setpolyDegree(polyDegree_);
  70. }
  71. unsigned int NFLLWEPublicParameters::getModulusRepresentationBitsize()
  72. {
  73. // We represent each 60 bit modulus by a 64 bit integer
  74. return ceil((double)getmodulusBitsize()/kModulusRepresentationBitsize)*kModulusRepresentationBitsize;
  75. }
  76. unsigned int NFLLWEPublicParameters::getSerializedModulusBitsize()
  77. {
  78. return getModulusRepresentationBitsize();
  79. }
  80. // Expected format of the parameters
  81. // k:polyDegree:modululusBitsize:AbsorptionBitsize
  82. void NFLLWEPublicParameters::setNewParameters(std::string crypto_param_descriptor)
  83. {
  84. // We want to get rid of public parameter objects so we transfer the most
  85. // we can to the crypto object
  86. crypto_container->setNewParameters(crypto_param_descriptor);
  87. }
  88. // Get a serialized version of the parameters
  89. std::string NFLLWEPublicParameters::getSerializedParams(bool shortversion)
  90. {
  91. std::string params;
  92. // Name:security:degree:modulusbitsize
  93. // WARNING send modulus representation
  94. params = cryptoName + ":" + std::to_string(getsecurityBits()) + ":" + std::to_string(getpolyDegree()) + ":" + std::to_string(getmodulusBitsize());
  95. if (!shortversion)
  96. {
  97. // Add :abs_per_coordinate if defined or :? otherwise
  98. if (absPerCoordinateBitsize==-1) params += ":?";
  99. else params += ":" + std::to_string(absPerCoordinateBitsize);
  100. }
  101. return params;
  102. }
  103. char* NFLLWEPublicParameters::getByteModulus()
  104. {
  105. char* byte_pub_key = new char[getpolyDegree() * sizeof(uint64_t)]();
  106. memcpy(byte_pub_key, &P64, getpolyDegree() * sizeof(uint64_t));
  107. return byte_pub_key;
  108. }
  109. void NFLLWEPublicParameters::getParameters()
  110. {
  111. }
  112. unsigned int NFLLWEPublicParameters::getAbsorptionBitsize()
  113. {
  114. return (absPerCoordinateBitsize < 0) ? 0 : getpolyDegree() * absPerCoordinateBitsize;
  115. }
  116. unsigned int NFLLWEPublicParameters::getAbsorptionBitsize(unsigned int i)
  117. {
  118. return (absPerCoordinateBitsize < 0) ? 0 : getpolyDegree() * absPerCoordinateBitsize;
  119. }
  120. unsigned int NFLLWEPublicParameters::getCiphertextBitsize()
  121. {
  122. return getModulusRepresentationBitsize() * getpolyDegree() * 2 ;
  123. }
  124. unsigned int NFLLWEPublicParameters::getCiphBitsizeFromRecLvl(unsigned int d)
  125. {
  126. return getCiphertextBitsize() ;
  127. }
  128. unsigned int NFLLWEPublicParameters::getQuerySizeFromRecLvl(unsigned int)
  129. {
  130. return getCiphertextBitsize();
  131. }
  132. void NFLLWEPublicParameters::computeNewParameters(const std::string& crypto_param_descriptor)
  133. {
  134. setNewParameters(crypto_param_descriptor);
  135. }