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
17template <size_t N> class KernelIOSerde_ {
18 public:
23 using NativeAppReturnDataCommitments = std::array<NativeG1, N>;
24
25 static constexpr size_t PUBLIC_INPUTS_SIZE = kernel_public_inputs_size(N);
26
32
40 static KernelIOSerde_ from_proof(const std::vector<NativeFF>& proof, size_t num_public_inputs)
41 {
42 KernelIOSerde_ result;
43 // KernelIO is at the end of public inputs, which are at the start of the proof
44 size_t idx = num_public_inputs - PUBLIC_INPUTS_SIZE;
45
46 // Each G1 point is 4 fr elements (2 limbs for x, 2 limbs for y) using 136-bit limb encoding
47 auto deserialize_point = [&]() {
49 NativeG1::PUBLIC_INPUTS_SIZE);
50 idx += NativeG1::PUBLIC_INPUTS_SIZE;
51 return FrCodec::deserialize_from_fields<NativeG1>(limbs);
52 };
53
54 result.pairing_inputs.P0() = deserialize_point();
55 result.pairing_inputs.P1() = deserialize_point();
56 result.kernel_return_data = deserialize_point();
57 for (auto& app_commitment : result.app_return_data) {
58 app_commitment = deserialize_point();
59 }
60 result.ecc_op_hash = proof[idx++];
61 result.output_hn_accum_hash = proof[idx];
62
63 return result;
64 }
65
71 void to_proof(std::vector<NativeFF>& proof, size_t num_public_inputs) const
72 {
73 // KernelIO is at the end of public inputs, which are at the start of the proof
74 size_t idx = num_public_inputs - PUBLIC_INPUTS_SIZE;
75
76 // Serialize fq to 2 fr limbs using 136-bit encoding (matching FrCodec)
77 auto serialize_fq = [&](const NativeFq& fq_val) {
78 constexpr uint64_t NUM_LIMB_BITS = 2 * NUM_LIMB_BITS_IN_FIELD_SIMULATION; // 136 bits
79 constexpr uint256_t LIMB_MASK = (uint256_t(1) << NUM_LIMB_BITS) - 1;
80 uint256_t val = static_cast<uint256_t>(fq_val);
81 proof[idx++] = NativeFF(val & LIMB_MASK);
82 proof[idx++] = NativeFF((val >> NUM_LIMB_BITS) & LIMB_MASK);
83 };
84
85 auto serialize_point = [&](const NativeG1& point) {
86 if (point.is_point_at_infinity()) {
87 for (size_t i = 0; i < NativeG1::PUBLIC_INPUTS_SIZE; ++i) {
88 proof[idx++] = NativeFF::zero();
89 }
90 } else {
91 serialize_fq(point.x);
92 serialize_fq(point.y);
93 }
94 };
95
96 serialize_point(pairing_inputs.P0());
97 serialize_point(pairing_inputs.P1());
98 serialize_point(kernel_return_data);
99 for (const auto& app_commitment : app_return_data) {
100 serialize_point(app_commitment);
101 }
102 proof[idx++] = ecc_op_hash;
103 proof[idx] = output_hn_accum_hash;
104 }
105};
106
108
116 public:
121 using NativeTableCommitments = std::array<NativeG1, MegaCircuitBuilder::NUM_WIRES>;
122
123 static constexpr size_t PUBLIC_INPUTS_SIZE = HIDING_KERNEL_PUBLIC_INPUTS_SIZE;
124
128
134 static HidingKernelIOSerde from_proof(const std::vector<NativeFF>& proof, size_t num_public_inputs)
135 {
136 HidingKernelIOSerde result;
137 size_t idx = num_public_inputs - PUBLIC_INPUTS_SIZE;
138
139 auto deserialize_point = [&]() {
141 NativeG1::PUBLIC_INPUTS_SIZE);
142 idx += NativeG1::PUBLIC_INPUTS_SIZE;
143 return FrCodec::deserialize_from_fields<NativeG1>(limbs);
144 };
145
146 result.pairing_inputs.P0() = deserialize_point();
147 result.pairing_inputs.P1() = deserialize_point();
148 result.kernel_return_data = deserialize_point();
149 for (auto& commitment : result.ecc_op_tables) {
150 commitment = deserialize_point();
151 }
152
153 return result;
154 }
155
161 void to_proof(std::vector<NativeFF>& proof, size_t num_public_inputs) const
162 {
163 size_t idx = num_public_inputs - PUBLIC_INPUTS_SIZE;
164
165 auto serialize_fq = [&](const NativeFq& fq_val) {
166 constexpr uint64_t NUM_LIMB_BITS = 2 * NUM_LIMB_BITS_IN_FIELD_SIMULATION;
167 constexpr uint256_t LIMB_MASK = (uint256_t(1) << NUM_LIMB_BITS) - 1;
168 uint256_t val = static_cast<uint256_t>(fq_val);
169 proof[idx++] = NativeFF(val & LIMB_MASK);
170 proof[idx++] = NativeFF((val >> NUM_LIMB_BITS) & LIMB_MASK);
171 };
172
173 auto serialize_point = [&](const NativeG1& point) {
174 if (point.is_point_at_infinity()) {
175 for (size_t i = 0; i < NativeG1::PUBLIC_INPUTS_SIZE; ++i) {
176 proof[idx++] = NativeFF::zero();
177 }
178 } else {
179 serialize_fq(point.x);
180 serialize_fq(point.y);
181 }
182 };
183
184 serialize_point(pairing_inputs.P0());
185 serialize_point(pairing_inputs.P1());
186 serialize_point(kernel_return_data);
187 for (const auto& commitment : ecc_op_tables) {
188 serialize_point(commitment);
189 }
190 }
191};
192
200 public:
205
206 static constexpr size_t PUBLIC_INPUTS_SIZE = DEFAULT_PUBLIC_INPUTS_SIZE; // 16 fr elements
207
209
217 static AppIOSerde from_proof(const std::vector<NativeFF>& proof, size_t num_public_inputs)
218 {
219 AppIOSerde result;
220 // AppIO is at the end of public inputs, which are at the start of the proof
221 size_t idx = num_public_inputs - PUBLIC_INPUTS_SIZE;
222
223 // Each G1 point is 4 fr elements (2 limbs for x, 2 limbs for y) using 136-bit limb encoding
224 auto deserialize_point = [&]() {
226 NativeG1::PUBLIC_INPUTS_SIZE);
227 idx += NativeG1::PUBLIC_INPUTS_SIZE;
228 return FrCodec::deserialize_from_fields<NativeG1>(limbs);
229 };
230
231 result.pairing_inputs.P0() = deserialize_point();
232 result.pairing_inputs.P1() = deserialize_point();
233
234 return result;
235 }
236
242 void to_proof(std::vector<NativeFF>& proof, size_t num_public_inputs) const
243 {
244 // AppIO is at the end of public inputs, which are at the start of the proof
245 size_t idx = num_public_inputs - PUBLIC_INPUTS_SIZE;
246
247 auto serialize_fq = [&](const NativeFq& fq_val) {
248 constexpr uint64_t NUM_LIMB_BITS = 2 * NUM_LIMB_BITS_IN_FIELD_SIMULATION;
249 constexpr uint256_t LIMB_MASK = (uint256_t(1) << NUM_LIMB_BITS) - 1;
250 uint256_t val = static_cast<uint256_t>(fq_val);
251 proof[idx++] = NativeFF(val & LIMB_MASK);
252 proof[idx++] = NativeFF((val >> NUM_LIMB_BITS) & LIMB_MASK);
253 };
254
255 auto serialize_point = [&](const NativeG1& point) {
256 if (point.is_point_at_infinity()) {
257 for (size_t i = 0; i < NativeG1::PUBLIC_INPUTS_SIZE; ++i) {
258 proof[idx++] = NativeFF::zero();
259 }
260 } else {
261 serialize_fq(point.x);
262 serialize_fq(point.y);
263 }
264 };
265
266 serialize_point(pairing_inputs.P0());
267 serialize_point(pairing_inputs.P1());
268 }
269};
270
271} // 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 std::size_t kernel_public_inputs_size(std::size_t num_apps)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static constexpr field zero()