MOTION  0.01
Framework for mixed-protocol multi-party computation
sb_provider.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 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 <cstdint>
28 #include <memory>
29 #include <type_traits>
30 #include <vector>
31 
34 
36 
37 class CommunicationLayer;
38 
39 } // namespace encrypto::motion::communication
40 
41 namespace encrypto::motion {
42 
43 class Logger;
44 class SpProvider;
45 struct RunTimeStatistics;
46 struct SharedBitsData;
47 
48 // Provider for Shared Bits (SBs),
49 // sharings of a random bit 0 or 1 in Z/2^kZ
50 class SbProvider {
51  public:
52  bool NeedSbs() const noexcept;
53 
54  template <typename T>
55  std::size_t GetNumberOfSbs() const noexcept {
56  if constexpr (std::is_same_v<T, std::uint8_t>) {
57  return number_of_sbs_8_;
58  } else if constexpr (std::is_same_v<T, std::uint16_t>) {
59  return number_of_sbs_16_;
60  } else if constexpr (std::is_same_v<T, std::uint32_t>) {
61  return number_of_sbs_32_;
62  } else if constexpr (std::is_same_v<T, std::uint64_t>) {
63  return number_of_sbs_64_;
64  } else {
65  throw std::runtime_error("Unknown type");
66  }
67  }
68 
69  template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
70  std::size_t RequestSbs(const std::size_t number_of_sbs) noexcept {
71  std::size_t offset;
72  if constexpr (std::is_same_v<T, std::uint8_t>) {
73  offset = number_of_sbs_8_;
74  number_of_sbs_8_ += number_of_sbs;
75  } else if constexpr (std::is_same_v<T, std::uint16_t>) {
76  offset = number_of_sbs_16_;
77  number_of_sbs_16_ += number_of_sbs;
78  } else if constexpr (std::is_same_v<T, std::uint32_t>) {
79  offset = number_of_sbs_32_;
80  number_of_sbs_32_ += number_of_sbs;
81  } else if constexpr (std::is_same_v<T, std::uint64_t>) {
82  offset = number_of_sbs_64_;
83  number_of_sbs_64_ += number_of_sbs;
84  } else {
85  throw std::runtime_error("Unknown type");
86  }
87  return offset;
88  }
89 
90  template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
91  std::vector<T> GetSbs(const std::size_t offset, const std::size_t n = 1) {
92  WaitFinished();
93  if constexpr (std::is_same_v<T, std::uint8_t>) {
94  return GetSbs(sbs_8_, offset, n);
95  } else if constexpr (std::is_same_v<T, std::uint16_t>) {
96  return GetSbs(sbs_16_, offset, n);
97  } else if constexpr (std::is_same_v<T, std::uint32_t>) {
98  return GetSbs(sbs_32_, offset, n);
99  } else if constexpr (std::is_same_v<T, std::uint64_t>) {
100  return GetSbs(sbs_64_, offset, n);
101  } else {
102  throw std::runtime_error("Unknown type");
103  }
104  }
105 
106  template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
107  const std::vector<T>& GetSbsAll() noexcept {
108  WaitFinished();
109  if constexpr (std::is_same_v<T, std::uint8_t>) {
110  return sbs_8_;
111  } else if constexpr (std::is_same_v<T, std::uint16_t>) {
112  return sbs_16_;
113  } else if constexpr (std::is_same_v<T, std::uint32_t>) {
114  return sbs_32_;
115  } else if constexpr (std::is_same_v<T, std::uint64_t>) {
116  return sbs_64_;
117  } else {
118  throw std::runtime_error("Unknown type");
119  }
120  }
121 
122  virtual void PreSetup() = 0;
123  virtual void Setup() = 0;
124 
125  // blocking wait
126  void WaitFinished() { finished_condition_->Wait(); }
127 
128  protected:
129  SbProvider(const std::size_t my_id);
130  SbProvider() = delete;
131 
133 
134  std::vector<std::uint8_t> sbs_8_;
135  std::vector<std::uint16_t> sbs_16_;
136  std::vector<std::uint32_t> sbs_32_;
137  std::vector<std::uint64_t> sbs_64_;
138 
139  const std::size_t my_id_;
140 
141  bool finished_ = false;
142  std::shared_ptr<FiberCondition> finished_condition_;
143 
144  private:
145  template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
146  inline std::vector<T> GetSbs(const std::vector<T>& sbs, const std::size_t offset,
147  const std::size_t n) const {
148  assert(offset + n <= sbs.size());
149  return std::vector(sbs.cbegin() + offset, sbs.cbegin() + offset + n);
150  }
151 };
152 
153 class SbProviderFromSps final : public SbProvider {
154  public:
156  std::shared_ptr<SpProvider> sp_provider, Logger& logger,
157  RunTimeStatistics& run_time_statistics);
159 
160  void PreSetup() final override;
161 
162  // needs completed SPs
163  void Setup() final override;
164 
165  private:
166  void RegisterSps();
167  void RegisterForMessages();
168  void ComputeSbs() noexcept;
169 
170  // void ReconstructionHelper(
171  // std::vector<std::uint16_t>& ds_8, std::vector<std::uint32_t>& ds_16,
172  // std::vector<std::uint64_t>& ds_32,
173  // std::function<void(std::size_t, const std::vector<uint8_t>&)> send_fctn,
174  // std::vector<ReusableFuture<std::vector<std::uint8_t>>>& futures);
175 
176  void ParseOutputs();
177 
178  communication::CommunicationLayer& communication_layer_;
179  std::size_t number_of_parties_;
180  std::shared_ptr<SpProvider> sp_provider_;
181  std::vector<std::unique_ptr<SharedBitsData>> data_;
182 
183  std::size_t offset_sps_16_;
184  std::size_t offset_sps_32_;
185  std::size_t offset_sps_64_;
186  std::size_t offset_sps_128_;
187 
188  std::vector<ReusableFuture<std::vector<uint8_t>>> mask_message_futures_;
189  std::vector<ReusableFuture<std::vector<uint8_t>>> reconstruct_message_futures_;
190 
191  const std::size_t kMaxBatchSize{10'000};
192 
193  Logger& logger_;
194  RunTimeStatistics& run_time_statistics_;
195 };
196 
197 } // namespace encrypto::motion
encrypto::motion::SbProviderFromSps::Setup
void Setup() final override
Definition: sb_provider.cpp:138
encrypto::motion::RunTimeStatistics::RecordEnd
void RecordEnd()
Definition: run_time_statistics.h:62
encrypto::motion::SbProviderFromSps::SbProviderFromSps
SbProviderFromSps(communication::CommunicationLayer &communication_layer, std::shared_ptr< SpProvider > sp_provider, Logger &logger, RunTimeStatistics &run_time_statistics)
Definition: sb_provider.cpp:86
shared_bits_data.h
helpers.h
encrypto::motion::SharedBitsData
Definition: shared_bits_data.h:35
encrypto::motion::kReconstructMessage
@ kReconstructMessage
Definition: shared_bits_data.h:33
encrypto::motion::RunTimeStatistics::StatisticsId::kSbSetup
@ kSbSetup
encrypto::motion::SbProvider::number_of_sbs_64_
std::size_t number_of_sbs_64_
Definition: sb_provider.h:132
fiber_condition.h
encrypto::motion::ReusableFuture
Definition: reusable_future.h:129
encrypto::motion::Logger
Definition: logger.h:40
sb_impl.h
encrypto::motion::SbProvider::GetSbs
std::vector< T > GetSbs(const std::size_t offset, const std::size_t n=1)
Definition: sb_provider.h:91
encrypto::motion::communication::GetMessage
const encrypto::motion::communication::Message * GetMessage(const void *buf)
Definition: message_generated.h:146
reusable_future.h
encrypto::motion::SbProvider
Definition: sb_provider.h:50
shared_bits_message_generated.h
encrypto::motion::SbProvider::NeedSbs
bool NeedSbs() const noexcept
Definition: sb_provider.cpp:43
encrypto::motion::SbProvider::my_id_
const std::size_t my_id_
Definition: sb_provider.h:139
encrypto::motion::SbProvider::GetSbsAll
const std::vector< T > & GetSbsAll() noexcept
Definition: sb_provider.h:107
encrypto::motion::SbProviderFromSps::~SbProviderFromSps
~SbProviderFromSps()
Definition: sb_provider.cpp:112
encrypto::motion::SbProvider::number_of_sbs_16_
std::size_t number_of_sbs_16_
Definition: sb_provider.h:132
encrypto::motion::SbProvider::number_of_sbs_32_
std::size_t number_of_sbs_32_
Definition: sb_provider.h:132
encrypto::motion::Gather
static std::vector< std::uint8_t > Gather(const std::vector< std::uint16_t > &ds_8, const std::vector< std::uint32_t > &ds_16, const std::vector< std::uint64_t > &ds_32, const std::vector< __uint128_t > &ds_64)
Definition: sb_provider.cpp:196
encrypto::motion::Scatter
static std::vector< std::uint8_t > Scatter(std::vector< std::uint16_t > &ds_8, std::vector< std::uint32_t > &ds_16, std::vector< std::uint64_t > &ds_32, std::vector< __uint128_t > &ds_64, const std::vector< std::uint8_t > &buffer)
Definition: sb_provider.cpp:217
encrypto::motion::Logger::LogDebug
void LogDebug(const std::string &message)
Definition: logger.cpp:142
encrypto::motion::SbProvider::sbs_64_
std::vector< std::uint64_t > sbs_64_
Definition: sb_provider.h:137
encrypto::motion::communication::CommunicationLayer::RegisterMessageHandler
void RegisterMessageHandler(MessageHandlerFunction, const std::vector< MessageType > &message_types)
Definition: communication_layer.cpp:393
communication_layer.h
encrypto::motion::SbProvider::number_of_sbs_8_
std::size_t number_of_sbs_8_
Definition: sb_provider.h:132
encrypto::motion::communication::CommunicationLayer::GetNumberOfParties
std::size_t GetNumberOfParties() const
Definition: communication_layer.h:65
encrypto::motion::SbProvider::sbs_8_
std::vector< std::uint8_t > sbs_8_
Definition: sb_provider.h:134
encrypto::motion::SbProvider::finished_
bool finished_
Definition: sb_provider.h:141
encrypto::motion::SbMessageHandler
Definition: sb_provider.cpp:51
encrypto::motion::SbProvider::SbProvider
SbProvider()=delete
encrypto::motion::SbProvider::RequestSbs
std::size_t RequestSbs(const std::size_t number_of_sbs) noexcept
Definition: sb_provider.h:70
logger.h
encrypto::motion
Definition: algorithm_description.cpp:35
encrypto::motion::SbProvider::sbs_16_
std::vector< std::uint16_t > sbs_16_
Definition: sb_provider.h:135
encrypto::motion::communication::CommunicationLayer::BroadcastMessage
void BroadcastMessage(std::vector< std::uint8_t > &&message)
Definition: communication_layer.cpp:353
encrypto::motion::SbProviderFromSps
Definition: sb_provider.h:153
encrypto::motion::communication::CommunicationLayer::GetMyId
std::size_t GetMyId() const
Definition: communication_layer.h:66
encrypto::motion::SbProvider::GetNumberOfSbs
std::size_t GetNumberOfSbs() const noexcept
Definition: sb_provider.h:55
encrypto::motion::RunTimeStatistics
Definition: run_time_statistics.h:32
encrypto::motion::communication::MessageHandler
Definition: message_handler.h:38
encrypto::motion::communication::CommunicationLayer
Definition: communication_layer.h:58
encrypto::motion::SharedBitsData::MessageReceived
void MessageReceived(const SharedBitsMessageType type, const std::uint8_t *message, const std::size_t size)
Definition: shared_bits_data.cpp:44
encrypto::motion::ReconstructionHelper
static void ReconstructionHelper(std::vector< std::uint16_t > &xs_8, std::vector< std::uint32_t > &xs_16, std::vector< std::uint64_t > &xs_32, std::vector< __uint128_t > &xs_64, std::size_t number_of_parties, std::function< void(const std::vector< uint8_t > &)> broadcast_function, std::vector< ReusableFuture< std::vector< std::uint8_t >>> &futures)
Definition: sb_provider.cpp:240
encrypto::motion::GetByteSize
static constexpr auto GetByteSize(const std::vector< T > &v)
Definition: sb_provider.cpp:191
message_handler.h
encrypto::motion::communication::BuildSharedBitsReconstructMessage
flatbuffers::FlatBufferBuilder BuildSharedBitsReconstructMessage(const std::vector< std::uint8_t > &buffer)
Definition: shared_bits_message.cpp:38
encrypto::motion::RunTimeStatistics::StatisticsId::kSbPresetup
@ kSbPresetup
encrypto::motion::SbProvider::PreSetup
virtual void PreSetup()=0
sb_provider.h
encrypto::motion::communication::MessageType::kSharedBitsMask
@ kSharedBitsMask
encrypto::motion::SbProvider::Setup
virtual void Setup()=0
encrypto::motion::communication::MessageType::kSharedBitsReconstruct
@ kSharedBitsReconstruct
encrypto::motion::SbProvider::sbs_32_
std::vector< std::uint32_t > sbs_32_
Definition: sb_provider.h:136
encrypto::motion::SbProvider::WaitFinished
void WaitFinished()
Definition: sb_provider.h:126
encrypto::motion::communication
Definition: backend.h:37
encrypto::motion::SbMessageHandler::SbMessageHandler
SbMessageHandler(SharedBitsData &data)
Definition: sb_provider.cpp:53
encrypto::motion::SbProvider::finished_condition_
std::shared_ptr< FiberCondition > finished_condition_
Definition: sb_provider.h:142
encrypto::motion::communication::GetSharedBitsMessage
const encrypto::motion::communication::SharedBitsMessage * GetSharedBitsMessage(const void *buf)
Definition: shared_bits_message_generated.h:68
encrypto::motion::SbMessageHandler::ReceivedMessage
void ReceivedMessage(std::size_t, std::vector< std::uint8_t > &&message)
Definition: sb_provider.cpp:62
encrypto::motion::communication::BuildSharedBitsMaskMessage
flatbuffers::FlatBufferBuilder BuildSharedBitsMaskMessage(const std::vector< std::uint8_t > &buffer)
Definition: shared_bits_message.cpp:31
constants.h
shared_bits_message.h
encrypto::motion::kDebug
constexpr bool kDebug
Definition: config.h:36
encrypto::motion::SbProviderFromSps::PreSetup
void PreSetup() final override
Definition: sb_provider.cpp:119
run_time_statistics.h
encrypto::motion::RunTimeStatistics::RecordStart
void RecordStart()
Definition: run_time_statistics.h:56
encrypto::motion::communication::CommunicationLayer::DeregisterMessageHandler
void DeregisterMessageHandler(const std::vector< MessageType > &message_types)
Definition: communication_layer.cpp:414
encrypto::motion::kMaskMessage
@ kMaskMessage
Definition: shared_bits_data.h:33
sp_provider.h