Skip to content

Commit 323f143

Browse files
authored
Revert "fix: Revert "feat: new per-enqueued-call gas limit" (#9139)"
This reverts commit 7677ca5.
1 parent 6e651de commit 323f143

File tree

15 files changed

+92
-29
lines changed

15 files changed

+92
-29
lines changed

barretenberg/cpp/src/barretenberg/vm/avm/tests/execution.test.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,11 +1760,30 @@ TEST_F(AvmExecutionTests, daGasLeft)
17601760
validate_trace(std::move(trace), public_inputs);
17611761
}
17621762

1763+
TEST_F(AvmExecutionTests, ExecutorThrowsWithTooMuchGasAllocated)
1764+
{
1765+
std::string bytecode_hex = to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16(sender)
1766+
"00" // Indirect flag
1767+
+ to_hex(static_cast<uint8_t>(EnvironmentVariable::SENDER)) + "0007"; // addr 7
1768+
1769+
std::vector<FF> calldata = {};
1770+
std::vector<FF> returndata = {};
1771+
std::vector<FF> public_inputs_vec(PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH, 0);
1772+
public_inputs_vec[L2_START_GAS_LEFT_PCPI_OFFSET] = MAX_L2_GAS_PER_ENQUEUED_CALL + 1;
1773+
1774+
auto bytecode = hex_to_bytes(bytecode_hex);
1775+
auto instructions = Deserialization::parse(bytecode);
1776+
1777+
EXPECT_THROW_WITH_MESSAGE(
1778+
Execution::gen_trace(instructions, returndata, calldata, public_inputs_vec),
1779+
"Cannot allocate more than MAX_L2_GAS_PER_ENQUEUED_CALL to the AVM for execution of an enqueued call");
1780+
}
1781+
17631782
// Should throw whenever the wrong number of public inputs are provided
17641783
TEST_F(AvmExecutionTests, ExecutorThrowsWithIncorrectNumberOfPublicInputs)
17651784
{
1766-
std::string bytecode_hex = to_hex(OpCode::GETENVVAR_16) + // opcode SENDER
1767-
"00" // Indirect flag
1785+
std::string bytecode_hex = to_hex(OpCode::GETENVVAR_16) + // opcode GETENVVAR_16(sender)
1786+
"00" // Indirect flag
17681787
+ to_hex(static_cast<uint8_t>(EnvironmentVariable::SENDER)) + "0007"; // addr 7
17691788

17701789
std::vector<FF> calldata = {};
@@ -1774,7 +1793,7 @@ TEST_F(AvmExecutionTests, ExecutorThrowsWithIncorrectNumberOfPublicInputs)
17741793
auto bytecode = hex_to_bytes(bytecode_hex);
17751794
auto instructions = Deserialization::parse(bytecode);
17761795

1777-
EXPECT_THROW_WITH_MESSAGE(Execution::gen_trace(instructions, calldata, returndata, public_inputs_vec),
1796+
EXPECT_THROW_WITH_MESSAGE(Execution::gen_trace(instructions, returndata, calldata, public_inputs_vec),
17781797
"Public inputs vector is not of PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH");
17791798
}
17801799

barretenberg/cpp/src/barretenberg/vm/avm/trace/helper.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ template <typename FF_> VmPublicInputs<FF_> convert_public_inputs(std::vector<FF
3737
throw_or_abort("Public inputs vector is not of PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH");
3838
}
3939

40+
// WARNING: this must be constrained by the kernel!
41+
// Here this is just a sanity check to prevent generation of proofs that
42+
// will be thrown out by the kernel anyway.
43+
if constexpr (IsAnyOf<FF_, bb::fr>) {
44+
if (public_inputs_vec[L2_START_GAS_LEFT_PCPI_OFFSET] > MAX_L2_GAS_PER_ENQUEUED_CALL) {
45+
throw_or_abort(
46+
"Cannot allocate more than MAX_L2_GAS_PER_ENQUEUED_CALL to the AVM for execution of an enqueued call");
47+
}
48+
} else {
49+
if (public_inputs_vec[L2_START_GAS_LEFT_PCPI_OFFSET].get_value() > MAX_L2_GAS_PER_ENQUEUED_CALL) {
50+
throw_or_abort(
51+
"Cannot allocate more than MAX_L2_GAS_PER_ENQUEUED_CALL to the AVM for execution of an enqueued call");
52+
}
53+
}
54+
4055
std::array<FF_, KERNEL_INPUTS_LENGTH>& kernel_inputs = std::get<KERNEL_INPUTS>(public_inputs);
4156

4257
// Copy items from PublicCircuitPublicInputs vector to public input columns

barretenberg/cpp/src/barretenberg/vm/aztec_constants.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL 16
1313
#define MAX_L1_TO_L2_MSG_READ_REQUESTS_PER_CALL 16
1414
#define MAX_UNENCRYPTED_LOGS_PER_CALL 4
15+
#define MAX_L2_GAS_PER_ENQUEUED_CALL 5000000
1516
#define AZTEC_ADDRESS_LENGTH 1
1617
#define GAS_FEES_LENGTH 2
1718
#define GAS_LENGTH 2

l1-contracts/src/core/libraries/ConstantsGen.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ library Constants {
117117
14061769416655647708490531650437236735160113654556896985372298487345;
118118
uint256 internal constant DEFAULT_GAS_LIMIT = 1000000000;
119119
uint256 internal constant DEFAULT_TEARDOWN_GAS_LIMIT = 100000000;
120+
uint256 internal constant MAX_L2_GAS_PER_ENQUEUED_CALL = 5000000;
120121
uint256 internal constant DEFAULT_MAX_FEE_PER_GAS = 10;
121122
uint256 internal constant DEFAULT_INCLUSION_FEE = 0;
122123
uint256 internal constant DA_BYTES_PER_FIELD = 32;

noir-projects/noir-protocol-circuits/crates/public-kernel-lib/src/components/enqueued_call_data_validator.nr

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use dep::types::{
55
kernel_circuit_public_inputs::PublicKernelCircuitPublicInputs, enqueued_call_data::EnqueuedCallData,
66
public_call_request::PublicCallRequest, validation_requests::PublicValidationRequestArrayLengths
77
},
8-
utils::arrays::array_length
8+
constants::MAX_L2_GAS_PER_ENQUEUED_CALL, utils::arrays::array_length
99
};
1010

1111
pub struct EnqueuedCallDataValidator {
@@ -89,17 +89,23 @@ impl EnqueuedCallDataValidator {
8989
// Validates that the start gas injected into the vm circuit matches the remaining gas.
9090
fn validate_start_gas(self, previous_kernel: PublicKernelCircuitPublicInputs) {
9191
let enqueued_call_start_gas = self.enqueued_call.data.start_gas_left;
92+
// NOTE: the AVM circuit will fail to generate a proof if its "start gas" is > MAX_L2_GAS_PER_ENQUEUED_CALL,
93+
// so the kernel never allocates more than that maximum to one enqueued call.
9294
if self.phase != PublicKernelPhase.TEARDOWN {
9395
// An enqueued call's start gas is the remaining gas left in the transaction after the previous kernel.
9496
let tx_gas_limits = previous_kernel.constants.tx_context.gas_settings.gas_limits;
95-
let computed_start_gas = tx_gas_limits.sub(previous_kernel.end.gas_used).sub(previous_kernel.end_non_revertible.gas_used);
97+
let mut computed_start_gas = tx_gas_limits.sub(previous_kernel.end.gas_used).sub(previous_kernel.end_non_revertible.gas_used);
98+
// Keep L2 gas below max
99+
computed_start_gas.l2_gas = std::cmp::min(computed_start_gas.l2_gas, MAX_L2_GAS_PER_ENQUEUED_CALL);
96100
assert_eq(
97-
enqueued_call_start_gas, computed_start_gas, "Start gas for enqueued call does not match transaction gas left"
101+
enqueued_call_start_gas, computed_start_gas, "Start gas for enqueued call does not match transaction gas left (with MAX_L2_GAS_PER_ENQUEUED_CALL applied)"
98102
);
99103
} else {
100-
let teardown_gas_limit = previous_kernel.constants.tx_context.gas_settings.teardown_gas_limits;
104+
let mut teardown_gas_limit = previous_kernel.constants.tx_context.gas_settings.teardown_gas_limits;
105+
// Keep L2 gas below max
106+
teardown_gas_limit.l2_gas = std::cmp::min(teardown_gas_limit.l2_gas, MAX_L2_GAS_PER_ENQUEUED_CALL);
101107
assert_eq(
102-
enqueued_call_start_gas, teardown_gas_limit, "Start gas for enqueued call does not match teardown gas allocation"
108+
enqueued_call_start_gas, teardown_gas_limit, "Start gas for enqueued call does not match teardown gas allocation (with MAX_L2_GAS_PER_ENQUEUED_CALL applied)"
103109
);
104110
}
105111
}

noir-projects/noir-protocol-circuits/crates/types/src/constants.nr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ global DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE = 0x85864497636cf755ae7bd
168168
// GAS DEFAULTS
169169
global DEFAULT_GAS_LIMIT: u32 = 1_000_000_000;
170170
global DEFAULT_TEARDOWN_GAS_LIMIT: u32 = 100_000_000;
171+
global MAX_L2_GAS_PER_ENQUEUED_CALL: u32 = 5_000_000;
171172
global DEFAULT_MAX_FEE_PER_GAS: Field = 10;
172173
global DEFAULT_INCLUSION_FEE: Field = 0;
173174
global DA_BYTES_PER_FIELD: u32 = 32;

yarn-project/circuits.js/src/constants.gen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export const DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE =
102102
14061769416655647708490531650437236735160113654556896985372298487345n;
103103
export const DEFAULT_GAS_LIMIT = 1000000000;
104104
export const DEFAULT_TEARDOWN_GAS_LIMIT = 100000000;
105+
export const MAX_L2_GAS_PER_ENQUEUED_CALL = 5000000;
105106
export const DEFAULT_MAX_FEE_PER_GAS = 10;
106107
export const DEFAULT_INCLUSION_FEE = 0;
107108
export const DA_BYTES_PER_FIELD = 32;

yarn-project/circuits.js/src/scripts/constants.in.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const CPP_CONSTANTS = [
7878
'MEM_TAG_U64',
7979
'MEM_TAG_U128',
8080
'MEM_TAG_FF',
81+
'MAX_L2_GAS_PER_ENQUEUED_CALL',
8182
];
8283

8384
const CPP_GENERATORS: string[] = [];

yarn-project/circuits.js/src/structs/gas.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { type FieldsOf } from '@aztec/foundation/types';
44

55
import { inspect } from 'util';
66

7+
import { MAX_L2_GAS_PER_ENQUEUED_CALL } from '../constants.gen.js';
78
import { type GasFees } from './gas_fees.js';
89
import { type UInt32 } from './shared.js';
910

@@ -36,7 +37,7 @@ export class Gas {
3637

3738
/** Returns large enough gas amounts for testing purposes. */
3839
static test() {
39-
return new Gas(1e9, 1e9);
40+
return new Gas(1e9, MAX_L2_GAS_PER_ENQUEUED_CALL);
4041
}
4142

4243
isEmpty() {

yarn-project/simulator/src/avm/avm_simulator.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { MAX_L2_GAS_PER_ENQUEUED_CALL } from '@aztec/circuits.js';
12
import { type DebugLogger, createDebugLogger } from '@aztec/foundation/log';
23

34
import { strict as assert } from 'assert';
@@ -21,6 +22,10 @@ export class AvmSimulator {
2122
private bytecode: Buffer | undefined;
2223

2324
constructor(private context: AvmContext) {
25+
assert(
26+
context.machineState.gasLeft.l2Gas <= MAX_L2_GAS_PER_ENQUEUED_CALL,
27+
`Cannot allocate more than ${MAX_L2_GAS_PER_ENQUEUED_CALL} to the AVM for execution of an enqueued call`,
28+
);
2429
this.log = createDebugLogger(`aztec:avm_simulator:core(f:${context.environment.functionSelector.toString()})`);
2530
}
2631

0 commit comments

Comments
 (0)