30 #ifndef CRYPTO_PROVIDER_IMPLEMENTATION_GUARD
31 #define CRYPTO_PROVIDER_IMPLEMENTATION_GUARD
38 template <
typename T_PublicKey,
typename T_PrivateKey,
typename T_Ciphertext,
typename T_Randomizer>
40 keyLength(keyLength), hasPrivateKey(true), precomputeSpeedupValues(false) {
49 template <
typename T_PublicKey,
typename T_PrivateKey,
typename T_Ciphertext,
typename T_Randomizer>
51 publicKey(publicKey), keyLength(keyLength), hasPrivateKey(false), precomputeSpeedupValues(true) {
61 template <
typename T_PublicKey,
typename T_PrivateKey,
typename T_Ciphertext,
typename T_Randomizer>
63 publicKey(publicKey), privateKey(privateKey), keyLength(keyLength), hasPrivateKey(true), precomputeSpeedupValues(true) {
70 template <
typename T_PublicKey,
typename T_PrivateKey,
typename T_Ciphertext,
typename T_Randomizer>
72 return this->RandomizeCiphertext(this->EncryptIntegerNonrandom(plaintext));
75 #ifdef ENABLE_CRYPTO_PROVIDER_HOMOMORPHIC_OPERATIONS
82 template <
typename T_PublicKey,
typename T_PrivateKey,
typename T_Ciphertext,
typename T_Randomizer>
84 return (lhs * rhs) % this->GetEncryptionModulus();
92 template <
typename T_PublicKey,
typename T_PrivateKey,
typename T_Ciphertext,
typename T_Randomizer>
94 return input.GetInverseModN(this->GetEncryptionModulus());
103 template <
typename T_PublicKey,
typename T_PrivateKey,
typename T_Ciphertext,
typename T_Randomizer>
104 T_Ciphertext CryptoProvider<T_PublicKey, T_PrivateKey, T_Ciphertext, T_Randomizer>::HomomorphicSubtract (
const T_Ciphertext &lhs,
const T_Ciphertext &rhs)
const {
105 return (lhs * rhs.GetInverseModN(this->GetEncryptionModulus())) % this->GetEncryptionModulus();
115 template <
typename T_PublicKey,
typename T_PrivateKey,
typename T_Ciphertext,
typename T_Randomizer>
116 T_Ciphertext CryptoProvider<T_PublicKey, T_PrivateKey, T_Ciphertext, T_Randomizer>::HomomorphicMultiply (
const T_Ciphertext &lhs,
const BigInteger &rhs)
const {
118 throw std::runtime_error(
"The plaintext term should not be 0.");
121 return lhs.GetPowModN(rhs, this->GetEncryptionModulus());
128 template <
typename T_PublicKey,
typename T_PrivateKey,
typename T_Ciphertext,
typename T_Randomizer>
130 return *this->encryptionModulus;
136 template <
typename T_PublicKey,
typename T_PrivateKey,
typename T_Ciphertext,
typename T_Randomizer>
138 return this->positiveNegativeBoundary;
144 template <
typename T_PublicKey,
typename T_PrivateKey,
typename T_Ciphertext,
typename T_Randomizer>
146 return this->publicKey;
152 template <
typename T_PublicKey,
typename T_PrivateKey,
typename T_Ciphertext,
typename T_Randomizer>
154 return this->privateKey;
161 template <
typename T_PublicKey,
typename T_PrivateKey,
typename T_Ciphertext,
typename T_Randomizer>
164 return this->RandomizeCiphertext(this->encryptedZero);
167 return this->encryptedZero;
175 template <
typename T_PublicKey,
typename T_PrivateKey,
typename T_Ciphertext,
typename T_Randomizer>
178 return this->RandomizeCiphertext(this->encryptedOne);
181 return this->encryptedOne;
188 #endif//CRYPTO_PROVIDER_IMPLEMENTATION_GUARD
Ciphertext GetEncryptedOne(const bool randomized=true) const
Returns [1].
Ciphertext GetEncryptedZero(const bool randomized=true) const
Returns [0].
const T_PublicKey & GetPublicKey() const
Public key getter.
virtual T_Ciphertext EncryptInteger(const BigInteger &plaintext) const
Encrypt an integer and apply randomization.
virtual const BigInteger & GetPositiveNegativeBoundary() const
Returns the biggest positive number that can be encrypted without overflowing.
const T_PrivateKey & GetPrivateKey() const
Private key getter.
const BigInteger & GetEncryptionModulus() const
Returns the modulus required for reducing the encryption after randomization.
Template abstract base class for homomorphic encryption primitives.
CryptoProvider(const unsigned int keyLength)
Constructor.