Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
composer_lib.test.cpp
Go to the documentation of this file.
7
8#include <array>
9#include <gtest/gtest.h>
10
11using namespace bb;
12
13class ComposerLibTests : public UltraHonkTests<UltraFlavor> {
14 public:
16 using FF = Flavor::FF;
19};
25TEST_F(ComposerLibTests, LookupReadCounts)
26{
27 auto UINT32_XOR = plookup::MultiTableId::UINT32_XOR;
29
30 // define some very simply inputs to XOR
31 FF left{ 1 };
32 FF right{ 5 };
33
34 auto left_idx = builder.add_variable(left);
35 auto right_idx = builder.add_variable(right);
36
37 // create a single lookup from the uint32 XOR table
38 auto accumulators = plookup::get_lookup_accumulators(UINT32_XOR, left, right, /*is_2_to_1_lookup*/ true);
39 builder.create_gates_from_plookup_accumulators(UINT32_XOR, accumulators, left_idx, right_idx);
40
41 EXPECT_EQ(builder.get_num_lookup_tables(), 2); // we only used two tables, first for 6 bits, second for 2 bits
42 EXPECT_EQ(builder.get_lookup_tables()[0].size(), 4096); // first table has size 64*64 (6 bit operands)
43 EXPECT_EQ(builder.get_lookup_tables()[1].size(), 16); // first table has size 4*4 (2 bit operands)
44
45 size_t circuit_size = 8192;
46
47 Polynomial read_counts{ circuit_size };
48 Polynomial read_tags{ circuit_size };
49
50 builder.blocks.compute_offsets();
51 construct_lookup_read_counts<Flavor>(read_counts, read_tags, builder);
52
53 // The uint32 XOR lookup table is constructed for 6 bit operands via double for loop that iterates through the left
54 // operand externally (0 to 63) then the right operand internally (0 to 63). Computing (1 XOR 5) will thus result in
55 // 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
56 // 6-bits limbs that are all 0) and one lookup from second table from the (64 * 64 + 0) index (for last 2 bits).
57 // The counts and tags at all other indices should be zero.
58 // Read counts are offset by the lookup block's trace offset.
59 const size_t table_offset = builder.blocks.lookup.trace_offset();
60 for (auto [idx, count, tag] : zip_polys(read_counts, read_tags)) {
61 if (idx == table_offset + 0) {
62 EXPECT_EQ(count, 4);
63 EXPECT_EQ(tag, 1);
64 } else if (idx == table_offset + 69) {
65 EXPECT_EQ(count, 1);
66 EXPECT_EQ(tag, 1);
67 } else if (idx == table_offset + 64 * 64) {
68 EXPECT_EQ(count, 1);
69 EXPECT_EQ(tag, 1);
70 } else {
71 EXPECT_EQ(count, 0) << "Unexpected count at index " << idx;
72 EXPECT_EQ(tag, 0) << "Unexpected tag at index " << idx;
73 }
74 idx++;
75 }
76}
77
83TEST_F(ComposerLibTests, TagCollisionFailure)
84{
85 {
87
88 FF val_a = FF::random_element();
89 FF val_b = FF::random_element(); // Different value
90 ASSERT_FALSE(val_a == val_b);
91
92 auto a_idx = builder.add_variable(val_a);
93 auto b_idx = builder.add_variable(val_b);
94
95 auto first_tag = builder.get_new_tag();
96 auto second_tag = builder.get_new_tag();
97 builder.set_tau_transposition(first_tag, second_tag);
98 // Assign the same tag to both variables (which have different values)
99 // As we only have two witnesses, this forces a multiset-equality check `{val_a} == {val_b}`, which should fail.
100 builder.assign_tag(a_idx, first_tag);
101 builder.assign_tag(b_idx, second_tag);
102
103 // Use the variables in gates so they appear in the trace
104 builder.create_add_gate(
105 { a_idx, builder.zero_idx(), builder.zero_idx(), FF::one(), FF::zero(), FF::zero(), -val_a });
106 builder.create_add_gate(
107 { b_idx, builder.zero_idx(), builder.zero_idx(), FF::one(), FF::zero(), FF::zero(), -val_b });
108
109 // Add required pairing points for Ultra circuits
110 set_default_pairing_points_and_ipa_claim_and_proof(builder);
111 // prove and verify
112 prove_and_verify(builder, /*expected_result=*/false);
113 }
114}
Curve::ScalarField FF
bb::Polynomial< FF > Polynomial
AluTraceBuilder builder
Definition alu.test.cpp:124
ReadData< bb::fr > get_lookup_accumulators(const MultiTableId id, const fr &key_a, const fr &key_b, const bool is_2_to_1_lookup)
Given a table ID and the key(s) for a key-value lookup, return the lookup accumulators.
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
TEST_F(IPATest, ChallengesAreZero)
Definition ipa.test.cpp:155
UltraCircuitBuilder_< UltraExecutionTraceBlocks > UltraCircuitBuilder
auto zip_polys(Poly &&poly, Polys &&... polys)
Contains various functions that help construct Honk Sigma and Id polynomials.
static field random_element(numeric::RNG *engine=nullptr) noexcept