MOTION  0.01
Framework for mixed-protocol multi-party computation
arithmetic_gmw_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 <fmt/format.h>
28 
29 #include "arithmetic_gmw_wire.h"
30 #include "protocols/share.h"
31 
33 /*
34  * Allow only unsigned integers for Arithmetic shares.
35  */
36 template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
37 class Share final : public motion::Share {
38  using Base = motion::Share;
39 
40  public:
41  Share(const motion::WirePointer& wire) : Base(wire->GetBackend()) {
42  wires_ = {wire};
43  if (!wires_.at(0)) {
44  throw(std::runtime_error("Something went wrong with creating an arithmetic share"));
45  }
46  }
47 
49  wires_ = {std::static_pointer_cast<motion::Wire>(wire)};
50  }
51 
52  Share(const std::vector<arithmetic_gmw::WirePointer<T>>& wires)
53  : Base(wires.at(0)->GetBackend()) {
54  for (auto i = 0ull; i < wires.size(); ++i) {
55  wires_.emplace_back(wires.at(i));
56  }
57  if (wires.size() == 0) {
58  throw(std::runtime_error("Trying to create an arithmetic share without wires"));
59  }
60  if (wires.size() > 1) {
61  throw(
62  std::runtime_error(fmt::format("Cannot create an arithmetic share "
63  "from more than 1 wire; got {} wires",
64  wires.size())));
65  }
66  }
67 
68  Share(const std::vector<motion::WirePointer>& wires) : Base(wires.at(0)->GetBackend()) {
69  if (wires.size() == 0) {
70  throw(std::runtime_error("Trying to create an arithmetic share without wires"));
71  }
72  if (wires.size() > 1) {
73  throw(
74  std::runtime_error(fmt::format("Cannot create an arithmetic share "
75  "from more than 1 wire; got {} wires",
76  wires.size())));
77  }
78  wires_ = {wires.at(0)};
79  if (!wires_.at(0)) {
80  throw(std::runtime_error("Something went wrong with creating an arithmetic share"));
81  }
82  }
83 
84  Share(const std::vector<T>& input, Backend& backend) : Base(backend) {
85  wires_ = {std::make_shared<arithmetic_gmw::Wire<T>>(input, backend)};
86  }
87 
88  Share(const T input, Backend& backend) : Base(backend) {
89  wires_ = {std::make_shared<arithmetic_gmw::Wire<T>>(input, backend)};
90  }
91 
92  // std::shared_ptr<Share> operator+(const std::shared_ptr<Share> &other) {}
93 
94  ~Share() override = default;
95 
96  std::size_t GetNumberOfSimdValues() const noexcept final {
97  return wires_.at(0)->GetNumberOfSimdValues();
98  };
99 
100  MpcProtocol GetProtocol() const noexcept final {
101  assert(wires_.at(0)->GetProtocol() == MpcProtocol::kArithmeticGmw);
102  return wires_.at(0)->GetProtocol();
103  }
104 
105  CircuitType GetCircuitType() const noexcept final {
106  assert(wires_.at(0)->GetCircuitType() == CircuitType::kArithmetic);
107  return wires_.at(0)->GetCircuitType();
108  }
109 
111  auto wire = std::dynamic_pointer_cast<arithmetic_gmw::Wire<T>>(wires_.at(0));
112  assert(wire);
113  return wire;
114  }
115 
116  const std::vector<motion::WirePointer>& GetWires() const noexcept final { return wires_; }
117 
118  std::vector<motion::WirePointer>& GetMutableWires() noexcept final { return wires_; }
119 
120  const bool& Finished() { return wires_.at(0)->IsReady(); }
121 
122  const std::vector<T>& GetValue() const {
123  auto wire = std::dynamic_pointer_cast<arithmetic_gmw::Wire<T>>(wires_.at(0));
124  assert(wire);
125  return wire->GetRawSharedValues();
126  }
127 
128  std::size_t GetBitLength() const noexcept final { return sizeof(T) * 8; }
129 
130  std::vector<std::shared_ptr<Base>> Split() const noexcept final {
131  std::vector<std::shared_ptr<Base>> v;
132  v.reserve(wires_.size());
133  for (const auto& w : wires_) {
134  const std::vector<motion::WirePointer> w_v = {std::static_pointer_cast<motion::Wire>(w)};
135  v.emplace_back(std::make_shared<Share<T>>(w_v));
136  }
137  return v;
138  }
139 
140  std::shared_ptr<Base> GetWire(std::size_t i) const override {
141  if (i >= wires_.size()) {
142  throw std::out_of_range(
143  fmt::format("Trying to access wire #{} out of {} wires", i, wires_.size()));
144  }
145  std::vector<motion::WirePointer> result = {std::static_pointer_cast<motion::Wire>(wires_[i])};
146  return std::make_shared<Share<T>>(result);
147  }
148 
149  Share(Share&) = delete;
150 
151  private:
152  Share() = default;
153 };
154 
155 template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
156 using SharePointer = std::shared_ptr<Share<T>>;
157 
158 } // namespace encrypto::motion::proto::arithmetic_gmw
encrypto::motion::proto::arithmetic_gmw::Share::Finished
const bool & Finished()
Definition: arithmetic_gmw_share.h:120
encrypto::motion::proto::arithmetic_gmw::Share::GetMutableWires
std::vector< motion::WirePointer > & GetMutableWires() noexcept final
Definition: arithmetic_gmw_share.h:118
encrypto::motion::proto::arithmetic_gmw::Share
Definition: arithmetic_gmw_share.h:37
encrypto::motion::CircuitType
CircuitType
Definition: typedefs.h:165
encrypto::motion::proto::arithmetic_gmw::Share::GetBitLength
std::size_t GetBitLength() const noexcept final
Definition: arithmetic_gmw_share.h:128
encrypto::motion::proto::arithmetic_gmw::Share::GetValue
const std::vector< T > & GetValue() const
Definition: arithmetic_gmw_share.h:122
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::proto::arithmetic_gmw::Share::GetWire
std::shared_ptr< Base > GetWire(std::size_t i) const override
Definition: arithmetic_gmw_share.h:140
encrypto::motion::proto::arithmetic_gmw::Share::GetNumberOfSimdValues
std::size_t GetNumberOfSimdValues() const noexcept final
Definition: arithmetic_gmw_share.h:96
encrypto::motion::proto::arithmetic_gmw::Share::Share
Share(const arithmetic_gmw::WirePointer< T > &wire)
Definition: arithmetic_gmw_share.h:48
encrypto::motion::proto::arithmetic_gmw::Share::Share
Share(const T input, Backend &backend)
Definition: arithmetic_gmw_share.h:88
encrypto::motion::CircuitType::kArithmetic
@ kArithmetic
encrypto::motion::Share::wires_
std::vector< WirePointer > wires_
Definition: share.h:77
encrypto::motion::Backend
Definition: backend.h:88
encrypto::motion::proto::arithmetic_gmw::Share::GetCircuitType
CircuitType GetCircuitType() const noexcept final
Definition: arithmetic_gmw_share.h:105
encrypto::motion::proto::arithmetic_gmw::Share::Share
Share(const std::vector< arithmetic_gmw::WirePointer< T >> &wires)
Definition: arithmetic_gmw_share.h:52
encrypto::motion::proto::arithmetic_gmw::Share::GetProtocol
MpcProtocol GetProtocol() const noexcept final
Definition: arithmetic_gmw_share.h:100
encrypto::motion::proto::arithmetic_gmw::Share::Share
Share(const motion::WirePointer &wire)
Definition: arithmetic_gmw_share.h:41
encrypto::motion::proto::arithmetic_gmw::SharePointer
std::shared_ptr< Share< T > > SharePointer
Definition: arithmetic_gmw_share.h:156
encrypto::motion::proto::arithmetic_gmw::Share::GetArithmeticWire
const arithmetic_gmw::WirePointer< T > GetArithmeticWire()
Definition: arithmetic_gmw_share.h:110
encrypto::motion::proto::arithmetic_gmw::Share::Split
std::vector< std::shared_ptr< Base > > Split() const noexcept final
Definition: arithmetic_gmw_share.h:130
encrypto::motion::MpcProtocol
MpcProtocol
Definition: typedefs.h:140
share.h
encrypto::motion::proto::arithmetic_gmw::Share::Share
Share(const std::vector< motion::WirePointer > &wires)
Definition: arithmetic_gmw_share.h:68
encrypto::motion::MpcProtocol::kArithmeticGmw
@ kArithmeticGmw
encrypto::motion::Share
Definition: share.h:41
encrypto::motion::proto::arithmetic_gmw::Share::Share
Share(const std::vector< T > &input, Backend &backend)
Definition: arithmetic_gmw_share.h:84
encrypto::motion::proto::arithmetic_gmw
Definition: arithmetic_gmw_gate.h:45
encrypto::motion::proto::arithmetic_gmw::WirePointer
std::shared_ptr< Wire< T > > WirePointer
Definition: arithmetic_gmw_wire.h:68
encrypto::motion::proto::arithmetic_gmw::Share::GetWires
const std::vector< motion::WirePointer > & GetWires() const noexcept final
Definition: arithmetic_gmw_share.h:116
encrypto::motion::proto::arithmetic_gmw::Share::~Share
~Share() override=default
arithmetic_gmw_wire.h