Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
goblin_field.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#include "../bigfield/bigfield.hpp"
9#include "../circuit_builders/circuit_builders_fwd.hpp"
10#include "../field/field.hpp"
12
13namespace bb::stdlib {
14
28template <class Builder> class goblin_field {
29 public:
34 static constexpr size_t NUM_LAST_LIMB_BITS =
36
40
41 // Number of bb::fr elements used to represent a goblin field in the public inputs. (Note: 4 instead of 2 for
42 // consistency with bigfield).
43 static constexpr size_t PUBLIC_INPUTS_SIZE = 4;
44
45 // constructors mirror bigfield constructors
47 : limbs{ 0, 0 }
48 {}
49 goblin_field(Builder* parent_context, const uint256_t& value)
50 {
51 (*this) = goblin_field(bb::fq(value));
52 limbs[0].context = parent_context;
53 limbs[1].context = parent_context;
54 }
56 {
57 uint256_t converted(input);
58 uint256_t lo_v = converted.slice(0, NUM_LIMB_BITS * 2);
60 limbs = { bb::fr(lo_v), bb::fr(hi_v) };
61 }
63 : limbs{ lo, hi }
64 {}
65
66 // N.B. this method is because AggregationState expects group element coordinates to be split into 4 slices
67 // (we could update to only use 2 for Mega but that feels complex)
69 field_ct lolo, field_ct lohi, field_ct hilo, field_ct hihi, [[maybe_unused]] bool can_overflow = false)
70 {
71 goblin_field result;
72 result.limbs = { lolo + lohi * (uint256_t(1) << NUM_LIMB_BITS), hilo + hihi * (uint256_t(1) << NUM_LIMB_BITS) };
73 return result;
74 }
75
76 void assert_equal(const goblin_field& other) const
77 {
78 limbs[0].assert_equal(other.limbs[0]);
79 limbs[1].assert_equal(other.limbs[1]);
80 }
81 static goblin_field zero() { return goblin_field{ 0, 0 }; }
82
84 {
85 uint256_t converted(input);
86 uint256_t lo_v = converted.slice(0, NUM_LIMB_BITS * 2);
88 field_ct lo = field_ct::from_witness(ctx, lo_v);
89 field_ct hi = field_ct::from_witness(ctx, hi_v);
90 auto result = goblin_field(lo, hi);
91 result.set_free_witness_tag();
92 return result;
93 }
94
99 {
100 for (auto& limb : limbs) {
101 limb.convert_constant_to_fixed_witness(builder);
102 }
104 }
105
110 {
111 for (auto& limb : limbs) {
112 limb.fix_witness();
113 }
114 // This is now effectively a constant
116 }
117
118 static goblin_field conditional_assign(const bool_ct& predicate, const goblin_field& lhs, goblin_field& rhs)
119 {
120 goblin_field result;
121 result.limbs = {
122 field_ct::conditional_assign(predicate, lhs.limbs[0], rhs.limbs[0]),
123 field_ct::conditional_assign(predicate, lhs.limbs[1], rhs.limbs[1]),
124 };
125 return result;
126 }
127
128 // matches the interface for bigfield
130 {
131 uint256_t lo = limbs[0].get_value();
132 uint256_t hi = limbs[1].get_value();
133 uint256_t result = lo + (hi << 136);
134 return result;
135 }
136
137 // matches the interface for bigfield
138 uint512_t get_maximum_value() const { return (*this).get_value(); }
139
141 {
142 if (limbs[0].get_context()) {
143 return limbs[0].get_context();
144 }
145 return limbs[1].get_context();
146 }
147
148 // done in the translator circuit
150
152
153 void set_origin_tag(const OriginTag& tag) const
154 {
155 limbs[0].set_origin_tag(tag);
156 limbs[1].set_origin_tag(tag);
157 }
158
163 {
164 for (auto& limb : limbs) {
165 limb.set_free_witness_tag();
166 }
167 }
168
173 {
174 for (auto& limb : limbs) {
175 limb.unset_free_witness_tag();
176 }
177 }
184 uint32_t set_public() const
185 {
187
188 BigFq bigfield_equivalent(limbs[0], limbs[1]);
189 const uint32_t start_idx = bigfield_equivalent.set_public();
190
191 return start_idx;
192 }
193
200 {
201 return construct_from_limbs(limbs[0], limbs[1], limbs[2], limbs[3], /*can_overflow=*/false);
202 }
203};
204template <typename C> inline std::ostream& operator<<(std::ostream& os, goblin_field<C> const& v)
205{
206 return os << "{ " << v.limbs[0] << " , " << v.limbs[1] << " }";
207}
208} // namespace bb::stdlib
constexpr uint256_t slice(uint64_t start, uint64_t end) const
Implements boolean logic in-circuit.
Definition bool.hpp:59
static field_t conditional_assign(const bool_t< Builder > &predicate, const field_t &lhs, const field_t &rhs)
If predicate == true then return lhs, else return rhs.
Definition field.cpp:884
static field_t from_witness(Builder *ctx, const bb::fr &input)
Definition field.hpp:424
goblin_field wraps x/y coordinates of bn254 group elements when using goblin
void set_free_witness_tag()
Set the free witness flag for the goblin field's tags.
static goblin_field reconstruct_from_public(const std::span< const field_ct, PUBLIC_INPUTS_SIZE > &limbs)
Reconstruct goblin field from its representation as limbs as stored in the public inputs.
OriginTag get_origin_tag() const
static constexpr size_t PUBLIC_INPUTS_SIZE
Builder * get_context() const
std::array< field_ct, 2 > limbs
static goblin_field from_witness(Builder *ctx, bb::fq input)
static goblin_field construct_from_limbs(field_ct lolo, field_ct lohi, field_ct hilo, field_ct hihi, bool can_overflow=false)
static goblin_field zero()
static constexpr size_t NUM_LAST_LIMB_BITS
goblin_field(field_ct lo, field_ct hi)
static constexpr size_t NUM_LIMBS
void convert_constant_to_fixed_witness(Builder *builder)
goblin_field(Builder *parent_context, const uint256_t &value)
uint32_t set_public() const
Set the witness indices for the limbs of the goblin field to public.
void assert_equal(const goblin_field &other) const
void unset_free_witness_tag()
Unset the free witness flag for the goblin field's tags.
uint512_t get_value() const
static constexpr size_t NUM_LIMB_BITS
static const uint1024_t DEFAULT_MAXIMUM_REMAINDER
void set_origin_tag(const OriginTag &tag) const
static goblin_field conditional_assign(const bool_ct &predicate, const goblin_field &lhs, goblin_field &rhs)
uint512_t get_maximum_value() const
AluTraceBuilder builder
Definition alu.test.cpp:123
std::ostream & operator<<(std::ostream &os, uint256_t const &a)
Definition uint256.hpp:246
field< Bn254FrParams > fr
Definition fr.hpp:174
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...