79 t(Utils::Config::GetInstance().GetParameter(
"Core.Dgk.t", 160)),
80 l(Utils::Config::GetInstance().GetParameter(
"Core.Dgk.l", 16)),
81 precomputeDecryptionMap(precomputeDecryptionMap) {
91 t(Utils::Config::GetInstance().GetParameter(
"Core.Dgk.t", 160)),
92 l(Utils::Config::GetInstance().GetParameter(
"Core.Dgk.l", 16)) {
107 t(Utils::Config::GetInstance().GetParameter(
"Core.Dgk.t", 160)),
108 l(Utils::Config::GetInstance().GetParameter(
"Core.Dgk.l", 16)),
109 precomputeDecryptionMap(precomputeDecryptionMap) {
163 this->
publicKey.
u = BigInteger(2).GetPow(this->
l + 2).GetNextPrime();
168 this->
privateKey.
vp = RandomProvider::GetInstance().GetMaxLengthRandomPrime(this->
t);
170 this->
privateKey.
vq = RandomProvider::GetInstance().GetMaxLengthRandomPrime(this->
t);
180 BigInteger pRand, qRand;
187 sizeRand = this->
keyLength / 2 - aux.GetSize();
191 throw std::runtime_error(
"Parameter k is too small.");
196 pRand = RandomProvider::GetInstance().GetMaxLengthRandomPrime(sizeRand);
206 sizeRand = this->
keyLength / 2 - aux.GetSize();
210 throw std::runtime_error(
"Parameter k is too small.");
215 qRand = RandomProvider::GetInstance().GetMaxLengthRandomPrime(sizeRand);
231 hRandP = RandomProvider::GetInstance().GetRandomInteger(this->
privateKey.
p);
234 while (hRandP == 1 ||
235 BigInteger::Gcd(hRandP, this->
privateKey.
p) != 1 ||
236 hRandP.GetPowModN(this->privateKey.vp * this->publicKey.u * 2, this->privateKey.p) == 1 ||
237 hRandP.GetPowModN(pRand * this->
publicKey.
u * 2, this->privateKey.p) == 1 ||
238 hRandP.GetPowModN(pRand * this->
privateKey.
vp * 2, this->privateKey.p) == 1 ||
239 hRandP.GetPowModN(pRand * this->
privateKey.
vp * this->publicKey.u, this->privateKey.p) == 1);
244 hRandQ = RandomProvider::GetInstance().GetRandomInteger(this->
privateKey.
q);
247 while (hRandQ == 1 ||
248 BigInteger::Gcd(hRandQ, this->
privateKey.
q) != 1 ||
249 hRandQ.GetPowModN(this->privateKey.vq * this->publicKey.u * 2, this->privateKey.q) == 1 ||
250 hRandQ.GetPowModN(qRand * this->
publicKey.
u * 2, this->privateKey.q) == 1 ||
251 hRandQ.GetPowModN(qRand * this->
privateKey.
vq * 2, this->privateKey.q) == 1 ||
252 hRandQ.GetPowModN(qRand * this->
privateKey.
vq * this->publicKey.u, this->privateKey.q) == 1);
258 this->publicKey.h = hRand.GetPowModN(BigInteger(2) * this->
publicKey.
u * pRand * qRand, this->publicKey.n);
263 gRandP = RandomProvider::GetInstance().GetRandomInteger(this->
privateKey.
p);
267 while (gRandP == 1 ||
268 BigInteger::Gcd(gRandP, this->
privateKey.
p) != 1 ||
269 gRandP.GetPowModN(this->privateKey.vp * this->publicKey.u * 2, this->privateKey.p) == 1 ||
270 gRandP.GetPowModN(pRand * this->
publicKey.
u * 2, this->privateKey.p) == 1 ||
271 gRandP.GetPowModN(pRand * this->
privateKey.
vp * 2, this->privateKey.p) == 1 ||
272 gRandP.GetPowModN(pRand * this->
privateKey.
vp * this->publicKey.u, this->privateKey.p) == 1);
277 gRandQ = RandomProvider::GetInstance().GetRandomInteger(this->
privateKey.
q);
281 while (gRandQ == 1 ||
282 BigInteger::Gcd(gRandQ, this->
privateKey.
q) != 1 ||
283 gRandQ.GetPowModN(this->privateKey.vq * this->publicKey.u * 2, this->privateKey.q) == 1 ||
284 gRandQ.GetPowModN(qRand * this->
publicKey.
u * 2, this->privateKey.q) == 1 ||
285 gRandQ.GetPowModN(qRand * this->
privateKey.
vq * 2, this->privateKey.q) == 1 ||
286 gRandQ.GetPowModN(qRand * this->
privateKey.
vq * this->publicKey.u, this->privateKey.q) == 1);
292 this->publicKey.g = gRand.GetPowModN(pRand * qRand * 2, this->publicKey.n);
311 throw std::runtime_error(
"This operation requires the private key.");
315 throw std::runtime_error(
"This operation requires the decryption map.");
321 BigInteger cPowVpModP = ciphertext.
data.GetPowModN(this->
privateKey.
vp, this->privateKey.p);
324 if (cPowVpModP == 1) {
331 DecryptionMap::const_iterator iterator = this->
decryptionMap.find(cPowVpModP);
335 output = BigInteger(iterator->second);
339 throw std::runtime_error(
"Can't decrypt ciphertext.");
419 BigInteger random = RandomProvider::GetInstance().GetRandomInteger(2 * this->
t);
465 throw std::runtime_error(
"This operation requires the private key.");
468 BigInteger test = ciphertext.
data.GetPowModN(this->
privateKey.
vp, this->privateKey.p);
470 return test == 1 ?
true :
false;
479 if (this->l < 8 || this->
l > 32) {
480 throw std::runtime_error(
"The l parameter must obey the following constraints: 8 <= l <= 32.");
482 if (this->t <= this->
l) {
483 throw std::runtime_error(
"Parameter t must be greater than l.");
485 if (this->keyLength <= this->
t) {
486 throw std::runtime_error(
"Parameter k must be greater than t.");
490 throw std::runtime_error(
"The k parameter must be even.");
497 if (this->
keyLength / 2 < this->l + this->t + 10) {
498 throw std::runtime_error(
"Choose parameters k, l, t such that k / 2 >= l + t + 10.");
512 for (BigInteger i = 0; i < this->
publicKey.
u; ++i) {
523 catch (std::runtime_error) {
525 throw std::runtime_error(
"p and q are not coprime.");
DgkCiphertext Ciphertext
Provide public access to the T_Ciphertext type.
DecryptionMap decryptionMap
Contains all possible values of , where , and it is required for decryption.
virtual void validateParameters()
Validate configuration parameters.
Dgk(const bool precomputeDecryptionMap=false)
RandomizerCache< RandomizerContainer< CryptoProvider< DgkPublicKey, DgkPrivateKey, DgkCiphertext, DgkRandomizer >, RandomizerCacheParameters > > RandomizerCacheType
Data type of the randomizer cache.
virtual size_t GetMessageSpaceSize() const
Returns the message space bit size.
unsigned int keyLength
The key length in bits.
BigInteger qTimesQInvModP
Contains .
The public key container structure for the Dgk cryptosystem.
virtual DgkCiphertext EncryptInteger(const BigInteger &plaintext) const
Encrypt an integer and apply randomization.
virtual void doPrecomputations()
Precompute values for speedups.
virtual Ciphertext RandomizeCiphertext(const Ciphertext &ciphertext) const
Randomize encrypted number with a self-generated random value.
DgkPrivateKey privateKey
Private key container.
virtual Ciphertext EncryptIntegerNonrandom(const BigInteger &plaintext) const
Encrypt number without randomization.
The randomizer type for DGK.
DgkCiphertext encryptedZero
Contains [0] used as initializer for homomorphic addition accumulators. Precompute it for optimizatio...
DgkRandomizer()
Default constructor.
virtual const BigInteger & GetMessageSpaceUpperBound() const
Returns the message space upper bound.
virtual Randomizer GetRandomizer() const
Compute the random factor required for the encryption operation.
const unsigned int l
Parameter .
bool IsEncryptedZero(const Ciphertext &ciphertext) const
Determines if ciphertext contains an encryption of 0 or not.
std::unique_ptr< RandomizerCacheType > randomizerCache
Lazy loading randomizer cache.
bool precomputeDecryptionMap
If true, full decryptions are enabled and the decryption map is (pre)computed.
BigInteger data
The ciphertext data.
virtual BigInteger DecryptInteger(const Ciphertext &ciphertext) const
virtual bool GenerateKeys()
Generate the public and private keys.
CiphertextBase template class.
DgkCiphertext encryptedOne
Contains [1].
BigInteger positiveNegativeBoundary
Contains the delimiter between positive and negative values in the message space (usually ) ...
The private key container structure for the Dgk cryptosystem.
DgkRandomizer Randomizer
Provide public access to the T_Randomizer type.
const BigInteger & GetEncryptionModulus() const
Returns the modulus required for reducing the encryption after randomization.
DgkPublicKey publicKey
Public key container.
BigInteger u
- The message space upper bound
Template abstract base class for homomorphic encryption primitives.
DgkCiphertext()
Default constructor.
std::shared_ptr< BigInteger > encryptionModulus
The encryption modulus.
bool hasPrivateKey
Boolean flag that enables decryption if the private key is present.
const unsigned int t
Parameter .
BigInteger pTimesPInvModQ
Contains .