Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: align AVM witgen's limits on number of side effects with AVM simulator. Witgen supports phases and rollbacks. #10329

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace bb::avm_trace {

enum class AvmError : uint32_t {
NO_ERROR,
REVERT_OPCODE,
INVALID_PROGRAM_COUNTER,
INVALID_OPCODE,
INVALID_TAG_VALUE,
Expand All @@ -18,6 +19,7 @@ enum class AvmError : uint32_t {
CONTRACT_INST_MEM_UNKNOWN,
RADIX_OUT_OF_BOUNDS,
DUPLICATE_NULLIFIER,
SIDE_EFFECT_LIMIT_REACHED,
};

} // namespace bb::avm_trace
1,119 changes: 577 additions & 542 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/execution.cpp

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion barretenberg/cpp/src/barretenberg/vm/avm/trace/execution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@

namespace bb::avm_trace {

enum class TxExecutionPhase : uint32_t {
SETUP,
APP_LOGIC,
TEARDOWN,
};

std::string to_name(TxExecutionPhase phase);

class Execution {
public:
static constexpr size_t SRS_SIZE = 1 << 22;
Expand All @@ -31,7 +39,8 @@ class Execution {
// Eventually this will be the bytecode of the dispatch function of top-level contract
static std::vector<Row> gen_trace(AvmPublicInputs const& public_inputs,
std::vector<FF>& returndata,
ExecutionHints const& execution_hints);
ExecutionHints const& execution_hints,
bool apply_end_gas_assertions = false);

// For testing purposes only.
static void set_trace_builder_constructor(TraceBuilderConstructor constructor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ using Poseidon2 = crypto::Poseidon2<crypto::Poseidon2Bn254ScalarFieldParams>;
* UNCONSTRAINED TREE OPERATIONS
**************************************************************************************************/

void AvmMerkleTreeTraceBuilder::checkpoint_non_revertible_state()
{
non_revertible_tree_snapshots = tree_snapshots.copy();
}
void AvmMerkleTreeTraceBuilder::rollback_to_non_revertible_checkpoint()
{
tree_snapshots = non_revertible_tree_snapshots;
}

FF AvmMerkleTreeTraceBuilder::unconstrained_hash_nullifier_preimage(const NullifierLeafPreimage& preimage)
{
return Poseidon2::hash({ preimage.nullifier, preimage.next_nullifier, preimage.next_index });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class AvmMerkleTreeTraceBuilder {

void reset();

void checkpoint_non_revertible_state();
void rollback_to_non_revertible_checkpoint();

bool check_membership(
uint32_t clk, const FF& leaf_value, const uint64_t leaf_index, const std::vector<FF>& path, const FF& root);

Expand Down Expand Up @@ -106,6 +109,7 @@ class AvmMerkleTreeTraceBuilder {

private:
std::vector<MerkleEntry> merkle_check_trace;
TreeSnapshots non_revertible_tree_snapshots;
TreeSnapshots tree_snapshots;
MerkleEntry compute_root_from_path(uint32_t clk,
const FF& leaf_value,
Expand Down
11 changes: 11 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ std::string to_name(AvmError error)
switch (error) {
case AvmError::NO_ERROR:
return "NO ERROR";
case AvmError::REVERT_OPCODE:
return "REVERT OPCODE";
case AvmError::INVALID_PROGRAM_COUNTER:
return "INVALID PROGRAM COUNTER";
case AvmError::INVALID_OPCODE:
Expand All @@ -127,6 +129,10 @@ std::string to_name(AvmError error)
return "CONTRACT INSTANCE MEMBER UNKNOWN";
case AvmError::RADIX_OUT_OF_BOUNDS:
return "RADIX OUT OF BOUNDS";
case AvmError::DUPLICATE_NULLIFIER:
return "DUPLICATE NULLIFIER";
case AvmError::SIDE_EFFECT_LIMIT_REACHED:
return "SIDE EFFECT LIMIT REACHED";
default:
throw std::runtime_error("Invalid error type");
break;
Expand All @@ -138,6 +144,11 @@ bool is_ok(AvmError error)
return error == AvmError::NO_ERROR;
}

bool exceptionally_halted(AvmError error)
{
return error != AvmError::NO_ERROR && error != AvmError::REVERT_OPCODE;
}

/**
*
* ONLY FOR TESTS - Required by dsl module and therefore cannot be moved to test/helpers.test.cpp
Expand Down
1 change: 1 addition & 0 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ std::string to_name(bb::avm_trace::AvmMemoryTag tag);

std::string to_name(AvmError error);
bool is_ok(AvmError error);
bool exceptionally_halted(AvmError error);

// Mutate the inputs
void inject_end_gas_values(AvmPublicInputs& public_inputs, std::vector<Row>& trace);
Expand Down
21 changes: 21 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/public_inputs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,27 @@ struct TreeSnapshots {
return l1_to_l2_message_tree == rhs.l1_to_l2_message_tree && note_hash_tree == rhs.note_hash_tree &&
nullifier_tree == rhs.nullifier_tree && public_data_tree == rhs.public_data_tree;
}
inline TreeSnapshots copy()
{
return {
.l1_to_l2_message_tree = {
.root = l1_to_l2_message_tree.root,
.size = l1_to_l2_message_tree.size,
},
.note_hash_tree = {
.root = note_hash_tree.root,
.size = note_hash_tree.size,
},
.nullifier_tree = {
.root = nullifier_tree.root,
.size = nullifier_tree.size,
},
.public_data_tree = {
.root = public_data_tree.root,
.size = public_data_tree.size,
},
};
}
};

inline void read(uint8_t const*& it, TreeSnapshots& tree_snapshots)
Expand Down
Loading
Loading