MOTION  0.01
Framework for mixed-protocol multi-party computation
bit_matrix.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 "bit_vector.h"
28 
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <cassert>
32 #include <memory>
33 
35 
36 class Prg;
37 
38 } // namespace encrypto::motion::primitives
39 
40 namespace encrypto::motion {
41 
42 class BitMatrix {
43  public:
44  BitMatrix() = default;
45 
50  BitMatrix(std::size_t rows, std::size_t columns, bool value = false)
51  : number_of_columns_(columns) {
52  for (auto i = 0ull; i < rows; ++i) {
53  data_.emplace_back(columns, value);
54  }
55  }
56 
60  BitMatrix(std::vector<AlignedBitVector>&& vectors) : data_(std::move(vectors)) {
61  if (data_.size() > 0) {
62  auto number_of_columns = data_.at(0).GetSize();
63  for (auto i = 1ull; i < data_.size(); ++i) {
64  assert(data_.at(i).GetSize() == number_of_columns);
65  }
66  number_of_columns_ = number_of_columns;
67  }
68  }
69 
73  BitMatrix(const std::vector<AlignedBitVector>& vectors) : data_(vectors) {
74  if (data_.size() > 0) {
75  auto number_of_columns = data_.at(0).GetSize();
76  for (auto i = 1ull; i < data_.size(); ++i) {
77  assert(data_.at(i).GetSize() == number_of_columns);
78  }
79  number_of_columns_ = number_of_columns;
80  }
81  }
82 
83  // XXX The copy and move constructors/assignments can set to default.
84  // XXX Assignments do not return BitMatrix& and do not check for self-assignment.
85  BitMatrix(const BitMatrix& other) {
86  data_.insert(data_.begin(), other.data_.begin(), other.data_.end());
87  number_of_columns_ = other.number_of_columns_;
88  }
89 
90  BitMatrix(BitMatrix&& other) {
91  data_ = std::move(other.data_);
92  number_of_columns_ = other.number_of_columns_;
93  }
94 
95  void operator=(const BitMatrix& other) {
96  data_.insert(data_.begin(), other.data_.begin(), other.data_.end());
97  number_of_columns_ = other.number_of_columns_;
98  }
99 
100  void operator=(BitMatrix&& other) {
101  data_ = std::move(other.data_);
102  number_of_columns_ = other.number_of_columns_;
103  }
104 
107  const AlignedBitVector& GetRow(std::size_t i) const { return data_.at(i); }
108 
114  AlignedBitVector& GetMutableRow(std::size_t i) { return data_.at(i); }
115 
119  bool Get(std::size_t row_i, std::size_t column_i) const { return data_.at(row_i).Get(column_i); }
120 
125  void Set(std::size_t row_i, std::size_t column_i, bool value) {
126  data_.at(row_i).Set(value, column_i);
127  }
128 
132  void AppendRow(const AlignedBitVector& bit_vector);
133 
137  void AppendRow(AlignedBitVector&& bit_vector);
138 
142  void AppendColumn(const AlignedBitVector& bit_vector);
143 
147  void AppendColumn(AlignedBitVector&& bit_vector) { AppendColumn(bit_vector); }
148 
150  std::string AsString() const;
151 
154  void ForceSetNumColumns(std::size_t n) { number_of_columns_ = n; }
155 
157  void Transpose();
158 
161  void Transpose128Rows();
162 
167  static void Transpose128RowsInplace(std::array<std::byte*, 128>& matrix,
168  std::size_t number_of_columns);
169 
174  static void TransposeUsingBitSlicing(std::array<std::byte*, 128>& matrix,
175  std::size_t number_of_columns);
176 
183  static void SenderTransposeAndEncrypt(const std::array<const std::byte*, 128>& matrix,
184  std::vector<BitVector<>>& y0, std::vector<BitVector<>>& y1,
185  const BitVector<> choices, primitives::Prg& prg_fixed_key,
186  const std::size_t number_of_columns,
187  const std::vector<std::size_t>& bitlengths);
188 
194  static void ReceiverTransposeAndEncrypt(const std::array<const std::byte*, 128>& matrix,
195  std::vector<BitVector<>>& output,
196  primitives::Prg& prg_fixed_key,
197  const std::size_t number_of_columns,
198  const std::vector<std::size_t>& bitlengths);
199 
202  bool operator==(const BitMatrix& other) const;
203 
206  bool operator!=(const BitMatrix& other) const { return !(*this == other); }
207 
209  auto GetNumRows() const noexcept { return data_.size(); }
210 
212  auto GetNumColumns() const noexcept { return number_of_columns_; }
213 
214  private:
215  std::vector<AlignedBitVector> data_;
216 
217  std::size_t number_of_columns_ = 0;
218 
219  // blockwise inplace
220  void TransposeInternal();
221 
222  // blockwise inplace
223  void Transpose128RowsInternal();
224 
225  static void Transpose128x128InPlace(std::array<std::uint64_t*, 128>& rows_64_bit,
226  std::array<std::uint32_t*, 128>& rows_32_bit);
227 };
228 
229 } // namespace encrypto::motion
to_string
std::string to_string(Provider p)
Definition: benchmark_providers.h:44
CreateParty
encrypto::motion::PartyPointer CreateParty(const program_options::variables_map &user_options)
Definition: benchmark_providers_main.cpp:204
encrypto::motion::BitMatrix::ForceSetNumColumns
void ForceSetNumColumns(std::size_t n)
Force the number of columns to be equal to n.
Definition: bit_matrix.h:154
kSb
@ kSb
Definition: benchmark_providers.h:38
encrypto::motion::AccumulatedCommunicationStatistics::Add
void Add(const communication::TransportStatistics &statistics)
Definition: analysis.cpp:125
kROt
@ kROt
Definition: benchmark_providers.h:37
helpers.h
analysis.h
encrypto::motion::BitMatrix
Definition: bit_matrix.h:42
party.h
encrypto::motion::AccumulatedRunTimeStatistics
Definition: analysis.h:43
encrypto::motion::BitMatrix::TransposeUsingBitSlicing
static void TransposeUsingBitSlicing(std::array< std::byte *, 128 > &matrix, std::size_t number_of_columns)
Transposes a matrix of 128 rows and arbitrary column size inplace using BitSlicing.
Definition: bit_matrix.cpp:409
Combination::bit_size
std::size_t bit_size
Definition: benchmark_main.cpp:60
pseudo_random_generator.h
encrypto::motion::BitMatrix::ReceiverTransposeAndEncrypt
static void ReceiverTransposeAndEncrypt(const std::array< const std::byte *, 128 > &matrix, std::vector< BitVector<>> &output, primitives::Prg &prg_fixed_key, const std::size_t number_of_columns, const std::vector< std::size_t > &bitlengths)
Transposes a matrix of 128 rows and arbitrary column size and encrypts it for the recepient role.
Definition: bit_matrix.cpp:640
benchmark_providers.h
ParsePartyArgument
std::tuple< std::size_t, std::string, std::uint16_t > ParsePartyArgument(const std::string &party_arguments)
Definition: benchmark_providers_main.cpp:124
Combination::bit_size_
std::size_t bit_size_
Definition: benchmark_integers_main.cpp:60
encrypto::motion::primitives::Prg::SetKey
void SetKey(const std::uint8_t *key)
Definition: pseudo_random_generator.cpp:32
encrypto::motion::BitMatrix::BitMatrix
BitMatrix(std::size_t rows, std::size_t columns, bool value=false)
Construct a rows x columns BitMatrix with all bits set to value.
Definition: bit_matrix.h:50
encrypto::motion::BitMatrix::AsString
std::string AsString() const
Return a string representation of the BitMatrix.
Definition: bit_matrix.cpp:244
encrypto::motion::PartyPointer
std::unique_ptr< Party > PartyPointer
Definition: party.h:387
encrypto::motion::BitsToBytes
constexpr std::size_t BitsToBytes(const std::size_t bits)
Returns the number of bytes necessary to store bits bits.
Definition: helpers.h:504
encrypto::motion::BitMatrix::GetNumColumns
auto GetNumColumns() const noexcept
Get number of columns in the BitMatrix.
Definition: bit_matrix.h:212
bit_vector.h
kBmt
@ kBmt
Definition: benchmark_providers.h:33
encrypto::motion::kKappa
constexpr std::size_t kKappa
Definition: constants.h:65
kAcOt
@ kAcOt
Definition: benchmark_providers.h:36
encrypto::motion::BitVector
Class representing a series of bits and providing single bit access.
Definition: bit_vector.h:64
OUT
#define OUT(r, c)
encrypto::motion::communication::TcpSetupHelper
Definition: tcp_transport.h:69
encrypto::motion::BitMatrix::BitMatrix
BitMatrix(std::vector< AlignedBitVector > &&vectors)
Construct a BitMatrix by moving a vector of AlignedBitVectors.
Definition: bit_matrix.h:60
kAmt
@ kAmt
Definition: benchmark_providers.h:32
encrypto::motion::BitMatrix::AppendColumn
void AppendColumn(const AlignedBitVector &bit_vector)
Appends a new column by copying the input bit_vector to the matrix.
Definition: bit_matrix.cpp:234
geninput.choices
choices
Definition: geninput.py:153
communication_layer.h
Provider
Provider
Definition: benchmark_providers.h:31
kPartyArgumentRegex
const std::regex kPartyArgumentRegex("(\\d+),(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}),(\\d{1,5})")
encrypto::motion::BitMatrix::operator!=
bool operator!=(const BitMatrix &other) const
Compare with another BitMatrix for inequality.
Definition: bit_matrix.h:206
encrypto::motion::primitives
Definition: motion_base_provider.h:35
encrypto::motion::BitMatrix::GetNumRows
auto GetNumRows() const noexcept
Get number of rows in the BitMatrix.
Definition: bit_matrix.h:209
encrypto::motion::BitMatrix::Transpose128RowsInplace
static void Transpose128RowsInplace(std::array< std::byte *, 128 > &matrix, std::size_t number_of_columns)
Transposes a matrix of 128 rows and arbitrary column size inplace.
Definition: bit_matrix.cpp:252
encrypto::motion::BitMatrix::BitMatrix
BitMatrix(const BitMatrix &other)
Definition: bit_matrix.h:85
encrypto::motion::communication::TcpPartiesConfiguration
std::vector< TcpConnectionConfiguration > TcpPartiesConfiguration
Definition: tcp_transport.h:62
Combination
Definition: benchmark_main.cpp:52
encrypto::motion::BitMatrix::AppendRow
void AppendRow(const AlignedBitVector &bit_vector)
Appends a new row by copying the input bit_vector to the matrix.
Definition: bit_matrix.cpp:220
main
int main(int ac, char *av[])
Definition: benchmark_providers_main.cpp:50
CheckPartyArgumentSyntax
bool CheckPartyArgumentSyntax(const std::string &party_arguments)
Definition: benchmark_providers_main.cpp:119
encrypto::motion::BitMatrix::AppendColumn
void AppendColumn(AlignedBitVector &&bit_vector)
Appends a new column by moving the input bit_vector to the matrix.
Definition: bit_matrix.h:147
encrypto::motion
Definition: algorithm_description.cpp:35
INP
#define INP(r, c)
encrypto::motion::BitVector::GetSize
auto GetSize() const noexcept
Get size of BitVector.
Definition: bit_vector.h:149
Combination::Combination
Combination(std::size_t bit_size, encrypto::motion::MpcProtocol protocol, encrypto::motion::PrimitiveOperationType operation_type, std::size_t number_of_simd)
Definition: benchmark_main.cpp:53
geninput.help
help
Definition: geninput.py:150
encrypto::motion::BitVector::Get
bool Get(std::size_t position) const
Get bit at given position.
Definition: bit_vector.cpp:471
encrypto::motion::primitives::Prg
Definition: pseudo_random_generator.h:41
kGOt
@ kGOt
Definition: benchmark_providers.h:34
encrypto::motion::BitMatrix::operator==
bool operator==(const BitMatrix &other) const
Compare with another BitMatrix for equality.
Definition: bit_matrix.cpp:751
encrypto::motion::BitMatrix::BitMatrix
BitMatrix(const std::vector< AlignedBitVector > &vectors)
Construct a BitMatrix by copying a vector of AlignedBitVectors.
Definition: bit_matrix.h:73
ParseProgramOptions
std::tuple< program_options::variables_map, bool, bool > ParseProgramOptions(int ac, char *av[])
Definition: benchmark_providers_main.cpp:135
encrypto::motion::PrintStatistics
std::string PrintStatistics(const std::string &experiment_name, const AccumulatedRunTimeStatistics &execution_statistics, const AccumulatedCommunicationStatistics &communication_statistics)
Definition: analysis.cpp:176
encrypto::motion::primitives::Prg::Encrypt
std::vector< std::byte > Encrypt(const std::size_t bytes)
Definition: pseudo_random_generator.cpp:46
typedefs.h
encrypto::motion::BitMatrix::GetRow
const AlignedBitVector & GetRow(std::size_t i) const
Returns a const AlignedBitVector reference to row i of the matrix.
Definition: bit_matrix.h:107
bit_matrix.h
kSp
@ kSp
Definition: benchmark_providers.h:39
encrypto::motion::BitMatrix::Transpose128Rows
void Transpose128Rows()
Transposes a matrix with exactly 128 rows (faster).
Definition: bit_matrix.cpp:147
encrypto::motion::primitives::Prg::Mmo
void Mmo(std::byte *input)
Definition: pseudo_random_generator.cpp:148
encrypto::motion::BitMatrix::operator=
void operator=(const BitMatrix &other)
Definition: bit_matrix.h:95
encrypto::motion::BitMatrix::operator=
void operator=(BitMatrix &&other)
Definition: bit_matrix.h:100
encrypto::motion::swap
void swap(ReusablePromise< R, MutexType, ConditionVariableType > &lhs, ReusablePromise< R, MutexType, ConditionVariableType > &rhs) noexcept
Definition: reusable_future.h:270
BenchmarkProvider
encrypto::motion::RunTimeStatistics BenchmarkProvider(encrypto::motion::PartyPointer &party, std::size_t batch_size, Provider provider, std::size_t bit_size)
Definition: benchmark_providers.cpp:37
tcp_transport.h
encrypto::motion::BitMatrix::SenderTransposeAndEncrypt
static void SenderTransposeAndEncrypt(const std::array< const std::byte *, 128 > &matrix, std::vector< BitVector<>> &y0, std::vector< BitVector<>> &y1, const BitVector<> choices, primitives::Prg &prg_fixed_key, const std::size_t number_of_columns, const std::vector< std::size_t > &bitlengths)
Transposes a matrix of 128 rows and arbitrary column size and encrypts it for the sender role.
Definition: bit_matrix.cpp:511
encrypto::motion::BitMatrix::BitMatrix
BitMatrix(BitMatrix &&other)
Definition: bit_matrix.h:90
encrypto::motion::BitMatrix::Set
void Set(std::size_t row_i, std::size_t column_i, bool value)
Set or unset bit at input position.
Definition: bit_matrix.h:125
encrypto::motion::BitMatrix::GetMutableRow
AlignedBitVector & GetMutableRow(std::size_t i)
Returns a mutable AlignedBitVector reference to row i of the matrix.
Definition: bit_matrix.h:114
encrypto::motion::AccumulatedCommunicationStatistics
Definition: analysis.h:65
encrypto::motion::BitMatrix::Transpose
void Transpose()
Transposes the matrix inplace.
Definition: bit_matrix.cpp:37
encrypto::motion::BitMatrix::Get
bool Get(std::size_t row_i, std::size_t column_i) const
Get Bit at input position.
Definition: bit_matrix.h:119
encrypto::motion::AccumulatedRunTimeStatistics::Add
void Add(const RunTimeStatistics &statistics)
Definition: analysis.cpp:43
encrypto::motion::BitMatrix::BitMatrix
BitMatrix()=default
kXcOt
@ kXcOt
Definition: benchmark_providers.h:35
encrypto::motion::communication::TcpSetupHelper::SetupConnections
std::vector< std::unique_ptr< Transport > > SetupConnections()
Definition: tcp_transport.cpp:203