Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
cycle_scalar.hpp
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
7#pragma once
8
12
13namespace bb::stdlib {
14
15// Forward declaration
16template <typename Builder> class cycle_group;
17
31template <typename Builder> class cycle_scalar {
32 public:
34 using Curve = typename Builder::EmbeddedCurve;
37
38 static constexpr size_t NUM_BITS = ScalarField::modulus.get_msb() + 1; // equivalent for both bn254 and grumpkin
39 static constexpr size_t LO_BITS = field_t::native::Params::MAX_BITS_PER_ENDOMORPHISM_SCALAR;
40 static constexpr size_t HI_BITS = NUM_BITS - LO_BITS;
41
42 // Enforce the architectural constraint that cycle_scalar is hardcoded for 254-bit scalars
43 static_assert(NUM_BITS == 254);
44 static_assert(LO_BITS == 128 && HI_BITS == 126);
45
46 enum class SkipValidation { FLAG };
47
48 private:
49 field_t _lo; // LO_BITS of the scalar
50 field_t _hi; // Remaining HI_BITS of the scalar
51
62
63 cycle_scalar(const field_t& lo, const field_t& hi, SkipValidation flag);
64
68 void validate_scalar_is_in_field() const;
69
70 public:
71 cycle_scalar(const ScalarField& in = 0);
72 cycle_scalar(const field_t& lo, const field_t& hi);
74 explicit cycle_scalar(BigScalarField& scalar);
75
76 [[nodiscard]] bool is_constant() const;
77 ScalarField get_value() const;
78 Builder* get_context() const { return _lo.get_context() != nullptr ? _lo.get_context() : _hi.get_context(); }
79
80 const field_t& lo() const { return _lo; }
81 const field_t& hi() const { return _hi; }
82
115};
116
117} // namespace bb::stdlib
Represents a member of the Grumpkin curve scalar field (i.e. BN254 base field).
typename Builder::EmbeddedCurve Curve
typename Curve::ScalarField ScalarField
stdlib::bigfield< Builder, typename ScalarField::Params > BigScalarField
static constexpr size_t NUM_BITS
const field_t & hi() const
ScalarField get_value() const
const field_t & lo() const
static std::pair< uint256_t, uint256_t > decompose_into_lo_hi_u256(const uint256_t &value)
Decompose a uint256_t value into lo and hi parts for cycle_scalar representation.
static cycle_scalar from_witness(Builder *context, const ScalarField &value)
Construct a cycle scalar from a witness value in the Grumpkin scalar field.
static constexpr size_t LO_BITS
void validate_scalar_is_in_field() const
Validates that the scalar (lo + hi * 2^LO_BITS) is less than the Grumpkin scalar field modulus.
void unset_free_witness_tag()
Unset the free witness flag for the cycle scalar's tags.
Builder * get_context() const
void set_free_witness_tag()
Set the free witness flag for the cycle scalar's tags.
static constexpr size_t HI_BITS
OriginTag get_origin_tag() const
Get the origin tag of the cycle_scalar (a merge of the lo and hi tags)
void set_origin_tag(const OriginTag &tag)
Set the origin tag of lo and hi members of cycle scalar.
void unset_free_witness_tag() const
Unset the free witness flag for the field element's tag.
Definition field.hpp:356
Builder * get_context() const
Definition field.hpp:419
OriginTag get_origin_tag() const
Definition field.hpp:346
void set_free_witness_tag()
Set the free witness flag for the field element's tag.
Definition field.hpp:351
void set_origin_tag(const OriginTag &new_tag) const
Definition field.hpp:345
StrictMock< MockContext > context
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
This file contains part of the logic for the Origin Tag mechanism that tracks the use of in-circuit p...