Skip to content

Commit 59389fb

Browse files
jeanmonstevenplatt
authored andcommitted
feat(avm): Simulator enforces integral tag for DIV and field tag for FDIV (#9944)
1 parent 081c1a1 commit 59389fb

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

yarn-project/simulator/src/avm/opcodes/arithmetic.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ describe('Arithmetic Instructions', () => {
231231
});
232232

233233
describe.each([
234-
[new Field(200n), new Field(99n), new Field(2n), TypeTag.FIELD],
235234
[new Uint8(200n), new Uint8(99n), new Uint8(2n), TypeTag.UINT8],
236235
[new Uint16(200n), new Uint16(99n), new Uint16(2n), TypeTag.UINT16],
237236
[new Uint32(200n), new Uint32(99n), new Uint32(2n), TypeTag.UINT32],

yarn-project/simulator/src/avm/opcodes/arithmetic.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import type { AvmContext } from '../avm_context.js';
2-
import { type Field, type MemoryValue } from '../avm_memory_types.js';
2+
import {
3+
type Field,
4+
type MemoryValue,
5+
TaggedMemory,
6+
type TaggedMemoryInterface,
7+
TypeTag,
8+
} from '../avm_memory_types.js';
39
import { ArithmeticError } from '../errors.js';
410
import { Opcode } from '../serialization/instruction_serialization.js';
511
import { Addressing } from './addressing_mode.js';
@@ -13,7 +19,7 @@ export abstract class ThreeOperandArithmeticInstruction extends ThreeOperandInst
1319
const operands = [this.aOffset, this.bOffset, this.dstOffset];
1420
const addressing = Addressing.fromWire(this.indirect, operands.length);
1521
const [aOffset, bOffset, dstOffset] = addressing.resolve(operands, memory);
16-
memory.checkTagsAreSame(aOffset, bOffset);
22+
this.checkTags(memory, aOffset, bOffset);
1723

1824
const a = memory.get(aOffset);
1925
const b = memory.get(bOffset);
@@ -25,6 +31,9 @@ export abstract class ThreeOperandArithmeticInstruction extends ThreeOperandInst
2531
}
2632

2733
protected abstract compute(a: MemoryValue, b: MemoryValue): MemoryValue;
34+
protected checkTags(memory: TaggedMemoryInterface, aOffset: number, bOffset: number) {
35+
memory.checkTagsAreSame(aOffset, bOffset);
36+
}
2837
}
2938

3039
export class Add extends ThreeOperandArithmeticInstruction {
@@ -65,6 +74,11 @@ export class Div extends ThreeOperandArithmeticInstruction {
6574

6675
return a.div(b);
6776
}
77+
78+
protected override checkTags(memory: TaggedMemoryInterface, aOffset: number, bOffset: number) {
79+
memory.checkTagsAreSame(aOffset, bOffset);
80+
TaggedMemory.checkIsIntegralTag(memory.getTag(aOffset)); // Follows that bOffset tag is also of integral type
81+
}
6882
}
6983

7084
export class FieldDiv extends ThreeOperandArithmeticInstruction {
@@ -75,4 +89,9 @@ export class FieldDiv extends ThreeOperandArithmeticInstruction {
7589
// return (a as Field).fdiv(b as Field);
7690
return a.fdiv(b);
7791
}
92+
93+
protected override checkTags(memory: TaggedMemoryInterface, aOffset: number, bOffset: number) {
94+
memory.checkTagsAreSame(aOffset, bOffset);
95+
memory.checkTag(TypeTag.FIELD, aOffset); // Follows that bOffset has also tag of type Field
96+
}
7897
}

0 commit comments

Comments
 (0)