MOTION  0.01
Framework for mixed-protocol multi-party computation
constant_gate.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Oleksandr Tkachenko
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 "constant_wire.h"
28 
29 #include "base/register.h"
33 #include "protocols/gate.h"
34 #include "utility/bit_vector.h"
35 #include "utility/constants.h"
36 #include "utility/logger.h"
37 
38 namespace encrypto::motion::proto {
39 
40 class Share;
41 using SharePointer = std::shared_ptr<Share>;
42 
43 // constant input gates do not inherit from InputGate, since they have no owner
44 class ConstantBooleanInputGate final : public Gate {
45  public:
47  : ConstantBooleanInputGate(BitVector<>(1, b), backend) {}
48 
50  : ConstantBooleanInputGate(std::vector<BitVector<>>{std::move(bv)}, backend) {}
51 
53  : ConstantBooleanInputGate(std::vector<BitVector<>>{bv}, backend) {}
54 
55  ConstantBooleanInputGate(std::vector<BitVector<>>&& v, Backend& backend);
56 
57  ConstantBooleanInputGate(const std::vector<BitVector<>>& v, Backend& backend);
58 
60 
61  void InitializationHelper();
62 
63  void EvaluateSetup() final override {
66  }
67 
68  void EvaluateOnline() final override {
69  WaitSetup();
72  }
73 
75 };
76 
77 // constant input gates do not inherit from InputGate, since they have no owner
78 template <typename T>
79 class ConstantArithmeticInputGate final : public Gate {
80  public:
81  ConstantArithmeticInputGate(const std::vector<T>& v, Backend& backend);
82 
83  ConstantArithmeticInputGate(std::vector<T>&& v, Backend& backend);
84 
85  ~ConstantArithmeticInputGate() final = default;
86 
88  static_assert(!std::is_same_v<T, bool>);
89 
91  if constexpr (kVerboseDebug) {
93  fmt::format("Created a ConstantArithmeticInputGate with global id {}", gate_id_));
94  }
95 
96  for (auto& w : output_wires_) GetRegister().RegisterNextWire(w);
97 
98  auto gate_info = fmt::format("uint{}_t type, gate id {}", sizeof(T) * 8, gate_id_);
99  GetLogger().LogDebug(fmt::format(
100  "Allocated a ConstantArithmeticInputGate with following properties: {}", gate_info));
101  }
102 
103  void EvaluateSetup() final override {
104  SetSetupIsReady();
106  };
107 
108  void EvaluateOnline() final override {
109  WaitSetup();
112  };
113 
115 };
116 
117 template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
119  public:
123 
126  : TwoGate(a->GetBackend()) {
127  parent_a_ = {std::static_pointer_cast<Wire>(a)};
128  parent_b_ = {std::static_pointer_cast<Wire>(b)};
129 
130  assert(parent_a_.at(0)->GetNumberOfSimdValues() == parent_b_.at(0)->GetNumberOfSimdValues());
131 
132  // assert that not both parent are const
133  // TODO: a separate gate for this probably rather rare case
134  // needs some mediocre implementation effort if implemented with a deferred inputs option
135  assert(!parent_a_.at(0)->IsConstant() && parent_b_.at(0)->IsConstant());
136 
139 
141 
142  RegisterWaitingFor(parent_a_.at(0)->GetWireId());
143  parent_a_.at(0)->RegisterWaitingGate(gate_id_);
144 
145  RegisterWaitingFor(parent_b_.at(0)->GetWireId());
146  parent_b_.at(0)->RegisterWaitingGate(gate_id_);
147 
148  {
149  auto w = std::make_shared<arithmetic_gmw::Wire<T>>(backend_, a->GetNumberOfSimdValues());
151  output_wires_ = {std::move(w)};
152  }
153 
154  auto gate_info =
155  fmt::format("uint{}_t type, gate id {}, parents: {}, {}", sizeof(T) * 8, gate_id_,
156  parent_a_.at(0)->GetWireId(), parent_b_.at(0)->GetWireId());
157  GetLogger().LogDebug(fmt::format(
158  "Created an ConstantArithmeticAdditionGate with following properties: {}", gate_info));
159  }
160 
162 
163  void EvaluateSetup() final override {
164  SetSetupIsReady();
166  }
167 
168  void EvaluateOnline() final override {
169  WaitSetup();
170  assert(setup_is_ready_);
171 
172  parent_a_.at(0)->GetIsReadyCondition().Wait();
173  parent_b_.at(0)->GetIsReadyCondition().Wait();
174 
175  auto non_constant_wire_origin = parent_a_.at(0);
176  auto constant_wire_origin = parent_b_.at(0);
177 
178  auto non_constant_wire =
179  std::dynamic_pointer_cast<const arithmetic_gmw::Wire<T>>(non_constant_wire_origin);
180  auto constant_wire =
181  std::dynamic_pointer_cast<const ConstantArithmeticWire<T>>(constant_wire_origin);
182 
183  assert(non_constant_wire);
184  assert(constant_wire);
185 
186  std::vector<T> output;
187  if (GetCommunicationLayer().GetMyId() ==
188  (gate_id_ % GetCommunicationLayer().GetNumberOfParties())) {
189  output = RestrictAddVectors(constant_wire->GetValues(), non_constant_wire->GetValues());
190  } else {
191  output = non_constant_wire->GetValues();
192  }
193 
194  auto arithmetic_wire = std::dynamic_pointer_cast<arithmetic_gmw::Wire<T>>(output_wires_.at(0));
195  arithmetic_wire->GetMutableValues() = std::move(output);
196 
198  fmt::format("Evaluated arithmetic_gmw::AdditionGate with id#{}", gate_id_));
201  }
202 
203  // perhaps, we should return a copy of the pointer and not move it for the
204  // case we need it multiple times
206  auto arithmetic_wire = std::dynamic_pointer_cast<arithmetic_gmw::Wire<T>>(output_wires_.at(0));
207  assert(arithmetic_wire);
208  auto result = std::make_shared<arithmetic_gmw::Share<T>>(arithmetic_wire);
209  return result;
210  }
211 
213 
215 };
216 
217 template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
219  public:
223 
226  : TwoGate(a->GetBackend()) {
227  parent_a_ = {std::static_pointer_cast<Wire>(a)};
228  parent_b_ = {std::static_pointer_cast<Wire>(b)};
229 
230  assert(parent_a_.at(0)->GetNumberOfSimdValues() == parent_b_.at(0)->GetNumberOfSimdValues());
231 
232  // assert that not both parent are const
233  // TODO: a separate gate for this probably rather rare case
234  // needs some mediocre implementation effort if implemented with a deferred inputs option
235  assert(!parent_a_.at(0)->IsConstant() && parent_b_.at(0)->IsConstant());
236 
239 
241 
242  RegisterWaitingFor(parent_a_.at(0)->GetWireId());
243  parent_a_.at(0)->RegisterWaitingGate(gate_id_);
244 
245  RegisterWaitingFor(parent_b_.at(0)->GetWireId());
246  parent_b_.at(0)->RegisterWaitingGate(gate_id_);
247 
248  {
249  auto w = std::make_shared<arithmetic_gmw::Wire<T>>(backend_, a->GetNumberOfSimdValues());
251  output_wires_ = {std::move(w)};
252  }
253 
254  auto gate_info =
255  fmt::format("uint{}_t type, gate id {}, parents: {}, {}", sizeof(T) * 8, gate_id_,
256  parent_a_.at(0)->GetWireId(), parent_b_.at(0)->GetWireId());
257  GetLogger().LogDebug(fmt::format(
258  "Created an ConstantArithmeticAdditionGate with following properties: {}", gate_info));
259  }
260 
262 
263  void EvaluateSetup() final override {
264  SetSetupIsReady();
266  }
267 
268  void EvaluateOnline() final override {
269  WaitSetup();
270  assert(setup_is_ready_);
271 
272  parent_a_.at(0)->GetIsReadyCondition().Wait();
273  parent_b_.at(0)->GetIsReadyCondition().Wait();
274 
275  auto non_constant_wire_origin = parent_a_.at(0);
276  auto constant_wire_origin = parent_b_.at(0);
277 
278  auto non_constant_wire =
279  std::dynamic_pointer_cast<const arithmetic_gmw::Wire<T>>(non_constant_wire_origin);
280  auto constant_wire =
281  std::dynamic_pointer_cast<const ConstantArithmeticWire<T>>(constant_wire_origin);
282 
283  assert(non_constant_wire);
284  assert(constant_wire);
285 
286  std::vector<T> output =
287  RestrictMulVectors(constant_wire->GetValues(), non_constant_wire->GetValues());
288 
289  auto arithmetic_wire = std::dynamic_pointer_cast<arithmetic_gmw::Wire<T>>(output_wires_.at(0));
290  arithmetic_wire->GetMutableValues() = std::move(output);
291 
293  fmt::format("Evaluated arithmetic_gmw::AdditionGate with id#{}", gate_id_));
296  }
297 
298  // perhaps, we should return a copy of the pointer and not move it for the
299  // case we need it multiple times
301  auto arithmetic_wire = std::dynamic_pointer_cast<arithmetic_gmw::Wire<T>>(output_wires_.at(0));
302  assert(arithmetic_wire);
303  auto result = std::make_shared<arithmetic_gmw::Share<T>>(arithmetic_wire);
304  return result;
305  }
306 
308 
310 };
311 
312 } // namespace encrypto::motion::proto
encrypto::motion::proto::ConstantArithmeticInputGate::ConstantArithmeticInputGate
ConstantArithmeticInputGate(const std::vector< T > &v, Backend &backend)
Definition: constant_gate.cpp:71
encrypto::motion::proto
Definition: backend.h:43
encrypto::motion::Logger::LogTrace
void LogTrace(const std::string &message)
Definition: logger.cpp:110
encrypto::motion::proto::ConstantArithmeticInputGate
Definition: constant_gate.h:79
encrypto::motion::Gate::gate_type_
GateType gate_type_
Definition: gate.h:105
encrypto::motion::RestrictMulVectors
std::vector< T > RestrictMulVectors(const std::vector< T > &a, const std::vector< T > &b)
Mulitiplies each element in a and b and returns the result. It is assumed that the vectors do not ove...
Definition: helpers.h:241
encrypto::motion::Gate::GetCommunicationLayer
communication::CommunicationLayer & GetCommunicationLayer()
Definition: gate.cpp:92
encrypto::motion::Gate::output_wires_
std::vector< WirePointer > output_wires_
Definition: gate.h:100
encrypto::motion::TwoGate
Definition: gate.h:218
encrypto::motion::proto::ConstantArithmeticMultiplicationGate
Definition: constant_gate.h:218
encrypto::motion::proto::ConstantArithmeticAdditionGate::GetOutputAsArithmeticShare
arithmetic_gmw::SharePointer< T > GetOutputAsArithmeticShare()
Definition: constant_gate.h:205
encrypto::motion::proto::ConstantBooleanInputGate::ConstantBooleanInputGate
ConstantBooleanInputGate(const BitVector<> &bv, Backend &backend)
Definition: constant_gate.h:52
encrypto::motion::TwoGate::parent_b_
std::vector< WirePointer > parent_b_
Definition: gate.h:221
encrypto::motion::proto::ConstantBooleanInputGate
Definition: constant_gate.h:44
encrypto::motion::proto::ConstantArithmeticMultiplicationGate::ConstantArithmeticMultiplicationGate
ConstantArithmeticMultiplicationGate(const arithmetic_gmw::WirePointer< T > &a, const ConstantArithmeticWirePointer< T > &b)
Definition: constant_gate.h:224
encrypto::motion::Gate::GetLogger
Logger & GetLogger()
Definition: gate.cpp:100
encrypto::motion::proto::ConstantArithmeticAdditionGate::ConstantArithmeticAdditionGate
ConstantArithmeticAdditionGate(const arithmetic_gmw::WirePointer< T > &a, const ConstantArithmeticWirePointer< T > &b)
Definition: constant_gate.h:124
encrypto::motion::proto::ConstantArithmeticAdditionGate::EvaluateSetup
void EvaluateSetup() final override
Definition: constant_gate.h:163
encrypto::motion::proto::ConstantBooleanInputGate::ConstantBooleanInputGate
ConstantBooleanInputGate(bool b, Backend &backend)
Definition: constant_gate.h:46
encrypto::motion::proto::ConstantArithmeticAdditionGate::ConstantArithmeticAdditionGate
ConstantArithmeticAdditionGate(const ConstantArithmeticWirePointer< T > &a, const arithmetic_gmw::WirePointer< T > &b)
Definition: constant_gate.h:120
bit_vector.h
encrypto::motion::proto::ConstantArithmeticMultiplicationGate::EvaluateOnline
void EvaluateOnline() final override
Definition: constant_gate.h:268
encrypto::motion::proto::ConstantArithmeticMultiplicationGate::ConstantArithmeticMultiplicationGate
ConstantArithmeticMultiplicationGate(const ConstantArithmeticWirePointer< T > &a, const arithmetic_gmw::WirePointer< T > &b)
Definition: constant_gate.h:220
encrypto::motion::proto::ConstantArithmeticInputGate::GetOutputAsShare
motion::SharePointer GetOutputAsShare() const
Definition: constant_gate.cpp:88
encrypto::motion::proto::SharePointer
std::shared_ptr< Share > SharePointer
Definition: constant_gate.h:41
encrypto::motion::GateType::kNonInteractive
@ kNonInteractive
encrypto::motion::proto::ConstantBooleanInputGate::EvaluateOnline
void EvaluateOnline() final override
Definition: constant_gate.h:68
encrypto::motion::BitVector<>
encrypto::motion::Logger::LogDebug
void LogDebug(const std::string &message)
Definition: logger.cpp:142
encrypto::motion::proto::ConstantArithmeticInputGate::EvaluateOnline
void EvaluateOnline() final override
Definition: constant_gate.h:108
encrypto::motion::Gate::requires_online_interaction_
std::atomic< bool > requires_online_interaction_
Definition: gate.h:108
encrypto::motion::Gate::GetRegister
Register & GetRegister()
Definition: gate.cpp:96
constant_wire.h
encrypto::motion::proto::ConstantArithmeticInputGate::InitializationHelper
void InitializationHelper()
Definition: constant_gate.h:87
encrypto::motion::TwoGate::parent_a_
std::vector< WirePointer > parent_a_
Definition: gate.h:220
encrypto::motion::proto::ConstantArithmeticAdditionGate::EvaluateOnline
void EvaluateOnline() final override
Definition: constant_gate.h:168
encrypto::motion::Register::IncrementEvaluatedGatesOnlineCounter
void IncrementEvaluatedGatesOnlineCounter()
Definition: register.cpp:125
encrypto::motion::Backend
Definition: backend.h:88
constant_share.h
encrypto::motion::Gate::RegisterWaitingFor
void RegisterWaitingFor(std::size_t wire_id)
Definition: gate.cpp:36
communication_layer.h
encrypto::motion::proto::ConstantArithmeticMultiplicationGate::GetOutputAsArithmeticShare
arithmetic_gmw::SharePointer< T > GetOutputAsArithmeticShare()
Definition: constant_gate.h:300
encrypto::motion::Gate::SetOnlineIsReady
void SetOnlineIsReady()
Definition: gate.cpp:54
encrypto::motion::Gate::backend_
Backend & backend_
Definition: gate.h:101
encrypto::motion::RestrictAddVectors
std::vector< T > RestrictAddVectors(const std::vector< T > &a, const std::vector< T > &b)
Adds each element in a and b and returns the result. It is assumed that the vectors do not overlap.
Definition: helpers.h:197
encrypto::motion::Gate::SetSetupIsReady
void SetSetupIsReady()
Definition: gate.cpp:46
encrypto::motion::Register::IncrementEvaluatedGatesSetupCounter
void IncrementEvaluatedGatesSetupCounter()
Definition: register.cpp:114
encrypto::motion::proto::ConstantBooleanInputGate::~ConstantBooleanInputGate
~ConstantBooleanInputGate() final=default
register.h
logger.h
constant_gate.h
arithmetic_gmw_share.h
encrypto::motion::SharePointer
std::shared_ptr< Share > SharePointer
Definition: conversion_gate.h:49
encrypto::motion::proto::ConstantBooleanInputGate::GetOutputAsShare
motion::SharePointer GetOutputAsShare() const
Definition: constant_gate.cpp:66
encrypto::motion::Gate::gate_id_
std::int64_t gate_id_
Definition: gate.h:102
encrypto::motion::proto::ConstantArithmeticInputGate::~ConstantArithmeticInputGate
~ConstantArithmeticInputGate() final=default
encrypto::motion::Gate
Definition: gate.h:67
encrypto::motion::proto::ConstantArithmeticAdditionGate::ConstantArithmeticAdditionGate
ConstantArithmeticAdditionGate()=delete
encrypto::motion::proto::arithmetic_gmw::SharePointer
std::shared_ptr< Share< T > > SharePointer
Definition: arithmetic_gmw_share.h:156
encrypto::motion::proto::ConstantArithmeticMultiplicationGate::~ConstantArithmeticMultiplicationGate
~ConstantArithmeticMultiplicationGate() final=default
encrypto::motion::Gate::setup_is_ready_
std::atomic< bool > setup_is_ready_
Definition: gate.h:106
encrypto::motion::kVerboseDebug
constexpr bool kVerboseDebug
Definition: constants.h:50
encrypto::motion::proto::ConstantArithmeticAdditionGate
Definition: constant_gate.h:118
encrypto::motion::proto::ConstantBooleanInputGate::InitializationHelper
void InitializationHelper()
Definition: constant_gate.cpp:52
gate.h
encrypto::motion::proto::ConstantArithmeticWirePointer
std::shared_ptr< ConstantArithmeticWire< T > > ConstantArithmeticWirePointer
Definition: constant_wire.h:67
encrypto::motion::proto::ConstantArithmeticMultiplicationGate::ConstantArithmeticMultiplicationGate
ConstantArithmeticMultiplicationGate()=delete
encrypto::motion::Gate::WaitSetup
void WaitSetup() const
Definition: gate.cpp:68
encrypto::motion::Register::RegisterNextWire
void RegisterNextWire(WirePointer wire)
Definition: register.h:78
encrypto::motion::proto::ConstantArithmeticMultiplicationGate::EvaluateSetup
void EvaluateSetup() final override
Definition: constant_gate.h:263
constants.h
geninput.default
default
Definition: geninput.py:149
encrypto::motion::proto::ConstantArithmeticAdditionGate::~ConstantArithmeticAdditionGate
~ConstantArithmeticAdditionGate() final=default
encrypto::motion::proto::ConstantBooleanInputGate::ConstantBooleanInputGate
ConstantBooleanInputGate(BitVector<> &&bv, Backend &backend)
Definition: constant_gate.h:49
encrypto::motion::proto::ConstantArithmeticWire
Definition: constant_wire.h:33
encrypto::motion::proto::arithmetic_gmw::WirePointer
std::shared_ptr< Wire< T > > WirePointer
Definition: arithmetic_gmw_wire.h:68
encrypto::motion::proto::ConstantArithmeticInputGate::EvaluateSetup
void EvaluateSetup() final override
Definition: constant_gate.h:103
encrypto::motion::proto::ConstantBooleanInputGate::EvaluateSetup
void EvaluateSetup() final override
Definition: constant_gate.h:63
encrypto::motion::Register::NextGateId
std::size_t NextGateId() noexcept
Definition: register.cpp:53
arithmetic_gmw_wire.h