Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
sparse.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Luke], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
8
9#include "./types.hpp"
10
15
17
35template <uint64_t base, uint64_t num_rotated_bits>
37{
38 const auto t0 = numeric::map_into_sparse_form<base>(key[0]);
39 bb::fr t1;
40 if constexpr (num_rotated_bits > 0) {
41 t1 = numeric::map_into_sparse_form<base>(numeric::rotate32((uint32_t)key[0], num_rotated_bits));
42 } else {
43 t1 = t0;
44 }
45 return { bb::fr(t0), bb::fr(t1) };
46}
47
79template <uint64_t base, uint64_t bits_per_slice, uint64_t num_rotated_bits>
81{
82 BasicTable table;
83 table.id = id;
84 table.table_index = table_index;
85 auto table_size = (1U << bits_per_slice);
86 table.use_twin_keys = false;
87
88 for (uint64_t i = 0; i < table_size; ++i) {
89 const uint64_t source = i;
90 const auto target = numeric::map_into_sparse_form<base>(source);
91 table.column_1.emplace_back(bb::fr(source));
92 table.column_2.emplace_back(bb::fr(target));
93
94 if constexpr (num_rotated_bits > 0) {
95 const auto rotated =
96 numeric::map_into_sparse_form<base>(numeric::rotate32((uint32_t)source, num_rotated_bits));
97 table.column_3.emplace_back(bb::fr(rotated));
98 } else {
99 table.column_3.emplace_back(bb::fr(target));
100 }
101 }
102
103 table.get_values_from_key = &get_sparse_table_with_rotation_values<base, num_rotated_bits>;
104
105 uint256_t sparse_step_size = 1;
106 for (size_t i = 0; i < bits_per_slice; ++i) {
107 sparse_step_size *= base;
108 }
109 table.column_1_step_size = bb::fr((1 << 11));
110 table.column_2_step_size = bb::fr(sparse_step_size);
111 table.column_3_step_size = bb::fr(sparse_step_size);
112
113 return table;
114}
115
124template <size_t base, const uint64_t* base_table>
126{
127 uint64_t accumulator = 0;
128 uint64_t input = key[0];
129 uint64_t count = 0;
130 while (input > 0) {
131 const uint64_t slice = input % base;
132 const uint64_t bit = base_table[static_cast<size_t>(slice)];
133 accumulator += (bit << count);
134 input -= slice;
135 input /= base;
136 ++count;
137 }
138 return { bb::fr(accumulator), bb::fr(0) };
139}
140
161template <size_t base, uint64_t num_bits, const uint64_t* base_table>
163{
164 BasicTable table;
165 table.id = id;
166 table.table_index = table_index;
167 table.use_twin_keys = false;
168 auto table_size = numeric::pow64(static_cast<uint64_t>(base), num_bits);
169
172 for (size_t i = 0; i < table_size; ++i) {
173 const std::array<uint64_t, num_bits>& limbs = accumulator.get_limbs();
174 uint64_t normalized_output = 0;
175 for (size_t j = 0; j < num_bits; ++j) {
176 const auto sparse_digit = limbs[j];
177 normalized_output += base_table[sparse_digit] << j;
178 }
179
180 table.column_1.emplace_back(accumulator.get_sparse_value());
181 table.column_2.emplace_back(normalized_output);
182 table.column_3.emplace_back(bb::fr(0));
183 accumulator += to_add;
184 }
185
186 table.get_values_from_key = &get_sparse_normalization_values<base, base_table>;
187
188 table.column_1_step_size = bb::fr(table_size);
189 table.column_2_step_size = bb::fr(1ULL << num_bits);
190 table.column_3_step_size = bb::fr(0);
191 return table;
192}
193} // namespace bb::plookup::sparse_tables
uint64_t get_sparse_value() const
const std::array< uint64_t, num_bits > & get_limbs() const
constexpr uint64_t pow64(const uint64_t input, const uint64_t exponent)
Definition pow.hpp:13
constexpr uint32_t rotate32(const uint32_t value, const uint32_t rotation)
Definition rotate.hpp:18
BasicTable generate_sparse_table_with_rotation(BasicTableId id, const size_t table_index)
Generates a BasicTable that maps integers to their sparse form representation, with optional 32-bit r...
Definition sparse.hpp:80
std::array< bb::fr, 2 > get_sparse_table_with_rotation_values(const std::array< uint64_t, 2 > key)
Computes the sparse form values for a given key, used as a callback for plookup table queries.
Definition sparse.hpp:36
std::array< bb::fr, 2 > get_sparse_normalization_values(const std::array< uint64_t, 2 > key)
Computes the normalized output for a sparse value based on a provided normalization table.
Definition sparse.hpp:125
BasicTable generate_sparse_normalization_table(BasicTableId id, const size_t table_index)
Generates a BasicTable for normalizing sparse form values back to normal form.
Definition sparse.hpp:162
field< Bn254FrParams > fr
Definition fr.hpp:174
C slice(C const &container, size_t start)
Definition container.hpp:9
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
A basic table from which we can perform lookups (for example, an xor table)
Definition types.hpp:285
std::vector< bb::fr > column_3
Definition types.hpp:320
std::vector< bb::fr > column_2
Definition types.hpp:319
std::array< bb::fr, 2 >(* get_values_from_key)(const std::array< uint64_t, 2 >)
Definition types.hpp:328
std::vector< bb::fr > column_1
Definition types.hpp:318