9#include "../bigfield/bigfield.hpp"
10#include "../bigfield/goblin_field.hpp"
11#include "../circuit_builders/circuit_builders_fwd.hpp"
12#include "../field/field.hpp"
13#include "../memory/rom_table.hpp"
14#include "../memory/twin_rom_table.hpp"
38template <
class Builder_,
class Fq,
class Fr,
class NativeGroup>
class goblin_element {
68 info(
"WARNING: goblin_element::assert_equal value check failed!");
70 x.assert_equal(other.
x);
71 y.assert_equal(other.
y);
78 if (input.is_point_at_infinity()) {
84 Fq x = Fq::from_witness(ctx, input.x);
85 Fq y = Fq::from_witness(ctx, input.y);
99 this->
x.convert_constant_to_fixed_witness(builder);
100 this->
y.convert_constant_to_fixed_witness(builder);
110 this->
x.fix_witness();
111 this->
y.fix_witness();
133 Fr zero = Fr::from_witness_index(ctx, ctx->zero_idx);
134 zero.unset_free_witness_tag();
160 ASSERT(
builder->op_queue->get_accumulator().is_point_at_infinity());
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]);
180 auto x_lo = Fr::from_witness_index(
builder, op_tuple2.
x_lo);
181 auto x_hi = Fr::from_witness_index(
builder, op_tuple2.
x_hi);
182 auto y_lo = Fr::from_witness_index(
builder, op_tuple2.
y_lo);
183 auto y_hi = Fr::from_witness_index(
builder, op_tuple2.
y_hi);
185 Fq result_x(x_lo, x_hi);
186 Fq result_y(y_lo, y_hi);
191 auto op2_is_infinity = (x_lo.add_two(x_hi, y_lo) + y_hi).is_zero();
195 auto x_lo = Fr::from_witness_index(
builder, op_tuple3.
x_lo);
196 auto x_hi = Fr::from_witness_index(
builder, op_tuple3.
x_hi);
197 auto y_lo = Fr::from_witness_index(
builder, op_tuple3.
y_lo);
198 auto y_hi = Fr::from_witness_index(
builder, op_tuple3.
y_hi);
200 x_lo.assert_equal(
x.limbs[0]);
201 x_hi.assert_equal(
x.limbs[1]);
202 y_lo.assert_equal(
y.limbs[0]);
203 y_hi.assert_equal(
y.limbs[1]);
215 *
this = *
this + other;
220 *
this = *
this - other;
234 result.
y = Fq::conditional_assign(predicate, negated.
y, result.
y);
256 const std::vector<Fr>& scalars,
257 const size_t max_num_bits = 0,
258 const bool handle_edge_cases =
false);
265 bb::fq x_val =
x.get_value().lo;
266 bb::fq y_val =
y.get_value().lo;
267 auto result =
typename NativeGroup::affine_element(x_val, y_val);
269 result.self_set_infinity();
276 if (
x.get_context() !=
nullptr) {
277 return x.get_context();
279 if (
y.get_context() !=
nullptr) {
280 return y.get_context();
287 if (
x.get_context() !=
nullptr) {
288 return x.get_context();
290 if (
y.get_context() !=
nullptr) {
291 return y.get_context();
293 if (other.
x.get_context() !=
nullptr) {
294 return other.
x.get_context();
296 if (other.
y.get_context() !=
nullptr) {
297 return other.
y.get_context();
316 result.
x = Fq::conditional_assign(is_infinity, zero, result.
x);
317 result.
y = Fq::conditional_assign(is_infinity, zero, result.
y);
328 x.set_origin_tag(tag);
329 y.set_origin_tag(tag);
338 x.set_free_witness_tag();
339 y.set_free_witness_tag();
348 x.unset_free_witness_tag();
349 y.unset_free_witness_tag();
363 const uint32_t start_idx =
x.set_public();
398template <
typename C,
typename Fq,
typename Fr,
typename G>
401 return os <<
"{ " << v.
x <<
" , " << v.
y <<
" }";
#define ASSERT(expression,...)
group class. Represents an elliptic curve group element. Group is parametrised by Fq and Fr
Implements boolean logic in-circuit.
void set_origin_tag(const OriginTag &new_tag) const
void set_free_witness_tag()
void unset_free_witness_tag()
OriginTag get_origin_tag() const
Custom element class for when using goblin.
bool_ct is_point_at_infinity() const
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.
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)
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
Enforce x and y coordinates of a point to be (0,0) in the case of point at infinity.
goblin_element dbl() const
goblin_element operator-() const
static goblin_element reconstruct_from_public(const std::span< const Fr, PUBLIC_INPUTS_SIZE > &limbs)
Reconstruct a goblin element from its representation as limbs stored in the public inputs.
~goblin_element()=default
Builder * get_context(const goblin_element &other) const
static goblin_element point_at_infinity(Builder *ctx)
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
void assert_equal(const goblin_element &other) const
goblin_element & operator=(goblin_element &&other) noexcept=default
goblin_element(const Fq &x, const Fq &y)
void set_point_at_infinity(const bool_ct &is_infinity)
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...
static constexpr size_t PUBLIC_INPUTS_SIZE
static field reconstruct_from_public(const std::span< const field< V >, PUBLIC_INPUTS_SIZE > &limbs)
static constexpr field zero()