Skip to content

Commit e7eab21

Browse files
authored
fix!: Tx overhead and private emission L2 gas based on simulation time (#20648)
This PR: - Splits AVM_MAX_PROCESSABLE_GAS (6M) from MAX_PROCESSABLE_GAS (6M+PUBLIC_TX_OVERHEAD) - Splits Public tx overhead and private tx overhead - Plugs in numbers from simulation metrics to tx overheads - Plugs in numbers from simulation metrics to side effects - TODO: Figure out contract class logs: we are having trouble measuring them.
2 parents b2b258d + bf1232b commit e7eab21

File tree

28 files changed

+172
-109
lines changed

28 files changed

+172
-109
lines changed

barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzz_lib/constants.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const std::vector<ScopedL2ToL1Message> REVERTIBLE_ACCUMULATED_DATA_L2_TO_L1_MESS
3232
const std::vector<PublicCallRequestWithCalldata> SETUP_ENQUEUED_CALLS = {};
3333
const FF MSG_SENDER = 100;
3434
const std::optional<PublicCallRequestWithCalldata> TEARDOWN_ENQUEUED_CALLS = std::nullopt;
35-
const Gas GAS_USED_BY_PRIVATE = Gas{ .l2_gas = 0, .da_gas = 0 };
35+
const Gas GAS_USED_BY_PRIVATE = Gas{ .l2_gas = PUBLIC_TX_L2_GAS_OVERHEAD, .da_gas = TX_DA_GAS_OVERHEAD };
3636
const AztecAddress FEE_PAYER = AztecAddress{ 0 };
3737
const FF CONTRACT_ADDRESS = 42;
3838
const FF TRANSACTION_FEE = 0;

barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/tx_data.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void mutate_tx(Tx& tx, std::vector<AztecAddress>& contract_addresses, std::mt199
112112
case TxMutationOptions::GasUsedByPrivate:
113113
// Mutate gas_used_by_private
114114
fuzz_info("Mutating gas used by private");
115-
mutate_gas(tx.gas_used_by_private, rng, tx.gas_settings.gas_limits);
115+
mutate_gas(tx.gas_used_by_private, rng, GAS_USED_BY_PRIVATE, tx.gas_settings.gas_limits);
116116
break;
117117
case TxMutationOptions::FeePayer:
118118
// Mutate fee_payer

barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/tx_types/gas.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,26 @@ uint128_t generate_u128(std::mt19937_64& rng, uint128_t min = 0, uint128_t max =
2828

2929
namespace bb::avm2::fuzzer {
3030

31-
Gas generate_gas(std::mt19937_64& rng)
31+
Gas generate_gas(std::mt19937_64& rng, const Gas& min, const Gas& max)
3232
{
33-
uint32_t l2_gas = std::uniform_int_distribution<uint32_t>(MIN_GAS, AVM_MAX_PROCESSABLE_L2_GAS)(rng);
34-
uint32_t da_gas = std::uniform_int_distribution<uint32_t>(MIN_GAS, AVM_MAX_PROCESSABLE_DA_GAS)(rng);
33+
uint32_t l2_gas = std::uniform_int_distribution<uint32_t>(min.l2_gas, max.l2_gas)(rng);
34+
uint32_t da_gas = std::uniform_int_distribution<uint32_t>(min.da_gas, max.da_gas)(rng);
3535

3636
return Gas{ l2_gas, da_gas };
3737
}
3838

39-
void mutate_gas(Gas& gas, std::mt19937_64& rng, const Gas& max)
39+
void mutate_gas(Gas& gas, std::mt19937_64& rng, const Gas& min, const Gas& max)
4040
{
4141
auto choice = std::uniform_int_distribution<uint8_t>(0, 1)(rng);
4242

4343
switch (choice) {
4444
case 0:
4545
// Mutate l2_gas
46-
gas.l2_gas = std::uniform_int_distribution<uint32_t>(MIN_GAS, max.l2_gas)(rng);
46+
gas.l2_gas = std::uniform_int_distribution<uint32_t>(min.l2_gas, max.l2_gas)(rng);
4747
break;
4848
case 1:
4949
// Mutate da_gas
50-
gas.da_gas = std::uniform_int_distribution<uint32_t>(MIN_GAS, max.da_gas)(rng);
50+
gas.da_gas = std::uniform_int_distribution<uint32_t>(min.da_gas, max.da_gas)(rng);
5151
break;
5252
}
5353
}

barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/tx_types/gas.hpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ constexpr uint128_t MIN_FEE = 1;
1515
constexpr uint128_t MAX_FEE = 1000;
1616
//
1717
// Gas bounds for mutation
18-
constexpr uint32_t MIN_GAS = 0;
19-
constexpr uint32_t AVM_MAX_PROCESSABLE_DA_GAS = (MAX_NOTE_HASHES_PER_TX * AVM_EMITNOTEHASH_BASE_DA_GAS) +
20-
(MAX_NULLIFIERS_PER_TX * AVM_EMITNULLIFIER_BASE_DA_GAS) +
21-
(MAX_L2_TO_L1_MSGS_PER_TX * AVM_SENDL2TOL1MSG_BASE_DA_GAS) +
22-
(MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX * AVM_SSTORE_DYN_DA_GAS) +
23-
(PUBLIC_LOGS_LENGTH * AVM_EMITPUBLICLOG_BASE_DA_GAS);
18+
constexpr uint32_t MAX_PROCESSABLE_DA_GAS = TX_DA_GAS_OVERHEAD +
19+
(MAX_NOTE_HASHES_PER_TX * AVM_EMITNOTEHASH_BASE_DA_GAS) +
20+
(MAX_NULLIFIERS_PER_TX * AVM_EMITNULLIFIER_BASE_DA_GAS) +
21+
(MAX_L2_TO_L1_MSGS_PER_TX * AVM_SENDL2TOL1MSG_BASE_DA_GAS) +
22+
(MAX_TOTAL_PUBLIC_DATA_UPDATE_REQUESTS_PER_TX * AVM_SSTORE_DYN_DA_GAS) +
23+
(PUBLIC_LOGS_LENGTH * AVM_EMITPUBLICLOG_BASE_DA_GAS);
24+
constexpr Gas MAX_GAS_LIMIT = Gas{ .l2_gas = MAX_PROCESSABLE_L2_GAS, .da_gas = MAX_PROCESSABLE_DA_GAS };
2425

2526
enum class GasSettingsMutationOptions : uint8_t {
2627
GasLimits,
@@ -38,10 +39,8 @@ constexpr GasSettingsMutationConfig GAS_SETTINGS_MUTATION_CONFIGURATION = GasSet
3839
{ GasSettingsMutationOptions::MaxPriorityFeesPerGas, 5 },
3940
});
4041

41-
Gas generate_gas(std::mt19937_64& rng);
42-
void mutate_gas(Gas& gas,
43-
std::mt19937_64& rng,
44-
const Gas& max = Gas{ AVM_MAX_PROCESSABLE_L2_GAS, AVM_MAX_PROCESSABLE_DA_GAS });
42+
Gas generate_gas(std::mt19937_64& rng, const Gas& min = {}, const Gas& max = MAX_GAS_LIMIT);
43+
void mutate_gas(Gas& gas, std::mt19937_64& rng, const Gas& min = {}, const Gas& max = MAX_GAS_LIMIT);
4544

4645
GasSettings generate_gas_settings(std::mt19937_64& rng);
4746
void mutate_gas_settings(GasSettings& data, std::mt19937_64& rng);

barretenberg/cpp/src/barretenberg/vm2/common/aztec_constants.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@
179179
#define AVM_PUBLIC_INPUTS_COLUMNS_COMBINED_LENGTH 18740
180180
#define AVM_V2_PROOF_LENGTH_IN_FIELDS_PADDED 16400
181181
#define AVM_V2_VERIFICATION_KEY_LENGTH_IN_FIELDS_PADDED 1000
182+
#define TX_DA_GAS_OVERHEAD 96
183+
#define PUBLIC_TX_L2_GAS_OVERHEAD 540000
182184
#define AVM_MAX_PROCESSABLE_L2_GAS 6000000
185+
#define MAX_PROCESSABLE_L2_GAS 6540000
183186
#define AVM_PC_SIZE_IN_BITS 32
184187
#define AVM_MAX_OPERANDS 7
185188
#define AVM_MAX_REGISTERS 6
@@ -221,7 +224,7 @@
221224
#define AVM_L1TOL2MSGEXISTS_BASE_L2_GAS 540
222225
#define AVM_GETCONTRACTINSTANCE_BASE_L2_GAS 6108
223226
#define AVM_EMITPUBLICLOG_BASE_L2_GAS 15
224-
#define AVM_SENDL2TOL1MSG_BASE_L2_GAS 478
227+
#define AVM_SENDL2TOL1MSG_BASE_L2_GAS 5239
225228
#define AVM_CALL_BASE_L2_GAS 9936
226229
#define AVM_STATICCALL_BASE_L2_GAS 9936
227230
#define AVM_RETURN_BASE_L2_GAS 9

barretenberg/cpp/src/barretenberg/vm2/constraining/avm_fixed_vk.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class AvmHardCodedVKAndHash {
1717
using FF = bb::curve::BN254::ScalarField;
1818

1919
// Precomputed VK hash (hash of all commitments below).
20-
static FF vk_hash() { return FF(uint256_t("0x02296b934ced1a5cdacae120d2032d88a119bdb0738d4c4f3ada4f5a831a5153")); }
20+
static FF vk_hash() { return FF(uint256_t("0x18952f5711d5f7a6e29c980f1077eecd2ec45e80ba0ae78d5a4ae08e50428cab")); }
2121

2222
static constexpr std::array<Commitment, NUM_PRECOMPUTED_ENTITIES> get_all()
2323
{
@@ -71,9 +71,9 @@ class AvmHardCodedVKAndHash {
7171
uint256_t(
7272
"0x090dda25e7d64ab5cabe09fd80fbb731af2a98de7a608157dc10394b4fc022a4")), // precomputed_exec_opcode_dynamic_l2_gas
7373
Commitment(
74-
uint256_t("0x2216a1693dcb1cc83f57ea8058f681d71bdf0e6cfc839502cf16fb0a88a5f673"),
74+
uint256_t("0x26086b5fb31a24f236f0441d5b922b94ca141e861b9cc640184681c518cd68d3"),
7575
uint256_t(
76-
"0x255e6760ed9adda61aca7d0b7d4bb28bb62e3cca6e860009461a9a1708184be2")), // precomputed_exec_opcode_opcode_gas
76+
"0x0bab134bb4e25ff33584c1094847e762ce6573054bae27715d0e4eb2b7278d80")), // precomputed_exec_opcode_opcode_gas
7777
Commitment(
7878
uint256_t("0x296def9415d1c96b4d8ab91df5f59ad8522a726f98461b1ab5c4d4c5b22471a4"),
7979
uint256_t(
Binary file not shown.
Binary file not shown.

noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/components/gas_meter.nr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,11 @@ pub fn meter_gas_used(public_inputs: PrivateKernelCircuitPublicInputs, is_for_pu
8989

9090
let metered_da_gas = metered_da_fields * DA_GAS_PER_FIELD;
9191

92-
Gas::tx_overhead() + Gas::new(metered_da_gas, metered_l2_gas) + teardown_gas
92+
let overhead = if is_for_public {
93+
Gas::public_tx_overhead()
94+
} else {
95+
Gas::private_tx_overhead()
96+
};
97+
98+
overhead + Gas::new(metered_da_gas, metered_l2_gas) + teardown_gas
9399
}

noir-projects/noir-protocol-circuits/crates/private-kernel-lib/src/tests/private_kernel_tail/meter_gas_used_tests.nr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use types::{
1212
/// A minimum (private) tx initialized in the TestBuilder contains a protocol nullifier, which must exist in every tx.
1313
fn get_minimum_private_tx_gas_used() -> Gas {
1414
let nullifier_gas_used = Gas { da_gas: DA_GAS_PER_FIELD, l2_gas: L2_GAS_PER_NULLIFIER };
15-
Gas::tx_overhead() + nullifier_gas_used
15+
Gas::private_tx_overhead() + nullifier_gas_used
1616
}
1717

1818
#[test]
@@ -100,7 +100,7 @@ fn full_side_effects() {
100100
let mut builder = TestBuilder::new();
101101

102102
// Fill the tx with side effects and compute the expected gas used.
103-
let mut expected_gas_used = Gas::tx_overhead();
103+
let mut expected_gas_used = Gas::private_tx_overhead();
104104
// Note hashes.
105105
builder.previous_kernel.append_siloed_note_hashes(MAX_NOTE_HASHES_PER_TX);
106106
expected_gas_used += Gas {

0 commit comments

Comments
 (0)