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
26
32
40 static KernelIOSerde_ from_proof(const std::vector<NativeFF>& proof, size_t num_public_inputs)
41 {
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
122 static constexpr size_t PUBLIC_INPUTS_SIZE = DEFAULT_PUBLIC_INPUTS_SIZE; // 16 fr elements
123
125
133 static AppIOSerde from_proof(const std::vector<NativeFF>& proof, size_t num_public_inputs)
134 {
136 // AppIO is at the end of public inputs, which are at the start of the proof
137 size_t idx = num_public_inputs - PUBLIC_INPUTS_SIZE;
138
139 // Each G1 point is 4 fr elements (2 limbs for x, 2 limbs for y) using 136-bit limb encoding
140 auto deserialize_point = [&]() {
142 NativeG1::PUBLIC_INPUTS_SIZE);
143 idx += NativeG1::PUBLIC_INPUTS_SIZE;
144 return FrCodec::deserialize_from_fields<NativeG1>(limbs);
145 };
146
147 result.pairing_inputs.P0() = deserialize_point();
148 result.pairing_inputs.P1() = deserialize_point();
149
150 return result;
151 }
152
158 void to_proof(std::vector<NativeFF>& proof, size_t num_public_inputs) const
159 {
160 // AppIO is at the end of public inputs, which are at the start of the proof
161 size_t idx = num_public_inputs - PUBLIC_INPUTS_SIZE;
162
163 auto serialize_fq = [&](const NativeFq& fq_val) {
164 constexpr uint64_t NUM_LIMB_BITS = 2 * NUM_LIMB_BITS_IN_FIELD_SIMULATION;
165 constexpr uint256_t LIMB_MASK = (uint256_t(1) << NUM_LIMB_BITS) - 1;
166 uint256_t val = static_cast<uint256_t>(fq_val);
167 proof[idx++] = NativeFF(val & LIMB_MASK);
168 proof[idx++] = NativeFF((val >> NUM_LIMB_BITS) & LIMB_MASK);
169 };
170
171 auto serialize_point = [&](const NativeG1& point) {
172 if (point.is_point_at_infinity()) {
173 for (size_t i = 0; i < NativeG1::PUBLIC_INPUTS_SIZE; ++i) {
174 proof[idx++] = NativeFF::zero();
175 }
176 } else {
177 serialize_fq(point.x);
178 serialize_fq(point.y);
179 }
180 };
181
182 serialize_point(pairing_inputs.P0());
183 serialize_point(pairing_inputs.P1());
184 }
185};
186
187} // namespace bb::stdlib::recursion::honk
constexpr size_t N
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.
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()
VectorField result