Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
uint.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"
10
12
14
15template <uint64_t bits_per_slice, uint64_t num_rotated_output_bits>
17{
18 return { numeric::rotate64(key[0] ^ key[1], num_rotated_output_bits), 0ULL };
19}
20
21template <uint64_t bits_per_slice, uint64_t num_rotated_output_bits>
22inline BasicTable generate_xor_rotate_table(BasicTableId id, const size_t table_index)
23{
24 const uint64_t base = 1UL << bits_per_slice;
25 BasicTable table;
26 table.id = id;
27 table.table_index = table_index;
28 table.use_twin_keys = true;
29
30 for (uint64_t i = 0; i < base; ++i) {
31 for (uint64_t j = 0; j < base; ++j) {
32 table.column_1.emplace_back(i);
33 table.column_2.emplace_back(j);
34 table.column_3.emplace_back(numeric::rotate64(i ^ j, num_rotated_output_bits));
35 }
36 }
37
38 table.get_values_from_key = &get_xor_rotate_values_from_key<bits_per_slice, num_rotated_output_bits>;
39
40 table.column_1_step_size = base;
41 table.column_2_step_size = base;
42 table.column_3_step_size = base;
43
44 return table;
45}
46
47template <uint64_t bits_per_slice, uint64_t num_rotated_output_bits>
49{
50 return { numeric::rotate64(key[0] & key[1], num_rotated_output_bits), 0ULL };
51}
52
53template <uint64_t bits_per_slice, uint64_t num_rotated_output_bits>
54inline BasicTable generate_and_rotate_table(BasicTableId id, const size_t table_index)
55{
56 const uint64_t base = 1UL << bits_per_slice;
57 BasicTable table;
58 table.id = id;
59 table.table_index = table_index;
60 table.use_twin_keys = true;
61
62 for (uint64_t i = 0; i < base; ++i) {
63 for (uint64_t j = 0; j < base; ++j) {
64 table.column_1.emplace_back(i);
65 table.column_2.emplace_back(j);
66 table.column_3.emplace_back(numeric::rotate64(i & j, num_rotated_output_bits));
67 }
68 }
69
70 table.get_values_from_key = &get_and_rotate_values_from_key<bits_per_slice, num_rotated_output_bits>;
71
72 table.column_1_step_size = base;
73 table.column_2_step_size = base;
74 table.column_3_step_size = base;
75
76 return table;
77}
78
79template <size_t uint_size> inline MultiTable get_uint_xor_table(const MultiTableId id)
80{
81 // uint_size must be one of 8, 16, 32, or 64.
82 ASSERT(uint_size == 8 || uint_size == 16 || uint_size == 32 || uint_size == 64,
83 "unsupported uint size for XOR table generation");
84
85 const size_t TABLE_BIT_SIZE = 6;
86 const size_t num_entries = uint_size / TABLE_BIT_SIZE;
87 const uint64_t base = 1 << TABLE_BIT_SIZE;
88 MultiTable table(base, base, base, num_entries);
89
90 table.id = id;
91 for (size_t i = 0; i < num_entries; ++i) {
92 table.slice_sizes.emplace_back(base);
94 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>);
95 }
96
97 // remaining bits
98 const size_t LAST_TABLE_BIT_SIZE = uint_size - TABLE_BIT_SIZE * num_entries;
99 const size_t LAST_SLICE_SIZE = 1 << LAST_TABLE_BIT_SIZE;
100 table.slice_sizes.emplace_back(LAST_SLICE_SIZE);
101 if (uint_size == 8 || uint_size == 32) {
102 // uint8 and uint32 have 2 bits left because: 8 % 6 = 2 and 32 % 6 = 2
103 table.basic_table_ids.emplace_back(UINT_XOR_SLICE_2_ROTATE_0);
104 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<2, 0>);
105 } else {
106 // uint16 and uint64 have 4 bits left because: 16 % 6 = 4 and 64 % 6 = 4
107 table.basic_table_ids.emplace_back(UINT_XOR_SLICE_4_ROTATE_0);
108 table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<4, 0>);
109 }
110 return table;
111}
112
113template <size_t uint_size> inline MultiTable get_uint_and_table(const MultiTableId id)
114{
115 // uint_size must be one of 8, 16, 32, or 64.
116 ASSERT(uint_size == 8 || uint_size == 16 || uint_size == 32 || uint_size == 64,
117 "unsupported uint size for AND table generation");
118
119 const size_t TABLE_BIT_SIZE = 6;
120 const size_t num_entries = uint_size / TABLE_BIT_SIZE;
121 const uint64_t base = 1 << TABLE_BIT_SIZE;
122 MultiTable table(base, base, base, num_entries);
123
124 table.id = id;
125 for (size_t i = 0; i < num_entries; ++i) {
126 table.slice_sizes.emplace_back(base);
127 table.basic_table_ids.emplace_back(UINT_AND_SLICE_6_ROTATE_0);
128 table.get_table_values.emplace_back(&get_and_rotate_values_from_key<6, 0>);
129 }
130
131 // remaining bits
132 const size_t LAST_TABLE_BIT_SIZE = uint_size - TABLE_BIT_SIZE * num_entries;
133 const size_t LAST_SLICE_SIZE = 1 << LAST_TABLE_BIT_SIZE;
134 table.slice_sizes.emplace_back(LAST_SLICE_SIZE);
135 if (uint_size == 8 || uint_size == 32) {
136 // uint8 and uint32 have 2 bits left because: 8 % 6 = 2 and 32 % 6 = 2
137 table.basic_table_ids.emplace_back(UINT_AND_SLICE_2_ROTATE_0);
138 table.get_table_values.emplace_back(&get_and_rotate_values_from_key<2, 0>);
139 } else {
140 // uint16 and uint64 have 4 bits left because: 16 % 6 = 4 and 64 % 6 = 4
141 table.basic_table_ids.emplace_back(UINT_AND_SLICE_4_ROTATE_0);
142 table.get_table_values.emplace_back(&get_and_rotate_values_from_key<4, 0>);
143 }
144 return table;
145}
146
147} // namespace bb::plookup::uint_tables
#define ASSERT(expression,...)
Definition assert.hpp:49
constexpr uint64_t rotate64(const uint64_t value, const uint64_t rotation)
Definition rotate.hpp:13
BasicTable generate_xor_rotate_table(BasicTableId id, const size_t table_index)
Definition uint.hpp:22
BasicTable generate_and_rotate_table(BasicTableId id, const size_t table_index)
Definition uint.hpp:54
MultiTable get_uint_xor_table(const MultiTableId id)
Definition uint.hpp:79
std::array< bb::fr, 2 > get_and_rotate_values_from_key(const std::array< uint64_t, 2 > key)
Definition uint.hpp:48
std::array< bb::fr, 2 > get_xor_rotate_values_from_key(const std::array< uint64_t, 2 > key)
Definition uint.hpp:16
MultiTable get_uint_and_table(const MultiTableId id)
Definition uint.hpp:113
@ UINT_AND_SLICE_6_ROTATE_0
Definition types.hpp:44
@ UINT_AND_SLICE_4_ROTATE_0
Definition types.hpp:46
@ UINT_XOR_SLICE_4_ROTATE_0
Definition types.hpp:43
@ UINT_XOR_SLICE_6_ROTATE_0
Definition types.hpp:41
@ UINT_AND_SLICE_2_ROTATE_0
Definition types.hpp:45
@ UINT_XOR_SLICE_2_ROTATE_0
Definition types.hpp:42
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