PaillierKeys.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "PaillierKeys.hpp"
  18. #include <iostream>
  19. #include <cstring>
  20. /*************** PRVKEY**************/
  21. paillier_prvkey::paillier_prvkey(){
  22. init_key();
  23. }
  24. paillier_prvkey::~paillier_prvkey(){
  25. clear_key();
  26. }
  27. void paillier_prvkey::init_key(){
  28. mpz_inits(d, inv_d, NULL);
  29. }
  30. void paillier_prvkey::clear_key()
  31. {
  32. mpz_clears(d, inv_d, NULL);
  33. }
  34. /*************** PUBKEY**************/
  35. paillier_pubkey::paillier_pubkey() :
  36. bits(0),
  37. init_s(1)
  38. {
  39. init_key();
  40. }
  41. paillier_pubkey::paillier_pubkey(unsigned int bits, char* rawKey) :
  42. bits(0),
  43. init_s(1)
  44. {
  45. init_key(bits, rawKey);
  46. }
  47. void paillier_pubkey::init_key() {
  48. for (int i = 0; i <= MAX_S; i++)
  49. {
  50. mpz_init_set_ui(nj[i],1);
  51. }
  52. mpz_init_set_ui(g,1);
  53. }
  54. void paillier_pubkey::init_key(unsigned int _bits, char* rawKey) {
  55. int init_s_;
  56. bits = _bits;
  57. init_key();
  58. mpz_import(nj[1], _bits / 8, 1, sizeof(char), 0, 0, rawKey);
  59. mpz_add_ui(g, nj[1], 1);
  60. memcpy(&init_s_, rawKey+_bits/8, sizeof(int));
  61. // The client should not be using s above MAX_S
  62. if (init_s_ >= MAX_S)
  63. {
  64. std::cout << "PaillierKeys: WARNING. The client tries to use s>=MAX_S. Setting s=MAX_S-1."<<std::endl;
  65. init_s = MAX_S-1;
  66. }
  67. else init_s = init_s_;
  68. for (int i = 2; i <= init_s+1; i++)
  69. {
  70. mpz_pow_ui(nj[i], nj[1], i);
  71. }
  72. }
  73. //mocked key
  74. void paillier_pubkey::init_key(unsigned int key_bit_size)
  75. {
  76. init_s = 1;
  77. gmp_randstate_t rand;
  78. gmp_randinit_default(rand);
  79. mpz_urandomb(nj[1], rand, key_bit_size);
  80. mpz_add_ui(g, nj[1], 1);
  81. for (int i = 2; i <= init_s+1; i++)
  82. {
  83. mpz_pow_ui(nj[i], nj[1], i);
  84. }
  85. gmp_randclear(rand);
  86. }
  87. inline void paillier_pubkey::init_nj(int i)
  88. {
  89. mpz_pow_ui(nj[i], nj[1], i);
  90. }
  91. paillier_pubkey::~paillier_pubkey(){
  92. mpz_clear(g);
  93. for(int i = 0; i < MAX_S; i++)
  94. mpz_clear(nj[i]);
  95. }
  96. // Complete nj array up to index s_
  97. void paillier_pubkey::complete_key(unsigned int s_){
  98. int s = s_;
  99. // The client should not be using moduli above MAX_S
  100. if (s > MAX_S)
  101. {
  102. std::cerr << "PaillierKeys: WARNING trying to complete keys above MAX_S bounding it to MAX_S" << std::endl;
  103. s = MAX_S;
  104. }
  105. // If g's value has not been initialized do it now
  106. if (mpz_get_ui(g) == 1 ) mpz_add_ui(g, nj[1], 1);
  107. // Initialize the array's values
  108. for (unsigned int i = 2; i <= s ; i++){
  109. // Should we save polar bears ? if (mpz_get_ui(nj[i]) == 1 )
  110. init_nj(i);
  111. }
  112. }
  113. // Provides the ciphertext modulus key i levels above the s defined in the class
  114. // Ugly to return an mpz_t but the function purpose is to ensure that it has the correct
  115. // value before its reference is returned
  116. mpz_t* paillier_pubkey::getnj(int s_)
  117. {
  118. int s = s_;
  119. // The client should not be using moduli above MAX_S
  120. if (s > MAX_S)
  121. {
  122. std::cerr << "PaillierKeys: WARNING trying to get key above MAX_S bounding it to MAX_S" << std::endl;
  123. s = MAX_S;
  124. }
  125. // If the key has been defined do it now
  126. if (mpz_get_ui(nj[s]) == 1 ) init_nj(s);
  127. return &nj[s];
  128. }
  129. // Simple getters
  130. int paillier_pubkey::getinit_s()
  131. {
  132. return init_s;
  133. }
  134. mpz_t* paillier_pubkey::getg()
  135. {
  136. return &g;
  137. }
  138. int paillier_pubkey::getbits()
  139. {
  140. return bits;
  141. }
  142. // Simple setters
  143. void paillier_pubkey::setinit_s(int init_s_)
  144. {
  145. init_s = init_s_;
  146. }
  147. void paillier_pubkey::setbits(int bits_)
  148. {
  149. bits = bits_;
  150. }