Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
acir_bincode_mocks.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <cstddef>
7#include <vector>
8
10
11const size_t BIT_COUNT = 254;
12
13inline uint8_t hex_char_to_value(char c)
14{
15 if (c >= '0' && c <= '9') {
16 return static_cast<uint8_t>(c - '0');
17 }
18 if (c >= 'a' && c <= 'f') {
19 return static_cast<uint8_t>(10 + (c - 'a'));
20 }
21 if (c >= 'A' && c <= 'F') {
22 return static_cast<uint8_t>(10 + (c - 'A'));
23 }
24 throw std::invalid_argument(std::string("Invalid hex character: '") + c + "'");
25}
26
27// Converts a hex string (must have even length) to a vector<uint8_t>
28inline std::vector<uint8_t> hex_string_to_bytes(const std::string& str)
29{
30 // Allow optional "0x" or "0X" prefix
31 size_t offset = 0;
32 if (str.size() >= 2 && (str[0] == '0') && (str[1] == 'x' || str[1] == 'X')) {
33 offset = 2;
34 }
35 size_t hex_len = str.size() - offset;
36 // Enforce that the input string must represent exactly 32 bytes (64 hex chars)
37 if (hex_len != 64) {
38 throw std::invalid_argument(
39 "Hex string must be exactly 64 characters (32 bytes), excluding optional 0x prefix");
40 }
41 std::vector<uint8_t> bytes;
42 bytes.reserve(32);
43 for (size_t i = 0; i < hex_len; i += 2) {
44 uint8_t high = hex_char_to_value(str[offset + i]);
45 uint8_t low = hex_char_to_value(str[offset + i + 1]);
46 bytes.push_back(static_cast<uint8_t>((high << 4) | low));
47 }
48 return bytes;
49}
50
58inline std::pair<std::vector<uint8_t>, std::vector<uint8_t>> create_simple_circuit_bytecode(size_t num_constraints = 1)
59{
60 std::vector<bb::fr> witness_values;
61
62 // Acir representation of a circuit
63 Acir::Circuit circuit;
64
65 // No public inputs
67
68 std::vector<uint8_t> one = bb::fr::one().to_buffer();
69 std::vector<uint8_t> minus_one = bb::fr(-1).to_buffer();
70
71 // Add num_constraints identical constraints, each using different witnesses
72 for (size_t i = 0; i < num_constraints; ++i) {
74
75 // Create constraint: w[index] * w[index+1] - w[index+2] = 0
76 bb::fr a = bb::fr(2);
77 bb::fr b = bb::fr(3);
78 bb::fr c = a * b;
79 uint32_t a_idx = acir_format::add_to_witness_and_track_indices(witness_values, a);
80 uint32_t b_idx = acir_format::add_to_witness_and_track_indices(witness_values, b);
81 uint32_t c_idx = acir_format::add_to_witness_and_track_indices(witness_values, c);
82
83 expr.mul_terms = { { one, Acir::Witness{ a_idx }, Acir::Witness{ b_idx } } };
84 expr.linear_combinations = { { minus_one, Acir::Witness{ c_idx } } };
85 expr.q_c = bb::fr::zero().to_buffer();
86
87 Acir::Opcode::AssertZero assert_zero;
88 assert_zero.value = expr;
89 Acir::Opcode opcode;
90 opcode.value = assert_zero;
91 circuit.opcodes.push_back(opcode);
92 }
93
94 circuit.current_witness_index = static_cast<uint32_t>(witness_values.size());
95 circuit.function_name = "simple_circuit";
96 circuit.private_parameters = {};
97 circuit.return_values = Acir::PublicInputs{ {} };
98 circuit.assert_messages = {};
99
100 // Create the program
101 Acir::Program program;
102 program.functions = { circuit };
103 program.unconstrained_functions = {};
104
105 // Create witness data
106 Witnesses::WitnessStack witness_stack;
107 Witnesses::StackItem stack_item{};
108
109 // For each constraint, add witnesses: w[i*3]=2, w[i*3+1]=3, w[i*3+2]=6 (so 2*3=6)
110 uint32_t witness_idx = 0;
111 for (size_t i = 0; i < num_constraints; ++i) {
112 for (size_t j = 0; j < 3; ++j) {
113 stack_item.witness.value[Witnesses::Witness{ witness_idx }] = witness_values[witness_idx].to_buffer();
114 witness_idx++;
115 }
116 }
117 witness_stack.stack.push_back(stack_item);
118
119 return { program.bincodeSerialize(), witness_stack.bincodeSerialize() };
120}
121
127inline std::vector<uint8_t> create_simple_kernel(size_t vk_size, bool is_init_kernel)
128{
129 Acir::Circuit circuit;
130 // Create witnesses equal to size of a mega VK in fields.
131 std::vector<Acir::FunctionInput> vk_inputs;
132 for (uint32_t i = 0; i < vk_size; i++) {
135 };
136 vk_inputs.push_back(Acir::FunctionInput{ .value = i_wit });
137 ;
138 }
139
141 std::in_place_type<Acir::FunctionInput::Witness>, Acir::Witness{ static_cast<uint32_t>(vk_size) }
142 };
143 Acir::FunctionInput key_hash{ .value = vk_size_wit };
144 ;
145 size_t total_num_witnesses = /* vk */ vk_size + /* key_hash */ 1;
146
149 hex_string_to_bytes("0000000000000000000000000000000000000000000000000000000000000001")
150 };
151 Acir::FunctionInput predicate{ .value = predicate_const };
152
153 // Modeled after noir-projects/mock-protocol-circuits/crates/mock-private-kernel-init/src/main.nr
154 // We mock the init or tail kernels using OINK or HN respectively.
156 .proof = {},
157 .public_inputs = {},
158 .key_hash = key_hash,
159 .proof_type = is_init_kernel ? acir_format::PROOF_TYPE::OINK
161 .predicate = predicate };
162
163 Acir::BlackBoxFuncCall black_box_call;
164 black_box_call.value = recursion;
165
166 circuit.opcodes.push_back(Acir::Opcode{ Acir::Opcode::BlackBoxFuncCall{ black_box_call } });
167 circuit.current_witness_index = static_cast<uint32_t>(total_num_witnesses);
168 circuit.function_name = "simple_circuit";
169
170 // Create the program with the circuit
171 Acir::Program program;
172 program.functions = { circuit };
173 // Serialize the program using bincode
174 return program.bincodeSerialize();
175}
176
182inline std::vector<uint8_t> create_kernel_witness(const std::vector<bb::fr>& app_vk_fields)
183{
184 Witnesses::WitnessStack kernel_witness;
185 kernel_witness.stack.push_back({});
186 for (uint32_t i = 0; i < app_vk_fields.size(); i++) {
187 std::stringstream ss;
188 ss << app_vk_fields[i];
189 kernel_witness.stack.back().witness.value[Witnesses::Witness{ i }] = hex_string_to_bytes(ss.str());
190 }
191 std::stringstream ss;
192 ss << crypto::Poseidon2<crypto::Poseidon2Bn254ScalarFieldParams>::hash(app_vk_fields);
193 kernel_witness.stack.back().witness.value[Witnesses::Witness{ static_cast<uint32_t>(app_vk_fields.size()) }] =
194 hex_string_to_bytes(ss.str());
195
196 return kernel_witness.bincodeSerialize();
197}
198
199} // namespace bb::acir_bincode_mocks
FF a
FF b
ssize_t offset
Definition engine.cpp:36
std::vector< uint32_t > add_to_witness_and_track_indices(WitnessVector &witness, const T &input)
Append values to a witness vector and track their indices.
Definition utils.hpp:70
std::pair< std::vector< uint8_t >, std::vector< uint8_t > > create_simple_circuit_bytecode(size_t num_constraints=1)
Helper function to create a minimal circuit bytecode and witness for testing.
std::vector< uint8_t > create_simple_kernel(size_t vk_size, bool is_init_kernel)
Create a simple kernel circuit for IVC testing.
std::vector< uint8_t > create_kernel_witness(const std::vector< bb::fr > &app_vk_fields)
Create a kernel witness for IVC testing.
uint8_t hex_char_to_value(char c)
std::vector< uint8_t > hex_string_to_bytes(const std::string &str)
field< Bn254FrParams > fr
Definition fr.hpp:174
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::vector< Acir::FunctionInput > verification_key
Definition acir.hpp:3479
std::variant< AES128Encrypt, AND, XOR, RANGE, Blake2s, Blake3, EcdsaSecp256k1, EcdsaSecp256r1, MultiScalarMul, EmbeddedCurveAdd, Keccakf1600, RecursiveAggregation, Poseidon2Permutation, Sha256Compression > value
Definition acir.hpp:3608
Acir::PublicInputs return_values
Definition acir.hpp:5014
std::vector< Acir::Opcode > opcodes
Definition acir.hpp:5011
uint32_t current_witness_index
Definition acir.hpp:5010
std::vector< Acir::Witness > private_parameters
Definition acir.hpp:5012
Acir::PublicInputs public_parameters
Definition acir.hpp:5013
std::string function_name
Definition acir.hpp:5009
std::vector< std::tuple< Acir::OpcodeLocation, Acir::AssertionPayload > > assert_messages
Definition acir.hpp:5015
std::vector< std::tuple< std::vector< uint8_t >, Acir::Witness > > linear_combinations
Definition acir.hpp:4006
std::vector< uint8_t > q_c
Definition acir.hpp:4007
std::vector< std::tuple< std::vector< uint8_t >, Acir::Witness, Acir::Witness > > mul_terms
Definition acir.hpp:4005
std::variant< Constant, Witness > value
Definition acir.hpp:2968
Acir::Expression value
Definition acir.hpp:4365
std::variant< AssertZero, BlackBoxFuncCall, MemoryOp, MemoryInit, BrilligCall, Call > value
Definition acir.hpp:4552
std::vector< Acir::Circuit > functions
Definition acir.hpp:5093
std::vector< Acir::BrilligBytecode > unconstrained_functions
Definition acir.hpp:5094
std::vector< uint8_t > bincodeSerialize() const
Definition acir.hpp:11508
Witnesses::WitnessMap witness
std::map< Witnesses::Witness, std::vector< uint8_t > > value
std::vector< Witnesses::StackItem > stack
std::vector< uint8_t > bincodeSerialize() const
static constexpr field one()
BB_INLINE std::vector< uint8_t > to_buffer() const
static constexpr field zero()