9#include "../bigfield/goblin_field.hpp"
10#include "../circuit_builders/circuit_builders_fwd.hpp"
31template <
class Builder_,
class Fq,
class Fr,
class NativeGroup>
class goblin_element {
47 if (input.is_point_at_infinity()) {
77 const std::string msg =
"goblin_element::incomplete_assert_equal")
const
79 _x.assert_equal(other.
_x, msg +
" (x coordinate)");
80 _y.assert_equal(other.
_y, msg +
" (y coordinate)");
88 if (input.is_point_at_infinity()) {
89 out.
_x = Fq::from_witness(ctx,
bb::fq(0));
90 out.
_y = Fq::from_witness(ctx,
bb::fq(0));
92 out.
_x = Fq::from_witness(ctx, input.x);
93 out.
_y = Fq::from_witness(ctx, input.y);
104 this->
_x.convert_constant_to_fixed_witness(builder);
105 this->
_y.convert_constant_to_fixed_witness(builder);
115 this->
_x.fix_witness();
116 this->
_y.fix_witness();
163 typename NativeGroup::affine_element result_value =
typename NativeGroup::affine_element(
164 typename NativeGroup::element(
get_value()) -
typename NativeGroup::element(other.
get_value()));
169 auto x_lo = Fr::from_witness_index(
builder, op_tuple.
x_lo);
170 auto x_hi = Fr::from_witness_index(
builder, op_tuple.
x_hi);
171 auto y_lo = Fr::from_witness_index(
builder, op_tuple.
y_lo);
172 auto y_hi = Fr::from_witness_index(
builder, op_tuple.
y_hi);
173 x_lo.assert_equal(other.
_x.limbs[0]);
174 x_hi.assert_equal(other.
_x.limbs[1]);
175 y_lo.assert_equal(other.
_y.limbs[0]);
176 y_hi.assert_equal(other.
_y.limbs[1]);
181 builder->update_used_witnesses({ op_tuple.
z_1, op_tuple.
z_2 });
184 auto x_lo = Fr::from_witness_index(
builder, op_tuple2.
x_lo);
185 auto x_hi = Fr::from_witness_index(
builder, op_tuple2.
x_hi);
186 auto y_lo = Fr::from_witness_index(
builder, op_tuple2.
y_lo);
187 auto y_hi = Fr::from_witness_index(
builder, op_tuple2.
y_hi);
192 builder->update_used_witnesses({ op_tuple2.
z_1, op_tuple2.
z_2 });
194 Fq result_x(x_lo, x_hi);
195 Fq result_y(y_lo, y_hi);
200 auto x_lo = Fr::from_witness_index(
builder, op_tuple3.
x_lo);
201 auto x_hi = Fr::from_witness_index(
builder, op_tuple3.
x_hi);
202 auto y_lo = Fr::from_witness_index(
builder, op_tuple3.
y_lo);
203 auto y_hi = Fr::from_witness_index(
builder, op_tuple3.
y_hi);
205 x_lo.assert_equal(
_x.limbs[0]);
206 x_hi.assert_equal(
_x.limbs[1]);
207 y_lo.assert_equal(
_y.limbs[0]);
208 y_hi.assert_equal(
_y.limbs[1]);
224 *
this = *
this + other;
229 *
this = *
this - other;
243 result.
_y = Fq::conditional_assign(predicate, negated.
_y, result.
_y);
257 result.
_x = Fq::conditional_assign(predicate, other.
_x, result.
_x);
258 result.
_y = Fq::conditional_assign(predicate, other.
_y, result.
_y);
282 const std::vector<Fr>& scalars,
283 const size_t max_num_bits = 0,
284 const bool handle_edge_cases =
false);
293 auto result =
typename NativeGroup::affine_element(x_val, y_val);
295 if (x_val == 0 && y_val == 0) {
296 result.self_set_infinity();
303 if (
_x.get_context() !=
nullptr) {
304 return _x.get_context();
306 if (
_y.get_context() !=
nullptr) {
307 return _y.get_context();
314 if (
_x.get_context() !=
nullptr) {
315 return _x.get_context();
317 if (
_y.get_context() !=
nullptr) {
318 return _y.get_context();
320 if (other.
_x.get_context() !=
nullptr) {
321 return other.
_x.get_context();
323 if (other.
_y.get_context() !=
nullptr) {
324 return other.
_y.get_context();
336 _x.set_origin_tag(
tag);
337 _y.set_origin_tag(
tag);
345 _x.set_free_witness_tag();
346 _y.set_free_witness_tag();
354 _x.unset_free_witness_tag();
355 _y.unset_free_witness_tag();
366 const uint32_t start_idx =
_x.set_public();
389template <
typename C,
typename Fq,
typename Fr,
typename G>
392 return os <<
"{ " << v.
x() <<
" , " << v.
y() <<
" }";
#define BB_ASSERT(expression,...)
group class. Represents an elliptic curve group element. Group is parametrised by Fq and Fr
Implements boolean logic in-circuit.
Custom element class for when using goblin.
goblin_element operator+(const goblin_element &other) const
goblin_element normalize() const
void set_origin_tag(const OriginTag &tag) const
goblin_element operator-(const goblin_element &other) const
goblin_element conditional_negate(const bool_ct &predicate) const
OriginTag get_origin_tag() const
goblin_element checked_unconditional_add(const goblin_element &other) const
static goblin_element batch_mul(const std::vector< goblin_element > &points, const std::vector< Fr > &scalars, const size_t max_num_bits=0, const bool handle_edge_cases=false)
Goblin style batch multiplication.
goblin_element conditional_select(const goblin_element &other, const bool_ct &predicate) const
Selects this if predicate is false, other if predicate is true.
Builder * get_context() const
static goblin_element one(Builder *ctx)
goblin_element operator*(const Fr &scalar) const
uint32_t set_public() const
Set the witness indices representing the goblin element to public.
goblin_element(goblin_element &&other) noexcept=default
std::array< goblin_element, 2 > checked_unconditional_add_sub(const goblin_element &other) const
goblin_element checked_unconditional_subtract(const goblin_element &other) const
goblin_element operator+=(const goblin_element &other)
void incomplete_assert_equal(const goblin_element &other, const std::string msg="goblin_element::incomplete_assert_equal") const
Asserts that two goblin elements are equal (i.e., x, y coordinates are equal).
NativeGroup::affine_element get_value() const
void set_free_witness_tag()
Set the free witness flag for the goblin element's tags.
void convert_constant_to_fixed_witness(Builder *builder)
Creates fixed witnesses from a constant element.
goblin_element reduce() const
goblin_element get_standard_form() const
goblin_element dbl() const
goblin_element operator-() const
goblin_element(const Fq &x, const Fq &y, bool assert_on_curve=true)
~goblin_element()=default
Builder * get_context(const goblin_element &other) const
goblin_element(const typename NativeGroup::affine_element &input)
goblin_element operator-=(const goblin_element &other)
void validate_on_curve() const
goblin_element & operator=(const goblin_element &other)=default
static goblin_element from_witness(Builder *ctx, const typename NativeGroup::affine_element &input)
void unset_free_witness_tag()
Unset the free witness flag for the goblin element's tags.
static constexpr size_t PUBLIC_INPUTS_SIZE
goblin_element & operator=(goblin_element &&other) noexcept=default
static goblin_element constant_infinity(Builder *ctx)
Creates a constant point at infinity with canonical (0, 0) coordinates.
goblin_element(const goblin_element &other)=default
goblin_field wraps x/y coordinates of bn254 group elements when using goblin
std::ostream & operator<<(std::ostream &os, goblin_element< C, Fq, Fr, G > const &v)
MegaCircuitBuilder_< field< Bn254FrParams > > MegaCircuitBuilder
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
This file contains part of the logic for the Origin Tag mechanism that tracks the use of in-circuit p...
curve::BN254::BaseField Fq