Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bbapi_wire_convert.hpp
Go to the documentation of this file.
1#pragma once
17#include "barretenberg/bbapi/generated/bb_types.hpp"
27
28#include <array>
29#include <utility>
30#include <vector>
31
32namespace bb::bbapi {
33
34// ---------------------------------------------------------------------------
35// Field element conversions. All field types (bb::fr, bb::fq, grumpkin::fr,
36// grumpkin::fq, secp256k1::*, secp256r1::*) pack as msgpack bin32. The wire
37// aliases are nominal C++ wrappers over the same 32 bytes, so conversions are
38// just serialize_to_buffer / serialize_from_buffer at the boundary.
39// ---------------------------------------------------------------------------
40
41inline const std::array<uint8_t, 32>& wire_bytes(const std::array<uint8_t, 32>& w)
42{
43 return w;
44}
45
46template <typename Wire> inline const std::array<uint8_t, 32>& wire_bytes(const Wire& w)
47{
48 return static_cast<const std::array<uint8_t, 32>&>(w);
49}
50
51template <typename Field> inline std::array<uint8_t, 32> field_to_bytes(const Field& d)
52{
53 std::array<uint8_t, 32> r{};
54 Field::serialize_to_buffer(d, r.data());
55 return r;
56}
57
58template <typename Field> inline std::array<uint8_t, 32> field_to_wire(const Field& d)
59{
60 return field_to_bytes(d);
61}
62
63template <typename Wire, typename Field> inline Wire field_to_wire_as(const Field& d)
64{
65 return Wire{ field_to_bytes(d) };
66}
67
68template <typename Field, typename Wire> inline Field field_from_wire(const Wire& w)
69{
70 return Field::serialize_from_buffer(wire_bytes(w).data());
71}
72
73inline Fr fr_to_wire(const bb::fr& d)
74{
75 return field_to_wire_as<Fr>(d);
76}
77inline bb::fr fr_from_wire(const Fr& w)
78{
79 return field_from_wire<bb::fr>(w);
80}
81
82inline std::vector<Fr> fr_vec_to_wire(const std::vector<bb::fr>& d)
83{
84 std::vector<Fr> r;
85 r.reserve(d.size());
86 for (const auto& x : d) {
87 r.push_back(fr_to_wire(x));
88 }
89 return r;
90}
91
92inline std::vector<bb::fr> fr_vec_from_wire(const std::vector<Fr>& w)
93{
94 std::vector<bb::fr> r;
95 r.reserve(w.size());
96 for (const auto& x : w) {
97 r.push_back(fr_from_wire(x));
98 }
99 return r;
100}
101
102template <std::size_t N> inline std::array<Fr, N> fr_array_to_wire(const std::array<bb::fr, N>& d)
103{
105 for (std::size_t i = 0; i < N; ++i) {
106 r[i] = fr_to_wire(d[i]);
107 }
108 return r;
109}
110
111template <std::size_t N> inline std::array<bb::fr, N> fr_array_from_wire(const std::array<Fr, N>& w)
112{
114 for (std::size_t i = 0; i < N; ++i) {
115 r[i] = fr_from_wire(w[i]);
116 }
117 return r;
118}
119
120// ---------------------------------------------------------------------------
121// Curve point conversions. Wire types follow a uniform {Fr x, Fr y} shape.
122// Domain types use the curve-specific affine_element. The default
123// affine_element msgpack adapter packs as a 2-field map {x: bin32, y: bin32},
124// matching the wire encoding, so field-by-field conversion is safe.
125// ---------------------------------------------------------------------------
126
127inline wire::GrumpkinPoint grumpkin_point_to_wire(const grumpkin::g1::affine_element& d)
128{
129 return { .x = field_to_wire_as<Fr>(d.x), .y = field_to_wire_as<Fr>(d.y) };
130}
131
132inline grumpkin::g1::affine_element grumpkin_point_from_wire(const wire::GrumpkinPoint& w)
133{
134 return { field_from_wire<grumpkin::fq>(w.x), field_from_wire<grumpkin::fq>(w.y) };
135}
136
138{
140 r.reserve(d.size());
141 for (const auto& p : d) {
142 r.push_back(grumpkin_point_to_wire(p));
143 }
144 return r;
145}
146
148{
150 r.reserve(w.size());
151 for (const auto& p : w) {
152 r.push_back(grumpkin_point_from_wire(p));
153 }
154 return r;
155}
156
157inline wire::Bn254G1Point bn254_g1_point_to_wire(const bb::g1::affine_element& d)
158{
159 return { .x = field_to_wire_as<Fq>(d.x), .y = field_to_wire_as<Fq>(d.y) };
160}
161
162inline bb::g1::affine_element bn254_g1_point_from_wire(const wire::Bn254G1Point& w)
163{
164 return { field_from_wire<bb::fq>(w.x), field_from_wire<bb::fq>(w.y) };
165}
166
167// Fq2 = { c0: bb::fq, c1: bb::fq }; wire Fq2 is two fq bin32 aliases.
169{
170 return { field_to_wire_as<Fq>(d.c0), field_to_wire_as<Fq>(d.c1) };
171}
172
174{
175 return { field_from_wire<bb::fq>(w[0]), field_from_wire<bb::fq>(w[1]) };
176}
177
178inline wire::Bn254G2Point bn254_g2_point_to_wire(const bb::g2::affine_element& d)
179{
180 return { .x = fq2_to_wire(d.x), .y = fq2_to_wire(d.y) };
181}
182
183inline bb::g2::affine_element bn254_g2_point_from_wire(const wire::Bn254G2Point& w)
184{
185 return { fq2_from_wire(w.x), fq2_from_wire(w.y) };
186}
187
188inline wire::Secp256k1Point secp256k1_point_to_wire(const secp256k1::g1::affine_element& d)
189{
190 return { .x = field_to_wire_as<Secp256k1Fq>(d.x), .y = field_to_wire_as<Secp256k1Fq>(d.y) };
191}
192
193inline secp256k1::g1::affine_element secp256k1_point_from_wire(const wire::Secp256k1Point& w)
194{
195 return { field_from_wire<secp256k1::fq>(w.x), field_from_wire<secp256k1::fq>(w.y) };
196}
197
198inline wire::Secp256r1Point secp256r1_point_to_wire(const secp256r1::g1::affine_element& d)
199{
200 return { .x = field_to_wire_as<Secp256r1Fq>(d.x), .y = field_to_wire_as<Secp256r1Fq>(d.y) };
201}
202
203inline secp256r1::g1::affine_element secp256r1_point_from_wire(const wire::Secp256r1Point& w)
204{
205 return { field_from_wire<secp256r1::fq>(w.x), field_from_wire<secp256r1::fq>(w.y) };
206}
207
208// ---------------------------------------------------------------------------
209// uint256_t ↔ Uint256 (= std::array<uint8_t, 32>).
210// Wire format is 32 bytes big-endian (matches uint256_t::msgpack_pack).
211// ---------------------------------------------------------------------------
212
214{
215 Uint256 r{};
216 for (std::size_t i = 0; i < 4; ++i) {
217 const uint64_t v = d.data[3 - i];
218 for (std::size_t j = 0; j < 8; ++j) {
219 r[i * 8 + j] = static_cast<uint8_t>(v >> (56 - j * 8));
220 }
221 }
222 return r;
223}
224
226{
227 uint64_t parts[4]{};
228 for (std::size_t i = 0; i < 4; ++i) {
229 uint64_t v = 0;
230 for (std::size_t j = 0; j < 8; ++j) {
231 v = (v << 8) | w[i * 8 + j];
232 }
233 parts[i] = v;
234 }
235 return bb::numeric::uint256_t(parts[3], parts[2], parts[1], parts[0]);
236}
237
238inline std::vector<Uint256> uint256_vec_to_wire(const std::vector<bb::numeric::uint256_t>& d)
239{
241 r.reserve(d.size());
242 for (const auto& x : d) {
243 r.push_back(uint256_to_wire(x));
244 }
245 return r;
246}
247
248inline std::vector<bb::numeric::uint256_t> uint256_vec_from_wire(const std::vector<Uint256>& w)
249{
250 std::vector<bb::numeric::uint256_t> r;
251 r.reserve(w.size());
252 for (const auto& x : w) {
253 r.push_back(uint256_from_wire(x));
254 }
255 return r;
256}
257
258inline ChonkProof chonk_proof_from_wire(wire::ChonkProof&& w)
259{
260 return ChonkProof(fr_vec_from_wire(w.hiding_oink_proof),
261 fr_vec_from_wire(w.merge_proof),
262 fr_vec_from_wire(w.eccvm_proof),
263 fr_vec_from_wire(w.ipa_proof),
264 fr_vec_from_wire(w.joint_proof));
265}
266
267inline wire::ChonkProof chonk_proof_to_wire(const ChonkProof& d)
268{
269 return { .hiding_oink_proof = fr_vec_to_wire(d.hiding_oink_proof),
270 .merge_proof = fr_vec_to_wire(d.merge_proof),
271 .eccvm_proof = fr_vec_to_wire(d.eccvm_proof),
272 .ipa_proof = fr_vec_to_wire(d.ipa_proof),
273 .joint_proof = fr_vec_to_wire(d.joint_proof) };
274}
275
277{
279 r.reserve(w.size());
280 for (auto& p : w) {
281 r.push_back(chonk_proof_from_wire(std::move(p)));
282 }
283 return r;
284}
285
286} // namespace bb::bbapi
Stateful Chonk batch-verifier service used by the IPC handlers.
Shared type definitions for the Barretenberg RPC API.
bb::numeric::uint256_t uint256_from_wire(const Uint256 &w)
wire::ChonkProof chonk_proof_to_wire(const ChonkProof &d)
Field field_from_wire(const Wire &w)
bb::fq2 fq2_from_wire(const std::array< Fq, 2 > &w)
std::vector< Uint256 > uint256_vec_to_wire(const std::vector< bb::numeric::uint256_t > &d)
ChonkProof chonk_proof_from_wire(wire::ChonkProof &&w)
std::vector< Fr > fr_vec_to_wire(const std::vector< bb::fr > &d)
Wire field_to_wire_as(const Field &d)
Fr fr_to_wire(const bb::fr &d)
wire::Bn254G2Point bn254_g2_point_to_wire(const bb::g2::affine_element &d)
std::vector< bb::numeric::uint256_t > uint256_vec_from_wire(const std::vector< Uint256 > &w)
std::vector< ChonkProof > chonk_proof_vec_from_wire(std::vector< wire::ChonkProof > &&w)
secp256r1::g1::affine_element secp256r1_point_from_wire(const wire::Secp256r1Point &w)
std::vector< grumpkin::g1::affine_element > grumpkin_point_vec_from_wire(const std::vector< wire::GrumpkinPoint > &w)
std::array< Fq, 2 > fq2_to_wire(const bb::fq2 &d)
std::array< bb::fr, N > fr_array_from_wire(const std::array< Fr, N > &w)
std::array< Fr, N > fr_array_to_wire(const std::array< bb::fr, N > &d)
secp256k1::g1::affine_element secp256k1_point_from_wire(const wire::Secp256k1Point &w)
bb::fr fr_from_wire(const Fr &w)
bb::g1::affine_element bn254_g1_point_from_wire(const wire::Bn254G1Point &w)
wire::GrumpkinPoint grumpkin_point_to_wire(const grumpkin::g1::affine_element &d)
bb::g2::affine_element bn254_g2_point_from_wire(const wire::Bn254G2Point &w)
std::vector< bb::fr > fr_vec_from_wire(const std::vector< Fr > &w)
std::vector< wire::GrumpkinPoint > grumpkin_point_vec_to_wire(const std::vector< grumpkin::g1::affine_element > &d)
wire::Bn254G1Point bn254_g1_point_to_wire(const bb::g1::affine_element &d)
Uint256 uint256_to_wire(const bb::numeric::uint256_t &d)
wire::Secp256r1Point secp256r1_point_to_wire(const secp256r1::g1::affine_element &d)
std::array< uint8_t, 32 > field_to_bytes(const Field &d)
std::array< uint8_t, 32 > field_to_wire(const Field &d)
grumpkin::g1::affine_element grumpkin_point_from_wire(const wire::GrumpkinPoint &w)
wire::Secp256k1Point secp256k1_point_to_wire(const secp256k1::g1::affine_element &d)
const std::array< uint8_t, 32 > & wire_bytes(const std::array< uint8_t, 32 > &w)
ChonkProof_< false > ChonkProof
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::byte * data
HonkProof ipa_proof
HonkProof merge_proof
HonkProof eccvm_proof
HonkProof joint_proof
HonkProof hiding_oink_proof