Skip to content

Commit

Permalink
chore: align AVM witgen's limits on number of side effects with AVM s…
Browse files Browse the repository at this point in the history
…imulator. Witgen supports phases and rollbacks. (#10329)

1. Aligns side effect limits between TS and CPP
2. Adds Noir test functions to spam side effects & adds that to proving
test
2. Adds support to witgen for tx phases
3. Never expects a read hint for nullifier writes. Always just uses the
write hint.
4. Adds an argument to finalize and `gen_trace` to skip end-gas
assertions (not sure this is the best way, but all the tests pass)
5. renames TS hint vectors to all just be `*_reads` or `*_writes`
instead of `*read/update_requests` etc.

Work needed in a follow-up PR:
- separate the opcode switch-case & phase management/enqueued-calls into
separate functions or even separate files in witgen
  • Loading branch information
dbanks12 authored and IlyasRidhuan committed Dec 3, 2024
1 parent 28eec59 commit 30dda53
Show file tree
Hide file tree
Showing 23 changed files with 996 additions and 947 deletions.
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

0 comments on commit 30dda53

Please sign in to comment.