MOTION  0.01
Framework for mixed-protocol multi-party computation
party.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2021 Oleksandr Tkachenko, Arianne Roselina Prananto
4 // Cryptography and Privacy Engineering Group (ENCRYPTO)
5 // TU Darmstadt, Germany
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 
25 #pragma once
26 
27 #include <fmt/format.h>
28 #include <memory>
29 #include <span>
30 #include <vector>
31 
32 #include "base/backend.h"
33 #include "base/configuration.h"
38 #include "protocols/share.h"
39 #include "utility/typedefs.h"
40 
42 
43 class CommunicationLayer;
44 
45 } // namespace encrypto::motion::communication
46 
47 namespace encrypto::motion {
48 
49 class Logger;
50 
51 class Party {
52  public:
53  Party() = delete;
54 
55  // Let's make only Configuration be copyable
56  Party(Party& party) = delete;
57 
58  Party(std::unique_ptr<communication::CommunicationLayer> parties);
59 
60  ~Party();
61 
62  ConfigurationPointer GetConfiguration() { return configuration_; }
63 
64  communication::CommunicationLayer& GetCommunicationLayer() { return *communication_layer_; }
65 
66  template <MpcProtocol P>
67  SharePointer In(std::span<const BitVector<>> input,
68  std::size_t party_id = std::numeric_limits<std::size_t>::max()) {
69  static_assert(P != MpcProtocol::kArithmeticGmw);
70  static_assert(P != MpcProtocol::kArithmeticConstant);
71  switch (P) {
73  // TODO implement
74  static_assert(P != MpcProtocol::kBooleanConstant, "Not implemented yet");
75  // return backend_->BooleanGmwInput(party_id, input);
76  }
78  return backend_->BooleanGmwInput(party_id, input);
79  }
80  case MpcProtocol::kBmr: {
81  return backend_->BmrInput(party_id, input);
82  }
83  default: {
84  throw(std::runtime_error(
85  fmt::format("Unknown MPC protocol with id {}", static_cast<unsigned int>(P))));
86  }
87  }
88  }
89 
90  template <MpcProtocol P>
91  SharePointer In(std::vector<BitVector<>>&& input,
92  std::size_t party_id = std::numeric_limits<std::size_t>::max()) {
93  static_assert(P != MpcProtocol::kArithmeticGmw);
94  static_assert(P != MpcProtocol::kArithmeticConstant);
95  switch (P) {
97  // TODO implement
98  static_assert(P != MpcProtocol::kBooleanConstant, "Not implemented yet");
99  // return backend_->BooleanGmwInput(party_id, input);
100  }
102  return backend_->BooleanGmwInput(party_id, std::move(input));
103  }
104  case MpcProtocol::kBmr: {
105  return backend_->BmrInput(party_id, input);
106  }
107  default: {
108  throw(std::runtime_error(
109  fmt::format("Unknown MPC protocol with id {}", static_cast<unsigned int>(P))));
110  }
111  }
112  }
113 
114  template <MpcProtocol P>
116  std::size_t party_id = std::numeric_limits<std::size_t>::max()) {
117  static_assert(P != MpcProtocol::kArithmeticGmw);
118  static_assert(P != MpcProtocol::kArithmeticConstant);
119  switch (P) {
121  // TODO implement
122  static_assert(P != MpcProtocol::kBooleanConstant, "Not implemented yet");
123  // return backend_->BooleanGmwInput(party_id, input);
124  }
126  return backend_->BooleanGmwInput(party_id, input);
127  }
128  case MpcProtocol::kBmr: {
129  return backend_->BmrInput(party_id, input);
130  }
131  default: {
132  throw(std::runtime_error(
133  fmt::format("Unknown MPC protocol with id {}", static_cast<unsigned int>(P))));
134  }
135  }
136  }
137 
138  template <MpcProtocol P>
140  std::size_t party_id = std::numeric_limits<std::size_t>::max()) {
141  static_assert(P != MpcProtocol::kArithmeticGmw);
142  static_assert(P != MpcProtocol::kArithmeticConstant);
143  switch (P) {
145  // TODO implement
146  static_assert(P != MpcProtocol::kBooleanConstant, "Not implemented yet");
147  // return backend_->BooleanGmwInput(party_id, input);
148  }
150  return backend_->BooleanGmwInput(party_id, std::move(input));
151  }
152  case MpcProtocol::kBmr: {
153  return backend_->BmrInput(party_id, input);
154  }
155  default: {
156  throw(std::runtime_error(
157  fmt::format("Unknown MPC protocol with id {}", static_cast<unsigned int>(P))));
158  }
159  }
160  }
161 
162  template <MpcProtocol P, typename T = std::uint8_t,
163  typename = std::enable_if_t<std::is_unsigned_v<T>>>
164  SharePointer In(const std::vector<T>& input,
165  std::size_t party_id = std::numeric_limits<std::size_t>::max()) {
166  switch (P) {
168  return backend_->ConstantArithmeticGmwInput(input);
169  }
171  return backend_->ArithmeticGmwInput(party_id, input);
172  }
174  throw std::runtime_error(
175  "Non-binary types have to be converted to BitVectors in BooleanGMW, "
176  "consider using TODO function for the input");
177  }
178  case MpcProtocol::kBmr: {
179  throw std::runtime_error(
180  "Non-binary types have to be converted to BitVectors in BMR, "
181  "consider using TODO function for the input");
182  }
183  default: {
184  throw(std::runtime_error(
185  fmt::format("Unknown MPC protocol with id {}", static_cast<unsigned int>(P))));
186  }
187  }
188  }
189 
190  template <MpcProtocol P, typename T = std::uint8_t,
191  typename = std::enable_if_t<std::is_unsigned_v<T>>>
192  SharePointer In(std::vector<T>&& input,
193  std::size_t party_id = std::numeric_limits<std::size_t>::max()) {
194  switch (P) {
196  return backend_->ConstantArithmeticGmwInput(std::move(input));
197  }
199  return backend_->ArithmeticGmwInput(party_id, std::move(input));
200  }
202  throw(std::runtime_error(
203  fmt::format("Non-binary types have to be converted to BitVectors in BooleanGMW, "
204  "consider using TODO function for the input")));
205  }
206  case MpcProtocol::kBmr: {
207  throw(std::runtime_error(
208  fmt::format("Non-binary types have to be converted to BitVectors in BMR, "
209  "consider using TODO function for the input")));
210  }
211  default: {
212  throw(std::runtime_error(
213  fmt::format("Unknown MPC protocol with id {}", static_cast<unsigned int>(P))));
214  }
215  }
216  }
217 
218  template <MpcProtocol P, typename T = std::uint8_t,
219  typename = std::enable_if_t<std::is_unsigned_v<T>>>
220  SharePointer In(T input, std::size_t party_id = std::numeric_limits<std::size_t>::max()) {
221  if constexpr (std::is_same_v<T, bool>) {
222  if constexpr (P == MpcProtocol::kBooleanGmw)
223  return backend_->BooleanGmwInput(party_id, input);
224  else
225  return backend_->BmrInput(party_id, input);
226  } else {
227  return In<P, T>(std::vector<T>{input}, party_id);
228  }
229  }
230 
231  template <MpcProtocol P, typename T = std::uint8_t,
232  typename = std::enable_if_t<std::is_unsigned_v<T>>>
234  switch (P) {
236  static_assert(P != MpcProtocol::kArithmeticConstant, "Not implemented yet");
237  }
239  encrypto::motion::RegisterPointer register_pointer{backend_->GetRegister()};
240 
242  std::make_shared<encrypto::motion::proto::arithmetic_gmw::Wire<T>>(input, *backend_);
243  register_pointer->RegisterNextWire(wire);
244  wire->SetOnlineFinished();
245 
246  return std::make_shared<encrypto::motion::proto::arithmetic_gmw::Share<T>>(wire);
247  }
249  throw std::runtime_error(
250  "Non-binary types have to be converted to BitVectors in BooleanGMW, "
251  "consider using TODO function for the input");
252  }
253  case MpcProtocol::kBmr: {
254  throw std::runtime_error(
255  "Non-binary types have to be converted to BitVectors in BMR, "
256  "consider using TODO function for the input");
257  }
258  default: {
259  throw(std::runtime_error(
260  fmt::format("Unknown MPC protocol with id {}", static_cast<unsigned int>(P))));
261  }
262  }
263  }
264 
265  template <MpcProtocol P, typename T = std::uint8_t,
266  typename = std::enable_if_t<std::is_unsigned_v<T>>>
267  SharePointer SharedIn(const std::vector<T>& input) {
268  switch (P) {
270  static_assert(P != MpcProtocol::kArithmeticConstant, "Not implemented yet");
271  }
273  encrypto::motion::RegisterPointer register_pointer{backend_->GetRegister()};
274 
276  std::make_shared<encrypto::motion::proto::arithmetic_gmw::Wire<T>>(input, *backend_);
277  register_pointer->RegisterNextWire(wire);
278  wire->SetOnlineFinished();
279 
280  return std::make_shared<encrypto::motion::proto::arithmetic_gmw::Share<T>>(wire);
281  }
283  throw std::runtime_error(
284  "Non-binary types have to be converted to BitVectors in BooleanGMW, "
285  "consider using TODO function for the input");
286  }
287  case MpcProtocol::kBmr: {
288  throw std::runtime_error(
289  "Non-binary types have to be converted to BitVectors in BMR, "
290  "consider using TODO function for the input");
291  }
292  default: {
293  throw(std::runtime_error(
294  fmt::format("Unknown MPC protocol with id {}", static_cast<unsigned int>(P))));
295  }
296  }
297  }
298 
299  template <MpcProtocol P>
300  SharePointer SharedIn(std::span<const BitVector<>> input) {
301  static_assert(P != MpcProtocol::kArithmeticGmw);
302  static_assert(P != MpcProtocol::kArithmeticConstant);
303 
304  switch (P) {
306  static_assert(P != MpcProtocol::kBooleanConstant, "Not implemented yet");
307  }
309  std::vector<encrypto::motion::WirePointer> wires(input.size());
310  encrypto::motion::RegisterPointer register_pointer{backend_->GetRegister()};
311 
312  for (std::size_t i = 0; i < wires.size(); i++) {
313  wires[i] =
314  std::make_shared<encrypto::motion::proto::boolean_gmw::Wire>(input[i], *backend_);
315  register_pointer->RegisterNextWire(wires[i]);
316  wires[i]->SetOnlineFinished();
317  }
318 
319  return std::make_shared<encrypto::motion::proto::boolean_gmw::Share>(wires);
320  }
321  case MpcProtocol::kBmr: {
322  static_assert(P != MpcProtocol::kBmr, "Not implemented yet");
323  }
324  default: {
325  throw(std::runtime_error(
326  fmt::format("Unknown MPC protocol with id {}", static_cast<unsigned int>(P))));
327  }
328  }
329  }
330 
331  SharePointer Xor(const SharePointer& a, const SharePointer& b);
332 
333  SharePointer Out(SharePointer parent, std::size_t output_owner);
334 
335  SharePointer Add(const SharePointer& a, const SharePointer& b);
336 
337  SharePointer And(const SharePointer& a, const SharePointer& b);
338 
343  void Run(std::size_t repetitions = 1);
344 
346  void Reset();
347 
350  void Clear();
351 
352  const auto& GetLogger() { return logger_; }
353 
364  void Finish();
365 
366  auto& GetBackend() { return backend_; }
367 
368  private:
369  std::unique_ptr<communication::CommunicationLayer> communication_layer_;
370  ConfigurationPointer configuration_;
371  std::shared_ptr<Logger> logger_;
372  BackendPointer backend_;
373  std::atomic<bool> finished_ = false;
374  std::atomic<bool> connected_ = false;
375 
376  void EvaluateCircuit();
377 };
378 
383 std::vector<std::unique_ptr<Party>> MakeLocallyConnectedParties(const std::size_t number_of_parties,
384  std::uint16_t port,
385  const bool logging = false);
386 
387 using PartyPointer = std::unique_ptr<Party>;
388 
389 } // namespace encrypto::motion
encrypto::motion::MakeLocallyConnectedParties
std::vector< std::unique_ptr< Party > > MakeLocallyConnectedParties(const std::size_t number_of_parties, std::uint16_t, const bool)
constructs number_of_parties motion::Party's locally connected via TCP.
Definition: party.cpp:227
encrypto::motion::Party::In
SharePointer In(const BitVector<> &input, std::size_t party_id=std::numeric_limits< std::size_t >::max())
Definition: party.h:115
encrypto::motion::Party::In
SharePointer In(T input, std::size_t party_id=std::numeric_limits< std::size_t >::max())
Definition: party.h:220
encrypto::motion::Party
Definition: party.h:51
encrypto::motion::Party::Add
SharePointer Add(const SharePointer &a, const SharePointer &b)
Definition: party.cpp:105
encrypto::motion::Party::SharedIn
SharePointer SharedIn(const std::vector< T > &input)
Definition: party.h:267
encrypto::motion::Party::Run
void Run(std::size_t repetitions=1)
Evaluates the constructed gates a predefined number of times. This is realized via repeatedly calling...
Definition: party.cpp:166
party.h
encrypto::motion::Party::Finish
void Finish()
Sends a termination message to all of the connected parties. In case a TCP connection is used,...
Definition: party.cpp:218
encrypto::motion::Party::GetConfiguration
ConfigurationPointer GetConfiguration()
Definition: party.h:62
encrypto::motion::MpcProtocol::kArithmeticConstant
@ kArithmeticConstant
encrypto::motion::Party::SharedIn
SharePointer SharedIn(std::span< const BitVector<>> input)
Definition: party.h:300
encrypto::motion::Logger
Definition: logger.h:40
encrypto::motion::MpcProtocol::kBmr
@ kBmr
encrypto::motion::PartyPointer
std::unique_ptr< Party > PartyPointer
Definition: party.h:387
encrypto::motion::Party::~Party
~Party()
Definition: party.cpp:44
encrypto::motion::Party::Reset
void Reset()
Destroys all the gates and wires that were constructed until now.
Definition: party.cpp:194
encrypto::motion::WirePointer
std::shared_ptr< Wire > WirePointer
Definition: register.h:40
configuration.h
encrypto::motion::Party::GetLogger
const auto & GetLogger()
Definition: party.h:352
encrypto::motion::Party::Xor
SharePointer Xor(const SharePointer &a, const SharePointer &b)
Definition: party.cpp:49
backend.h
encrypto::motion::MpcProtocol::kBooleanConstant
@ kBooleanConstant
encrypto::motion::BitVector<>
encrypto::motion::Party::In
SharePointer In(BitVector<> &&input, std::size_t party_id=std::numeric_limits< std::size_t >::max())
Definition: party.h:139
encrypto::motion::Party::Out
SharePointer Out(SharePointer parent, std::size_t output_owner)
Definition: party.cpp:68
encrypto::motion::MpcProtocol::kBooleanGmw
@ kBooleanGmw
encrypto::motion::communication::MakeDummyCommunicationLayers
std::vector< std::unique_ptr< CommunicationLayer > > MakeDummyCommunicationLayers(std::size_t number_of_parties)
Definition: communication_layer.cpp:503
encrypto::motion::BackendPointer
std::shared_ptr< Backend > BackendPointer
Definition: backend.h:333
encrypto::motion::Backend
Definition: backend.h:88
encrypto::motion::ConfigurationPointer
std::shared_ptr< Configuration > ConfigurationPointer
Definition: backend.h:81
encrypto::motion::RegisterPointer
std::shared_ptr< Register > RegisterPointer
Definition: backend.h:84
communication_layer.h
boolean_gmw_wire.h
encrypto::motion::Party::SharedIn
SharePointer SharedIn(T input)
Definition: party.h:233
ot_provider.h
register.h
logger.h
encrypto::motion
Definition: algorithm_description.cpp:35
arithmetic_gmw_share.h
encrypto::motion::SharePointer
std::shared_ptr< Share > SharePointer
Definition: conversion_gate.h:49
encrypto::motion::Party::GetCommunicationLayer
communication::CommunicationLayer & GetCommunicationLayer()
Definition: party.h:64
encrypto::motion::Party::In
SharePointer In(std::span< const BitVector<>> input, std::size_t party_id=std::numeric_limits< std::size_t >::max())
Definition: party.h:67
encrypto::motion::Party::GetBackend
auto & GetBackend()
Definition: party.h:366
encrypto::motion::Party::Party
Party()=delete
encrypto::motion::communication::CommunicationLayer
Definition: communication_layer.h:58
encrypto::motion::Party::Clear
void Clear()
Interprets the gates and wires as newly created, i.e., Party::Run() can be executed again.
Definition: party.cpp:202
typedefs.h
encrypto::motion::MpcProtocol
MpcProtocol
Definition: typedefs.h:140
share.h
encrypto::motion::Party::And
SharePointer And(const SharePointer &a, const SharePointer &b)
Definition: party.cpp:147
encrypto::motion::communication
Definition: backend.h:37
encrypto::motion::MpcProtocol::kArithmeticGmw
@ kArithmeticGmw
encrypto::motion::Configuration
Definition: configuration.h:32
encrypto::motion::Party::In
SharePointer In(std::vector< BitVector<>> &&input, std::size_t party_id=std::numeric_limits< std::size_t >::max())
Definition: party.h:91
encrypto::motion::Party::In
SharePointer In(std::vector< T > &&input, std::size_t party_id=std::numeric_limits< std::size_t >::max())
Definition: party.h:192
boolean_gmw_share.h
encrypto::motion::Party::In
SharePointer In(const std::vector< T > &input, std::size_t party_id=std::numeric_limits< std::size_t >::max())
Definition: party.h:164
arithmetic_gmw_wire.h