Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
bits.cpp
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 #include "libzeth/core/bits.hpp"
6 
8 #include "libzeth/core/utils.hpp"
9 
10 namespace libzeth
11 {
12 
13 std::vector<bool> bit_vector_from_hex(const std::string &hex_str)
14 {
15  std::vector<bool> result;
16  result.reserve(4 * hex_str.size());
17  for (char c : hex_str) {
18  const uint8_t nibble = char_to_nibble(c);
19  result.push_back(nibble & 8);
20  result.push_back(nibble & 4);
21  result.push_back(nibble & 2);
22  result.push_back(nibble & 1);
23  }
24 
25  return result;
26 }
27 
28 std::vector<bool> bit_vector_from_size_t_le(size_t x)
29 {
30  std::vector<bool> ret;
31  while (x) {
32  ret.push_back((x & 1) != 0);
33  x >>= 1;
34  }
35 
36  return ret;
37 }
38 
39 std::vector<bool> bit_vector_from_size_t_be(size_t x)
40 {
41  std::vector<bool> res;
42  size_t num_bits = 8 * sizeof(size_t);
43  const size_t mask = 1ULL << (num_bits - 1);
44 
45  // Remove 0-bits at the front
46  while (num_bits > 0) {
47  if ((x & mask) != 0) {
48  break;
49  }
50  x = x << 1;
51  --num_bits;
52  }
53 
54  // Pre-allocate and fill the vector with remaining bits
55  res.reserve(num_bits);
56  while (num_bits > 0) {
57  res.push_back((x & mask) != 0);
58  x = x << 1;
59  --num_bits;
60  }
61 
62  return res;
63 }
64 
65 void bit_vector_write_string(const std::vector<bool> &bits, std::ostream &out_s)
66 {
67  out_s << "{";
68  for (size_t i = 0; i < bits.size() - 1; ++i) {
69  out_s << bits[i] << ", ";
70  }
71  out_s << bits[bits.size() - 1] << "}\n";
72 }
73 
74 } // namespace libzeth
utils.hpp
include_libff.hpp
libzeth
Definition: binary_operation.hpp:15
libzeth::bits
Generic class representing a bit-array of a specific size.
Definition: bits.hpp:19
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::bit_vector_write_string
void bit_vector_write_string(const std::vector< bool > &bits, std::ostream &out_s)
Definition: bits.cpp:65
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
bits.hpp
analyzer.parse_r1cs.res
def res
Definition: parse_r1cs.py:144
libzeth::char_to_nibble
uint8_t char_to_nibble(const char c)
Definition: utils.cpp:15
libzeth::bit_vector_from_hex
std::vector< bool > bit_vector_from_hex(const std::string &hex_str)
Definition: bits.cpp:13