barretenberg
Loading...
Searching...
No Matches
keccak_output.hpp
1#pragma once
2
3#include "barretenberg/common/constexpr_utils.hpp"
4#include "barretenberg/numeric/bitop/pow.hpp"
5#include "barretenberg/numeric/bitop/sparse_form.hpp"
6
7#include "../sparse.hpp"
8#include "../types.hpp"
9
10namespace plookup {
11namespace keccak_tables {
12
18
19 public:
20 static constexpr uint64_t BASE = 11;
21
22 // effective base = maximum value each input 'quasi-bit' can reach
23 // (the input is in base-11 representation, but at this point in the Keccak algorithm each 'quasi-bit' can only
24 // take values [0, 1] not [0, ..., 10]
25 static constexpr uint64_t EFFECTIVE_BASE = 2;
26 static constexpr size_t TABLE_BITS = 8;
27
28 static constexpr uint64_t OUTPUT_NORMALIZATION_TABLE[2]{ 0, 1 };
29
36 static constexpr std::array<uint64_t, TABLE_BITS> get_scaled_bases()
37 {
38 std::array<uint64_t, TABLE_BITS> result;
39 uint64_t acc = 1;
40 for (size_t i = 0; i < TABLE_BITS; ++i) {
41 result[i] = acc;
42 acc *= BASE;
43 }
44 return result;
45 }
46
60 static std::array<uint64_t, 2> get_column_values_for_next_row(std::array<size_t, TABLE_BITS>& counts)
61 {
62 static constexpr auto scaled_bases = get_scaled_bases();
63
64 for (size_t i = 0; i < TABLE_BITS; ++i) {
65 if (counts[i] == EFFECTIVE_BASE - 1) {
66 counts[i] = 0;
67 } else {
68 counts[i] += 1;
69 break;
70 }
71 }
72
73 uint64_t value = 0;
74 uint64_t normalized_value = 0;
75 for (size_t i = 0; i < TABLE_BITS; ++i) {
76 value += counts[i] * scaled_bases[i];
77 normalized_value += (OUTPUT_NORMALIZATION_TABLE[counts[i]]) << i;
78 }
79 return { value, normalized_value };
80 }
81
89 static BasicTable generate_keccak_output_table(BasicTableId id, const size_t table_index)
90 {
91 BasicTable table;
92 table.id = id;
93 table.table_index = table_index;
94 table.use_twin_keys = false;
95 table.size = numeric::pow64(static_cast<uint64_t>(EFFECTIVE_BASE), TABLE_BITS);
96
97 std::array<size_t, TABLE_BITS> counts{};
98 std::array<uint64_t, 2> column_values{ 0, 0 };
99
100 for (size_t i = 0; i < table.size; ++i) {
101 table.column_1.emplace_back(column_values[0]);
102 table.column_2.emplace_back(column_values[1]);
103 table.column_3.emplace_back(0);
104 column_values = get_column_values_for_next_row(counts);
105 }
106
107 table.get_values_from_key = &sparse_tables::get_sparse_normalization_values<BASE, OUTPUT_NORMALIZATION_TABLE>;
108
109 table.column_1_step_size = barretenberg::fr(numeric::pow64(static_cast<size_t>(BASE), TABLE_BITS));
110 table.column_2_step_size = barretenberg::fr(((uint64_t)1 << TABLE_BITS));
111 table.column_3_step_size = 0;
112 return table;
113 }
114
155 static MultiTable get_keccak_output_table(const MultiTableId id = KECCAK_FORMAT_OUTPUT)
156 {
157 constexpr size_t num_tables_per_multitable =
158 64 / TABLE_BITS + (64 % TABLE_BITS == 0 ? 0 : 1); // 64 bits, 8 bits per entry
159
160 uint64_t column_multiplier = numeric::pow64(BASE, TABLE_BITS);
161 MultiTable table(column_multiplier, (1ULL << TABLE_BITS), 0, num_tables_per_multitable);
162
163 table.id = id;
164 for (size_t i = 0; i < num_tables_per_multitable; ++i) {
165 table.slice_sizes.emplace_back(numeric::pow64(BASE, TABLE_BITS));
166 table.lookup_ids.emplace_back(KECCAK_OUTPUT);
167 table.get_table_values.emplace_back(
168 &sparse_tables::get_sparse_normalization_values<BASE, OUTPUT_NORMALIZATION_TABLE>);
169 }
170 return table;
171 }
172};
173
174} // namespace keccak_tables
175} // namespace plookup
Converts a base-11 sparse integer representation into a regular base-2 binary integer....
Definition: keccak_output.hpp:17
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.
Definition: keccak_output.hpp:155
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...
Definition: keccak_output.hpp:36
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.
Definition: keccak_output.hpp:60
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.
Definition: keccak_output.hpp:89
The structure contains the most basic table serving one function (for, example an xor table)
Definition: types.hpp:263
Definition: types.hpp:124