diff --git a/barretenberg/cpp/src/barretenberg/circuit_checker/ultra_circuit_checker.cpp b/barretenberg/cpp/src/barretenberg/circuit_checker/ultra_circuit_checker.cpp index 55e7d3f44a1..1ad994064a0 100644 --- a/barretenberg/cpp/src/barretenberg/circuit_checker/ultra_circuit_checker.cpp +++ b/barretenberg/cpp/src/barretenberg/circuit_checker/ultra_circuit_checker.cpp @@ -25,7 +25,7 @@ template bool UltraCircuitChecker::check(const Builder& build LookupHashTable lookup_hash_table; for (const auto& table : builder.lookup_tables) { const FF table_index(table.table_index); - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table.size(); ++i) { lookup_hash_table.insert({ table.column_1[i], table.column_2[i], table.column_3[i], table_index }); } } diff --git a/barretenberg/cpp/src/barretenberg/plonk_honk_shared/composer/composer_lib.hpp b/barretenberg/cpp/src/barretenberg/plonk_honk_shared/composer/composer_lib.hpp index 06a901d7425..7745a853c58 100644 --- a/barretenberg/cpp/src/barretenberg/plonk_honk_shared/composer/composer_lib.hpp +++ b/barretenberg/cpp/src/barretenberg/plonk_honk_shared/composer/composer_lib.hpp @@ -26,7 +26,7 @@ void construct_lookup_table_polynomials(RefArray for (const auto& table : circuit.lookup_tables) { const fr table_index(table.table_index); - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table.size(); ++i) { table_polynomials[0][offset] = table.column_1[i]; table_polynomials[1][offset] = table.column_2[i]; table_polynomials[2][offset] = table.column_3[i]; @@ -67,7 +67,7 @@ std::array construct_sorted_list_polynomials(typ for (auto& table : circuit.lookup_tables) { const fr table_index(table.table_index); auto& lookup_gates = table.lookup_gates; - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table.size(); ++i) { if (table.use_twin_keys) { lookup_gates.push_back({ { diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/keccak/keccak.cpp b/barretenberg/cpp/src/barretenberg/stdlib/hash/keccak/keccak.cpp index 5ac843e6b8d..ab653fadfb7 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/hash/keccak/keccak.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/keccak/keccak.cpp @@ -126,7 +126,7 @@ field_t keccak::normalize_and_rotate(const field_ct& limb, fie // We need to provide a key/value object for this lookup in order for the Builder // to compute the plookup sorted list commitment const auto [input_quotient, input_slice] = input.divmod(divisor); - lookup.key_entries.push_back( + lookup.lookup_entries.push_back( { { static_cast(input_slice), 0 }, { normalized_slice, normalized_msb } }); // reduce the input and output by 11^{bit_slice} diff --git a/barretenberg/cpp/src/barretenberg/stdlib/primitives/plookup/plookup.cpp b/barretenberg/cpp/src/barretenberg/stdlib/primitives/plookup/plookup.cpp index 94d8bf089fd..375211e12a0 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/primitives/plookup/plookup.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/primitives/plookup/plookup.cpp @@ -77,7 +77,7 @@ field_t plookup_read::read_from_2_to_1_table(const MultiTableI const field_t& key_a, const field_t& key_b) { - const auto lookup = get_lookup_accumulators(id, key_a, key_b, true); + const auto lookup = get_lookup_accumulators(id, key_a, key_b, /*is_2_to_1_lookup=*/true); return lookup[ColumnIdx::C3][0]; } diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/aes128.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/aes128.hpp index f3102bbfd18..4dd529fb978 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/aes128.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/aes128.hpp @@ -25,9 +25,9 @@ inline BasicTable generate_aes_sparse_table(BasicTableId id, const size_t table_ BasicTable table; table.id = id; table.table_index = table_index; - table.size = 256; + size_t table_size = 256; table.use_twin_keys = true; - for (uint64_t i = 0; i < table.size; ++i) { + for (uint64_t i = 0; i < table_size; ++i) { uint64_t left = i; const auto right = numeric::map_into_sparse_form((uint8_t)i); table.column_1.emplace_back(bb::fr(left)); @@ -74,7 +74,6 @@ inline BasicTable generate_aes_sparse_normalization_table(BasicTableId id, const } } } - table.size = table.column_1.size(); table.use_twin_keys = false; table.get_values_from_key = &get_aes_sparse_normalization_values_from_key; @@ -102,7 +101,7 @@ inline MultiTable get_aes_normalization_table(const MultiTableId id = AES_NORMAL table.id = id; for (size_t i = 0; i < num_entries; ++i) { table.slice_sizes.emplace_back(AES_BASE * AES_BASE * AES_BASE * AES_BASE); - table.lookup_ids.emplace_back(AES_SPARSE_NORMALIZE); + table.basic_table_ids.emplace_back(AES_SPARSE_NORMALIZE); table.get_table_values.emplace_back(&get_aes_sparse_normalization_values_from_key); } return table; @@ -117,7 +116,7 @@ inline MultiTable get_aes_input_table(const MultiTableId id = AES_INPUT) table.id = id; for (size_t i = 0; i < num_entries; ++i) { table.slice_sizes.emplace_back(256); - table.lookup_ids.emplace_back(AES_SPARSE_MAP); + table.basic_table_ids.emplace_back(AES_SPARSE_MAP); table.get_table_values.emplace_back(&sparse_tables::get_sparse_table_with_rotation_values); } return table; @@ -137,9 +136,9 @@ inline BasicTable generate_aes_sbox_table(BasicTableId id, const size_t table_in BasicTable table; table.id = id; table.table_index = table_index; - table.size = 256; + size_t table_size = 256; table.use_twin_keys = false; - for (uint64_t i = 0; i < table.size; ++i) { + for (uint64_t i = 0; i < table_size; ++i) { const auto first = numeric::map_into_sparse_form((uint8_t)i); uint8_t sbox_value = crypto::aes128_sbox[(uint8_t)i]; uint8_t swizzled = ((uint8_t)(sbox_value << 1) ^ (uint8_t)(((sbox_value >> 7) & 1) * 0x1b)); @@ -167,7 +166,7 @@ inline MultiTable get_aes_sbox_table(const MultiTableId id = AES_SBOX) table.id = id; for (size_t i = 0; i < num_entries; ++i) { table.slice_sizes.emplace_back(numeric::pow64(AES_BASE, 8)); - table.lookup_ids.emplace_back(AES_SBOX_MAP); + table.basic_table_ids.emplace_back(AES_SBOX_MAP); table.get_table_values.emplace_back(&get_aes_sbox_values_from_key); } return table; diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/blake2s.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/blake2s.hpp index 10a56396e6b..204b830b945 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/blake2s.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/blake2s.hpp @@ -35,7 +35,6 @@ inline BasicTable generate_xor_rotate_table(BasicTableId id, const size_t table_ BasicTable table; table.id = id; table.table_index = table_index; - table.size = base * base; table.use_twin_keys = true; for (uint64_t i = 0; i < base; ++i) { @@ -95,12 +94,12 @@ inline MultiTable get_blake2s_xor_table(const MultiTableId id = BLAKE_XOR) table.id = id; for (size_t i = 0; i < num_entries - 1; ++i) { table.slice_sizes.emplace_back(base); - table.lookup_ids.emplace_back(BLAKE_XOR_ROTATE0); + table.basic_table_ids.emplace_back(BLAKE_XOR_ROTATE0); table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>); } table.slice_sizes.emplace_back(SIZE_OF_LAST_SLICE); - table.lookup_ids.emplace_back(BLAKE_XOR_ROTATE0_SLICE5_MOD4); + table.basic_table_ids.emplace_back(BLAKE_XOR_ROTATE0_SLICE5_MOD4); table.get_table_values.emplace_back(&get_xor_rotate_values_from_key); return table; @@ -128,8 +127,8 @@ inline MultiTable get_blake2s_xor_rotate_16_table(const MultiTableId id = BLAKE_ table.id = id; table.slice_sizes = { base, base, base, base, base, SIZE_OF_LAST_SLICE }; - table.lookup_ids = { BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE4, - BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE0_SLICE5_MOD4 }; + table.basic_table_ids = { BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE4, + BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE0_SLICE5_MOD4 }; table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>); table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>); @@ -163,8 +162,8 @@ inline MultiTable get_blake2s_xor_rotate_8_table(const MultiTableId id = BLAKE_X table.id = id; table.slice_sizes = { base, base, base, base, base, SIZE_OF_LAST_SLICE }; - table.lookup_ids = { BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE2, BLAKE_XOR_ROTATE0, - BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE0_SLICE5_MOD4 }; + table.basic_table_ids = { BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE2, BLAKE_XOR_ROTATE0, + BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE0_SLICE5_MOD4 }; table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>); table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 2>); @@ -198,8 +197,8 @@ inline MultiTable get_blake2s_xor_rotate_7_table(const MultiTableId id = BLAKE_X table.id = id; table.slice_sizes = { base, base, base, base, base, SIZE_OF_LAST_SLICE }; - table.lookup_ids = { BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE1, BLAKE_XOR_ROTATE0, - BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE0_SLICE5_MOD4 }; + table.basic_table_ids = { BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE1, BLAKE_XOR_ROTATE0, + BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE0, BLAKE_XOR_ROTATE0_SLICE5_MOD4 }; table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>); table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 1>); diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/dummy.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/dummy.hpp index f49409c9bbc..cc3b443ca09 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/dummy.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/dummy.hpp @@ -49,7 +49,6 @@ inline BasicTable generate_honk_dummy_table(const BasicTableId id, const size_t BasicTable table; table.id = id; table.table_index = table_index; - table.size = base * base; table.use_twin_keys = true; for (uint64_t i = 0; i < base; ++i) { for (uint64_t j = 0; j < base; ++j) { @@ -85,10 +84,10 @@ inline MultiTable get_honk_dummy_multitable() number_of_lookups); table.id = id; table.slice_sizes.emplace_back(number_of_elements_in_argument); - table.lookup_ids.emplace_back(HONK_DUMMY_BASIC1); + table.basic_table_ids.emplace_back(HONK_DUMMY_BASIC1); table.get_table_values.emplace_back(&get_value_from_key); table.slice_sizes.emplace_back(number_of_elements_in_argument); - table.lookup_ids.emplace_back(HONK_DUMMY_BASIC2); + table.basic_table_ids.emplace_back(HONK_DUMMY_BASIC2); table.get_table_values.emplace_back(&get_value_from_key); return table; } diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/fixed_base/fixed_base.cpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/fixed_base/fixed_base.cpp index 0feae681d45..edd9cce6b13 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/fixed_base/fixed_base.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/fixed_base/fixed_base.cpp @@ -197,12 +197,11 @@ BasicTable table::generate_basic_fixed_base_table(BasicTableId id, size_t basic_ BasicTable table; table.id = id; table.table_index = basic_table_index; - table.size = table_size; table.use_twin_keys = false; const auto& basic_table = fixed_base_tables[multitable_index][table_index]; - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table_size; ++i) { table.column_1.emplace_back(i); table.column_2.emplace_back(basic_table[i].x); table.column_3.emplace_back(basic_table[i].y); @@ -213,7 +212,7 @@ BasicTable table::generate_basic_fixed_base_table(BasicTableId id, size_t basic_ table.get_values_from_key = get_values_from_key_table[multitable_index][table_index]; ASSERT(table.get_values_from_key != nullptr); - table.column_1_step_size = table.size; + table.column_1_step_size = table_size; table.column_2_step_size = 0; table.column_3_step_size = 0; @@ -243,13 +242,13 @@ template MultiTable table::get_fixed_ MultiTable table(MAX_TABLE_SIZE, 0, 0, NUM_TABLES); table.id = id; table.get_table_values.resize(NUM_TABLES); - table.lookup_ids.resize(NUM_TABLES); + table.basic_table_ids.resize(NUM_TABLES); for (size_t i = 0; i < NUM_TABLES; ++i) { table.slice_sizes.emplace_back(MAX_TABLE_SIZE); table.get_table_values[i] = get_values_from_key_table[multitable_index][i]; static_assert(multitable_index < NUM_FIXED_BASE_MULTI_TABLES); size_t idx = i + static_cast(basic_table_ids[multitable_index]); - table.lookup_ids[i] = static_cast(idx); + table.basic_table_ids[i] = static_cast(idx); } return table; } diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_chi.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_chi.hpp index 0e5b4a8a5c3..4345112a713 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_chi.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_chi.hpp @@ -169,11 +169,11 @@ class Chi { table.id = id; table.table_index = table_index; table.use_twin_keys = false; - table.size = numeric::pow64(static_cast(EFFECTIVE_BASE), TABLE_BITS); + auto table_size = numeric::pow64(static_cast(EFFECTIVE_BASE), TABLE_BITS); std::array counts{}; std::array column_values{ 0, 0, 0 }; - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table_size; ++i) { table.column_1.emplace_back(column_values[0]); table.column_2.emplace_back(column_values[1]); table.column_3.emplace_back(column_values[2]); @@ -242,7 +242,7 @@ class Chi { table.id = id; for (size_t i = 0; i < num_tables_per_multitable; ++i) { table.slice_sizes.emplace_back(numeric::pow64(BASE, TABLE_BITS)); - table.lookup_ids.emplace_back(KECCAK_CHI); + table.basic_table_ids.emplace_back(KECCAK_CHI); table.get_table_values.emplace_back(&get_chi_renormalization_values); } return table; diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_input.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_input.hpp index c3b6d20ae98..f03e00bcc47 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_input.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_input.hpp @@ -57,11 +57,11 @@ class KeccakInput { BasicTable table; table.id = id; table.table_index = table_index; - table.size = (1U << TABLE_BITS); + auto table_size = (1U << TABLE_BITS); table.use_twin_keys = false; constexpr size_t msb_shift = (64 % TABLE_BITS == 0) ? TABLE_BITS - 1 : (64 % TABLE_BITS) - 1; - for (uint64_t i = 0; i < table.size; ++i) { + for (uint64_t i = 0; i < table_size; ++i) { const uint64_t source = i; const auto target = numeric::map_into_sparse_form(source); table.column_1.emplace_back(bb::fr(source)); @@ -132,7 +132,7 @@ class KeccakInput { table.id = id; for (size_t i = 0; i < num_entries; ++i) { table.slice_sizes.emplace_back(1 << 8); - table.lookup_ids.emplace_back(KECCAK_INPUT); + table.basic_table_ids.emplace_back(KECCAK_INPUT); table.get_table_values.emplace_back(&get_keccak_input_values); } return table; diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_output.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_output.hpp index 53f2fc9a53f..d5160f21a7c 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_output.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_output.hpp @@ -91,12 +91,12 @@ class KeccakOutput { table.id = id; table.table_index = table_index; table.use_twin_keys = false; - table.size = numeric::pow64(static_cast(EFFECTIVE_BASE), TABLE_BITS); + auto table_size = numeric::pow64(static_cast(EFFECTIVE_BASE), TABLE_BITS); std::array counts{}; std::array column_values{ 0, 0 }; - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table_size; ++i) { table.column_1.emplace_back(column_values[0]); table.column_2.emplace_back(column_values[1]); table.column_3.emplace_back(0); @@ -162,7 +162,7 @@ class KeccakOutput { table.id = id; for (size_t i = 0; i < num_tables_per_multitable; ++i) { table.slice_sizes.emplace_back(numeric::pow64(BASE, TABLE_BITS)); - table.lookup_ids.emplace_back(KECCAK_OUTPUT); + table.basic_table_ids.emplace_back(KECCAK_OUTPUT); table.get_table_values.emplace_back( &sparse_tables::get_sparse_normalization_values); } diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_rho.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_rho.hpp index c16085aca8a..5d548e70cbb 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_rho.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_rho.hpp @@ -170,12 +170,12 @@ template class Rho { table.id = id; table.table_index = table_index; table.use_twin_keys = false; - table.size = numeric::pow64(static_cast(EFFECTIVE_BASE), TABLE_BITS); + auto table_size = numeric::pow64(static_cast(EFFECTIVE_BASE), TABLE_BITS); std::array counts{}; std::array column_values{ 0, 0, 0 }; - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table_size; ++i) { table.column_1.emplace_back(column_values[0]); table.column_2.emplace_back(column_values[1]); table.column_3.emplace_back(column_values[2]); @@ -264,7 +264,7 @@ template class Rho { table.slice_sizes.push_back(scaled_base); table.get_table_values.emplace_back(&get_rho_renormalization_values); - table.lookup_ids.push_back((BasicTableId)((size_t)KECCAK_RHO_1 + (bit_slice - 1))); + table.basic_table_ids.push_back((BasicTableId)((size_t)KECCAK_RHO_1 + (bit_slice - 1))); }); // generate table selector values for the 'left' slice @@ -284,7 +284,7 @@ template class Rho { table.slice_sizes.push_back(scaled_base); table.get_table_values.emplace_back(&get_rho_renormalization_values); - table.lookup_ids.push_back((BasicTableId)((size_t)KECCAK_RHO_1 + (bit_slice - 1))); + table.basic_table_ids.push_back((BasicTableId)((size_t)KECCAK_RHO_1 + (bit_slice - 1))); }); return table; diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_theta.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_theta.hpp index 9f05cf94267..41c0cc8608f 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_theta.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/keccak/keccak_theta.hpp @@ -171,12 +171,12 @@ class Theta { table.id = id; table.table_index = table_index; table.use_twin_keys = false; - table.size = numeric::pow64(static_cast(BASE), TABLE_BITS); + auto table_size = numeric::pow64(static_cast(BASE), TABLE_BITS); std::array counts{}; std::array column_values{ 0, 0 }; - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table_size; ++i) { table.column_1.emplace_back(column_values[0]); table.column_2.emplace_back(column_values[1]); table.column_3.emplace_back(0); @@ -244,7 +244,7 @@ class Theta { table.id = id; for (size_t i = 0; i < num_tables_per_multitable; ++i) { table.slice_sizes.emplace_back(numeric::pow64(BASE, TABLE_BITS)); - table.lookup_ids.emplace_back(KECCAK_THETA); + table.basic_table_ids.emplace_back(KECCAK_THETA); table.get_table_values.emplace_back(&get_theta_renormalization_values); } return table; diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/non_native_group_generator.cpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/non_native_group_generator.cpp index ed172573e24..2031e949779 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/non_native_group_generator.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/non_native_group_generator.cpp @@ -182,10 +182,10 @@ template BasicTable ecc_generator_table::generate_xlo_table(Ba BasicTable table; table.id = id; table.table_index = table_index; - table.size = 256; + size_t table_size = 256; table.use_twin_keys = false; - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table_size; ++i) { table.column_1.emplace_back((i)); table.column_2.emplace_back(ecc_generator_table::generator_xlo_table[i].first); table.column_3.emplace_back(ecc_generator_table::generator_xlo_table[i].second); @@ -205,10 +205,10 @@ template BasicTable ecc_generator_table::generate_xhi_table(Ba BasicTable table; table.id = id; table.table_index = table_index; - table.size = 256; + size_t table_size = 256; table.use_twin_keys = false; - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table_size; ++i) { table.column_1.emplace_back((i)); table.column_2.emplace_back(ecc_generator_table::generator_xhi_table[i].first); table.column_3.emplace_back(ecc_generator_table::generator_xhi_table[i].second); @@ -229,10 +229,10 @@ BasicTable ecc_generator_table::generate_xlo_endo_table(BasicTableId id, con BasicTable table; table.id = id; table.table_index = table_index; - table.size = 256; + size_t table_size = 256; table.use_twin_keys = false; - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table_size; ++i) { table.column_1.emplace_back((i)); table.column_2.emplace_back(ecc_generator_table::generator_endo_xlo_table[i].first); table.column_3.emplace_back(ecc_generator_table::generator_endo_xlo_table[i].second); @@ -253,10 +253,10 @@ BasicTable ecc_generator_table::generate_xhi_endo_table(BasicTableId id, con BasicTable table; table.id = id; table.table_index = table_index; - table.size = 256; + size_t table_size = 256; table.use_twin_keys = false; - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table_size; ++i) { table.column_1.emplace_back((i)); table.column_2.emplace_back(ecc_generator_table::generator_endo_xhi_table[i].first); table.column_3.emplace_back(ecc_generator_table::generator_endo_xhi_table[i].second); @@ -276,10 +276,10 @@ template BasicTable ecc_generator_table::generate_ylo_table(Ba BasicTable table; table.id = id; table.table_index = table_index; - table.size = 256; + size_t table_size = 256; table.use_twin_keys = false; - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table_size; ++i) { table.column_1.emplace_back((i)); table.column_2.emplace_back(ecc_generator_table::generator_ylo_table[i].first); table.column_3.emplace_back(ecc_generator_table::generator_ylo_table[i].second); @@ -299,10 +299,10 @@ template BasicTable ecc_generator_table::generate_yhi_table(Ba BasicTable table; table.id = id; table.table_index = table_index; - table.size = 256; + size_t table_size = 256; table.use_twin_keys = false; - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table_size; ++i) { table.column_1.emplace_back((i)); table.column_2.emplace_back(ecc_generator_table::generator_yhi_table[i].first); table.column_3.emplace_back(ecc_generator_table::generator_yhi_table[i].second); @@ -323,10 +323,10 @@ BasicTable ecc_generator_table::generate_xyprime_table(BasicTableId id, cons BasicTable table; table.id = id; table.table_index = table_index; - table.size = 256; + size_t table_size = 256; table.use_twin_keys = false; - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table_size; ++i) { table.column_1.emplace_back((i)); table.column_2.emplace_back(ecc_generator_table::generator_xyprime_table[i].first); table.column_3.emplace_back(ecc_generator_table::generator_xyprime_table[i].second); @@ -347,10 +347,10 @@ BasicTable ecc_generator_table::generate_xyprime_endo_table(BasicTableId id, BasicTable table; table.id = id; table.table_index = table_index; - table.size = 256; + size_t table_size = 256; table.use_twin_keys = false; - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table_size; ++i) { table.column_1.emplace_back((i)); table.column_2.emplace_back(ecc_generator_table::generator_endo_xyprime_table[i].first); table.column_3.emplace_back(ecc_generator_table::generator_endo_xyprime_table[i].second); @@ -374,7 +374,7 @@ MultiTable ecc_generator_table::get_xlo_table(const MultiTableId id, const B table.id = id; for (size_t i = 0; i < num_entries; ++i) { table.slice_sizes.emplace_back(512); - table.lookup_ids.emplace_back(basic_id); + table.basic_table_ids.emplace_back(basic_id); table.get_table_values.emplace_back(&get_xlo_values); } return table; @@ -389,7 +389,7 @@ MultiTable ecc_generator_table::get_xhi_table(const MultiTableId id, const B table.id = id; for (size_t i = 0; i < num_entries; ++i) { table.slice_sizes.emplace_back(512); - table.lookup_ids.emplace_back(basic_id); + table.basic_table_ids.emplace_back(basic_id); table.get_table_values.emplace_back(&get_xhi_values); } return table; @@ -404,7 +404,7 @@ MultiTable ecc_generator_table::get_xlo_endo_table(const MultiTableId id, co table.id = id; for (size_t i = 0; i < num_entries; ++i) { table.slice_sizes.emplace_back(512); - table.lookup_ids.emplace_back(basic_id); + table.basic_table_ids.emplace_back(basic_id); table.get_table_values.emplace_back(&get_xlo_endo_values); } return table; @@ -419,7 +419,7 @@ MultiTable ecc_generator_table::get_xhi_endo_table(const MultiTableId id, co table.id = id; for (size_t i = 0; i < num_entries; ++i) { table.slice_sizes.emplace_back(512); - table.lookup_ids.emplace_back(basic_id); + table.basic_table_ids.emplace_back(basic_id); table.get_table_values.emplace_back(&get_xhi_endo_values); } return table; @@ -434,7 +434,7 @@ MultiTable ecc_generator_table::get_ylo_table(const MultiTableId id, const B table.id = id; for (size_t i = 0; i < num_entries; ++i) { table.slice_sizes.emplace_back(512); - table.lookup_ids.emplace_back(basic_id); + table.basic_table_ids.emplace_back(basic_id); table.get_table_values.emplace_back(&get_ylo_values); } return table; @@ -449,7 +449,7 @@ MultiTable ecc_generator_table::get_yhi_table(const MultiTableId id, const B table.id = id; for (size_t i = 0; i < num_entries; ++i) { table.slice_sizes.emplace_back(512); - table.lookup_ids.emplace_back(basic_id); + table.basic_table_ids.emplace_back(basic_id); table.get_table_values.emplace_back(&get_yhi_values); } return table; @@ -464,7 +464,7 @@ MultiTable ecc_generator_table::get_xyprime_table(const MultiTableId id, con table.id = id; for (size_t i = 0; i < num_entries; ++i) { table.slice_sizes.emplace_back(512); - table.lookup_ids.emplace_back(basic_id); + table.basic_table_ids.emplace_back(basic_id); table.get_table_values.emplace_back(&get_xyprime_values); } return table; @@ -479,7 +479,7 @@ MultiTable ecc_generator_table::get_xyprime_endo_table(const MultiTableId id table.id = id; for (size_t i = 0; i < num_entries; ++i) { table.slice_sizes.emplace_back(512); - table.lookup_ids.emplace_back(basic_id); + table.basic_table_ids.emplace_back(basic_id); table.get_table_values.emplace_back(&get_xyprime_endo_values); } return table; diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.cpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.cpp index 73e3814cba5..b897fc8c309 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.cpp @@ -26,7 +26,7 @@ std::array MULTI_TABLES; bool initialised = false; #ifndef NO_MULTITHREADING -// The multitables initialisation procedure is not thread-sage, so we need to make sure only 1 thread gets to initialize +// The multitables initialisation procedure is not thread-safe, so we need to make sure only 1 thread gets to initialize // them. std::mutex multi_table_mutex; #endif @@ -123,7 +123,16 @@ void init_multi_tables() initialised = true; } } // namespace -const MultiTable& create_table(const MultiTableId id) +/** + * @brief Return the multitable with the provided ID; construct all MultiTables if not constructed already + * @details The multitables are relatively light objects (they do not themselves store raw table data) so the first time + * we use one of them we simply construct all of them (regardless of which of them will actually be used) and store in + * MULTI_TABLES array. + * + * @param id The index of a MultiTable in the MULTI_TABLES array + * @return const MultiTable& + */ +const MultiTable& get_multitable(const MultiTableId id) { if (!initialised) { init_multi_tables(); @@ -132,14 +141,31 @@ const MultiTable& create_table(const MultiTableId id) return MULTI_TABLES[id]; } +/** + * @brief Given a table ID and the key(s) for a key-value lookup, return the lookup accumulators + * @details In general the number of bits in key/value is greater than what can be efficiently supported in lookup + * tables. For this reason we actually perform lookups on the corresponding limbs. However, since we're interested in + * the full values and not the limbs, its convenient to structure the witnesses of lookup gates to store the former. + * This way we don't have to waste gates reaccumulating the limbs to compute the actual value of interest. The way to do + * this is to populate the wires with 'accumulator' values such that the first gate in the series contains the full + * accumulated values, and successive gates contain prior stages of the accumulator such that wire_i - r*wire_{i-1} = + * v_i, where r = num limb bits and v_i is a limb that explicitly appears in one of the lookup tables. See the detailed + * comment block below for more explanation. + * + * @param id + * @param key_a + * @param key_b + * @param is_2_to_1_lookup + * @return ReadData + */ ReadData get_lookup_accumulators(const MultiTableId id, const fr& key_a, const fr& key_b, const bool is_2_to_1_lookup) { // return multi-table, populating global array of all multi-tables if need be - const auto& multi_table = create_table(id); - const size_t num_lookups = multi_table.lookup_ids.size(); + const auto& multi_table = get_multitable(id); + const size_t num_lookups = multi_table.basic_table_ids.size(); ReadData lookup; const auto key_a_slices = numeric::slice_input_using_variable_bases(key_a, multi_table.slice_sizes); @@ -150,21 +176,21 @@ ReadData get_lookup_accumulators(const MultiTableId id, std::vector column_3_raw_values; for (size_t i = 0; i < num_lookups; ++i) { - // get i-th table query function and then submit query + // compute the value(s) corresponding to the key(s) using on the i-th basic table query function const auto values = multi_table.get_table_values[i]({ key_a_slices[i], key_b_slices[i] }); // store all query data in raw columns and key entry column_1_raw_values.emplace_back(key_a_slices[i]); column_2_raw_values.emplace_back(is_2_to_1_lookup ? key_b_slices[i] : values[0]); column_3_raw_values.emplace_back(is_2_to_1_lookup ? values[0] : values[1]); - // Question: why are we storing the key slices twice? - const BasicTable::KeyEntry key_entry{ { key_a_slices[i], key_b_slices[i] }, values }; - lookup.key_entries.emplace_back(key_entry); + // Store the lookup entries for use in constructing the sorted table/lookup polynomials later on + const BasicTable::LookupEntry lookup_entry{ { key_a_slices[i], key_b_slices[i] }, values }; + lookup.lookup_entries.emplace_back(lookup_entry); } - lookup[ColumnIdx::C1].resize(num_lookups); - lookup[ColumnIdx::C2].resize(num_lookups); - lookup[ColumnIdx::C3].resize(num_lookups); + lookup[C1].resize(num_lookups); + lookup[C2].resize(num_lookups); + lookup[C3].resize(num_lookups); /** * A multi-table consists of multiple basic tables (say L = 6). @@ -178,8 +204,9 @@ ReadData get_lookup_accumulators(const MultiTableId id, * s1 s2 s3 s4 s5 s6 * * Note that different basic tables can be of different sizes. Every lookup query generates L output slices (one for - * each basic table, here, s1, s2, ..., s6). In other words, every lookup query add L lookup gates to the program. - * Let the input slices/keys be (a1, b1), (a2, b2), ..., (a6, b6). The lookup gate structure is as follows: + * each basic table, here, s1, s2, ..., s6). In other words, every lookup query adds L lookup gates to the program. + * For example, to look up the XOR of 32-bit inputs, we actually perform 6 individual lookups on the 6-bit XOR basic + * table. Let the input slices/keys be (a1, b1), (a2, b2), ..., (a6, b6). The lookup gate structure is as follows: * * +---+-----------------------------------+----------------------------------+-----------------------------------+ * | s | key_a | key_b | output | @@ -193,34 +220,25 @@ ReadData get_lookup_accumulators(const MultiTableId id, * +---+-----------------------------------+----------------------------------+-----------------------------------+ * * Note that we compute the accumulating sums of the slices so as to avoid using additonal gates for the purpose of - * reconstructing inputs/outputs. Here, (p, q, r) are referred to as column coefficients/step sizes. - * In the next few lines, we compute these accumulating sums from raw column values (a1, ..., a6), (b1, ..., b6), - * (s1, ..., s6) and column coefficients (p, q, r). + * reconstructing the original inputs/outputs. I.e. the output value at the 0th index in the above table is the + * actual value we were interested in computing in the first place. Importantly, the structure of the remaining rows + * is such that row_i - r*row_{i+1} produces an entry {a_j, b_j, s_j} that exactly corresponds to an entry in a + * BasicTable. This is what gives rise to the wire_i - scalar*wire_i_shift structure in the lookup relation. Here, + * (p, q, r) are referred to as column coefficients/step sizes. In the next few lines, we compute these accumulating + * sums from raw column values (a1, ..., a6), (b1, ..., b6), (s1, ..., s6) and column coefficients (p, q, r). * * For more details: see * https://app.gitbook.com/o/-LgCgJ8TCO7eGlBr34fj/s/-MEwtqp3H6YhHUTQ_pVJ/plookup-gates-for-ultraplonk/lookup-table-structures * */ - lookup[ColumnIdx::C1][num_lookups - 1] = column_1_raw_values[num_lookups - 1]; - lookup[ColumnIdx::C2][num_lookups - 1] = column_2_raw_values[num_lookups - 1]; - lookup[ColumnIdx::C3][num_lookups - 1] = column_3_raw_values[num_lookups - 1]; + lookup[C1][num_lookups - 1] = column_1_raw_values[num_lookups - 1]; + lookup[C2][num_lookups - 1] = column_2_raw_values[num_lookups - 1]; + lookup[C3][num_lookups - 1] = column_3_raw_values[num_lookups - 1]; - for (size_t i = 1; i < num_lookups; ++i) { - const auto& previous_1 = lookup[ColumnIdx::C1][num_lookups - i]; - const auto& previous_2 = lookup[ColumnIdx::C2][num_lookups - i]; - const auto& previous_3 = lookup[ColumnIdx::C3][num_lookups - i]; - - auto& current_1 = lookup[ColumnIdx::C1][num_lookups - 1 - i]; - auto& current_2 = lookup[ColumnIdx::C2][num_lookups - 1 - i]; - auto& current_3 = lookup[ColumnIdx::C3][num_lookups - 1 - i]; - - const auto& raw_1 = column_1_raw_values[num_lookups - 1 - i]; - const auto& raw_2 = column_2_raw_values[num_lookups - 1 - i]; - const auto& raw_3 = column_3_raw_values[num_lookups - 1 - i]; - - current_1 = raw_1 + previous_1 * multi_table.column_1_step_sizes[num_lookups - i]; - current_2 = raw_2 + previous_2 * multi_table.column_2_step_sizes[num_lookups - i]; - current_3 = raw_3 + previous_3 * multi_table.column_3_step_sizes[num_lookups - i]; + for (size_t i = num_lookups - 1; i > 0; --i) { + lookup[C1][i - 1] = column_1_raw_values[i - 1] + lookup[C1][i] * multi_table.column_1_step_sizes[i]; + lookup[C2][i - 1] = column_2_raw_values[i - 1] + lookup[C2][i] * multi_table.column_2_step_sizes[i]; + lookup[C3][i - 1] = column_3_raw_values[i - 1] + lookup[C3][i] * multi_table.column_3_step_sizes[i]; } return lookup; } diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.hpp index 504a4ba97b8..5b598f10a1f 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.hpp @@ -4,7 +4,7 @@ namespace bb::plookup { -const MultiTable& create_table(MultiTableId id); +const MultiTable& get_multitable(MultiTableId id); ReadData get_lookup_accumulators(MultiTableId id, const bb::fr& key_a, diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/sha256.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/sha256.hpp index a63b94cd20d..d702c5c24c9 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/sha256.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/sha256.hpp @@ -116,7 +116,7 @@ inline MultiTable get_witness_extension_output_table(const MultiTableId id = SHA table.id = id; for (size_t i = 0; i < num_entries; ++i) { table.slice_sizes.emplace_back(numeric::pow64(16, 3)); - table.lookup_ids.emplace_back(SHA256_WITNESS_NORMALIZE); + table.basic_table_ids.emplace_back(SHA256_WITNESS_NORMALIZE); table.get_table_values.emplace_back( &sparse_tables::get_sparse_normalization_values<16, witness_extension_normalization_table>); } @@ -132,7 +132,7 @@ inline MultiTable get_choose_output_table(const MultiTableId id = SHA256_CH_OUTP table.id = id; for (size_t i = 0; i < num_entries; ++i) { table.slice_sizes.emplace_back(numeric::pow64(28, 2)); - table.lookup_ids.emplace_back(SHA256_CH_NORMALIZE); + table.basic_table_ids.emplace_back(SHA256_CH_NORMALIZE); table.get_table_values.emplace_back( &sparse_tables::get_sparse_normalization_values<28, choose_normalization_table>); } @@ -148,7 +148,7 @@ inline MultiTable get_majority_output_table(const MultiTableId id = SHA256_MAJ_O table.id = id; for (size_t i = 0; i < num_entries; ++i) { table.slice_sizes.emplace_back(numeric::pow64(16, 3)); - table.lookup_ids.emplace_back(SHA256_MAJ_NORMALIZE); + table.basic_table_ids.emplace_back(SHA256_MAJ_NORMALIZE); table.get_table_values.emplace_back( &sparse_tables::get_sparse_normalization_values<16, majority_normalization_table>); } @@ -223,10 +223,10 @@ inline MultiTable get_witness_extension_input_table(const MultiTableId id = SHA2 MultiTable table(column_1_coefficients, column_2_coefficients, column_3_coefficients); table.id = id; table.slice_sizes = { (1 << 3), (1 << 7), (1 << 8), (1 << 18) }; - table.lookup_ids = { SHA256_WITNESS_SLICE_3, - SHA256_WITNESS_SLICE_7_ROTATE_4, - SHA256_WITNESS_SLICE_8_ROTATE_7, - SHA256_WITNESS_SLICE_14_ROTATE_1 }; + table.basic_table_ids = { SHA256_WITNESS_SLICE_3, + SHA256_WITNESS_SLICE_7_ROTATE_4, + SHA256_WITNESS_SLICE_8_ROTATE_7, + SHA256_WITNESS_SLICE_14_ROTATE_1 }; table.get_table_values = { &sparse_tables::get_sparse_table_with_rotation_values<16, 0>, @@ -321,7 +321,7 @@ inline MultiTable get_choose_input_table(const MultiTableId id = SHA256_CH_INPUT MultiTable table(column_1_coefficients, column_2_coefficients, column_3_coefficients); table.id = id; table.slice_sizes = { (1 << 11), (1 << 11), (1 << 10) }; - table.lookup_ids = { SHA256_BASE28_ROTATE6, SHA256_BASE28, SHA256_BASE28_ROTATE3 }; + table.basic_table_ids = { SHA256_BASE28_ROTATE6, SHA256_BASE28, SHA256_BASE28_ROTATE3 }; table.get_table_values.push_back(&sparse_tables::get_sparse_table_with_rotation_values<28, 6>); table.get_table_values.push_back(&sparse_tables::get_sparse_table_with_rotation_values<28, 0>); @@ -380,7 +380,7 @@ inline MultiTable get_majority_input_table(const MultiTableId id = SHA256_MAJ_IN MultiTable table(column_1_coefficients, column_2_coefficients, column_3_coefficients); table.id = id; table.slice_sizes = { (1 << 11), (1 << 11), (1 << 10) }; - table.lookup_ids = { SHA256_BASE16_ROTATE2, SHA256_BASE16_ROTATE2, SHA256_BASE16 }; + table.basic_table_ids = { SHA256_BASE16_ROTATE2, SHA256_BASE16_ROTATE2, SHA256_BASE16 }; table.get_table_values = { &sparse_tables::get_sparse_table_with_rotation_values<16, 2>, &sparse_tables::get_sparse_table_with_rotation_values<16, 2>, diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/sparse.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/sparse.hpp index 35c858e30a3..2dfdd880ea2 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/sparse.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/sparse.hpp @@ -28,10 +28,10 @@ inline BasicTable generate_sparse_table_with_rotation(BasicTableId id, const siz BasicTable table; table.id = id; table.table_index = table_index; - table.size = (1U << bits_per_slice); + auto table_size = (1U << bits_per_slice); table.use_twin_keys = false; - for (uint64_t i = 0; i < table.size; ++i) { + for (uint64_t i = 0; i < table_size; ++i) { const uint64_t source = i; const auto target = numeric::map_into_sparse_form(source); table.column_1.emplace_back(bb::fr(source)); @@ -89,11 +89,11 @@ inline BasicTable generate_sparse_normalization_table(BasicTableId id, const siz table.id = id; table.table_index = table_index; table.use_twin_keys = false; - table.size = numeric::pow64(static_cast(base), num_bits); + auto table_size = numeric::pow64(static_cast(base), num_bits); numeric::sparse_int accumulator(0); numeric::sparse_int to_add(1); - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table_size; ++i) { const auto& limbs = accumulator.get_limbs(); uint64_t key = 0; for (size_t j = 0; j < num_bits; ++j) { @@ -109,7 +109,7 @@ inline BasicTable generate_sparse_normalization_table(BasicTableId id, const siz table.get_values_from_key = &get_sparse_normalization_values; - table.column_1_step_size = bb::fr(table.size); + table.column_1_step_size = bb::fr(table_size); table.column_2_step_size = bb::fr(((uint64_t)1 << num_bits)); table.column_3_step_size = bb::fr(0); return table; diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/types.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/types.hpp index 0dfcaa916d3..c41d4e94670 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/types.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/types.hpp @@ -121,19 +121,33 @@ enum MultiTableId { NUM_MULTI_TABLES = KECCAK_NORMALIZE_AND_ROTATE + 25, }; +/** + * @brief Container for managing multiple BasicTables plus the data needed to combine basic table outputs (limbs) into + * accumulators. Does not store actual raw table data. + * @details As a simple example, consider using lookups to compute XOR on uint32_t inputs. To do this we decompose the + * inputs into 6 limbs and use a BasicTable for 6-bit XOR lookups. In this case the MultiTable simply manages 6 basic + * tables, all of which are the XOR BasicTable. (In many cases all of the BasicTables managed by a MultiTable are + * identical, however there are some cases where more than 1 type is required, e.g. if a certain limb has to be handled + * differently etc.). This class also stores the scalars needed to reconstruct full values from the components that are + * contained in the basic lookup tables. + * @note Note that a MultiTable does not actually *store* any table data. Rather it stores a set of basic table IDs, the + * methods used to compute the basic table entries, plus some metadata. + * + */ struct MultiTable { // Coefficients are accumulated products of corresponding step sizes until that point std::vector column_1_coefficients; std::vector column_2_coefficients; std::vector column_3_coefficients; MultiTableId id; - std::vector lookup_ids; + std::vector basic_table_ids; std::vector slice_sizes; std::vector column_1_step_sizes; std::vector column_2_step_sizes; std::vector column_3_step_sizes; typedef std::array table_out; typedef std::array table_in; + // Methods for computing the value from a key for each basic table std::vector get_table_values; private: @@ -255,19 +269,22 @@ struct MultiTable { // } /** - * @brief The structure contains the most basic table serving one function (for, example an xor table) + * @brief A basic table from which we can perform lookups (for example, an xor table) + * @details Also stores the lookup gate data for all lookups performed on this table * * @details You can find initialization example at * ../ultra_plonk_composer.cpp#UltraPlonkComposer::initialize_precomputed_table(..) * */ struct BasicTable { - struct KeyEntry { - bool operator==(const KeyEntry& other) const = default; + struct LookupEntry { + bool operator==(const LookupEntry& other) const = default; + // Storage for two key values and two result values to support different lookup formats, i.e. 1:1, 1:2, and 2:1 std::array key{ 0, 0 }; std::array value{ bb::fr(0), bb::fr(0) }; - bool operator<(const KeyEntry& other) const + // Comparison operator required for sorting; Used to construct sorted-concatenated table/lookup polynomial + bool operator<(const LookupEntry& other) const { return key[0] < other.key[0] || ((key[0] == other.key[0]) && key[1] < other.key[1]); } @@ -285,8 +302,6 @@ struct BasicTable { // Unique id of the table which is used to look it up, when we need its functionality. One of BasicTableId enum BasicTableId id; size_t table_index; - // The size of the table - size_t size; // This means that we are using two inputs to look up stuff, not translate a single entry into another one. bool use_twin_keys; @@ -294,13 +309,19 @@ struct BasicTable { bb::fr column_2_step_size = bb::fr(0); bb::fr column_3_step_size = bb::fr(0); std::vector column_1; - std::vector column_3; std::vector column_2; - std::vector lookup_gates; + std::vector column_3; + std::vector lookup_gates; // wire data for all lookup gates created for lookups on this table std::array (*get_values_from_key)(const std::array); bool operator==(const BasicTable& other) const = default; + + size_t size() const + { + ASSERT(column_1.size() == column_2.size() && column_2.size() == column_3.size()); + return column_1.size(); + } }; enum ColumnIdx { C1, C2, C3 }; @@ -324,9 +345,10 @@ template class ReadData { std::vector& operator[](ColumnIdx idx) { return columns[static_cast(idx)]; }; const std::vector& operator[](ColumnIdx idx) const { return columns[static_cast(idx)]; }; - std::vector key_entries; + std::vector lookup_entries; private: + // Container for the lookup accumulators; 0th index of each column contains full accumulated value std::array, 3> columns; }; diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/uint.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/uint.hpp index 1ccfecdd2a5..7ba5d03304a 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/uint.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/uint.hpp @@ -19,7 +19,6 @@ inline BasicTable generate_xor_rotate_table(BasicTableId id, const size_t table_ BasicTable table; table.id = id; table.table_index = table_index; - table.size = base * base; table.use_twin_keys = true; for (uint64_t i = 0; i < base; ++i) { @@ -52,7 +51,6 @@ inline BasicTable generate_and_rotate_table(BasicTableId id, const size_t table_ BasicTable table; table.id = id; table.table_index = table_index; - table.size = base * base; table.use_twin_keys = true; for (uint64_t i = 0; i < base; ++i) { @@ -81,7 +79,7 @@ inline MultiTable get_uint32_xor_table(const MultiTableId id = UINT32_XOR) table.id = id; for (size_t i = 0; i < num_entries; ++i) { table.slice_sizes.emplace_back(base); - table.lookup_ids.emplace_back(UINT_XOR_ROTATE0); + table.basic_table_ids.emplace_back(UINT_XOR_ROTATE0); table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>); } return table; @@ -96,7 +94,7 @@ inline MultiTable get_uint32_and_table(const MultiTableId id = UINT32_AND) table.id = id; for (size_t i = 0; i < num_entries; ++i) { table.slice_sizes.emplace_back(base); - table.lookup_ids.emplace_back(UINT_AND_ROTATE0); + table.basic_table_ids.emplace_back(UINT_AND_ROTATE0); table.get_table_values.emplace_back(&get_and_rotate_values_from_key<6, 0>); } return table; diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/ultra_circuit_builder.cpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/ultra_circuit_builder.cpp index e96c234c5ab..f498411d97b 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/ultra_circuit_builder.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/ultra_circuit_builder.cpp @@ -609,6 +609,14 @@ uint32_t UltraCircuitBuilder_::put_constant_variable(const FF& } } +/** + * @brief Get the basic table with provided ID from the set of tables for the present circuit; create it if it doesnt + * yet exist + * + * @tparam Arithmetization + * @param id + * @return plookup::BasicTable& + */ template plookup::BasicTable& UltraCircuitBuilder_::get_table(const plookup::BasicTableId id) { @@ -619,7 +627,7 @@ plookup::BasicTable& UltraCircuitBuilder_::get_table(const ploo } // Table doesn't exist! So try to create it. lookup_tables.emplace_back(plookup::create_basic_table(id, lookup_tables.size())); - return lookup_tables[lookup_tables.size() - 1]; + return lookup_tables.back(); } /** @@ -633,13 +641,14 @@ plookup::ReadData UltraCircuitBuilder_::create_gates_ const uint32_t key_a_index, std::optional key_b_index) { - const auto& multi_table = plookup::create_table(id); + const auto& multi_table = plookup::get_multitable(id); const size_t num_lookups = read_values[plookup::ColumnIdx::C1].size(); plookup::ReadData read_data; for (size_t i = 0; i < num_lookups; ++i) { - auto& table = get_table(multi_table.lookup_ids[i]); + // get basic lookup table; construct and add to builder.lookup_tables if not already present + auto& table = get_table(multi_table.basic_table_ids[i]); - table.lookup_gates.emplace_back(read_values.key_entries[i]); + table.lookup_gates.emplace_back(read_values.lookup_entries[i]); // used for constructing sorted polynomials const auto first_idx = (i == 0) ? key_a_index : this->add_variable(read_values[plookup::ColumnIdx::C1][i]); const auto second_idx = (i == 0 && (key_b_index.has_value())) @@ -2806,7 +2815,7 @@ template msgpack::sbuffer UltraCircuitBuilder_> tmp_table; - for (size_t i = 0; i < table.size; ++i) { + for (size_t i = 0; i < table.size(); ++i) { tmp_table.push_back({ table.column_1[i], table.column_2[i], table.column_3[i] }); } cir.lookup_tables.push_back(tmp_table); diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp index 10317b7a009..ce1a8b8aebf 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp @@ -281,8 +281,9 @@ class UltraCircuitBuilder_ : public CircuitBuilderBase constant_variable_indices; + // The set of lookup tables used by the circuit, plus the gate data for the lookups from each table std::vector lookup_tables; - std::vector lookup_multi_tables; + std::map range_lists; // DOCTODO: explain this. /** @@ -367,7 +368,6 @@ class UltraCircuitBuilder_ : public CircuitBuilderBase (*get_values_from_key)(const std::array)); plookup::BasicTable& get_table(const plookup::BasicTableId id); - plookup::MultiTable& create_table(const plookup::MultiTableId id); + plookup::MultiTable& get_multitable(const plookup::MultiTableId id); plookup::ReadData create_gates_from_plookup_accumulators( const plookup::MultiTableId& id,