Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
bits.hpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2022 Clearmatics Technologies Ltd
2 //
3 // SPDX-License-Identifier: LGPL-3.0+
4 
5 #ifndef __ZETH_CORE_BITS_HPP__
6 #define __ZETH_CORE_BITS_HPP__
7 
9 
10 #include <array>
11 #include <iostream>
12 #include <stddef.h>
13 #include <vector>
14 
15 namespace libzeth
16 {
17 
18 // Forward declarations
19 template<size_t numBits> class bits;
20 
22 template<size_t numBits>
24 
26 template<size_t numBits>
28  const bits<numBits> &a, const bits<numBits> &b, bool with_carry = false);
29 
31 template<size_t numBits> class bits : public std::array<bool, numBits>
32 {
33 public:
34  bits();
35 
37  // cppcheck-suppress noExplicitConstructor
38  template<typename... boolList> bits(const boolList &...bits);
39 
40  std::vector<bool> to_vector() const;
41 
42  static bits from_vector(const std::vector<bool> &bin);
43 
44  static bits from_hex(const std::string &hex);
45 
49  static bits from_size_t(size_t addr);
50 
51  bool is_zero() const;
52 
55  template<typename FieldT>
57  libsnark::protoboard<FieldT> &pb,
58  libsnark::pb_variable_array<FieldT> &var_array) const;
59 
60 protected:
61  template<typename boolIt> explicit bits(boolIt it);
62  template<typename boolIt> void fill_from_iterator(boolIt it);
63 
64  friend bits bits_xor<numBits>(const bits &, const bits &);
65  friend bits bits_add<numBits>(const bits &, const bits &, bool);
66 };
67 
69 using bits32 = bits<32>;
70 
72 using bits64 = bits<64>;
73 
74 // 256-bit array t
76 
78 
79 template<size_t TreeDepth> using bits_addr = bits<TreeDepth>;
80 
83 std::vector<bool> bit_vector_from_hex(const std::string &str);
84 
86 std::vector<bool> bit_vector_from_size_t_le(size_t x);
87 
89 std::vector<bool> bit_vector_from_size_t_be(size_t x);
90 
92  const std::vector<bool> &bits, std::ostream &out_s);
93 
94 } // namespace libzeth
95 
96 #include "libzeth/core/bits.tcc"
97 
98 #endif // __ZETH_CORE_BITS_HPP__
libzeth::bits::from_size_t
static bits from_size_t(size_t addr)
test_commands.mock.str
str
Definition: mock.py:18
libzeth::bits::is_zero
bool is_zero() const
libzeth::bits::to_vector
std::vector< bool > to_vector() const
libzeth::bits::from_vector
static bits from_vector(const std::vector< bool > &bin)
libzeth
Definition: binary_operation.hpp:15
bits.tcc
libzeth::bits
Generic class representing a bit-array of a specific size.
Definition: bits.hpp:19
libzeth::bits_xor
bits< numBits > bits_xor(const bits< numBits > &a, const bits< numBits > &b)
XOR two binary strings of the same length.
libzeth::bit_vector_from_size_t_be
std::vector< bool > bit_vector_from_size_t_be(size_t x)
Returns the big endian binary encoding of the integer x.
Definition: bits.cpp:39
libzeth::bits_add
bits< numBits > bits_add(const bits< numBits > &a, const bits< numBits > &b, bool with_carry=false)
Sum 2 binary strings with or without carry.
libzeth::bit_vector_write_string
void bit_vector_write_string(const std::vector< bool > &bits, std::ostream &out_s)
Definition: bits.cpp:65
libzeth::bits::bits
bits()
libzeth::bit_vector_from_size_t_le
std::vector< bool > bit_vector_from_size_t_le(size_t x)
Returns the little endian binary encoding of the integer x.
Definition: bits.cpp:28
libzeth::bit_vector_from_hex
std::vector< bool > bit_vector_from_hex(const std::string &hex_str)
Definition: bits.cpp:13
libzeth::bits::fill_pb_variable_array
void fill_pb_variable_array(libsnark::protoboard< FieldT > &pb, libsnark::pb_variable_array< FieldT > &var_array) const
libzeth::bits::from_hex
static bits from_hex(const std::string &hex)
include_libsnark.hpp
libzeth::bits::fill_from_iterator
void fill_from_iterator(boolIt it)