Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
gt.test.cpp
Go to the documentation of this file.
1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
3
4#include <cstdint>
5
15
16namespace bb::avm2::constraining {
17namespace {
18
19using tracegen::TestTraceContainer;
21using C = Column;
22using gt = bb::avm2::gt<FF>;
23using tracegen::GreaterThanTraceBuilder;
24using tracegen::RangeCheckTraceBuilder;
25
26const std::vector<std::array<uint128_t, 3>> TEST_VALUES = { { 1, 2, 16 },
27 { 2, 1, 16 },
28 { 2, 2, 16 },
29 { uint128_t{ (uint256_t(1) << 128) - 1 }, 1, 128 },
30 { 1, uint128_t{ (uint256_t(1) << 128) - 1 }, 128 } };
31
32class GreaterThanTest : public ::testing::TestWithParam<std::array<uint128_t, 3>> {};
33
34INSTANTIATE_TEST_SUITE_P(GreaterThanConstrainingTest, GreaterThanTest, ::testing::ValuesIn(TEST_VALUES));
35
36TEST_P(GreaterThanTest, GreaterThan)
37{
38 RangeCheckTraceBuilder range_check_builder;
39 auto [a, b, num_bits] = GetParam();
40 bool res = a > b;
41 uint128_t abs_diff = res ? a - b - 1 : b - a;
42 auto trace = TestTraceContainer({
43 {
44 { C::gt_abs_diff, abs_diff },
45 { C::gt_input_a, a },
46 { C::gt_input_b, b },
47 { C::gt_num_bits, num_bits },
48 { C::gt_res, static_cast<uint8_t>(res) },
49 { C::gt_sel, 1 },
50 },
51 });
52 range_check_builder.process({ { .value = abs_diff, .num_bits = static_cast<uint8_t>(num_bits) } }, trace);
53 check_all_interactions<GreaterThanTraceBuilder>(trace);
54 check_relation<gt>(trace);
55}
56
57TEST_P(GreaterThanTest, GreaterThanTraceGen)
58{
59 RangeCheckTraceBuilder range_check_builder;
60 TestTraceContainer trace;
61 GreaterThanTraceBuilder builder;
62 auto [a, b, num_bits] = GetParam();
64 {
65 {
66 .a = a,
67 .b = b,
68 .result = a > b,
69 },
70 },
71 trace);
72 range_check_builder.process({ { .value = a > b ? a - b - 1 : b - a, .num_bits = static_cast<uint8_t>(num_bits) } },
73 trace);
74 check_all_interactions<GreaterThanTraceBuilder>(trace);
75 check_relation<gt>(trace);
76}
77
78TEST(GreaterThanConstrainingTest, NegativeGT)
79{
80 RangeCheckTraceBuilder range_check_builder;
81 uint128_t a = 2;
82 uint128_t b = 1;
83 bool res = a > b;
84 uint128_t abs_diff = res ? a - b - 1 : b - a;
85 auto trace = TestTraceContainer({
86 {
87 { C::gt_abs_diff, abs_diff },
88 { C::gt_input_a, a },
89 { C::gt_input_b, b },
90 { C::gt_num_bits, 16 },
91 { C::gt_res, static_cast<uint8_t>(res) },
92 { C::gt_sel, 1 },
93 },
94 });
95 range_check_builder.process({ { .value = abs_diff, .num_bits = 16 } }, trace);
96 check_all_interactions<GreaterThanTraceBuilder>(trace);
97 check_relation<gt>(trace);
98 auto wrong_b = res ? FF(a) + 1 : FF(a) - 1;
99 trace.set(Column::gt_input_b, 0, wrong_b);
100 // The absolute diff is now wrong:
101 EXPECT_THROW_WITH_MESSAGE(check_relation<gt>(trace), "GT_RESULT");
102 // Correct the diff based on incorrect input:
103 auto new_abs_diff = res ? FF(a) - wrong_b - 1 : wrong_b - FF(a);
104 trace.set(Column::gt_abs_diff, 0, new_abs_diff);
105 // Now, we are range checking the correct value...
106 check_relation<gt>(trace);
107 // ..but the check itself correctly fails (note: new_result_to_range_check doesn't fit in a u128, I'm just adding
108 // an event which will definitely fail):
109 range_check_builder.process({ { .value = static_cast<uint128_t>(new_abs_diff), .num_bits = 128 } }, trace);
110 EXPECT_THROW_WITH_MESSAGE((check_all_interactions<GreaterThanTraceBuilder>(trace)), "LOOKUP_GT_GT_RANGE");
111}
112
113TEST(GreaterThanConstrainingTest, NegativeGTResult)
114{
115 RangeCheckTraceBuilder range_check_builder;
116 uint128_t a = 2;
117 uint128_t b = 1;
118 bool res = a > b;
119 uint128_t abs_diff = res ? a - b - 1 : b - a;
120 auto trace = TestTraceContainer({
121 {
122 { C::gt_abs_diff, abs_diff },
123 { C::gt_input_a, a },
124 { C::gt_input_b, b },
125 { C::gt_num_bits, 16 },
126 { C::gt_res, static_cast<uint8_t>(res) },
127 { C::gt_sel, 1 },
128 },
129 });
130 range_check_builder.process({ { .value = abs_diff, .num_bits = 16 } }, trace);
131 check_all_interactions<GreaterThanTraceBuilder>(trace);
132 check_relation<gt>(trace);
133 trace.set(Column::gt_res, 0, static_cast<uint8_t>(!res));
134 // The absolute diff is now wrong:
135 EXPECT_THROW_WITH_MESSAGE(check_relation<gt>(trace), "GT_RESULT");
136 // Correct the diff based on incorrect res:
137 auto new_abs_diff = res ? FF(b) - FF(a) : FF(a) - FF(b) - 1;
138 trace.set(Column::gt_abs_diff, 0, new_abs_diff);
139 // Now, we are range checking the correct value...
140 check_relation<gt>(trace);
141 // ..but the check itself correctly fails (note: new_result_to_range_check doesn't fit in a u128, I'm just adding
142 // an event which will definitely fail):
143 range_check_builder.process({ { .value = static_cast<uint128_t>(new_abs_diff), .num_bits = 128 } }, trace);
144 EXPECT_THROW_WITH_MESSAGE((check_all_interactions<GreaterThanTraceBuilder>(trace)), "LOOKUP_GT_GT_RANGE");
145}
146
147} // namespace
148} // namespace bb::avm2::constraining
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessageRegex)
Definition assert.hpp:192
void process(const simulation::EventEmitterInterface< simulation::AluEvent >::Container &events, TraceContainer &trace)
Process the ALU events and populate the ALU relevant columns in the trace.
void process(const simulation::EventEmitterInterface< simulation::RangeCheckEvent >::Container &events, TraceContainer &trace)
void set(Column col, uint32_t row, const FF &value)
RangeCheckTraceBuilder range_check_builder
Definition alu.test.cpp:121
AluTraceBuilder builder
Definition alu.test.cpp:124
GreaterThan gt
TestTraceContainer trace
FF a
FF b
INSTANTIATE_TEST_SUITE_P(PaddingVariants, AvmRecursiveTestsParameterized, ::testing::Values(false, true), [](const auto &info) { return info.param ? "Padded" :"Unpadded";})
TEST_P(AvmRecursiveTestsParameterized, GoblinRecursion)
A test of the Goblinized AVM recursive verifier.
TEST(AvmFixedVKTests, FixedVKCommitments)
Test that the fixed VK commitments agree with the ones computed from precomputed columns.
AvmFlavorSettings::FF FF
Definition field.hpp:10
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
unsigned __int128 uint128_t
Definition serialize.hpp:44