Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
keccak_theta.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Nishat], 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"
12
14
58class Theta {
59 public:
60 static constexpr size_t TABLE_BITS = 4;
61 static constexpr uint64_t BASE = 11;
62
63 static constexpr uint64_t THETA_NORMALIZATION_TABLE[11]{
64 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
65 };
66
76 {
77 uint64_t accumulator = 0;
78 uint64_t input = key[0];
79 uint64_t base_shift = 1;
80 while (input > 0) {
81 uint64_t slice = input % BASE;
82 uint64_t bit = THETA_NORMALIZATION_TABLE[static_cast<size_t>(slice)];
83 accumulator += (bit * base_shift);
84 input /= BASE;
85 base_shift *= BASE;
86 }
87 return { bb::fr(accumulator), bb::fr(0) };
88 }
89
97 {
99 uint64_t acc = 1;
100 for (size_t i = 0; i < TABLE_BITS; ++i) {
101 result[i] = acc;
102 acc *= BASE;
103 }
104 return result;
105 }
106
117 {
118 static constexpr auto scaled_bases = get_scaled_bases();
119
120 for (size_t i = 0; i < TABLE_BITS; ++i) {
121 if (counts[i] == BASE - 1) {
122 counts[i] = 0;
123 } else {
124 counts[i] += 1;
125 break;
126 }
127 }
128
129 uint64_t value = 0;
130 uint64_t normalized_value = 0;
131 for (size_t i = 0; i < TABLE_BITS; ++i) {
132 value += counts[i] * scaled_bases[i];
133 normalized_value += (THETA_NORMALIZATION_TABLE[counts[i]]) * scaled_bases[i];
134 }
135 return { value, normalized_value };
136 }
137
146 {
147 // max_base_value_plus_one sometimes may not equal base iff this is an intermediate lookup table
148 // (e.g. keccak, we have base11 values that need to be normalized where the actual values-per-base only range
149 // from [0, 1, 2])
150 BasicTable table;
151 table.id = id;
152 table.table_index = table_index;
153 table.use_twin_keys = false;
154 auto table_size = numeric::pow64(static_cast<uint64_t>(BASE), TABLE_BITS);
155
157 std::array<uint64_t, 2> column_values{ 0, 0 };
158
159 for (size_t i = 0; i < table_size; ++i) {
160 table.column_1.emplace_back(column_values[0]);
161 table.column_2.emplace_back(column_values[1]);
162 table.column_3.emplace_back(0);
163 column_values = get_column_values_for_next_row(counts);
164 }
165
167
168 constexpr uint64_t step_size = numeric::pow64(static_cast<uint64_t>(BASE), TABLE_BITS);
169 table.column_1_step_size = bb::fr(step_size);
170 table.column_2_step_size = bb::fr(step_size);
171 table.column_3_step_size = bb::fr(0);
172 return table;
173 }
174
217 {
218 constexpr size_t num_tables_per_multitable =
219 (64 / TABLE_BITS) + (64 % TABLE_BITS == 0 ? 0 : 1); // 64 bits, 4 bits per entry
220
221 uint64_t column_multiplier = numeric::pow64(BASE, TABLE_BITS);
222 MultiTable table(column_multiplier, column_multiplier, 0, num_tables_per_multitable);
223
224 table.id = id;
225 for (size_t i = 0; i < num_tables_per_multitable; ++i) {
226 table.slice_sizes.emplace_back(numeric::pow64(BASE, TABLE_BITS));
227 table.basic_table_ids.emplace_back(KECCAK_THETA);
229 }
230 return table;
231 }
232};
233} // namespace bb::plookup::keccak_tables
Generates plookup tables required for THETA round of Keccak hash function.
static MultiTable get_theta_output_table(const MultiTableId id=KECCAK_THETA_OUTPUT)
Create the THETA MultiTable used by plookup to generate a sequence of lookups.
static std::array< uint64_t, 2 > get_column_values_for_next_row(std::array< size_t, TABLE_BITS > &counts)
Get column values for next row of plookup table. Used to generate plookup table row values.
static constexpr uint64_t BASE
static std::array< bb::fr, 2 > get_theta_renormalization_values(const std::array< uint64_t, 2 > key)
Given a table input value, return the table output value.
static constexpr size_t TABLE_BITS
static constexpr std::array< uint64_t, TABLE_BITS > get_scaled_bases()
Precompute an array of base multipliers (11^i for i = [0, ..., TABLE_BITS - 1]) Code is slightly fast...
static constexpr uint64_t THETA_NORMALIZATION_TABLE[11]
static BasicTable generate_theta_renormalization_table(BasicTableId id, const size_t table_index)
Generate plookup table that normalizes a TABLE_BITS-slice of a base-11 integer.
constexpr uint64_t pow64(const uint64_t input, const uint64_t exponent)
Definition pow.hpp:13
@ KECCAK_THETA
Definition types.hpp:76
@ KECCAK_THETA_OUTPUT
Definition types.hpp:126
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
Container for managing multiple BasicTables plus the data needed to combine basic table outputs (e....
Definition types.hpp:147
std::vector< BasicTableId > basic_table_ids
Definition types.hpp:153
std::vector< uint64_t > slice_sizes
Definition types.hpp:154
std::vector< table_out(*)(table_in)> get_table_values
Definition types.hpp:163