SeComLib
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
okamoto_uchiyama.cpp
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 #include "okamoto_uchiyama.h"
31 
32 namespace SeComLib {
33 namespace Core {
39  }
40 
45  OkamotoUchiyamaCiphertext::OkamotoUchiyamaCiphertext (const std::shared_ptr<BigInteger> &encryptionModulus) :
46  CiphertextBase<OkamotoUchiyamaCiphertext> (encryptionModulus) {
47  }
48 
54  OkamotoUchiyamaCiphertext::OkamotoUchiyamaCiphertext (const BigInteger &data, const std::shared_ptr<BigInteger> &encryptionModulus) :
55  CiphertextBase<OkamotoUchiyamaCiphertext> (data, encryptionModulus) {
56  }
57 
61  }
62 
68  }
69 
74  CryptoProvider<OkamotoUchiyamaPublicKey, OkamotoUchiyamaPrivateKey, OkamotoUchiyamaCiphertext, OkamotoUchiyamaRandomizer>(Utils::Config::GetInstance().GetParameter("Core.OkamotoUchiyama.keySize", 1024)),
75  messageSpaceSize(Utils::Config::GetInstance().GetParameter<size_t>("Core.OkamotoUchiyama.messageSpaceSize")) {
76  }
77 
84  CryptoProvider<OkamotoUchiyamaPublicKey, OkamotoUchiyamaPrivateKey, OkamotoUchiyamaCiphertext, OkamotoUchiyamaRandomizer>(publicKey, Utils::Config::GetInstance().GetParameter("Core.OkamotoUchiyama.keySize", 1024)),
85  messageSpaceSize(Utils::Config::GetInstance().GetParameter<size_t>("Core.OkamotoUchiyama.messageSpaceSize")) {
86  //precompute values for optimization purposes
87  this->doPrecomputations();
88  }
89 
97  CryptoProvider<OkamotoUchiyamaPublicKey, OkamotoUchiyamaPrivateKey, OkamotoUchiyamaCiphertext, OkamotoUchiyamaRandomizer>(publicKey, privateKey, Utils::Config::GetInstance().GetParameter("Core.OkamotoUchiyama.keySize", 1024)),
98  messageSpaceSize(Utils::Config::GetInstance().GetParameter<size_t>("Core.OkamotoUchiyama.messageSpaceSize")) {
99  //precompute values for optimization purposes
100  this->doPrecomputations();
101  }
102 
111  unsigned int sizeT = Utils::Config::GetInstance().GetParameter("Core.OkamotoUchiyama.sizeT", 160);
112 
114  unsigned int primeLength = static_cast<unsigned int>(this->keyLength / 3);
115  if (sizeT >= primeLength) {
117  throw std::runtime_error("Key size must be larger than the t parameter.");
118  }
119 
121 
122  //pick a random prime t of size specified by sizeT
123  this->privateKey.t = RandomProvider::GetInstance().GetMaxLengthRandomPrime(sizeT);
124 
127  BigInteger u;
128  unsigned int sizeU = primeLength - sizeT;
129  do {
130  //generate a random number in the interval [0, 2^(sizeU - 1))
131  u = RandomProvider::GetInstance().GetRandomInteger(sizeU - 1);
132 
133  //shift number to the interval [2^(sizeU - 1), 2^sizeU)
134  u.SetBit(sizeU - 1);
135 
136  //u and t must divide p - 1
137  this->privateKey.p = this->privateKey.t * u + 1;
138  }
139  while (!this->privateKey.p.IsPrime());
140 
141  //precompute p^2 and store it for the decryption operation
142  this->pSquared = this->privateKey.p.GetPow(2);
143 
145  this->privateKey.q = RandomProvider::GetInstance().GetMaxLengthRandomPrime(primeLength);
146 
148  this->publicKey.n = this->pSquared * this->privateKey.q;
149 
151  //std::cout << this->publicKey.n.GetSize() << std::endl;
152 
154  do {
155  do {
156  //generate random g in the interval [0, n)
157  this->g = RandomProvider::GetInstance().GetRandomInteger(this->publicKey.n);
158  }
159  //ensure that g is in the cyclic group Z_{p^2}*
160  while (BigInteger::Gcd(this->g, this->privateKey.p) != 1);
161 
162  //gp = g^(p - 1) (mod p^2)
163  this->privateKey.gp = this->g.GetPowModN(this->privateKey.p - 1, this->pSquared);
164  }
165  //ensure that gp^p = 1 (mod p^2)
166  while (this->privateKey.gp.GetPowModN(this->privateKey.p, this->pSquared) != 1);
167 
169  this->publicKey.G = this->g.GetPowModN(u, this->publicKey.n);
170 
172  BigInteger gPrime;
173 
174  do {
175  //generate random g' in the interval [0, n)
176  gPrime = RandomProvider::GetInstance().GetRandomInteger(this->publicKey.n);
177  }
178  //ensure that g' is in the cyclic group Z_n*
179  while (BigInteger::Gcd(gPrime, this->publicKey.n) != 1);
180 
181  //H = g'^(n * u) (mod n)
182  this->publicKey.H = gPrime.GetPowModN(this->publicKey.n * u, this->publicKey.n);
183 
184  //precompute values for optimization purposes
185  this->doPrecomputations();
186 
187  return true;
188  }
189 
201  BigInteger OkamotoUchiyama::DecryptInteger (const OkamotoUchiyama::Ciphertext &ciphertext) const {
202  if (!this->hasPrivateKey) {
203  throw std::runtime_error("This operation requires the private key.");
204  }
205 
207  BigInteger output = (this->L(ciphertext.data.GetPowModN(this->privateKey.t, this->pSquared)) * this->lgpInv) % this->privateKey.p;
208 
210  if (output > this->positiveNegativeBoundary) {
211  output -= this->GetMessageSpaceUpperBound();
212  }
213 
214  return output;
215  }
216 
231  Ciphertext output(this->encryptionModulus);
232 
234 
236  if (plaintext < 0) {
237  if (this->hasPrivateKey) {
238  output.data = this->publicKey.G.GetPowModN(this->GetMessageSpaceUpperBound() + plaintext, this->GetEncryptionModulus());
239  }
244  else {
245  output.data = this->publicKey.G.GetPowModN(plaintext.GetAbs(), this->GetEncryptionModulus()).GetInverseModN(this->GetEncryptionModulus());
246  }
247  }
248  else {
249  output.data = this->publicKey.G.GetPowModN(plaintext, this->GetEncryptionModulus());
250  }
251 
252  return output;
253  }
254 
263  return Randomizer(this->publicKey.H.GetPowModN((RandomProvider::GetInstance().GetRandomInteger(this->publicKey.n - 1) + 1), this->publicKey.n));
264  }
265 
273  return Ciphertext((ciphertext.data * this->randomizerCache->Pop().randomizer.data) % this->GetEncryptionModulus(), this->encryptionModulus);
274  }
275 
279  const BigInteger &OkamotoUchiyama::GetMessageSpaceUpperBound () const {
280  return this->messageSpace;
281  }
282 
287  return this->messageSpaceSize;
288  }
289 
295  BigInteger OkamotoUchiyama::L (const BigInteger &input) const {
296  BigInteger output;
297 
298  output = (input - 1) / this->privateKey.p;
299 
300  return output;
301  }
302 
309  if (this->precomputeSpeedupValues) {
311  this->pSquared = this->privateKey.p.GetPow(2);
312  }
313 
314  if (this->hasPrivateKey) {
315  this->messageSpace = this->privateKey.p;
316  this->messageSpaceSize = this->privateKey.p.GetSize();
317 
319  this->lgpInv = this->L(this->privateKey.gp).GetInverseModN(this->privateKey.p);
320  }
326  else {
327  this->messageSpace = BigInteger(2).GetPow(static_cast<unsigned long>(this->messageSpaceSize));
328  }
329 
330  //set the encryption modulus, @f$ n @f$
331  this->encryptionModulus = std::make_shared<BigInteger>(this->publicKey.n);
332 
333  //precompute the limit between positive and negative values in the message space
335 
337  this->randomizerCache = std::unique_ptr<RandomizerCacheType>(new RandomizerCacheType(*this, "Core.RandomizerCache"));
338 
339  this->encryptedZero = this->EncryptInteger(BigInteger(0));
340 
341  this->encryptedOne = this->EncryptInteger(BigInteger(1));
342  }
343 
344 }//namespace Core
345 }//namespace SeComLib
virtual Randomizer GetRandomizer() const
Compute the random factor required for the encryption operation.
RandomizerCache< RandomizerContainer< CryptoProvider< OkamotoUchiyamaPublicKey, OkamotoUchiyamaPrivateKey, OkamotoUchiyamaCiphertext, OkamotoUchiyamaRandomizer >, RandomizerCacheParameters > > RandomizerCacheType
Data type of the randomizer cache.
size_t messageSpaceSize
Contains the bit size of the message space.
virtual bool GenerateKeys()
Generate the public and private keys.
virtual void doPrecomputations()
Precompute values for speedups.
static Config & GetInstance()
Returns a reference to the singleton.
Definition: config.cpp:48
RandomizerBase struct.
BigInteger pSquared
Contains , required for the decryption operation. Precompute it for optimization purposes.
The randomizer type for Okamoto-Uchiyama.
virtual OkamotoUchiyamaCiphertext EncryptInteger(const BigInteger &plaintext) const
Encrypt an integer and apply randomization.
BigInteger lgpInv
Stores precomputed value used to speedup decyption.
OkamotoUchiyamaCiphertext encryptedZero
Contains [0] used as initializer for homomorphic addition accumulators. Precompute it for optimizatio...
virtual Ciphertext RandomizeCiphertext(const Ciphertext &ciphertext) const
Randomize encrypted number with a self-generated random value.
virtual Ciphertext EncryptIntegerNonrandom(const BigInteger &plaintext) const
Encrypt number without randomization.
BigInteger t
, the prime factor of
BigInteger L(const BigInteger &input) const
L function evaluator.
BigInteger g
Stores , required for the decryption operation.
Definition of class OkamotoUchiyama.
virtual size_t GetMessageSpaceSize() const
Returns the message space bit size.
The private key container structure for the Okamoto-Uchiyama cryptosystem.
BigInteger data
The ciphertext data.
virtual const BigInteger & GetMessageSpaceUpperBound() const
Returns the message space upper bound.
bool precomputeSpeedupValues
Boolean flag that indicates wether doPrecomputations() should precompute certain values.
T GetParameter(const std::string &parameter) const
Template method which returns the value of the specified configuration parameter. ...
Definition: config.hpp:41
CiphertextBase template class.
BigInteger positiveNegativeBoundary
Contains the delimiter between positive and negative values in the message space (usually ) ...
const BigInteger & GetEncryptionModulus() const
Returns the modulus required for reducing the encryption after randomization.
virtual BigInteger DecryptInteger(const Ciphertext &ciphertext) const
Decrypt number.
The public key container structure for the Okamoto-Uchiyama cryptosystem.
Template abstract base class for homomorphic encryption primitives.
BigInteger messageSpace
The message space.