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
29template <uint64_t base, uint64_t num_rotated_bits>
31{
32 const auto t0 = numeric::map_into_sparse_form<base>(key[0]);
33 bb::fr t1;
34 if constexpr (num_rotated_bits > 0) {
35 t1 = numeric::map_into_sparse_form<base>(numeric::rotate32((uint32_t)key[0], num_rotated_bits));
36 } else {
37 t1 = t0;
38 }
39 return { bb::fr(t0), bb::fr(t1) };
40}
41
61template <uint64_t base, uint64_t bits_per_slice, uint64_t num_rotated_bits>
63{
64 BasicTable table;
65 table.id = id;
66 table.table_index = table_index;
67 auto table_size = (1U << bits_per_slice);
68 table.use_twin_keys = false;
69
70 for (uint64_t i = 0; i < table_size; ++i) {
71 const uint64_t source = i;
72 const auto target = numeric::map_into_sparse_form<base>(source);
73 table.column_1.emplace_back(bb::fr(source));
74 table.column_2.emplace_back(bb::fr(target));
75
76 if constexpr (num_rotated_bits > 0) {
77 const auto rotated =
78 numeric::map_into_sparse_form<base>(numeric::rotate32((uint32_t)source, num_rotated_bits));
79 table.column_3.emplace_back(bb::fr(rotated));
80 } else {
81 table.column_3.emplace_back(bb::fr(target));
82 }
83 }
84
85 table.get_values_from_key = &get_sparse_table_with_rotation_values<base, num_rotated_bits>;
86
87 uint256_t sparse_step_size = 1;
88 for (size_t i = 0; i < bits_per_slice; ++i) {
89 sparse_step_size *= base;
90 }
91 table.column_1_step_size = bb::fr((1 << 11));
92 table.column_2_step_size = bb::fr(sparse_step_size);
93 table.column_3_step_size = bb::fr(sparse_step_size);
94
95 return table;
96}
97
106template <size_t base, const uint64_t* base_table>
108{
109 uint64_t accumulator = 0;
110 uint64_t input = key[0];
111 uint64_t count = 0;
112 while (input > 0) {
113 const uint64_t slice = input % base;
114 const uint64_t bit = base_table[static_cast<size_t>(slice)];
115 accumulator += (bit << count);
116 input -= slice;
117 input /= base;
118 ++count;
119 }
120 return { bb::fr(accumulator), bb::fr(0) };
121}
122
143template <size_t base, uint64_t num_bits, const uint64_t* base_table>
145{
146 BasicTable table;
147 table.id = id;
148 table.table_index = table_index;
149 table.use_twin_keys = false;
150 auto table_size = numeric::pow64(static_cast<uint64_t>(base), num_bits);
151
154 for (size_t i = 0; i < table_size; ++i) {
155 const std::array<uint64_t, num_bits>& limbs = accumulator.get_limbs();
156 uint64_t normalized_output = 0;
157 for (size_t j = 0; j < num_bits; ++j) {
158 const auto sparse_digit = limbs[j];
159 normalized_output += base_table[sparse_digit] << j;
160 }
161
162 table.column_1.emplace_back(accumulator.get_sparse_value());
163 table.column_2.emplace_back(normalized_output);
164 table.column_3.emplace_back(bb::fr(0));
165 accumulator += to_add;
166 }
167
168 table.get_values_from_key = &get_sparse_normalization_values<base, base_table>;
169
170 table.column_1_step_size = bb::fr(table_size);
171 table.column_2_step_size = bb::fr(1ULL << num_bits);
172 table.column_3_step_size = bb::fr(0);
173 return table;
174}
175} // 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 for converting values to sparse form with optional rotation.
Definition sparse.hpp:62
std::array< bb::fr, 2 > get_sparse_table_with_rotation_values(const std::array< uint64_t, 2 > key)
Computes the C2 and C3 column values for a sparse lookup table with optional rotation.
Definition sparse.hpp:30
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:107
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:144
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