MOTION  0.01
Framework for mixed-protocol multi-party computation
share_wrapper.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 <cassert>
28 #include <limits>
29 #include <memory>
30 #include <span>
31 #include <vector>
32 
33 #include "share.h"
34 #include "utility/bit_vector.h"
35 #include "utility/typedefs.h"
36 
37 namespace encrypto::motion {
38 
39 struct AlgorithmDescription;
40 
41 class Share;
42 using SharePointer = std::shared_ptr<Share>;
43 
44 class ShareWrapper {
45  public:
46  ShareWrapper() : share_(nullptr){};
47 
48  ShareWrapper(const SharePointer& share) : share_(share) {}
49  ShareWrapper(const ShareWrapper& sw) : share_(sw.share_) {}
50 
51  void operator=(SharePointer share) { share_ = share; }
52  void operator=(const ShareWrapper& sw) { share_ = sw.share_; }
53 
54  ShareWrapper operator~() const;
55 
56  ShareWrapper operator^(const ShareWrapper& other) const;
57 
59  *this = *this ^ other;
60  return *this;
61  }
62 
63  ShareWrapper operator&(const ShareWrapper& other) const;
64 
66  *this = *this & other;
67  return *this;
68  }
69 
70  ShareWrapper operator|(const ShareWrapper& other) const;
71 
73  *this = *this | other;
74  return *this;
75  }
76 
77  ShareWrapper operator+(const ShareWrapper& other) const;
78 
80  *this = *this + other;
81  return *this;
82  }
83 
84  ShareWrapper operator-(const ShareWrapper& other) const;
85 
87  *this = *this - other;
88  return *this;
89  }
90 
91  ShareWrapper operator*(const ShareWrapper& other) const;
92 
94  *this = *this * other;
95  return *this;
96  }
97 
98  ShareWrapper operator==(const ShareWrapper& other) const;
99 
100  // use this as the selection bit
101  // returns this ? a : b
102  ShareWrapper Mux(const ShareWrapper& a, const ShareWrapper& b) const;
103 
104  template <MpcProtocol P>
105  ShareWrapper Convert() const;
106 
107  SharePointer& Get() { return share_; }
108 
109  const SharePointer& Get() const { return share_; }
110 
111  const SharePointer& operator*() const { return share_; }
112 
113  const SharePointer& operator->() const { return share_; }
114 
117  ShareWrapper Out(std::size_t output_owner = std::numeric_limits<std::int64_t>::max()) const;
118 
120  std::vector<ShareWrapper> Split() const;
121 
124  ShareWrapper GetWire(std::size_t i) const;
125 
128  static ShareWrapper Concatenate(std::vector<ShareWrapper>&& input) { return Concatenate(input); }
129 
132  static ShareWrapper Concatenate(const std::vector<ShareWrapper>::const_iterator vector_begin,
133  const std::vector<ShareWrapper>::const_iterator vector_end) {
134  const auto v{std::vector<ShareWrapper>(vector_begin, vector_end)};
135  return Concatenate(v);
136  }
137 
140  static ShareWrapper Concatenate(std::span<const ShareWrapper> input);
141 
144  ShareWrapper Evaluate(const std::shared_ptr<const AlgorithmDescription>& algo) const {
145  return Evaluate(*algo);
146  }
147 
150  ShareWrapper Evaluate(const AlgorithmDescription& algo) const;
151 
154  ShareWrapper Subset(std::vector<std::size_t>&& positions);
155 
163  ShareWrapper Subset(std::span<const std::size_t> positions);
164 
172  std::vector<ShareWrapper> Unsimdify();
173 
180  static ShareWrapper Simdify(std::span<SharePointer> input);
181 
184  static ShareWrapper Simdify(std::span<const ShareWrapper> input);
185 
188  static ShareWrapper Simdify(std::vector<ShareWrapper>&& input);
189 
198  template <typename T>
199  T As() const;
200 
201  private:
202  SharePointer share_;
203 
204  template <typename T>
205  ShareWrapper Add(SharePointer share, SharePointer other) const;
206 
207  template <typename T>
208  ShareWrapper Sub(SharePointer share, SharePointer other) const;
209 
210  template <typename T>
211  ShareWrapper Mul(SharePointer share, SharePointer other) const;
212 
213  template <typename T>
214  ShareWrapper Square(SharePointer share) const;
215 
216  ShareWrapper ArithmeticGmwToBmr() const;
217 
218  ShareWrapper BooleanGmwToArithmeticGmw() const;
219 
220  ShareWrapper BooleanGmwToBmr() const;
221 
222  ShareWrapper BmrToBooleanGmw() const;
223 
224  void ShareConsistencyCheck() const;
225 };
226 
227 } // namespace encrypto::motion
encrypto::motion::ShareWrapper::ShareWrapper
ShareWrapper(const ShareWrapper &sw)
Definition: share_wrapper.h:49
encrypto::motion::PrimitiveOperationType::kOr
@ kOr
encrypto::motion::ShareWrapper::Out
ShareWrapper Out(std::size_t output_owner=std::numeric_limits< std::int64_t >::max()) const
constructs an output gate, which reconstructs the cleartext result. The default parameter for the out...
Definition: share_wrapper.cpp:414
encrypto::motion::AlgorithmDescription::number_of_wires
std::size_t number_of_wires
Definition: algorithm_description.h:64
encrypto::motion::ShareWrapper::operator==
ShareWrapper operator==(const ShareWrapper &other) const
Definition: share_wrapper.cpp:238
encrypto::motion::ShareWrapper
Definition: share_wrapper.h:44
encrypto::motion::ShareWrapper::Get
SharePointer & Get()
Definition: share_wrapper.h:107
encrypto::motion::proto::arithmetic_gmw::Share
Definition: arithmetic_gmw_share.h:37
unsimdify_gate.h
boolean_gmw_gate.h
encrypto::motion::MpcProtocol::kArithmeticConstant
@ kArithmeticConstant
encrypto::motion::ShareWrapper::As
T As() const
converts the information on the wires to T. Boolean and arithmetic GMW returns the secret-shared valu...
Definition: share_wrapper.cpp:616
arithmetic_gmw_gate.h
encrypto::motion::ShareWrapper::Mux
ShareWrapper Mux(const ShareWrapper &a, const ShareWrapper &b) const
Definition: share_wrapper.cpp:282
encrypto::motion::MpcProtocol::kBmr
@ kBmr
bit_vector.h
geninput.type
type
Definition: geninput.py:149
bmr_wire.h
algorithm_description.h
encrypto::motion::ShareWrapper::ShareWrapper
ShareWrapper(const SharePointer &share)
Definition: share_wrapper.h:48
encrypto::motion::PrimitiveOperationType::kInv
@ kInv
encrypto::motion::PrimitiveOperationType::kXor
@ kXor
backend.h
encrypto::motion::PrimitiveOperationType::kAnd
@ kAnd
encrypto::motion::ShareWrapper::Split
std::vector< ShareWrapper > Split() const
splits the share into single wires.
Definition: share_wrapper.cpp:459
encrypto::motion::MpcProtocol::kBooleanConstant
@ kBooleanConstant
encrypto::motion::ShareWrapper::Subset
ShareWrapper Subset(std::vector< std::size_t > &&positions)
constructs a SubsetGate that returns values stored at positions in this->share_. Internally calls Sha...
Definition: share_wrapper.cpp:878
LowDepthReduce
T LowDepthReduce(std::vector< T > input, BinaryOperation operation)
Definition: low_depth_reduce.h:40
encrypto::motion::ShareWrapper::ShareWrapper
ShareWrapper()
Definition: share_wrapper.h:46
encrypto::motion::AlgorithmDescription::gates
std::vector< PrimitiveOperation > gates
Definition: algorithm_description.h:67
encrypto::motion::ShareWrapper::operator|
ShareWrapper operator|(const ShareWrapper &other) const
Definition: share_wrapper.cpp:136
encrypto::motion::BitVector<>
encrypto::motion::ShareWrapper::Concatenate
static ShareWrapper Concatenate(std::vector< ShareWrapper > &&input)
concatenates wires in multiple shares in one share.
Definition: share_wrapper.h:128
encrypto::motion::CircuitType::kArithmetic
@ kArithmetic
constant_wire.h
encrypto::motion::ShareWrapper::operator-=
ShareWrapper & operator-=(const ShareWrapper &other)
Definition: share_wrapper.h:86
encrypto::motion::ShareWrapper::operator=
void operator=(SharePointer share)
Definition: share_wrapper.h:51
encrypto::motion::MpcProtocol::kBooleanGmw
@ kBooleanGmw
encrypto::motion::ShareWrapper::Unsimdify
std::vector< ShareWrapper > Unsimdify()
constructs an Unsimdify gate with this->share_ as input. UnsimdifyGate decomposes this->share_ into s...
Definition: share_wrapper.cpp:889
bmr_share.h
constant_share.h
encrypto::motion::ShareWrapper::operator~
ShareWrapper operator~() const
Definition: share_wrapper.cpp:55
boolean_gmw_wire.h
encrypto::motion::AlgorithmDescription::number_of_output_wires
std::size_t number_of_output_wires
Definition: algorithm_description.h:64
encrypto::motion::ShareWrapper::operator&
ShareWrapper operator&(const ShareWrapper &other) const
Definition: share_wrapper.cpp:108
encrypto::motion::ShareWrapper::operator+
ShareWrapper operator+(const ShareWrapper &other) const
Definition: share_wrapper.cpp:151
simdify_gate.h
secure_unsigned_integer.h
encrypto::motion::AlgorithmDescription::number_of_gates
std::size_t number_of_gates
Definition: algorithm_description.h:65
encrypto::motion::ShareWrapper::GetWire
ShareWrapper GetWire(std::size_t i) const
yields wire #i from share_ as ShareWrapper.
Definition: share_wrapper.cpp:467
encrypto::motion::AlgorithmDescription
Definition: algorithm_description.h:43
b2a_gate.h
share_wrapper.h
encrypto::motion::ShareWrapper::operator*
const SharePointer & operator*() const
Definition: share_wrapper.h:111
encrypto::motion
Definition: algorithm_description.cpp:35
constant_gate.h
arithmetic_gmw_share.h
encrypto::motion::SharePointer
std::shared_ptr< Share > SharePointer
Definition: conversion_gate.h:49
encrypto::motion::ShareWrapper::Concatenate
static ShareWrapper Concatenate(const std::vector< ShareWrapper >::const_iterator vector_begin, const std::vector< ShareWrapper >::const_iterator vector_end)
concatenates wires in multiple shares in one share.
Definition: share_wrapper.h:132
encrypto::motion::ShareWrapper::Convert
ShareWrapper Convert() const
Definition: share_wrapper.cpp:322
encrypto::motion::MpcProtocol::kInvalid
@ kInvalid
encrypto::motion::ShareWrapper::Get
const SharePointer & Get() const
Definition: share_wrapper.h:109
encrypto::motion::IsPowerOfTwo
bool IsPowerOfTwo(UnsignedIntegralType x)
Check if unisgned integral value is a power of two.
Definition: helpers.h:398
encrypto::motion::ShareWrapper::operator=
void operator=(const ShareWrapper &sw)
Definition: share_wrapper.h:52
typedefs.h
conversion_gate.h
encrypto::motion::AlgorithmDescription::number_of_input_wires_parent_b
std::optional< std::size_t > number_of_input_wires_parent_b
Definition: algorithm_description.h:66
share.h
encrypto::motion::ShareWrapper::operator&=
ShareWrapper & operator&=(const ShareWrapper &other)
Definition: share_wrapper.h:65
encrypto::motion::ShareWrapper::operator*=
ShareWrapper & operator*=(const ShareWrapper &other)
Definition: share_wrapper.h:93
encrypto::motion::is_specialization
Definition: share_wrapper.cpp:610
bmr_gate.h
encrypto::motion::ShareWrapper::Simdify
static ShareWrapper Simdify(std::span< SharePointer > input)
constructs a SimdifyGate that composes the shares in input into a "larger" share with all the input s...
Definition: share_wrapper.cpp:907
subset_gate.h
encrypto::motion::ShareWrapper::operator-
ShareWrapper operator-(const ShareWrapper &other) const
Definition: share_wrapper.cpp:175
encrypto::motion::MpcProtocol::kArithmeticGmw
@ kArithmeticGmw
encrypto::motion::swap
void swap(ReusablePromise< R, MutexType, ConditionVariableType > &lhs, ReusablePromise< R, MutexType, ConditionVariableType > &rhs) noexcept
Definition: reusable_future.h:270
encrypto::motion::ShareWrapper::Evaluate
ShareWrapper Evaluate(const std::shared_ptr< const AlgorithmDescription > &algo) const
evaluates AlgorithmDescription also on this->share_ as input.
Definition: share_wrapper.h:144
encrypto::motion::ShareWrapper::operator+=
ShareWrapper & operator+=(const ShareWrapper &other)
Definition: share_wrapper.h:79
encrypto::motion::ShareWrapper::operator->
const SharePointer & operator->() const
Definition: share_wrapper.h:113
encrypto::motion::kDebug
constexpr bool kDebug
Definition: config.h:36
low_depth_reduce.h
boolean_gmw_share.h
encrypto::motion::ShareWrapper::operator^
ShareWrapper operator^(const ShareWrapper &other) const
Definition: share_wrapper.cpp:77
encrypto::motion::ShareWrapper::operator^=
ShareWrapper & operator^=(const ShareWrapper &other)
Definition: share_wrapper.h:58
encrypto::motion::ShareWrapper::operator|=
ShareWrapper & operator|=(const ShareWrapper &other)
Definition: share_wrapper.h:72
arithmetic_gmw_wire.h
encrypto::motion::AlgorithmDescription::number_of_input_wires_parent_a
std::size_t number_of_input_wires_parent_a
Definition: algorithm_description.h:64
encrypto::motion::CircuitType::kBoolean
@ kBoolean