From e46b865f4c53c8916061147c85fd298db31a384f Mon Sep 17 00:00:00 2001 From: David Banks <47112877+dbanks12@users.noreply.github.com> Date: Mon, 29 Jan 2024 07:48:45 -0500 Subject: [PATCH] chore(avm-simulator): cleanup, tags as first instruction constructor args (#4244) --- .../src/avm/avm_memory_types.ts | 5 +- .../src/avm/interpreter/interpreter.test.ts | 4 +- .../src/avm/opcodes/arithmetic.ts | 16 +++---- .../src/avm/opcodes/bitwise.test.ts | 22 ++++----- .../acir-simulator/src/avm/opcodes/bitwise.ts | 24 +++++----- .../src/avm/opcodes/control_flow.test.ts | 16 +++---- .../src/avm/opcodes/control_flow.ts | 2 +- .../src/avm/opcodes/decode_bytecode.ts | 5 +- .../src/avm/opcodes/encode_to_bytecode.ts | 6 ++- .../src/avm/opcodes/external_calls.test.ts | 4 +- .../src/avm/opcodes/memory.test.ts | 46 +++++++++---------- .../acir-simulator/src/avm/opcodes/memory.ts | 6 +-- .../acir-simulator/src/avm/opcodes/storage.ts | 4 +- 13 files changed, 82 insertions(+), 78 deletions(-) diff --git a/yarn-project/acir-simulator/src/avm/avm_memory_types.ts b/yarn-project/acir-simulator/src/avm/avm_memory_types.ts index 9f831bfd21e..6967a58fd8f 100644 --- a/yarn-project/acir-simulator/src/avm/avm_memory_types.ts +++ b/yarn-project/acir-simulator/src/avm/avm_memory_types.ts @@ -228,8 +228,8 @@ export class TaggedMemory { public getAs(offset: number): T { assert(offset < TaggedMemory.MAX_MEMORY_SIZE); - const e = this._mem[offset]; - return e; + const word = this._mem[offset]; + return word as T; } public getSlice(offset: number, size: number): MemoryValue[] { @@ -282,6 +282,7 @@ export class TaggedMemory { // Truncates the value to fit the type. public static integralFromTag(v: bigint, tag: TypeTag): IntegralValue { + v = BigInt(v); // FIXME: not sure why this cast is needed, but this errors otherwise switch (tag) { case TypeTag.UINT8: return new Uint8(v & ((1n << 8n) - 1n)); diff --git a/yarn-project/acir-simulator/src/avm/interpreter/interpreter.test.ts b/yarn-project/acir-simulator/src/avm/interpreter/interpreter.test.ts index 7ecaf130942..79b814da07c 100644 --- a/yarn-project/acir-simulator/src/avm/interpreter/interpreter.test.ts +++ b/yarn-project/acir-simulator/src/avm/interpreter/interpreter.test.ts @@ -22,8 +22,8 @@ describe('interpreter', () => { const calldata: Fr[] = [new Fr(1), new Fr(2)]; const instructions: Instruction[] = [ - new CalldataCopy(/*cdOffset=*/ 0, /*copySize=*/ 2, /*destOffset=*/ 0), - new Add(/*aOffset=*/ 0, /*bOffset=*/ 1, /*destOffset=*/ 2), + new CalldataCopy(/*cdOffset=*/ 0, /*copySize=*/ 2, /*dstOffset=*/ 0), + new Add(/*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 2), new Return(/*returnOffset=*/ 2, /*copySize=*/ 1), ]; diff --git a/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.ts b/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.ts index 507430feae7..5366c8428d6 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/arithmetic.ts @@ -6,7 +6,7 @@ export class Add extends Instruction { static type: string = 'ADD'; static numberOfOperands = 3; - constructor(private aOffset: number, private bOffset: number, private destOffset: number) { + constructor(private aOffset: number, private bOffset: number, private dstOffset: number) { super(); } @@ -15,7 +15,7 @@ export class Add extends Instruction { const b = machineState.memory.get(this.bOffset); const dest = a.add(b); - machineState.memory.set(this.destOffset, dest); + machineState.memory.set(this.dstOffset, dest); this.incrementPc(machineState); } @@ -25,7 +25,7 @@ export class Sub extends Instruction { static type: string = 'SUB'; static numberOfOperands = 3; - constructor(private aOffset: number, private bOffset: number, private destOffset: number) { + constructor(private aOffset: number, private bOffset: number, private dstOffset: number) { super(); } @@ -34,7 +34,7 @@ export class Sub extends Instruction { const b = machineState.memory.get(this.bOffset); const dest = a.sub(b); - machineState.memory.set(this.destOffset, dest); + machineState.memory.set(this.dstOffset, dest); this.incrementPc(machineState); } @@ -44,7 +44,7 @@ export class Mul extends Instruction { static type: string = 'MUL'; static numberOfOperands = 3; - constructor(private aOffset: number, private bOffset: number, private destOffset: number) { + constructor(private aOffset: number, private bOffset: number, private dstOffset: number) { super(); } @@ -53,7 +53,7 @@ export class Mul extends Instruction { const b = machineState.memory.get(this.bOffset); const dest = a.mul(b); - machineState.memory.set(this.destOffset, dest); + machineState.memory.set(this.dstOffset, dest); this.incrementPc(machineState); } @@ -64,7 +64,7 @@ export class Div extends Instruction { static type: string = 'DIV'; static numberOfOperands = 3; - constructor(private aOffset: number, private bOffset: number, private destOffset: number) { + constructor(private aOffset: number, private bOffset: number, private dstOffset: number) { super(); } @@ -73,7 +73,7 @@ export class Div extends Instruction { const b = machineState.memory.get(this.bOffset); const dest = a.div(b); - machineState.memory.set(this.destOffset, dest); + machineState.memory.set(this.dstOffset, dest); this.incrementPc(machineState); } diff --git a/yarn-project/acir-simulator/src/avm/opcodes/bitwise.test.ts b/yarn-project/acir-simulator/src/avm/opcodes/bitwise.test.ts index 426542a2733..ae8ce802724 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/bitwise.test.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/bitwise.test.ts @@ -19,7 +19,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, new Uint32(0b11111110010011100100n)); machineState.memory.set(1, new Uint32(0b11100100111001001111n)); - await new And(0, 1, 2, TypeTag.UINT32).execute(machineState, journal); + await new And(TypeTag.UINT32, 0, 1, 2).execute(machineState, journal); const actual = machineState.memory.get(2); expect(actual).toEqual(new Uint32(0b11100100010001000100n)); @@ -32,7 +32,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - await new Or(0, 1, 2, TypeTag.UINT32).execute(machineState, journal); + await new Or(TypeTag.UINT32, 0, 1, 2).execute(machineState, journal); const expected = new Uint32(0b11111110111011101111n); const actual = machineState.memory.get(2); @@ -46,7 +46,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - await new Xor(0, 1, 2, TypeTag.UINT32).execute(machineState, journal); + await new Xor(TypeTag.UINT32, 0, 1, 2).execute(machineState, journal); const expected = new Uint32(0b00011010101010101011n); const actual = machineState.memory.get(2); @@ -61,7 +61,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - await new Shr(0, 1, 2, TypeTag.UINT32).execute(machineState, journal); + await new Shr(TypeTag.UINT32, 0, 1, 2).execute(machineState, journal); const expected = a; const actual = machineState.memory.get(2); @@ -75,7 +75,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - await new Shr(0, 1, 2, TypeTag.UINT32).execute(machineState, journal); + await new Shr(TypeTag.UINT32, 0, 1, 2).execute(machineState, journal); const expected = new Uint32(0b00111111100100111001n); const actual = machineState.memory.get(2); @@ -89,7 +89,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - await new Shr(0, 1, 2, TypeTag.UINT32).execute(machineState, journal); + await new Shr(TypeTag.UINT32, 0, 1, 2).execute(machineState, journal); const expected = new Uint32(0b01n); const actual = machineState.memory.get(2); @@ -105,7 +105,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - await new Shl(0, 1, 2, TypeTag.UINT32).execute(machineState, journal); + await new Shl(TypeTag.UINT32, 0, 1, 2).execute(machineState, journal); const expected = a; const actual = machineState.memory.get(2); @@ -119,7 +119,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - await new Shl(0, 1, 2, TypeTag.UINT32).execute(machineState, journal); + await new Shl(TypeTag.UINT32, 0, 1, 2).execute(machineState, journal); const expected = new Uint32(0b1111111001001110010000n); const actual = machineState.memory.get(2); @@ -133,7 +133,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - await new Shl(0, 1, 2, TypeTag.UINT16).execute(machineState, journal); + await new Shl(TypeTag.UINT16, 0, 1, 2).execute(machineState, journal); const expected = new Uint16(0n); const actual = machineState.memory.get(2); @@ -147,7 +147,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); machineState.memory.set(1, b); - await new Shl(0, 1, 2, TypeTag.UINT16).execute(machineState, journal); + await new Shl(TypeTag.UINT16, 0, 1, 2).execute(machineState, journal); const expected = new Uint16(0b1001001110011100n); const actual = machineState.memory.get(2); @@ -160,7 +160,7 @@ describe('Bitwise instructions', () => { machineState.memory.set(0, a); - await new Not(0, 1, TypeTag.UINT16).execute(machineState, journal); + await new Not(TypeTag.UINT16, 0, 1).execute(machineState, journal); const expected = new Uint16(0b1001101100011011n); // high bits! const actual = machineState.memory.get(1); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/bitwise.ts b/yarn-project/acir-simulator/src/avm/opcodes/bitwise.ts index 31420d0b3bc..e439a8bd447 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/bitwise.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/bitwise.ts @@ -7,7 +7,7 @@ export class And extends Instruction { static type: string = 'AND'; static numberOfOperands = 3; - constructor(private aOffset: number, private bOffset: number, private destOffset: number, private inTag: TypeTag) { + constructor(private inTag: TypeTag, private aOffset: number, private bOffset: number, private dstOffset: number) { super(); } @@ -18,7 +18,7 @@ export class And extends Instruction { const b = machineState.memory.getAs(this.bOffset); const res = a.and(b); - machineState.memory.set(this.destOffset, res); + machineState.memory.set(this.dstOffset, res); this.incrementPc(machineState); } @@ -28,7 +28,7 @@ export class Or extends Instruction { static type: string = 'OR'; static numberOfOperands = 3; - constructor(private aOffset: number, private bOffset: number, private destOffset: number, private inTag: TypeTag) { + constructor(private inTag: TypeTag, private aOffset: number, private bOffset: number, private dstOffset: number) { super(); } @@ -39,7 +39,7 @@ export class Or extends Instruction { const b = machineState.memory.getAs(this.bOffset); const res = a.or(b); - machineState.memory.set(this.destOffset, res); + machineState.memory.set(this.dstOffset, res); this.incrementPc(machineState); } @@ -49,7 +49,7 @@ export class Xor extends Instruction { static type: string = 'XOR'; static numberOfOperands = 3; - constructor(private aOffset: number, private bOffset: number, private destOffset: number, private inTag: TypeTag) { + constructor(private inTag: TypeTag, private aOffset: number, private bOffset: number, private dstOffset: number) { super(); } @@ -60,7 +60,7 @@ export class Xor extends Instruction { const b = machineState.memory.getAs(this.bOffset); const res = a.xor(b); - machineState.memory.set(this.destOffset, res); + machineState.memory.set(this.dstOffset, res); this.incrementPc(machineState); } @@ -70,7 +70,7 @@ export class Not extends Instruction { static type: string = 'NOT'; static numberOfOperands = 2; - constructor(private aOffset: number, private destOffset: number, private inTag: TypeTag) { + constructor(private inTag: TypeTag, private aOffset: number, private dstOffset: number) { super(); } @@ -80,7 +80,7 @@ export class Not extends Instruction { const a = machineState.memory.getAs(this.aOffset); const res = a.not(); - machineState.memory.set(this.destOffset, res); + machineState.memory.set(this.dstOffset, res); this.incrementPc(machineState); } @@ -90,7 +90,7 @@ export class Shl extends Instruction { static type: string = 'SHL'; static numberOfOperands = 3; - constructor(private aOffset: number, private bOffset: number, private destOffset: number, private inTag: TypeTag) { + constructor(private inTag: TypeTag, private aOffset: number, private bOffset: number, private dstOffset: number) { super(); } @@ -101,7 +101,7 @@ export class Shl extends Instruction { const b = machineState.memory.getAs(this.bOffset); const res = a.shl(b); - machineState.memory.set(this.destOffset, res); + machineState.memory.set(this.dstOffset, res); this.incrementPc(machineState); } @@ -111,7 +111,7 @@ export class Shr extends Instruction { static type: string = 'SHR'; static numberOfOperands = 3; - constructor(private aOffset: number, private bOffset: number, private destOffset: number, private inTag: TypeTag) { + constructor(private inTag: TypeTag, private aOffset: number, private bOffset: number, private dstOffset: number) { super(); } @@ -122,7 +122,7 @@ export class Shr extends Instruction { const b = machineState.memory.getAs(this.bOffset); const res = a.shr(b); - machineState.memory.set(this.destOffset, res); + machineState.memory.set(this.dstOffset, res); this.incrementPc(machineState); } diff --git a/yarn-project/acir-simulator/src/avm/opcodes/control_flow.test.ts b/yarn-project/acir-simulator/src/avm/opcodes/control_flow.test.ts index 973655279e6..59e24adb042 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/control_flow.test.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/control_flow.test.ts @@ -124,17 +124,17 @@ describe('Control Flow Opcodes', () => { new Lt(TypeTag.UINT16, 0, 1, 2), new Lte(TypeTag.UINT16, 0, 1, 2), new Eq(TypeTag.UINT16, 0, 1, 2), - new Xor(0, 1, 2, TypeTag.UINT16), - new And(0, 1, 2, TypeTag.UINT16), - new Or(0, 1, 2, TypeTag.UINT16), - new Shl(0, 1, 2, TypeTag.UINT16), - new Shr(0, 1, 2, TypeTag.UINT16), - new Not(0, 2, TypeTag.UINT16), + new Xor(TypeTag.UINT16, 0, 1, 2), + new And(TypeTag.UINT16, 0, 1, 2), + new Or(TypeTag.UINT16, 0, 1, 2), + new Shl(TypeTag.UINT16, 0, 1, 2), + new Shr(TypeTag.UINT16, 0, 1, 2), + new Not(TypeTag.UINT16, 0, 2), new CalldataCopy(0, 1, 2), - new Set(0n, 1, TypeTag.UINT16), + new Set(TypeTag.UINT16, 0n, 1), new Mov(0, 1), new CMov(0, 1, 2, 3), - new Cast(0, 1, TypeTag.UINT16), + new Cast(TypeTag.UINT16, 0, 1), ]; for (const instruction of instructions) { diff --git a/yarn-project/acir-simulator/src/avm/opcodes/control_flow.ts b/yarn-project/acir-simulator/src/avm/opcodes/control_flow.ts index 518b987a1de..6bbee244cbb 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/control_flow.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/control_flow.ts @@ -16,7 +16,7 @@ export class Return extends Instruction { async execute(machineState: AvmMachineState, _journal: AvmJournal): Promise { const returnData = machineState.memory .getSlice(this.returnOffset, this.copySize) - .map(fvt => new Fr(fvt.toBigInt())); + .map(word => new Fr(word.toBigInt())); machineState.setReturnData(returnData); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/decode_bytecode.ts b/yarn-project/acir-simulator/src/avm/opcodes/decode_bytecode.ts index 624546cfbeb..33533f31abb 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/decode_bytecode.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/decode_bytecode.ts @@ -17,17 +17,18 @@ export function decodeBytecode(bytecode: Buffer): Instruction[] { const opcodeByte = bytecode[bytePtr]; bytePtr += AVM_OPCODE_BYTE_LENGTH; if (!(opcodeByte in Opcode)) { - throw new Error(`Opcode ${opcodeByte} not implemented`); + throw new Error(`Opcode 0x${opcodeByte.toString(16)} not implemented`); } const opcode = opcodeByte as Opcode; const instructionType = INSTRUCTION_SET.get(opcode); if (instructionType === undefined) { - throw new Error(`Opcode ${opcode} not implemented`); + throw new Error(`Opcode 0x${opcode.toString(16)} not implemented`); } const numberOfOperands = instructionType.numberOfOperands; const operands: number[] = []; for (let i = 0; i < numberOfOperands; i++) { + // TODO: support constants which might not be u32s const operand = bytecode.readUInt32BE(bytePtr); bytePtr += AVM_OPERAND_BYTE_LENGTH; operands.push(operand); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/encode_to_bytecode.ts b/yarn-project/acir-simulator/src/avm/opcodes/encode_to_bytecode.ts index 186706847d3..105c0f808ab 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/encode_to_bytecode.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/encode_to_bytecode.ts @@ -11,12 +11,14 @@ import { Opcode } from './opcodes.js'; export function encodeToBytecode(opcode: Opcode, args: number[]): Buffer { const instructionType = INSTRUCTION_SET.get(opcode); if (instructionType === undefined) { - throw new Error(`Opcode ${opcode} not implemented`); + throw new Error(`Opcode 0x${opcode.toString(16)} not implemented`); } const numberOfOperands = instructionType.numberOfOperands; if (args.length !== numberOfOperands) { - throw new Error(`Opcode ${opcode} expects ${numberOfOperands} arguments, but ${args.length} were provided`); + throw new Error( + `Opcode 0x${opcode.toString(16)} expects ${numberOfOperands} arguments, but ${args.length} were provided`, + ); } const bytecode = Buffer.alloc(AVM_OPCODE_BYTE_LENGTH + numberOfOperands * AVM_OPERAND_BYTE_LENGTH); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/external_calls.test.ts b/yarn-project/acir-simulator/src/avm/opcodes/external_calls.test.ts index 8f6fd076038..1d91c3bc405 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/external_calls.test.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/external_calls.test.ts @@ -54,7 +54,7 @@ describe('External Calls', () => { const otherContextInstructions: [Opcode, any[]][] = [ // Place [1,2,3] into memory - [Opcode.CALLDATACOPY, [/*value=*/ 0, /*copySize=*/ argsSize, /*destOffset=*/ 0]], + [Opcode.CALLDATACOPY, [/*value=*/ 0, /*copySize=*/ argsSize, /*dstOffset=*/ 0]], // Store 1 into slot 1 [Opcode.SSTORE, [/*slotOffset=*/ 0, /*dataOffset=*/ 0]], // Return [1,2] from memory @@ -110,7 +110,7 @@ describe('External Calls', () => { const otherContextInstructions: [Opcode, any[]][] = [ // Place [1,2,3] into memory - [Opcode.CALLDATACOPY, [/*value=*/ 0, /*copySize=*/ argsSize, /*destOffset=*/ 0]], + [Opcode.CALLDATACOPY, [/*value=*/ 0, /*copySize=*/ argsSize, /*dstOffset=*/ 0]], [Opcode.SSTORE, [/*slotOffset*/ 1, /*dataOffset=*/ 0]], ]; diff --git a/yarn-project/acir-simulator/src/avm/opcodes/memory.test.ts b/yarn-project/acir-simulator/src/avm/opcodes/memory.test.ts index de9af42972d..1a10c5be15b 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/memory.test.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/memory.test.ts @@ -19,7 +19,7 @@ describe('Memory instructions', () => { describe('SET', () => { it('should correctly set value and tag (uninitialized)', async () => { - await new Set(/*value=*/ 1234n, /*offset=*/ 1, TypeTag.UINT16).execute(machineState, journal); + await new Set(TypeTag.UINT16, /*value=*/ 1234n, /*offset=*/ 1).execute(machineState, journal); const actual = machineState.memory.get(1); const tag = machineState.memory.getTag(1); @@ -31,7 +31,7 @@ describe('Memory instructions', () => { it('should correctly set value and tag (overwriting)', async () => { machineState.memory.set(1, new Field(27)); - await new Set(/*value=*/ 1234n, /*offset=*/ 1, TypeTag.UINT32).execute(machineState, journal); + await new Set(TypeTag.UINT32, /*value=*/ 1234n, /*offset=*/ 1).execute(machineState, journal); const actual = machineState.memory.get(1); const tag = machineState.memory.getTag(1); @@ -50,11 +50,11 @@ describe('Memory instructions', () => { machineState.memory.set(4, new Uint128(1n << 100n)); [ - new Cast(/*aOffset=*/ 0, /*dstOffset=*/ 10, TypeTag.UINT16), - new Cast(/*aOffset=*/ 1, /*dstOffset=*/ 11, TypeTag.UINT32), - new Cast(/*aOffset=*/ 2, /*dstOffset=*/ 12, TypeTag.UINT64), - new Cast(/*aOffset=*/ 3, /*dstOffset=*/ 13, TypeTag.UINT128), - new Cast(/*aOffset=*/ 4, /*dstOffset=*/ 14, TypeTag.UINT128), + new Cast(TypeTag.UINT16, /*aOffset=*/ 0, /*dstOffset=*/ 10), + new Cast(TypeTag.UINT32, /*aOffset=*/ 1, /*dstOffset=*/ 11), + new Cast(TypeTag.UINT64, /*aOffset=*/ 2, /*dstOffset=*/ 12), + new Cast(TypeTag.UINT128, /*aOffset=*/ 3, /*dstOffset=*/ 13), + new Cast(TypeTag.UINT128, /*aOffset=*/ 4, /*dstOffset=*/ 14), ].forEach(i => i.execute(machineState, journal)); const actual = machineState.memory.getSlice(/*offset=*/ 10, /*size=*/ 5); @@ -77,11 +77,11 @@ describe('Memory instructions', () => { machineState.memory.set(4, new Uint128((1n << 100n) - 1n)); [ - new Cast(/*aOffset=*/ 0, /*dstOffset=*/ 10, TypeTag.UINT8), - new Cast(/*aOffset=*/ 1, /*dstOffset=*/ 11, TypeTag.UINT8), - new Cast(/*aOffset=*/ 2, /*dstOffset=*/ 12, TypeTag.UINT16), - new Cast(/*aOffset=*/ 3, /*dstOffset=*/ 13, TypeTag.UINT32), - new Cast(/*aOffset=*/ 4, /*dstOffset=*/ 14, TypeTag.UINT64), + new Cast(TypeTag.UINT8, /*aOffset=*/ 0, /*dstOffset=*/ 10), + new Cast(TypeTag.UINT8, /*aOffset=*/ 1, /*dstOffset=*/ 11), + new Cast(TypeTag.UINT16, /*aOffset=*/ 2, /*dstOffset=*/ 12), + new Cast(TypeTag.UINT32, /*aOffset=*/ 3, /*dstOffset=*/ 13), + new Cast(TypeTag.UINT64, /*aOffset=*/ 4, /*dstOffset=*/ 14), ].forEach(i => i.execute(machineState, journal)); const actual = machineState.memory.getSlice(/*offset=*/ 10, /*size=*/ 5); @@ -104,11 +104,11 @@ describe('Memory instructions', () => { machineState.memory.set(4, new Uint128(1n << 100n)); [ - new Cast(/*aOffset=*/ 0, /*dstOffset=*/ 10, TypeTag.FIELD), - new Cast(/*aOffset=*/ 1, /*dstOffset=*/ 11, TypeTag.FIELD), - new Cast(/*aOffset=*/ 2, /*dstOffset=*/ 12, TypeTag.FIELD), - new Cast(/*aOffset=*/ 3, /*dstOffset=*/ 13, TypeTag.FIELD), - new Cast(/*aOffset=*/ 4, /*dstOffset=*/ 14, TypeTag.FIELD), + new Cast(TypeTag.FIELD, /*aOffset=*/ 0, /*dstOffset=*/ 10), + new Cast(TypeTag.FIELD, /*aOffset=*/ 1, /*dstOffset=*/ 11), + new Cast(TypeTag.FIELD, /*aOffset=*/ 2, /*dstOffset=*/ 12), + new Cast(TypeTag.FIELD, /*aOffset=*/ 3, /*dstOffset=*/ 13), + new Cast(TypeTag.FIELD, /*aOffset=*/ 4, /*dstOffset=*/ 14), ].forEach(i => i.execute(machineState, journal)); const actual = machineState.memory.getSlice(/*offset=*/ 10, /*size=*/ 5); @@ -131,11 +131,11 @@ describe('Memory instructions', () => { machineState.memory.set(4, new Field((1n << 200n) - 1n)); [ - new Cast(/*aOffset=*/ 0, /*dstOffset=*/ 10, TypeTag.UINT8), - new Cast(/*aOffset=*/ 1, /*dstOffset=*/ 11, TypeTag.UINT16), - new Cast(/*aOffset=*/ 2, /*dstOffset=*/ 12, TypeTag.UINT32), - new Cast(/*aOffset=*/ 3, /*dstOffset=*/ 13, TypeTag.UINT64), - new Cast(/*aOffset=*/ 4, /*dstOffset=*/ 14, TypeTag.UINT128), + new Cast(TypeTag.UINT8, /*aOffset=*/ 0, /*dstOffset=*/ 10), + new Cast(TypeTag.UINT16, /*aOffset=*/ 1, /*dstOffset=*/ 11), + new Cast(TypeTag.UINT32, /*aOffset=*/ 2, /*dstOffset=*/ 12), + new Cast(TypeTag.UINT64, /*aOffset=*/ 3, /*dstOffset=*/ 13), + new Cast(TypeTag.UINT128, /*aOffset=*/ 4, /*dstOffset=*/ 14), ].forEach(i => i.execute(machineState, journal)); const actual = machineState.memory.getSlice(/*offset=*/ 10, /*size=*/ 5); @@ -153,7 +153,7 @@ describe('Memory instructions', () => { it('Should cast between field elements', async () => { machineState.memory.set(0, new Field(12345678n)); - await new Cast(/*aOffset=*/ 0, /*dstOffset=*/ 1, TypeTag.FIELD).execute(machineState, journal); + await new Cast(TypeTag.FIELD, /*aOffset=*/ 0, /*dstOffset=*/ 1).execute(machineState, journal); const actual = machineState.memory.get(1); expect(actual).toEqual(new Field(12345678n)); diff --git a/yarn-project/acir-simulator/src/avm/opcodes/memory.ts b/yarn-project/acir-simulator/src/avm/opcodes/memory.ts index 0c8ada5b6cd..2524684dc02 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/memory.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/memory.ts @@ -7,12 +7,12 @@ export class Set extends Instruction { static type: string = 'SET'; static numberOfOperands = 3; - constructor(private value: bigint, private dstOffset: number, private dstTag: TypeTag) { + constructor(private inTag: TypeTag, private value: bigint, private dstOffset: number) { super(); } async execute(machineState: AvmMachineState, _journal: AvmJournal): Promise { - const res = TaggedMemory.integralFromTag(this.value, this.dstTag); + const res = TaggedMemory.integralFromTag(this.value, this.inTag); machineState.memory.set(this.dstOffset, res); @@ -24,7 +24,7 @@ export class Cast extends Instruction { static type: string = 'CAST'; static numberOfOperands = 3; - constructor(private aOffset: number, private dstOffset: number, private dstTag: TypeTag) { + constructor(private dstTag: TypeTag, private aOffset: number, private dstOffset: number) { super(); } diff --git a/yarn-project/acir-simulator/src/avm/opcodes/storage.ts b/yarn-project/acir-simulator/src/avm/opcodes/storage.ts index 419cbecc1da..7364945d992 100644 --- a/yarn-project/acir-simulator/src/avm/opcodes/storage.ts +++ b/yarn-project/acir-simulator/src/avm/opcodes/storage.ts @@ -38,7 +38,7 @@ export class SLoad extends Instruction { static type: string = 'SLOAD'; static numberOfOperands = 2; - constructor(private slotOffset: number, private destOffset: number) { + constructor(private slotOffset: number, private dstOffset: number) { super(); } @@ -50,7 +50,7 @@ export class SLoad extends Instruction { new Fr(slot.toBigInt()), ); - machineState.memory.set(this.destOffset, new Field(data)); + machineState.memory.set(this.dstOffset, new Field(data)); this.incrementPc(machineState); }