Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
cycle_group.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
20#include <optional>
21
22namespace bb::stdlib {
23
31template <typename Builder> class cycle_group {
32 public:
37
43
48
49 // Bit-size for scalars represented in the ROM lookup tables used in the variable-base MSM algorithm
50 static constexpr size_t ROM_TABLE_BITS = 4;
51 static constexpr size_t NUM_BITS_FULL_FIELD_SIZE = bb::fq::modulus.get_msb() + 1;
52 // Domain separator for generating offset generator points in the variable-base MSM algorithm
53 static constexpr std::string_view OFFSET_GENERATOR_DOMAIN_SEPARATOR = "cycle_group_offset_generator";
54
55 // Since the cycle_group base field is the circuit's native field, it can be stored using two public inputs.
56 static constexpr size_t PUBLIC_INPUTS_SIZE = 2;
57
58 private:
67
68 public:
69 cycle_group(Builder* _context = nullptr);
71 cycle_group(const bb::fr& _x, const bb::fr& _y, bool _is_infinity);
72 cycle_group(const AffineElement& _in);
73 static cycle_group one(Builder* _context);
74 static cycle_group constant_infinity(Builder* _context = nullptr);
75 static cycle_group from_witness(Builder* _context, const AffineElement& _in);
76 static cycle_group from_constant_witness(Builder* _context, const AffineElement& _in);
77 Builder* get_context(const cycle_group& other) const;
78 Builder* get_context() const { return context; }
80 [[nodiscard]] bool is_constant() const { return x.is_constant() && y.is_constant() && _is_infinity.is_constant(); }
82 [[nodiscard]] bool is_constant_point_at_infinity() const
83 {
85 }
86#ifdef FUZZING
87 void set_point_at_infinity(const bool_t& is_infinity);
88#endif
89 void standardize();
90 bool is_standard() const { return this->_is_standard; };
92 void validate_on_curve() const;
101 const std::optional<AffineElement> hint = std::nullopt) const;
102 cycle_group operator+(const cycle_group& other) const;
103 cycle_group operator-(const cycle_group& other) const;
104 cycle_group operator-() const;
105 cycle_group& operator+=(const cycle_group& other);
106 cycle_group& operator-=(const cycle_group& other);
108 const std::vector<BigScalarField>& scalars,
110 {
111 std::vector<cycle_scalar> cycle_scalars;
112 for (auto scalar : scalars) {
113 cycle_scalars.emplace_back(scalar);
114 }
115 return batch_mul(base_points, cycle_scalars, context);
116 }
117 static cycle_group batch_mul(const std::vector<cycle_group>& base_points,
118 const std::vector<cycle_scalar>& scalars,
119 const GeneratorContext& context = {});
120 cycle_group operator*(const cycle_scalar& scalar) const;
121 cycle_group& operator*=(const cycle_scalar& scalar);
122 cycle_group operator*(const BigScalarField& scalar) const;
123 cycle_group& operator*=(const BigScalarField& scalar);
124 bool_t operator==(cycle_group& other);
125 void assert_equal(cycle_group& other, std::string const& msg = "cycle_group::assert_equal");
126 static cycle_group conditional_assign(const bool_t& predicate, const cycle_group& lhs, const cycle_group& rhs);
127 cycle_group operator/(const cycle_group& other) const;
128
134 void set_origin_tag(OriginTag tag) const
135 {
136 x.set_origin_tag(tag);
137 y.set_origin_tag(tag);
139 }
149
159
169
174 {
175 // Origin tags should be updated within
176 x.fix_witness();
177 y.fix_witness();
179
180 // This is now effectively a constant
182 }
188 uint32_t set_public()
189 {
190 uint32_t start_idx = x.set_public();
191 y.set_public();
192 return start_idx;
193 }
194
204 {
205 cycle_group result(limbs[0], limbs[1], false);
206 result.validate_on_curve();
207 return result;
208 }
209
212
213 private:
215 // The point is considered to be `standard` or in `standard form` when:
216 // - It's not a point at infinity, and the coordinates belong to the curve
217 // - It's a point at infinity and both of the coordinates are set to be 0. (0, 0)
218 // Most of the time it is true, so we won't need to do extra conditional_assign
219 // during `get_standard_form`, `assert_equal` or `==` calls
220 // However sometimes it won't be the case(due to some previous design choices),
221 // so we can handle these cases using this flag
224
226 std::span<cycle_group> base_points,
227 std::span<AffineElement const> offset_generators,
228 bool unconditional_add);
229
231 std::span<AffineElement> base_points);
232
233 // Internal implementation for unconditional_add and unconditional_subtract
235 bool is_addition,
236 const std::optional<AffineElement> hint) const;
237};
238
239template <typename Builder> inline std::ostream& operator<<(std::ostream& os, cycle_group<Builder> const& v)
240{
241 return os << "{ " << v.x << ", " << v.y << " }";
242}
243} // namespace bb::stdlib
element class. Implements ecc group arithmetic using Jacobian coordinates See https://hyperelliptic....
Definition element.hpp:33
group class. Represents an elliptic curve group element. Group is parametrised by Fq and Fr
Definition group.hpp:36
group_elements::affine_element< Fq, Fr, Params > affine_element
Definition group.hpp:42
group_elements::element< Fq, Fr, Params > element
Definition group.hpp:41
constexpr uint64_t get_msb() const
Implements boolean logic in-circuit.
Definition bool.hpp:59
bool get_value() const
Definition bool.hpp:111
void fix_witness()
Definition bool.hpp:132
bool is_constant() const
Definition bool.hpp:113
void set_origin_tag(const OriginTag &new_tag) const
Definition bool.hpp:128
void set_free_witness_tag()
Definition bool.hpp:130
void unset_free_witness_tag()
Definition bool.hpp:131
OriginTag get_origin_tag() const
Definition bool.hpp:129
cycle_group represents a group Element of the proving system's embedded curve, i.e....
cycle_group dbl(const std::optional< AffineElement > hint=std::nullopt) const
Evaluates a point doubling using Ultra ECC double gate (if non-constant)
static cycle_group from_constant_witness(Builder *_context, const AffineElement &_in)
Converts a native AffineElement into a witness, but constrains the witness values to be known constan...
stdlib::bool_t< Builder > bool_t
cycle_group & operator*=(const cycle_scalar &scalar)
void standardize()
Convert the point to standard form.
cycle_group unconditional_add(const cycle_group &other, const std::optional< AffineElement > hint=std::nullopt) const
void validate_on_curve() const
On-curve check.
cycle_group get_standard_form()
Convert the point to standard form.
bool_t operator==(cycle_group &other)
cycle_group & operator-=(const cycle_group &other)
static cycle_group conditional_assign(const bool_t &predicate, const cycle_group &lhs, const cycle_group &rhs)
void unset_free_witness_tag()
Unset the free witness flag for the cycle_group's tags.
static cycle_group reconstruct_from_public(const std::span< const field_t, 2 > &limbs)
Reconstruct a cycle_group from limbs (generally stored in the public inputs)
cycle_group checked_unconditional_subtract(const cycle_group &other, const std::optional< AffineElement > hint=std::nullopt) const
Will evaluate ECC point subtraction over *this and other.
cycle_group _unconditional_add_or_subtract(const cycle_group &other, bool is_addition, const std::optional< AffineElement > hint) const
Will evaluate ECC point addition or subtraction over *this and other.
static cycle_group from_witness(Builder *_context, const AffineElement &_in)
Converts an AffineElement into a circuit witness.
cycle_group operator-() const
Negates a point.
static cycle_group one(Builder *_context)
Construct a constant cycle_group representation of Group::one.
void set_free_witness_tag()
Set the free witness flag for the cycle_group's tags.
void set_origin_tag(OriginTag tag) const
Set the origin tag for x, y and _is_infinity members of cycle_group.
crypto::GeneratorContext< Curve > GeneratorContext
cycle_group operator/(const cycle_group &other) const
cycle_group & operator+=(const cycle_group &other)
bb::grumpkin::g1::affine_element AffineElement
static cycle_group constant_infinity(Builder *_context=nullptr)
Construct a constant point at infinity.
bool is_constant_point_at_infinity() const
bool_t is_point_at_infinity() const
static batch_mul_internal_output _variable_base_batch_mul_internal(std::span< cycle_scalar > scalars, std::span< cycle_group > base_points, std::span< AffineElement const > offset_generators, bool unconditional_add)
Internal logic to perform a variable-base batch mul using the Straus MSM algorithm.
stdlib::bigfield< Builder, bb::fq::Params > BigScalarField
static constexpr size_t ROM_TABLE_BITS
static constexpr size_t NUM_BITS_FULL_FIELD_SIZE
stdlib::field_t< Builder > field_t
cycle_group(Builder *_context=nullptr)
Construct a new constant point at infinity cycle group object.
static constexpr size_t PUBLIC_INPUTS_SIZE
AffineElement get_value() const
OriginTag get_origin_tag() const
Get the origin tag of cycle_group (a merege of origin tags of x, y and _is_infinity members)
cycle_group operator*(const cycle_scalar &scalar) const
static batch_mul_internal_output _fixed_base_batch_mul_internal(std::span< cycle_scalar > scalars, std::span< AffineElement > base_points)
Internal algorithm to perform a fixed-base batch mul.
void assert_equal(cycle_group &other, std::string const &msg="cycle_group::assert_equal")
cycle_group operator+(const cycle_group &other) const
Will evaluate ECC point addition over *this and other.
::bb::stdlib::cycle_scalar< Builder > cycle_scalar
cycle_group unconditional_subtract(const cycle_group &other, const std::optional< AffineElement > hint=std::nullopt) const
Builder * get_context() const
cycle_group checked_unconditional_add(const cycle_group &other, const std::optional< AffineElement > hint=std::nullopt) const
Will evaluate ECC point addition over *this and other.
uint32_t set_public()
Set the witness indices representing the cycle_group to public.
static constexpr std::string_view OFFSET_GENERATOR_DOMAIN_SEPARATOR
static cycle_group batch_mul(const std::vector< cycle_group > &base_points, const std::vector< BigScalarField > &scalars, GeneratorContext context={})
Represents a member of the Grumpkin curve scalar field (i.e. BN254 base field).
uint32_t set_public() const
Definition field.hpp:412
void unset_free_witness_tag() const
Unset the free witness flag for the field element's tag.
Definition field.hpp:343
OriginTag get_origin_tag() const
Definition field.hpp:333
bool is_constant() const
Definition field.hpp:407
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
straus_lookup_table computes a lookup table of size 1 << table_bits
straus_scalar_slices decomposes an input scalar into bit-slices of size table_bits....
bb::group< bb::fr, bb::fq, G1Params > g1
Definition grumpkin.hpp:45
std::ostream & operator<<(std::ostream &os, uint256_t const &a)
Definition uint256.hpp:245
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...
static constexpr uint256_t modulus
Stores temporary variables produced by internal multiplication algorithms.