PaillierPublicParameters.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "PaillierPublicParameters.hpp"
  18. #include <iostream>
  19. PaillierPublicParameters::PaillierPublicParameters()
  20. {
  21. cryptoName = "Paillier";
  22. securityBits = 0;
  23. }
  24. //PaillierPublicParameters::PaillierPublicParameters(unsigned int bit_key_size) :
  25. // bitKeySize(bit_key_size),
  26. // bitAbsSize(bitKeySize - 8),
  27. // ciphSize(bitKeySize * 2)
  28. //{
  29. // cryptoName = "Paillier";
  30. // securityBits = 0;
  31. //}
  32. PaillierPublicParameters::~PaillierPublicParameters()
  33. {
  34. }
  35. char* PaillierPublicParameters::getByteModulus() {
  36. char *key = new char[(getKeyBitsize() / 8 ) + sizeof(int)]();
  37. mpz_export(key, NULL, 1, sizeof(char) , 0, 0, *pubkey.getnj(1));
  38. int init_s = pubkey.getinit_s();
  39. memcpy(key+(getKeyBitsize()/8), &init_s, sizeof(int));
  40. return key;
  41. }
  42. unsigned int PaillierPublicParameters::getCiphertextSize() {
  43. return ciphSize;
  44. }
  45. unsigned int PaillierPublicParameters::getKeyBitsize() {
  46. return bitKeySize;
  47. }
  48. unsigned int PaillierPublicParameters::getAbsorptionBitsize() {
  49. return bitAbsSize;
  50. }
  51. unsigned int PaillierPublicParameters::getAbsorptionBitsize(unsigned int rec_lvl)
  52. {
  53. if (rec_lvl == 0)
  54. {
  55. return getAbsorptionBitsize();
  56. }
  57. else
  58. {
  59. // If we are being used in a recursive scheme take all the bits
  60. return getKeyBitsize()*(rec_lvl+1);
  61. }
  62. }
  63. paillier_pubkey* PaillierPublicParameters::getPubKey() {
  64. return &pubkey;
  65. }
  66. void PaillierPublicParameters::setModulus(char* rawPubKey){
  67. pubkey.init_key(getKeyBitsize(), rawPubKey);
  68. }
  69. //lvl starts at 1 !!!
  70. unsigned int PaillierPublicParameters::getCiphBitsizeFromRecLvl(unsigned int lvl)
  71. {
  72. return (lvl+pubkey.getinit_s()) * bitKeySize;
  73. }
  74. unsigned int PaillierPublicParameters::getQuerySizeFromRecLvl(unsigned int lvl)
  75. {
  76. return getCiphBitsizeFromRecLvl(lvl);
  77. }
  78. unsigned int PaillierPublicParameters::getSerializedModulusBitsize()
  79. {
  80. return getKeyBitsize() + sizeof(int) * 8;
  81. }
  82. void PaillierPublicParameters::setModulusbits(unsigned int modulusbits_)
  83. {
  84. bitKeySize = modulusbits_ ;
  85. bitAbsSize = (bitKeySize - 8);
  86. ciphSize = (bitKeySize * 2);
  87. }
  88. void PaillierPublicParameters::setSecurityBits(unsigned int securitybits_)
  89. {
  90. securityBits = securitybits_;
  91. }
  92. void PaillierPublicParameters::computeNewParameters(const std::string& crypto_param_descriptor)
  93. {
  94. std::vector<std::string> fields;
  95. boost::algorithm::split(fields, crypto_param_descriptor, boost::algorithm::is_any_of(":"));
  96. securityBits = (unsigned)atoi (fields[1].c_str());
  97. unsigned int keySize = (unsigned)atoi(fields[2].c_str());
  98. setModulusbits(keySize);
  99. }
  100. // Get a serialized version of the parameters
  101. std::string PaillierPublicParameters::getSerializedParams(bool shortversion)
  102. {
  103. std::string params;
  104. // Name:security:plaintext_modulusbitsize:ciphertext_modulusbitsize:abs_bits
  105. params = cryptoName + ":" + std::to_string(securityBits) + ":" + std::to_string(bitKeySize) + ":" + std::to_string(ciphSize);
  106. if (!shortversion) params += ":" + std::to_string(bitAbsSize);
  107. return params;
  108. }
  109. unsigned int PaillierPublicParameters::getCiphertextBitsize()
  110. {
  111. return getCiphBitsizeFromRecLvl(1);
  112. }
  113. void PaillierPublicParameters::setMockedPubKey()
  114. {
  115. pubkey.init_key(getKeyBitsize());
  116. }