Skip to content

Commit

Permalink
Merge branch 'master' into lde/fix_wasm_ivc_integration
Browse files Browse the repository at this point in the history
  • Loading branch information
ledwards2225 committed Dec 11, 2024
2 parents 10c5924 + 98ba747 commit e3e4fd8
Show file tree
Hide file tree
Showing 70 changed files with 63,585 additions and 749 deletions.
21 changes: 0 additions & 21 deletions barretenberg/cpp/src/barretenberg/vm/avm/tests/execution.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1739,27 +1739,6 @@ TEST_F(AvmExecutionTests, daGasLeft)
validate_trace(std::move(trace), public_inputs);
}

TEST_F(AvmExecutionTests, ExecutorThrowsWithTooMuchGasAllocated)
{
GTEST_SKIP();
std::string bytecode_hex = to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16(sender)
"00" // Indirect flag
+ "0007" + to_hex(static_cast<uint8_t>(EnvironmentVariable::SENDER)); // addr 7

std::vector<FF> calldata = {};
std::vector<FF> returndata = {};
public_inputs.gas_settings.gas_limits.l2_gas = MAX_L2_GAS_PER_ENQUEUED_CALL;

auto bytecode = hex_to_bytes(bytecode_hex);
auto [instructions, error] = Deserialization::parse_bytecode_statically(bytecode);
ASSERT_TRUE(is_ok(error));

ExecutionHints execution_hints;
EXPECT_THROW_WITH_MESSAGE(gen_trace(bytecode, calldata, public_inputs, returndata, execution_hints),
"Cannot allocate more than MAX_L2_GAS_PER_ENQUEUED_CALL to the AVM for "
"execution of an enqueued call");
}

// Should throw whenever the wrong number of public inputs are provided
// TEST_F(AvmExecutionTests, ExecutorThrowsWithIncorrectNumberOfPublicInputs)
// {
Expand Down
9 changes: 8 additions & 1 deletion barretenberg/cpp/src/barretenberg/vm/avm/trace/execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <cstdint>
#include <cstdlib>
#include <filesystem>
#include <stdexcept>
#include <string>
#include <tuple>
#include <unordered_map>
Expand Down Expand Up @@ -377,10 +378,16 @@ std::vector<Row> Execution::gen_trace(AvmPublicInputs const& public_inputs,

if (!is_ok(phase_error) && phase == TxExecutionPhase::SETUP) {
// Stop processing phases. Halt TX.
info("A revert during SETUP phase halts the entire TX");
info("A revert was encountered in the SETUP phase, killing the entire TX");
throw std::runtime_error("A revert was encountered in the SETUP phase, killing the entire TX");
break;
}
}

if (apply_e2e_assertions) {
trace_builder.pay_fee();
}

auto trace = trace_builder.finalize(apply_e2e_assertions);

returndata = trace_builder.get_all_returndata();
Expand Down
15 changes: 0 additions & 15 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,6 @@ template <typename FF_> VmPublicInputs_<FF_> convert_public_inputs(std::vector<F
throw_or_abort("Public inputs vector is not of PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH");
}

// WARNING: this must be constrained by the kernel!
// Here this is just a sanity check to prevent generation of proofs that
// will be thrown out by the kernel anyway.
if constexpr (IsAnyOf<FF_, bb::fr>) {
if (public_inputs_vec[L2_START_GAS_LEFT_PCPI_OFFSET] > MAX_L2_GAS_PER_ENQUEUED_CALL) {
throw_or_abort(
"Cannot allocate more than MAX_L2_GAS_PER_ENQUEUED_CALL to the AVM for execution of an enqueued call");
}
} else {
if (public_inputs_vec[L2_START_GAS_LEFT_PCPI_OFFSET].get_value() > MAX_L2_GAS_PER_ENQUEUED_CALL) {
throw_or_abort(
"Cannot allocate more than MAX_L2_GAS_PER_ENQUEUED_CALL to the AVM for execution of an enqueued call");
}
}

std::array<FF_, KERNEL_INPUTS_LENGTH>& kernel_inputs = std::get<KERNEL_INPUTS>(public_inputs);

