Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
keccak_output.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
12
13#include "../sparse.hpp"
14#include "../types.hpp"
15
17
23
24 public:
25 static constexpr uint64_t BASE = 11;
26
27 // effective base = maximum value each input 'quasi-bit' can reach
28 // (the input is in base-11 representation, but at this point in the Keccak algorithm each 'quasi-bit' can only
29 // take values [0, 1] not [0, ..., 10]
30 static constexpr uint64_t EFFECTIVE_BASE = 2;
31 static constexpr size_t TABLE_BITS = 8;
32
33 static constexpr uint64_t OUTPUT_NORMALIZATION_TABLE[2]{ 0, 1 };
34
42 {
44 uint64_t acc = 1;
45 for (size_t i = 0; i < TABLE_BITS; ++i) {
46 result[i] = acc;
47 acc *= BASE;
48 }
49 return result;
50 }
51
66 {
67 static constexpr auto scaled_bases = get_scaled_bases();
68
69 for (size_t i = 0; i < TABLE_BITS; ++i) {
70 if (counts[i] == EFFECTIVE_BASE - 1) {
71 counts[i] = 0;
72 } else {
73 counts[i] += 1;
74 break;
75 }
76 }
77
78 uint64_t value = 0;
79 uint64_t normalized_value = 0;
80 for (size_t i = 0; i < TABLE_BITS; ++i) {
81 value += counts[i] * scaled_bases[i];
82 normalized_value += (OUTPUT_NORMALIZATION_TABLE[counts[i]]) << i;
83 }
84 return { value, normalized_value };
85 }
86
94 static BasicTable generate_keccak_output_table(BasicTableId id, const size_t table_index)
95 {
96 BasicTable table;
97 table.id = id;
98 table.table_index = table_index;
99 table.use_twin_keys = false;
100 auto table_size = numeric::pow64(static_cast<uint64_t>(EFFECTIVE_BASE), TABLE_BITS);
101
103 std::array<uint64_t, 2> column_values{ 0, 0 };
104
105 for (size_t i = 0; i < table_size; ++i) {
106 table.column_1.emplace_back(column_values[0]);
107 table.column_2.emplace_back(column_values[1]);
108 table.column_3.emplace_back(0);
109 column_values = get_column_values_for_next_row(counts);
110 }
111
112 table.get_values_from_key = &sparse_tables::get_sparse_normalization_values<BASE, OUTPUT_NORMALIZATION_TABLE>;
113
114 table.column_1_step_size = bb::fr(numeric::pow64(static_cast<size_t>(BASE), TABLE_BITS));
115 table.column_2_step_size = bb::fr(((uint64_t)1 << TABLE_BITS));
116 table.column_3_step_size = 0;
117 return table;
118 }
119
161 {
162 constexpr size_t num_tables_per_multitable =
163 64 / TABLE_BITS + (64 % TABLE_BITS == 0 ? 0 : 1); // 64 bits, 8 bits per entry
164
165 uint64_t column_multiplier = numeric::pow64(BASE, TABLE_BITS);
166 MultiTable table(column_multiplier, (1ULL << TABLE_BITS), 0, num_tables_per_multitable);
167
168 table.id = id;
169 for (size_t i = 0; i < num_tables_per_multitable; ++i) {
170 table.slice_sizes.emplace_back(numeric::pow64(BASE, TABLE_BITS));
171 table.basic_table_ids.emplace_back(KECCAK_OUTPUT);
172 table.get_table_values.emplace_back(
173 &sparse_tables::get_sparse_normalization_values<BASE, OUTPUT_NORMALIZATION_TABLE>);
174 }
175 return table;
176 }
177};
178
179} // namespace bb::plookup::keccak_tables
Converts a base-11 sparse integer representation into a regular base-2 binary integer....
static constexpr uint64_t OUTPUT_NORMALIZATION_TABLE[2]
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 EFFECTIVE_BASE
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 MultiTable get_keccak_output_table(const MultiTableId id=KECCAK_FORMAT_OUTPUT)
Create the KeccakOutput MultiTable used by plookup to generate a sequence of lookups.
static BasicTable generate_keccak_output_table(BasicTableId id, const size_t table_index)
Generate plookup table that maps a TABLE_BITS-slice of a base-11 integer into a base-2 integer.
constexpr uint64_t pow64(const uint64_t input, const uint64_t exponent)
Definition pow.hpp:13
@ KECCAK_OUTPUT
Definition types.hpp:78
@ KECCAK_FORMAT_OUTPUT
Definition types.hpp:136
field< Bn254FrParams > fr
Definition fr.hpp:174
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