MOTION  0.01
Framework for mixed-protocol multi-party computation
register.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Oleksandr Tkachenko, Lennart Braun
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 <queue>
31 #include <unordered_map>
32 
33 namespace encrypto::motion {
34 
35 struct AlgorithmDescription;
36 class FiberCondition;
37 class Gate;
38 using GatePointer = std::shared_ptr<Gate>;
39 class Wire;
40 using WirePointer = std::shared_ptr<Wire>;
41 
42 // >> forward declarations
43 
44 class Logger;
45 
46 // forward declarations <<
47 
48 class Register {
49  public:
50  Register(std::shared_ptr<Logger> logger);
51 
52  ~Register();
53 
54  std::shared_ptr<Logger> GetLogger() { return logger_; }
55 
56  std::size_t NextGateId() noexcept;
57 
58  std::size_t NextWireId() noexcept;
59 
60  std::size_t NextArithmeticSharingId(std::size_t number_of_parallel_values);
61 
62  std::size_t NextBooleanGmwSharingId(std::size_t number_of_parallel_values);
63 
64  void RegisterNextGate(GatePointer gate);
65 
67 
68  const GatePointer& GetGate(std::size_t gate_id) const {
69  return gates_.at(gate_id - gate_id_offset_);
70  }
71 
72  const auto& GetInputGates() const { return input_gates_; }
73 
74  auto& GetGates() const { return gates_; }
75 
76  void UnregisterGate(std::size_t gate_id) { gates_.at(gate_id) = nullptr; }
77 
78  void RegisterNextWire(WirePointer wire) { wires_.push_back(wire); }
79 
80  WirePointer GetWire(std::size_t wire_id) const { return wires_.at(wire_id - wire_id_offset_); }
81 
82  void UnregisterWire(std::size_t wire_id) { wires_.at(wire_id) = nullptr; }
83 
84  void AddToActiveQueue(std::size_t gate_id);
85 
86  void ClearActiveQueue();
87 
88  std::int64_t GetNextGateFromActiveQueue();
89 
91 
93 
94  std::size_t GetNumberOfEvaluatedGateSetups() const { return evaluated_gates_setup_; }
95 
96  std::size_t GetNumberOfEvaluatedGates() const { return evaluated_gates_online_; }
97 
98  std::size_t GetTotalNumberOfGates() const { return global_gate_id_ - gate_id_offset_; }
99 
100  void Reset();
101 
102  void Clear();
103 
104  std::shared_ptr<FiberCondition> GetGatesSetupDoneCondition() {
105  return gates_setup_done_condition_;
106  };
107 
108  std::shared_ptr<FiberCondition> GetGatesOnlineDoneCondition() {
109  return gates_online_done_condition_;
110  };
111 
117  std::string path, const std::shared_ptr<AlgorithmDescription>& algorithm_description);
118 
121  std::shared_ptr<AlgorithmDescription> GetCachedAlgorithmDescription(const std::string& path);
122 
123  private:
124  std::shared_ptr<Logger> logger_;
125 
126  // don't need atomic here, since only the master thread has access to these
127  std::size_t global_gate_id_ = 0, global_wire_id_ = 0;
128  std::size_t global_arithmetic_gmw_sharing_id_ = 0, global_boolean_gmw_sharing_id_ = 0;
129  std::size_t gate_id_offset_ = 0, wire_id_offset_ = 0;
130 
131  std::atomic<std::size_t> evaluated_gates_online_ = 0;
132  std::atomic<std::size_t> evaluated_gates_setup_ = 0;
133  // flags which should be changed to true as soon as the counters above reach
134  // gates_.size(); need to be protected using the mutexes from the conditions below
135  bool gates_setup_done_flag_ = false;
136  bool gates_online_done_flag_ = false;
137  // conditions which enable waiting for the above flags to change to true
138  std::shared_ptr<FiberCondition> gates_setup_done_condition_;
139  std::shared_ptr<FiberCondition> gates_online_done_condition_;
140 
141  std::queue<std::size_t> active_gates_;
142  std::mutex active_queue_mutex_;
143 
144  std::vector<GatePointer> input_gates_;
145  std::vector<GatePointer> gates_;
146 
147  std::vector<WirePointer> wires_;
148 
149  std::unordered_map<std::string, std::shared_ptr<AlgorithmDescription>> cached_algos_;
150  std::mutex cached_algos_mutex_;
151 };
152 
153 using RegisterPointer = std::shared_ptr<Register>;
154 
155 } // namespace encrypto::motion
encrypto::motion::Register::GetCachedAlgorithmDescription
std::shared_ptr< AlgorithmDescription > GetCachedAlgorithmDescription(const std::string &path)
Gets cached AlgorithmDescription object read from a file and placed into cached_algos_.
Definition: register.cpp:188
encrypto::motion::Register::GetTotalNumberOfGates
std::size_t GetTotalNumberOfGates() const
Definition: register.h:98
encrypto::motion::Register::RegisterNextInputGate
void RegisterNextInputGate(GatePointer gate)
Definition: register.cpp:82
encrypto::motion::Register::GetNumberOfEvaluatedGateSetups
std::size_t GetNumberOfEvaluatedGateSetups() const
Definition: register.h:94
fiber_condition.h
encrypto::motion::Register::Clear
void Clear()
Definition: register.cpp:161
encrypto::motion::Logger
Definition: logger.h:40
encrypto::motion::WirePointer
std::shared_ptr< Wire > WirePointer
Definition: register.h:40
encrypto::motion::Register::UnregisterGate
void UnregisterGate(std::size_t gate_id)
Definition: register.h:76
configuration.h
wire.h
encrypto::motion::Register::GetGates
auto & GetGates() const
Definition: register.h:74
encrypto::motion::Register::AddCachedAlgorithmDescription
bool AddCachedAlgorithmDescription(std::string path, const std::shared_ptr< AlgorithmDescription > &algorithm_description)
Tries to insert an AlgorithmDescription object read from a file into cached_algos_.
Definition: register.cpp:181
encrypto::motion::Register::IncrementEvaluatedGatesOnlineCounter
void IncrementEvaluatedGatesOnlineCounter()
Definition: register.cpp:125
encrypto::motion::RegisterPointer
std::shared_ptr< Register > RegisterPointer
Definition: backend.h:84
encrypto::motion::Register::~Register
~Register()
Definition: register.cpp:47
encrypto::motion::Register::IncrementEvaluatedGatesSetupCounter
void IncrementEvaluatedGatesSetupCounter()
Definition: register.cpp:114
register.h
logger.h
encrypto::motion
Definition: algorithm_description.cpp:35
encrypto::motion::Register::GetNumberOfEvaluatedGates
std::size_t GetNumberOfEvaluatedGates() const
Definition: register.h:96
encrypto::motion::GatePointer
std::shared_ptr< Gate > GatePointer
Definition: backend.h:64
encrypto::motion::Register::ClearActiveQueue
void ClearActiveQueue()
Definition: register.cpp:96
encrypto::motion::Register::RegisterNextGate
void RegisterNextGate(GatePointer gate)
Definition: register.cpp:77
encrypto::motion::Register::GetLogger
std::shared_ptr< Logger > GetLogger()
Definition: register.h:54
encrypto::motion::Register::GetGatesSetupDoneCondition
std::shared_ptr< FiberCondition > GetGatesSetupDoneCondition()
Definition: register.h:104
encrypto::motion::Register
Definition: register.h:48
encrypto::motion::kVerboseDebug
constexpr bool kVerboseDebug
Definition: constants.h:50
encrypto::motion::Register::GetGate
const GatePointer & GetGate(std::size_t gate_id) const
Definition: register.h:68
encrypto::motion::Register::NextBooleanGmwSharingId
std::size_t NextBooleanGmwSharingId(std::size_t number_of_parallel_values)
Definition: register.cpp:70
gate.h
encrypto::motion::Register::RegisterNextWire
void RegisterNextWire(WirePointer wire)
Definition: register.h:78
constants.h
encrypto::motion::Register::GetGatesOnlineDoneCondition
std::shared_ptr< FiberCondition > GetGatesOnlineDoneCondition()
Definition: register.h:108
encrypto::motion::Register::NextArithmeticSharingId
std::size_t NextArithmeticSharingId(std::size_t number_of_parallel_values)
Definition: register.cpp:63
encrypto::motion::Register::UnregisterWire
void UnregisterWire(std::size_t wire_id)
Definition: register.h:82
encrypto::motion::Register::GetNextGateFromActiveQueue
std::int64_t GetNextGateFromActiveQueue()
Definition: register.cpp:102
encrypto::motion::Register::GetWire
WirePointer GetWire(std::size_t wire_id) const
Definition: register.h:80
encrypto::motion::Register::Reset
void Reset()
Definition: register.cpp:136
encrypto::motion::Register::NextWireId
std::size_t NextWireId() noexcept
Definition: register.cpp:58
encrypto::motion::Register::Register
Register(std::shared_ptr< Logger > logger)
Definition: register.cpp:40
encrypto::motion::Register::GetInputGates
const auto & GetInputGates() const
Definition: register.h:72
encrypto::motion::Register::NextGateId
std::size_t NextGateId() noexcept
Definition: register.cpp:53
encrypto::motion::Register::AddToActiveQueue
void AddToActiveQueue(std::size_t gate_id)
Definition: register.cpp:88