Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
fuzzer_comparison_helper.cpp
Go to the documentation of this file.
1// This file contains helper functions to compare simulation results from different AVM2 C++ simulator runs.
2// It is needed as opposed to relying on operator== because some fields (like halting_message) are not compared (since
3// they are known to be different)
5
6#include <string>
7#include <vector>
8
10
11using namespace bb::avm2;
12
14{
15 if (a.timestamp != b.timestamp || a.phase != b.phase || a.contract_address != b.contract_address ||
16 a.caller_pc != b.caller_pc || a.calldata != b.calldata || a.is_static_call != b.is_static_call ||
17 a.gas_limit != b.gas_limit || a.output != b.output || a.reverted != b.reverted ||
18 a.internal_call_stack_at_exit != b.internal_call_stack_at_exit || a.num_nested_calls != b.num_nested_calls) {
19 return false;
20 }
21 // Compare nested calls recursively
22 if (a.nested.size() != b.nested.size()) {
23 return false;
24 }
25 for (size_t i = 0; i < a.nested.size(); ++i) {
26 if (!compare_call_stack_metadata(a.nested[i], b.nested[i])) {
27 return false;
28 }
29 }
30 return true;
31}
32
34{
35 if (a.size() != b.size()) {
36 return false;
37 }
38 for (size_t i = 0; i < a.size(); ++i) {
39 if (!compare_call_stack_metadata(a[i], b[i])) {
40 return false;
41 }
42 }
43 return true;
44}
45
47{
48 BB_ASSERT(results.size() >= 2, "Need at least 2 results to compare");
49 BB_ASSERT(std::all_of(results.begin(),
50 results.end(),
51 [](const TxSimulationResult& result) { return result.public_inputs.has_value(); }),
52 "All results must have public_inputs to compare");
53
54 for (size_t i = 1; i < results.size(); ++i) {
55 const auto& prev = results[i - 1];
56 const auto& curr = results[i];
57
58 std::vector<std::string> mismatches;
59
60 // Compare TxSimulationResult fields
61 if (prev.gas_used != curr.gas_used) {
62 mismatches.push_back("gas_used");
63 }
64 if (prev.revert_code != curr.revert_code) {
65 mismatches.push_back("revert_code");
66 }
67 if (prev.public_tx_effect != curr.public_tx_effect) {
68 mismatches.push_back("public_tx_effect");
69 }
70 // call_stack_metadata uses custom comparison that ignores halting_message
71 if (!compare_call_stack_metadata_vec(prev.call_stack_metadata, curr.call_stack_metadata)) {
72 mismatches.push_back("call_stack_metadata");
73 }
74
75 // Compare public_inputs output fields (guaranteed present by assertion)
76 const auto& pi_prev = prev.public_inputs.value();
77 const auto& pi_curr = curr.public_inputs.value();
78
79 if (pi_prev.end_tree_snapshots != pi_curr.end_tree_snapshots) {
80 mismatches.push_back("public_inputs.end_tree_snapshots");
81 }
82 if (pi_prev.end_gas_used != pi_curr.end_gas_used) {
83 mismatches.push_back("public_inputs.end_gas_used");
84 }
85 if (pi_prev.accumulated_data_array_lengths != pi_curr.accumulated_data_array_lengths) {
86 mismatches.push_back("public_inputs.accumulated_data_array_lengths");
87 }
88 if (pi_prev.accumulated_data != pi_curr.accumulated_data) {
89 mismatches.push_back("public_inputs.accumulated_data");
90 }
91 if (pi_prev.transaction_fee != pi_curr.transaction_fee) {
92 mismatches.push_back("public_inputs.transaction_fee");
93 }
94 if (pi_prev.reverted != pi_curr.reverted) {
95 mismatches.push_back("public_inputs.reverted");
96 }
97
98 if (!mismatches.empty()) {
99 fuzz_info("=== Mismatch between results ", i - 1, " and ", i, " ===");
100 for (const auto& field : mismatches) {
101 fuzz_info(" MISMATCH in ", field);
102 }
103 return false;
104 }
105 }
106 return true;
107}
#define BB_ASSERT(expression,...)
Definition assert.hpp:80
#define fuzz_info(...)
Definition constants.hpp:51
FF a
FF b
bool compare_call_stack_metadata_vec(const std::vector< CallStackMetadata > &a, const std::vector< CallStackMetadata > &b)
bool compare_call_stack_metadata(const CallStackMetadata &a, const CallStackMetadata &b)
bool compare_cpp_simulator_results(const std::vector< TxSimulationResult > &results)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
General class for prime fields see Prime field documentation["field documentation"] for general imple...