// Copy items from PublicCircuitPublicInputs vector to public input columns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ class AvmPublicInputs {
TreeSnapshots start_tree_snapshots;
Gas start_gas_used;
GasSettings gas_settings;
FF fee_payer;
std::array<PublicCallRequest, MAX_ENQUEUED_CALLS_PER_TX> public_setup_call_requests;
std::array<PublicCallRequest, MAX_ENQUEUED_CALLS_PER_TX> public_app_logic_call_requests;
PublicCallRequest public_teardown_call_request;
Expand All @@ -330,6 +331,7 @@ class AvmPublicInputs {
read(it, public_inputs.start_tree_snapshots);
read(it, public_inputs.start_gas_used);
read(it, public_inputs.gas_settings);
read(it, public_inputs.fee_payer);
read(it, public_inputs.public_setup_call_requests);
read(it, public_inputs.public_app_logic_call_requests);
read(it, public_inputs.public_teardown_call_request);
Expand Down
71 changes: 70 additions & 1 deletion barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <iostream>
#include <limits>
#include <set>
#include <stdexcept>
#include <string>
#include <sys/types.h>
#include <unordered_map>
Expand All @@ -32,6 +33,7 @@
#include "barretenberg/vm/avm/trace/gadgets/cmp.hpp"
#include "barretenberg/vm/avm/trace/gadgets/keccak.hpp"
#include "barretenberg/vm/avm/trace/gadgets/merkle_tree.hpp"
#include "barretenberg/vm/avm/trace/gadgets/poseidon2.hpp"
#include "barretenberg/vm/avm/trace/gadgets/slice_trace.hpp"
#include "barretenberg/vm/avm/trace/helper.hpp"
#include "barretenberg/vm/avm/trace/opcode.hpp"
Expand Down Expand Up @@ -225,6 +227,63 @@ void AvmTraceBuilder::insert_private_state(const std::vector<FF>& siloed_nullifi
}
}

void AvmTraceBuilder::pay_fee()
{
auto clk = static_cast<uint32_t>(main_trace.size()) + 1;

auto tx_fee = (public_inputs.global_variables.gas_fees.fee_per_da_gas * public_inputs.end_gas_used.da_gas) +
(public_inputs.global_variables.gas_fees.fee_per_l2_gas * public_inputs.end_gas_used.l2_gas);

if (public_inputs.fee_payer == 0) {
vinfo("No one is paying the fee of ", tx_fee);
return;
}

// ** Compute the storage slot **
// using the base slot of the balances map and the fee payer address (map key)
// TS equivalent:
// computeFeePayerBalanceStorageSlot(fee_payer);
std::vector<FF> slot_hash_inputs = { FEE_JUICE_BALANCES_SLOT, public_inputs.fee_payer };
const auto balance_slot = poseidon2_trace_builder.poseidon2_hash(slot_hash_inputs, clk, Poseidon2Caller::SILO);

// ** Read the balance before fee payment **
// TS equivalent:
// current_balance = readStorage(FEE_JUICE_ADDRESS, balance_slot);
PublicDataReadTreeHint read_hint = execution_hints.storage_read_hints.at(storage_read_counter++);
FF computed_tree_slot =
merkle_tree_trace_builder.compute_public_tree_leaf_slot(clk, FEE_JUICE_ADDRESS, balance_slot);
// Sanity check that the computed slot using the value read from slot_offset should match the read hint
ASSERT(computed_tree_slot == read_hint.leaf_preimage.slot);

// ** Write the updated balance after fee payment **
// TS equivalent:
// Check that the leaf is a member of the public data tree
bool is_member = merkle_tree_trace_builder.perform_storage_read(
clk, read_hint.leaf_preimage, read_hint.leaf_index, read_hint.sibling_path);
ASSERT(is_member);
FF current_balance = read_hint.leaf_preimage.value;

const auto updated_balance = current_balance - tx_fee;
if (current_balance < tx_fee) {
info("Not enough balance for fee payer to pay for transaction (got ", current_balance, " needs ", tx_fee);
throw std::runtime_error("Not enough balance for fee payer to pay for transaction");
}

// writeStorage(FEE_JUICE_ADDRESS, balance_slot, updated_balance);
PublicDataWriteTreeHint write_hint = execution_hints.storage_write_hints.at(storage_write_counter++);
ASSERT(write_hint.new_leaf_preimage.value == updated_balance);
merkle_tree_trace_builder.perform_storage_write(clk,
write_hint.low_leaf_membership.leaf_preimage,
write_hint.low_leaf_membership.leaf_index,
write_hint.low_leaf_membership.sibling_path,
write_hint.new_leaf_preimage.slot,
write_hint.new_leaf_preimage.value,
write_hint.insertion_path);

debug("pay fee side-effect cnt: ", side_effect_counter);
side_effect_counter++;
}

