Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
sparse_form.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Luke], commit: dd03c4a23ab067274b4964cacb36d1545f73fb14}
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
10#include <array>
11#include <cstddef>
12#include <cstdint>
13#include <vector>
14
15#include "../uint256/uint256.hpp"
16
17namespace bb::numeric {
18
23inline std::vector<uint64_t> slice_input(const uint256_t& input, const uint64_t base, const size_t num_slices)
24{
25 BB_ASSERT(base > 0);
26 uint256_t target = input;
27 std::vector<uint64_t> slices;
28 if (num_slices > 0) {
29 for (size_t i = 0; i < num_slices; ++i) {
30 slices.push_back((target % base).data[0]);
31 target /= base;
32 }
33 } else {
34 while (target > 0) {
35 slices.push_back((target % base).data[0]);
36 target /= base;
37 }
38 }
39 return slices;
40}
41
46inline std::vector<uint64_t> slice_input_using_variable_bases(const uint256_t& input,
47 const std::vector<uint64_t>& bases)
48{
49 uint256_t target = input;
50 std::vector<uint64_t> slices;
51 for (size_t i = 0; i < bases.size(); ++i) {
52 BB_ASSERT(bases[i] > 0);
53 if (target >= bases[i] && i == bases.size() - 1) {
54 throw_or_abort(format("Last key slice greater than ", bases[i]));
55 }
56 slices.push_back((target % bases[i]).data[0]);
57 target /= bases[i];
58 }
59 return slices;
60}
61
65template <uint64_t base, uint64_t num_slices> constexpr std::array<uint256_t, num_slices> get_base_powers()
66{
68 output[0] = 1;
69 for (size_t i = 1; i < num_slices; ++i) {
70 output[i] = output[i - 1] * base;
71 }
72 return output;
73}
74
80template <uint64_t base> constexpr uint256_t map_into_sparse_form(const uint64_t input)
81{
82 uint256_t out = 0UL;
83 auto converted = input;
84
85 constexpr auto base_powers = get_base_powers<base, 32>();
86 for (size_t i = 0; i < 32; ++i) {
87 uint64_t sparse_bit = ((converted >> i) & 1U);
88 if (sparse_bit) {
89 out += base_powers[i];
90 }
91 }
92 return out;
93}
94
100template <uint64_t base> constexpr uint64_t map_from_sparse_form(const uint256_t& input)
101{
102 uint256_t target = input;
103 uint64_t output = 0;
104
105 constexpr auto bases = get_base_powers<base, 32>();
106
107 for (uint64_t i = 0; i < 32; ++i) {
108 const auto& base_power = bases[static_cast<size_t>(31 - i)];
109 uint256_t prev_threshold = 0;
110 for (uint64_t j = 1; j < base + 1; ++j) {
111 const auto threshold = prev_threshold + base_power;
112 if (target < threshold) {
113 bool bit = ((j - 1) & 1);
114 if (bit) {
115 output += (1ULL << (31ULL - i));
116 }
117 if (j > 1) {
118 target -= (prev_threshold);
119 }
120 break;
121 }
122 prev_threshold = threshold;
123 }
124 }
125
126 return output;
127}
128
135template <uint64_t base, size_t num_bits> class sparse_int {
136 public:
137 sparse_int(const uint64_t input = 0)
138 : value(input)
139 {
140 for (size_t i = 0; i < num_bits; ++i) {
141 const uint64_t bit = (input >> i) & 1U;
142 limbs[i] = bit;
143 }
144 }
145 sparse_int(const sparse_int& other) noexcept = default;
146 sparse_int(sparse_int&& other) noexcept = default;
147 sparse_int& operator=(const sparse_int& other) noexcept = default;
148 sparse_int& operator=(sparse_int&& other) noexcept = default;
149 ~sparse_int() noexcept = default;
150
151 // Single-pass carry propagation: correct when all input limbs are < base, which is guaranteed
152 // by the constructor (limbs are 0 or 1) and maintained by this operator (carry produces values < base).
153 sparse_int operator+(const sparse_int& other) const
154 {
155 sparse_int result(*this);
156 for (size_t i = 0; i < num_bits - 1; ++i) {
157 result.limbs[i] += other.limbs[i];
158 if (result.limbs[i] >= base) {
159 result.limbs[i] -= base;
160 ++result.limbs[i + 1];
161 // After carry: result.limbs[i] < base (since both inputs were < base, sum < 2*base,
162 // so subtracting base gives a value < base). The carry of 1 into limbs[i+1] cannot
163 // cascade because limbs[i+1] hasn't been added to other.limbs[i+1] yet.
164 }
165 }
166 result.limbs[num_bits - 1] += other.limbs[num_bits - 1];
167 result.limbs[num_bits - 1] %= base;
168 result.value += other.value;
169 return result;
170 };
171
173 {
174 *this = *this + other;
175 return *this;
176 }
177
178 [[nodiscard]] uint64_t get_value() const { return value; }
179
180 [[nodiscard]] uint64_t get_sparse_value() const
181 {
182 uint64_t result = 0;
183 for (size_t i = num_bits - 1; i < num_bits; --i) {
184 result *= base;
185 result += limbs[i];
186 }
187 return result;
188 }
189
190 const std::array<uint64_t, num_bits>& get_limbs() const { return limbs; }
191
192 private:
193 std::array<uint64_t, num_bits> limbs;
194 uint64_t value;
195};
196
197} // namespace bb::numeric
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
Integer type that stores each bit as a separate digit in the given base. Supports addition with singl...
sparse_int & operator=(sparse_int &&other) noexcept=default
sparse_int & operator=(const sparse_int &other) noexcept=default
std::array< uint64_t, num_bits > limbs
sparse_int(const sparse_int &other) noexcept=default
uint64_t get_sparse_value() const
sparse_int(sparse_int &&other) noexcept=default
const std::array< uint64_t, num_bits > & get_limbs() const
uint64_t get_value() const
~sparse_int() noexcept=default
sparse_int(const uint64_t input=0)
sparse_int operator+=(const sparse_int &other)
std::string format(Args... args)
Definition log.hpp:23
const std::vector< MemoryValue > data
constexpr uint64_t map_from_sparse_form(const uint256_t &input)
Decode a sparse-form uint256_t back to a 32-bit value. Extracts the base-adic digits from most-signif...
constexpr uint256_t map_into_sparse_form(const uint64_t input)
Encode a 32-bit value into sparse form: each binary bit of input becomes a digit in the given base....
std::vector< uint64_t > slice_input(const uint256_t &input, const uint64_t base, const size_t num_slices)
Decompose a uint256_t into digits in the given base (least-significant digit first)....
constexpr std::array< uint256_t, num_slices > get_base_powers()
Compute [1, base, base^2, ..., base^(num_slices-1)] as uint256_t values.
std::vector< uint64_t > slice_input_using_variable_bases(const uint256_t &input, const std::vector< uint64_t > &bases)
Decompose a uint256_t using a different base for each digit position (least-significant first)....
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
void throw_or_abort(std::string const &err)