MOTION  0.01
Framework for mixed-protocol multi-party computation
bmr_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 "bmr_share.h"
28 
29 #include <future>
30 #include <span>
31 
33 #include "protocols/gate.h"
34 #include "utility/bit_vector.h"
35 #include "utility/block.h"
37 
38 namespace encrypto::motion {
39 
40 class OtVectorSender;
41 class OtVectorReceiver;
42 class FixedXcOt128Sender;
43 class FixedXcOt128Receiver;
44 class XcOtBitSender;
45 class XcOtBitReceiver;
46 
47 } // namespace encrypto::motion
48 
50 
51 class InputGate final : public motion::InputGate {
52  using Base = motion::InputGate;
53 
54  public:
55  InputGate(std::size_t number_of_simd, std::size_t bit_size, std::size_t input_owner_id,
56  Backend& backend);
57 
58  InputGate(std::span<const motion::BitVector<>> input, std::size_t input_owner_id,
59  Backend& backend);
60 
61  InputGate(std::vector<motion::BitVector<>>&& input, std::size_t input_owner_id, Backend& backend);
62 
63  void InitializationHelper();
64 
65  ~InputGate() final = default;
66 
67  void EvaluateSetup() final override;
68 
69  void EvaluateOnline() final override;
70 
72 
74 
75  auto& GetInputPromise() { return input_promise_; }
76 
77  protected:
78  std::size_t number_of_simd_{0};
79  std::size_t bit_size_{0};
81  std::vector<motion::ReusableFiberFuture<motion::Block128Vector>> received_public_keys_;
84 };
85 
86 constexpr std::size_t kAll = std::numeric_limits<std::int64_t>::max();
87 
88 class OutputGate final : public motion::OutputGate {
89  using Base = motion::OutputGate;
90 
91  public:
92  OutputGate(const motion::SharePointer& parent, std::size_t output_owner = kAll);
93 
94  ~OutputGate() final = default;
95 
96  void EvaluateSetup() final override;
97 
98  void EvaluateOnline() final override;
99 
101 
103 
104  protected:
106  std::shared_ptr<boolean_gmw::OutputGate> output_gate_;
107 
108  std::vector<motion::BitVector<>> output_;
109  std::vector<std::vector<motion::BitVector<>>> shared_outputs_;
110 
111  bool is_my_output_ = false;
112 
113  std::mutex m;
114 };
115 
116 class XorGate final : public TwoGate {
117  public:
119 
120  ~XorGate() final = default;
121 
122  void EvaluateSetup() final override;
123 
124  void EvaluateOnline() final override;
125 
127 
129 
130  XorGate() = delete;
131 
132  XorGate(const Gate&) = delete;
133 };
134 
135 class InvGate final : public OneGate {
136  public:
137  InvGate(const motion::SharePointer& parent);
138 
139  ~InvGate() final = default;
140 
141  void EvaluateSetup() final override;
142 
143  void EvaluateOnline() final override;
144 
146 
148 
149  InvGate() = delete;
150 
151  InvGate(const Gate&) = delete;
152 };
153 
154 class AndGate final : public TwoGate {
155  public:
157 
158  ~AndGate() final;
159 
160  void EvaluateSetup() final override;
161 
162  void EvaluateOnline() final override;
163 
165 
167 
168  AndGate() = delete;
169 
170  AndGate(const Gate&) = delete;
171 
172  private:
173  std::vector<std::vector<std::unique_ptr<motion::XcOtBitSender>>> sender_ots_1_;
174  std::vector<std::vector<std::unique_ptr<motion::FixedXcOt128Sender>>> sender_ots_kappa_;
175  std::vector<std::vector<std::unique_ptr<motion::XcOtBitReceiver>>> receiver_ots_1_;
176  std::vector<std::vector<std::unique_ptr<motion::FixedXcOt128Receiver>>> receiver_ots_kappa_;
177 
178  std::vector<motion::ReusableFiberFuture<motion::Block128Vector>> received_garbled_rows_;
179 
180  // buffer to store all garbled tables for all wires
181  // structure: wires X (simd X (row_00 || row_01 || row_10 || row_11))
182  motion::Block128Vector garbled_tables_;
183 
184  void GenerateRandomness();
185 };
186 
187 } // namespace encrypto::motion::proto::bmr
encrypto::motion::proto::bmr::Provider::GetGlobalOffset
const Block128 & GetGlobalOffset() const
Definition: bmr_provider.h:46
encrypto::motion::Block128Vector::resize
void resize(std::size_t new_size)
Resize the Block128Vector to contain new_size elements. New elements are left uninitialized.
Definition: block.h:233
to_string
std::string to_string(Provider p)
Definition: benchmark_providers.h:44
encrypto::motion::Logger::LogTrace
void LogTrace(const std::string &message)
Definition: logger.cpp:110
encrypto::motion::proto::bmr::SharePointer
std::shared_ptr< Share > SharePointer
Definition: bmr_share.h:52
encrypto::motion::Gate::gate_type_
GateType gate_type_
Definition: gate.h:105
encrypto::motion::proto::bmr::InputGate
Definition: bmr_gate.h:51
encrypto::motion::proto::bmr::XorGate::EvaluateOnline
void EvaluateOnline() final override
Definition: bmr_gate.cpp:544
motion_base_provider.h
encrypto::motion::communication::BuildBmrAndMessage
flatbuffers::FlatBufferBuilder BuildBmrAndMessage(const std::size_t id, const std::vector< std::uint8_t > &payload)
Definition: bmr_message.cpp:65
boolean_gmw_gate.h
encrypto::motion::proto::bmr::InvGate::GetOutputAsBmrShare
const bmr::SharePointer GetOutputAsBmrShare() const
Definition: bmr_gate.cpp:707
pseudo_random_generator.h
encrypto::motion::proto::bmr::XorGate::GetOutputAsShare
const motion::SharePointer GetOutputAsShare() const
Definition: bmr_gate.cpp:591
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::proto::bmr::OutputGate::EvaluateSetup
void EvaluateSetup() final override
Definition: bmr_gate.cpp:403
encrypto::motion::BitVector::GetData
const auto & GetData() const noexcept
Get const reference to content of BitVector.
Definition: bit_vector.h:152
encrypto::motion::TwoGate
Definition: gate.h:218
encrypto::motion::primitives::Prg::SetKey
void SetKey(const std::uint8_t *key)
Definition: pseudo_random_generator.cpp:32
encrypto::motion::TwoGate::parent_b_
std::vector< WirePointer > parent_b_
Definition: gate.h:221
encrypto::motion::ReusableFuture
Definition: reusable_future.h:129
encrypto::motion::proto::bmr::InputGate::bit_size_
std::size_t bit_size_
Number of wires.
Definition: bmr_gate.h:79
encrypto::motion::Gate::GetLogger
Logger & GetLogger()
Definition: gate.cpp:100
encrypto::motion::MpcProtocol::kBmr
@ kBmr
encrypto::motion::Block128::MakeZero
static Block128 MakeZero()
Create a zero-initialized Block128.
Definition: block.h:63
AesniBmrDkc
void AesniBmrDkc(const void *round_keys_input, const void *key_a, const void *key_b, std::uint64_t gate_id, std::size_t number_of_parties, void *output_input_pointer)
Definition: aesni_primitives.cpp:339
encrypto::motion::proto::bmr::InvGate::EvaluateSetup
void EvaluateSetup() final override
Definition: bmr_gate.cpp:632
reusable_future.h
encrypto::motion::proto::bmr::AndGate::GetOutputAsBmrShare
const bmr::SharePointer GetOutputAsBmrShare() const
Definition: bmr_gate.cpp:1270
encrypto::motion::InputGate
Definition: gate.h:170
bit_vector.h
bmr_wire.h
encrypto::motion::communication::BuildBmrInput0Message
flatbuffers::FlatBufferBuilder BuildBmrInput0Message(const std::size_t id, const std::vector< std::uint8_t > &payload)
Definition: bmr_message.cpp:45
encrypto::motion::proto::bmr::InputGate::~InputGate
~InputGate() final=default
encrypto::motion::OutputGate::output_owner_
std::int64_t output_owner_
Definition: gate.h:203
encrypto::motion::proto::bmr::XorGate::GetOutputAsBmrShare
const bmr::SharePointer GetOutputAsBmrShare() const
Definition: bmr_gate.cpp:585
encrypto::motion::proto::bmr::InvGate::~InvGate
~InvGate() final=default
encrypto::motion::kKappa
constexpr std::size_t kKappa
Definition: constants.h:65
encrypto::motion::OneGate::parent_
std::vector< WirePointer > parent_
Definition: gate.h:155
encrypto::motion::proto::bmr::OutputGate::is_my_output_
bool is_my_output_
Definition: bmr_gate.h:111
encrypto::motion::Block128Vector::ByteSize
std::size_t ByteSize() const
Get size of the Block128Vector content in bytes.
Definition: block.h:228
backend.h
encrypto::motion::GateType::kNonInteractive
@ kNonInteractive
encrypto::motion::Block128Vector
Vector of 128 bit / 16 B blocks.
Definition: block.h:168
block.h
encrypto::motion::proto::bmr::AndGate
Definition: bmr_gate.h:154
encrypto::motion::proto::bmr::OutputGate::GetOutputAsShare
const motion::SharePointer GetOutputAsShare() const
Definition: bmr_gate.cpp:467
encrypto::motion::BitVector::Reserve
void Reserve(std::size_t number_of_bits)
Reserves new space for BitVector, so that it can contain at least number_of_bits bits.
Definition: bit_vector.h:185
encrypto::motion::BitVector<>
encrypto::motion::proto::bmr::OutputGate
Definition: bmr_gate.h:88
encrypto::motion::Logger::LogDebug
void LogDebug(const std::string &message)
Definition: logger.cpp:142
encrypto::motion::Block128Vector::SetToZero
void SetToZero()
Set all Block128 in this vector to zero.
Definition: block.h:255
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::proto::boolean_gmw::SharePointer
std::shared_ptr< Share > SharePointer
Definition: backend.h:46
encrypto::motion::proto::bmr::OutputGate::gmw_output_share_
boolean_gmw::SharePointer gmw_output_share_
Definition: bmr_gate.h:105
encrypto::motion::TwoGate::parent_a_
std::vector< WirePointer > parent_a_
Definition: gate.h:220
bmr_share.h
encrypto::motion::Register::IncrementEvaluatedGatesOnlineCounter
void IncrementEvaluatedGatesOnlineCounter()
Definition: register.cpp:125
encrypto::motion::Gate::online_is_ready_
std::atomic< bool > online_is_ready_
Definition: gate.h:107
encrypto::motion::proto::bmr::OutputGate::shared_outputs_
std::vector< std::vector< motion::BitVector<> > > shared_outputs_
Definition: bmr_gate.h:109
encrypto::motion::Backend
Definition: backend.h:88
encrypto::motion::Backend::GetBmrProvider
proto::bmr::Provider & GetBmrProvider()
Definition: backend.h:293
encrypto::motion::proto::bmr
Definition: backend.h:50
encrypto::motion::Gate::RegisterWaitingFor
void RegisterWaitingFor(std::size_t wire_id)
Definition: gate.cpp:36
encrypto::motion::proto::bmr::OutputGate::GetOutputAsBmrShare
const bmr::SharePointer GetOutputAsBmrShare() const
Definition: bmr_gate.cpp:461
geninput.choices
choices
Definition: geninput.py:153
communication_layer.h
boolean_gmw_wire.h
aesni_primitives.h
encrypto::motion::Gate::SetOnlineIsReady
void SetOnlineIsReady()
Definition: gate.cpp:54
encrypto::motion::proto::bmr::InputGate::EvaluateOnline
void EvaluateOnline() final override
Definition: bmr_gate.cpp:169
encrypto::motion::communication::CommunicationLayer::GetNumberOfParties
std::size_t GetNumberOfParties() const
Definition: communication_layer.h:65
bmr_provider.h
encrypto::motion::proto::bmr::Provider::RegisterForGarbledRows
std::vector< ReusableFiberFuture< Block128Vector > > RegisterForGarbledRows(std::size_t gate_id, std::size_t number_blocks)
Definition: bmr_provider.cpp:109
encrypto::motion::proto::bmr::InputGate::GetOutputAsBmrShare
const bmr::SharePointer GetOutputAsBmrShare() const
Definition: bmr_gate.cpp:315
encrypto::motion::proto::bmr::AndGate::GetOutputAsShare
const motion::SharePointer GetOutputAsShare() const
Definition: bmr_gate.cpp:1276
encrypto::motion::Gate::backend_
Backend & backend_
Definition: gate.h:101
encrypto::motion::proto::bmr::OutputGate::output_
std::vector< motion::BitVector<> > output_
Definition: bmr_gate.h:108
encrypto::motion::proto::bmr::InputGate::received_public_keys_
std::vector< motion::ReusableFiberFuture< motion::Block128Vector > > received_public_keys_
Definition: bmr_gate.h:81
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
ot_provider.h
encrypto::motion::Register::IncrementEvaluatedGatesSetupCounter
void IncrementEvaluatedGatesSetupCounter()
Definition: register.cpp:114
encrypto::motion::OneGate
Definition: gate.h:148
encrypto::motion::proto::bmr::InputGate::received_public_values_
motion::ReusableFiberFuture< motion::BitVector<> > received_public_values_
Definition: bmr_gate.h:80
encrypto::motion::Block128Vector::data
Block128 * data()
Get pointer to the first Block128.
Definition: block.h:219
encrypto::motion::proto::bmr::InputGate::GetInputPromise
auto & GetInputPromise()
Definition: bmr_gate.h:75
encrypto::motion::proto::bmr::InvGate::EvaluateOnline
void EvaluateOnline() final override
Definition: bmr_gate.cpp:669
encrypto::motion::BitVector::Empty
bool Empty() const
Check if BitVector is empty.
Definition: bit_vector.h:146
encrypto::motion
Definition: algorithm_description.cpp:35
encrypto::motion::proto::bmr::InputGate::input_future_
motion::ReusableFiberFuture< std::vector< motion::BitVector<> > > input_future_
Definition: bmr_gate.h:82
encrypto::motion::proto::bmr::OutputGate::OutputGate
OutputGate(const motion::SharePointer &parent, std::size_t output_owner=kAll)
Definition: bmr_gate.cpp:327
encrypto::motion::proto::bmr::kAll
constexpr std::size_t kAll
Definition: bmr_gate.h:86
encrypto::motion::SharePointer
std::shared_ptr< Share > SharePointer
Definition: conversion_gate.h:49
encrypto::motion::proto::bmr::InputGate::number_of_simd_
std::size_t number_of_simd_
Number of parallel values on wires.
Definition: bmr_gate.h:78
encrypto::motion::Gate::gate_id_
std::int64_t gate_id_
Definition: gate.h:102
bmr_message.h
encrypto::motion::proto::bmr::OutputGate::m
std::mutex m
Definition: bmr_gate.h:113
encrypto::motion::proto::bmr::InvGate
Definition: bmr_gate.h:135
encrypto::motion::proto::bmr::OutputGate::~OutputGate
~OutputGate() final=default
encrypto::motion::communication::CommunicationLayer::GetMyId
std::size_t GetMyId() const
Definition: communication_layer.h:66
encrypto::motion::OutputGate
Definition: gate.h:194
encrypto::motion::primitives::Prg
Definition: pseudo_random_generator.h:41
encrypto::motion::communication::BuildBmrInput1Message
flatbuffers::FlatBufferBuilder BuildBmrInput1Message(const std::size_t id, const std::vector< std::uint8_t > &payload)
Definition: bmr_message.cpp:55
encrypto::motion::proto::bmr::InputGate::InitializationHelper
void InitializationHelper()
Definition: bmr_gate.cpp:79
encrypto::motion::proto::bmr::XorGate::XorGate
XorGate()=delete
encrypto::motion::Gate
Definition: gate.h:67
encrypto::motion::proto::bmr::InputGate::InputGate
InputGate(std::size_t number_of_simd, std::size_t bit_size, std::size_t input_owner_id, Backend &backend)
Definition: bmr_gate.cpp:45
ot_flavors.h
encrypto::motion::OtProvider::RegisterReceiveXcOtBit
std::unique_ptr< XcOtBitReceiver > RegisterReceiveXcOtBit(std::size_t number_of_ots=1)
Definition: ot_provider.cpp:123
encrypto::motion::Register::RegisterNextGate
void RegisterNextGate(GatePointer gate)
Definition: register.cpp:77
encrypto::motion::OtProvider::RegisterSendFixedXcOt128
std::unique_ptr< FixedXcOt128Sender > RegisterSendFixedXcOt128(std::size_t number_of_ots=1)
Definition: ot_provider.cpp:66
encrypto::motion::OtProvider::RegisterReceiveFixedXcOt128
std::unique_ptr< FixedXcOt128Receiver > RegisterReceiveFixedXcOt128(std::size_t number_of_ots=1)
Definition: ot_provider.cpp:118
encrypto::motion::Gate::setup_is_ready_
std::atomic< bool > setup_is_ready_
Definition: gate.h:106
encrypto::motion::proto::bmr::InvGate::InvGate
InvGate()=delete
encrypto::motion::OtProvider::RegisterSendXcOtBit
std::unique_ptr< XcOtBitSender > RegisterSendXcOtBit(std::size_t number_of_ots=1)
Definition: ot_provider.cpp:71
encrypto::motion::proto::bmr::InvGate::GetOutputAsShare
const motion::SharePointer GetOutputAsShare() const
Definition: bmr_gate.cpp:713
encrypto::motion::kVerboseDebug
constexpr bool kVerboseDebug
Definition: constants.h:50
encrypto::motion::proto::bmr::InputGate::EvaluateSetup
void EvaluateSetup() final override
Definition: bmr_gate.cpp:118
encrypto::motion::proto::bmr::XorGate
Definition: bmr_gate.h:116
encrypto::motion::ReusablePromise
Definition: reusable_future.h:123
encrypto::motion::proto::bmr::InputGate::GetOutputAsShare
const motion::SharePointer GetOutputAsShare() const
Definition: bmr_gate.cpp:321
encrypto::motion::BitVector::Append
void Append(bool bit) noexcept
Appends a bit to BitVector.
Definition: bit_vector.cpp:621
bmr_gate.h
gate.h
encrypto::motion::proto::bmr::OutputGate::EvaluateOnline
void EvaluateOnline() final override
Definition: bmr_gate.cpp:408
encrypto::motion::proto::bmr::XorGate::EvaluateSetup
void EvaluateSetup() final override
Definition: bmr_gate.cpp:514
encrypto::motion::proto::bmr::AndGate::EvaluateOnline
void EvaluateOnline() final override
Definition: bmr_gate.cpp:1138
encrypto::motion::proto::bmr::OutputGate::output_gate_
std::shared_ptr< boolean_gmw::OutputGate > output_gate_
Definition: bmr_gate.h:106
encrypto::motion::primitives::Prg::GetRoundKeys
const void * GetRoundKeys() const
Definition: pseudo_random_generator.h:51
encrypto::motion::proto::bmr::InputGate::input_promise_
motion::ReusableFiberPromise< std::vector< motion::BitVector<> > > input_promise_
Definition: bmr_gate.h:83
encrypto::motion::proto::bmr::XorGate::~XorGate
~XorGate() final=default
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::bmr::AndGate::AndGate
AndGate()=delete
encrypto::motion::GateType::kInteractive
@ kInteractive
encrypto::motion::kDebug
constexpr bool kDebug
Definition: config.h:36
encrypto::motion::proto::bmr::AndGate::~AndGate
~AndGate() final
encrypto::motion::BitVector::Subset
BitVector Subset(std::size_t from, std::size_t to) const
Returns a new BitVector containing the bits of this BitVector between positions from and to.
Definition: bit_vector.cpp:806
bmr_data.h
encrypto::motion::to_string
std::string to_string(std::vector< T > values)
Returns a string representation of the std::vector values.
Definition: helpers.h:455
encrypto::motion::Block128Vector::size
std::size_t size() const
Get size of Block128Vector.
Definition: block.h:225
encrypto::motion::Block128Vector::at
Block128 & at(std::size_t index)
Access Block128 at index. Throws an exception if index is out of bounds.
Definition: block.h:215
encrypto::motion::Register::NextGateId
std::size_t NextGateId() noexcept
Definition: register.cpp:53
encrypto::motion::proto::bmr::AndGate::EvaluateSetup
void EvaluateSetup() final override
Definition: bmr_gate.cpp:833
encrypto::motion::InputGate::input_owner_id_
std::int64_t input_owner_id_
Definition: gate.h:179