Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
field_conversion.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Sergei], commit: 777717f6af324188ecd6bb68c3c86ee7befef94d}
3// external_1: { status: Complete, auditors: [@ed25519 (Spearbit)], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
14#pragma once
15
24
25namespace bb::stdlib {
26
27template <typename Field> class StdlibCodec {
28 public:
29 using DataType = Field;
30 using Builder = typename Field::Builder;
35
44 template <typename T> static T convert_challenge(const fr& challenge)
45 {
46 if constexpr (std::is_same_v<T, fr>) {
47 return challenge;
48 } else if constexpr (std::is_same_v<T, fq>) {
49 // Sanity check that the input challenge fits into the first 2 bigfield limbs.
50 BB_ASSERT_LT(static_cast<uint256_t>(challenge.get_value()).get_msb(),
51 T::NUM_LIMB_BITS * 2,
52 "field_conversion: convert_challenge");
53 Builder* builder = challenge.get_context();
54 // All challenges must be circuit witnesses.
56 BB_ASSERT(!challenge.is_constant());
57 return T(challenge, fr::from_witness_index(builder, builder->zero_idx()));
58 }
59 }
60
61 static std::vector<fr> convert_goblin_fr_to_bn254_frs(const goblin_field<Builder>& input)
62 {
63 return { input.limbs[0], input.limbs[1] };
64 }
65
66 static std::vector<fr> convert_grumpkin_fr_to_bn254_frs(const fq& input)
67 {
68 static constexpr uint64_t NUM_LIMB_BITS = fq::NUM_LIMB_BITS;
69
70 static constexpr bb::fr shift(static_cast<uint256_t>(1) << NUM_LIMB_BITS);
71 std::vector<fr> result(2);
72 result[0] = input.binary_basis_limbs[0].element + (input.binary_basis_limbs[1].element * shift);
73 result[1] = input.binary_basis_limbs[2].element + (input.binary_basis_limbs[3].element * shift);
74 return result;
75 }
76
80 template <typename T> static constexpr size_t calc_num_fields()
81 {
82 if constexpr (IsAnyOf<T, fr>) {
84 } else if constexpr (IsAnyOf<T, fq, goblin_field<Builder>>) {
87 return 2 * calc_num_fields<typename T::BaseField>();
88 } else {
89 // Array or Univariate
90 return calc_num_fields<typename T::value_type>() * (std::tuple_size<T>::value);
91 }
92 }
93
130 template <typename T> static T deserialize_from_fields(std::span<const fr> fr_vec)
131 {
132 using field_ct = fr;
133 using bigfield_ct = fq;
134
135 constexpr size_t expected_size = calc_num_fields<T>();
136 BB_ASSERT_EQ(fr_vec.size(), expected_size);
137
138 BB_ASSERT(validate_context<Builder>(fr_vec));
139
140 if constexpr (IsAnyOf<T, field_ct>) {
141 // Case 1: input type matches the output type
142 return fr_vec[0];
143 } else if constexpr (IsAnyOf<T, bigfield_ct>) {
144 // Case 2: bigfield is reconstructed from low and high limbs with in-field validation.
145 // This ensures aliased values (>= Fq::modulus) are rejected.
146 T result(fr_vec[0], fr_vec[1]);
147 result.assert_is_in_field();
148 return result;
149 } else if constexpr (IsAnyOf<T, goblin_field<Builder>>) {
150 // Case 3: goblin_field stores limbs as-is; range validation is deferred to Translator circuit.
151 return T(fr_vec[0], fr_vec[1]);
153 // Case 4 and 5: Convert a vector of frs to a group element
154 using Basefield = typename T::BaseField;
155
156 constexpr size_t base_field_frs = expected_size / 2;
157
158 Basefield x = deserialize_from_fields<Basefield>(fr_vec.subspan(0, base_field_frs));
159 Basefield y = deserialize_from_fields<Basefield>(fr_vec.subspan(base_field_frs, base_field_frs));
160
161 // Construct the group element (validates the point is on curve).
162 // The 2-arg constructor auto-detects infinity from (x == 0 && y == 0).
163 // For goblin_element (bn254 with Mega arithmetization), the on-curve check is delegated to ECCVM, see
164 // `on_curve_check` in `ECCVMTranscriptRelationImpl`.
165 return T(x, y, /*assert_on_curve=*/true);
166 } else {
167 // Case 6: Array or Univariate
168 T val;
169 using element_type = typename T::value_type;
170 const size_t scalar_frs = calc_num_fields<element_type>();
171
172 size_t i = 0;
173 for (auto& x : val) {
174 x = deserialize_from_fields<element_type>(fr_vec.subspan(scalar_frs * i, scalar_frs));
175 ++i;
176 }
177 return val;
178 }
179 }
180
231 template <typename T> static std::vector<fr> serialize_to_fields(const T& val)
232 {
233 using field_ct = fr;
234 using bigfield_ct = fq;
235
236 if constexpr (IsAnyOf<T, field_ct>) {
237 return std::vector<T>{ val };
238 } else if constexpr (IsAnyOf<T, bigfield_ct>) {
240 } else if constexpr (IsAnyOf<T, goblin_field<Builder>>) {
242 } else if constexpr (IsAnyOf<T, grumpkin_commitment>) {
243 // Canonicalize: output (0,0) for infinity points, critical for the IPA accumulation flow.
244 auto is_inf = val.is_point_at_infinity();
245 fr canon_x = fr::conditional_assign(is_inf, fr(0), val.x());
246 fr canon_y = fr::conditional_assign(is_inf, fr(0), val.y());
247 return { canon_x, canon_y };
248 } else if constexpr (IsAnyOf<T, bn254_commitment>) {
249 // bn254_commitment serialization does not standardize infinity (unlike grumpkin_commitment above).
250 // All existing code paths produce canonical (0,0) infinity, so just assert that invariant.
251 if (val.get_value().is_point_at_infinity()) {
252 BB_ASSERT(val.x().get_value() == 0 && val.y().get_value() == 0,
253 "serialize_to_fields: bn254_commitment point at infinity must be canonical (0,0)");
254 }
255 using BaseField = typename T::BaseField;
256
257 std::vector<field_ct> fr_vec_x = serialize_to_fields<BaseField>(val.x());
258 std::vector<field_ct> fr_vec_y = serialize_to_fields<BaseField>(val.y());
259 std::vector<field_ct> fr_vec(fr_vec_x.begin(), fr_vec_x.end());
260 fr_vec.insert(fr_vec.end(), fr_vec_y.begin(), fr_vec_y.end());
261 return fr_vec;
262 } else {
263 // Array or Univariate
265 for (auto& x : val) {
266 auto tmp_vec = serialize_to_fields<typename T::value_type>(x);
267 fr_vec.insert(fr_vec.end(), tmp_vec.begin(), tmp_vec.end());
268 }
269 return fr_vec;
270 }
271 }
280 static std::array<fr, 2> split_challenge(const fr& challenge)
281 {
282 constexpr size_t TOTAL_BITS = fr::native::modulus.get_msb() + 1; // 254
283 constexpr size_t lo_bits = TOTAL_BITS / 2; // 127
284 // Construct a unique lo/hi decomposition of the challenge (hi_bits will be 127)
285 const auto [lo, hi] = split_unique(challenge, lo_bits);
286 return std::array<fr, 2>{ lo, hi };
287 }
288
299 template <typename TargetType> static TargetType deserialize_from_frs(std::span<fr> elements, size_t& num_frs_read)
300 {
301 constexpr size_t num_frs = calc_num_fields<TargetType>();
302 BB_ASSERT_GTE(elements.size(), num_frs_read + num_frs);
303 TargetType result = deserialize_from_fields<TargetType>(elements.subspan(num_frs_read, num_frs));
304 num_frs_read += num_frs;
305 return result;
306 }
307};
308} // namespace bb::stdlib
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
#define BB_ASSERT_GTE(left, right,...)
Definition assert.hpp:128
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
#define BB_ASSERT_LT(left, right,...)
Definition assert.hpp:143
static constexpr size_t NUM_BN254_SCALARS
Definition fq.hpp:146
static constexpr size_t NUM_BN254_SCALARS
Definition fr.hpp:148
constexpr uint64_t get_msb() const
static constexpr size_t calc_num_fields()
Calculates the size of a type (in its native form) in terms of frs.
bigfield< Builder, bb::Bn254FqParams > fq
typename Field::Builder Builder
static std::vector< fr > convert_goblin_fr_to_bn254_frs(const goblin_field< Builder > &input)
static std::vector< fr > convert_grumpkin_fr_to_bn254_frs(const fq &input)
element< Builder, fq, fr, curve::BN254::Group > bn254_commitment
static T convert_challenge(const fr &challenge)
A stdlib Transcript method needed to convert an fr challenge to a bigfield one. Assumes that challeng...
static std::array< fr, 2 > split_challenge(const fr &challenge)
Split a challenge field element into two equal-width challenges.
static T deserialize_from_fields(std::span< const fr > fr_vec)
Core stdlib Transcript deserialization method.
static TargetType deserialize_from_frs(std::span< fr > elements, size_t &num_frs_read)
A stdlib VerificationKey-specific method.
static std::vector< fr > serialize_to_fields(const T &val)
Core stdlib Transcript serialization method.
std::array< Limb, NUM_LIMBS > binary_basis_limbs
Represents a bigfield element in the binary basis. A bigfield element is represented as a combination...
Definition bigfield.hpp:81
cycle_group represents a group Element of the proving system's embedded curve, i.e....
static field_t from_witness_index(Builder *ctx, uint32_t witness_index)
Definition field.cpp:67
Builder * get_context() const
Definition field.hpp:431
bb::fr get_value() const
Given a := *this, compute its value given by a.v * a.mul + a.add.
Definition field.cpp:836
static constexpr uint256_t modulus
Definition field.hpp:237
bool is_constant() const
Definition field.hpp:441
static field_t conditional_assign(const bool_t< Builder > &predicate, const field_t &lhs, const field_t &rhs)
Definition field.hpp:384
goblin_field wraps x/y coordinates of bn254 group elements when using goblin
std::array< field_ct, 2 > limbs
AluTraceBuilder builder
Definition alu.test.cpp:124
std::pair< field_t< Builder >, field_t< Builder > > split_unique(const field_t< Builder > &field, const size_t lo_bits, const bool skip_range_constraints)
Split a bn254 scalar field element into unique lo and hi limbs.
std::conditional_t< IsGoblinBigGroup< C, Fq, Fr, G >, element_goblin::goblin_element< C, goblin_field< C >, Fr, G >, element_default::element< C, Fq, Fr, G > > element
element wraps either element_default::element or element_goblin::goblin_element depending on parametr...
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13