SeComLib
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
private_recommendations_utils/dgk_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 */
29 #include "dgk_comparison_server.h"
30 //avoid circular includes
31 #include "dgk_comparison_client.h"
32 
33 namespace SeComLib {
34 namespace PrivateRecommendationsUtils {
40  DgkComparisonServer::DgkComparisonServer (const Paillier &paillierCryptoProvider, const Dgk &dgkCryptoProvider, const size_t l) :
41  paillierCryptoProvider(paillierCryptoProvider),
42  dgkCryptoProvider(dgkCryptoProvider),
43  l(l) {
44  }
45 
54  Paillier::Ciphertext DgkComparisonServer::Compare (const BigInteger &rModTwoPowL) const {
55  BigInteger c = RandomProvider::GetInstance().GetRandomInteger(1);
56 
57  Dgk::Ciphertext tau = this->computeTau(rModTwoPowL, c);
58 
60  Dgk::Ciphertext tl;
62  if (c == 0) {
64  tl = tau;
65  }
66  else {
68  tl = this->dgkCryptoProvider.GetEncryptedOne(false) - tau;
69  }
70 
73  return this->dgkComparisonClient.lock()->ConvertToPaillier(tl);
74  }
75 
81  BigInteger CiSP = RandomProvider::GetInstance().GetRandomInteger(1);
82 
84  Dgk::Ciphertext CiPSP = this->computeTau(ri, CiSP);
85 
87  CiPSP = this->dgkCryptoProvider.RandomizeCiphertext(CiPSP);
88 
90  Paillier::Ciphertext diPSP = this->dgkComparisonClient.lock()->ComputeDiPSP(CiPSP);
91 
93  int diSP;
94  if (CiSP != static_cast<long>(ri.GetBit(this->GetMSBPosition()))) {
95  diSP = 1;
96  }
97  else {
98  diSP = 0;
99  }
100 
102  if (diSP == 0) {
103  return diPSP;
104  }
105  else {
107  return this->paillierCryptoProvider.GetEncryptedOne(false) - diPSP;
108  }
109  }
110 
116  Dgk::Ciphertext DgkComparisonServer::computeTau (const BigInteger &a, const BigInteger &tSP) const {
117  Dgk::Ciphertext b0 = this->dgkComparisonClient.lock()->GetBi(0);
118 
119  Dgk::Ciphertext t;
120 
122  if (a.GetBit(0) == 0) {
124  t = this->dgkCryptoProvider.GetEncryptedZero(false);
125  }
126  else {
128  t = this->dgkCryptoProvider.GetEncryptedOne(false) - b0;
129  }
130 
132  for (size_t i = 1; i < this->l; ++i) {
134  BigInteger c = RandomProvider::GetInstance().GetRandomInteger(1);
135 
136  Dgk::Ciphertext tau;
137 
138  //perform blinding
139  if (c == 0) {
141  tau = t;
142  }
143  else {
145  tau = this->dgkCryptoProvider.GetEncryptedOne(false) - t;
146  }
147 
149  tau = this->dgkCryptoProvider.RandomizeCiphertext(tau);
150 
152  Dgk::Ciphertext tb = this->dgkComparisonClient.lock()->GetTb(tau, i);
153 
155  Dgk::Ciphertext bi = this->dgkComparisonClient.lock()->GetBi(i);
156 
158  if (c == 1) {
160  tb = bi - tb;
161  }
162 
163  if (a.GetBit(i) == 0) {
165  t = t - tb;
166  }
167  else {
169  t = tb + (this->dgkCryptoProvider.GetEncryptedOne(false) - bi);
170  }
171  }
172 
174  if (tSP == 0) {
176  return t;
177  }
178  else {
180  return this->dgkCryptoProvider.GetEncryptedOne(false) - t;
181  }
182  }
183 
188  return this->l;
189  }
190 
194  void DgkComparisonServer::SetClient (const std::shared_ptr<DgkComparisonClient> &dgkComparisonClient) {
195  this->dgkComparisonClient = dgkComparisonClient;
196  }
197 
198 }//namespace PrivateRecommendationsUtils
199 }//namespace SeComLib
Implementation of the public-key DGK Cryptosystem.
Definition: dgk.h:104
Ciphertext GetEncryptedOne(const bool randomized=true) const
Returns [1].
Dgk::Ciphertext computeTau(const BigInteger &a, const BigInteger &tSP) const
Computes the encrypted additive share of the client.
Ciphertext GetEncryptedZero(const bool randomized=true) const
Returns [0].
Paillier::Ciphertext Compare(const BigInteger &rModTwoPowL) const
Interactive secure comparison.
virtual Ciphertext RandomizeCiphertext(const Ciphertext &ciphertext) const
Randomize encrypted number with a self-generated random value.
Definition: dgk.cpp:437
Definition of class DgkComparisonServer.
Paillier::Ciphertext ComputeDi(const BigInteger &rModTwoPowL) const
Computes .
Implementation of the public-key Paillier Cryptosystem.
Definition: paillier.h:103
size_t GetMSBPosition() const
Returns the bit position of the MSB of the operands (since l is not available for the dgkComparisonCl...
void SetClient(const std::shared_ptr< DgkComparisonClient > &dgkComparisonClient)
Setter for this->dgkComparisonClient.
DgkComparisonServer(const Paillier &paillierCryptoProvider, const Dgk &dgkCryptoProvider, const size_t l)
Constructor.
Definition of class DgkComparisonClient.
std::weak_ptr< const DgkComparisonClient > dgkComparisonClient
A reference to the DgkComparisonClient.
const Paillier & paillierCryptoProvider
Reference to the Paillier crypto provider.