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
14
16
18
22
23std::vector<FF> random_fields(size_t n)
24{
25 std::vector<FF> fields;
26 fields.reserve(n);
27 for (size_t i = 0; i < n; ++i) {
28 fields.push_back(FF::random_element());
29 }
30 return fields;
31}
32
33std::vector<uint8_t> random_bytes(size_t n)
34{
35 std::vector<uint8_t> bytes;
36 bytes.reserve(n);
37 for (size_t i = 0; i < n; ++i) {
38 bytes.push_back(static_cast<uint8_t>(rand() % 256));
39 }
40 return bytes;
41}
42
44{
46 messages.reserve(n);
47 for (size_t i = 0; i < n; ++i) {
48 messages.push_back(ScopedL2ToL1Message{
49 .message =
51 .recipient = FF::random_element(),
52 .content = FF::random_element(),
53 },
54 .contractAddress = FF::random_element(),
55 });
56 }
57 return messages;
58}
59
61{
63 calls.reserve(n);
64 for (size_t i = 0; i < n; ++i) {
65 calls.push_back(PublicCallRequestWithCalldata{
66 .request{
67 .msgSender = FF::random_element(),
68 .contractAddress = FF::random_element(),
69 .isStaticCall = rand() % 2 == 0,
70 },
71 .calldata = random_fields(5),
72 });
73 }
74 return calls;
75}
76
77Operand random_operand(OperandType operand_type)
78{
79 const auto rand_bytes = random_bytes(simulation::testonly::get_operand_type_sizes().at(operand_type));
80 const uint8_t* pos_ptr = &rand_bytes.at(0);
81
82 switch (operand_type) {
83 case OperandType::INDIRECT8: // Irrelevant bits might be toggled but they are ignored during address resolution.
84 case OperandType::UINT8: {
85 uint8_t operand_u8 = 0;
86 serialize::read(pos_ptr, operand_u8);
87 return Operand::from<uint8_t>(operand_u8);
88 }
89 case OperandType::TAG: {
90 uint8_t operand_u8 = 0;
91 serialize::read(pos_ptr, operand_u8);
92 return Operand::from<uint8_t>(operand_u8 % static_cast<uint8_t>(MemoryTag::MAX) +
93 1); // Insecure bias but it is fine for testing purposes.
94 }
95 case OperandType::INDIRECT16: // Irrelevant bits might be toggled but they are ignored during address resolution.
96 case OperandType::UINT16: {
97 uint16_t operand_u16 = 0;
98 serialize::read(pos_ptr, operand_u16);
99 return Operand::from<uint16_t>(operand_u16);
100 }
101 case OperandType::UINT32: {
102 uint32_t operand_u32 = 0;
103 serialize::read(pos_ptr, operand_u32);
104 return Operand::from<uint32_t>(operand_u32);
105 }
106 case OperandType::UINT64: {
107 uint64_t operand_u64 = 0;
108 serialize::read(pos_ptr, operand_u64);
109 return Operand::from<uint64_t>(operand_u64);
110 }
111 case OperandType::UINT128: {
112 uint128_t operand_u128 = 0;
113 serialize::read(pos_ptr, operand_u128);
114 return Operand::from<uint128_t>(operand_u128);
115 }
116 case OperandType::FF:
117 return Operand::from<FF>(FF::random_element());
118 }
119
120 // Need this for gcc compilation even though we fully handle the switch cases.
121 // We never reach this point.
122 __builtin_unreachable();
123}
124
126{
128 std::vector<Operand> operands;
129 uint16_t indirect = 0;
130 operands.reserve(format.size()); // Might be a bit larger (due to indirect)
131
132 for (const auto& operand_type : format) {
133 switch (operand_type) {
134 case OperandType::INDIRECT8:
135 indirect = random_operand(operand_type).as<uint8_t>();
136 break;
137 case OperandType::INDIRECT16:
138 indirect = random_operand(operand_type).as<uint16_t>();
139 break;
140 default:
141 operands.emplace_back(random_operand(operand_type));
142 break;
143 }
144 }
145
146 return Instruction{
147 .opcode = w_opcode,
148 .indirect = indirect,
149 .operands = std::move(operands),
150 };
151}
152
154{
155 return TestTraceContainer::from_rows({ { .precomputed_first_row = 1 }, { .precomputed_clk = 1 } });
156}
157
159{
160 ContractInstance instance = { .salt = FF::random_element(),
161 .deployer_addr = FF::random_element(),
162 .current_class_id = FF::random_element(),
163 .original_class_id = FF::random_element(),
164 .initialisation_hash = FF::random_element(),
165 .public_keys = PublicKeys{
167 .incoming_viewing_key = AffinePoint::random_element(),
168 .outgoing_viewing_key = AffinePoint::random_element(),
169 .tagging_key = AffinePoint::random_element(),
170 } };
171 return instance;
172}
173
175{
176 return ContractClass{ .artifact_hash = FF::random_element(),
177 .private_function_root = FF::random_element(),
178 .public_bytecode_commitment = FF::random_element(),
179 .packed_bytecode = random_bytes(bytecode_size) };
180}
181
183{
184 // cwd is expected to be barretenberg/cpp/build.
185 auto data = read_file("../src/barretenberg/vm2/testing/minimal_tx.testdata.bin");
187
188 AvmSimulationHelper simulation_helper(inputs.hints);
189
190 auto events = simulation_helper.simulate();
191
192 AvmTraceGenHelper trace_gen_helper;
193
194 auto trace = trace_gen_helper.generate_trace(std::move(events), inputs.publicInputs);
195
196 return { std::move(trace), inputs.publicInputs };
197}
198
200{
201 return std::getenv("AVM_SLOW_TESTS") == nullptr;
202}
203
204} // namespace bb::avm2::testing
simulation::EventsContainer simulate()
tracegen::TraceContainer generate_trace(simulation::EventsContainer &&events, const PublicInputs &public_inputs)
static TestTraceContainer from_rows(const std::vector< AvmFullRow > &rows)
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:20
const std::vector< FF > data
TestTraceContainer trace
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:60
ContractClass random_contract_class(size_t bytecode_size)
Definition fixtures.cpp:174
std::vector< uint8_t > random_bytes(size_t n)
Definition fixtures.cpp:33
Operand random_operand(OperandType operand_type)
Definition fixtures.cpp:77
bool skip_slow_tests()
Check if slow tests should be skipped.
Definition fixtures.cpp:199
Instruction random_instruction(WireOpCode w_opcode)
Definition fixtures.cpp:125
ContractInstance random_contract_instance()
Definition fixtures.cpp:158
TestTraceContainer empty_trace()
Definition fixtures.cpp:153
std::vector< ScopedL2ToL1Message > random_l2_to_l1_messages(size_t n)
Definition fixtures.cpp:43
std::vector< FF > random_fields(size_t n)
Definition fixtures.cpp:23
std::pair< tracegen::TraceContainer, PublicInputs > get_minimal_trace_with_pi()
Definition fixtures.cpp:182
std::vector< uint8_t > read_file(const std::string &filename, size_t bytes=0)
Definition file_io.hpp:29
void read(auto &it, msgpack_concepts::HasMsgPack auto &obj)
Automatically derived read for any object that defines .msgpack() (implicitly defined by MSGPACK_FIEL...
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
unsigned __int128 uint128_t
Definition serialize.hpp:44
static AvmProvingInputs from(const std::vector< uint8_t > &data)
AffinePoint nullifier_key