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
30template <typename Builder> class cycle_scalar {
31 public:
33 using Curve = typename Builder::EmbeddedCurve;
36
37 static constexpr size_t NUM_BITS = ScalarField::modulus.get_msb() + 1; // equivalent for both bn254 and grumpkin
38 static constexpr size_t LO_BITS = field_t::native::Params::MAX_BITS_PER_ENDOMORPHISM_SCALAR;
39 static constexpr size_t HI_BITS = NUM_BITS - LO_BITS;
40
41 field_t lo; // LO_BITS of the scalar
42 field_t hi; // Remaining HI_BITS of the scalar
43
44 private:
47 // if our scalar multiplier is a bn254 FF scalar (e.g. pedersen hash),
48 // we want to validate the cycle_scalar < bn254::fr::modulus *not* grumpkin::fr::modulus
50
61
72
73 public:
74 // AUDITTODO: this is used only in the fuzzer.
75 cycle_scalar(const ScalarField& _in = 0);
76 cycle_scalar(const field_t& _lo, const field_t& _hi);
77 // AUDITTODO: this is used only in the fuzzer. Its not inherently problematic, but perhaps the fuzzer should use a
78 // production entrypoint.
80 static cycle_scalar from_u256_witness(Builder* context, const uint256_t& bitstring);
82 explicit cycle_scalar(BigScalarField& scalar);
83
84 [[nodiscard]] bool is_constant() const;
85 ScalarField get_value() const;
86 Builder* get_context() const { return lo.get_context() != nullptr ? lo.get_context() : hi.get_context(); }
87 [[nodiscard]] size_t num_bits() const { return _num_bits; }
88 [[nodiscard]] bool skip_primality_test() const { return _skip_primality_test; }
93
98 void validate_scalar_is_in_field() const;
99
111 void set_origin_tag(const OriginTag& tag) const
112 {
113 lo.set_origin_tag(tag);
114 hi.set_origin_tag(tag);
115 }
132};
133
134} // 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
bool skip_primality_test() const
bool use_bn254_scalar_field_for_primality_test() const
static cycle_scalar from_u256_witness(Builder *context, const uint256_t &bitstring)
Construct a cycle scalar from a uint256_t witness bitstring.
stdlib::bigfield< Builder, typename ScalarField::Params > BigScalarField
static constexpr size_t NUM_BITS
ScalarField get_value() const
bool _use_bn254_scalar_field_for_primality_test
cycle_scalar(const field_t &_lo, const field_t &_hi, const size_t bits, const bool skip_primality_test, const bool use_bn254_scalar_field_for_primality_test)
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 appropriate 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.
void set_origin_tag(const OriginTag &tag) const
Set the origin tag of lo and hi members of cycle scalar.
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)
static cycle_scalar create_from_bn254_scalar(const field_t &_in)
Construct a cycle scalar (grumpkin scalar field element) from a bn254 scalar field element.
void unset_free_witness_tag() const
Unset the free witness flag for the field element's tag.
Definition field.hpp:343
Builder * get_context() const
Definition field.hpp:397
OriginTag get_origin_tag() const
Definition field.hpp:333
void set_free_witness_tag()
Set the free witness flag for the field element's tag.
Definition field.hpp:338
void set_origin_tag(const OriginTag &new_tag) const
Definition field.hpp:332
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...