Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
univariate_coefficient_basis.test.cpp
Go to the documentation of this file.
3#include "univariate.hpp"
4#include <bitset>
5#include <gtest/gtest.h>
6
7using namespace bb;
8
9template <typename FF> class UnivariateCoefficientBasisTest : public testing::Test {
10 public:
11 template <size_t view_length> using UnivariateView = UnivariateView<FF, view_length>;
12};
13
14using FieldTypes = testing::Types<fr>;
16
18{
21
22 Univariate<fr, 2> expected({ a0, a1 });
25 EXPECT_EQ(result, expected);
26}
27
29{
30 Univariate<fr, 2> f1{ { 1, 2 } };
31 Univariate<fr, 2> f2{ { 3, 4 } };
34
35 Univariate<fr, 2> result(f1_m + f2_m);
36 Univariate<fr, 2> expected = f1 + f2;
37 EXPECT_EQ(result, expected);
38}
39
41{
42
43 Univariate<fr, 2> f1({ 1, 2 });
44 Univariate<fr, 2> f2({ 3, 4 });
47
48 Univariate<fr, 3> result(f1_m * f2_m);
49 Univariate<fr, 3> expected = (f1.template extend_to<3>()) * (f2.template extend_to<3>());
50 EXPECT_EQ(result, expected);
51}
52
53// `VectorField::is_zero()` (bool form) and `UnivariateCoefficientBasis<Vec>::is_zero()` must both mean
54// "every lane of every coefficient is zero". Selector-gated prover relations use these as the skip
55// predicate; a partial-zero lane pattern reading as "is_zero == true" would drop a subrelation whose
56// selector fires on at least one of the rows packed into the lanes.
58
59namespace {
60
62
63bb::fr fr_from_lane(size_t lane, bool nonzero)
64{
65 // Distinct nonzero value per lane so a lane-mixing bug surfaces as a wrong value, not just a non-zero one.
66 return nonzero ? bb::fr(static_cast<uint64_t>(0x100 + lane)) : bb::fr::zero();
67}
68
69Vec vec_lane_pattern(uint32_t nonzero_mask)
70{
72 for (size_t i = 0; i < 5; ++i) {
73 lanes[i] = fr_from_lane(i, (nonzero_mask >> i) & 1u);
74 }
75 return Vec(lanes);
76}
77
78} // namespace
79
80TEST(VectorFieldIsZeroBool, AllZeroLanesIsZero)
81{
82 EXPECT_TRUE(vec_lane_pattern(0b00000).is_zero());
83 EXPECT_EQ(vec_lane_pattern(0b00000).is_zero_mask(), 0b11111u);
84}
85
86TEST(VectorFieldIsZeroBool, AllNonZeroLanesIsNotZero)
87{
88 EXPECT_FALSE(vec_lane_pattern(0b11111).is_zero());
89 EXPECT_EQ(vec_lane_pattern(0b11111).is_zero_mask(), 0u);
90}
91
92// Mixed lane patterns (e.g. one row carries `lagrange_first = 1`, the other lanes hold rows where it's
93// zero). Each must read as not-zero so the relation gate fires on the row that needs it.
94TEST(VectorFieldIsZeroBool, PartiallyNonZeroLanesIsNotZero)
95{
96 for (uint32_t pattern : { 0b00001u, 0b00010u, 0b00100u, 0b01000u, 0b10000u, 0b01010u, 0b11110u, 0b10101u }) {
97 EXPECT_FALSE(vec_lane_pattern(pattern).is_zero()) << "pattern=0b" << std::bitset<5>(pattern);
98 }
99}
100
101// `UnivariateCoefficientBasis<Vec>::is_zero()` is the actual call site for the relation gate
102// (`gate.is_zero()` where `gate` is a `CoefficientAccumulator`); a non-zero lane anywhere in any
103// coefficient must read as not-zero.
104TEST(UnivariateCoefficientBasisVec, IsZeroFalseWhenSingleLaneIsNonZero)
105{
107 UCB gate;
108 gate.coefficients[0] = vec_lane_pattern(0b00001);
109 gate.coefficients[1] = vec_lane_pattern(0b00000);
110 gate.coefficients[2] = gate.coefficients[0];
111 EXPECT_FALSE(gate.is_zero());
112}
113
114TEST(UnivariateCoefficientBasisVec, IsZeroTrueOnlyWhenAllLanesZero)
115{
117 UCB gate;
118 gate.coefficients[0] = vec_lane_pattern(0b00000);
119 gate.coefficients[1] = vec_lane_pattern(0b00000);
120 gate.coefficients[2] = vec_lane_pattern(0b00000);
121 EXPECT_TRUE(gate.is_zero());
122}
UnivariateView< FF, view_length > UnivariateView
A view of a univariate, also used to truncate univariates.
std::array< Fr, 3 > coefficients
Storage for polynomial coefficients (always 3 elements for uniform layout).
A univariate polynomial represented by its values on {0, 1,..., domain_end - 1}.
testing::Types< bb::fr > FieldTypes
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
TYPED_TEST_SUITE(CommitmentKeyTest, Curves)
field< Bn254FrParams > fr
Definition fr.hpp:155
TYPED_TEST(CommitmentKeyTest, CommitToZeroPoly)
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static field random_element(numeric::RNG *engine=nullptr) noexcept
bb::VectorField< bb::Bn254FrParams > Vec
VectorField result