MOTION  0.01
Framework for mixed-protocol multi-party computation
constant_share.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 "constant_wire.h"
28 
29 #include "protocols/share.h"
30 
31 namespace encrypto::motion::proto {
32 
33 /*
34  * Allow only unsigned integers for Arithmetic shares.
35  */
36 template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
38  public:
39  ConstantArithmeticShare(const WirePointer& wire) : motion::Share(wire->GetBackend()) {
40  wires_ = {wire};
41  if (!wires_.at(0)) {
42  throw(std::runtime_error("Something went wrong with creating a constant arithmetic share"));
43  }
44 
45  if constexpr (kDebug) ConstructorConsistencyCheck();
46  }
47 
49  : motion::Share(wire->GetBackend()) {
50  wires_ = {std::static_pointer_cast<Wire>(wire)};
51 
52  if constexpr (kDebug) ConstructorConsistencyCheck();
53  }
54 
56  : motion::Share(wires.at(0)->GetBackend()) {
57  for (auto i = 0ull; i < wires.size(); ++i) {
58  wires_.emplace_back(wires.at(i));
59  }
60 
61  if (wires.empty()) {
62  throw(std::runtime_error("Trying to create a constant arithmetic share without wires"));
63  } else if (wires.size() > 1) {
64  throw(
65  std::runtime_error(fmt::format("Cannot create a constant arithmetic share "
66  "from more than 1 wire; got {} wires",
67  wires.size())));
68  }
69 
70  if constexpr (kDebug) ConstructorConsistencyCheck();
71  }
72 
73  ConstantArithmeticShare(const std::vector<WirePointer>& wires)
74  : motion::Share(wires.at(0)->GetBackend()) {
75  if (wires.size() == 0) {
76  throw(std::runtime_error("Trying to create an arithmetic share without wires"));
77  }
78  if (wires.size() > 1) {
79  throw(
80  std::runtime_error(fmt::format("Cannot create an arithmetic share "
81  "from more than 1 wire; got {} wires",
82  wires.size())));
83  }
84  wires_ = {wires.at(0)};
85  if (!wires_.at(0)) {
86  throw(std::runtime_error("Something went wrong with creating an arithmetic share"));
87  }
88  }
89 
90  ConstantArithmeticShare(const std::vector<T>& input, Backend& backend) : motion::Share(backend) {
91  wires_ = {std::make_shared<ConstantArithmeticWirePointer<T>>(input, backend)};
92  }
93 
94  ConstantArithmeticShare(const T input, Backend& backend) : motion::Share(backend) {
95  wires_ = {std::make_shared<ConstantArithmeticWirePointer<T>>(input, backend)};
96  }
97 
98  ~ConstantArithmeticShare() override = default;
99 
100  std::size_t GetNumberOfSimdValues() const noexcept final {
101  return wires_.at(0)->GetNumberOfSimdValues();
102  };
103 
104  MpcProtocol GetProtocol() const noexcept final {
105  assert(wires_.at(0)->GetProtocol() == MpcProtocol::kArithmeticConstant);
106  return wires_.at(0)->GetProtocol();
107  }
108 
109  CircuitType GetCircuitType() const noexcept final {
110  assert(wires_.at(0)->GetCircuitType() == CircuitType::kArithmetic);
111  return wires_.at(0)->GetCircuitType();
112  }
113 
115  auto wire = std::dynamic_pointer_cast<ConstantArithmeticWirePointer<T>>(wires_.at(0));
116  assert(wire);
117  return wire;
118  }
119 
120  const std::vector<WirePointer>& GetWires() const noexcept final { return wires_; }
121 
122  std::vector<WirePointer>& GetMutableWires() noexcept final { return wires_; }
123 
124  const bool& Finished() { return wires_.at(0)->IsReady(); }
125 
126  const std::vector<T>& GetValue() const {
127  auto wire = std::dynamic_pointer_cast<ConstantArithmeticWirePointer<T>>(wires_.at(0));
128  assert(wire);
129  return wire->GetRawSharedValues();
130  }
131 
132  std::size_t GetBitLength() const noexcept final { return sizeof(T) * 8; }
133 
134  std::vector<std::shared_ptr<motion::Share>> Split() const noexcept final {
135  std::vector<std::shared_ptr<motion::Share>> v;
136  v.reserve(wires_.size());
137  for (const auto& w : wires_) {
138  const std::vector<WirePointer> w_v = {std::static_pointer_cast<Wire>(w)};
139  v.emplace_back(std::make_shared<ConstantArithmeticShare<T>>(w_v));
140  }
141  return v;
142  }
143 
144  std::shared_ptr<motion::Share> GetWire(std::size_t i) const override {
145  if (i >= wires_.size()) {
146  throw std::out_of_range(
147  fmt::format("Trying to access wire #{} out of {} wires", i, wires_.size()));
148  }
149  std::vector<motion::WirePointer> result = {std::static_pointer_cast<motion::Wire>(wires_[i])};
150  return std::make_shared<ConstantArithmeticShare<T>>(result);
151  };
152 
154 
155  private:
156  ConstantArithmeticShare() = default;
157 
158  void ConstructorConsistencyCheck() const {
159  assert(wires_.size() == 1);
160  auto arithmetic_wire =
161  std::dynamic_pointer_cast<ConstantArithmeticWirePointer<T>>(wires_.at(0));
162  assert(arithmetic_wire);
163  assert(arithmetic_wire->IsConstant());
164  assert(arithmetic_wire->GetProtocol() == MpcProtocol::kArithmeticConstant);
165  assert(arithmetic_wire->GetCircuitType() == CircuitType::kArithmetic);
166  };
167 };
168 
169 template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
170 using ConstantArithmeticSharePointer = std::shared_ptr<ConstantArithmeticShare<T>>;
171 
172 class ConstantBooleanShare final : public BooleanShare {
173  public:
174  ConstantBooleanShare(const std::vector<WirePointer>& wires);
175 
176  ConstantBooleanShare(std::vector<WirePointer>&& wires);
177 
178  const std::vector<WirePointer>& GetWires() const noexcept final { return wires_; }
179 
180  std::vector<WirePointer>& GetMutableWires() noexcept final { return wires_; }
181 
182  std::size_t GetNumberOfSimdValues() const noexcept final;
183 
184  MpcProtocol GetProtocol() const noexcept final;
185 
186  CircuitType GetCircuitType() const noexcept final;
187 
188  std::size_t GetBitLength() const noexcept final { return wires_.size(); }
189 
190  std::vector<std::shared_ptr<motion::Share>> Split() const noexcept final;
191 
192  std::shared_ptr<motion::Share> GetWire(std::size_t i) const final;
193 };
194 
196 
197 } // namespace encrypto::motion::proto
encrypto::motion::proto
Definition: backend.h:43
encrypto::motion::proto::ConstantBooleanShare::Split
std::vector< std::shared_ptr< motion::Share > > Split() const noexcept final
Definition: constant_share.cpp:88
encrypto::motion::proto::ConstantArithmeticShare::GetWire
std::shared_ptr< motion::Share > GetWire(std::size_t i) const override
Definition: constant_share.h:144
encrypto::motion::proto::ConstantBooleanShare::GetMutableWires
std::vector< WirePointer > & GetMutableWires() noexcept final
Definition: constant_share.h:180
encrypto::motion::proto::ConstantBooleanShare
Definition: constant_share.h:172
encrypto::motion::proto::ConstantArithmeticShare::ConstantArithmeticShare
ConstantArithmeticShare(const T input, Backend &backend)
Definition: constant_share.h:94
encrypto::motion::proto::ConstantBooleanShare::GetProtocol
MpcProtocol GetProtocol() const noexcept final
Definition: constant_share.cpp:29
encrypto::motion::MpcProtocol::kArithmeticConstant
@ kArithmeticConstant
encrypto::motion::CircuitType
CircuitType
Definition: typedefs.h:165
encrypto::motion::proto::ConstantArithmeticShare::Split
std::vector< std::shared_ptr< motion::Share > > Split() const noexcept final
Definition: constant_share.h:134
encrypto::motion::proto::ConstantBooleanShare::GetCircuitType
CircuitType GetCircuitType() const noexcept final
Definition: constant_share.cpp:37
encrypto::motion::Share::GetBackend
Backend & GetBackend() const
Definition: share.h:61
encrypto::motion::WirePointer
std::shared_ptr< Wire > WirePointer
Definition: register.h:40
encrypto::motion::BooleanShare
Definition: share.h:82
encrypto::motion::proto::ConstantBooleanShare::GetNumberOfSimdValues
std::size_t GetNumberOfSimdValues() const noexcept final
Definition: constant_share.cpp:81
encrypto::motion::proto::ConstantArithmeticShare::GetMutableWires
std::vector< WirePointer > & GetMutableWires() noexcept final
Definition: constant_share.h:122
encrypto::motion::MpcProtocol::kBooleanConstant
@ kBooleanConstant
encrypto::motion::proto::ConstantArithmeticShare::GetWires
const std::vector< WirePointer > & GetWires() const noexcept final
Definition: constant_share.h:120
encrypto::motion::proto::ConstantBooleanShare::GetBitLength
std::size_t GetBitLength() const noexcept final
Definition: constant_share.h:188
encrypto::motion::proto::ConstantArithmeticShare::ConstantArithmeticShare
ConstantArithmeticShare(const std::vector< T > &input, Backend &backend)
Definition: constant_share.h:90
encrypto::motion::proto::ConstantArithmeticShare::ConstantArithmeticShare
ConstantArithmeticShare(const WirePointer &wire)
Definition: constant_share.h:39
encrypto::motion::CircuitType::kArithmetic
@ kArithmetic
constant_wire.h
encrypto::motion::proto::ConstantArithmeticShare::Finished
const bool & Finished()
Definition: constant_share.h:124
encrypto::motion::proto::ConstantArithmeticSharePointer
std::shared_ptr< ConstantArithmeticShare< T > > ConstantArithmeticSharePointer
Definition: constant_share.h:170
encrypto::motion::Share::wires_
std::vector< WirePointer > wires_
Definition: share.h:77
encrypto::motion::Backend
Definition: backend.h:88
constant_share.h
encrypto::motion::proto::ConstantBooleanSharePointer
std::shared_ptr< ConstantBooleanShare > ConstantBooleanSharePointer
Definition: constant_share.h:195
encrypto::motion::proto::ConstantArithmeticShare::GetCircuitType
CircuitType GetCircuitType() const noexcept final
Definition: constant_share.h:109
encrypto::motion::proto::ConstantArithmeticShare::ConstantArithmeticShare
ConstantArithmeticShare(const std::vector< WirePointer > &wires)
Definition: constant_share.h:73
encrypto::motion::proto::ConstantArithmeticShare::GetConstantArithmeticWire
const ConstantArithmeticWirePointer< T > GetConstantArithmeticWire() const
Definition: constant_share.h:114
encrypto::motion::proto::ConstantArithmeticShare::GetProtocol
MpcProtocol GetProtocol() const noexcept final
Definition: constant_share.h:104
encrypto::motion::proto::ConstantArithmeticShare
Definition: constant_share.h:37
encrypto::motion::MpcProtocol
MpcProtocol
Definition: typedefs.h:140
encrypto::motion::proto::ConstantArithmeticShare::ConstantArithmeticShare
ConstantArithmeticShare(const std::vector< ConstantArithmeticWirePointer< T >> &wires)
Definition: constant_share.h:55
share.h
encrypto::motion::proto::ConstantArithmeticShare::GetValue
const std::vector< T > & GetValue() const
Definition: constant_share.h:126
encrypto::motion::proto::ConstantArithmeticWirePointer
std::shared_ptr< ConstantArithmeticWire< T > > ConstantArithmeticWirePointer
Definition: constant_wire.h:67
encrypto::motion::proto::ConstantArithmeticShare::~ConstantArithmeticShare
~ConstantArithmeticShare() override=default
encrypto::motion::proto::ConstantArithmeticShare::ConstantArithmeticShare
ConstantArithmeticShare(const ConstantArithmeticWirePointer< T > &wire)
Definition: constant_share.h:48
encrypto::motion::proto::ConstantArithmeticShare::GetNumberOfSimdValues
std::size_t GetNumberOfSimdValues() const noexcept final
Definition: constant_share.h:100
encrypto::motion::Share
Definition: share.h:41
encrypto::motion::kDebug
constexpr bool kDebug
Definition: config.h:36
encrypto::motion::proto::ConstantArithmeticShare::GetBitLength
std::size_t GetBitLength() const noexcept final
Definition: constant_share.h:132
encrypto::motion::proto::ConstantBooleanShare::GetWires
const std::vector< WirePointer > & GetWires() const noexcept final
Definition: constant_share.h:178
encrypto::motion::proto::ConstantBooleanShare::ConstantBooleanShare
ConstantBooleanShare(const std::vector< WirePointer > &wires)
Definition: constant_share.cpp:45
encrypto::motion::proto::ConstantBooleanShare::GetWire
std::shared_ptr< motion::Share > GetWire(std::size_t i) const final
Definition: constant_share.cpp:98
encrypto::motion::CircuitType::kBoolean
@ kBoolean