MOTION  0.01
Framework for mixed-protocol multi-party computation
bit_vector.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 <cstddef>
28 #include <iostream>
29 #include <memory>
30 #include <random>
31 #include <string>
32 #include <vector>
33 
34 #include <fmt/format.h>
35 #include <boost/align/aligned_allocator.hpp>
36 
37 #include "config.h"
38 #include "helpers.h"
39 
40 namespace encrypto::motion {
41 
42 // bitmask to set a specific bit in a byte
43 constexpr std::byte kSetBitMask[] = {
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)};
46 
47 // bitmask to unset a specific bit in a byte
48 constexpr std::byte kUnsetBitMask[] = {
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)};
51 
52 // bitmask to truncate the a byte
53 constexpr std::byte TruncationBitMask[] = {
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)};
56 
57 class BitSpan;
58 
59 using StdAllocator = std::allocator<std::byte>;
60 using AlignedAllocator = boost::alignment::aligned_allocator<std::byte, kAlignment>;
61 
63 template <typename Allocator = std::allocator<std::byte>>
64 class BitVector {
65  template <typename OtherAllocator>
66  friend class BitVector;
67  friend class BitSpan;
68 
69  public:
70  // Default constructor, results in an empty vector
71  BitVector() noexcept : bit_size_(0){};
72 
73  // Move constructor
74  BitVector(BitVector&& bit_vector) noexcept
75  : data_vector_(std::move(bit_vector.data_vector_)), bit_size_(bit_vector.bit_size_) {}
76 
77  // XXX Copy and Move constructor/assigment can be defaulted
78 
79  // Copy constructor
80  BitVector(const BitVector& bit_vector) noexcept
81  : data_vector_(bit_vector.data_vector_), bit_size_(bit_vector.bit_size_) {}
82 
83  // Copy assignment
84  BitVector<Allocator>& operator=(const BitVector<Allocator>& other) noexcept;
85 
86  // Move assignment
88 
92  template <typename OtherAllocator>
93  BitVector(const BitVector<OtherAllocator>& bit_vector) noexcept
94  : data_vector_(bit_vector.data_vector_.cbegin(), bit_vector.data_vector_.cend()),
95  bit_size_(bit_vector.bit_size_) {}
96 
100  template <typename OtherAllocator>
102 
106  BitVector(const std::vector<bool>& data) : BitVector(data, data.size()) {}
107 
113  explicit BitVector(const std::vector<bool>& data, std::size_t number_of_bits);
114 
118  explicit BitVector(std::size_t number_of_bits, bool value = false) noexcept;
119 
123  BitVector(const unsigned char* buffer, std::size_t bits)
124  : BitVector(reinterpret_cast<const std::byte*>(buffer), bits) {}
125 
129  BitVector(const std::byte* buffer, std::size_t bits);
130 
135  template <typename OtherAllocator>
136  explicit BitVector(const std::vector<std::byte, OtherAllocator>& data,
137  std::size_t number_of_bits);
138 
143  explicit BitVector(std::vector<std::byte, Allocator>&& data, std::size_t number_of_bits);
144 
146  bool Empty() const { return bit_size_ == 0; }
147 
149  auto GetSize() const noexcept { return bit_size_; }
150 
152  const auto& GetData() const noexcept { return data_vector_; }
153 
155  auto& GetMutableData() noexcept { return data_vector_; }
156 
159  void Assign(const BitVector& other) noexcept { *this = other; }
160 
163  void Assign(BitVector&& other) noexcept { *this = std::move(other); }
164 
167  void Set(bool value) noexcept;
168 
172  void Set(bool value, std::size_t position);
173 
176  bool Get(std::size_t position) const;
177 
181  void Resize(std::size_t number_of_bits, bool zero_fill = false) noexcept;
182 
185  void Reserve(std::size_t number_of_bits) { data_vector_.reserve(BitsToBytes(number_of_bits)); }
186 
189  void Append(bool bit) noexcept;
190 
193  void Append(const BitVector<Allocator>& other) noexcept;
194 
197  void Append(BitVector&& other) noexcept;
198 
203  void Append(const BitSpan& other);
204 
209  void Append(BitSpan&& other);
210 
214  void Append(const std::byte* pointer, const std::size_t append_bit_size) noexcept;
215 
219  void Copy(const std::size_t dest_from, const std::size_t dest_to, const BitVector& other);
220 
223  void Copy(const std::size_t dest_from, const BitVector& other);
224 
227  BitVector Subset(std::size_t from, std::size_t to) const;
228 
230  std::string AsString() const noexcept;
231 
233  void Clear() noexcept;
234 
236  void Invert();
237 
240  bool operator[](std::size_t position) const { return Get(position); }
241 
243  BitVector operator~() const;
244 
247  template <typename OtherAllocator>
248  BitVector operator&(const BitVector<OtherAllocator>& other) const noexcept;
249 
254  BitVector operator&(const BitSpan& other) const noexcept;
255 
258  template <typename OtherAllocator>
259  BitVector operator^(const BitVector<OtherAllocator>& other) const noexcept;
260 
265  BitVector operator^(const BitSpan& other) const noexcept;
266 
269  template <typename OtherAllocator>
270  BitVector operator|(const BitVector<OtherAllocator>& other) const noexcept;
271 
276  BitVector operator|(const BitSpan& other) const noexcept;
277 
280  template <typename OtherAllocator>
281  bool operator!=(const BitVector<OtherAllocator>& other) const noexcept;
282 
287  bool operator!=(const BitSpan& other) const noexcept;
288 
291  template <typename OtherAllocator>
292  bool operator==(const BitVector<OtherAllocator>& other) const noexcept;
293 
298  bool operator==(const BitSpan& other) const noexcept;
299 
302  template <typename OtherAllocator>
303  BitVector& operator&=(const BitVector<OtherAllocator>& other) noexcept;
304 
309  BitVector& operator&=(const BitSpan& other) noexcept;
310 
313  template <typename OtherAllocator>
314  BitVector& operator^=(const BitVector<OtherAllocator>& other) noexcept;
315 
320  BitVector& operator^=(const BitSpan& other) noexcept;
321 
324  template <typename OtherAllocator>
325  BitVector& operator|=(const BitVector<OtherAllocator>& other) noexcept;
326 
331  BitVector& operator|=(const BitSpan& other) noexcept;
332 
335  static BitVector SecureRandom(const std::size_t size) noexcept;
336 
341  static BitVector RandomSeeded(const std::size_t size, const std::size_t seed = 0) noexcept;
342 
345  static bool OrReduceBitVector(const BitVector& bit_vector);
346 
349  static BitVector OrBitVectors(const std::vector<BitVector>& bit_vectors);
350 
353  static bool AndReduceBitVector(const BitVector& bit_vector);
354 
357  static BitVector AndBitVectors(const std::vector<BitVector>& bit_vectors);
358 
363  static std::vector<BitVector> AndBitVectors(const std::vector<BitVector>& a,
364  const std::vector<BitVector>& b);
365 
368  static std::vector<BitVector> AndBitVectors(
369  const std::vector<std::vector<BitVector>>& bit_vectors);
370 
373  static bool XorReduceBitVector(const BitVector& bit_vector);
374 
377  static BitVector XorBitVectors(const std::vector<BitVector>& bit_vectors);
378 
383  static std::vector<BitVector> XorBitVectors(const std::vector<BitVector>& a,
384  const std::vector<BitVector>& b);
385 
388  static std::vector<BitVector> XorBitVectors(
389  const std::vector<std::vector<BitVector>>& bit_vectors);
390 
393  static bool IsEqualSizeDimensions(const std::vector<BitVector>& bit_vectors);
394 
396  static constexpr bool IsAligned() noexcept { return std::is_same_v<Allocator, AlignedAllocator>; }
397 
398  private:
399  std::vector<std::byte, Allocator> data_vector_;
400 
401  std::size_t bit_size_;
402 
403  void TruncateToFit() noexcept;
404 
405  void BoundsCheckEquality([[maybe_unused]] const std::size_t bit_size) const;
406 
407  void BoundsCheckInRange([[maybe_unused]] const std::size_t bit_size) const;
408 };
409 
411 template <typename Allocator>
412 std::ostream& operator<<(std::ostream& os, const BitVector<Allocator>& bit_vector) {
413  return os << bit_vector.AsString();
414 }
415 
416 // Input functions that convert inputs of integer and floating point types to vectors of
417 // BitVectors, which are a suitable input to MOTION.
418 
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);
437 
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);
456 
457 // Output functions for converting vectors of BitVectors to vectors of floating point or
458 // integer numbers
459 
472 template <typename UnsignedIntegralType,
473  typename = std::enable_if_t<std::is_unsigned_v<UnsignedIntegralType>>,
474  typename Allocator = std::allocator<std::byte>>
475 UnsignedIntegralType ToOutput(std::vector<BitVector<Allocator>> bit_vectors) {
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>);
486  }
487 
488  // kBitLength is always equal to bit_vectors
489  constexpr auto kBitLength{sizeof(UnsignedIntegralType) * 8};
490 
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));
496  }
497 
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);
502 
503  UnsignedIntegralType output_value{0};
504  // Converting values in a BitVector to UnsignedIntegralType
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;
508  }
509 
510  return output_value;
511 }
512 
526 template <typename UnsignedIntegralType,
527  typename = std::enable_if_t<std::is_unsigned_v<UnsignedIntegralType>>,
528  typename Allocator = std::allocator<std::byte>>
529 std::vector<UnsignedIntegralType> ToVectorOutput(std::vector<BitVector<Allocator>> bit_vectors) {
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>);
540  }
541 
542  constexpr auto kBitLength{sizeof(UnsignedIntegralType) * 8};
543 
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));
549  }
550 
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);
555 
556  // Converting values in a BitVector to a value of UnsignedIntegralType
557  // Example: If the jth BitVector of size 3 is represented by the tuple: (xj, yj, zj)
558  // Then output_vector[0] == x0*2^0 + x1*2^1 + ... + xn*2^n
559  // output_vector[1] == y0*2^0 + y1*2^1 + ... + yn*2^n
560  // output_vector[2] == z0*2^0 + z1*2^1 + ... + zn*2^n
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;
566  }
567  output_vector.emplace_back(value);
568  }
569  return output_vector;
570 }
571 
573 
578 class BitSpan {
579  public:
580  BitSpan() = default;
581 
582  ~BitSpan() = default;
583 
584  // XXX Copy and move constructor/assignment can be declared default.
585  // XXX Assignments do not check for self-assignment.
586  BitSpan(const BitSpan& other);
587 
588  BitSpan(BitSpan&& other);
589 
590  BitSpan& operator=(const BitSpan& other);
591 
592  BitSpan& operator=(BitSpan&& other);
593 
596  template <typename BitVectorType>
597  BitSpan(BitVectorType& bit_vector)
598  : pointer_(bit_vector.GetMutableData().data()),
599  bit_size_(bit_vector.GetSize()),
600  aligned_(bit_vector.IsAligned()) {}
601 
604  template <typename BitVectorType>
605  BitSpan& operator=(BitVectorType& bit_vector) {
606  pointer_ = bit_vector.GetMutableData().data();
607  bit_size_ = bit_vector.GetSize();
608  aligned_ = bit_vector.IsAligned();
609  return *this;
610  }
611 
616  BitSpan(std::byte* buffer, std::size_t bit_size, bool aligned = false);
617 
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) {}
625 
626  // TODO make this a user-defined conversion.
629  template <typename BitVectorType = AlignedBitVector>
630  BitVectorType As() const {
631  return BitVectorType(pointer_, bit_size_);
632  }
633 
636  template <typename BitVectorType = AlignedBitVector>
637  BitVectorType Subset(const std::size_t from, const std::size_t to) const;
638 
640  bool Empty() const noexcept { return bit_size_; }
641 
643  void Invert();
644 
646  template <typename BitVectorType = AlignedBitVector>
647  BitVectorType operator~() const {
648  BitVectorType result(pointer_, bit_size_);
649  result.Invert();
650  return result;
651  }
652 
656  template <typename BitVectorType = AlignedBitVector>
657  bool operator==(const BitVectorType& other) const;
658 
661  bool operator==(const BitSpan& other) const;
662 
665  template <typename BitVectorType = AlignedBitVector>
666  BitVectorType operator&(const BitVectorType& other) const;
667 
670  template <typename BitVectorType = AlignedBitVector>
671  BitVectorType operator&(const BitSpan& other) const;
672 
675  template <typename BitVectorType = AlignedBitVector>
676  BitVectorType operator|(const BitVectorType& other) const;
677 
680  template <typename BitVectorType = AlignedBitVector>
681  BitVectorType operator|(const BitSpan& other) const;
682 
685  template <typename BitVectorType = AlignedBitVector>
686  BitVectorType operator^(const BitVectorType& other) const;
687 
690  template <typename BitVectorType = AlignedBitVector>
691  BitVectorType operator^(const BitSpan& other) const;
692 
695  template <typename BitVectorType = AlignedBitVector>
696  BitSpan& operator&=(const BitVectorType& other);
697 
700  BitSpan& operator&=(const BitSpan& other);
701 
704  template <typename BitVectorType = AlignedBitVector>
705  BitSpan& operator|=(const BitVectorType& other);
706 
709  BitSpan& operator|=(const BitSpan& other);
710 
713  template <typename BitVectorType = AlignedBitVector>
714  BitSpan& operator^=(const BitVectorType& other);
715 
718  BitSpan& operator^=(const BitSpan& other);
719 
722  bool Get(const std::size_t position) const;
723 
726  bool operator[](const std::size_t position) const { return Get(position); }
727 
730  void Set(const bool value);
731 
735  void Set(const bool value, const std::size_t position);
736 
738  const std::byte* GetData() const noexcept { return pointer_; }
739 
741  std::byte* GetMutableData() noexcept { return pointer_; }
742 
744  std::size_t GetSize() const noexcept { return bit_size_; }
745 
747  std::string AsString() const noexcept;
748 
750  bool IsAligned() const noexcept { return aligned_; }
751 
755  template <typename BitVectorType>
756  void Copy(const std::size_t dest_from, const std::size_t dest_to, BitVectorType& other);
757 
760  template <typename BitVectorType>
761  void Copy(const std::size_t dest_from, BitVectorType& other);
762 
766  void Copy(const std::size_t dest_from, const std::size_t dest_to, BitSpan& other);
767 
771  void Copy(const std::size_t dest_from, const std::size_t dest_to, BitSpan&& other);
772 
775  void Copy(const std::size_t dest_from, BitSpan& other);
776 
779  void Copy(const std::size_t dest_from, BitSpan&& other);
780 
781  private:
782  std::byte* pointer_;
783  std::size_t bit_size_;
784  bool aligned_;
785 };
786 
788 std::ostream& operator<<(std::ostream& os, const BitSpan& bit_span);
789 
790 } // namespace encrypto::motion
encrypto::motion::BitVector::operator==
bool operator==(const BitVector< OtherAllocator > &other) const noexcept
Compares two BitVectors for equality.
Definition: bit_vector.cpp:413
encrypto::motion::OrImplementation
void OrImplementation(const T *input, U *result, const std::size_t byte_size)
Definition: bit_vector.cpp:131
to_string
std::string to_string(Provider p)
Definition: benchmark_providers.h:44
encrypto::motion::AlignedAllocator
boost::alignment::aligned_allocator< std::byte, kAlignment > AlignedAllocator
Definition: bit_vector.h:60
helpers.h
encrypto::motion::BitVector::GetMutableData
auto & GetMutableData() noexcept
Get reference to content of BitVector.
Definition: bit_vector.h:155
encrypto::motion::BitVector::Assign
void Assign(BitVector &&other) noexcept
Move-assign other BitVector.
Definition: bit_vector.h:163
encrypto::motion::BitVector::operator~
BitVector operator~() const
Return an inverted copy of this BitVector.
Definition: bit_vector.cpp:293
encrypto::motion::BitSpan::operator&=
BitSpan & operator&=(const BitVectorType &other)
Perform AND-assign operation on every bit of BitSpan and BitVector.
Definition: bit_vector.cpp:1421
encrypto::motion::BitVector::BitVector
BitVector() noexcept
Definition: bit_vector.h:71
encrypto::motion::BitSpan::operator^=
BitSpan & operator^=(const BitVectorType &other)
Perform XOR-assign operation on every bit of BitSpan and BitVector.
Definition: bit_vector.cpp:1475
encrypto::motion::BitVector::OrReduceBitVector
static bool OrReduceBitVector(const BitVector &bit_vector)
Performs OR operation between all bits in BitVector.
Definition: bit_vector.cpp:956
encrypto::motion::XorImplementation
void XorImplementation(const T *input, U *result, const std::size_t byte_size)
Definition: bit_vector.cpp:96
encrypto::motion::BitSpan::Empty
bool Empty() const noexcept
Check if BitSpan is empty.
Definition: bit_vector.h:640
encrypto::motion::BitVector::operator|
BitVector operator|(const BitVector< OtherAllocator > &other) const noexcept
Perform OR operation between every bit of two BitVectors.
encrypto::motion::BitVector::GetData
const auto & GetData() const noexcept
Get const reference to content of BitVector.
Definition: bit_vector.h:152
encrypto::motion::BitVector::SecureRandom
static BitVector SecureRandom(const std::size_t size) noexcept
Returns a random BitVector.
Definition: bit_vector.cpp:893
encrypto::motion::NumberOfBitsToNumberOfBytes
constexpr auto NumberOfBitsToNumberOfBytes(std::size_t number_of_bits)
Definition: bit_vector.cpp:29
encrypto::motion::BitSpan::operator==
bool operator==(const BitVectorType &other) const
Compare the content of a BitVectorType for equality.
Definition: bit_vector.cpp:1264
encrypto::motion::BitVector::OrBitVectors
static BitVector OrBitVectors(const std::vector< BitVector > &bit_vectors)
Performs OR operation between all BitVectors in bit_vectors.
Definition: bit_vector.cpp:986
encrypto::motion::BitVector::operator&=
BitVector & operator&=(const BitVector< OtherAllocator > &other) noexcept
Perform AND-assign operation between every bit of this and the other BitVector.
encrypto::motion::BitVector::BitVector
BitVector(BitVector &&bit_vector) noexcept
Definition: bit_vector.h:74
encrypto::motion::BitSpan::Copy
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
config.h
encrypto::motion::SetImplementation
void SetImplementation(std::byte *pointer, const bool value, const std::size_t bit_size) noexcept
Definition: bit_vector.cpp:41
encrypto::motion::BitVector::operator&
BitVector operator&(const BitVector< OtherAllocator > &other) const noexcept
Perform AND operation between every bit of two BitVectors.
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::TruncationBitMask
constexpr std::byte TruncationBitMask[]
Definition: bit_vector.h:53
encrypto::motion::BitSpan::operator|
BitVectorType operator|(const BitVectorType &other) const
Perform OR operation on every bit of BitSpan and BitVector.
Definition: bit_vector.cpp:1331
bit_vector.h
encrypto::motion::BitSpan::AsString
std::string AsString() const noexcept
Returns a string representation of this BitVector.
Definition: bit_vector.cpp:1572
encrypto::motion::BitSpan::Get
bool Get(const std::size_t position) const
Get bit at given position.
Definition: bit_vector.cpp:1501
encrypto::motion::BitSpan::BitSpan
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
encrypto::motion::BitVector::BitVector
BitVector(const std::vector< bool > &data)
Initialize from a std::vector<bool>.
Definition: bit_vector.h:106
encrypto::motion::BitVector::Set
void Set(bool value) noexcept
Sets or unsets all bits in the BitVector.
Definition: bit_vector.cpp:440
encrypto::motion::BitSpan::operator=
BitSpan & operator=(const BitSpan &other)
Definition: bit_vector.cpp:1249
encrypto::motion::BitSpan::BitSpan
BitSpan(BitVectorType &bit_vector)
Construct a BitSpan from a BitVector.
Definition: bit_vector.h:597
encrypto::motion::BitVector::Assign
void Assign(const BitVector &other) noexcept
Copy-assign other BitVector.
Definition: bit_vector.h:159
encrypto::motion::BitVector::Reserve
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
encrypto::motion::BitVector
Class representing a series of bits and providing single bit access.
Definition: bit_vector.h:64
encrypto::motion::BitVector::Clear
void Clear() noexcept
Clear this Bitvector.
Definition: bit_vector.cpp:864
encrypto::motion::operator<<
std::ostream & operator<<(std::ostream &os, const BitSpan &bit_span)
Output string representation of BitVector to std::ostream.
Definition: bit_vector.cpp:1634
encrypto::motion::kAlignment
constexpr std::size_t kAlignment
Definition: config.h:42
encrypto::motion::BitVector::XorBitVectors
static BitVector XorBitVectors(const std::vector< BitVector > &bit_vectors)
Performs XOR operation between all BitVectors in bit_vectors.
Definition: bit_vector.cpp:1064
encrypto::motion::AlignedXorImplementation
void AlignedXorImplementation(const T *input, U *result, const std::size_t byte_size)
Definition: bit_vector.cpp:105
encrypto::motion::BitVector::IsEqualSizeDimensions
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
encrypto::motion::BitSpan::GetData
const std::byte * GetData() const noexcept
Get const reference to content of BitSpan.
Definition: bit_vector.h:738
encrypto::motion::BitSpan::Set
void Set(const bool value)
Sets all bits to value.
Definition: bit_vector.cpp:1505
encrypto::motion::GetImplementation
bool GetImplementation(const std::byte *pointer, const std::size_t position) noexcept
Definition: bit_vector.cpp:69
encrypto::motion::AlignedEqualImplementation
bool AlignedEqualImplementation(const T *pointer1, const U *pointer2, const std::size_t byte_size)
Definition: bit_vector.cpp:86
encrypto::motion::kUnsetBitMask
constexpr std::byte kUnsetBitMask[]
Definition: bit_vector.h:48
encrypto::motion::BitVector::BitVector
friend class BitVector
Definition: bit_vector.h:66
encrypto::motion::BitSpan::Invert
void Invert()
In-place bit-wise invert.
Definition: bit_vector.cpp:1511
encrypto::motion::BitVector::ToVectorOutput
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
encrypto::motion::BitSpan::Subset
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
encrypto::motion::BitVector::RandomSeeded
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
encrypto::motion::BitVector::operator!=
bool operator!=(const BitVector< OtherAllocator > &other) const noexcept
Compares two BitVectors for inequality.
Definition: bit_vector.cpp:301
encrypto::motion::BitSpan::operator&
BitVectorType operator&(const BitVectorType &other) const
Perform AND operation on every bit of BitSpan and BitVector.
Definition: bit_vector.cpp:1286
encrypto::motion::TruncateToFitImplementation
void TruncateToFitImplementation(std::byte *pointer, const std::size_t bit_size)
Definition: bit_vector.cpp:35
encrypto::motion::EqualImplementation
bool EqualImplementation(const T *pointer1, const U *pointer2, const std::size_t byte_size)
Definition: bit_vector.cpp:78
encrypto::motion::BitSpan::operator[]
bool operator[](const std::size_t position) const
Get bit at given position in the BitSpan.
Definition: bit_vector.h:726
encrypto::motion::BitVector::Empty
bool Empty() const
Check if BitVector is empty.
Definition: bit_vector.h:146
encrypto::motion
Definition: algorithm_description.cpp:35
encrypto::motion::BitVector::IsAligned
static constexpr bool IsAligned() noexcept
Returns true if Allocator is aligned allocator.
Definition: bit_vector.h:396
encrypto::motion::BitVector::Invert
void Invert()
In-place bit-wise invert.
Definition: bit_vector.cpp:284
encrypto::motion::CopyImplementation
void CopyImplementation(const std::size_t from, const std::size_t to, std::byte *source, std::byte *destination)
Definition: bit_vector.cpp:147
encrypto::motion::BitVector::AsString
std::string AsString() const noexcept
Returns a string representation of this BitVector.
Definition: bit_vector.cpp:855
encrypto::motion::BitVector::GetSize
auto GetSize() const noexcept
Get size of BitVector.
Definition: bit_vector.h:149
encrypto::motion::BitSpan::GetSize
std::size_t GetSize() const noexcept
Get size of BitSpan.
Definition: bit_vector.h:744
encrypto::motion::BitSpan::GetMutableData
std::byte * GetMutableData() noexcept
Get reference to content of BitSpan.
Definition: bit_vector.h:741
encrypto::motion::BitVector::Get
bool Get(std::size_t position) const
Get bit at given position.
Definition: bit_vector.cpp:471
encrypto::motion::StdAllocator
std::allocator< std::byte > StdAllocator
Definition: bit_vector.h:59
encrypto::motion::BitVector::BitVector
BitVector(const BitVector< OtherAllocator > &bit_vector) noexcept
Copy from a BitVector with different allocator.
Definition: bit_vector.h:93
encrypto::motion::BitSpan::operator=
BitSpan & operator=(BitVectorType &bit_vector)
Assignment from BitVector.
Definition: bit_vector.h:605
encrypto::motion::BitVector::Copy
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
encrypto::motion::BitSpan::operator^
BitVectorType operator^(const BitVectorType &other) const
Perform XOR operation on every bit of BitSpan and BitVector.
Definition: bit_vector.cpp:1376
encrypto::motion::BitVector::AndBitVectors
static BitVector AndBitVectors(const std::vector< BitVector > &bit_vectors)
Performs AND operation between all BitVectors in bit_vectors.
Definition: bit_vector.cpp:939
encrypto::motion::AndImplementation
void AndImplementation(const T *input, U *result, const std::size_t byte_size)
Definition: bit_vector.cpp:114
encrypto::motion::BitVector::operator^=
BitVector & operator^=(const BitVector< OtherAllocator > &other) noexcept
Perform XOR-assign operation between every bit of this and the other BitVector.
encrypto::motion::BitSpan::operator~
BitVectorType operator~() const
Return a BitVector containing bit-inverted values of this BitSpan.
Definition: bit_vector.h:647
encrypto::motion::curve25519::equal
static uint8_t equal(signed char b, signed char c)
Definition: mycurve25519.cpp:3971
encrypto::motion::BitSpan::operator|=
BitSpan & operator|=(const BitVectorType &other)
Perform OR-assign operation on every bit of BitSpan and BitVector.
Definition: bit_vector.cpp:1448
encrypto::motion::ToOutput
UnsignedIntegralType ToOutput(std::vector< BitVector< Allocator >> bit_vectors)
Converts a vector of BitVectors to a value of UnsignedIntegralType.
Definition: bit_vector.h:475
encrypto::motion::BitSpan
Non-owning non-resizeable BitVector.
Definition: bit_vector.h:578
encrypto::motion::AlignedAndImplementation
void AlignedAndImplementation(const T *input, U *result, const std::size_t byte_size)
Definition: bit_vector.cpp:122
encrypto::motion::BitVector::ToInput
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.
encrypto::motion::BitVector::Append
void Append(bool bit) noexcept
Appends a bit to BitVector.
Definition: bit_vector.cpp:621
d
static const fe d
Definition: mycurve25519_tables.h:30
encrypto::motion::kSetBitMask
constexpr std::byte kSetBitMask[]
Definition: bit_vector.h:43
encrypto::motion::BitVector::BitSpan
friend class BitSpan
Definition: bit_vector.h:67
encrypto::motion::BitVector::AndReduceBitVector
static bool AndReduceBitVector(const BitVector &bit_vector)
Performs AND operation between all bits in BitVector.
Definition: bit_vector.cpp:905
encrypto::motion::BitVector::XorReduceBitVector
static bool XorReduceBitVector(const BitVector &bit_vector)
Performs XOR operation between all bits in BitVector.
Definition: bit_vector.cpp:1034
encrypto::motion::BitVector::operator=
BitVector< Allocator > & operator=(const BitVector< Allocator > &other) noexcept
Definition: bit_vector.cpp:384
encrypto::motion::kDebug
constexpr bool kDebug
Definition: config.h:36
encrypto::motion::BitSpan::As
BitVectorType As() const
Converts this BitSpan to a BitVector.
Definition: bit_vector.h:630
encrypto::motion::BitVector::Subset
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
encrypto::motion::AlignedOrImplementation
void AlignedOrImplementation(const T *input, U *result, const std::size_t byte_size)
Definition: bit_vector.cpp:139
encrypto::motion::SetAtImplementation
void SetAtImplementation(std::byte *pointer, const bool value, const std::size_t position) noexcept
Definition: bit_vector.cpp:57
encrypto::motion::BitVector::operator|=
BitVector & operator|=(const BitVector< OtherAllocator > &other) noexcept
Perform OR-assign operation between every bit of this and the other BitVector.
encrypto::motion::BitVector::BitVector
BitVector(const BitVector &bit_vector) noexcept
Definition: bit_vector.h:80
encrypto::motion::BitSpan::BitSpan
BitSpan()=default
encrypto::motion::BitVector::Resize
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
encrypto::motion::BitVector::operator^
BitVector operator^(const BitVector< OtherAllocator > &other) const noexcept
Perform XOR operation between every bit of two BitVectors.