Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(avm): implement comparator opcodes #4232

Merged
merged 4 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions yarn-project/acir-simulator/src/avm/avm_memory_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export abstract class MemoryValue {
public abstract mul(rhs: MemoryValue): MemoryValue;
public abstract div(rhs: MemoryValue): MemoryValue;

public abstract equals(rhs: MemoryValue): boolean;
public abstract lt(rhs: MemoryValue): boolean;

// We need this to be able to build an instance of the subclasses.
public abstract build(n: bigint): MemoryValue;

Expand Down Expand Up @@ -92,6 +95,16 @@ abstract class UnsignedInteger extends IntegralValue {
return this.build(~this.n & this.bitmask);
}

public equals(rhs: UnsignedInteger): boolean {
assert(this.bits == rhs.bits);
return this.n === rhs.n;
}

public lt(rhs: UnsignedInteger): boolean {
assert(this.bits == rhs.bits);
return this.n < rhs.n;
}

public toBigInt(): bigint {
return this.n;
}
Expand Down Expand Up @@ -176,6 +189,14 @@ export class Field extends MemoryValue {
return new Field(this.rep.div(rhs.rep));
}

public equals(rhs: Field): boolean {
return this.rep.equals(rhs.rep);
}

public lt(rhs: Field): boolean {
return this.rep.lt(rhs.rep);
}

public toBigInt(): bigint {
return this.rep.toBigInt();
}
Expand Down
147 changes: 147 additions & 0 deletions yarn-project/acir-simulator/src/avm/opcodes/comparators.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { MockProxy, mock } from 'jest-mock-extended';

import { AvmMachineState } from '../avm_machine_state.js';
import { Field, TypeTag, Uint16, Uint32 } from '../avm_memory_types.js';
import { initExecutionEnvironment } from '../fixtures/index.js';
import { AvmJournal } from '../journal/journal.js';
import { Eq, Lt, Lte } from './comparators.js';
import { InstructionExecutionError } from './instruction.js';

describe('Comparators', () => {
let machineState: AvmMachineState;
let journal: MockProxy<AvmJournal>;

beforeEach(async () => {
machineState = new AvmMachineState(initExecutionEnvironment());
journal = mock<AvmJournal>();
});

describe('Eq', () => {
it('Works on integral types', async () => {
machineState.memory.setSlice(0, [new Uint32(1), new Uint32(2), new Uint32(3), new Uint32(1)]);

[
new Eq(TypeTag.UINT32, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 10),
new Eq(TypeTag.UINT32, /*aOffset=*/ 0, /*bOffset=*/ 2, /*dstOffset=*/ 11),
new Eq(TypeTag.UINT32, /*aOffset=*/ 0, /*bOffset=*/ 3, /*dstOffset=*/ 12),
].forEach(i => i.execute(machineState, journal));

const actual = machineState.memory.getSlice(/*offset=*/ 10, /*size=*/ 4);
expect(actual).toEqual([new Uint32(0), new Uint32(0), new Uint32(1)]);
});

it('Works on field elements', async () => {
machineState.memory.setSlice(0, [new Field(1), new Field(2), new Field(3), new Field(1)]);

[
new Eq(TypeTag.FIELD, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 10),
new Eq(TypeTag.FIELD, /*aOffset=*/ 0, /*bOffset=*/ 2, /*dstOffset=*/ 11),
new Eq(TypeTag.FIELD, /*aOffset=*/ 0, /*bOffset=*/ 3, /*dstOffset=*/ 12),
].forEach(i => i.execute(machineState, journal));

const actual = machineState.memory.getSlice(/*offset=*/ 10, /*size=*/ 4);
expect(actual).toEqual([new Field(0), new Field(0), new Field(1)]);
});

it('InTag is checked', async () => {
machineState.memory.setSlice(0, [new Field(1), new Uint32(2), new Uint16(3)]);

const ops = [
new Eq(TypeTag.FIELD, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 10),
new Eq(TypeTag.UINT32, /*aOffset=*/ 0, /*bOffset=*/ 2, /*dstOffset=*/ 10),
new Eq(TypeTag.UINT16, /*aOffset=*/ 1, /*bOffset=*/ 2, /*dstOffset=*/ 10),
new Eq(TypeTag.UINT16, /*aOffset=*/ 1, /*bOffset=*/ 1, /*dstOffset=*/ 10),
];

for (const o of ops) {
await expect(() => o.execute(machineState, journal)).rejects.toThrow(InstructionExecutionError);
}
});
});

describe('Lt', () => {
it('Works on integral types', async () => {
machineState.memory.setSlice(0, [new Uint32(1), new Uint32(2), new Uint32(0)]);

[
new Lt(TypeTag.UINT32, /*aOffset=*/ 0, /*bOffset=*/ 0, /*dstOffset=*/ 10),
new Lt(TypeTag.UINT32, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 11),
new Lt(TypeTag.UINT32, /*aOffset=*/ 0, /*bOffset=*/ 2, /*dstOffset=*/ 12),
].forEach(i => i.execute(machineState, journal));

const actual = machineState.memory.getSlice(/*offset=*/ 10, /*size=*/ 4);
expect(actual).toEqual([new Uint32(0), new Uint32(1), new Uint32(0)]);
});

it('Works on field elements', async () => {
machineState.memory.setSlice(0, [new Field(1), new Field(2), new Field(0)]);

[
new Lt(TypeTag.FIELD, /*aOffset=*/ 0, /*bOffset=*/ 0, /*dstOffset=*/ 10),
new Lt(TypeTag.FIELD, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 11),
new Lt(TypeTag.FIELD, /*aOffset=*/ 0, /*bOffset=*/ 2, /*dstOffset=*/ 12),
].forEach(i => i.execute(machineState, journal));

const actual = machineState.memory.getSlice(/*offset=*/ 10, /*size=*/ 4);
expect(actual).toEqual([new Field(0), new Field(1), new Field(0)]);
});

it('InTag is checked', async () => {
machineState.memory.setSlice(0, [new Field(1), new Uint32(2), new Uint16(3)]);

const ops = [
new Lt(TypeTag.FIELD, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 10),
new Lt(TypeTag.UINT32, /*aOffset=*/ 0, /*bOffset=*/ 2, /*dstOffset=*/ 10),
new Lt(TypeTag.UINT16, /*aOffset=*/ 1, /*bOffset=*/ 2, /*dstOffset=*/ 10),
new Lt(TypeTag.UINT16, /*aOffset=*/ 1, /*bOffset=*/ 1, /*dstOffset=*/ 10),
];

for (const o of ops) {
await expect(() => o.execute(machineState, journal)).rejects.toThrow(InstructionExecutionError);
}
});
});

describe('Lte', () => {
it('Works on integral types', async () => {
machineState.memory.setSlice(0, [new Uint32(1), new Uint32(2), new Uint32(0)]);

[
new Lte(TypeTag.UINT32, /*aOffset=*/ 0, /*bOffset=*/ 0, /*dstOffset=*/ 10),
new Lte(TypeTag.UINT32, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 11),
new Lte(TypeTag.UINT32, /*aOffset=*/ 0, /*bOffset=*/ 2, /*dstOffset=*/ 12),
].forEach(i => i.execute(machineState, journal));

const actual = machineState.memory.getSlice(/*offset=*/ 10, /*size=*/ 4);
expect(actual).toEqual([new Uint32(1), new Uint32(1), new Uint32(0)]);
});

it('Works on field elements', async () => {
machineState.memory.setSlice(0, [new Field(1), new Field(2), new Field(0)]);

[
new Lte(TypeTag.FIELD, /*aOffset=*/ 0, /*bOffset=*/ 0, /*dstOffset=*/ 10),
new Lte(TypeTag.FIELD, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 11),
new Lte(TypeTag.FIELD, /*aOffset=*/ 0, /*bOffset=*/ 2, /*dstOffset=*/ 12),
].forEach(i => i.execute(machineState, journal));

const actual = machineState.memory.getSlice(/*offset=*/ 10, /*size=*/ 4);
expect(actual).toEqual([new Field(1), new Field(1), new Field(0)]);
});

it('InTag is checked', async () => {
machineState.memory.setSlice(0, [new Field(1), new Uint32(2), new Uint16(3)]);

const ops = [
new Lte(TypeTag.FIELD, /*aOffset=*/ 0, /*bOffset=*/ 1, /*dstOffset=*/ 10),
new Lte(TypeTag.UINT32, /*aOffset=*/ 0, /*bOffset=*/ 2, /*dstOffset=*/ 10),
new Lte(TypeTag.UINT16, /*aOffset=*/ 1, /*bOffset=*/ 2, /*dstOffset=*/ 10),
new Lte(TypeTag.UINT16, /*aOffset=*/ 1, /*bOffset=*/ 1, /*dstOffset=*/ 10),
];

for (const o of ops) {
await expect(() => o.execute(machineState, journal)).rejects.toThrow(InstructionExecutionError);
}
});
});
});
29 changes: 19 additions & 10 deletions yarn-project/acir-simulator/src/avm/opcodes/comparators.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { AvmMachineState } from '../avm_machine_state.js';
import { Field } from '../avm_memory_types.js';
import { TypeTag } from '../avm_memory_types.js';
import { AvmJournal } from '../journal/index.js';
import { Instruction } from './instruction.js';

export class Eq extends Instruction {
static type: string = 'EQ';
static numberOfOperands = 3;

constructor(private aOffset: number, private bOffset: number, private destOffset: number) {
constructor(private inTag: TypeTag, private aOffset: number, private bOffset: number, private dstOffset: number) {
super();
}

async execute(machineState: AvmMachineState, _journal: AvmJournal): Promise<void> {
Instruction.checkTags(machineState, this.inTag, this.aOffset, this.bOffset);

const a = machineState.memory.get(this.aOffset);
const b = machineState.memory.get(this.bOffset);

const dest = new Field(a.toBigInt() == b.toBigInt() ? 1 : 0);
machineState.memory.set(this.destOffset, dest);
// Result will be of the same type as 'a'.
const dest = a.build(a.equals(b) ? 1n : 0n);
machineState.memory.set(this.dstOffset, dest);

this.incrementPc(machineState);
}
Expand All @@ -26,16 +29,19 @@ export class Lt extends Instruction {
static type: string = 'Lt';
static numberOfOperands = 3;

constructor(private aOffset: number, private bOffset: number, private destOffset: number) {
constructor(private inTag: TypeTag, private aOffset: number, private bOffset: number, private dstOffset: number) {
super();
}

async execute(machineState: AvmMachineState, _journal: AvmJournal): Promise<void> {
Instruction.checkTags(machineState, this.inTag, this.aOffset, this.bOffset);

const a = machineState.memory.get(this.aOffset);
const b = machineState.memory.get(this.bOffset);

const dest = new Field(a.toBigInt() < b.toBigInt() ? 1 : 0);
machineState.memory.set(this.destOffset, dest);
// Result will be of the same type as 'a'.
const dest = a.build(a.lt(b) ? 1n : 0n);
machineState.memory.set(this.dstOffset, dest);

this.incrementPc(machineState);
}
Expand All @@ -45,16 +51,19 @@ export class Lte extends Instruction {
static type: string = 'LTE';
static numberOfOperands = 3;

constructor(private aOffset: number, private bOffset: number, private destOffset: number) {
constructor(private inTag: TypeTag, private aOffset: number, private bOffset: number, private dstOffset: number) {
super();
}

async execute(machineState: AvmMachineState, _journal: AvmJournal): Promise<void> {
Instruction.checkTags(machineState, this.inTag, this.aOffset, this.bOffset);

const a = machineState.memory.get(this.aOffset);
const b = machineState.memory.get(this.bOffset);

const dest = new Field(a.toBigInt() < b.toBigInt() ? 1 : 0);
machineState.memory.set(this.destOffset, dest);
// Result will be of the same type as 'a'.
const dest = a.build(a.equals(b) || a.lt(b) ? 1n : 0n);
machineState.memory.set(this.dstOffset, dest);

this.incrementPc(machineState);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ describe('Control Flow Opcodes', () => {
new Add(0, 1, 2),
new Sub(0, 1, 2),
new Mul(0, 1, 2),
new Lt(0, 1, 2),
new Lte(0, 1, 2),
new Eq(0, 1, 2),
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),
Expand Down
Loading