Skip to content

Commit f091287

Browse files
author
dbanks12
committed
chore: speed up AVM TS simulator
1 parent f8448bf commit f091287

File tree

21 files changed

+140
-414
lines changed

21 files changed

+140
-414
lines changed

noir-projects/noir-contracts/contracts/avm_test_contract/src/main.nr

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -297,42 +297,6 @@ pub contract AvmTest {
297297
}
298298

299299
/************************************************************************
300-
<<<<<<< HEAD
301-
=======
302-
* Hashing functions
303-
************************************************************************/
304-
#[public]
305-
fn keccak_hash(data: [u8; 10]) -> [u8; 32] {
306-
std::hash::keccak256(data, data.len() as u32)
307-
}
308-
309-
#[public]
310-
fn keccak_f1600(data: [u64; 25]) -> [u64; 25] {
311-
std::hash::keccak::keccakf1600(data)
312-
}
313-
314-
#[public]
315-
fn poseidon2_hash(data: [Field; 10]) -> Field {
316-
std::hash::poseidon2::Poseidon2::hash(data, data.len())
317-
}
318-
319-
#[public]
320-
fn sha256_hash(data: [u8; 10]) -> [u8; 32] {
321-
sha256::digest(data)
322-
}
323-
324-
#[public]
325-
fn pedersen_hash(data: [Field; 10]) -> Field {
326-
std::hash::pedersen_hash(data)
327-
}
328-
329-
#[public]
330-
fn pedersen_hash_with_index(data: [Field; 10]) -> Field {
331-
std::hash::pedersen_hash_with_separator(data, /*index=*/ 20)
332-
}
333-
334-
/************************************************************************
335-
>>>>>>> master
336300
* Contract instance
337301
************************************************************************/
338302
#[public]

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type Fr } from '@aztec/circuits.js';
22

3-
import { GAS_DIMENSIONS, type Gas } from './avm_gas.js';
3+
import { type Gas } from './avm_gas.js';
44
import { TaggedMemory } from './avm_memory_types.js';
55
import { type AvmRevertReason, OutOfGasError } from './errors.js';
66

