SeComLib
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
crypto_provider.hpp
Go to the documentation of this file.
1 /*
2 SeComLib
3 Copyright 2012-2013 TU Delft, Information Security & Privacy Lab (http://isplab.tudelft.nl/)
4 
5 Contributors:
6 Inald Lagendijk (R.L.Lagendijk@TUDelft.nl)
7 Mihai Todor (todormihai@gmail.com)
8 Thijs Veugen (P.J.M.Veugen@tudelft.nl)
9 Zekeriya Erkin (z.erkin@tudelft.nl)
10 
11 Licensed under the Apache License, Version 2.0 (the "License");
12 you may not use this file except in compliance with the License.
13 You may obtain a copy of the License at
14 
15 http://www.apache.org/licenses/LICENSE-2.0
16 
17 Unless required by applicable law or agreed to in writing, software
18 distributed under the License is distributed on an "AS IS" BASIS,
19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 See the License for the specific language governing permissions and
21 limitations under the License.
22 */
30 #ifndef CRYPTO_PROVIDER_IMPLEMENTATION_GUARD
31 #define CRYPTO_PROVIDER_IMPLEMENTATION_GUARD
32 
33 namespace SeComLib {
34 namespace Core {
38  template <typename T_PublicKey, typename T_PrivateKey, typename T_Ciphertext, typename T_Randomizer>
40  keyLength(keyLength), hasPrivateKey(true), precomputeSpeedupValues(false) {
41  }
42 
49  template <typename T_PublicKey, typename T_PrivateKey, typename T_Ciphertext, typename T_Randomizer>
50  CryptoProvider<T_PublicKey, T_PrivateKey, T_Ciphertext, T_Randomizer>::CryptoProvider (const T_PublicKey &publicKey, const unsigned int keyLength) :
51  publicKey(publicKey), keyLength(keyLength), hasPrivateKey(false), precomputeSpeedupValues(true) {
52  }
53 
61  template <typename T_PublicKey, typename T_PrivateKey, typename T_Ciphertext, typename T_Randomizer>
62  CryptoProvider<T_PublicKey, T_PrivateKey, T_Ciphertext, T_Randomizer>::CryptoProvider (const T_PublicKey &publicKey, const T_PrivateKey &privateKey, const unsigned int keyLength) :
63  publicKey(publicKey), privateKey(privateKey), keyLength(keyLength), hasPrivateKey(true), precomputeSpeedupValues(true) {
64  }
65 
70  template <typename T_PublicKey, typename T_PrivateKey, typename T_Ciphertext, typename T_Randomizer>
72  return this->RandomizeCiphertext(this->EncryptIntegerNonrandom(plaintext));
73  }
74 
75 #ifdef ENABLE_CRYPTO_PROVIDER_HOMOMORPHIC_OPERATIONS
76 
82  template <typename T_PublicKey, typename T_PrivateKey, typename T_Ciphertext, typename T_Randomizer>
83  T_Ciphertext CryptoProvider<T_PublicKey, T_PrivateKey, T_Ciphertext, T_Randomizer>::HomomorphicAdd (const T_Ciphertext &lhs, const T_Ciphertext &rhs) const {
84  return (lhs * rhs) % this->GetEncryptionModulus();
85  }
86 
92  template <typename T_PublicKey, typename T_PrivateKey, typename T_Ciphertext, typename T_Randomizer>
94  return input.GetInverseModN(this->GetEncryptionModulus());
95  }
96 
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();
106  }
107 
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 {
117  if (rhs == 0) {
118  throw std::runtime_error("The plaintext term should not be 0.");
119  }
120 
121  return lhs.GetPowModN(rhs, this->GetEncryptionModulus());
122  }
123 #endif
124 
128  template <typename T_PublicKey, typename T_PrivateKey, typename T_Ciphertext, typename T_Randomizer>
130  return *this->encryptionModulus;
131  }
132 
136  template <typename T_PublicKey, typename T_PrivateKey, typename T_Ciphertext, typename T_Randomizer>
138  return this->positiveNegativeBoundary;
139  }
140 
144  template <typename T_PublicKey, typename T_PrivateKey, typename T_Ciphertext, typename T_Randomizer>
146  return this->publicKey;
147  }
148 
152  template <typename T_PublicKey, typename T_PrivateKey, typename T_Ciphertext, typename T_Randomizer>
154  return this->privateKey;
155  }
156 
161  template <typename T_PublicKey, typename T_PrivateKey, typename T_Ciphertext, typename T_Randomizer>
163  if (randomized) {
164  return this->RandomizeCiphertext(this->encryptedZero);
165  }
166  else {
167  return this->encryptedZero;
168  }
169  }
170 
175  template <typename T_PublicKey, typename T_PrivateKey, typename T_Ciphertext, typename T_Randomizer>
177  if (randomized) {
178  return this->RandomizeCiphertext(this->encryptedOne);
179  }
180  else {
181  return this->encryptedOne;
182  }
183 
184  }
185 }//namespace Core
186 }//namespace SeComLib
187 
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.