Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
vec_relation_spike.test.cpp
Go to the documentation of this file.
1// Compile-time + native-parity tests for using `VectorField` as the element type of `Univariate<...>` and
2// relation accumulators. Each test pins one abstraction layer (Univariate ops, edge extension, relation
3// instantiation), so a missing Vec API or a regression in the relation set surfaces at a known callsite.
4//
5// Lives in `polynomials/` to keep the lower-level tests off the relations + flavor link surface.
6
15
16#include <array>
17#include <gtest/gtest.h>
18
19namespace {
20
21using bb::fr;
23
24Vec random_vec()
25{
27 for (auto& v : a) {
29 }
30 return Vec(a);
31}
32
33Vec broadcast(const fr& s)
34{
35 return Vec::broadcast(s);
36}
37
38TEST(VecRelationSpike, UnivariateVecDefaultAndScalarCtor)
39{
40 using UVec = bb::Univariate<Vec, 6>;
41 UVec a; // default
42 UVec b{ Vec::one() }; // explicit Univariate(const Vec&) — all slots = Vec::one()
43 (void)a;
44 (void)b;
45 SUCCEED();
46}
47
48TEST(VecRelationSpike, UnivariateVecArithmetic)
49{
50 using UVec = bb::Univariate<Vec, 6>;
51 std::array<Vec, 6> av{ random_vec(), random_vec(), random_vec(), random_vec(), random_vec(), random_vec() };
52 std::array<Vec, 6> bv{ random_vec(), random_vec(), random_vec(), random_vec(), random_vec(), random_vec() };
53 UVec a{ av };
54 UVec b{ bv };
55 auto sum = a + b;
56 auto diff = a - b;
57 auto prod = a * b;
58 (void)sum;
59 (void)diff;
60 (void)prod;
61 SUCCEED();
62}
63
64// `extend_to<K>` is what `Sumcheck::extend_edges` calls on each (eval@0, eval@1) edge before relation
65// algebra runs; the SimdLane path goes through the same code with `Vec` substituted for `FF`.
66TEST(VecRelationSpike, UnivariateVecExtendTo)
67{
68 std::array<Vec, 2> base{ random_vec(), random_vec() };
69 bb::Univariate<Vec, 2> u2{ base };
70 auto extended = u2.template extend_to<6>();
71 (void)extended;
72 SUCCEED();
73}
74
75// Running the same algebraic expression on `Univariate<Vec>` built from `broadcast(scalar)` should yield
76// a Vec result whose every lane equals the scalar result.
77TEST(VecRelationSpike, UnivariateVecBroadcastParity)
78{
79 using UFF = bb::Univariate<fr, 6>;
80 using UVec = bb::Univariate<Vec, 6>;
81
84 for (size_t k = 0; k < 6; ++k) {
85 a_arr[k] = fr::random_element();
86 b_arr[k] = fr::random_element();
87 }
88 UFF a_ff{ a_arr };
89 UFF b_ff{ b_arr };
90
91 std::array<Vec, 6> a_vec_arr;
92 std::array<Vec, 6> b_vec_arr;
93 for (size_t k = 0; k < 6; ++k) {
94 a_vec_arr[k] = broadcast(a_arr[k]);
95 b_vec_arr[k] = broadcast(b_arr[k]);
96 }
97 UVec a_vec{ a_vec_arr };
98 UVec b_vec{ b_vec_arr };
99
100 auto result_ff = a_ff * b_ff + a_ff - b_ff;
101 auto result_vec = a_vec * b_vec + a_vec - b_vec;
102
103 for (size_t k = 0; k < 6; ++k) {
104 auto lanes = result_vec.evaluations[k].to_array();
105 for (size_t L = 0; L < 5; ++L) {
106 EXPECT_EQ(lanes[L], result_ff.evaluations[k]) << "k=" << k << " lane=" << L;
107 }
108 }
109}
110
111// Compile-time check that `Vec` is a valid `FF` substitute for relation templates.
112TEST(VecRelationSpike, ArithmeticRelationImplInstantiates)
113{
116 static_assert(Rel::SUBRELATION_PARTIAL_LENGTHS[0] == 6);
117 static_assert(Rel::SUBRELATION_PARTIAL_LENGTHS[1] == 5);
118 SUCCEED();
119}
120
121// Force instantiation of `accumulate<Vec>` through the codegen entity container
122// `MegaFlavor_Generated::AllEntities<Univariate<Vec,K>>` -- the same path the SimdLane uses. Runs on native
123// (Vec is the scalar fallback), so this only proves compile feasibility; SIMD correctness is covered by
124// `VectorFieldTest` on WASM and end-to-end by the WASM proving tests.
125TEST(VecRelationSpike, RelationsInstantiateWithVecElement)
126{
127 using UVec = bb::Univariate<Vec, 8>;
130 const Vec scaling = Vec::broadcast(fr::random_element());
131
132 typename bb::Relation<bb::ArithmeticRelationImpl<Vec>>::SumcheckTupleOfUnivariatesOverSubrelations arith_acc{};
133 bb::ArithmeticRelationImpl<Vec>::accumulate(arith_acc, in, params, scaling);
134
135 // Permutation exercises the `RelationParameters<Vec>` View path (beta/gamma/public_input_delta).
136 typename bb::Relation<bb::UltraPermutationRelationImpl<Vec>>::SumcheckTupleOfUnivariatesOverSubrelations perm_acc{};
137 bb::UltraPermutationRelationImpl<Vec>::accumulate(perm_acc, in, params, scaling);
138
139 SUCCEED();
140}
141
142// `Univariate<Vec>::is_zero()` must mean "every coefficient zero on every lane" -- the relation skip-batch
143// decision (`selector.is_zero()`) is only safe when the selector is identically zero across all packed rows.
144TEST(VecRelationSpike, UnivariateVecIsZeroMeansAllLanesAllCoeffs)
145{
146 using UVec = bb::Univariate<Vec, 4>;
147 const std::array<Vec, 4> zeros{ Vec::zero(), Vec::zero(), Vec::zero(), Vec::zero() };
148
149 EXPECT_TRUE((UVec{ zeros }).is_zero());
150
151 // One coefficient with one non-zero lane -- must NOT be reported as identically zero.
152 {
153 std::array<Vec, 4> arr = zeros;
154 const std::array<fr, 5> lanes{ fr::zero(), fr::zero(), fr::one(), fr::zero(), fr::zero() };
155 arr[2] = Vec(lanes);
156 EXPECT_FALSE((UVec{ arr }).is_zero());
157 }
158
159 // One coefficient with every lane non-zero.
160 {
161 std::array<Vec, 4> arr = zeros;
162 arr[0] = Vec::broadcast(fr::one());
163 EXPECT_FALSE((UVec{ arr }).is_zero());
164 }
165}
166
167} // namespace
TEST(acir_formal_proofs, uint_terms_add)
Tests 128-bit unsigned addition Verifies that the ACIR implementation of addition is correct Executio...
Expression for the Ultra (width-4) Arithmetic gate.
static void accumulate(ContainerOverSubrelations &evals, const AllEntities &in, BB_UNUSED const Parameters &params, const FF &scaling_factor)
A wrapper for Relations to expose methods used by the Sumcheck prover or verifier to add the contribu...
static void accumulate(ContainerOverSubrelations &accumulators, const AllEntities &in, const Parameters &params, const FF &scaling_factor)
Compute contribution of the permutation relation for a given edge (internal function)
A univariate polynomial represented by its values on {0, 1,..., domain_end - 1}.
FF a
FF b
Inner sum(Cont< Inner, Args... > const &in)
Definition container.hpp:70
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
Container for parameters used by the grand product (permutation, lookup) Honk relations.
static VectorField zero() noexcept
static VectorField broadcast(const Field &s) noexcept
static VectorField one() noexcept
static constexpr field one()
static field random_element(numeric::RNG *engine=nullptr) noexcept
static constexpr field zero()
bb::VectorField< bb::Bn254FrParams > Vec