Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
special_public_inputs_test_serde.hpp
Go to the documentation of this file.
1#pragma once
2
9
11
18 public:
23
24 static constexpr size_t PUBLIC_INPUTS_SIZE = KERNEL_PUBLIC_INPUTS_SIZE;
25
31
39 static KernelIOSerde from_proof(const std::vector<NativeFF>& proof, size_t num_public_inputs)
40 {
41 KernelIOSerde result;
42 // KernelIO is at the end of public inputs, which are at the start of the proof
43 size_t idx = num_public_inputs - PUBLIC_INPUTS_SIZE;
44
45 // Each G1 point is 4 fr elements (2 limbs for x, 2 limbs for y) using 136-bit limb encoding
46 auto deserialize_point = [&]() {
48 NativeG1::PUBLIC_INPUTS_SIZE);
49 idx += NativeG1::PUBLIC_INPUTS_SIZE;
50 return FrCodec::deserialize_from_fields<NativeG1>(limbs);
51 };
52
53 result.pairing_inputs.P0() = deserialize_point();
54 result.pairing_inputs.P1() = deserialize_point();
55 result.kernel_return_data = deserialize_point();
56 result.app_return_data = deserialize_point();
57 result.ecc_op_hash = proof[idx++];
58 result.output_hn_accum_hash = proof[idx];
59
60 return result;
61 }
62
68 void to_proof(std::vector<NativeFF>& proof, size_t num_public_inputs) const
69 {
70 // KernelIO is at the end of public inputs, which are at the start of the proof
71 size_t idx = num_public_inputs - PUBLIC_INPUTS_SIZE;
72
73 // Serialize fq to 2 fr limbs using 136-bit encoding (matching FrCodec)
74 auto serialize_fq = [&](const NativeFq& fq_val) {
75 constexpr uint64_t NUM_LIMB_BITS = 2 * NUM_LIMB_BITS_IN_FIELD_SIMULATION; // 136 bits
76 constexpr uint256_t LIMB_MASK = (uint256_t(1) << NUM_LIMB_BITS) - 1;
77 uint256_t val = static_cast<uint256_t>(fq_val);
78 proof[idx++] = NativeFF(val & LIMB_MASK);
79 proof[idx++] = NativeFF((val >> NUM_LIMB_BITS) & LIMB_MASK);
80 };
81
82 auto serialize_point = [&](const NativeG1& point) {
83 if (point.is_point_at_infinity()) {
84 for (size_t i = 0; i < NativeG1::PUBLIC_INPUTS_SIZE; ++i) {
85 proof[idx++] = NativeFF::zero();
86 }
87 } else {
88 serialize_fq(point.x);
89 serialize_fq(point.y);
90 }
91 };
92
93 serialize_point(pairing_inputs.P0());
94 serialize_point(pairing_inputs.P1());
95 serialize_point(kernel_return_data);
96 serialize_point(app_return_data);
97 proof[idx++] = ecc_op_hash;
98 proof[idx] = output_hn_accum_hash;
99 }
100};
101
109 public:
114 using NativeTableCommitments = std::array<NativeG1, MegaCircuitBuilder::NUM_WIRES>;
115
116 static constexpr size_t PUBLIC_INPUTS_SIZE = HIDING_KERNEL_PUBLIC_INPUTS_SIZE;
117
121
127 static HidingKernelIOSerde from_proof(const std::vector<NativeFF>& proof, size_t num_public_inputs)
128 {
129 HidingKernelIOSerde result;
130 size_t idx = num_public_inputs - PUBLIC_INPUTS_SIZE;
131
132 auto deserialize_point = [&]() {
134 NativeG1::PUBLIC_INPUTS_SIZE);
135 idx += NativeG1::PUBLIC_INPUTS_SIZE;
136 return FrCodec::deserialize_from_fields<NativeG1>(limbs);
137 };
138
139 result.pairing_inputs.P0() = deserialize_point();
140 result.pairing_inputs.P1() = deserialize_point();
141 result.kernel_return_data = deserialize_point();
142 for (auto& commitment : result.ecc_op_tables) {
143 commitment = deserialize_point();
144 }
145
146 return result;
147 }
148
154 void to_proof(std::vector<NativeFF>& proof, size_t num_public_inputs) const
155 {
156 size_t idx = num_public_inputs - PUBLIC_INPUTS_SIZE;
157
158 auto serialize_fq = [&](const NativeFq& fq_val) {
159 constexpr uint64_t NUM_LIMB_BITS = 2 * NUM_LIMB_BITS_IN_FIELD_SIMULATION;
160 constexpr uint256_t LIMB_MASK = (uint256_t(1) << NUM_LIMB_BITS) - 1;
161 uint256_t val = static_cast<uint256_t>(fq_val);
162 proof[idx++] = NativeFF(val & LIMB_MASK);
163 proof[idx++] = NativeFF((val >> NUM_LIMB_BITS) & LIMB_MASK);
164 };
165
166 auto serialize_point = [&](const NativeG1& point) {
167 if (point.is_point_at_infinity()) {
168 for (size_t i = 0; i < NativeG1::PUBLIC_INPUTS_SIZE; ++i) {
169 proof[idx++] = NativeFF::zero();
170 }
171 } else {
172 serialize_fq(point.x);
173 serialize_fq(point.y);
174 }
175 };
176
177 serialize_point(pairing_inputs.P0());
178 serialize_point(pairing_inputs.P1());
179 serialize_point(kernel_return_data);
180 for (const auto& commitment : ecc_op_tables) {
181 serialize_point(commitment);
182 }
183 }
184};
185
193 public:
198
199 static constexpr size_t PUBLIC_INPUTS_SIZE = DEFAULT_PUBLIC_INPUTS_SIZE; // 16 fr elements
200
202
210 static AppIOSerde from_proof(const std::vector<NativeFF>& proof, size_t num_public_inputs)
211 {
212 AppIOSerde result;
213 // AppIO is at the end of public inputs, which are at the start of the proof
214 size_t idx = num_public_inputs - PUBLIC_INPUTS_SIZE;
215
216 // Each G1 point is 4 fr elements (2 limbs for x, 2 limbs for y) using 136-bit limb encoding
217 auto deserialize_point = [&]() {
219 NativeG1::PUBLIC_INPUTS_SIZE);
220 idx += NativeG1::PUBLIC_INPUTS_SIZE;
221 return FrCodec::deserialize_from_fields<NativeG1>(limbs);
222 };
223
224 result.pairing_inputs.P0() = deserialize_point();
225 result.pairing_inputs.P1() = deserialize_point();
226
227 return result;
228 }
229
235 void to_proof(std::vector<NativeFF>& proof, size_t num_public_inputs) const
236 {
237 // AppIO is at the end of public inputs, which are at the start of the proof
238 size_t idx = num_public_inputs - PUBLIC_INPUTS_SIZE;
239
240 auto serialize_fq = [&](const NativeFq& fq_val) {
241 constexpr uint64_t NUM_LIMB_BITS = 2 * NUM_LIMB_BITS_IN_FIELD_SIMULATION;
242 constexpr uint256_t LIMB_MASK = (uint256_t(1) << NUM_LIMB_BITS) - 1;
243 uint256_t val = static_cast<uint256_t>(fq_val);
244 proof[idx++] = NativeFF(val & LIMB_MASK);
245 proof[idx++] = NativeFF((val >> NUM_LIMB_BITS) & LIMB_MASK);
246 };
247
248 auto serialize_point = [&](const NativeG1& point) {
249 if (point.is_point_at_infinity()) {
250 for (size_t i = 0; i < NativeG1::PUBLIC_INPUTS_SIZE; ++i) {
251 proof[idx++] = NativeFF::zero();
252 }
253 } else {
254 serialize_fq(point.x);
255 serialize_fq(point.y);
256 }
257 };
258
259 serialize_point(pairing_inputs.P0());
260 serialize_point(pairing_inputs.P1());
261 }
262};
263
264} // namespace bb::stdlib::recursion::honk
bb::fq BaseField
Definition bn254.hpp:19
typename Group::affine_element AffineElement
Definition bn254.hpp:22
Native representation and serde for AppIO public inputs.
void to_proof(std::vector< NativeFF > &proof, size_t num_public_inputs) const
Serialize AppIO back to a proof vector.
static AppIOSerde from_proof(const std::vector< NativeFF > &proof, size_t num_public_inputs)
Deserialize AppIO from a proof vector.
Native representation and serde for HidingKernelIO public inputs.
void to_proof(std::vector< NativeFF > &proof, size_t num_public_inputs) const
Serialize HidingKernelIO back to a proof vector.
std::array< NativeG1, MegaCircuitBuilder::NUM_WIRES > NativeTableCommitments
static HidingKernelIOSerde from_proof(const std::vector< NativeFF > &proof, size_t num_public_inputs)
Deserialize HidingKernelIO from a proof vector.
For test purposes only: Native representation and serde for KernelIO public inputs
void to_proof(std::vector< NativeFF > &proof, size_t num_public_inputs) const
Serialize KernelIO back to a proof vector.
static KernelIOSerde from_proof(const std::vector< NativeFF > &proof, size_t num_public_inputs)
Deserialize KernelIO from a proof vector.
field< Bn254FrParams > fr
Definition fr.hpp:155
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static constexpr field zero()