MOTION  0.01
Framework for mixed-protocol multi-party computation
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 <atomic>
28 #include <memory>
29 #include <mutex>
30 #include <unordered_set>
31 #include <vector>
32 
34 #include "utility/typedefs.h"
35 
37 
38 class CommunicationLayer;
39 
40 } // namespace encrypto::motion::communication
41 
42 namespace encrypto::motion {
43 
44 class BaseProvider;
45 class OtProvider;
46 class Wire;
47 using WirePointer = std::shared_ptr<Wire>;
48 class Backend;
49 class Register;
50 class Configuration;
51 class Logger;
52 class MtProvider;
53 class SbProvider;
54 class SpProvider;
55 
56 //
57 // inputs are not defined in the Gate class but only in the child classes
58 //
59 // --------
60 // | |
61 // | Gate |
62 // | |
63 // --------
64 // | <- one abstract output
65 //
66 
67 class Gate {
68  public:
69  virtual ~Gate() = default;
70 
71  virtual void EvaluateSetup() = 0;
72 
73  virtual void EvaluateOnline() = 0;
74 
75  const std::vector<WirePointer>& GetOutputWires() const { return output_wires_; }
76 
77  void Clear();
78 
79  void RegisterWaitingFor(std::size_t wire_id);
80 
82 
84 
85  void SetSetupIsReady();
86 
87  void SetOnlineIsReady();
88 
89  void WaitSetup() const;
90 
91  void WaitOnline() const;
92 
93  bool SetupIsReady() const { return setup_is_ready_; }
94 
95  std::int64_t GetId() const { return gate_id_; }
96 
97  Gate(Gate&) = delete;
98 
99  protected:
100  std::vector<WirePointer> output_wires_;
102  std::int64_t gate_id_ = -1;
103  std::unordered_set<std::size_t> wire_dependencies_;
104 
106  std::atomic<bool> setup_is_ready_ = false;
107  std::atomic<bool> online_is_ready_ = false;
108  std::atomic<bool> requires_online_interaction_ = false;
109 
110  std::atomic<bool> added_to_active_queue_ = false;
111 
114 
115  std::atomic<std::size_t> number_of_ready_dependencies_ = 0;
116 
117  Gate(Backend& backend);
118 
121  Logger& GetLogger();
127  OtProvider& GetOtProvider(const std::size_t i);
128  bool own_output_wires_{true};
129 
130  private:
131  void IfReadyAddToProcessingQueue();
132 
133  std::mutex mutex_;
134 };
135 
136 using GatePointer = std::shared_ptr<Gate>;
137 
138 //
139 // | <- one abstract input
140 // --------
141 // | |
142 // | Gate |
143 // | |
144 // --------
145 // | <- one abstract output
146 //
147 
148 class OneGate : public Gate {
149  public:
150  ~OneGate() override = default;
151 
152  OneGate(OneGate&) = delete;
153 
154  protected:
155  std::vector<WirePointer> parent_;
156 
157  OneGate(Backend& backend) : Gate(backend) {}
158 };
159 
160 //
161 // | <- one abstract (perhaps !SharePointer) input
162 // --------
163 // | |
164 // | Gate |
165 // | |
166 // --------
167 // | <- SharePointer output
168 //
169 
170 class InputGate : public OneGate {
171  public:
172  protected:
173  ~InputGate() override = default;
174 
176 
177  InputGate(InputGate&) = delete;
178 
179  std::int64_t input_owner_id_ = -1;
180 };
181 
182 using InputGatePointer = std::shared_ptr<InputGate>;
183 
184 //
185 // | <- one SharePointer input
186 // --------
187 // | |
188 // | Gate |
189 // | |
190 // --------
191 // | <- abstract output
192 //
193 
194 class OutputGate : public OneGate {
195  public:
196  ~OutputGate() override = default;
197 
198  OutputGate(OutputGate&) = delete;
199 
201 
202  protected:
203  std::int64_t output_owner_ = -1;
204 };
205 
206 using OutputGatePointer = std::shared_ptr<OutputGate>;
207 
208 //
209 // | | <- two SharePointers input
210 // --------
211 // | |
212 // | Gate |
213 // | |
214 // --------
215 // | <- SharePointer output
216 //
217 
218 class TwoGate : public Gate {
219  protected:
220  std::vector<WirePointer> parent_a_;
221  std::vector<WirePointer> parent_b_;
222 
223  TwoGate(Backend& backend) : Gate(backend) {}
224 
225  public:
226  ~TwoGate() override = default;
227 };
228 
229 //
230 // | | | <- three SharePointers input
231 // --------
232 // | |
233 // | Gate |
234 // | |
235 // --------
236 // | <- SharePointer output
237 //
238 
239 class ThreeGate : public Gate {
240  protected:
241  std::vector<WirePointer> parent_a_;
242  std::vector<WirePointer> parent_b_;
243  std::vector<WirePointer> parent_c_;
244 
245  ThreeGate(Backend& backend) : Gate(backend) {}
246 
247  public:
248  ~ThreeGate() override = default;
249 };
250 
251 //
252 // | |... | <- n SharePointers input
253 // --------
254 // | |
255 // | Gate |
256 // | |
257 // --------
258 // | <- SharePointer output
259 //
260 
261 class NInputGate : public Gate {
262  protected:
263  std::vector<WirePointer> parents_;
264 
265  NInputGate(Backend& backend) : Gate(backend) {}
266 
267  public:
268  ~NInputGate() override = default;
269 };
270 
271 } // namespace encrypto::motion
condition.h
encrypto::motion::Gate::GetSbProvider
SbProvider & GetSbProvider()
Definition: gate.cpp:108
encrypto::motion::InputGate::InputGate
InputGate(Backend &backend)
Definition: gate.h:175
encrypto::motion::OneGate::~OneGate
~OneGate() override=default
encrypto::motion::Gate::gate_type_
GateType gate_type_
Definition: gate.h:105
encrypto::motion::Gate::EvaluateOnline
virtual void EvaluateOnline()=0
encrypto::motion::Backend::GetLogger
const LoggerPointer & GetLogger() const noexcept
Definition: backend.cpp:92
encrypto::motion::OutputGate::~OutputGate
~OutputGate() override=default
encrypto::motion::FiberCondition::GetMutex
boost::fibers::mutex & GetMutex() noexcept
Get the mutex.
Definition: fiber_condition.h:76
encrypto::motion::Gate::setup_is_ready_condition_
FiberCondition setup_is_ready_condition_
Definition: gate.h:112
encrypto::motion::Gate::SignalDependencyIsReady
void SignalDependencyIsReady()
Definition: gate.cpp:41
encrypto::motion::Gate::AreDependenciesReady
bool AreDependenciesReady()
Definition: gate.h:83
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::NInputGate::parents_
std::vector< WirePointer > parents_
Definition: gate.h:263
encrypto::motion::TwoGate
Definition: gate.h:218
fiber_condition.h
encrypto::motion::OtProvider
Definition: ot_provider.h:204
encrypto::motion::TwoGate::parent_b_
std::vector< WirePointer > parent_b_
Definition: gate.h:221
encrypto::motion::Gate::GetLogger
Logger & GetLogger()
Definition: gate.cpp:100
encrypto::motion::Logger
Definition: logger.h:40
encrypto::motion::SbProvider
Definition: sb_provider.h:50
encrypto::motion::InputGate
Definition: gate.h:170
encrypto::motion::WirePointer
std::shared_ptr< Wire > WirePointer
Definition: register.h:40
encrypto::motion::FiberCondition
Wraps a boost::fibers::condition_variable with a boost::fibers::mutex and a condition checking functi...
Definition: fiber_condition.h:35
wire.h
encrypto::motion::GateType::kInput
@ kInput
encrypto::motion::OneGate::OneGate
OneGate(Backend &backend)
Definition: gate.h:157
encrypto::motion::Gate::GetSpProvider
SpProvider & GetSpProvider()
Definition: gate.cpp:106
encrypto::motion::OutputGate::output_owner_
std::int64_t output_owner_
Definition: gate.h:203
encrypto::motion::NInputGate::NInputGate
NInputGate(Backend &backend)
Definition: gate.h:265
encrypto::motion::OneGate::parent_
std::vector< WirePointer > parent_
Definition: gate.h:155
encrypto::motion::InputGate::~InputGate
~InputGate() override=default
backend.h
encrypto::motion::ThreeGate::parent_c_
std::vector< WirePointer > parent_c_
Definition: gate.h:243
encrypto::motion::Gate::Gate
Gate(Gate &)=delete
encrypto::motion::TwoGate::TwoGate
TwoGate(Backend &backend)
Definition: gate.h:223
encrypto::motion::ThreeGate::~ThreeGate
~ThreeGate() override=default
encrypto::motion::MtProvider
Definition: mt_provider.h:48
encrypto::motion::OneGate::OneGate
OneGate(OneGate &)=delete
encrypto::motion::Gate::~Gate
virtual ~Gate()=default
encrypto::motion::Gate::requires_online_interaction_
std::atomic< bool > requires_online_interaction_
Definition: gate.h:108
encrypto::motion::Gate::GetBaseProvider
BaseProvider & GetBaseProvider()
Definition: gate.cpp:102
encrypto::motion::Gate::GetRegister
Register & GetRegister()
Definition: gate.cpp:96
encrypto::motion::Gate::GetOutputWires
const std::vector< WirePointer > & GetOutputWires() const
Definition: gate.h:75
encrypto::motion::Backend::GetSbProvider
auto & GetSbProvider()
Definition: backend.h:303
encrypto::motion::Gate::number_of_ready_dependencies_
std::atomic< std::size_t > number_of_ready_dependencies_
Definition: gate.h:115
encrypto::motion::ThreeGate::ThreeGate
ThreeGate(Backend &backend)
Definition: gate.h:245
encrypto::motion::FiberCondition::NotifyAll
void NotifyAll() const noexcept
Unblocks all threads waiting for condition_variable_.
Definition: fiber_condition.h:71
encrypto::motion::TwoGate::parent_a_
std::vector< WirePointer > parent_a_
Definition: gate.h:220
encrypto::motion::Gate::GetId
std::int64_t GetId() const
Definition: gate.h:95
encrypto::motion::Gate::online_is_ready_
std::atomic< bool > online_is_ready_
Definition: gate.h:107
encrypto::motion::Backend
Definition: backend.h:88
encrypto::motion::NInputGate
Definition: gate.h:261
encrypto::motion::Gate::RegisterWaitingFor
void RegisterWaitingFor(std::size_t wire_id)
Definition: gate.cpp:36
encrypto::motion::Gate::SetOnlineIsReady
void SetOnlineIsReady()
Definition: gate.cpp:54
encrypto::motion::OutputGate::OutputGate
OutputGate(OutputGate &)=delete
encrypto::motion::Gate::SetupIsReady
bool SetupIsReady() const
Definition: gate.h:93
encrypto::motion::Gate::backend_
Backend & backend_
Definition: gate.h:101
encrypto::motion::Gate::GetOtProvider
OtProvider & GetOtProvider(const std::size_t i)
Definition: gate.cpp:110
encrypto::motion::Gate::SetSetupIsReady
void SetSetupIsReady()
Definition: gate.cpp:46
encrypto::motion::Backend::GetRegister
const RegisterPointer & GetRegister() const noexcept
Definition: backend.h:101
ot_provider.h
encrypto::motion::Gate::EvaluateSetup
virtual void EvaluateSetup()=0
encrypto::motion::OneGate
Definition: gate.h:148
encrypto::motion::Backend::GetConfiguration
const ConfigurationPointer & GetConfiguration() const noexcept
Definition: backend.h:97
encrypto::motion::Gate::online_is_ready_condition_
FiberCondition online_is_ready_condition_
Definition: gate.h:113
register.h
encrypto::motion::SpProvider
Definition: sp_provider.h:52
encrypto::motion::ThreeGate
Definition: gate.h:239
encrypto::motion
Definition: algorithm_description.cpp:35
encrypto::motion::GatePointer
std::shared_ptr< Gate > GatePointer
Definition: backend.h:64
encrypto::motion::Gate::gate_id_
std::int64_t gate_id_
Definition: gate.h:102
encrypto::motion::Gate::Clear
void Clear()
Definition: gate.cpp:80
encrypto::motion::OutputGate
Definition: gate.h:194
encrypto::motion::TwoGate::~TwoGate
~TwoGate() override=default
encrypto::motion::OutputGatePointer
std::shared_ptr< OutputGate > OutputGatePointer
Definition: gate.h:206
encrypto::motion::Gate
Definition: gate.h:67
encrypto::motion::communication::CommunicationLayer
Definition: communication_layer.h:58
encrypto::motion::GateType::kInvalid
@ kInvalid
encrypto::motion::Gate::added_to_active_queue_
std::atomic< bool > added_to_active_queue_
Definition: gate.h:110
encrypto::motion::Gate::wire_dependencies_
std::unordered_set< std::size_t > wire_dependencies_
Definition: gate.h:103
typedefs.h
encrypto::motion::NInputGate::~NInputGate
~NInputGate() override=default
encrypto::motion::Gate::setup_is_ready_
std::atomic< bool > setup_is_ready_
Definition: gate.h:106
encrypto::motion::Gate::WaitOnline
void WaitOnline() const
Definition: gate.cpp:70
encrypto::motion::Gate::own_output_wires_
bool own_output_wires_
Definition: gate.h:128
encrypto::motion::Register
Definition: register.h:48
encrypto::motion::OutputGate::OutputGate
OutputGate(Backend &backend)
Definition: gate.h:200
encrypto::motion::FiberCondition::Wait
void Wait() const
Blocks until fiber is notified and condition_function_ returns true.
Definition: fiber_condition.h:53
encrypto::motion::Gate::GetMtProvider
MtProvider & GetMtProvider()
Definition: gate.cpp:104
encrypto::motion::Backend::GetCommunicationLayer
communication::CommunicationLayer & GetCommunicationLayer()
Definition: backend.h:289
encrypto::motion::Backend::GetOtProvider
OtProvider & GetOtProvider(std::size_t party_id)
Definition: backend.cpp:373
encrypto::motion::BaseProvider
Definition: motion_base_provider.h:48
gate.h
encrypto::motion::communication
Definition: backend.h:37
encrypto::motion::Backend::GetBaseProvider
BaseProvider & GetBaseProvider()
Definition: backend.h:291
encrypto::motion::ThreeGate::parent_a_
std::vector< WirePointer > parent_a_
Definition: gate.h:241
encrypto::motion::Gate::WaitSetup
void WaitSetup() const
Definition: gate.cpp:68
encrypto::motion::Backend::GetMtProvider
auto & GetMtProvider()
Definition: backend.h:299
encrypto::motion::Configuration
Definition: configuration.h:32
encrypto::motion::GateType::kInteractive
@ kInteractive
encrypto::motion::ThreeGate::parent_b_
std::vector< WirePointer > parent_b_
Definition: gate.h:242
encrypto::motion::Backend::GetSpProvider
auto & GetSpProvider()
Definition: backend.h:301
encrypto::motion::InputGatePointer
std::shared_ptr< InputGate > InputGatePointer
Definition: backend.h:66
encrypto::motion::Gate::GetConfiguration
Configuration & GetConfiguration()
Definition: gate.cpp:98
encrypto::motion::Register::AddToActiveQueue
void AddToActiveQueue(std::size_t gate_id)
Definition: register.cpp:88
encrypto::motion::GateType
GateType
Definition: typedefs.h:177
encrypto::motion::InputGate::input_owner_id_
std::int64_t input_owner_id_
Definition: gate.h:179