SeComLib
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
secure_extremum_selection_server.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 */
29 #ifndef SECURE_EXTREMUM_SELECTION_SERVER_IMPLEMENTATION_GUARD
30 #define SECURE_EXTREMUM_SELECTION_SERVER_IMPLEMENTATION_GUARD
31 
32 namespace SeComLib {
33 namespace Core {
39  template <typename T_SecureComparisonServer, typename T_SecureComparisonClient>
40  SecureExtremumSelectionServer<T_SecureComparisonServer, T_SecureComparisonClient>::SecureExtremumSelectionServer (const Paillier &paillierCryptoProvider, const Dgk &dgkCryptoProvider, const std::string &configurationPath) :
41  paillierCryptoProvider(paillierCryptoProvider),
42  dgkCryptoProvider(dgkCryptoProvider),
43  secureComparisonServer(std::make_shared<T_SecureComparisonServer>(paillierCryptoProvider, dgkCryptoProvider, configurationPath)),
44  secureMultiplicationServer(std::make_shared<SecureMultiplicationServer<Paillier>>(paillierCryptoProvider, Utils::Config::GetInstance().GetParameter<size_t>(configurationPath + ".l"), configurationPath)) {
45  }
46 
51  template <typename T_SecureComparisonServer, typename T_SecureComparisonClient>
53  if (items.size() < 2) {
54  return *items.begin();
55  }
56 
57  /* Create a vector of the minimums obtained by comparing each adjacent pair of items:
58  @f$ [min] = \left\{\begin{array}{ll} [0 * (y - x) + x] = ([0] * [y - x])[x] = [x] & \text{if $ x < y $} \\ [1 * (y - x) + x] = ([1] * [y - x])[x] = [y] & \text{if $ x \geq y} \end{array} \right. @f$
59  */
60  ItemContainer temp;
61  temp.reserve(items.size() / 2 + items.size() % 2);//inefficient ceil
62  for (size_t i = 0; i < items.size() - 1; i += 2) {
63  temp.push_back(this->secureMultiplicationServer->Multiply(this->secureComparisonServer->Compare(items[i], items[i + 1]), items[i + 1] - items[i]) + items[i]);
64  }
65 
66  //for odd lenghts, we also want to keep the last element
67  if (items.size() % 2 == 1) {
68  temp.push_back(*items.rbegin());
69  }
70 
72  return this->GetMinimum(temp);
73  }
74 
79  template <typename T_SecureComparisonServer, typename T_SecureComparisonClient>
81  if (items.size() < 2) {
82  return *items.begin();
83  }
84 
85  /* Create a vector of the maximums obtained by comparing each adjacent pair of items:
86  @f$ [max] = \left\{\begin{array}{ll} [0 * (x - y) + y] = ([0] * [x - y])[y] = [y] & \text{if $ x < y $} \\ [1 * (x - y) + y] = ([1] * [x - y])[y] = [x] & \text{if $ x \geq y} \end{array} \right. @f$
87  */
88  ItemContainer temp;
89  temp.reserve(items.size() / 2 + items.size() % 2);//inefficient ceil
90  for (size_t i = 0; i < items.size() - 1; i += 2) {
91  temp.push_back(this->secureMultiplicationServer->Multiply(this->secureComparisonServer->Compare(items[i], items[i + 1]), items[i] - items[i + 1]) + items[i + 1]);
92  }
93 
94  //for odd lenghts, we also want to keep the last element
95  if (items.size() % 2 == 1) {
96  temp.push_back(*items.rbegin());
97  }
98 
100  return this->GetMaximum(temp);
101  }
102 
106  template <typename T_SecureComparisonServer, typename T_SecureComparisonClient>
108  this->secureExtremumSelectionClient = secureExtremumSelectionClient;
109  this->secureComparisonServer->SetClient(secureExtremumSelectionClient->GetSecureComparisonClient());
110  this->secureMultiplicationServer->SetClient(secureExtremumSelectionClient->GetSecureMultiplicationClient());
111  }
112 
116  template <typename T_SecureComparisonServer, typename T_SecureComparisonClient>
118  return this->secureComparisonServer;
119  }
120 
124  template <typename T_SecureComparisonServer, typename T_SecureComparisonClient>
126  return this->secureMultiplicationServer;
127  }
128 
129 }//namespace Core
130 }//namespace SeComLib
131 
132 #endif//SECURE_EXTREMUM_SELECTION_SERVER_IMPLEMENTATION_GUARD
SecureExtremumSelectionServer(const Paillier &paillierCryptoProvider, const Dgk &dgkCryptoProvider, const std::string &configurationPath)
Constructor.
Implementation of the public-key DGK Cryptosystem.
Definition: dgk.h:104
Paillier::Ciphertext GetMinimum(const ItemContainer &items) const
Interactive secure minimum selection.
const std::shared_ptr< T_SecureComparisonServer > & GetSecureComparisonServer() const
Getter for this->secureComparisonServer.
void SetClient(const std::shared_ptr< SecureExtremumSelectionClient< T_SecureComparisonServer, T_SecureComparisonClient >> &secureExtremumSelectionClient)
Setter for this->secureExtremumSelectionClient.
Paillier::Ciphertext GetMaximum(const ItemContainer &items) const
Interactive secure maximum selection.
Implementation of the public-key Paillier Cryptosystem.
Definition: paillier.h:103
Paillier cipertext.
Definition: paillier.h:77
const std::shared_ptr< SecureMultiplicationServer< Paillier > > & GetSecureMultiplicationServer() const
Getter for this->secureMultiplicationServer.
std::vector< Paillier::Ciphertext > ItemContainer
Alias for the item container.