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: 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"
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
67 // template <size_t i> static std::pair<uint64_t, uint64_t> update_counts(std::array<size_t, TABLE_BITS>& counts)
68 // {
69 // ASSERT(i <= TABLE_BITS);
70 // if constexpr (i >= TABLE_BITS) {
71 // // TODO use concepts or template metaprogramming to put this condition in method declaration
72 // return std::make_pair(0, 0);
73 // } else {
74 // if (counts[i] == BASE - 1) {
75 // counts[i] = 0;
76 // return update_counts<i + 1>(counts);
77 // } else {
78 // counts[i] += 1;
79 // }
80
81 // uint64_t value = 0;
82 // uint64_t normalized_value = 0;
83 // uint64_t cumulative_base = 1;
84 // for (size_t j = 0; j < TABLE_BITS; ++j) {
85 // value += counts[j] * cumulative_base;
86 // normalized_value += (THETA_NORMALIZATION_TABLE[counts[j]]) * cumulative_base;
87 // cumulative_base *= BASE;
88 // }
89 // return std::make_pair(value, normalized_value);
90 // }
91 // }
92
102 {
103 uint64_t accumulator = 0;
104 uint64_t input = key[0];
105 uint64_t base_shift = 1;
106 while (input > 0) {
107 uint64_t slice = input % BASE;
108 uint64_t bit = THETA_NORMALIZATION_TABLE[static_cast<size_t>(slice)];
109 accumulator += (bit * base_shift);
110 input /= BASE;
111 base_shift *= BASE;
112 }
113 return { bb::fr(accumulator), bb::fr(0) };
114 }
115
123 {
125 uint64_t acc = 1;
126 for (size_t i = 0; i < TABLE_BITS; ++i) {
127 result[i] = acc;
128 acc *= BASE;
129 }
130 return result;
131 }
132
143 {
144 static constexpr auto scaled_bases = get_scaled_bases();
145
146 for (size_t i = 0; i < TABLE_BITS; ++i) {
147 if (counts[i] == BASE - 1) {
148 counts[i] = 0;
149 } else {
150 counts[i] += 1;
151 break;
152 }
153 }
154
155 uint64_t value = 0;
156 uint64_t normalized_value = 0;
157 for (size_t i = 0; i < TABLE_BITS; ++i) {
158 value += counts[i] * scaled_bases[i];
159 normalized_value += (THETA_NORMALIZATION_TABLE[counts[i]]) * scaled_bases[i];
160 }
161 return { value, normalized_value };
162 }
163
172 {
173 // max_base_value_plus_one sometimes may not equal base iff this is an intermediate lookup table
174 // (e.g. keccak, we have base11 values that need to be normalized where the actual values-per-base only range
175 // from [0, 1, 2])
176 BasicTable table;
177 table.id = id;
178 table.table_index = table_index;
179 table.use_twin_keys = false;
180 auto table_size = numeric::pow64(static_cast<uint64_t>(BASE), TABLE_BITS);
181
183 std::array<uint64_t, 2> column_values{ 0, 0 };
184
185 for (size_t i = 0; i < table_size; ++i) {
186 table.column_1.emplace_back(column_values[0]);
187 table.column_2.emplace_back(column_values[1]);
188 table.column_3.emplace_back(0);
189 column_values = get_column_values_for_next_row(counts);
190 }
191
193
194 constexpr uint64_t step_size = numeric::pow64(static_cast<uint64_t>(BASE), TABLE_BITS);
195 table.column_1_step_size = bb::fr(step_size);
196 table.column_2_step_size = bb::fr(step_size);
197 table.column_3_step_size = bb::fr(0);
198 return table;
199 }
200
243 {
244 constexpr size_t num_tables_per_multitable =
245 (64 / TABLE_BITS) + (64 % TABLE_BITS == 0 ? 0 : 1); // 64 bits, 5 bits per entry
246
247 uint64_t column_multiplier = numeric::pow64(BASE, TABLE_BITS);
248 MultiTable table(column_multiplier, column_multiplier, 0, num_tables_per_multitable);
249
250 table.id = id;
251 for (size_t i = 0; i < num_tables_per_multitable; ++i) {
252 table.slice_sizes.emplace_back(numeric::pow64(BASE, TABLE_BITS));
253 table.basic_table_ids.emplace_back(KECCAK_THETA);
255 }
256 return table;
257 }
258};
259} // 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:75
@ KECCAK_THETA_OUTPUT
Definition types.hpp:133
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
Container for managing multiple BasicTables plus the data needed to combine basic table outputs (e....
Definition types.hpp:154
std::vector< BasicTableId > basic_table_ids
Definition types.hpp:160
std::vector< uint64_t > slice_sizes
Definition types.hpp:161
std::vector< table_out(*)(table_in)> get_table_values
Definition types.hpp:168