Skip to content

Commit 6d0bad7

Browse files
fix: barretenberg/stdlib/logic bugs (redo) (#11691)
Original PR: #11651 --------- Co-authored-by: defkit <[email protected]>
1 parent ce3d92c commit 6d0bad7

File tree

4 files changed

+49
-18
lines changed

4 files changed

+49
-18
lines changed

barretenberg/cpp/src/barretenberg/plonk_honk_shared/composer/composer_lib.test.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ TEST_F(ComposerLibTests, LookupReadCounts)
4343
auto accumulators = plookup::get_lookup_accumulators(UINT32_XOR, left, right, /*is_2_to_1_lookup*/ true);
4444
builder.create_gates_from_plookup_accumulators(UINT32_XOR, accumulators, left_idx, right_idx);
4545

46-
EXPECT_EQ(builder.lookup_tables.size(), 1); // we only used a single table
47-
EXPECT_EQ(builder.lookup_tables[0].size(), 4096); // table has size 64*64 (6 bit operands)
46+
EXPECT_EQ(builder.lookup_tables.size(), 2); // we only used two tables, first for 6 bits, second for 2 bits
47+
EXPECT_EQ(builder.lookup_tables[0].size(), 4096); // first table has size 64*64 (6 bit operands)
48+
EXPECT_EQ(builder.lookup_tables[1].size(), 16); // first table has size 4*4 (2 bit operands)
4849

4950
size_t circuit_size = 8192;
5051

@@ -59,15 +60,19 @@ TEST_F(ComposerLibTests, LookupReadCounts)
5960

6061
// The uint32 XOR lookup table is constructed for 6 bit operands via double for loop that iterates through the left
6162
// operand externally (0 to 63) then the right operand internally (0 to 63). Computing (1 XOR 5) will thus result in
62-
// 1 lookup from the (1*64 + 5)th index in the table and 5 lookups from the (0*64 + 0)th index (for the remaining 5
63-
// limbs that are all 0). The counts and tags at all other indices should be zero.
63+
// 1 lookup from the (1*64 + 5)th index in the table and 4 lookups from the (0*64 + 0)th index (for the remaining 4
64+
// 6-bits limbs that are all 0) and one lookup from second table from the (64 * 64 + 0) index (for last 2 bits).
65+
// The counts and tags at all other indices should be zero.
6466
for (auto [idx, count, tag] : zip_polys(read_counts, read_tags)) {
6567
if (idx == (0 + offset)) {
66-
EXPECT_EQ(count, 5);
68+
EXPECT_EQ(count, 4);
6769
EXPECT_EQ(tag, 1);
6870
} else if (idx == (69 + offset)) {
6971
EXPECT_EQ(count, 1);
7072
EXPECT_EQ(tag, 1);
73+
} else if (idx == (64 * 64 + offset)) {
74+
EXPECT_EQ(count, 1);
75+
EXPECT_EQ(tag, 1);
7176
} else {
7277
EXPECT_EQ(count, 0);
7378
EXPECT_EQ(tag, 0);

barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,17 @@ BasicTable create_basic_table(const BasicTableId id, const size_t index)
309309
case SHA256_BASE16_ROTATE2: {
310310
return sparse_tables::generate_sparse_table_with_rotation<16, 11, 2>(SHA256_BASE16_ROTATE2, index);
311311
}
312-
case UINT_XOR_ROTATE0: {
313-
return uint_tables::generate_xor_rotate_table<6, 0>(UINT_XOR_ROTATE0, index);
312+
case UINT_XOR_SLICE_6_ROTATE_0: {
313+
return uint_tables::generate_xor_rotate_table<6, 0>(UINT_XOR_SLICE_6_ROTATE_0, index);
314314
}
315-
case UINT_AND_ROTATE0: {
316-
return uint_tables::generate_and_rotate_table<6, 0>(UINT_AND_ROTATE0, index);
315+
case UINT_AND_SLICE_6_ROTATE_0: {
316+
return uint_tables::generate_and_rotate_table<6, 0>(UINT_AND_SLICE_6_ROTATE_0, index);
317+
}
318+
case UINT_XOR_SLICE_2_ROTATE_0: {
319+
return uint_tables::generate_xor_rotate_table<2, 0>(UINT_XOR_SLICE_2_ROTATE_0, index);
320+
}
321+
case UINT_AND_SLICE_2_ROTATE_0: {
322+
return uint_tables::generate_and_rotate_table<2, 0>(UINT_AND_SLICE_2_ROTATE_0, index);
317323
}
318324
case BN254_XLO_BASIC: {
319325
return ecc_generator_tables::ecc_generator_table<bb::g1>::generate_xlo_table(BN254_XLO_BASIC, index);

barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/types.hpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ enum BasicTableId {
3030
SHA256_BASE16_ROTATE6,
3131
SHA256_BASE16_ROTATE7,
3232
SHA256_BASE16_ROTATE8,
33-
UINT_XOR_ROTATE0,
34-
UINT_AND_ROTATE0,
33+
UINT_XOR_SLICE_6_ROTATE_0,
34+
UINT_XOR_SLICE_2_ROTATE_0,
35+
UINT_AND_SLICE_6_ROTATE_0,
36+
UINT_AND_SLICE_2_ROTATE_0,
3537
BN254_XLO_BASIC,
3638
BN254_XHI_BASIC,
3739
BN254_YLO_BASIC,

barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/uint.hpp

+25-7
Original file line numberDiff line numberDiff line change
@@ -72,31 +72,49 @@ inline BasicTable generate_and_rotate_table(BasicTableId id, const size_t table_
7272

7373
inline MultiTable get_uint32_xor_table(const MultiTableId id = UINT32_XOR)
7474
{
75-
const size_t num_entries = (32 + 5) / 6;
76-
const uint64_t base = 1 << 6;
75+
const size_t TABLE_BIT_SIZE = 6;
76+
const size_t num_entries = 32 / TABLE_BIT_SIZE;
77+
const uint64_t base = 1 << TABLE_BIT_SIZE;
7778
MultiTable table(base, base, base, num_entries);
7879

7980
table.id = id;
8081
for (size_t i = 0; i < num_entries; ++i) {
8182
table.slice_sizes.emplace_back(base);
82-
table.basic_table_ids.emplace_back(UINT_XOR_ROTATE0);
83+
table.basic_table_ids.emplace_back(UINT_XOR_SLICE_6_ROTATE_0);
8384
table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<6, 0>);
8485
}
86+
87+
// 32 = 5 * 6 + 2
88+
// all remaining bits
89+
const size_t LAST_TABLE_BIT_SIZE = 32 - TABLE_BIT_SIZE * num_entries;
90+
const size_t LAST_SLICE_SIZE = 1 << LAST_TABLE_BIT_SIZE;
91+
table.slice_sizes.emplace_back(LAST_SLICE_SIZE);
92+
table.basic_table_ids.emplace_back(UINT_XOR_SLICE_2_ROTATE_0);
93+
table.get_table_values.emplace_back(&get_xor_rotate_values_from_key<LAST_TABLE_BIT_SIZE, 0>);
8594
return table;
8695
}
8796

8897
inline MultiTable get_uint32_and_table(const MultiTableId id = UINT32_AND)
8998
{
90-
const size_t num_entries = (32 + 5) / 6;
91-
const uint64_t base = 1 << 6;
99+
const size_t TABLE_BIT_SIZE = 6;
100+
const size_t num_entries = 32 / TABLE_BIT_SIZE;
101+
const uint64_t base = 1 << TABLE_BIT_SIZE;
92102
MultiTable table(base, base, base, num_entries);
93103

94104
table.id = id;
95105
for (size_t i = 0; i < num_entries; ++i) {
96106
table.slice_sizes.emplace_back(base);
97-
table.basic_table_ids.emplace_back(UINT_AND_ROTATE0);
98-
table.get_table_values.emplace_back(&get_and_rotate_values_from_key<6, 0>);
107+
table.basic_table_ids.emplace_back(UINT_AND_SLICE_6_ROTATE_0);
108+
table.get_table_values.emplace_back(&get_and_rotate_values_from_key<TABLE_BIT_SIZE, 0>);
99109
}
110+
// 32 = 5 * 6 + 2
111+
// all remaining bits
112+
const size_t LAST_TABLE_BIT_SIZE = 32 - TABLE_BIT_SIZE * num_entries;
113+
const size_t LAST_SLICE_SIZE = 1 << LAST_TABLE_BIT_SIZE;
114+
table.slice_sizes.emplace_back(LAST_SLICE_SIZE);
115+
table.basic_table_ids.emplace_back(UINT_AND_SLICE_2_ROTATE_0);
116+
table.get_table_values.emplace_back(&get_and_rotate_values_from_key<LAST_TABLE_BIT_SIZE, 0>);
117+
100118
return table;
101119
}
102120

0 commit comments

Comments
 (0)