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: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
8
9#include "./types.hpp"
10
15
17
18template <uint64_t base, uint64_t num_rotated_bits>
20{
21 const auto t0 = numeric::map_into_sparse_form<base>(key[0]);
22 bb::fr t1;
23 if constexpr (num_rotated_bits > 0) {
24 t1 = numeric::map_into_sparse_form<base>(numeric::rotate32((uint32_t)key[0], num_rotated_bits));
25 } else {
26 t1 = t0;
27 }
28 return { bb::fr(t0), bb::fr(t1) };
29}
30
31template <uint64_t base, uint64_t bits_per_slice, uint64_t num_rotated_bits>
33{
34 BasicTable table;
35 table.id = id;
36 table.table_index = table_index;
37 auto table_size = (1U << bits_per_slice);
38 table.use_twin_keys = false;
39
40 for (uint64_t i = 0; i < table_size; ++i) {
41 const uint64_t source = i;
42 const auto target = numeric::map_into_sparse_form<base>(source);
43 table.column_1.emplace_back(bb::fr(source));
44 table.column_2.emplace_back(bb::fr(target));
45
46 if constexpr (num_rotated_bits > 0) {
47 const auto rotated =
48 numeric::map_into_sparse_form<base>(numeric::rotate32((uint32_t)source, num_rotated_bits));
49 table.column_3.emplace_back(bb::fr(rotated));
50 } else {
51 table.column_3.emplace_back(bb::fr(target));
52 }
53 }
54
55 table.get_values_from_key = &get_sparse_table_with_rotation_values<base, num_rotated_bits>;
56
57 uint256_t sparse_step_size = 1;
58 for (size_t i = 0; i < bits_per_slice; ++i) {
59 sparse_step_size *= base;
60 }
61 table.column_1_step_size = bb::fr((1 << 11));
62 table.column_2_step_size = bb::fr(sparse_step_size);
63 table.column_3_step_size = bb::fr(sparse_step_size);
64
65 return table;
66}
67
68template <size_t base, const uint64_t* base_table>
70{
71 uint64_t accumulator = 0;
72 uint64_t input = key[0];
73 uint64_t count = 0;
74 while (input > 0) {
75 uint64_t slice = input % base;
76 uint64_t bit = base_table[static_cast<size_t>(slice)];
77 accumulator += (bit << count);
78 input -= slice;
79 input /= base;
80 ++count;
81 }
82 return { bb::fr(accumulator), bb::fr(0) };
83}
84
85template <size_t base, uint64_t num_bits, const uint64_t* base_table>
87{
94 BasicTable table;
95 table.id = id;
96 table.table_index = table_index;
97 table.use_twin_keys = false;
98 auto table_size = numeric::pow64(static_cast<uint64_t>(base), num_bits);
99
102 for (size_t i = 0; i < table_size; ++i) {
103 const auto& limbs = accumulator.get_limbs();
104 uint64_t key = 0;
105 for (size_t j = 0; j < num_bits; ++j) {
106 const size_t table_idx = static_cast<size_t>(limbs[j]);
107 key += ((base_table[table_idx]) << static_cast<uint64_t>(j));
108 }
109
110 table.column_1.emplace_back(accumulator.get_sparse_value());
111 table.column_2.emplace_back(key);
112 table.column_3.emplace_back(bb::fr(0));
113 accumulator += to_add;
114 }
115
116 table.get_values_from_key = &get_sparse_normalization_values<base, base_table>;
117
118 table.column_1_step_size = bb::fr(table_size);
119 table.column_2_step_size = bb::fr(((uint64_t)1 << num_bits));
120 table.column_3_step_size = bb::fr(0);
121 return table;
122}
123} // 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)
Definition sparse.hpp:32
std::array< bb::fr, 2 > get_sparse_table_with_rotation_values(const std::array< uint64_t, 2 > key)
Definition sparse.hpp:19
std::array< bb::fr, 2 > get_sparse_normalization_values(const std::array< uint64_t, 2 > key)
Definition sparse.hpp:69
BasicTable generate_sparse_normalization_table(BasicTableId id, const size_t table_index)
Definition sparse.hpp:86
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:348
std::vector< bb::fr > column_3
Definition types.hpp:383
std::vector< bb::fr > column_2
Definition types.hpp:382
std::array< bb::fr, 2 >(* get_values_from_key)(const std::array< uint64_t, 2 >)
Definition types.hpp:391
std::vector< bb::fr > column_1
Definition types.hpp:381