From de82a206a9f55969cf2ef1970eb2e36e78071947 Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Wed, 27 Mar 2024 09:12:12 -0300 Subject: [PATCH] Apply suggestions from code review and fix test --- yarn-project/foundation/package.json | 1 - yarn-project/foundation/src/number/index.ts | 9 --------- .../simulator/src/avm/avm_gas_cost.test.ts | 15 ++++++++------- yarn-project/simulator/src/avm/fixtures/index.ts | 6 +++--- .../simulator/src/avm/opcodes/addressing_mode.ts | 2 +- .../simulator/src/avm/opcodes/arithmetic.ts | 11 +++++++---- 6 files changed, 19 insertions(+), 25 deletions(-) delete mode 100644 yarn-project/foundation/src/number/index.ts diff --git a/yarn-project/foundation/package.json b/yarn-project/foundation/package.json index c7650362040..780d5cd721f 100644 --- a/yarn-project/foundation/package.json +++ b/yarn-project/foundation/package.json @@ -22,7 +22,6 @@ "./json-rpc/client": "./dest/json-rpc/client/index.js", "./log": "./dest/log/index.js", "./mutex": "./dest/mutex/index.js", - "./number": "./dest/number/index.js", "./fields": "./dest/fields/index.js", "./retry": "./dest/retry/index.js", "./running-promise": "./dest/running-promise/index.js", diff --git a/yarn-project/foundation/src/number/index.ts b/yarn-project/foundation/src/number/index.ts deleted file mode 100644 index 0cd83c75f6b..00000000000 --- a/yarn-project/foundation/src/number/index.ts +++ /dev/null @@ -1,9 +0,0 @@ -/** Returns the number of bits set in the given number. */ -export function countSetBits(num: number): number { - let count = 0; - while (num !== 0) { - count += num & 1; - num >>= 1; - } - return count; -} diff --git a/yarn-project/simulator/src/avm/avm_gas_cost.test.ts b/yarn-project/simulator/src/avm/avm_gas_cost.test.ts index 45eef3246df..c2c9cd450b7 100644 --- a/yarn-project/simulator/src/avm/avm_gas_cost.test.ts +++ b/yarn-project/simulator/src/avm/avm_gas_cost.test.ts @@ -1,7 +1,6 @@ -import { AvmMachineState } from './avm_machine_state.js'; import { TypeTag } from './avm_memory_types.js'; import { AvmSimulator } from './avm_simulator.js'; -import { initContext, initExecutionEnvironment } from './fixtures/index.js'; +import { initContext } from './fixtures/index.js'; import { Add, CalldataCopy, Div, Mul, Set as SetInstruction, Sub } from './opcodes/index.js'; import { encodeToBytecode } from './serialization/bytecode_serialization.js'; @@ -19,13 +18,15 @@ describe('AVM simulator: dynamic gas costs per instruction', () => { [new Div(/*indirect=*/ 3, /*inTag=*/ TypeTag.UINT8, /*aOffset=*/ 1, /*bOffset=*/ 2, /*dstOffset=*/ 3), [20, 0, 0]], ] as const)('computes gas cost for %s', async (instruction, [l2GasCost, l1GasCost, daGasCost]) => { const bytecode = encodeToBytecode([instruction]); + const context = initContext(); + const { + l2GasLeft: initialL2GasLeft, + daGasLeft: initialDaGasLeft, + l1GasLeft: initialL1GasLeft, + } = context.machineState; - const context = initContext({ env: initExecutionEnvironment({ calldata: [] }) }); - const initialState = AvmMachineState.fromState(context.machineState); - const { l2GasLeft: initialL2GasLeft, daGasLeft: initialDaGasLeft, l1GasLeft: initialL1GasLeft } = initialState; - const simulator = new AvmSimulator(context); + await new AvmSimulator(context).executeBytecode(bytecode); - await simulator.executeBytecode(bytecode); expect(initialL2GasLeft - context.machineState.l2GasLeft).toEqual(l2GasCost); expect(initialL1GasLeft - context.machineState.l1GasLeft).toEqual(l1GasCost); expect(initialDaGasLeft - context.machineState.daGasLeft).toEqual(daGasCost); diff --git a/yarn-project/simulator/src/avm/fixtures/index.ts b/yarn-project/simulator/src/avm/fixtures/index.ts index ed81c4fde16..88fc65a2de2 100644 --- a/yarn-project/simulator/src/avm/fixtures/index.ts +++ b/yarn-project/simulator/src/avm/fixtures/index.ts @@ -89,9 +89,9 @@ export function initGlobalVariables(overrides?: Partial): Globa */ export function initMachineState(overrides?: Partial): AvmMachineState { return AvmMachineState.fromState({ - l1GasLeft: overrides?.l1GasLeft ?? 1e6, - l2GasLeft: overrides?.l2GasLeft ?? 1e6, - daGasLeft: overrides?.daGasLeft ?? 1e6, + l1GasLeft: overrides?.l1GasLeft ?? 100e6, + l2GasLeft: overrides?.l2GasLeft ?? 100e6, + daGasLeft: overrides?.daGasLeft ?? 100e6, }); } diff --git a/yarn-project/simulator/src/avm/opcodes/addressing_mode.ts b/yarn-project/simulator/src/avm/opcodes/addressing_mode.ts index 280d15e1ade..97e19d9f083 100644 --- a/yarn-project/simulator/src/avm/opcodes/addressing_mode.ts +++ b/yarn-project/simulator/src/avm/opcodes/addressing_mode.ts @@ -12,7 +12,7 @@ export enum AddressingMode { export class Addressing { public constructor( /** The addressing mode for each operand. The length of this array is the number of operands of the instruction. */ - private readonly modePerOperand: AddressingMode[], + public readonly modePerOperand: AddressingMode[], ) { assert(modePerOperand.length <= 8, 'At most 8 operands are supported'); } diff --git a/yarn-project/simulator/src/avm/opcodes/arithmetic.ts b/yarn-project/simulator/src/avm/opcodes/arithmetic.ts index 43acff895ff..44790a692d8 100644 --- a/yarn-project/simulator/src/avm/opcodes/arithmetic.ts +++ b/yarn-project/simulator/src/avm/opcodes/arithmetic.ts @@ -1,9 +1,8 @@ -import { countSetBits } from '@aztec/foundation/number'; - import type { AvmContext } from '../avm_context.js'; import { GasCost, GasCostConstants, getGasCostMultiplierFromTypeTag, makeGasCost } from '../avm_gas_cost.js'; import { Field, MemoryValue, TypeTag } from '../avm_memory_types.js'; import { Opcode, OperandType } from '../serialization/instruction_serialization.js'; +import { Addressing, AddressingMode } from './addressing_mode.js'; import { Instruction } from './instruction.js'; import { ThreeOperandInstruction } from './instruction_impl.js'; @@ -21,9 +20,13 @@ export abstract class ThreeOperandArithmeticInstruction extends ThreeOperandInst } protected gasCost(): GasCost { + const indirectCount = Addressing.fromWire(this.indirect).modePerOperand.filter( + mode => mode === AddressingMode.INDIRECT, + ).length; + const l2Gas = - countSetBits(this.indirect) * GasCostConstants.ARITHMETIC_COST_PER_INDIRECT_ACCESS + - GasCostConstants.ARITHMETIC_COST_PER_BYTE * getGasCostMultiplierFromTypeTag(this.inTag); + indirectCount * GasCostConstants.ARITHMETIC_COST_PER_INDIRECT_ACCESS + + getGasCostMultiplierFromTypeTag(this.inTag) * GasCostConstants.ARITHMETIC_COST_PER_BYTE; return makeGasCost({ l2Gas }); }