Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
blake2s.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
10
11#include "sparse.hpp"
12#include "types.hpp"
13
15
16static constexpr size_t BITS_IN_LAST_SLICE = 5UL;
17static constexpr size_t SIZE_OF_LAST_SLICE = (1UL << BITS_IN_LAST_SLICE);
18
24template <uint64_t bits_per_slice, uint64_t num_rotated_output_bits, bool filter = false>
26{
27 uint64_t filtered_key0 = filter ? key[0] & 3ULL : key[0];
28 uint64_t filtered_key1 = filter ? key[1] & 3ULL : key[1];
29 return { uint256_t(numeric::rotate32(uint32_t(filtered_key0) ^ uint32_t(filtered_key1),
30 uint32_t(num_rotated_output_bits))),
31 0ULL };
32}
33
37template <uint64_t bits_per_slice, uint64_t num_rotated_output_bits, bool filter = false>
38inline BasicTable generate_xor_rotate_table(BasicTableId id, const size_t table_index)
39{
40 const uint64_t base = 1UL << bits_per_slice;
41 BasicTable table;
42 table.id = id;
43 table.table_index = table_index;
44 table.use_twin_keys = true;
45
46 for (uint64_t i = 0; i < base; ++i) {
47 for (uint64_t j = 0; j < base; ++j) {
48 table.column_1.emplace_back(i);
49 table.column_2.emplace_back(j);
50 uint64_t i_copy = i;
51 uint64_t j_copy = j;
52 if (filter) {
53 i_copy &= 3ULL;
54 j_copy &= 3ULL;
55 }
56 table.column_3.emplace_back(
57 uint256_t(numeric::rotate32(uint32_t(i_copy) ^ uint32_t(j_copy), uint32_t(num_rotated_output_bits))));
58 }
59 }
60
61 table.get_values_from_key = &get_xor_rotate_values_from_key<bits_per_slice, num_rotated_output_bits, filter>;
62
63 table.column_1_step_size = base;
64 table.column_2_step_size = base;
65 table.column_3_step_size = base;
66
67 return table;
68}
69
95{
96 const size_t num_entries = (32 + 2) / 6 + 1;
97 const uint64_t base = 1 << 6;
98 MultiTable table(base, base, base, num_entries);
99
100 table.id = id;
101 for (size_t i = 0; i < num_entries - 1; ++i) {
102 table.slice_sizes.emplace_back(base);
103 table.basic_table_ids.emplace_back(BLAKE_XOR_ROTATE0);
104 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>);
105 }
106
107 table.slice_sizes.emplace_back(SIZE_OF_LAST_SLICE);
109 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<BITS_IN_LAST_SLICE, 0, true>);
110
111 return table;
112}
113
118{
119 const uint64_t base = 1 << 6;
120 constexpr bb::fr coefficient_16 = bb::fr(1) / bb::fr(1 << 16);
121
122 std::vector<bb::fr> column_1_coefficients{ bb::fr(1), bb::fr(1 << 6), bb::fr(1 << 12),
123 bb::fr(1 << 18), bb::fr(1 << 24), bb::fr(1 << 30) };
124
125 std::vector<bb::fr> column_3_coefficients{ bb::fr(1),
126 bb::fr(1 << 6),
127 coefficient_16,
128 coefficient_16 * bb::fr(1 << 2),
129 coefficient_16 * bb::fr(1 << 8),
130 coefficient_16 * bb::fr(1 << 14) };
131
132 MultiTable table(column_1_coefficients, column_1_coefficients, column_3_coefficients);
133
134 table.id = id;
135 table.slice_sizes = { base, base, base, base, base, SIZE_OF_LAST_SLICE };
138
139 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>);
140 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>);
141 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 4>);
142 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>);
143 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>);
144 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<BITS_IN_LAST_SLICE, 0, true>);
145
146 return table;
147}
148
153{
154 const uint64_t base = 1 << 6;
155 constexpr bb::fr coefficient_24 = bb::fr(1) / bb::fr(1 << 24);
156
157 std::vector<bb::fr> column_1_coefficients{ bb::fr(1), bb::fr(1 << 6), bb::fr(1 << 12),
158 bb::fr(1 << 18), bb::fr(1 << 24), bb::fr(1 << 30) };
159
160 std::vector<bb::fr> column_3_coefficients{ bb::fr(1),
161 coefficient_24,
162 coefficient_24 * bb::fr(1 << 4),
163 coefficient_24 * bb::fr(1 << (4 + 6)),
164 coefficient_24 * bb::fr(1 << (4 + 12)),
165 coefficient_24 * bb::fr(1 << (4 + 18)) };
166
167 MultiTable table(column_1_coefficients, column_1_coefficients, column_3_coefficients);
168
169 table.id = id;
170 table.slice_sizes = { base, base, base, base, base, SIZE_OF_LAST_SLICE };
173
174 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>);
175 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 2>);
176 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>);
177 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>);
178 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>);
179 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<BITS_IN_LAST_SLICE, 0, true>);
180
181 return table;
182}
183
188{
189 const uint64_t base = 1 << 6;
190 constexpr bb::fr coefficient_25 = bb::fr(1) / bb::fr(1 << 25);
191
192 std::vector<bb::fr> column_1_coefficients{ bb::fr(1), bb::fr(1 << 6), bb::fr(1 << 12),
193 bb::fr(1 << 18), bb::fr(1 << 24), bb::fr(1 << 30) };
194
195 std::vector<bb::fr> column_3_coefficients{ bb::fr(1),
196 coefficient_25,
197 coefficient_25 * bb::fr(1 << 5),
198 coefficient_25 * bb::fr(1 << (5 + 6)),
199 coefficient_25 * bb::fr(1 << (5 + 12)),
200 coefficient_25 * bb::fr(1 << (5 + 18)) };
201
202 MultiTable table(column_1_coefficients, column_1_coefficients, column_3_coefficients);
203
204 table.id = id;
205 table.slice_sizes = { base, base, base, base, base, SIZE_OF_LAST_SLICE };
208
209 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>);
210 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 1>);
211 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>);
212 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>);
213 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>);
214 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<BITS_IN_LAST_SLICE, 0, true>);
215
216 return table;
217}
218
219} // namespace bb::plookup::blake2s_tables
constexpr uint32_t rotate32(const uint32_t value, const uint32_t rotation)
Definition rotate.hpp:18
MultiTable get_blake2s_xor_rotate_8_table(const MultiTableId id=BLAKE_XOR_ROTATE_8)
Definition blake2s.hpp:152
MultiTable get_blake2s_xor_rotate_7_table(const MultiTableId id=BLAKE_XOR_ROTATE_7)
Definition blake2s.hpp:187
BasicTable generate_xor_rotate_table(BasicTableId id, const size_t table_index)
Definition blake2s.hpp:38
MultiTable get_blake2s_xor_rotate_16_table(const MultiTableId id=BLAKE_XOR_ROTATE_16)
Definition blake2s.hpp:117
std::array< bb::fr, 2 > get_xor_rotate_values_from_key(const std::array< uint64_t, 2 > key)
Definition blake2s.hpp:25
MultiTable get_blake2s_xor_table(const MultiTableId id=BLAKE_XOR)
Definition blake2s.hpp:94
@ BLAKE_XOR_ROTATE0
Definition types.hpp:63
@ BLAKE_XOR_ROTATE0_SLICE5_MOD4
Definition types.hpp:64
@ BLAKE_XOR_ROTATE1
Definition types.hpp:65
@ BLAKE_XOR_ROTATE2
Definition types.hpp:66
@ BLAKE_XOR_ROTATE4
Definition types.hpp:67
@ BLAKE_XOR_ROTATE_16
Definition types.hpp:129
@ BLAKE_XOR_ROTATE_7
Definition types.hpp:131
@ BLAKE_XOR_ROTATE_8
Definition types.hpp:130
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