Skip to content

Commit

Permalink
some naming updates and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ledwards2225 committed Jun 10, 2024
1 parent 60f2b1b commit 760a9fb
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ field_t<Builder> keccak<Builder>::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<uint64_t>(input_slice), 0 }, { normalized_slice, normalized_msb } });

// reduce the input and output by 11^{bit_slice}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,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;
Expand All @@ -117,7 +117,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<AES_BASE, 0>);
}
return table;
Expand Down Expand Up @@ -167,7 +167,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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,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<BITS_IN_LAST_SLICE, 0, true>);

return table;
Expand Down Expand Up @@ -128,8 +128,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>);
Expand Down Expand Up @@ -163,8 +163,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>);
Expand Down Expand Up @@ -198,8 +198,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>);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,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<HONK_DUMMY_BASIC1>);
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<HONK_DUMMY_BASIC2>);
return table;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,13 @@ template <size_t multitable_index, size_t num_bits> 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<size_t>(basic_table_ids[multitable_index]);
table.lookup_ids[i] = static_cast<plookup::BasicTableId>(idx);
table.basic_table_ids[i] = static_cast<plookup::BasicTableId>(idx);
}
return table;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BASE, OUTPUT_NORMALIZATION_TABLE>);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ template <size_t TABLE_BITS = 0, size_t LANE_INDEX = 0> 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
Expand All @@ -284,7 +284,7 @@ template <size_t TABLE_BITS = 0, size_t LANE_INDEX = 0> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ MultiTable ecc_generator_table<G1>::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;
Expand All @@ -389,7 +389,7 @@ MultiTable ecc_generator_table<G1>::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;
Expand All @@ -404,7 +404,7 @@ MultiTable ecc_generator_table<G1>::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;
Expand All @@ -419,7 +419,7 @@ MultiTable ecc_generator_table<G1>::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;
Expand All @@ -434,7 +434,7 @@ MultiTable ecc_generator_table<G1>::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;
Expand All @@ -449,7 +449,7 @@ MultiTable ecc_generator_table<G1>::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;
Expand All @@ -464,7 +464,7 @@ MultiTable ecc_generator_table<G1>::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;
Expand All @@ -479,7 +479,7 @@ MultiTable ecc_generator_table<G1>::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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ std::array<MultiTable, MultiTableId::NUM_MULTI_TABLES> 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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -155,8 +164,8 @@ ReadData<bb::fr> get_lookup_accumulators(const MultiTableId id,
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<bb::fr> lookup;
const auto key_a_slices = numeric::slice_input_using_variable_bases(key_a, multi_table.slice_sizes);
Expand All @@ -175,8 +184,8 @@ ReadData<bb::fr> get_lookup_accumulators(const MultiTableId id,
column_3_raw_values.emplace_back(is_2_to_1_lookup ? values[0] : values[1]);

// Question: why are we storing the key slices twice? // WORKTODO: resolve question?
const BasicTable::LookupEntry key_entry{ { key_a_slices[i], key_b_slices[i] }, values };
lookup.key_entries.emplace_back(key_entry);
const BasicTable::LookupEntry lookup_entry{ { key_a_slices[i], key_b_slices[i] }, values };
lookup.lookup_entries.emplace_back(lookup_entry);
}

lookup[C1].resize(num_lookups);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace bb::plookup {

const MultiTable& create_table(MultiTableId id);
const MultiTable& get_multitable(MultiTableId id);

ReadData<bb::fr> get_lookup_accumulators(MultiTableId id,
const bb::fr& key_a,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>);
}
Expand All @@ -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>);
}
Expand All @@ -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>);
}
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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>);
Expand Down Expand Up @@ -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>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ enum MultiTableId {

/**
* @brief Container for managing multiple BasicTables plus the data needed to combine basic table outputs (limbs) into
* accumulators
* 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
Expand All @@ -140,7 +140,7 @@ struct MultiTable {
std::vector<bb::fr> column_2_coefficients;
std::vector<bb::fr> column_3_coefficients;
MultiTableId id;
std::vector<BasicTableId> lookup_ids;
std::vector<BasicTableId> basic_table_ids;
std::vector<uint64_t> slice_sizes;
std::vector<bb::fr> column_1_step_sizes;
std::vector<bb::fr> column_2_step_sizes;
Expand Down Expand Up @@ -341,7 +341,7 @@ template <class DataType> class ReadData {
std::vector<DataType>& operator[](ColumnIdx idx) { return columns[static_cast<size_t>(idx)]; };
const std::vector<DataType>& operator[](ColumnIdx idx) const { return columns[static_cast<size_t>(idx)]; };

std::vector<BasicTable::LookupEntry> key_entries;
std::vector<BasicTable::LookupEntry> lookup_entries;

private:
std::array<std::vector<DataType>, 3> columns;
Expand Down
Loading

0 comments on commit 760a9fb

Please sign in to comment.