barretenberg
Loading...
Searching...
No Matches
cycle_group.hpp
1#pragma once
2
3#include "barretenberg/crypto/pedersen_commitment/pedersen.hpp"
4#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp"
5#include "barretenberg/proof_system/plookup_tables/fixed_base/fixed_base_params.hpp"
6#include "barretenberg/stdlib/primitives/bool/bool.hpp"
7#include "barretenberg/stdlib/primitives/circuit_builders/circuit_builders.hpp"
8#include "barretenberg/stdlib/primitives/field/field.hpp"
9#include <optional>
10
11namespace proof_system::plonk::stdlib {
12
13template <typename Composer>
14concept IsUltraArithmetic = (Composer::CIRCUIT_TYPE == CircuitType::ULTRA);
15template <typename Composer>
16concept IsNotUltraArithmetic = (Composer::CIRCUIT_TYPE != CircuitType::ULTRA);
17
27template <typename Composer> class cycle_group {
28 public:
32 using FF = typename Composer::FF;
33 using Curve = typename Composer::EmbeddedCurve;
34 using Group = typename Curve::Group;
35 using Element = typename Curve::Element;
36 using AffineElement = typename Curve::AffineElement;
38 using ScalarField = typename Curve::ScalarField;
39
40 static constexpr size_t STANDARD_NUM_TABLE_BITS = 1;
41 static constexpr size_t ULTRA_NUM_TABLE_BITS = 4;
42 static constexpr bool IS_ULTRA = Composer::CIRCUIT_TYPE == CircuitType::ULTRA;
43 static constexpr size_t TABLE_BITS = IS_ULTRA ? ULTRA_NUM_TABLE_BITS : STANDARD_NUM_TABLE_BITS;
44 static constexpr size_t NUM_BITS = ScalarField::modulus.get_msb() + 1;
45 static constexpr size_t NUM_ROUNDS = (NUM_BITS + TABLE_BITS - 1) / TABLE_BITS;
46 inline static constexpr std::string_view OFFSET_GENERATOR_DOMAIN_SEPARATOR = "cycle_group_offset_generator";
47
48 private:
49 public:
66 struct cycle_scalar {
67 static constexpr size_t LO_BITS = plookup::FixedBaseParams::BITS_PER_LO_SCALAR;
68 static constexpr size_t HI_BITS = NUM_BITS - LO_BITS;
69 field_t lo;
70 field_t hi;
71
72 private:
73 size_t _num_bits = NUM_BITS;
74 bool _skip_primality_test = false;
75 // if our scalar multiplier is a bn254 FF scalar (e.g. pedersen hash),
76 // we want to validate the cycle_scalar < bn254::fr::modulus *not* grumpkin::fr::modulus
77 bool _use_bn254_scalar_field_for_primality_test = false;
78
79 public:
80 cycle_scalar(const field_t& _lo,
81 const field_t& _hi,
82 const size_t bits,
83 const bool skip_primality_test,
84 const bool use_bn254_scalar_field_for_primality_test)
85 : lo(_lo)
86 , hi(_hi)
87 , _num_bits(bits)
88 , _skip_primality_test(skip_primality_test)
89 , _use_bn254_scalar_field_for_primality_test(use_bn254_scalar_field_for_primality_test){};
90 cycle_scalar(const ScalarField& _in = 0);
91 cycle_scalar(const field_t& _lo, const field_t& _hi);
92 cycle_scalar(const field_t& _in);
93 static cycle_scalar from_witness(Composer* context, const ScalarField& value);
94 static cycle_scalar from_witness_bitstring(Composer* context, const uint256_t& bitstring, size_t num_bits);
95 static cycle_scalar create_from_bn254_scalar(const field_t& _in, bool skip_primality_test = false);
96 [[nodiscard]] bool is_constant() const;
97 ScalarField get_value() const;
98 Composer* get_context() const { return lo.get_context() != nullptr ? lo.get_context() : hi.get_context(); }
99 [[nodiscard]] size_t num_bits() const { return _num_bits; }
100 [[nodiscard]] bool skip_primality_test() const { return _skip_primality_test; }
101 [[nodiscard]] bool use_bn254_scalar_field_for_primality_test() const
102 {
103 return _use_bn254_scalar_field_for_primality_test;
104 }
105 void validate_scalar_is_in_field() const;
106 };
107
114 straus_scalar_slice(Composer* context, const cycle_scalar& scalars, size_t table_bits);
115 std::optional<field_t> read(size_t index);
116 size_t _table_bits;
117 std::vector<field_t> slices;
118 };
119
146 public:
147 straus_lookup_table() = default;
149 const cycle_group& base_point,
150 const cycle_group& offset_generator,
151 size_t table_bits);
152 cycle_group read(const field_t& index);
153 size_t _table_bits;
154 Composer* _context;
155 std::vector<cycle_group> point_table;
156 size_t rom_id = 0;
157 };
158
159 private:
164 struct batch_mul_internal_output {
165 cycle_group accumulator;
166 AffineElement offset_generator_delta;
167 };
168
169 public:
170 cycle_group(Composer* _context = nullptr);
171 cycle_group(field_t _x, field_t _y, bool_t _is_infinity);
172 cycle_group(const FF& _x, const FF& _y, bool _is_infinity);
173 cycle_group(const AffineElement& _in);
174 static cycle_group from_witness(Composer* _context, const AffineElement& _in);
175 static cycle_group from_constant_witness(Composer* _context, const AffineElement& _in);
176 Composer* get_context(const cycle_group& other) const;
177 Composer* get_context() const { return context; }
178 AffineElement get_value() const;
179 [[nodiscard]] bool is_constant() const { return _is_constant; }
180 bool_t is_point_at_infinity() const { return _is_infinity; }
181 void set_point_at_infinity(const bool_t& is_infinity) { _is_infinity = is_infinity; }
182 void validate_is_on_curve() const;
183 cycle_group dbl() const
184 requires IsUltraArithmetic<Composer>;
185 cycle_group dbl() const
186 requires IsNotUltraArithmetic<Composer>;
187 cycle_group unconditional_add(const cycle_group& other) const
188 requires IsUltraArithmetic<Composer>;
189 cycle_group unconditional_add(const cycle_group& other) const
190 requires IsNotUltraArithmetic<Composer>;
191 cycle_group unconditional_subtract(const cycle_group& other) const;
192 cycle_group checked_unconditional_add(const cycle_group& other) const;
193 cycle_group checked_unconditional_subtract(const cycle_group& other) const;
194 cycle_group operator+(const cycle_group& other) const;
195 cycle_group operator-(const cycle_group& other) const;
196 cycle_group operator-() const;
197 cycle_group& operator+=(const cycle_group& other);
198 cycle_group& operator-=(const cycle_group& other);
199 static cycle_group batch_mul(const std::vector<cycle_scalar>& scalars,
200 const std::vector<cycle_group>& base_points,
201 GeneratorContext context = {});
202 cycle_group operator*(const cycle_scalar& scalar) const;
203 cycle_group& operator*=(const cycle_scalar& scalar);
204 bool_t operator==(const cycle_group& other) const;
205 void assert_equal(const cycle_group& other, std::string const& msg = "cycle_group::assert_equal") const;
206 static cycle_group conditional_assign(const bool_t& predicate, const cycle_group& lhs, const cycle_group& rhs);
207 cycle_group operator/(const cycle_group& other) const;
208 field_t x;
209 field_t y;
210
211 private:
212 bool_t _is_infinity;
213 bool _is_constant;
214 Composer* context;
215
216 static batch_mul_internal_output _variable_base_batch_mul_internal(std::span<cycle_scalar> scalars,
217 std::span<cycle_group> base_points,
218 std::span<AffineElement const> offset_generators,
219 bool unconditional_add);
220
221 static batch_mul_internal_output _fixed_base_batch_mul_internal(std::span<cycle_scalar> scalars,
222 std::span<AffineElement> base_points,
223 std::span<AffineElement const> offset_generators)
224 requires IsUltraArithmetic<Composer>;
225 static batch_mul_internal_output _fixed_base_batch_mul_internal(std::span<cycle_scalar> scalars,
226 std::span<AffineElement> base_points,
227 std::span<AffineElement const> offset_generators)
228 requires IsNotUltraArithmetic<Composer>;
229};
230
231template <typename ComposerContext>
232inline std::ostream& operator<<(std::ostream& os, cycle_group<ComposerContext> const& v)
233{
234 return os << v.get_value();
235}
236
237EXTERN_STDLIB_TYPE(cycle_group);
238
239} // namespace proof_system::plonk::stdlib
Definition: uint256.hpp:25
Definition: standard_composer.hpp:14
cycle_group represents a group Element of the proving system's embedded curve i.e....
Definition: cycle_group.hpp:27
void validate_is_on_curve() const
On-curve check.
Definition: cycle_group.cpp:156
cycle_group checked_unconditional_add(const cycle_group &other) const
Will evaluate ECC point addition over *this and other. Uses incomplete addition formula If incomplete...
Definition: cycle_group.cpp:365
cycle_group dbl() const
Evaluates a doubling. Does not use Ultra double gate.
Definition: cycle_group.cpp:174
static cycle_group batch_mul(const std::vector< cycle_scalar > &scalars, const std::vector< cycle_group > &base_points, GeneratorContext context={})
Multiscalar multiplication algorithm.
Definition: cycle_group.cpp:1185
cycle_group unconditional_add(const cycle_group &other) const
Will evaluate ECC point addition over *this and other. Incomplete addition formula edge cases are NOT...
Definition: cycle_group.cpp:234
static cycle_group from_witness(Composer *_context, const AffineElement &_in)
Converts an AffineElement into a circuit witness.
Definition: cycle_group.cpp:97
static cycle_group from_constant_witness(Composer *_context, const AffineElement &_in)
Converts a native AffineElement into a witness, but constrains the witness values to be known constan...
Definition: cycle_group.cpp:121
cycle_group checked_unconditional_subtract(const cycle_group &other) const
Will evaluate ECC point subtraction over *this and other. Uses incomplete addition formula If incompl...
Definition: cycle_group.cpp:385
cycle_group unconditional_subtract(const cycle_group &other) const
will evaluate ECC point subtraction over *this and other. Incomplete addition formula edge cases are ...
Definition: cycle_group.cpp:309
Definition: witness.hpp:10
Definition: generator_data.hpp:133
cycle_scalar represents a member of the cycle curve SCALAR FIELD. This is NOT the native circuit fiel...
Definition: cycle_group.hpp:66
void validate_scalar_is_in_field() const
Checks that a cycle_scalar value is smaller than a prime field modulus when evaluated over the INTEGE...
Definition: cycle_group.cpp:645
static cycle_scalar create_from_bn254_scalar(const field_t &_in, bool skip_primality_test=false)
Use when we want to multiply a group element by a string of bits of known size. N....
Definition: cycle_group.cpp:610
static cycle_scalar from_witness_bitstring(Composer *context, const uint256_t &bitstring, size_t num_bits)
Use when we want to multiply a group element by a string of bits of known size. N....
Definition: cycle_group.cpp:587
straus_lookup_table computes a lookup table of size 1 << table_bits
Definition: cycle_group.hpp:145
cycle_group read(const field_t &index)
Given an _index witness, return straus_lookup_table[index]
Definition: cycle_group.cpp:852
straus_scalar_slice decomposes an input scalar into table_bits bit-slices. Used in batch_mul,...
Definition: cycle_group.hpp:113
std::optional< field_t > read(size_t index)
Return a bit-slice associated with round index.
Definition: cycle_group.cpp:773