Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
fr.test.cpp
Go to the documentation of this file.
1
11#include "fr.hpp"
13#include <gtest/gtest.h>
14
15using namespace bb;
16
17namespace {
19} // namespace
20
21// ================================
22// Fixed Compile-Time Tests (field-specific expected values)
23// These tests use hardcoded expected values that are only valid for native builds (R = 2^256).
24// WASM uses R = 2^261.
25// ================================
26
27#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
28TEST(BN254Fr, CompileTimeMultiplication)
29{
30 constexpr fr a{ 0x20565a572c565a66, 0x7bccd0f01f5f7bff, 0x63ec2beaad64711f, 0x624953caaf44a814 };
31 constexpr fr b{ 0xa17307a2108adeea, 0x74629976c14c5e2b, 0x9ce6f072ab1740ee, 0x398c753702b2bef0 };
32 constexpr fr expected{ 0xe8cdd06343386834, 0x8cbb3f556258a9af, 0x5aef2f34f2d66fd4, 0x2d8263c7e10213ca };
33
34 constexpr fr result = a * b;
35 static_assert(result == expected);
36}
37
38TEST(BN254Fr, CompileTimeSquaring)
39{
40 constexpr fr a{ 0x20565a572c565a66, 0x7bccd0f01f5f7bff, 0x63ec2beaad64711f, 0x624953caaf44a814 };
41 constexpr fr expected{ 0x3e928bdb06267b99, 0x1e5834571f52dfbf, 0x3d63bdf9bf7d0d4b, 0x353bb31adaa033c7 };
42
43 constexpr fr result = a.sqr();
44 static_assert(result == expected);
45}
46
47TEST(BN254Fr, CompileTimeAddition)
48{
49 constexpr fr a{ 0x20565a572c565a66, 0x7bccd0f01f5f7bff, 0x63ec2beaad64711f, 0x624953caaf44a814 };
50 constexpr fr b{ 0xa17307a2108adeea, 0x74629976c14c5e2b, 0x9ce6f072ab1740ee, 0x398c753702b2bef0 };
51 constexpr fr expected{ 0x3a0576d15ce1394e, 0x9fc799d5ed38f908, 0x903290f055790153, 0x3b0d2c1bef9426b1 };
52
53 constexpr fr result = a + b;
54 static_assert(result == expected);
55}
56
57TEST(BN254Fr, CompileTimeSubtraction)
58{
59 constexpr fr a{ 0xcfbcfcf457cf2d38, 0x7b27af26ce62aa61, 0xf0378e90d48f2b92, 0x4734b22cb21ded };
60 constexpr fr b{ 0x569fdb1db5198770, 0x446ddccef8347d52, 0xef215227182d22a, 0x8281b4fb109306 };
61 constexpr fr expected{ 0xe10cfe82b5a5ca, 0x8721a2e8c9a10e32, 0x51e604db660f0a22, 0x608d4fe2f404cb3b };
62
63 constexpr fr result = a - b;
64 static_assert(result == expected);
65}
66#endif
67
68TEST(BN254Fr, CompileTimeInversion)
69{
70 constexpr fr a{ 0x20565a572c565a66, 0x7bccd0f01f5f7bff, 0x63ec2beaad64711f, 0x624953caaf44a814 };
71 constexpr fr inv = a.invert();
72 // Verify a * a^-1 = 1
73 static_assert(a * inv == fr::one());
74}
75
76// ================================
77// BN254 Scalar Field Specific
78// ================================
79// AUDITTODO: delete this (`multiplicative_generator` is misnamed and is no longer used.)
80TEST(BN254Fr, MultiplicativeGenerator)
81{
82 EXPECT_EQ(fr::multiplicative_generator(), fr(5));
83}
84
85TEST(BN254Fr, SplitIntoEndomorphismScalars)
86{
88 fr k1 = { 0, 0, 0, 0 };
89 fr k2 = { 0, 0, 0, 0 };
90
92
93 fr result{ 0, 0, 0, 0 };
94
97
98 fr lambda = fr::cube_root_of_unity();
99 result = k2 * lambda;
100 result = k1 - result;
101
103 EXPECT_EQ(result, k);
104}
105
106TEST(BN254Fr, SplitIntoEndomorphismScalarsSimple)
107{
108 fr input = { 1, 0, 0, 0 };
109 fr k = { 0, 0, 0, 0 };
110 fr k1 = { 0, 0, 0, 0 };
111 fr k2 = { 0, 0, 0, 0 };
112 fr::__copy(input, k);
113
115
116 fr result{ 0, 0, 0, 0 };
119
120 fr lambda = fr::cube_root_of_unity();
121 result = k2 * lambda;
122 result = k1 - result;
123
125 for (size_t i = 0; i < 4; ++i) {
126 EXPECT_EQ(result.data[i], k.data[i]);
127 }
128}
129
130// ================================
131// Regression / Optimization Tests
132// ================================
133
134// Tests that (lo + 2^256 * hi) mod r == ((lo|hi) % r) in uint512_t
135// This validates the optimization of avoiding slow uint512_t modulo
136TEST(BN254Fr, EquivalentRandomness)
137{
138 uint512_t random_uint512 = engine.get_random_uint512();
139 auto random_lo = fr(random_uint512.lo);
140 auto random_hi = fr(random_uint512.hi);
142 constexpr auto pow_2_256 = fr(uint256_t(1) << 128).sqr();
143 EXPECT_EQ(random_lo + pow_2_256 * random_hi, fr((random_uint512 % r).lo));
144}
uint512_t get_random_uint512()
Definition engine.hpp:38
FF a
FF b
numeric::RNG & engine
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
Definition engine.cpp:190
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
field< Bn254FrParams > fr
Definition fr.hpp:174
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
static constexpr field cube_root_of_unity()
static constexpr field one()
static constexpr uint256_t modulus
static void split_into_endomorphism_scalars(const field &k, field &k1, field &k2)
constexpr field invert() const noexcept
static field random_element(numeric::RNG *engine=nullptr) noexcept
BB_INLINE constexpr field sqr() const noexcept
static BB_INLINE void __copy(const field &a, field &r) noexcept
BB_INLINE constexpr void self_from_montgomery_form() &noexcept
static constexpr field multiplicative_generator() noexcept
BB_INLINE constexpr void self_to_montgomery_form() &noexcept