12#include <gtest/gtest.h>
26#if defined(__SIZEOF_INT128__) && !defined(__wasm__)
27TEST(BN254Fr, CompileTimeMultiplication)
29 constexpr fr a{ 0x20565a572c565a66, 0x7bccd0f01f5f7bff, 0x63ec2beaad64711f, 0x624953caaf44a814 };
30 constexpr fr b{ 0xa17307a2108adeea, 0x74629976c14c5e2b, 0x9ce6f072ab1740ee, 0x398c753702b2bef0 };
31 constexpr fr expected{ 0xe8cdd06343386834, 0x8cbb3f556258a9af, 0x5aef2f34f2d66fd4, 0x2d8263c7e10213ca };
33 constexpr fr result =
a *
b;
34 static_assert(result == expected);
37TEST(BN254Fr, CompileTimeSquaring)
39 constexpr fr a{ 0x20565a572c565a66, 0x7bccd0f01f5f7bff, 0x63ec2beaad64711f, 0x624953caaf44a814 };
40 constexpr fr expected{ 0x3e928bdb06267b99, 0x1e5834571f52dfbf, 0x3d63bdf9bf7d0d4b, 0x353bb31adaa033c7 };
42 constexpr fr result =
a.
sqr();
43 static_assert(result == expected);
46TEST(BN254Fr, CompileTimeAddition)
48 constexpr fr a{ 0x20565a572c565a66, 0x7bccd0f01f5f7bff, 0x63ec2beaad64711f, 0x624953caaf44a814 };
49 constexpr fr b{ 0xa17307a2108adeea, 0x74629976c14c5e2b, 0x9ce6f072ab1740ee, 0x398c753702b2bef0 };
50 constexpr fr expected{ 0x3a0576d15ce1394e, 0x9fc799d5ed38f908, 0x903290f055790153, 0x3b0d2c1bef9426b1 };
52 constexpr fr result =
a +
b;
53 static_assert(result == expected);
56TEST(BN254Fr, CompileTimeSubtraction)
58 constexpr fr a{ 0xcfbcfcf457cf2d38, 0x7b27af26ce62aa61, 0xf0378e90d48f2b92, 0x4734b22cb21ded };
59 constexpr fr b{ 0x569fdb1db5198770, 0x446ddccef8347d52, 0xef215227182d22a, 0x8281b4fb109306 };
60 constexpr fr expected{ 0xe10cfe82b5a5ca, 0x8721a2e8c9a10e32, 0x51e604db660f0a22, 0x608d4fe2f404cb3b };
62 constexpr fr result =
a -
b;
63 static_assert(result == expected);
67TEST(BN254Fr, CompileTimeInversion)
69 constexpr fr a{ 0x20565a572c565a66, 0x7bccd0f01f5f7bff, 0x63ec2beaad64711f, 0x624953caaf44a814 };
79TEST(BN254Fr, SplitIntoEndomorphismScalars)
82 fr k1 = { 0, 0, 0, 0 };
83 fr k2 = { 0, 0, 0, 0 };
87 fr result{ 0, 0, 0, 0 };
100 EXPECT_EQ(result, k);
103TEST(BN254Fr, SplitIntoEndomorphismScalarsSimple)
105 fr input = { 1, 0, 0, 0 };
106 fr k = { 0, 0, 0, 0 };
107 fr k1 = { 0, 0, 0, 0 };
108 fr k2 = { 0, 0, 0, 0 };
112 fr result{ 0, 0, 0, 0 };
120 result = k2 * lambda;
121 result = k1 - result;
124 for (
size_t i = 0; i < 4; ++i) {
125 EXPECT_EQ(result.data[i], k.
data[i]);
131TEST(BN254Fr, SplitEndomorphismNegativeK2)
136 {{ 0x01624731e1195570, 0x3ba491482db4da14, 0x59e26bcea0d48bac, 0x0 },
"m=1"},
137 {{ 0x02c48e63c232aadf, 0x774922905b69b428, 0xb3c4d79d41a91758, 0x0 },
"m=2"},
138 {{ 0x0426d595a34c004e, 0xb2edb3d8891e8e3c, 0x0da7436be27da304, 0x1 },
"m=3"},
144 for (
const auto& tc : cases) {
145 fr k{ tc.limbs[0], tc.limbs[1], tc.limbs[2], tc.limbs[3] };
151 k1.self_to_montgomery_form();
152 k2.self_to_montgomery_form();
153 fr result = k1 - k2 * lambda;
156 EXPECT_EQ(result, k) << tc.tag;
166TEST(BN254Fr, EquivalentRandomness)
169 auto random_lo =
fr(random_uint512.
lo);
170 auto random_hi =
fr(random_uint512.
hi);
173 EXPECT_EQ(random_lo + pow_2_256 * random_hi,
fr((random_uint512 % r).lo));
uint512_t get_random_uint512()
constexpr T get_msb(const T in)
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
Entry point for Barretenberg command-line interface.
field< Bn254FrParams > fr
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
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)
Full-width endomorphism decomposition: k ≡ k1 - k2·λ (mod r). Modifies the field elements k1 and 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