13#ifndef PAILLIER_CRYPTOSYSTEM
14#define PAILLIER_CRYPTOSYSTEM
31template <
typename T_in,
typename T_out>
61 static std::random_device rd;
62 static std::mt19937 gen(rd());
63 std::uniform_int_distribution<std::uint64_t> dis(min, max);
80 bitset<BITSETSIZE> bits = bitset<BITSETSIZE>(e);
121 std::vector<uint64_t> result;
122 for (uint64_t i = 0; i < n; i++)
144 std::vector<uint64_t> result;
146 uint64_t x, r = 0, mu = 0;
147 for (uint64_t g = 0; g < n; g++)
152 uint64_t l = (u - 1) / n;
179 uint64_t g = 0, r = 1, r2 = 1, mu = 0;
180 while (r != 0 && r2 != 0 && mu == 0)
182 i_position = rand() % set.size();
183 g = set.at(i_position);
203 int i_position = rand() % set.size();
204 return set.at(i_position);
215 uint64_t
L_64t(uint64_t x, uint64_t n)
215 uint64_t
L_64t(uint64_t x, uint64_t n) {
…}
244 for (uint64_t x = 1; x < n; x++)
246 if ((a * x) % n == 1)
287 }
while (r >= 1 &&
gcd_64t(r, n) != 1);
301 std::vector<uint64_t> ZNZStar;
302 for (uint64_t i = 1; i < n; i++)
306 ZNZStar.push_back(i);
323 std::vector<uint64_t> ZN2ZStar;
324 for (uint64_t g = 1; g < n * n; g++)
331 ZN2ZStar.push_back(g);
369 void generateMu_64t(uint64_t &mu,
const uint64_t &g,
const uint64_t &lambda,
const uint64_t &n)
372 uint64_t l = (u - 1) / n;
369 void generateMu_64t(uint64_t &mu,
const uint64_t &g,
const uint64_t &lambda,
const uint64_t &n) {
…}
387 void generatePrivateKey_64t(uint64_t &lambda, uint64_t &mu,
const uint64_t &p,
const uint64_t &q,
const uint64_t &n,
const uint64_t &g)
389 lambda =
lcm_64t(p - 1, q - 1);
387 void generatePrivateKey_64t(uint64_t &lambda, uint64_t &mu,
const uint64_t &p,
const uint64_t &q,
const uint64_t &n,
const uint64_t &g) {
…}
409 if (m >= std::numeric_limits<uint64_t>::max())
411 throw std::runtime_error(
"Erreur m ne peut pas être stocké dans 64 bits.");
413 uint64_t m_64 =
static_cast<uint64_t
>(m);
427 c = (fm1 * fm2) % (n * n);
429 if (c >= std::numeric_limits<T_out>::max())
431 throw std::runtime_error(
"Erreur le résultat ne peut pas être stocké dans n*2 bits.");
433 return static_cast<T_out
>(c);
450 if (m >= std::numeric_limits<uint64_t>::max())
452 throw std::runtime_error(
"Erreur m ne peut pas être stocké dans 64 bits.");
454 uint64_t m_64 =
static_cast<uint64_t
>(m);
459 c = (fm1 * fm2) % (n * n);
461 if (c >= std::numeric_limits<T_out>::max())
463 throw std::runtime_error(
"Erreur le résultat ne peut pas être stocké dans n*2 bits.");
465 return static_cast<T_out
>(c);
482 if (c >= std::numeric_limits<uint64_t>::max())
484 throw std::runtime_error(
"Erreur m ne peut pas être stocké dans 64 bits.");
486 uint64_t c_64 =
static_cast<uint64_t
>(c);
489 uint64_t result = ((
fastMod_64t(c_64, lambda, n * n) - 1) / n) * mu % n;
491 if (result >= std::numeric_limits<T_in>::max())
493 throw std::runtime_error(
"Erreur le résultat ne peut pas être stocké dans 8 bits.");
495 return static_cast<T_in
>(result);
#define BITSETSIZE
Definition Paillier.hpp:11
This class implements the Paillier cryptosystem.
Definition Paillier.hpp:33
std::vector< uint64_t > get_set_ZN2ZStar(uint64_t n, uint64_t lambda)
Return the set Z/n²Z* that satisfy the condition gcd(L(g^lambda mod n²), n) = 1 as a vector.
Definition Paillier.hpp:321
uint64_t choose_g_in_vec_64t(std::vector< uint64_t > &set, const uint64_t &n, const uint64_t &lambda)
Choose g from the set of elements in (Z/n²Z)* that satisfy the condition L(x) = (x-1)/n where x is a ...
Definition Paillier.hpp:175
void generatePrivateKey_64t(uint64_t &lambda, uint64_t &mu, const uint64_t &p, const uint64_t &q, const uint64_t &n, const uint64_t &g)
Generate private key for Paillier cryptosystem.
Definition Paillier.hpp:387
T_out paillierEncryption(uint64_t n, uint64_t g, T_in m)
Encrypt a message using Paillier cryptosystem.
Definition Paillier.hpp:407
uint64_t lcm_64t(uint64_t a, uint64_t b)
Calculate the least common multiple (LCM) of two 64-bit unsigned integers.
Definition Paillier.hpp:228
uint64_t L_64t(uint64_t x, uint64_t n)
Calculate L(x).
Definition Paillier.hpp:215
uint64_t fastMod_64t(uint64_t x, uint64_t e, uint64_t n)
Calculate the modular exponentiation of a base raised to a power modulo a modulus.
Definition Paillier.hpp:77
std::vector< uint64_t > calc_set_same_remainder_divide_euclide_64t_v2(uint64_t n, const uint64_t &lambda)
Calculate the set of elements in (Z/n²Z)* that satisfy the condition L(x) = (x-1)/n where x is a posi...
Definition Paillier.hpp:142
uint64_t pow_uint64_t(uint64_t x, uint64_t n)
Calculate the power of a 64-bit unsigned integer.
Definition Paillier.hpp:262
std::vector< uint64_t > calc_set_same_remainder_divide_euclide_64t(uint64_t n)
Calculate the set of elements in (Z/n²Z)*.
Definition Paillier.hpp:119
uint64_t modInverse_64t(uint64_t a, uint64_t n)
Calculate the modular inverse of a 64-bit unsigned integer modulo a modulus.
Definition Paillier.hpp:241
Paillier()
Construct a new Paillier object.
Definition Paillier.hpp:40
uint64_t choose_g_in_vec_64t_v2(std::vector< uint64_t > &set)
Choose g from the set of elements in (Z/n²Z)*.
Definition Paillier.hpp:201
T_in paillierDecryption(uint64_t n, uint64_t lambda, uint64_t mu, T_out c)
Decrypt a ciphertext using Paillier cryptosystem.
Definition Paillier.hpp:480
uint64_t generate_g_64t(uint64_t n, uint64_t lambda)
Generate g for Paillier cryptosystem.
Definition Paillier.hpp:347
uint64_t random64(uint64_t min, uint64_t max)
Generate a random 64-bit unsigned integer.
Definition Paillier.hpp:59
uint64_t gcd_64t(uint64_t a, uint64_t b)
Calculate the greatest common divisor (GCD) of two 64-bit unsigned integers.
Definition Paillier.hpp:101
std::vector< uint64_t > get_set_ZNZStar(uint64_t n)
Return the set Z/nZ* as a vector.
Definition Paillier.hpp:299
T_out paillierEncryption(uint64_t n, uint64_t g, T_in m, uint64_t r)
Encrypt a message using Paillier cryptosystem with a given random value.
Definition Paillier.hpp:448
~Paillier()
Destroy the Paillier object.
Definition Paillier.hpp:48
uint64_t randomZNStar(uint64_t n)
Choose a random element from the set Z/nZ*.
Definition Paillier.hpp:281
void generateMu_64t(uint64_t &mu, const uint64_t &g, const uint64_t &lambda, const uint64_t &n)
Generate Mu for Paillier cryptosystem.
Definition Paillier.hpp:369