Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
accumulator.test.cpp
Go to the documentation of this file.
2
5
6#include <gtest/gtest.h>
7
8namespace {
9
10using Fr = bb::fr;
11using Acc = bb::Accumulator<Fr>;
13
14TEST(AccumulatorTest, DefaultConstructorReducesToZero)
15{
16 Acc acc;
17 EXPECT_TRUE(acc.reduce().is_zero());
18}
19
20TEST(AccumulatorTest, ScalarOnlyAddsMatchSerialSum)
21{
22 Acc acc;
24 for (auto& x : xs) {
26 }
27 Fr expected = Fr::zero();
28 for (const auto& x : xs) {
29 acc += x;
30 expected = expected + x;
31 }
32 EXPECT_EQ(acc.reduce(), expected);
33}
34
35TEST(AccumulatorTest, VectorOnlyAddsMatchSerialSum)
36{
37 Acc acc;
38 // Three width-5 lane contributions; total = 15 random Fr's summed.
40 for (auto& x : xs) {
42 }
43 for (size_t group = 0; group < 3; ++group) {
45 xs[5 * group + 0], xs[5 * group + 1], xs[5 * group + 2], xs[5 * group + 3], xs[5 * group + 4]
46 };
47 acc += Vec(lanes);
48 }
49 Fr expected = Fr::zero();
50 for (const auto& x : xs) {
51 expected = expected + x;
52 }
53 EXPECT_EQ(acc.reduce(), expected);
54}
55
56TEST(AccumulatorTest, MixedScalarAndVectorAdds)
57{
58 // Two width-5 lane blocks + a 3-element scalar tail — the canonical
59 // bulk + tail shape produced by vectorized_for inside a kernel.
60 Acc acc;
62 for (auto& x : xs) {
64 }
65 acc += Vec(std::array<Fr, 5>{ xs[0], xs[1], xs[2], xs[3], xs[4] });
66 acc += Vec(std::array<Fr, 5>{ xs[5], xs[6], xs[7], xs[8], xs[9] });
67 acc += xs[10];
68 acc += xs[11];
69 acc += xs[12];
70
71 Fr expected = Fr::zero();
72 for (const auto& x : xs) {
73 expected = expected + x;
74 }
75 EXPECT_EQ(acc.reduce(), expected);
76}
77
78TEST(AccumulatorTest, ReduceOrderInvariantOverLanePermutation)
79{
80 // Field addition is associative + commutative, so reduce()'s tree-shape
81 // is order-invariant. Verifies the implementation doesn't accidentally
82 // depend on a specific summation order.
83 std::array<Fr, 5> lanes_a = { Fr(11), Fr(22), Fr(33), Fr(44), Fr(55) };
84 std::array<Fr, 5> lanes_b = { Fr(55), Fr(44), Fr(33), Fr(22), Fr(11) };
85 Acc a;
86 a += Vec(lanes_a);
87 Acc b;
88 b += Vec(lanes_b);
89 EXPECT_EQ(a.reduce(), b.reduce());
90}
91
92} // namespace
TEST(acir_formal_proofs, uint_terms_add)
Tests 128-bit unsigned addition Verifies that the ACIR implementation of addition is correct Executio...
FF a
FF b
field< Bn254FrParams > fr
Definition fr.hpp:155
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
Curve::ScalarField Fr
static field random_element(numeric::RNG *engine=nullptr) noexcept
BB_INLINE constexpr field reduce() const noexcept
reduce once, i.e., if the value is bigger than the modulus, subtract off the modulus once.
static constexpr field zero()
bb::VectorField< bb::Bn254FrParams > Vec