SeComLib
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
private_recommendations_data_packing/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 PrivateRecommendationsDataPacking {
45  SecureComparisonServer::SecureComparisonServer (const Paillier &paillierCryptoProvider, const Dgk &dgkCryptoProvider, const BigInteger &similarityTreshold, const size_t l, const size_t bucketSize, const size_t maxPackedBuckets, const std::deque<BigInteger> &emptyBuckets, const std::string &configurationPath) :
46  paillierCryptoProvider(paillierCryptoProvider),
47  dgkCryptoProvider(dgkCryptoProvider),
48  blindingFactorCache(paillierCryptoProvider, ComparisonBlindingFactorCacheParameters(configurationPath + ".BlindingFactorCache", bucketSize * maxPackedBuckets, emptyBuckets)),
49  l(l),
50  emptyBuckets(emptyBuckets),
51  maxPackedBuckets(maxPackedBuckets),
52  //the dgkComparisonServer operates on l + 1 bit values (due to the empty bit at the beginning of each bucket)
53  dgkComparisonServer(std::make_shared<DgkComparisonServer>(paillierCryptoProvider, dgkCryptoProvider, l + 1)) {
55  BigInteger twoTimestwoPowLMinusDelta = (BigInteger(2).GetPow(static_cast<unsigned long>(this->l)) - similarityTreshold) * 2;
56 
57  BigInteger one(1);
58  BigInteger partialD = twoTimestwoPowLMinusDelta;
59  for (size_t i = 1; i < maxPackedBuckets; ++i) {
60  partialD += (one << (static_cast<unsigned long>(bucketSize * i))) * twoTimestwoPowLMinusDelta;//bucketSize = l + 2
61  }
62 
63  this->encryptedPartialD = this->paillierCryptoProvider.EncryptIntegerNonrandom(partialD);
64  }
65 
71  SecureComparisonServer::EncryptedUserData SecureComparisonServer::Compare (const PackedData &packedSimilarityValues, const size_t similarityValueCountInLastEncryption) {
72  EncryptedUserData gammaVector;
73 
75  for (size_t encryptionIndex = 0; encryptionIndex < packedSimilarityValues.size(); ++encryptionIndex) {
80  Paillier::Ciphertext D = packedSimilarityValues[encryptionIndex] + this->encryptedPartialD;
81 
82  const BlindingFactorContainer &blindingFactorContainer = this->blindingFactorCache.Pop();
83 
85  Paillier::Ciphertext z = D + blindingFactorContainer.encryptedR;
86 
87  //the last element of the packedSimilarityValues vector may contain fewer packed buckets
88  size_t encryptedBucketsCount = encryptionIndex < packedSimilarityValues.size() - 1 ? this->maxPackedBuckets : similarityValueCountInLastEncryption;
89 
91  this->secureComparisonClient.lock()->UnpackZ(z, this->emptyBuckets, encryptedBucketsCount);
92 
94  for (size_t i = 0; i < encryptedBucketsCount; ++i) {
95  this->secureComparisonClient.lock()->SetZi(i);
96 
97  //std::cout << "r_i: " << blindingFactorContainer.ri[i].ToString() << std::endl;
98  gammaVector.emplace_back(this->dgkComparisonServer->ComputeDi(blindingFactorContainer.ri[i]));
99 
100  //this->secureComparisonClient.lock()->DebugPaillierEncryption(gammaVector.back());
101  }
102  }
103 
104  return gammaVector;
105  }
106 
111  const BigInteger &SecureComparisonServer::GetEmptyBucket (const size_t index) const {
112  return this->emptyBuckets.at(index);
113  }
114 
118  void SecureComparisonServer::SetClient (const std::shared_ptr<SecureComparisonClient> &secureComparisonClient) {
119  this->secureComparisonClient = secureComparisonClient;
120  this->dgkComparisonServer->SetClient(secureComparisonClient->GetDgkComparisonClient());
121  }
122 
126  const std::shared_ptr<DgkComparisonServer> &SecureComparisonServer::GetDgkComparisonServer () const {
127  return this->dgkComparisonServer;
128  }
129 
130 }//namespace PrivateRecommendationsDataPacking
131 }//namespace SeComLib
Implementation of the public-key DGK Cryptosystem.
Definition: dgk.h:104
Definition of class SecureComparisonServer.
std::weak_ptr< SecureComparisonClient > secureComparisonClient
A reference to the SecureComparisonClient.
const BigInteger & GetEmptyBucket(const size_t index) const
Fetch an empty bucket for the given index.
const std::shared_ptr< DgkComparisonServer > & GetDgkComparisonServer() const
Getter for this->dgkComparisonServer.
Definition of class SecureComparisonClient.
virtual Ciphertext EncryptIntegerNonrandom(const BigInteger &plaintext) const
Encrypt number without randomization.
Definition: paillier.cpp:214
Implementation of the public-key Paillier Cryptosystem.
Definition: paillier.h:103
const std::shared_ptr< DgkComparisonServer > dgkComparisonServer
A reference to the DgkComparisonServer.
SecureComparisonServer(const Paillier &paillierCryptoProvider, const Dgk &dgkCryptoProvider, const BigInteger &similarityTreshold, const size_t l, const size_t bucketSize, const size_t maxPackedBuckets, const std::deque< BigInteger > &emptyBuckets, const std::string &configurationPath)
Constructor.
RandomizerCache< BlindingFactorContainer > blindingFactorCache
Blinding factor cache instance.
EncryptedUserData Compare(const PackedData &packedSimilarityValues, const size_t similarityValueCountInLastEncryption)
Interactive secure comparison.
void SetClient(const std::shared_ptr< SecureComparisonClient > &secureComparisonClient)
Setter for this->secureComparisonClient.