diff --git a/yarn-project/simulator/src/avm/opcodes/arithmetic.test.ts b/yarn-project/simulator/src/avm/opcodes/arithmetic.test.ts index 21d0ca370866..022957eb2d2a 100644 --- a/yarn-project/simulator/src/avm/opcodes/arithmetic.test.ts +++ b/yarn-project/simulator/src/avm/opcodes/arithmetic.test.ts @@ -231,7 +231,6 @@ describe('Arithmetic Instructions', () => { }); describe.each([ - [new Field(200n), new Field(99n), new Field(2n), TypeTag.FIELD], [new Uint8(200n), new Uint8(99n), new Uint8(2n), TypeTag.UINT8], [new Uint16(200n), new Uint16(99n), new Uint16(2n), TypeTag.UINT16], [new Uint32(200n), new Uint32(99n), new Uint32(2n), TypeTag.UINT32], diff --git a/yarn-project/simulator/src/avm/opcodes/arithmetic.ts b/yarn-project/simulator/src/avm/opcodes/arithmetic.ts index 30a1ead0efa9..0b5c88a92cb3 100644 --- a/yarn-project/simulator/src/avm/opcodes/arithmetic.ts +++ b/yarn-project/simulator/src/avm/opcodes/arithmetic.ts @@ -1,5 +1,11 @@ import type { AvmContext } from '../avm_context.js'; -import { type Field, type MemoryValue } from '../avm_memory_types.js'; +import { + type Field, + type MemoryValue, + TaggedMemory, + type TaggedMemoryInterface, + TypeTag, +} from '../avm_memory_types.js'; import { ArithmeticError } from '../errors.js'; import { Opcode } from '../serialization/instruction_serialization.js'; import { Addressing } from './addressing_mode.js'; @@ -13,7 +19,7 @@ export abstract class ThreeOperandArithmeticInstruction extends ThreeOperandInst const operands = [this.aOffset, this.bOffset, this.dstOffset]; const addressing = Addressing.fromWire(this.indirect, operands.length); const [aOffset, bOffset, dstOffset] = addressing.resolve(operands, memory); - memory.checkTagsAreSame(aOffset, bOffset); + this.checkTags(memory, aOffset, bOffset); const a = memory.get(aOffset); const b = memory.get(bOffset); @@ -25,6 +31,9 @@ export abstract class ThreeOperandArithmeticInstruction extends ThreeOperandInst } protected abstract compute(a: MemoryValue, b: MemoryValue): MemoryValue; + protected checkTags(memory: TaggedMemoryInterface, aOffset: number, bOffset: number) { + memory.checkTagsAreSame(aOffset, bOffset); + } } export class Add extends ThreeOperandArithmeticInstruction { @@ -65,6 +74,11 @@ export class Div extends ThreeOperandArithmeticInstruction { return a.div(b); } + + protected override checkTags(memory: TaggedMemoryInterface, aOffset: number, bOffset: number) { + memory.checkTagsAreSame(aOffset, bOffset); + TaggedMemory.checkIsIntegralTag(memory.getTag(aOffset)); // Follows that bOffset tag is also of integral type + } } export class FieldDiv extends ThreeOperandArithmeticInstruction { @@ -75,4 +89,9 @@ export class FieldDiv extends ThreeOperandArithmeticInstruction { // return (a as Field).fdiv(b as Field); return a.fdiv(b); } + + protected override checkTags(memory: TaggedMemoryInterface, aOffset: number, bOffset: number) { + memory.checkTagsAreSame(aOffset, bOffset); + memory.checkTag(TypeTag.FIELD, aOffset); // Follows that bOffset has also tag of type Field + } }