@@ -92,26 +92,29 @@ export class AvmMachineState {
9292
*/
9393
public consumeGas(gasCost: Partial<Gas>) {
9494
// Assert there is enough gas on every dimension.
95-
const outOfGasDimensions = GAS_DIMENSIONS.filter(
96-
dimension => this[`${dimension}Left`] - (gasCost[dimension] ?? 0) < 0,
97-
);
95+
const outOfL2Gas = this.l2GasLeft - (gasCost.l2Gas ?? 0) < 0;
96+
const outOfDaGas = this.daGasLeft - (gasCost.daGas ?? 0) < 0;
9897
// If not, trigger an exceptional halt.
99-
// See https://yp-aztec.netlify.app/docs/public-vm/execution#gas-checks-and-tracking
100-
if (outOfGasDimensions.length > 0) {
98+
if (outOfL2Gas || outOfDaGas) {
10199
this.exceptionalHalt();
102-
throw new OutOfGasError(outOfGasDimensions);
100+
const dimensions = [];
101+
if (outOfL2Gas) {
102+
dimensions.push('l2Gas');
103+
}
104+
if (outOfDaGas) {
105+
dimensions.push('daGas');
106+
}
107+
throw new OutOfGasError(dimensions);
103108
}
104109
// Otherwise, charge the corresponding gas
105-
for (const dimension of GAS_DIMENSIONS) {
106-
this[`${dimension}Left`] -= gasCost[dimension] ?? 0;
107-
}
110+
this.l2GasLeft -= gasCost.l2Gas ?? 0;
111+
this.daGasLeft -= gasCost.daGas ?? 0;
108112
}
109113

110114
/** Increases the gas left by the amounts specified. */
111115
public refundGas(gasRefund: Partial<Gas>) {
112-
for (const dimension of GAS_DIMENSIONS) {
113-
this[`${dimension}Left`] += gasRefund[dimension] ?? 0;
114-
}
116+
this.l2GasLeft += gasRefund.l2Gas ?? 0;
117+
this.daGasLeft += gasRefund.daGas ?? 0;
115118
}
116119

117120
/**
@@ -151,7 +154,8 @@ export class AvmMachineState {
151154
* Flag an exceptional halt. Clears gas left and sets the reverted flag. No output data.
152155
*/
153156
private exceptionalHalt() {
154-
GAS_DIMENSIONS.forEach(dimension => (this[`${dimension}Left`] = 0));
157+
this.l2GasLeft = 0;
158+
this.daGasLeft = 0;
155159
this.reverted = true;
156160
this.halted = true;
157161
}

yarn-project/simulator/src/avm/avm_memory_types.test.ts

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
import { AssertionError } from 'assert';
22

3-
import {
4-
Field,
5-
MeteredTaggedMemory,
6-
TaggedMemory,
7-
Uint1,
8-
Uint8,
9-
Uint16,
10-
Uint32,
11-
Uint64,
12-
Uint128,
13-
} from './avm_memory_types.js';
3+
import { Field, TaggedMemory, Uint1, Uint8, Uint16, Uint32, Uint64, Uint128 } from './avm_memory_types.js';
144

155
describe('TaggedMemory', () => {
166
it('Elements should be Field(0) after construction', () => {
@@ -67,58 +57,6 @@ describe('TaggedMemory', () => {
6757
});
6858
});
6959

70-
describe('MeteredTaggedMemory', () => {
71-
let mem: MeteredTaggedMemory;
72-
73-
beforeEach(() => {
74-
mem = new MeteredTaggedMemory(new TaggedMemory());
75-
});
76-
77-
it(`Counts reads`, () => {
78-
mem.get(10);
79-
mem.getAs(20);
80-
expect(mem.reset()).toEqual({ reads: 2, writes: 0 });
81-
});
82-
83-
it(`Counts reading slices`, () => {
84-
const val = [new Field(5), new Field(6), new Field(7)];
85-
mem.setSlice(10, val);
86-
mem.reset();
87-
88-
mem.getSlice(10, 3);
89-
mem.getSliceAs(11, 2);
90-
expect(mem.reset()).toEqual({ reads: 5, writes: 0 });
91-
});
92-
93-
it(`Counts writes`, () => {
94-
mem.set(10, new Uint8(5));
95-
expect(mem.reset()).toEqual({ reads: 0, writes: 1 });
96-
});
97-
98-
it(`Counts writing slices`, () => {
99-
mem.setSlice(10, [new Field(5), new Field(6)]);
100-
expect(mem.reset()).toEqual({ reads: 0, writes: 2 });
101-
});
102-
103-
it(`Clears stats`, () => {
104-
mem.get(10);
105-
mem.set(20, new Uint8(5));
106-
expect(mem.reset()).toEqual({ reads: 1, writes: 1 });
107-
expect(mem.reset()).toEqual({ reads: 0, writes: 0 });
108-
});
109-
110-
it(`Asserts stats`, () => {
111-
mem.get(10);
112-
mem.set(20, new Uint8(5));
113-
expect(() => mem.assert({ reads: 1, writes: 1 })).not.toThrow();
114-
});
115-
116-
it(`Throws on failed stat assertion`, () => {
117-
mem.get(10);
118-
expect(() => mem.assert({ reads: 1, writes: 1 })).toThrow();
119-
});
120-
});
121-
12260
type IntegralClass = typeof Uint1 | typeof Uint8 | typeof Uint16 | typeof Uint32 | typeof Uint64 | typeof Uint128;
12361

12462
describe.each([Uint1])('Integral Types (U1 only)', (clsValue: IntegralClass) => {

0 commit comments

Comments
 (0)