/**
* @brief Loads a value from memory into a given intermediate register at a specified clock cycle.
* Handles both direct and indirect memory access.
Expand Down Expand Up @@ -401,9 +460,16 @@ AvmTraceBuilder::AvmTraceBuilder(AvmPublicInputs public_inputs,
, bytecode_trace_builder(execution_hints.all_contract_bytecode)
, merkle_tree_trace_builder(public_inputs.start_tree_snapshots)
{
// Only allocate up to the maximum L2 gas for execution
// TODO: constrain this!
auto const l2_gas_left_after_private =
public_inputs.gas_settings.gas_limits.l2_gas - public_inputs.start_gas_used.l2_gas;
// TODO: think about cast
auto const allocated_l2_gas =
std::min(l2_gas_left_after_private, static_cast<uint32_t>(MAX_L2_GAS_PER_TX_PUBLIC_PORTION));
// TODO: think about cast
gas_trace_builder.set_initial_gas(
static_cast<uint32_t>(public_inputs.gas_settings.gas_limits.l2_gas - public_inputs.start_gas_used.l2_gas),
static_cast<uint32_t>(allocated_l2_gas),
static_cast<uint32_t>(public_inputs.gas_settings.gas_limits.da_gas - public_inputs.start_gas_used.da_gas));
}

Expand Down Expand Up @@ -2528,6 +2594,9 @@ AvmError AvmTraceBuilder::op_sstore(uint8_t indirect, uint32_t src_offset, uint3
auto clk = static_cast<uint32_t>(main_trace.size()) + 1;

if (storage_write_counter >= MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX) {
// NOTE: the circuit constraint for this limit should only be applied
// for the storage writes performed by this opcode. An exception should before
// made for the fee juice storage write made after teardown.
error = AvmError::SIDE_EFFECT_LIMIT_REACHED;
auto row = Row{
.main_clk = clk,
Expand Down
1 change: 1 addition & 0 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ class AvmTraceBuilder {
std::vector<uint8_t> get_bytecode(const FF contract_address, bool check_membership = false);
std::unordered_set<FF> bytecode_membership_cache;
void insert_private_state(const std::vector<FF>& siloed_nullifiers, const std::vector<FF>& siloed_note_hashes);
void pay_fee();

// These are used for testing only.
AvmTraceBuilder& set_range_check_required(bool required)
Expand Down
7 changes: 4 additions & 3 deletions barretenberg/cpp/src/barretenberg/vm/aztec_constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
#define MAX_L2_TO_L1_MSGS_PER_TX 8
#define MAX_UNENCRYPTED_LOGS_PER_TX 8
#define MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS 3000
#define MAX_L2_GAS_PER_ENQUEUED_CALL 12000000
#define MAX_L2_GAS_PER_TX_PUBLIC_PORTION 12000000
#define CANONICAL_AUTH_REGISTRY_ADDRESS 1
#define DEPLOYER_CONTRACT_ADDRESS 2
#define REGISTERER_CONTRACT_ADDRESS 3
#define MULTI_CALL_ENTRYPOINT_ADDRESS 4
#define FEE_JUICE_ADDRESS 5
#define ROUTER_ADDRESS 6
#define FEE_JUICE_BALANCES_SLOT 1
#define AZTEC_ADDRESS_LENGTH 1
#define GAS_FEES_LENGTH 2
#define GAS_LENGTH 2
Expand All @@ -45,8 +46,8 @@
#define STATE_REFERENCE_LENGTH 8
#define TOTAL_FEES_LENGTH 1
#define PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH 867
#define AVM_ACCUMULATED_DATA_LENGTH 318
#define AVM_CIRCUIT_PUBLIC_INPUTS_LENGTH 1006
#define AVM_ACCUMULATED_DATA_LENGTH 320
#define AVM_CIRCUIT_PUBLIC_INPUTS_LENGTH 1008
#define AVM_VERIFICATION_KEY_LENGTH_IN_FIELDS 86
#define AVM_PROOF_LENGTH_IN_FIELDS 4155
#define AVM_PUBLIC_COLUMN_MAX_SIZE 1024
Expand Down
4 changes: 4 additions & 0 deletions docs/docs/migration_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Further changes are planned, so that:

## 0.66

### L2 Gas limit of 12M enforced for public portion of TX

This limit was previously enforced per-enqueued-public-call. The protocol now enforces a stricter limit that the entire public portion of a transaction consumes at most 12,000,000 L2 gas.

### DEBUG env var is removed

The `DEBUG` variable is no longer used. Use `LOG_LEVEL` with one of `silent`, `fatal`, `error`, `warn`, `info`, `verbose`, `debug`, or `trace`. To tweak log levels per module, add a list of module prefixes with their overridden level. For example, LOG_LEVEL="info; verbose: aztec:sequencer, aztec:archiver; debug: aztec:kv-store" sets `info` as the default log level, `verbose` for the sequencer and archiver, and `debug` for the kv-store. Module name match is done by prefix.
Expand Down
9 changes: 5 additions & 4 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ library Constants {
14061769416655647708490531650437236735160113654556896985372298487345;
uint256 internal constant DEFAULT_GAS_LIMIT = 1000000000;
uint256 internal constant DEFAULT_TEARDOWN_GAS_LIMIT = 12000000;
uint256 internal constant MAX_L2_GAS_PER_ENQUEUED_CALL = 12000000;
uint256 internal constant MAX_L2_GAS_PER_TX_PUBLIC_PORTION = 12000000;
uint256 internal constant DA_BYTES_PER_FIELD = 32;
uint256 internal constant DA_GAS_PER_BYTE = 16;
uint256 internal constant FIXED_DA_GAS = 512;
uint256 internal constant FIXED_L2_GAS = 512;
uint256 internal constant FIXED_AVM_STARTUP_L2_GAS = 1024;
uint256 internal constant FIXED_AVM_STARTUP_L2_GAS = 20000;
uint256 internal constant L2_GAS_DISTRIBUTED_STORAGE_PREMIUM = 1024;
uint256 internal constant L2_GAS_PER_READ_MERKLE_HASH = 30;
uint256 internal constant L2_GAS_PER_WRITE_MERKLE_HASH = 40;
Expand All @@ -138,6 +138,7 @@ library Constants {
uint256 internal constant MULTI_CALL_ENTRYPOINT_ADDRESS = 4;
uint256 internal constant FEE_JUICE_ADDRESS = 5;
uint256 internal constant ROUTER_ADDRESS = 6;
uint256 internal constant FEE_JUICE_BALANCES_SLOT = 1;
uint256 internal constant DEFAULT_NPK_M_X =
582240093077765400562621227108555700500271598878376310175765873770292988861;
uint256 internal constant DEFAULT_NPK_M_Y =
Expand Down Expand Up @@ -208,7 +209,7 @@ library Constants {
uint256 internal constant SCOPED_READ_REQUEST_LEN = 3;
uint256 internal constant PUBLIC_DATA_READ_LENGTH = 3;
uint256 internal constant PRIVATE_VALIDATION_REQUESTS_LENGTH = 772;
uint256 internal constant COMBINED_ACCUMULATED_DATA_LENGTH = 900;
uint256 internal constant COMBINED_ACCUMULATED_DATA_LENGTH = 902;
uint256 internal constant TX_CONSTANT_DATA_LENGTH = 35;
uint256 internal constant COMBINED_CONSTANT_DATA_LENGTH = 44;
uint256 internal constant PRIVATE_ACCUMULATED_DATA_LENGTH = 1412;
Expand All @@ -217,7 +218,7 @@ library Constants {
uint256 internal constant PRIVATE_TO_AVM_ACCUMULATED_DATA_LENGTH = 160;
uint256 internal constant NUM_PRIVATE_TO_AVM_ACCUMULATED_DATA_ARRAYS = 3;
uint256 internal constant PRIVATE_TO_PUBLIC_KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 1845;
uint256 internal constant KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 956;
uint256 internal constant KERNEL_CIRCUIT_PUBLIC_INPUTS_LENGTH = 958;
uint256 internal constant CONSTANT_ROLLUP_DATA_LENGTH = 13;
uint256 internal constant BASE_OR_MERGE_PUBLIC_INPUTS_LENGTH = 31;
uint256 internal constant BLOCK_ROOT_OR_BLOCK_MERGE_PUBLIC_INPUTS_LENGTH = 90;
Expand Down
Loading

0 comments on commit e3e4fd8

Please sign in to comment.