Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
fixtures.cpp
Go to the documentation of this file.
2
3#include <utility>
4#include <vector>
5
17
19
20namespace bb::avm2::testing {
21
22using simulation::Instruction;
25
26std::vector<FF> random_fields(size_t n)
27{
28 std::vector<FF> fields;
29 fields.reserve(n);
30 for (size_t i = 0; i < n; ++i) {
31 fields.push_back(FF::random_element());
32 }
33 return fields;
34}
35
36std::vector<uint8_t> random_bytes(size_t n)
37{
38 std::vector<uint8_t> bytes;
39 bytes.reserve(n);
40 for (size_t i = 0; i < n; ++i) {
41 bytes.push_back(static_cast<uint8_t>(rand() % 256));
42 }
43 return bytes;
44}
45
47{
49 messages.reserve(n);
50 for (size_t i = 0; i < n; ++i) {
51 messages.push_back(ScopedL2ToL1Message{
52 .message =
55 .content = FF::random_element(),
56 },
57 .contract_address = FF::random_element(),
58 });
59 }
60 return messages;
61}
62
64{
66 calls.reserve(n);
67 for (size_t i = 0; i < n; ++i) {
68 calls.push_back(PublicCallRequestWithCalldata{
69 .request{
71 .contract_address = FF::random_element(),
72 .is_static_call = rand() % 2 == 0,
73 },
74 .calldata = random_fields(5),
75 });
76 }
77 return calls;
78}
79
80Operand random_operand(OperandType operand_type)
81{
82 const auto rand_bytes = random_bytes(simulation::testonly::get_operand_type_sizes().at(operand_type));
83 const uint8_t* pos_ptr = &rand_bytes.at(0);
84
85 switch (operand_type) {
86 case OperandType::INDIRECT8: // Irrelevant bits might be toggled but they are ignored during address resolution.
87 case OperandType::UINT8: {
88 uint8_t operand_u8 = 0;
89 serialize::read(pos_ptr, operand_u8);
90 return Operand::from<uint8_t>(operand_u8);
91 }
92 case OperandType::TAG: {
93 uint8_t operand_u8 = 0;
94 serialize::read(pos_ptr, operand_u8);
95 return Operand::from<uint8_t>(operand_u8 % static_cast<uint8_t>(MemoryTag::MAX) +
96 1); // Insecure bias but it is fine for testing purposes.
97 }
98 case OperandType::INDIRECT16: // Irrelevant bits might be toggled but they are ignored during address resolution.
99 case OperandType::UINT16: {
100 uint16_t operand_u16 = 0;
101 serialize::read(pos_ptr, operand_u16);
102 return Operand::from<uint16_t>(operand_u16);
103 }
104 case OperandType::UINT32: {
105 uint32_t operand_u32 = 0;
106 serialize::read(pos_ptr, operand_u32);
107 return Operand::from<uint32_t>(operand_u32);
108 }
109 case OperandType::UINT64: {
110 uint64_t operand_u64 = 0;
111 serialize::read(pos_ptr, operand_u64);
112 return Operand::from<uint64_t>(operand_u64);
113 }
114 case OperandType::UINT128: {
115 uint128_t operand_u128 = 0;
116 serialize::read(pos_ptr, operand_u128);
117 return Operand::from<uint128_t>(operand_u128);
118 }
119 case OperandType::FF:
120 return Operand::from<FF>(FF::random_element());
121 }
122
123 // Need this for gcc compilation even though we fully handle the switch cases.
124 // We never reach this point.
125 __builtin_unreachable();
126}
127
129{
131 std::vector<Operand> operands;
132 uint16_t addressing_mode = 0;
133 operands.reserve(format.size()); // Might be a bit larger (due to addressing_mode)
134
135 for (const auto& operand_type : format) {
136 switch (operand_type) {
137 case OperandType::INDIRECT8:
138 addressing_mode = random_operand(operand_type).as<uint8_t>();
139 break;
140 case OperandType::INDIRECT16:
141 addressing_mode = random_operand(operand_type).as<uint16_t>();
142 break;
143 default:
144 operands.emplace_back(random_operand(operand_type));
145 break;
146 }
147 }
148
149 return Instruction{
150 .opcode = w_opcode,
151 .addressing_mode = addressing_mode,
152 .operands = std::move(operands),
153 };
154}
155
157{
158 using C = Column;
159 return TestTraceContainer({ { { C::precomputed_first_row, 1 } }, { { C::precomputed_idx, 1 } } });
160}
161
163{
164 ContractInstance instance = { .salt = FF::random_element(),
165 .deployer = FF::random_element(),
166 .current_contract_class_id = FF::random_element(),
167 .original_contract_class_id = FF::random_element(),
168 .initialization_hash = FF::random_element(),
169 .immutables_hash = FF::random_element(),
170 .public_keys = PublicKeys{
172 .incoming_viewing_key = AffinePoint::random_element(),
173 .outgoing_viewing_key_hash = FF::random_element(),
174 .tagging_key_hash = FF::random_element(),
175 .message_signing_key_hash = FF::random_element(),
176 .fallback_key_hash = FF::random_element(),
177 } };
178 return instance;
179}
180
187
189{
191 .artifact_hash = FF::random_element(),
192 .private_functions_root = FF::random_element(),
193 .packed_bytecode = random_bytes(bytecode_size) };
194}
195
197{
198 // Minimal program: SET 1 -> [0], SET 2 -> [1], ADD [0]+[1] -> [2], RETURN mem[0..mem[0]) from [2].
201 .operand<uint8_t>(0)
202 .operand(MemoryTag::U32)
203 .operand<uint8_t>(1)
204 .build())
206 .operand<uint8_t>(1)
207 .operand(MemoryTag::U32)
208 .operand<uint8_t>(2)
209 .build())
211 .operand<uint8_t>(0)
212 .operand<uint8_t>(1)
213 .operand<uint8_t>(2)
214 .build())
215 .add(InstructionBuilder(WireOpCode::RETURN).operand<uint16_t>(0).operand<uint16_t>(2).build())
216 .build();
217
219 const auto deployed = tester.deploy_contract(bytecode);
220
222 config.collect_hints = true;
223 config.collect_public_inputs = true;
225 tester.simulate_tx({ TestEnqueuedCall{ .contract_address = deployed.address } }, config);
226
227 return AvmProvingInputs{ .public_inputs = result.public_inputs.value(), .hints = result.hints.value() };
228}
229
231{
233
234 AvmSimulationHelper simulation_helper;
235
236 auto events = simulation_helper.simulate_for_witgen(inputs.hints);
237
238 AvmTraceGenHelper trace_gen_helper;
239 auto trace = trace_gen_helper.generate_trace(std::move(events), inputs.public_inputs);
240
241 return { std::move(trace), inputs.public_inputs };
242}
243
245{
246 return std::getenv("AVM_SKIP_SLOW_TESTS") != nullptr;
247}
248
249} // namespace bb::avm2::testing
simulation::EventsContainer simulate_for_witgen(const ExecutionHints &hints)
tracegen::TraceContainer generate_trace(simulation::EventsContainer &&events, const PublicInputs &public_inputs)
std::vector< uint8_t > build() const
BytecodeBuilder & add(const simulation::Instruction &instruction)
TxSimulationResult simulate_tx(const std::vector< TestEnqueuedCall > &app_calls, const PublicSimulatorConfig &config=default_config())
DeployedContract deploy_contract(std::span< const uint8_t > bytecode, const FF &salt=0)
static affine_element random_element(numeric::RNG *engine=nullptr) noexcept
Samples a random point on the curve.
std::string format(Args... args)
Definition log.hpp:23
TestTraceContainer trace
std::vector< uint8_t > bytecode
AvmProvingInputs inputs
const std::unordered_map< OperandType, uint32_t > & get_operand_type_sizes()
const std::unordered_map< WireOpCode, std::vector< OperandType > > & get_instruction_wire_formats()
std::vector< PublicCallRequestWithCalldata > random_enqueued_calls(size_t n)
Definition fixtures.cpp:63
ContractClass random_contract_class(size_t bytecode_size)
Definition fixtures.cpp:188
std::vector< uint8_t > random_bytes(size_t n)
Definition fixtures.cpp:36
Operand random_operand(OperandType operand_type)
Definition fixtures.cpp:80
bool skip_slow_tests()
Check if slow tests should be skipped.
Definition fixtures.cpp:244
AvmProvingInputs get_minimal_proving_inputs()
Definition fixtures.cpp:196
ContractInstance random_protocol_contract_instance()
Definition fixtures.cpp:181
Instruction random_instruction(WireOpCode w_opcode)
Definition fixtures.cpp:128
ContractInstance random_contract_instance()
Definition fixtures.cpp:162
TestTraceContainer empty_trace()
Definition fixtures.cpp:156
std::vector< ScopedL2ToL1Message > random_l2_to_l1_messages(size_t n)
Definition fixtures.cpp:46
std::vector< FF > random_fields(size_t n)
Definition fixtures.cpp:26
std::pair< tracegen::TraceContainer, PublicInputs > get_minimal_trace_with_pi()
Definition fixtures.cpp:230
void read(auto &it, msgpack_concepts::HasMsgPack auto &obj)
Automatically derived read for any object that defines .msgpack() (implicitly defined by SERIALIZATIO...
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
unsigned __int128 uint128_t
Definition serialize.hpp:45
PublicInputs public_inputs
Definition avm_io.hpp:421
ContractClassId current_contract_class_id
ContractClassId original_contract_class_id
static field random_element(numeric::RNG *engine=nullptr) noexcept
VectorField result