Skip to content

Commit 891d46b

Browse files
authored
chore!: opcode gas multipliers to account for slow simulation opcodes (#19130)
After these multipliers, all opcode-spam tests take 300ms or less (1 cpu, 8g ram). I chose the smallest whole-number multipliers to get us there. | Opcode | Multiplier | Original Cost | New Cost | Notes | |--------|------------|---------------|------------|-------| | FDIV | 25x | 9 | 225 | | | SLOAD | 10x | 129 | 1,290 | | | SSTORE | 20x | 1,657 | 33,140 | | | NOTEHASHEXISTS | 4x | 126 | 504 | +1x for slow proving | | EMITNOTEHASH | 15x | 1,285 | 19,275 | | | NULLIFIEREXISTS | 7x | 132 | 924 | +1x for slow proving | | EMITNULLIFIER | 20x | 1,540 | 30,800 | | | L1TOL2MSGEXISTS | 5x | 108 | 540 | +1x for slow proving | | SENDL2TOL1MSG | 2x | 209 | 418 | | | CALL | 3x | 3,312 | 9,936 | | | STATICCALL | 3x | 3,312 | 9,936 | | | GETCONTRACTINSTANCE | 4x | 1,527 | 6,108 | Slow simulation | | POSEIDON2 | 15x | 24 | 360 | | | ECADD | 10x | 27 | 270 | | ## Discussion For certain exceptionally slow-to-simulate opcodes, we are multiplying their *L2 Gas* by a factor to normalize them relative to others. This brings mana/s and worst-case simulation (when spamming an opcode) of these outlier opcodes back near the others. More concretely, we measured simulation time when meaningfully spamming each opcode as many times as possible in a transaction. For such transactions, these multipliers bring worst-case simulation down under 300ms/tx. For context, non-outlier opcodes generally range from 150-250ms/tx (for txs that infinitely spam the opcode). *Note: this is only for L2 gas and is completely separate from DA gas.* We have plans to make merkle/db optimizations in the future that might let us reduce some of these multipliers, but that will be substantial work that will not be complete for Alpha. ## Other changes Gas cost changes cause AVM VK changes along with inner and outer recursive verifier changes ## Follow-up work: 1. Commit the scripts used to measure these with consistent resources
1 parent dd42d7e commit 891d46b

File tree

7 files changed

+78
-53
lines changed

7 files changed

+78
-53
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@
188188
#define AVM_SUB_BASE_L2_GAS 12
189189
#define AVM_MUL_BASE_L2_GAS 27
190190
#define AVM_DIV_BASE_L2_GAS 27
191-
#define AVM_FDIV_BASE_L2_GAS 9
191+
#define AVM_FDIV_BASE_L2_GAS 225
192192
#define AVM_EQ_BASE_L2_GAS 12
193193
#define AVM_LT_BASE_L2_GAS 42
194194
#define AVM_LTE_BASE_L2_GAS 42
@@ -210,25 +210,25 @@
210210
#define AVM_INTERNALRETURN_BASE_L2_GAS 9
211211
#define AVM_SET_BASE_L2_GAS 27
212212
#define AVM_MOV_BASE_L2_GAS 12
213-
#define AVM_SLOAD_BASE_L2_GAS 129
214-
#define AVM_SSTORE_BASE_L2_GAS 1657
215-
#define AVM_NOTEHASHEXISTS_BASE_L2_GAS 126
216-
#define AVM_EMITNOTEHASH_BASE_L2_GAS 1285
217-
#define AVM_NULLIFIEREXISTS_BASE_L2_GAS 132
218-
#define AVM_EMITNULLIFIER_BASE_L2_GAS 1540
219-
#define AVM_L1TOL2MSGEXISTS_BASE_L2_GAS 108
220-
#define AVM_GETCONTRACTINSTANCE_BASE_L2_GAS 1527
213+
#define AVM_SLOAD_BASE_L2_GAS 1290
214+
#define AVM_SSTORE_BASE_L2_GAS 33140
215+
#define AVM_NOTEHASHEXISTS_BASE_L2_GAS 504
216+
#define AVM_EMITNOTEHASH_BASE_L2_GAS 19275
217+
#define AVM_NULLIFIEREXISTS_BASE_L2_GAS 924
218+
#define AVM_EMITNULLIFIER_BASE_L2_GAS 30800
219+
#define AVM_L1TOL2MSGEXISTS_BASE_L2_GAS 540
220+
#define AVM_GETCONTRACTINSTANCE_BASE_L2_GAS 6108
221221
#define AVM_EMITUNENCRYPTEDLOG_BASE_L2_GAS 15
222-
#define AVM_SENDL2TOL1MSG_BASE_L2_GAS 209
223-
#define AVM_CALL_BASE_L2_GAS 3312
224-
#define AVM_STATICCALL_BASE_L2_GAS 3312
222+
#define AVM_SENDL2TOL1MSG_BASE_L2_GAS 418
223+
#define AVM_CALL_BASE_L2_GAS 9936
224+
#define AVM_STATICCALL_BASE_L2_GAS 9936
225225
#define AVM_RETURN_BASE_L2_GAS 9
226226
#define AVM_REVERT_BASE_L2_GAS 9
227227
#define AVM_DEBUGLOG_BASE_L2_GAS 9
228-
#define AVM_POSEIDON2_BASE_L2_GAS 24
228+
#define AVM_POSEIDON2_BASE_L2_GAS 360
229229
#define AVM_SHA256COMPRESSION_BASE_L2_GAS 12288
230230
#define AVM_KECCAKF1600_BASE_L2_GAS 58176
231-
#define AVM_ECADD_BASE_L2_GAS 27
231+
#define AVM_ECADD_BASE_L2_GAS 270
232232
#define AVM_TORADIXBE_BASE_L2_GAS 24
233233
#define AVM_CALLDATACOPY_DYN_L2_GAS 3
234234
#define AVM_RETURNDATACOPY_DYN_L2_GAS 3

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ class AvmFixedVKCommitments {
6666
uint256_t(
6767
"0x090dda25e7d64ab5cabe09fd80fbb731af2a98de7a608157dc10394b4fc022a4")), // precomputed_exec_opcode_dynamic_l2_gas
6868
Commitment(
69-
uint256_t("0x1de2201730f35721687604adf0035c8213a4b19caf17756063b9e516a0904cca"),
69+
uint256_t("0x1139e8df5eabbe4201a0d73a0cfa7e12833955b6e190ec05fcdc0002d2756736"),
7070
uint256_t(
71-
"0x1efb2dc65f3d27cdde93c07d042a0da3050355239d3e2d7908797b1780c10f40")), // precomputed_exec_opcode_opcode_gas
71+
"0x1ed8930b01998a1ba6c2df226f9e9a2aa93606228ed6d74b568c93cbdd6fb77f")), // precomputed_exec_opcode_opcode_gas
7272
Commitment(
7373
uint256_t("0x296def9415d1c96b4d8ab91df5f59ad8522a726f98461b1ab5c4d4c5b22471a4"),
7474
uint256_t(

barretenberg/cpp/src/barretenberg/vm2/dsl/avm2_recursion_constraint.test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ TEST_F(AvmRecursionConstraintTest, GateCountAndVKCheck)
136136
using ProverInstance = ProverInstance_<UltraRollupFlavor>;
137137

138138
static constexpr FF EXPECTED_OUTER_VK_HASH =
139-
FF("0x077847493e9e67cc6ceb143e292d635ab7cc2b0652a00b073cdef17240c4a4dd");
139+
FF("0x2db814b00aa8881bf5701dc7f203e4c7c7c976cfff151d8bfe477779c75bd96c");
140140

141141
AcirConstraint constraint;
142142
WitnessVector witness;
@@ -167,7 +167,7 @@ class AvmRecursionInnerCircuitTests : public ::testing::Test {
167167
using FF = Builder::FF;
168168

169169
static constexpr FF EXPECTED_INNER_VK_HASH =
170-
FF("0x0badaf8fd586e28bcb8c724ae6b79e97da2dd583f37496cb68076b0d5db5b2e9");
170+
FF("0x2370e99c105d4dd689b626c376de4ee5f257ed99163094d57720159ac1e96881");
171171

172172
static void SetUpTestSuite() { bb::srs::init_file_crs_factory(bb::srs::bb_crs_path()); }
173173

docs/docs-developers/docs/resources/migration_notes.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@ Aztec is in full-speed development. Literally every version breaks compatibility
99

1010
## TBD
1111

12+
### [AVM] Gas cost multipliers for public execution to reach simulation/proving parity
13+
14+
Gas costs for several AVM opcodes have been adjusted with multipliers to better align public simulation costs with actual proving costs.
15+
16+
| Opcode | Multiplier | Previous Cost | New Cost |
17+
|--------|------------|---------------|----------|
18+
| FDIV | 25x | 9 | 225 |
19+
| SLOAD | 10x | 129 | 1,290 |
20+
| SSTORE | 20x | 1,657 | 33,140 |
21+
| NOTEHASHEXISTS | 4x | 126 | 504 |
22+
| EMITNOTEHASH | 15x | 1,285 | 19,275 |
23+
| NULLIFIEREXISTS | 7x | 132 | 924 |
24+
| EMITNULLIFIER | 20x | 1,540 | 30,800 |
25+
| L1TOL2MSGEXISTS | 5x | 108 | 540 |
26+
| SENDL2TOL1MSG | 2x | 209 | 418 |
27+
| CALL | 3x | 3,312 | 9,936 |
28+
| STATICCALL | 3x | 3,312 | 9,936 |
29+
| GETCONTRACTINSTANCE | 4x | 1,527 | 6,108 |
30+
| POSEIDON2 | 15x | 24 | 360 |
31+
| ECADD | 10x | 27 | 270 |
32+
33+
**Impact**: Contracts with public bytecode performing any of these operations will see increased gas consumption.
34+
1235
### [PXE] deprecated `getNotes`
1336

1437
This function serves only for debugging purposes so we are taking it out of the main PXE API. If you still need to consume it, you can

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,11 +1096,13 @@ pub global L2_GAS_PER_PRIVATE_LOG: u32 = 0;
10961096
pub global L2_GAS_PER_CONTRACT_CLASS_LOG: u32 = 0; // TODO: this should be nonzero, because the sequencer is doing work to hash this, as part of tx validation
10971097
// Note: magic numbers here are derived from each op's AVM circuit trace area
10981098
// https://docs.google.com/spreadsheets/d/1FPyLfJFPOfZTmZC-T6b_4yB5dSrOAofz3dufte23_TA/edit?usp=sharing
1099+
// Some have a "SLOW_SIM_MUL" multiplier because they are slower to simulate and their trace-area-derived gas cost
1100+
// is not sufficient to cover the simulation effort.
10991101
pub global AVM_ADD_BASE_L2_GAS: u32 = 12;
11001102
pub global AVM_SUB_BASE_L2_GAS: u32 = 12;
11011103
pub global AVM_MUL_BASE_L2_GAS: u32 = 27;
11021104
pub global AVM_DIV_BASE_L2_GAS: u32 = 27;
1103-
pub global AVM_FDIV_BASE_L2_GAS: u32 = 9;
1105+
pub global AVM_FDIV_BASE_L2_GAS: u32 = 9 * 25; // SLOW_SIM_MUL = 25
11041106
pub global AVM_EQ_BASE_L2_GAS: u32 = 12;
11051107
pub global AVM_LT_BASE_L2_GAS: u32 = 42;
11061108
pub global AVM_LTE_BASE_L2_GAS: u32 = 42;
@@ -1122,26 +1124,26 @@ pub global AVM_INTERNALCALL_BASE_L2_GAS: u32 = 9;
11221124
pub global AVM_INTERNALRETURN_BASE_L2_GAS: u32 = 9;
11231125
pub global AVM_SET_BASE_L2_GAS: u32 = 27;
11241126
pub global AVM_MOV_BASE_L2_GAS: u32 = 12;
1125-
pub global AVM_SLOAD_BASE_L2_GAS: u32 = 129;
1126-
pub global AVM_SSTORE_BASE_L2_GAS: u32 = 633 + L2_GAS_DISTRIBUTED_STORAGE_PREMIUM;
1127-
pub global AVM_NOTEHASHEXISTS_BASE_L2_GAS: u32 = 126;
1128-
pub global AVM_EMITNOTEHASH_BASE_L2_GAS: u32 = 261 + L2_GAS_DISTRIBUTED_STORAGE_PREMIUM;
1129-
pub global AVM_NULLIFIEREXISTS_BASE_L2_GAS: u32 = 132;
1130-
pub global AVM_EMITNULLIFIER_BASE_L2_GAS: u32 = 516 + L2_GAS_DISTRIBUTED_STORAGE_PREMIUM;
1131-
pub global AVM_L1TOL2MSGEXISTS_BASE_L2_GAS: u32 = 108;
1132-
pub global AVM_GETCONTRACTINSTANCE_BASE_L2_GAS: u32 = 1527; // does scalar mul
1127+
pub global AVM_SLOAD_BASE_L2_GAS: u32 = 129 * 10; // SLOW_SIM_MUL = 10
1128+
pub global AVM_SSTORE_BASE_L2_GAS: u32 = (633 + L2_GAS_DISTRIBUTED_STORAGE_PREMIUM) * 20; // SLOW_SIM_MUL = 20
1129+
pub global AVM_NOTEHASHEXISTS_BASE_L2_GAS: u32 = 126 * 4; // SLOW_SIM_MUL = 3 (+1 for slow proving)
1130+
pub global AVM_EMITNOTEHASH_BASE_L2_GAS: u32 = (261 + L2_GAS_DISTRIBUTED_STORAGE_PREMIUM) * 15; // SLOW_SIM_MUL = 15
1131+
pub global AVM_NULLIFIEREXISTS_BASE_L2_GAS: u32 = 132 * 7; // SLOW_SIM_MUL = 6 (+1 for slow proving)
1132+
pub global AVM_EMITNULLIFIER_BASE_L2_GAS: u32 = (516 + L2_GAS_DISTRIBUTED_STORAGE_PREMIUM) * 20; // SLOW_SIM_MUL = 20
1133+
pub global AVM_L1TOL2MSGEXISTS_BASE_L2_GAS: u32 = 108 * 5; // SLOW_SIM_MUL = 4 (+1 for slow proving)
1134+
pub global AVM_GETCONTRACTINSTANCE_BASE_L2_GAS: u32 = 1527 * 4; // SLOW_SIM_MUL = 4
11331135
pub global AVM_EMITUNENCRYPTEDLOG_BASE_L2_GAS: u32 = 15;
1134-
pub global AVM_SENDL2TOL1MSG_BASE_L2_GAS: u32 = 9 + L2_GAS_PER_L2_TO_L1_MSG;
1136+
pub global AVM_SENDL2TOL1MSG_BASE_L2_GAS: u32 = (9 + L2_GAS_PER_L2_TO_L1_MSG) * 2; // SLOW_SIM_MUL = 2
11351137
// See PR https://github.com/AztecProtocol/aztec-packages/pull/15495 on why we need this buffer.
1136-
pub global AVM_CALL_BASE_L2_GAS: u32 = 3312; // hashes bytecode (1k rows for 3k fields)
1137-
pub global AVM_STATICCALL_BASE_L2_GAS: u32 = 3312; // hashes bytecode (1k rows for 3k fields)
1138+
pub global AVM_CALL_BASE_L2_GAS: u32 = 3312 * 3; // SLOW_SIM_MUL = 3
1139+
pub global AVM_STATICCALL_BASE_L2_GAS: u32 = 3312 * 3; // SLOW_SIM_MUL = 3
11381140
pub global AVM_RETURN_BASE_L2_GAS: u32 = 9;
11391141
pub global AVM_REVERT_BASE_L2_GAS: u32 = 9;
11401142
pub global AVM_DEBUGLOG_BASE_L2_GAS: u32 = 9;
1141-
pub global AVM_POSEIDON2_BASE_L2_GAS: u32 = 24;
1143+
pub global AVM_POSEIDON2_BASE_L2_GAS: u32 = 24 * 15; // SLOW_SIM_MUL = 15
11421144
pub global AVM_SHA256COMPRESSION_BASE_L2_GAS: u32 = 12288;
11431145
pub global AVM_KECCAKF1600_BASE_L2_GAS: u32 = 58176;
1144-
pub global AVM_ECADD_BASE_L2_GAS: u32 = 27;
1146+
pub global AVM_ECADD_BASE_L2_GAS: u32 = 27 * 10; // SLOW_SIM_MUL = 10
11451147
pub global AVM_TORADIXBE_BASE_L2_GAS: u32 = 24;
11461148

11471149
// Dynamic L2 GAS

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ export const AVM_ADD_BASE_L2_GAS = 12;
419419
export const AVM_SUB_BASE_L2_GAS = 12;
420420
export const AVM_MUL_BASE_L2_GAS = 27;
421421
export const AVM_DIV_BASE_L2_GAS = 27;
422-
export const AVM_FDIV_BASE_L2_GAS = 9;
422+
export const AVM_FDIV_BASE_L2_GAS = 225;
423423
export const AVM_EQ_BASE_L2_GAS = 12;
424424
export const AVM_LT_BASE_L2_GAS = 42;
425425
export const AVM_LTE_BASE_L2_GAS = 42;
@@ -441,25 +441,25 @@ export const AVM_INTERNALCALL_BASE_L2_GAS = 9;
441441
export const AVM_INTERNALRETURN_BASE_L2_GAS = 9;
442442
export const AVM_SET_BASE_L2_GAS = 27;
443443
export const AVM_MOV_BASE_L2_GAS = 12;
444-
export const AVM_SLOAD_BASE_L2_GAS = 129;
445-
export const AVM_SSTORE_BASE_L2_GAS = 1657;
446-
export const AVM_NOTEHASHEXISTS_BASE_L2_GAS = 126;
447-
export const AVM_EMITNOTEHASH_BASE_L2_GAS = 1285;
448-
export const AVM_NULLIFIEREXISTS_BASE_L2_GAS = 132;
449-
export const AVM_EMITNULLIFIER_BASE_L2_GAS = 1540;
450-
export const AVM_L1TOL2MSGEXISTS_BASE_L2_GAS = 108;
451-
export const AVM_GETCONTRACTINSTANCE_BASE_L2_GAS = 1527;
444+
export const AVM_SLOAD_BASE_L2_GAS = 1290;
445+
export const AVM_SSTORE_BASE_L2_GAS = 33140;
446+
export const AVM_NOTEHASHEXISTS_BASE_L2_GAS = 504;
447+
export const AVM_EMITNOTEHASH_BASE_L2_GAS = 19275;
448+
export const AVM_NULLIFIEREXISTS_BASE_L2_GAS = 924;
449+
export const AVM_EMITNULLIFIER_BASE_L2_GAS = 30800;
450+
export const AVM_L1TOL2MSGEXISTS_BASE_L2_GAS = 540;
451+
export const AVM_GETCONTRACTINSTANCE_BASE_L2_GAS = 6108;
452452
export const AVM_EMITUNENCRYPTEDLOG_BASE_L2_GAS = 15;
453-
export const AVM_SENDL2TOL1MSG_BASE_L2_GAS = 209;
454-
export const AVM_CALL_BASE_L2_GAS = 3312;
455-
export const AVM_STATICCALL_BASE_L2_GAS = 3312;
453+
export const AVM_SENDL2TOL1MSG_BASE_L2_GAS = 418;
454+
export const AVM_CALL_BASE_L2_GAS = 9936;
455+
export const AVM_STATICCALL_BASE_L2_GAS = 9936;
456456
export const AVM_RETURN_BASE_L2_GAS = 9;
457457
export const AVM_REVERT_BASE_L2_GAS = 9;
458458
export const AVM_DEBUGLOG_BASE_L2_GAS = 9;
459-
export const AVM_POSEIDON2_BASE_L2_GAS = 24;
459+
export const AVM_POSEIDON2_BASE_L2_GAS = 360;
460460
export const AVM_SHA256COMPRESSION_BASE_L2_GAS = 12288;
461461
export const AVM_KECCAKF1600_BASE_L2_GAS = 58176;
462-
export const AVM_ECADD_BASE_L2_GAS = 27;
462+
export const AVM_ECADD_BASE_L2_GAS = 270;
463463
export const AVM_TORADIXBE_BASE_L2_GAS = 24;
464464
export const AVM_CALLDATACOPY_DYN_L2_GAS = 3;
465465
export const AVM_RETURNDATACOPY_DYN_L2_GAS = 3;
@@ -543,4 +543,4 @@ export enum GeneratorIndex {
543543
SECRET_HASH = 20,
544544
TX_NULLIFIER = 32,
545545
SIGNATURE_PAYLOAD = 34,
546-
}
546+
}

yarn-project/simulator/src/public/fixtures/bulk_test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ export async function megaBulkTest(
119119
const argsField3 = [7, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
120120
const argsField4 = [9, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
121121
const argsField5 = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
122-
const argsField6 = [13, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
123-
const argsField7 = [15, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
124-
const argsField8 = [17, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
122+
//const argsField6 = [13, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
123+
//const argsField7 = [15, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
124+
//const argsField8 = [17, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
125125
const argsU8 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x));
126126
const genArgs = (argsField: Fr[]) => [
127127
argsField,
@@ -145,9 +145,9 @@ export async function megaBulkTest(
145145
{ address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField3) },
146146
{ address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField4) },
147147
{ address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField5) },
148-
{ address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField6) },
149-
{ address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField7) },
150-
{ address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField8) },
148+
//{ address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField6) },
149+
//{ address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField7) },
150+
//{ address: avmTestContract.address, fnName: 'bulk_testing', args: genArgs(argsField8) },
151151
],
152152
/*teardownCall=*/ undefined,
153153
/*feePayer*/ undefined,

0 commit comments

Comments
 (0)