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: Complete, auditors: [Luke], commit: a48c205d6dcd4338f5b83b4fda18bff6015be07b}
3// external_1: { status: Complete, auditors: [Sherlock], commit: 13c1b927c6c }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
8
21#include <optional>
22
23namespace bb::stdlib {
24
40template <typename Builder> class cycle_group {
41 public:
46
52
58
59 // Bit-size for scalars represented in the ROM lookup tables used in the variable-base MSM algorithm
60 static constexpr size_t ROM_TABLE_BITS = 4;
61 static constexpr size_t NUM_BITS_FULL_FIELD_SIZE = bb::fq::modulus.get_msb() + 1;
62 // Domain separator for generating offset generator points in the variable-base MSM algorithm
63 static constexpr std::string_view OFFSET_GENERATOR_DOMAIN_SEPARATOR = "cycle_group_offset_generator";
64
65 // Since the cycle_group base field is the circuit's native field, it can be stored using two public inputs.
66 static constexpr size_t PUBLIC_INPUTS_SIZE = 2;
67
68 private:
77
78 public:
79 cycle_group(Builder* _context = nullptr);
80 // Construct from coordinates. Infinity is auto-detected from (x == 0 && y == 0).
81 explicit cycle_group(const field_t& x, const field_t& y, bool assert_on_curve = true);
82 cycle_group(const AffineElement& _in);
83 static cycle_group one(Builder* _context);
84 static cycle_group constant_infinity(Builder* _context = nullptr);
85 static cycle_group from_witness(Builder* _context, const AffineElement& _in);
86 static cycle_group from_constant_witness(Builder* _context, const AffineElement& _in);
87 Builder* get_context(const cycle_group& other) const;
88 Builder* get_context() const { return context; }
90
91 // Coordinate accessors (non-owning, const reference)
92 const field_t& x() const { return _x; }
93 const field_t& y() const { return _y; }
94 [[nodiscard]] bool is_constant() const
95 {
97 }
99 [[nodiscard]] bool is_constant_point_at_infinity() const
100 {
102 }
103 void standardize();
104 void validate_on_curve() const;
105
108 const std::optional<AffineElement> hint = std::nullopt) const;
110 const std::optional<AffineElement> hint = std::nullopt) const;
112 const std::optional<AffineElement> hint = std::nullopt) const;
114 const std::optional<AffineElement> hint = std::nullopt) const;
115 cycle_group operator+(const cycle_group& other) const;
116 cycle_group operator-(const cycle_group& other) const;
117 cycle_group operator-() const;
118 cycle_group& operator+=(const cycle_group& other);
119 cycle_group& operator-=(const cycle_group& other);
121 const std::vector<BigScalarField>& scalars,
123 {
124 std::vector<cycle_scalar> cycle_scalars;
125 for (auto scalar : scalars) {
126 cycle_scalars.emplace_back(scalar);
127 }
128 return batch_mul(base_points, cycle_scalars, context);
129 }
130 static cycle_group batch_mul(const std::vector<cycle_group>& base_points,
131 const std::vector<cycle_scalar>& scalars,
132 const GeneratorContext& context = {});
133
135 const std::vector<BigScalarField>& scalars,
137 size_t table_bits = ROM_TABLE_BITS)
138 {
139 std::vector<cycle_scalar> cycle_scalars;
140 for (auto scalar : scalars) {
141 cycle_scalars.emplace_back(scalar);
142 }
143 return fixed_batch_mul(constant_points, cycle_scalars, context, table_bits);
144 }
145 static cycle_group fixed_batch_mul(const std::vector<cycle_group>& constant_points,
146 const std::vector<cycle_scalar>& scalars,
147 const GeneratorContext& context = {},
148 size_t table_bits = ROM_TABLE_BITS);
149 cycle_group operator*(const cycle_scalar& scalar) const;
150 cycle_group& operator*=(const cycle_scalar& scalar);
151 cycle_group operator*(const BigScalarField& scalar) const;
152 cycle_group& operator*=(const BigScalarField& scalar);
153 bool_t operator==(cycle_group& other);
154 void assert_equal(cycle_group& other, std::string const& msg = "cycle_group::assert_equal");
155 static cycle_group conditional_assign(const bool_t& predicate, const cycle_group& lhs, const cycle_group& rhs);
156
168
178
188
198
203 {
204 // Origin tags should be updated within
205 _x.fix_witness();
206 _y.fix_witness();
208
209 // This is now effectively a constant
211 }
217 uint32_t set_public()
218 {
219 standardize(); // if point is at infinity, ensure coordinates are (0,0).
220 uint32_t start_idx = _x.set_public();
221 _y.set_public();
222 return start_idx;
223 }
224
225 private:
226 // Allow straus_lookup_table and straus_plookup_table to access the private constructor for efficiency
227 friend class ::bb::stdlib::straus_lookup_table<Builder>;
228 friend class ::bb::stdlib::straus_plookup_table<Builder>;
229
230 // Private constructor that allows explicit control over infinity flag.
231 // Use public constructors or factory methods instead - they auto-detect infinity from coordinates.
232 cycle_group(const field_t& x, const field_t& y, bool_t is_infinity, bool assert_on_curve);
233
238
240 std::span<cycle_group> base_points,
241 std::span<AffineElement const> offset_generators,
242 bool unconditional_add);
243
245 std::span<AffineElement> base_points);
246
250 std::span<AffineElement const> offset_generators,
251 size_t table_bits = ROM_TABLE_BITS);
252
253 // Internal implementation for unconditional_add and unconditional_subtract
255 bool is_addition,
256 const std::optional<AffineElement> hint) const;
257};
258
259template <typename Builder> inline std::ostream& operator<<(std::ostream& os, cycle_group<Builder> const& v)
260{
261 return os << "{ " << v.x() << ", " << v.y() << " }";
262}
263} // namespace bb::stdlib
element class. Implements ecc group arithmetic using Jacobian coordinates See https://hyperelliptic....
Definition element.hpp:35
group class. Represents an elliptic curve group element. Group is parametrised by Fq and Fr
Definition group.hpp:38
group_elements::affine_element< Fq, Fr, Params > affine_element
Definition group.hpp:44
group_elements::element< Fq, Fr, Params > element
Definition group.hpp:43
constexpr uint64_t get_msb() const
Implements boolean logic in-circuit.
Definition bool.hpp:60
bool get_value() const
Definition bool.hpp:125
void fix_witness()
Definition bool.hpp:159
bool is_constant() const
Definition bool.hpp:127
void set_origin_tag(const OriginTag &new_tag) const
Definition bool.hpp:154
void set_free_witness_tag()
Definition bool.hpp:156
void unset_free_witness_tag()
Definition bool.hpp:157
OriginTag get_origin_tag() const
Definition bool.hpp:155
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)
const field_t & x() const
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
Evaluate incomplete ECC point addition over *this and other.
void validate_on_curve() const
On-curve check.
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.
cycle_group checked_unconditional_subtract(const cycle_group &other, const std::optional< AffineElement > hint=std::nullopt) const
Evaluate incomplete ECC point subtraction over *this and other, with x-coordinate collision checks.
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)
static batch_mul_internal_output _fixed_base_plookup_batch_mul_internal(std::span< cycle_scalar > scalars, std::span< AffineElement const > base_points, std::span< AffineElement const > offset_generators, size_t table_bits=ROM_TABLE_BITS)
Internal algorithm to perform a fixed-base batch mul using plookup tables.
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
static cycle_group fixed_batch_mul(const std::vector< cycle_group > &constant_points, const std::vector< BigScalarField > &scalars, GeneratorContext context={}, size_t table_bits=ROM_TABLE_BITS)
bool_t is_point_at_infinity() const
const field_t & y() 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
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
Evaluate incomplete ECC point subtraction over *this and other.
Builder * get_context() const
cycle_group checked_unconditional_add(const cycle_group &other, const std::optional< AffineElement > hint=std::nullopt) const
Evaluate incomplete ECC point addition over *this and other, with x-coordinate collision checks.
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:447
void unset_free_witness_tag() const
Unset the free witness flag for the field element's tag.
Definition field.hpp:369
OriginTag get_origin_tag() const
Definition field.hpp:359
bool is_constant() const
Definition field.hpp:442
void set_free_witness_tag()
Set the free witness flag for the field element's tag.
Definition field.hpp:364
void set_origin_tag(const OriginTag &new_tag) const
Definition field.hpp:358
straus_lookup_table computes a lookup table of size 1 << table_bits
straus_plookup_table computes a plookup-based 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:46
std::ostream & operator<<(std::ostream &os, uint256_t const &a)
Definition uint256.hpp:258
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.