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
80TEST(BN254Fr, SplitIntoEndomorphismScalars)
81{
83 fr k1 = { 0, 0, 0, 0 };
84 fr k2 = { 0, 0, 0, 0 };
85
87
88 fr result{ 0, 0, 0, 0 };
89
92
93 fr lambda = fr::cube_root_of_unity();
94 result = k2 * lambda;
95 result = k1 - result;
96
98 EXPECT_EQ(result, k);
99}
100
101TEST(BN254Fr, SplitIntoEndomorphismScalarsSimple)
102{
103 fr input = { 1, 0, 0, 0 };
104 fr k = { 0, 0, 0, 0 };
105 fr k1 = { 0, 0, 0, 0 };
106 fr k2 = { 0, 0, 0, 0 };
107 fr::__copy(input, k);
108
110 // AUDITTODO: double check this test.
111 fr result{ 0, 0, 0, 0 };
114
115 fr lambda = fr::cube_root_of_unity();
116 result = k2 * lambda;
117 result = k1 - result;
118
120 for (size_t i = 0; i < 4; ++i) {
121 EXPECT_EQ(result.data[i], k.data[i]);
122 }
123}
124
125// ================================
126// Regression / Optimization Tests
127// ================================
128
129// Tests that (lo + 2^256 * hi) mod r == ((lo|hi) % r) in uint512_t
130// This validates the optimization of avoiding slow uint512_t modulo
131TEST(BN254Fr, EquivalentRandomness)
132{
133 uint512_t random_uint512 = engine.get_random_uint512();
134 auto random_lo = fr(random_uint512.lo);
135 auto random_hi = fr(random_uint512.hi);
137 constexpr auto pow_2_256 = fr(uint256_t(1) << 128).sqr();
138 EXPECT_EQ(random_lo + pow_2_256 * random_hi, fr((random_uint512 % r).lo));
139}
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:212
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()
BB_INLINE constexpr void self_to_montgomery_form_reduced() &noexcept
static constexpr field one()
static constexpr uint256_t modulus
BB_INLINE constexpr void self_from_montgomery_form_reduced() &noexcept
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
BB_INLINE constexpr void self_to_montgomery_form() &noexcept