diff --git a/barretenberg/cpp/src/barretenberg/vm2/simulation/gadgets/bytecode_manager.cpp b/barretenberg/cpp/src/barretenberg/vm2/simulation/gadgets/bytecode_manager.cpp index c23b95550a01..80ee05a504b5 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/simulation/gadgets/bytecode_manager.cpp +++ b/barretenberg/cpp/src/barretenberg/vm2/simulation/gadgets/bytecode_manager.cpp @@ -155,7 +155,9 @@ Instruction TxBytecodeManager::read_instruction(const BytecodeId& bytecode_id, std::shared_ptr> TxBytecodeManager::get_bytecode_data(const BytecodeId& bytecode_id) { - return bytecodes.at(bytecode_id); + auto it = bytecodes.find(bytecode_id); + BB_ASSERT(it != bytecodes.end(), "Bytecode not found for the given bytecode_id"); + return it->second; } } // namespace bb::avm2::simulation diff --git a/barretenberg/cpp/src/barretenberg/vm2/simulation/gadgets/concrete_dbs.cpp b/barretenberg/cpp/src/barretenberg/vm2/simulation/gadgets/concrete_dbs.cpp index da800d186eb4..c55559064544 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/simulation/gadgets/concrete_dbs.cpp +++ b/barretenberg/cpp/src/barretenberg/vm2/simulation/gadgets/concrete_dbs.cpp @@ -17,11 +17,15 @@ std::optional ContractDB::get_contract_instance(const AztecAdd } // If we did get a contract instance, we need to prove that the address is derived from the instance. // For protocol contracts the input address is the canonical address, we need to retrieve the derived address. - AztecAddress derived_address = is_protocol_contract_address(address) - ? get_derived_address(protocol_contracts, address) - .value() /* We can assume that get_derived_address will not return a - nullopt, since we have succesfully fetched the instance.*/ - : address; + AztecAddress derived_address; + if (is_protocol_contract_address(address)) { + auto maybe_derived = get_derived_address(protocol_contracts, address); + BB_ASSERT(maybe_derived.has_value(), + "Derived address should be found for protocol contract whose instance is found"); + derived_address = maybe_derived.value(); + } else { + derived_address = address; + } address_derivation.assert_derivation(derived_address, instance.value()); return instance; } diff --git a/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/hinting_dbs.cpp b/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/hinting_dbs.cpp index bcce6453bcad..71f55503510f 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/hinting_dbs.cpp +++ b/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/hinting_dbs.cpp @@ -264,6 +264,8 @@ SequentialInsertionResult HintingRawDB::insert_indexed_leav AppendOnlyTreeSnapshot state_after = db.get_tree_roots().public_data_tree; SequentialInsertHintPublicDataTreeKey key = { tree_info, world_state::MerkleTreeId::PUBLIC_DATA_TREE, leaf_value }; + BB_ASSERT(!result.low_leaf_witness_data.empty(), "Expected non-empty low_leaf_witness_data after insertion"); + BB_ASSERT(!result.insertion_witness_data.empty(), "Expected non-empty insertion_witness_data after insertion"); SequentialInsertHint sequential_insert_hint = { .hint_key = tree_info, .tree_id = world_state::MerkleTreeId::PUBLIC_DATA_TREE, @@ -286,6 +288,8 @@ SequentialInsertionResult HintingRawDB::insert_indexed_leave auto state_after = db.get_tree_roots().nullifier_tree; SequentialInsertHintNullifierTreeKey key = { tree_info, world_state::MerkleTreeId::NULLIFIER_TREE, leaf_value }; + BB_ASSERT(!result.low_leaf_witness_data.empty(), "Expected non-empty low_leaf_witness_data after insertion"); + BB_ASSERT(!result.insertion_witness_data.empty(), "Expected non-empty insertion_witness_data after insertion"); SequentialInsertHint sequential_insert_hint = { .hint_key = tree_info, .tree_id = world_state::MerkleTreeId::NULLIFIER_TREE, diff --git a/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/indexed_memory_tree.hpp b/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/indexed_memory_tree.hpp index b4b42f3956c2..a82f4dbc33ff 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/indexed_memory_tree.hpp +++ b/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/indexed_memory_tree.hpp @@ -117,6 +117,7 @@ GetLowIndexedLeafResponse IndexedMemoryTree::get_low_in template IndexedLeaf IndexedMemoryTree::get_leaf_preimage(size_t leaf_index) const { + BB_ASSERT_DEBUG(leaf_index < leaves.size(), "Leaf index out of bounds"); return leaves.at(leaf_index); } diff --git a/barretenberg/cpp/src/barretenberg/vm2/simulation/standalone/pure_bytecode_manager.cpp b/barretenberg/cpp/src/barretenberg/vm2/simulation/standalone/pure_bytecode_manager.cpp index c8f8c67f97a1..514145741977 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/simulation/standalone/pure_bytecode_manager.cpp +++ b/barretenberg/cpp/src/barretenberg/vm2/simulation/standalone/pure_bytecode_manager.cpp @@ -133,7 +133,9 @@ Instruction PureTxBytecodeManager::read_instruction(const BytecodeId&, std::shared_ptr> PureTxBytecodeManager::get_bytecode_data(const BytecodeId& bytecode_id) { - return bytecodes.at(bytecode_id); + auto it = bytecodes.find(bytecode_id); + BB_ASSERT_DEBUG(it != bytecodes.end(), "Bytecode not found for the given bytecode_id"); + return it->second; } } // namespace bb::avm2::simulation diff --git a/barretenberg/cpp/src/barretenberg/vm2/tracegen/sha256_trace.cpp b/barretenberg/cpp/src/barretenberg/vm2/tracegen/sha256_trace.cpp index dc3d317c22a1..93227ff96ce6 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/tracegen/sha256_trace.cpp +++ b/barretenberg/cpp/src/barretenberg/vm2/tracegen/sha256_trace.cpp @@ -434,6 +434,7 @@ void Sha256TraceBuilder::process( // If during simulation we encounter an invalid tag, it will have been the last element we retrieved // before we threw an error - so it will be the last element in the input vector. // Therefore, it is just sufficient to check the tag of the last element + BB_ASSERT(!event.input.empty(), "SHA256 input cannot be empty"); bool invalid_tag_err = event.input.back().get_tag() != MemoryTag::U32; // Note that if we encountered an invalid tag error, the row that loaded the invalid tag needs to contain