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: Planned, auditors: [], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
8#include "../bigfield/bigfield.hpp"
9#include "../circuit_builders/circuit_builders_fwd.hpp"
10#include "../field/field.hpp"
13
14namespace bb::stdlib {
15
29template <class Builder> class goblin_field {
30 public:
35 static constexpr size_t NUM_LAST_LIMB_BITS =
37
41
42 // Number of bb::fr elements used to represent a goblin field in the public inputs
43 // This matches the Codec representation (2 limbs)
44 static constexpr size_t PUBLIC_INPUTS_SIZE = GOBLIN_FIELD_PUBLIC_INPUTS_SIZE;
45
46 // constructors mirror bigfield constructors
48 : limbs{ 0, 0 }
49 {}
50 goblin_field(Builder* parent_context, const uint256_t& value)
51 {
52 (*this) = goblin_field(bb::fq(value));
53 limbs[0].context = parent_context;
54 limbs[1].context = parent_context;
55 }
57 {
58 uint256_t converted(input);
59 uint256_t lo_v = converted.slice(0, NUM_LIMB_BITS * 2);
61 limbs = { bb::fr(lo_v), bb::fr(hi_v) };
62 }
64 : limbs{ lo, hi }
65 {}
66
67 // N.B. this method is because AggregationState expects group element coordinates to be split into 4 slices
68 // (we could update to only use 2 for Mega but that feels complex)
70 field_ct lolo, field_ct lohi, field_ct hilo, field_ct hihi, [[maybe_unused]] bool can_overflow = false)
71 {
72 goblin_field result;
73 result.limbs = { lolo + lohi * (uint256_t(1) << NUM_LIMB_BITS), hilo + hihi * (uint256_t(1) << NUM_LIMB_BITS) };
74 return result;
75 }
76
77 void assert_equal(const goblin_field& other, const std::string& msg = "goblin_field::assert_equal") const
78 {
79 limbs[0].assert_equal(other.limbs[0], msg);
80 limbs[1].assert_equal(other.limbs[1], msg);
81 }
82 static goblin_field zero() { return goblin_field{ 0, 0 }; }
83
85 {
86 uint256_t converted(input);
87 uint256_t lo_v = converted.slice(0, NUM_LIMB_BITS * 2);
89 field_ct lo = field_ct::from_witness(ctx, bb::fr(lo_v));
90 field_ct hi = field_ct::from_witness(ctx, bb::fr(hi_v));
91 auto result = goblin_field(lo, hi);
92 result.set_free_witness_tag();
93 return result;
94 }
95
100 {
101 for (auto& limb : limbs) {
102 limb.convert_constant_to_fixed_witness(builder);
103 }
105 }
106
111 {
112 for (auto& limb : limbs) {
113 limb.fix_witness();
114 }
115 // This is now effectively a constant
117 }
118
119 static goblin_field conditional_assign(const bool_ct& predicate, const goblin_field& lhs, goblin_field& rhs)
120 {
121 goblin_field result;
122 result.limbs = {
123 field_ct::conditional_assign(predicate, lhs.limbs[0], rhs.limbs[0]),
124 field_ct::conditional_assign(predicate, lhs.limbs[1], rhs.limbs[1]),
125 };
126 return result;
127 }
128
129 // matches the interface for bigfield
131 {
132 uint256_t lo = limbs[0].get_value();
133 uint256_t hi = limbs[1].get_value();
134 uint256_t result = lo + (hi << 136);
135 return result;
136 }
137
138 // matches the interface for bigfield
139 uint512_t get_maximum_value() const { return (*this).get_value(); }
140
142 {
143 if (limbs[0].get_context()) {
144 return limbs[0].get_context();
145 }
146 return limbs[1].get_context();
147 }
148
149 // done in the translator circuit
151
153
154 void set_origin_tag(const OriginTag& tag) const
155 {
156 limbs[0].set_origin_tag(tag);
157 limbs[1].set_origin_tag(tag);
158 }
159
164 {
165 for (auto& limb : limbs) {
166 limb.set_free_witness_tag();
167 }
168 }
169
174 {
175 for (auto& limb : limbs) {
176 limb.unset_free_witness_tag();
177 }
178 }
185 uint32_t set_public() const
186 {
187 uint32_t start_idx = limbs[0].set_public();
188 limbs[1].set_public();
189 return start_idx;
190 }
191};
192template <typename C> inline std::ostream& operator<<(std::ostream& os, goblin_field<C> const& v)
193{
194 return os << "{ " << v.limbs[0] << " , " << v.limbs[1] << " }";
195}
196} // namespace bb::stdlib
constexpr uint256_t slice(uint64_t start, uint64_t end) const
Implements boolean logic in-circuit.
Definition bool.hpp:60
static field_t from_witness(Builder *ctx, const bb::fr &input)
Definition field.hpp:466
static field_t conditional_assign(const bool_t< Builder > &predicate, const field_t &lhs, const field_t &rhs)
Definition field.hpp:384
goblin_field wraps x/y coordinates of bn254 group elements when using goblin
void assert_equal(const goblin_field &other, const std::string &msg="goblin_field::assert_equal") const
void set_free_witness_tag()
Set the free witness flag for the goblin field's tags.
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 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:124
std::ostream & operator<<(std::ostream &os, uint256_t const &a)
Definition uint256.hpp:258
field< Bn254FrParams > fr
Definition fr.hpp:155
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...