MOTION
0.01
Framework for mixed-protocol multi-party computation
|
Go to the documentation of this file.
34 #include <fmt/format.h>
35 #include <boost/align/aligned_allocator.hpp>
44 std::byte(0b10000000), std::byte(0b01000000), std::byte(0b00100000), std::byte(0b00010000),
45 std::byte(0b00001000), std::byte(0b00000100), std::byte(0b00000010), std::byte(0b00000001)};
49 std::byte(0b01111111), std::byte(0b10111111), std::byte(0b11011111), std::byte(0b11101111),
50 std::byte(0b11110111), std::byte(0b11111011), std::byte(0b11111101), std::byte(0b11111110)};
54 std::byte(0b10000000), std::byte(0b11000000), std::byte(0b11100000), std::byte(0b111110000),
55 std::byte(0b11111000), std::byte(0b11111100), std::byte(0b11111110), std::byte(0b11111111)};
63 template <
typename Allocator = std::allocator<std::
byte>>
65 template <
typename OtherAllocator>
75 : data_vector_(std::move(bit_vector.data_vector_)), bit_size_(bit_vector.bit_size_) {}
81 : data_vector_(bit_vector.data_vector_), bit_size_(bit_vector.bit_size_) {}
92 template <
typename OtherAllocator>
94 : data_vector_(bit_vector.data_vector_.cbegin(), bit_vector.data_vector_.cend()),
95 bit_size_(bit_vector.bit_size_) {}
100 template <
typename OtherAllocator>
113 explicit BitVector(
const std::vector<bool>& data, std::size_t number_of_bits);
118 explicit BitVector(std::size_t number_of_bits,
bool value =
false) noexcept;
123 BitVector(const
unsigned char* buffer, std::
size_t bits)
124 :
BitVector(reinterpret_cast<const std::
byte*>(buffer), bits) {}
129 BitVector(
const std::byte* buffer, std::size_t bits);
135 template <
typename OtherAllocator>
136 explicit BitVector(
const std::vector<std::byte, OtherAllocator>& data,
137 std::size_t number_of_bits);
143 explicit BitVector(std::vector<std::byte, Allocator>&& data, std::size_t number_of_bits);
146 bool Empty()
const {
return bit_size_ == 0; }
149 auto GetSize() const noexcept {
return bit_size_; }
152 const auto&
GetData() const noexcept {
return data_vector_; }
167 void Set(
bool value) noexcept;
172 void Set(
bool value, std::size_t position);
176 bool Get(std::size_t position)
const;
181 void Resize(std::size_t number_of_bits,
bool zero_fill =
false) noexcept;
189 void Append(
bool bit) noexcept;
214 void Append(
const std::byte* pointer,
const std::size_t append_bit_size) noexcept;
219 void Copy(
const std::size_t dest_from,
const std::size_t dest_to,
const BitVector& other);
223 void Copy(
const std::size_t dest_from,
const BitVector& other);
230 std::string
AsString() const noexcept;
233 void Clear() noexcept;
240 bool operator[](std::
size_t position)
const {
return Get(position); }
247 template <
typename OtherAllocator>
258 template <
typename OtherAllocator>
269 template <
typename OtherAllocator>
280 template <
typename OtherAllocator>
291 template <
typename OtherAllocator>
302 template <
typename OtherAllocator>
313 template <
typename OtherAllocator>
324 template <
typename OtherAllocator>
369 const std::vector<std::vector<
BitVector>>& bit_vectors);
389 const std::vector<std::vector<
BitVector>>& bit_vectors);
396 static constexpr
bool IsAligned() noexcept {
return std::is_same_v<Allocator, AlignedAllocator>; }
399 std::vector<std::byte, Allocator> data_vector_;
401 std::size_t bit_size_;
403 void TruncateToFit() noexcept;
405 void BoundsCheckEquality([[maybe_unused]] const std::
size_t bit_size) const;
407 void BoundsCheckInRange([[maybe_unused]] const std::
size_t bit_size) const;
411 template <typename Allocator>
412 std::ostream& operator<<(std::ostream& os, const
BitVector<Allocator>& bit_vector) {
413 return os << bit_vector.AsString();
433 template <
typename T,
434 typename = std::enable_if_t<std::is_floating_point_v<T> || std::is_unsigned_v<T>>,
435 typename Allocator = std::allocator<std::byte>>
436 std::vector<BitVector<Allocator>>
ToInput(T value);
452 template <
typename T,
453 typename = std::enable_if_t<std::is_floating_point_v<T> || std::is_unsigned_v<T>>,
454 typename Allocator = std::allocator<std::byte>>
455 std::vector<BitVector<Allocator>>
ToInput(
const std::vector<T>& vector);
472 template <
typename UnsignedIntegralType,
473 typename = std::enable_if_t<std::is_unsigned_v<UnsignedIntegralType>>,
474 typename Allocator = std::allocator<std::byte>>
476 static_assert(std::is_integral<UnsignedIntegralType>::value);
477 static_assert(
sizeof(UnsignedIntegralType) <= 8);
478 if constexpr (
sizeof(UnsignedIntegralType) == 1) {
479 static_assert(std::is_same_v<UnsignedIntegralType, std::uint8_t>);
480 }
else if constexpr (
sizeof(UnsignedIntegralType) == 2) {
481 static_assert(std::is_same_v<UnsignedIntegralType, std::uint16_t>);
482 }
else if constexpr (
sizeof(UnsignedIntegralType) == 4) {
483 static_assert(std::is_same_v<UnsignedIntegralType, std::uint32_t>);
484 }
else if constexpr (
sizeof(UnsignedIntegralType) == 8) {
485 static_assert(std::is_same_v<UnsignedIntegralType, std::uint64_t>);
489 constexpr
auto kBitLength{
sizeof(UnsignedIntegralType) * 8};
491 assert(!bit_vectors.empty());
492 if (kBitLength != bit_vectors.size()) {
493 throw std::runtime_error(
494 fmt::format(
"Trying to convert to different bitlength: is {}, expected {}",
495 bit_vectors.size(), kBitLength));
498 const auto number_of_simd{bit_vectors.at(0).GetSize()};
499 assert(number_of_simd > 0u);
500 for ([[maybe_unused]]
auto i = 0ull; i < bit_vectors.size(); ++i)
501 assert(bit_vectors.at(i).GetSize() == number_of_simd);
503 UnsignedIntegralType output_value{0};
505 for (
auto i = 0ull; i < kBitLength; ++i) {
506 assert(bit_vectors.at(i).GetSize() == 1);
507 output_value +=
static_cast<UnsignedIntegralType
>(bit_vectors.at(i)[0]) << i;
526 template <
typename UnsignedIntegralType,
527 typename = std::enable_if_t<std::is_unsigned_v<UnsignedIntegralType>>,
528 typename Allocator = std::allocator<std::byte>>
530 static_assert(std::is_integral<UnsignedIntegralType>::value);
531 static_assert(
sizeof(UnsignedIntegralType) <= 8);
532 if constexpr (
sizeof(UnsignedIntegralType) == 1) {
533 static_assert(std::is_same_v<UnsignedIntegralType, std::uint8_t>);
534 }
else if constexpr (
sizeof(UnsignedIntegralType) == 2) {
535 static_assert(std::is_same_v<UnsignedIntegralType, std::uint16_t>);
536 }
else if constexpr (
sizeof(UnsignedIntegralType) == 4) {
537 static_assert(std::is_same_v<UnsignedIntegralType, std::uint32_t>);
538 }
else if constexpr (
sizeof(UnsignedIntegralType) == 8) {
539 static_assert(std::is_same_v<UnsignedIntegralType, std::uint64_t>);
542 constexpr
auto kBitLength{
sizeof(UnsignedIntegralType) * 8};
544 assert(!bit_vectors.empty());
545 if (kBitLength != bit_vectors.size()) {
546 throw std::runtime_error(
547 fmt::format(
"Trying to convert to different bitlength: is {}, expected {}",
548 bit_vectors.size(), kBitLength));
551 const auto number_of_simd{bit_vectors.at(0).GetSize()};
552 assert(number_of_simd > 0u);
553 for ([[maybe_unused]]
auto i = 0ull; i < bit_vectors.size(); ++i)
554 assert(bit_vectors.at(i).GetSize() == number_of_simd);
561 std::vector<UnsignedIntegralType> output_vector;
562 for (
auto i = 0ull; i < number_of_simd; ++i) {
563 UnsignedIntegralType value{0};
564 for (
auto j = 0ull; j < kBitLength; ++j) {
565 value +=
static_cast<UnsignedIntegralType
>(bit_vectors.at(j)[i]) << j;
567 output_vector.emplace_back(value);
569 return output_vector;
596 template <
typename BitVectorType>
599 bit_size_(bit_vector.
GetSize()),
604 template <
typename BitVectorType>
607 bit_size_ = bit_vector.GetSize();
608 aligned_ = bit_vector.IsAligned();
616 BitSpan(std::byte* buffer, std::size_t bit_size,
bool aligned =
false);
622 template <
typename T>
623 BitSpan(T* buffer, std::size_t bit_size,
bool aligned =
false)
624 : pointer_(reinterpret_cast<std::byte*>(buffer)), bit_size_(bit_size), aligned_(aligned) {}
629 template <
typename BitVectorType = AlignedBitVector>
630 BitVectorType
As()
const {
631 return BitVectorType(pointer_, bit_size_);
636 template <
typename BitVectorType = AlignedBitVector>
637 BitVectorType
Subset(
const std::size_t from,
const std::size_t to)
const;
640 bool Empty() const noexcept {
return bit_size_; }
646 template <
typename BitVectorType = AlignedBitVector>
648 BitVectorType result(pointer_, bit_size_);
656 template <
typename BitVectorType = AlignedBitVector>
657 bool operator==(
const BitVectorType& other)
const;
665 template <
typename BitVectorType = AlignedBitVector>
666 BitVectorType
operator&(
const BitVectorType& other)
const;
670 template <
typename BitVectorType = AlignedBitVector>
675 template <
typename BitVectorType = AlignedBitVector>
676 BitVectorType
operator|(
const BitVectorType& other)
const;
680 template <
typename BitVectorType = AlignedBitVector>
685 template <
typename BitVectorType = AlignedBitVector>
686 BitVectorType
operator^(
const BitVectorType& other)
const;
690 template <
typename BitVectorType = AlignedBitVector>
695 template <
typename BitVectorType = AlignedBitVector>
704 template <
typename BitVectorType = AlignedBitVector>
713 template <
typename BitVectorType = AlignedBitVector>
722 bool Get(
const std::size_t position)
const;
730 void Set(
const bool value);
735 void Set(
const bool value,
const std::size_t position);
738 const std::byte*
GetData() const noexcept {
return pointer_; }
744 std::size_t
GetSize() const noexcept {
return bit_size_; }
747 std::string
AsString() const noexcept;
755 template <
typename BitVectorType>
756 void Copy(
const std::size_t dest_from,
const std::size_t dest_to, BitVectorType& other);
760 template <
typename BitVectorType>
761 void Copy(
const std::size_t dest_from, BitVectorType& other);
766 void Copy(
const std::size_t dest_from,
const std::size_t dest_to,
BitSpan& other);
771 void Copy(
const std::size_t dest_from,
const std::size_t dest_to,
BitSpan&& other);
775 void Copy(
const std::size_t dest_from,
BitSpan& other);
779 void Copy(
const std::size_t dest_from,
BitSpan&& other);
783 std::size_t bit_size_;
bool operator==(const BitVector< OtherAllocator > &other) const noexcept
Compares two BitVectors for equality.
Definition: bit_vector.cpp:413
void OrImplementation(const T *input, U *result, const std::size_t byte_size)
Definition: bit_vector.cpp:131
std::string to_string(Provider p)
Definition: benchmark_providers.h:44
boost::alignment::aligned_allocator< std::byte, kAlignment > AlignedAllocator
Definition: bit_vector.h:60
auto & GetMutableData() noexcept
Get reference to content of BitVector.
Definition: bit_vector.h:155
void Assign(BitVector &&other) noexcept
Move-assign other BitVector.
Definition: bit_vector.h:163
BitVector operator~() const
Return an inverted copy of this BitVector.
Definition: bit_vector.cpp:293
BitSpan & operator&=(const BitVectorType &other)
Perform AND-assign operation on every bit of BitSpan and BitVector.
Definition: bit_vector.cpp:1421
BitVector() noexcept
Definition: bit_vector.h:71
BitSpan & operator^=(const BitVectorType &other)
Perform XOR-assign operation on every bit of BitSpan and BitVector.
Definition: bit_vector.cpp:1475
static bool OrReduceBitVector(const BitVector &bit_vector)
Performs OR operation between all bits in BitVector.
Definition: bit_vector.cpp:956
void XorImplementation(const T *input, U *result, const std::size_t byte_size)
Definition: bit_vector.cpp:96
bool Empty() const noexcept
Check if BitSpan is empty.
Definition: bit_vector.h:640
BitVector operator|(const BitVector< OtherAllocator > &other) const noexcept
Perform OR operation between every bit of two BitVectors.
const auto & GetData() const noexcept
Get const reference to content of BitVector.
Definition: bit_vector.h:152
static BitVector SecureRandom(const std::size_t size) noexcept
Returns a random BitVector.
Definition: bit_vector.cpp:893
constexpr auto NumberOfBitsToNumberOfBytes(std::size_t number_of_bits)
Definition: bit_vector.cpp:29
bool operator==(const BitVectorType &other) const
Compare the content of a BitVectorType for equality.
Definition: bit_vector.cpp:1264
static BitVector OrBitVectors(const std::vector< BitVector > &bit_vectors)
Performs OR operation between all BitVectors in bit_vectors.
Definition: bit_vector.cpp:986
BitVector & operator&=(const BitVector< OtherAllocator > &other) noexcept
Perform AND-assign operation between every bit of this and the other BitVector.
BitVector(BitVector &&bit_vector) noexcept
Definition: bit_vector.h:74
void Copy(const std::size_t dest_from, const std::size_t dest_to, BitVectorType &other)
copies the first (dest_to - dest_from) bits from other to the bits [dest_from, dest_to) in this.
Definition: bit_vector.cpp:1581
void SetImplementation(std::byte *pointer, const bool value, const std::size_t bit_size) noexcept
Definition: bit_vector.cpp:41
BitVector operator&(const BitVector< OtherAllocator > &other) const noexcept
Perform AND operation between every bit of two BitVectors.
constexpr std::size_t BitsToBytes(const std::size_t bits)
Returns the number of bytes necessary to store bits bits.
Definition: helpers.h:504
constexpr std::byte TruncationBitMask[]
Definition: bit_vector.h:53
BitVectorType operator|(const BitVectorType &other) const
Perform OR operation on every bit of BitSpan and BitVector.
Definition: bit_vector.cpp:1331
std::string AsString() const noexcept
Returns a string representation of this BitVector.
Definition: bit_vector.cpp:1572
bool Get(const std::size_t position) const
Get bit at given position.
Definition: bit_vector.cpp:1501
BitSpan(T *buffer, std::size_t bit_size, bool aligned=false)
Construct a BitSpan from a buffer of length bit_size.
Definition: bit_vector.h:623
BitVector(const std::vector< bool > &data)
Initialize from a std::vector<bool>.
Definition: bit_vector.h:106
void Set(bool value) noexcept
Sets or unsets all bits in the BitVector.
Definition: bit_vector.cpp:440
BitSpan & operator=(const BitSpan &other)
Definition: bit_vector.cpp:1249
BitSpan(BitVectorType &bit_vector)
Construct a BitSpan from a BitVector.
Definition: bit_vector.h:597
void Assign(const BitVector &other) noexcept
Copy-assign other BitVector.
Definition: bit_vector.h:159
void Reserve(std::size_t number_of_bits)
Reserves new space for BitVector, so that it can contain at least number_of_bits bits.
Definition: bit_vector.h:185
Class representing a series of bits and providing single bit access.
Definition: bit_vector.h:64
void Clear() noexcept
Clear this Bitvector.
Definition: bit_vector.cpp:864
std::ostream & operator<<(std::ostream &os, const BitSpan &bit_span)
Output string representation of BitVector to std::ostream.
Definition: bit_vector.cpp:1634
constexpr std::size_t kAlignment
Definition: config.h:42
static BitVector XorBitVectors(const std::vector< BitVector > &bit_vectors)
Performs XOR operation between all BitVectors in bit_vectors.
Definition: bit_vector.cpp:1064
void AlignedXorImplementation(const T *input, U *result, const std::size_t byte_size)
Definition: bit_vector.cpp:105
static bool IsEqualSizeDimensions(const std::vector< BitVector > &bit_vectors)
Check if all Bitvectors in bit_vectors are of equal dimension.
Definition: bit_vector.cpp:1113
const std::byte * GetData() const noexcept
Get const reference to content of BitSpan.
Definition: bit_vector.h:738
void Set(const bool value)
Sets all bits to value.
Definition: bit_vector.cpp:1505
bool GetImplementation(const std::byte *pointer, const std::size_t position) noexcept
Definition: bit_vector.cpp:69
bool AlignedEqualImplementation(const T *pointer1, const U *pointer2, const std::size_t byte_size)
Definition: bit_vector.cpp:86
constexpr std::byte kUnsetBitMask[]
Definition: bit_vector.h:48
friend class BitVector
Definition: bit_vector.h:66
void Invert()
In-place bit-wise invert.
Definition: bit_vector.cpp:1511
std::vector< UnsignedIntegralType > ToVectorOutput(std::vector< BitVector< Allocator >> bit_vectors)
Converts a vector of UnsignedIntegralType to a vector of BitVectors.
Definition: bit_vector.h:529
BitVectorType Subset(const std::size_t from, const std::size_t to) const
Returns a new BitVector containing the bits of this BitSpan between positions from and to.
Definition: bit_vector.cpp:1518
static BitVector RandomSeeded(const std::size_t size, const std::size_t seed=0) noexcept
Returns a random BitVector using an input seed. Internally uses Mersenne twister, do not use as crypt...
Definition: bit_vector.cpp:870
bool operator!=(const BitVector< OtherAllocator > &other) const noexcept
Compares two BitVectors for inequality.
Definition: bit_vector.cpp:301
BitVectorType operator&(const BitVectorType &other) const
Perform AND operation on every bit of BitSpan and BitVector.
Definition: bit_vector.cpp:1286
void TruncateToFitImplementation(std::byte *pointer, const std::size_t bit_size)
Definition: bit_vector.cpp:35
bool EqualImplementation(const T *pointer1, const U *pointer2, const std::size_t byte_size)
Definition: bit_vector.cpp:78
bool operator[](const std::size_t position) const
Get bit at given position in the BitSpan.
Definition: bit_vector.h:726
bool Empty() const
Check if BitVector is empty.
Definition: bit_vector.h:146
Definition: algorithm_description.cpp:35
static constexpr bool IsAligned() noexcept
Returns true if Allocator is aligned allocator.
Definition: bit_vector.h:396
void Invert()
In-place bit-wise invert.
Definition: bit_vector.cpp:284
void CopyImplementation(const std::size_t from, const std::size_t to, std::byte *source, std::byte *destination)
Definition: bit_vector.cpp:147
std::string AsString() const noexcept
Returns a string representation of this BitVector.
Definition: bit_vector.cpp:855
auto GetSize() const noexcept
Get size of BitVector.
Definition: bit_vector.h:149
std::size_t GetSize() const noexcept
Get size of BitSpan.
Definition: bit_vector.h:744
std::byte * GetMutableData() noexcept
Get reference to content of BitSpan.
Definition: bit_vector.h:741
bool Get(std::size_t position) const
Get bit at given position.
Definition: bit_vector.cpp:471
std::allocator< std::byte > StdAllocator
Definition: bit_vector.h:59
BitVector(const BitVector< OtherAllocator > &bit_vector) noexcept
Copy from a BitVector with different allocator.
Definition: bit_vector.h:93
BitSpan & operator=(BitVectorType &bit_vector)
Assignment from BitVector.
Definition: bit_vector.h:605
void Copy(const std::size_t dest_from, const std::size_t dest_to, const BitVector &other)
copies the first (dest_to - dest_from) bits from other to the bits [dest_from, dest_to) in this.
Definition: bit_vector.cpp:720
BitVectorType operator^(const BitVectorType &other) const
Perform XOR operation on every bit of BitSpan and BitVector.
Definition: bit_vector.cpp:1376
static BitVector AndBitVectors(const std::vector< BitVector > &bit_vectors)
Performs AND operation between all BitVectors in bit_vectors.
Definition: bit_vector.cpp:939
void AndImplementation(const T *input, U *result, const std::size_t byte_size)
Definition: bit_vector.cpp:114
BitVector & operator^=(const BitVector< OtherAllocator > &other) noexcept
Perform XOR-assign operation between every bit of this and the other BitVector.
BitVectorType operator~() const
Return a BitVector containing bit-inverted values of this BitSpan.
Definition: bit_vector.h:647
static uint8_t equal(signed char b, signed char c)
Definition: mycurve25519.cpp:3971
BitSpan & operator|=(const BitVectorType &other)
Perform OR-assign operation on every bit of BitSpan and BitVector.
Definition: bit_vector.cpp:1448
UnsignedIntegralType ToOutput(std::vector< BitVector< Allocator >> bit_vectors)
Converts a vector of BitVectors to a value of UnsignedIntegralType.
Definition: bit_vector.h:475
Non-owning non-resizeable BitVector.
Definition: bit_vector.h:578
void AlignedAndImplementation(const T *input, U *result, const std::size_t byte_size)
Definition: bit_vector.cpp:122
std::vector< BitVector< Allocator > > ToInput(T value)
Converts a value of an unsigned integer type or a floating point type to a vector of BitVector.
void Append(bool bit) noexcept
Appends a bit to BitVector.
Definition: bit_vector.cpp:621
static const fe d
Definition: mycurve25519_tables.h:30
constexpr std::byte kSetBitMask[]
Definition: bit_vector.h:43
friend class BitSpan
Definition: bit_vector.h:67
static bool AndReduceBitVector(const BitVector &bit_vector)
Performs AND operation between all bits in BitVector.
Definition: bit_vector.cpp:905
static bool XorReduceBitVector(const BitVector &bit_vector)
Performs XOR operation between all bits in BitVector.
Definition: bit_vector.cpp:1034
BitVector< Allocator > & operator=(const BitVector< Allocator > &other) noexcept
Definition: bit_vector.cpp:384
constexpr bool kDebug
Definition: config.h:36
BitVectorType As() const
Converts this BitSpan to a BitVector.
Definition: bit_vector.h:630
BitVector Subset(std::size_t from, std::size_t to) const
Returns a new BitVector containing the bits of this BitVector between positions from and to.
Definition: bit_vector.cpp:806
void AlignedOrImplementation(const T *input, U *result, const std::size_t byte_size)
Definition: bit_vector.cpp:139
void SetAtImplementation(std::byte *pointer, const bool value, const std::size_t position) noexcept
Definition: bit_vector.cpp:57
BitVector & operator|=(const BitVector< OtherAllocator > &other) noexcept
Perform OR-assign operation between every bit of this and the other BitVector.
BitVector(const BitVector &bit_vector) noexcept
Definition: bit_vector.h:80
void Resize(std::size_t number_of_bits, bool zero_fill=false) noexcept
Resize BitVector to size number_of_bits. New bits are uninitialized by default.
Definition: bit_vector.cpp:602
BitVector operator^(const BitVector< OtherAllocator > &other) const noexcept
Perform XOR operation between every bit of two BitVectors.