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 for (auto [idx, count, tag] : zip_polys(read_counts, read_tags)) {
59 if (idx == (0)) {
60 EXPECT_EQ(count, 4);
61 EXPECT_EQ(tag, 1);
62 } else if (idx == (69)) {
63 EXPECT_EQ(count, 1);
64 EXPECT_EQ(tag, 1);
65 } else if (idx == (64 * 64)) {
66 EXPECT_EQ(count, 1);
67 EXPECT_EQ(tag, 1);
68 } else {
69 EXPECT_EQ(count, 0);
70 EXPECT_EQ(tag, 0);
71 }
72 idx++;
73 }
74}
75
81TEST_F(ComposerLibTests, TagCollisionFailure)
82{
83 {
85
86 FF val_a = FF::random_element();
87 FF val_b = FF::random_element(); // Different value
88 ASSERT_FALSE(val_a == val_b);
89
90 auto a_idx = builder.add_variable(val_a);
91 auto b_idx = builder.add_variable(val_b);
92
93 auto first_tag = builder.get_new_tag();
94 auto second_tag = builder.get_new_tag();
95 builder.set_tau_transposition(first_tag, second_tag);
96 // Assign the same tag to both variables (which have different values)
97 // As we only have two witnesses, this forces a multiset-equality check `{val_a} == {val_b}`, which should fail.
98 builder.assign_tag(a_idx, first_tag);
99 builder.assign_tag(b_idx, second_tag);
100
101 // Use the variables in gates so they appear in the trace
102 builder.create_add_gate(
103 { a_idx, builder.zero_idx(), builder.zero_idx(), FF::one(), FF::zero(), FF::zero(), -val_a });
104 builder.create_add_gate(
105 { b_idx, builder.zero_idx(), builder.zero_idx(), FF::one(), FF::zero(), FF::zero(), -val_b });
106
107 // Add required pairing points for Ultra circuits
108 set_default_pairing_points_and_ipa_claim_and_proof(builder);
109 // prove and verify
110 prove_and_verify(builder, /*expected_result=*/false);
111 }
112}
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:185
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