MOTION  0.01
Framework for mixed-protocol multi-party computation
wire.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 <string>
31 #include <unordered_set>
32 #include <vector>
33 
35 #include "utility/typedefs.h"
36 
37 namespace encrypto::motion {
38 
39 class FiberCondition;
40 class Backend;
41 
42 class Gate; // forward declaration
43 using GatePointer = std::shared_ptr<Gate>;
44 
45 class Wire {
46  public:
47  std::size_t GetNumberOfSimdValues() const;
48 
49  virtual enum CircuitType GetCircuitType() const = 0;
50 
51  virtual enum MpcProtocol GetProtocol() const = 0;
52 
53  virtual ~Wire();
54 
55  void RegisterWaitingGate(std::size_t gate_id);
56 
57  void SetOnlineFinished();
58 
59  const auto& GetWaitingGatesIds() const noexcept { return waiting_gate_ids_; }
60 
61  const std::atomic<bool>& IsReady() const noexcept;
62 
63  const FiberCondition& GetIsReadyCondition() const noexcept { return is_done_condition_; }
64 
65  std::size_t GetWireId() const { return static_cast<std::size_t>(wire_id_); }
66 
67  Backend& GetBackend() const { return backend_; }
68 
69  static std::string PrintIds(const std::vector<std::shared_ptr<Wire>>& wires);
70 
71  virtual std::size_t GetBitLength() const = 0;
72 
73  void Clear() {
74  is_done_ = false;
75  DynamicClear();
76  }
77 
78  virtual bool IsConstant() const noexcept = 0;
79 
80  Wire(const Wire&) = delete;
81 
82  protected:
84 
86  std::size_t n_simd_ = 0;
87 
88  // is ready flag is needed for callbacks, i.e.,
89  // gates will wait for wires to be evaluated to proceed with their evaluation
90  std::atomic<bool> is_done_ = false;
91 
93 
94  std::int64_t wire_id_ = -1;
95 
96  std::unordered_set<std::size_t> waiting_gate_ids_;
97 
98  Wire(Backend& backend, std::size_t number_of_simd);
99 
100  static void SignalReadyToDependency(std::size_t gate_id, Backend& backend);
101 
102  virtual void DynamicClear(){};
103 
104  private:
105  void InitializationHelper();
106 
107  std::mutex mutex_;
108 };
109 
110 using WirePointer = std::shared_ptr<Wire>;
111 
112 class BooleanWire : public Wire {
113  public:
114  ~BooleanWire() override = default;
115 
117 
118  BooleanWire(BooleanWire&) = delete;
119 
120  protected:
121  BooleanWire(Backend& backend, std::size_t number_of_simd) : Wire(backend, number_of_simd) {}
122 };
123 
124 using BooleanWirePointer = std::shared_ptr<BooleanWire>;
125 
126 } // namespace encrypto::motion
encrypto::motion::BooleanWire::BooleanWire
BooleanWire(BooleanWire &)=delete
encrypto::motion::Wire::n_simd_
std::size_t n_simd_
Number of values that are logically processed in parallel.
Definition: wire.h:86
encrypto::motion::Wire::backend_
Backend & backend_
Definition: wire.h:83
encrypto::motion::BooleanWire::GetCircuitType
CircuitType GetCircuitType() const final
Definition: wire.h:116
encrypto::motion::FiberCondition::GetMutex
boost::fibers::mutex & GetMutex() noexcept
Get the mutex.
Definition: fiber_condition.h:76
encrypto::motion::Wire::Clear
void Clear()
Definition: wire.h:73
encrypto::motion::CircuitType
CircuitType
Definition: typedefs.h:165
encrypto::motion::BooleanWire::~BooleanWire
~BooleanWire() override=default
fiber_condition.h
encrypto::motion::BooleanWire
Definition: wire.h:112
encrypto::motion::Wire::wire_id_
std::int64_t wire_id_
Definition: wire.h:94
encrypto::motion::WirePointer
std::shared_ptr< Wire > WirePointer
Definition: register.h:40
encrypto::motion::Wire::~Wire
virtual ~Wire()
Definition: wire.cpp:45
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::Wire::DynamicClear
virtual void DynamicClear()
Definition: wire.h:102
backend.h
encrypto::motion::FiberCondition::NotifyAll
void NotifyAll() const noexcept
Unblocks all threads waiting for condition_variable_.
Definition: fiber_condition.h:71
encrypto::motion::Wire::waiting_gate_ids_
std::unordered_set< std::size_t > waiting_gate_ids_
Definition: wire.h:96
encrypto::motion::Backend
Definition: backend.h:88
encrypto::motion::Wire::IsConstant
virtual bool IsConstant() const noexcept=0
encrypto::motion::Wire::GetWireId
std::size_t GetWireId() const
Definition: wire.h:65
encrypto::motion::Wire::GetIsReadyCondition
const FiberCondition & GetIsReadyCondition() const noexcept
Definition: wire.h:63
encrypto::motion::Wire::is_done_
std::atomic< bool > is_done_
Definition: wire.h:90
encrypto::motion::Wire::Wire
Wire(const Wire &)=delete
encrypto::motion::Backend::GetRegister
const RegisterPointer & GetRegister() const noexcept
Definition: backend.h:101
register.h
encrypto::motion
Definition: algorithm_description.cpp:35
encrypto::motion::Wire::is_done_condition_
FiberCondition is_done_condition_
Definition: wire.h:92
encrypto::motion::Wire::GetWaitingGatesIds
const auto & GetWaitingGatesIds() const noexcept
Definition: wire.h:59
encrypto::motion::GatePointer
std::shared_ptr< Gate > GatePointer
Definition: backend.h:64
encrypto::motion::Wire::GetProtocol
virtual enum MpcProtocol GetProtocol() const =0
encrypto::motion::Wire::GetBackend
Backend & GetBackend() const
Definition: wire.h:67
encrypto::motion::Wire::GetCircuitType
virtual enum CircuitType GetCircuitType() const =0
encrypto::motion::Wire::SetOnlineFinished
void SetOnlineFinished()
Definition: wire.cpp:52
encrypto::motion::Wire::SignalReadyToDependency
static void SignalReadyToDependency(std::size_t gate_id, Backend &backend)
Definition: wire.cpp:80
typedefs.h
encrypto::motion::MpcProtocol
MpcProtocol
Definition: typedefs.h:140
encrypto::motion::BooleanWirePointer
std::shared_ptr< BooleanWire > BooleanWirePointer
Definition: wire.h:124
encrypto::motion::Wire
Definition: wire.h:45
encrypto::motion::BooleanWire::BooleanWire
BooleanWire(Backend &backend, std::size_t number_of_simd)
Definition: wire.h:121
gate.h
encrypto::motion::Wire::GetNumberOfSimdValues
std::size_t GetNumberOfSimdValues() const
Definition: wire.cpp:36
encrypto::motion::Wire::IsReady
const std::atomic< bool > & IsReady() const noexcept
Definition: wire.cpp:69
encrypto::motion::Wire::GetBitLength
virtual std::size_t GetBitLength() const =0
encrypto::motion::Wire::RegisterWaitingGate
void RegisterWaitingGate(std::size_t gate_id)
Definition: wire.cpp:47
encrypto::motion::Backend::GetGate
const GatePointer & GetGate(std::size_t gate_id) const
Definition: backend.cpp:163
encrypto::motion::Wire::PrintIds
static std::string PrintIds(const std::vector< std::shared_ptr< Wire >> &wires)
Definition: wire.cpp:71
encrypto::motion::CircuitType::kBoolean
@ kBoolean