SeComLib
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
private_recommendations_utils/secure_comparison_server.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 //avoid circular includes
32 
33 namespace SeComLib {
34 namespace PrivateRecommendationsUtils {
35 
45  SecureComparisonServer::SecureComparisonServer (const Paillier &paillierCryptoProvider, const Dgk &dgkCryptoProvider, const std::string &configurationPath) :
46  paillierCryptoProvider(paillierCryptoProvider),
47  dgkCryptoProvider(dgkCryptoProvider),
48  l(Utils::Config::GetInstance().GetParameter<size_t>(configurationPath + ".l")),
49  minusThreshold(paillierCryptoProvider.GetEncryptedZero(false)),//set this member to [0], since we won't be using it in this case
50  twoPowL(BigInteger(2).GetPow(static_cast<unsigned long>(l))),
51  encryptedTwoPowL(paillierCryptoProvider.EncryptInteger(twoPowL)),
52  blindingFactorCache(paillierCryptoProvider, ComparisonBlindingFactorCacheParameters(configurationPath + ".BlindingFactorCache", l)),
53  dgkComparisonServer(std::make_shared<DgkComparisonServer>(paillierCryptoProvider, dgkCryptoProvider, l)) {
54  }
55 
64  SecureComparisonServer::SecureComparisonServer (const Paillier &paillierCryptoProvider, const Dgk &dgkCryptoProvider, const BigInteger &similarityTreshold, const std::string &configurationPath) :
65  paillierCryptoProvider(paillierCryptoProvider),
66  dgkCryptoProvider(dgkCryptoProvider),
67  l(Utils::Config::GetInstance().GetParameter<size_t>(configurationPath + ".l")),
68  minusThreshold(paillierCryptoProvider.EncryptInteger(-similarityTreshold)),
69  twoPowL(BigInteger(2).GetPow(static_cast<unsigned long>(l))),
70  encryptedTwoPowL(paillierCryptoProvider.EncryptInteger(twoPowL)),
71  blindingFactorCache(paillierCryptoProvider, ComparisonBlindingFactorCacheParameters(configurationPath + ".BlindingFactorCache", l)),
72  dgkComparisonServer(std::make_shared<DgkComparisonServer>(paillierCryptoProvider, dgkCryptoProvider, l)) {
73  }
74 
81  return this->compare(a, -b);
82  }
83 
93  return this->compare(similarityValue, this->minusThreshold);
94  }
95 
105  Paillier::Ciphertext d = encryptedTwoPowL + a + minusB;
106 
107  const BlindingFactorContainer &blindingFactorContainer = this->blindingFactorCache.Pop();
108 
110  Paillier::Ciphertext z = d + blindingFactorContainer.encryptedR;
111 
113  Paillier::Ciphertext zDivTwoPowL = this->secureComparisonClient.lock()->ComputeZDivTwoPowL(z);
114 
116  Paillier::Ciphertext t = this->dgkComparisonServer->Compare(blindingFactorContainer.rModTwoPowL);
117 
119  Paillier::Ciphertext gamma = zDivTwoPowL - (blindingFactorContainer.encryptedRDivTwoPowL + t);
120 
121  /*
122  std::cout << "sim(A,B): "; this->secureComparisonClient.lock()->DebugPaillierEncryption(similarityValue);
123  std::cout << "-delta: "; this->secureComparisonClient.lock()->DebugPaillierEncryption(minusThreshold);
124  std::cout << "2^l: "; this->secureComparisonClient.lock()->DebugPaillierEncryption(encryptedTwoPowL);
125  std::cout << "d: "; this->secureComparisonClient.lock()->DebugPaillierEncryption(d);
126  std::cout << "z: "; this->secureComparisonClient.lock()->DebugPaillierEncryption(z);
127  std::cout << "z div 2^l: "; this->secureComparisonClient.lock()->DebugPaillierEncryption(zDivTwoPowL);
128  std::cout << "r (mod 2^l): " << blindingFactorContainer.rModTwoPowL.ToString(10) << std::endl;
129  std::cout << "r div 2^l: "; this->secureComparisonClient.lock()->DebugPaillierEncryption(blindingFactorContainer.encryptedRDivTwoPowL);
130  std::cout << "t: "; this->secureComparisonClient.lock()->DebugPaillierEncryption(t);
131  std::cout << "gamma: "; this->secureComparisonClient.lock()->DebugPaillierEncryption(gamma);
132  */
133 
134  return gamma;
135  }
136 
140  void SecureComparisonServer::SetClient (const std::shared_ptr<SecureComparisonClient> &secureComparisonClient) {
141  this->secureComparisonClient = secureComparisonClient;
142  this->dgkComparisonServer->SetClient(secureComparisonClient->GetDgkComparisonClient());
143  }
144 
148  const std::shared_ptr<DgkComparisonServer> &SecureComparisonServer::GetDgkComparisonServer () const {
149  return this->dgkComparisonServer;
150  }
151 
152 }//namespace PrivateRecommendationsUtils
153 }//namespace SeComLib
Implementation of the public-key DGK Cryptosystem.
Definition: dgk.h:104
const std::shared_ptr< DgkComparisonServer > dgkComparisonServer
A reference to the DgkComparisonServer.
void SetClient(const std::shared_ptr< SecureComparisonClient > &secureComparisonClient)
Setter for this->secureComparisonClient.
const std::shared_ptr< DgkComparisonServer > & GetDgkComparisonServer() const
Getter for this->dgkComparisonServer.
Paillier::Ciphertext Compare(const Paillier::Ciphertext &a, const Paillier::Ciphertext &b)
Interactive secure comparison of two encrypted values (for generic applications)
Paillier::Ciphertext compare(const Paillier::Ciphertext &a, const Paillier::Ciphertext &minusB)
Interactive secure comparison implementation.
SecureComparisonServer(const Paillier &paillierCryptoProvider, const Dgk &dgkCryptoProvider, const std::string &configurationPath)
Constructor for Compare(a, b)
Definition of class SecureComparisonServer.
Implementation of the public-key Paillier Cryptosystem.
Definition: paillier.h:103
std::weak_ptr< const SecureComparisonClient > secureComparisonClient
A reference to the SecureComparisonClient.
RandomizerCache< BlindingFactorContainer > blindingFactorCache
Blinding factor cache instance.
Definition of class SecureComparisonClient.