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
24template <typename Builder>
25concept IsUltraArithmetic = (Builder::CIRCUIT_TYPE == CircuitType::ULTRA);
26
36template <typename Builder> class cycle_group {
37 public:
41 using FF = typename Builder::FF;
42 using Curve = typename Builder::EmbeddedCurve;
43 using Group = typename Curve::Group;
44 using Element = typename Curve::Element;
49
50 static constexpr size_t STANDARD_NUM_TABLE_BITS = 1;
51 static constexpr size_t ULTRA_NUM_TABLE_BITS = 4;
52 static constexpr bool IS_ULTRA = Builder::CIRCUIT_TYPE == CircuitType::ULTRA;
54 static constexpr size_t NUM_BITS = ScalarField::modulus.get_msb() + 1;
55 static constexpr size_t NUM_ROUNDS = (NUM_BITS + TABLE_BITS - 1) / TABLE_BITS;
56 inline static constexpr std::string_view OFFSET_GENERATOR_DOMAIN_SEPARATOR = "cycle_group_offset_generator";
57
58 // Since the cycle_group base field is the circuit's native field, it can be stored using two public inputs.
59 static constexpr size_t PUBLIC_INPUTS_SIZE = 2;
60
64
65 private:
74
75 public:
76 cycle_group(Builder* _context = nullptr);
78 cycle_group(const FF& _x, const FF& _y, bool _is_infinity);
79 cycle_group(const AffineElement& _in);
80 static cycle_group one(Builder* _context);
81 static cycle_group from_witness(Builder* _context, const AffineElement& _in);
82 static cycle_group from_constant_witness(Builder* _context, const AffineElement& _in);
83 Builder* get_context(const cycle_group& other) const;
84 Builder* get_context() const { return context; }
86 [[nodiscard]] bool is_constant() const { return _is_constant; }
88 void set_point_at_infinity(const bool_t& is_infinity);
89 void standardize();
90 bool is_standard() const { return this->_is_standard; };
92 void validate_is_on_curve() const;
101 const std::optional<AffineElement> hint = std::nullopt) const;
103 const std::optional<AffineElement> hint = std::nullopt) const;
104 cycle_group operator+(const cycle_group& other) const;
105 cycle_group operator-(const cycle_group& other) const;
106 cycle_group operator-() const;
107 cycle_group& operator+=(const cycle_group& other);
108 cycle_group& operator-=(const cycle_group& other);
110 const std::vector<BigScalarField>& scalars,
112 {
113 std::vector<cycle_scalar> cycle_scalars;
114 for (auto scalar : scalars) {
115 cycle_scalars.emplace_back(scalar);
116 }
117 return batch_mul(base_points, cycle_scalars, context);
118 }
119 static cycle_group batch_mul(const std::vector<cycle_group>& base_points,
120 const std::vector<cycle_scalar>& scalars,
122 cycle_group operator*(const cycle_scalar& scalar) const;
123 cycle_group& operator*=(const cycle_scalar& scalar);
124 cycle_group operator*(const BigScalarField& scalar) const;
125 cycle_group& operator*=(const BigScalarField& scalar);
126 bool_t operator==(cycle_group& other);
127 void assert_equal(cycle_group& other, std::string const& msg = "cycle_group::assert_equal");
128 static cycle_group conditional_assign(const bool_t& predicate, const cycle_group& lhs, const cycle_group& rhs);
129 cycle_group operator/(const cycle_group& other) const;
130
136 void set_origin_tag(OriginTag tag) const
137 {
138 x.set_origin_tag(tag);
139 y.set_origin_tag(tag);
141 }
151
161
171
176 {
177 // Origin tags should be updated within
178 x.fix_witness();
179 y.fix_witness();
181
182 // This is now effectively a constant
184 }
190 uint32_t set_public()
191 {
192 uint32_t start_idx = x.set_public();
193 y.set_public();
194 return start_idx;
195 }
196
206 {
207 return cycle_group(limbs[0], limbs[1], false);
208 }
209
212
213 private:
216 // The point is considered to be `standard` or in `standard form` when:
217 // - It's not a point at infinity, and the coordinates belong to the curve
218 // - It's a point at infinity and both of the coordinates are set to be 0. (0, 0)
219 // Most of the time it is true, so we won't need to do extra conditional_assign
220 // during `get_standard_form`, `assert_equal` or `==` calls
221 // However sometimes it won't be the case(due to some previous design choices),
222 // so we can handle these cases using this flag
225
227 std::span<cycle_group> base_points,
228 std::span<AffineElement const> offset_generators,
229 bool unconditional_add);
230
232 std::span<AffineElement> base_points,
233 std::span<AffineElement const> offset_generators)
235};
236
237template <typename Builder> inline std::ostream& operator<<(std::ostream& os, cycle_group<Builder> const& v)
238{
239 return os << "{ " << v.x << ", " << v.y << " }";
240}
241} // namespace bb::stdlib
typename Group::element Element
Definition grumpkin.hpp:55
typename grumpkin::g1 Group
Definition grumpkin.hpp:54
typename Group::affine_element AffineElement
Definition grumpkin.hpp:56
Implements boolean logic in-circuit.
Definition bool.hpp:59
void fix_witness()
Definition bool.hpp:123
void set_origin_tag(const OriginTag &new_tag) const
Definition bool.hpp:119
void set_free_witness_tag()
Definition bool.hpp:121
void unset_free_witness_tag()
Definition bool.hpp:122
OriginTag get_origin_tag() const
Definition bool.hpp:120
cycle_group represents a group Element of the proving system's embedded curve i.e....
typename Curve::Group Group
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()
Get the point to the standard form. If the point is a point at infinity, ensure the coordinates are (...
static batch_mul_internal_output _fixed_base_batch_mul_internal(std::span< cycle_scalar > scalars, std::span< AffineElement > base_points, std::span< AffineElement const > offset_generators)
Internal algorithm to perform a fixed-base batch mul for ULTRA Builder.
cycle_group get_standard_form()
Get point in standard form. If the point is a point at infinity, ensure the coordinates are (0,...
typename Curve::Element Element
bool_t operator==(cycle_group &other)
static constexpr size_t NUM_ROUNDS
typename Builder::EmbeddedCurve Curve
cycle_group & operator-=(const cycle_group &other)
static cycle_group conditional_assign(const bool_t &predicate, const cycle_group &lhs, const cycle_group &rhs)
static constexpr size_t NUM_BITS
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. Uses incomplete addition formula If incompl...
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 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
static constexpr size_t TABLE_BITS
cycle_group operator/(const cycle_group &other) const
cycle_group & operator+=(const cycle_group &other)
void validate_is_on_curve() const
On-curve check.
bool_t is_point_at_infinity() const
static constexpr size_t STANDARD_NUM_TABLE_BITS
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 algorithm to perform a variable-base batch mul.
typename Builder::FF FF
cycle_group(Builder *_context=nullptr)
Construct a new cycle group<Builder>::cycle group object defaults to a constant point at infinity.
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
stdlib::bigfield< Builder, typename ScalarField::Params > BigScalarField
void assert_equal(cycle_group &other, std::string const &msg="cycle_group::assert_equal")
typename Curve::ScalarField ScalarField
void set_point_at_infinity(const bool_t &is_infinity)
Set the point to the point at infinity. Depending on constant'ness of the predicate put the coordinat...
cycle_group dbl(const std::optional< AffineElement > hint=std::nullopt) const
Evaluates a doubling. Uses Ultra double gate.
cycle_group operator+(const cycle_group &other) const
Will evaluate ECC point addition over *this and other. This method uses complete addition i....
static constexpr bool IS_ULTRA
static constexpr size_t ULTRA_NUM_TABLE_BITS
typename Curve::AffineElement AffineElement
::bb::stdlib::cycle_scalar< Builder > cycle_scalar
cycle_group unconditional_subtract(const cycle_group &other, const std::optional< AffineElement > hint=std::nullopt) const
will evaluate ECC point subtraction over *this and other. Incomplete addition formula edge cases are ...
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. Uses incomplete addition formula If incomplete...
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={})
cycle_group unconditional_add(const cycle_group &other, const std::optional< AffineElement > hint=std::nullopt) const
Will evaluate ECC point addition over *this and other. Incomplete addition formula edge cases are NOT...
cycle_scalar represents a member of the cycle curve SCALAR FIELD. This is NOT the native circuit fiel...
uint32_t set_public() const
Definition field.hpp:404
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
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_slice decomposes an input scalar into table_bits bit-slices. Used in batch_mul,...
std::ostream & operator<<(std::ostream &os, uint256_t const &a)
Definition uint256.hpp:246
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...
Stores temporary variables produced by internal multiplication algorithms.