Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
field_conversion.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
9
11
12static constexpr uint64_t NUM_LIMB_BITS = stdlib::NUM_LIMB_BITS_IN_FIELD_SIMULATION;
13static constexpr uint64_t TOTAL_BITS = 254;
14
29{
30 // Combines the two elements into one uint256_t, and then convert that to a grumpkin::fr
31 BB_ASSERT_LT(uint256_t(fr_vec[0]),
32 (uint256_t(1) << (NUM_LIMB_BITS * 2)),
33 "Conversion error here usually implies some bad proof serde or parsing");
34 BB_ASSERT_LT(uint256_t(fr_vec[1]),
35 (uint256_t(1) << (TOTAL_BITS - NUM_LIMB_BITS * 2)),
36 "Conversion error here usually implies some bad proof serde or parsing");
37 uint256_t value = uint256_t(fr_vec[0]) + (uint256_t(fr_vec[1]) << (NUM_LIMB_BITS * 2));
38 grumpkin::fr result(value);
39 return result;
40}
41
56{
57 // Goal is to slice up the 64 bit limbs of grumpkin::fr/uint256_t to mirror the 68 bit limbs of bigfield
58 // We accomplish this by dividing the grumpkin::fr's value into two 68*2=136 bit pieces.
59 constexpr uint64_t LOWER_BITS = 2 * NUM_LIMB_BITS;
60 constexpr uint256_t LOWER_MASK = (uint256_t(1) << LOWER_BITS) - 1;
61 auto value = uint256_t(val);
62 BB_ASSERT_LT(value, (uint256_t(1) << TOTAL_BITS));
63 std::vector<bb::fr> result(2);
64 result[0] = static_cast<uint256_t>(value & LOWER_MASK);
65 result[1] = static_cast<uint256_t>(value >> LOWER_BITS);
66 BB_ASSERT_LT(static_cast<uint256_t>(result[1]), (uint256_t(1) << (TOTAL_BITS - LOWER_BITS)));
67 return result;
68}
69
71{
72 const uint64_t NUM_BITS_IN_TWO_LIMBS = 2 * NUM_LIMB_BITS; // the number of bits in 2 bigfield limbs which is 136
73
74 constexpr uint256_t LIMB_MASK =
75 (uint256_t(1) << NUM_BITS_IN_TWO_LIMBS) - 1; // split bn254_fr into two 136 bit pieces
76 const uint256_t value = f;
77 const uint256_t low = static_cast<uint256_t>(value & LIMB_MASK);
78 const uint256_t hi = static_cast<uint256_t>(value >> NUM_BITS_IN_TWO_LIMBS);
79 BB_ASSERT_EQ(static_cast<uint256_t>(low) + (static_cast<uint256_t>(hi) << NUM_BITS_IN_TWO_LIMBS), value);
80
81 std::vector<bb::fr> fr_vec{ low, hi };
82 return convert_from_bn254_frs<grumpkin::fr>(fr_vec);
83}
84
85} // namespace bb::field_conversion
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:59
#define BB_ASSERT_LT(left, right,...)
Definition assert.hpp:115
std::vector< bb::fr > convert_grumpkin_fr_to_bn254_frs(const grumpkin::fr &val)
Converts grumpkin::fr to 2 bb::fr elements.
grumpkin::fr convert_to_grumpkin_fr(const bb::fr &f)
grumpkin::fr convert_grumpkin_fr_from_bn254_frs(std::span< const bb::fr > fr_vec)
Converts 2 bb::fr elements to grumpkin::fr.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13