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

Some improvements on primitives #48

Merged
merged 8 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion src/memory/memory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MemoryError, WriteOnceError } from 'errors/memory';
import { SegmentError } from 'errors/primitives';
import { MaybeRelocatable, Relocatable } from 'primitives/relocatable';
import { MaybeRelocatable } from 'primitives/maybeRelocatable';
import { Relocatable } from 'primitives/relocatable';

export class Memory {
data: Array<Array<MaybeRelocatable>>;
Expand Down
13 changes: 6 additions & 7 deletions src/primitives/felt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { MaybeRelocatable, Relocatable } from './relocatable';

import { MaybeRelocatable, isFelt, isRelocatable } from './maybeRelocatable';
import {
ForbiddenOperation,
OutOfRangeBigInt,
Expand All @@ -19,14 +18,14 @@ export class Felt {
}

add(other: MaybeRelocatable): Felt {
if (!MaybeRelocatable.isFelt(other)) {
if (!isFelt(other)) {
throw new PrimitiveError(ForbiddenOperation);
}
return new Felt((this.inner + other.inner) % Felt.PRIME);
}

sub(other: MaybeRelocatable): Felt {
if (!MaybeRelocatable.isFelt(other)) {
if (!isFelt(other)) {
throw new PrimitiveError(ForbiddenOperation);
}

Expand All @@ -38,21 +37,21 @@ export class Felt {
}

mul(other: MaybeRelocatable): Felt {
if (!MaybeRelocatable.isFelt(other)) {
if (!isFelt(other)) {
throw new PrimitiveError(ForbiddenOperation);
}
return new Felt((this.inner * other.inner) % Felt.PRIME);
}

div(other: MaybeRelocatable): Felt {
if (!MaybeRelocatable.isFelt(other) || other.inner === 0n) {
if (!isFelt(other) || other.inner === 0n) {
throw new PrimitiveError(ForbiddenOperation);
}
return new Felt(this.inner / other.inner);
}

eq(other: MaybeRelocatable): boolean {
return !MaybeRelocatable.isRelocatable(other) && this.inner === other.inner;
return !isRelocatable(other) && this.inner === other.inner;
}

toString(): string {
Expand Down
28 changes: 28 additions & 0 deletions src/primitives/maybeRelocatable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Felt } from './felt';
import { Relocatable } from './relocatable';

export type MaybeRelocatable = Relocatable | Felt;

export function isFelt(
maybeRelocatable: MaybeRelocatable
): maybeRelocatable is Felt;
export function isFelt(
maybeRelocatable: MaybeRelocatable | number
): maybeRelocatable is Felt;
export function isFelt(
maybeRelocatable: MaybeRelocatable | number
): maybeRelocatable is Felt {
return maybeRelocatable instanceof Felt;
}

export function isRelocatable(
maybeRelocatable: MaybeRelocatable
): maybeRelocatable is Relocatable;
export function isRelocatable(
maybeRelocatable: number | Relocatable
tcoratger marked this conversation as resolved.
Show resolved Hide resolved
): maybeRelocatable is Relocatable;
export function isRelocatable(
maybeRelocatable: MaybeRelocatable | number
): maybeRelocatable is Relocatable {
return maybeRelocatable instanceof Relocatable;
}
27 changes: 6 additions & 21 deletions src/primitives/relocatable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,7 @@ import {
PrimitiveError,
SegmentError,
} from 'errors/primitives';

export type MaybeRelocatable = Relocatable | Felt;

export namespace MaybeRelocatable {
export function isFelt(
maybeRelocatable: MaybeRelocatable
): maybeRelocatable is Felt {
return maybeRelocatable instanceof Felt;
}

export function isRelocatable(
maybeRelocatable: MaybeRelocatable
): maybeRelocatable is Relocatable {
return maybeRelocatable instanceof Relocatable;
}
}
import { MaybeRelocatable, isFelt, isRelocatable } from './maybeRelocatable';

export class Relocatable {
segment: number;
Expand All @@ -36,14 +21,14 @@ export class Relocatable {
add(other: Relocatable): never;
add(other: MaybeRelocatable): MaybeRelocatable;
add(other: MaybeRelocatable | number): MaybeRelocatable {
if (other instanceof Felt) {
if (isFelt(other)) {
const offset = new Felt(BigInt(this.offset));
const newOffset = Number(offset.add(other));

return new Relocatable(this.segment, newOffset);
}

if (other instanceof Relocatable) {
if (isRelocatable(other)) {
throw new PrimitiveError(ForbiddenOperation);
}

Expand All @@ -55,7 +40,7 @@ export class Relocatable {
sub(other: Relocatable): Felt;
sub(other: MaybeRelocatable): MaybeRelocatable;
sub(other: MaybeRelocatable | number): MaybeRelocatable {
if (other instanceof Felt) {
if (isFelt(other)) {
const delta = Number(other);

if (this.offset < delta) {
Expand All @@ -64,7 +49,7 @@ export class Relocatable {
return new Relocatable(this.segment, this.offset - delta);
}

if (other instanceof Relocatable) {
if (isRelocatable(other)) {
if (this.offset < other.offset) {
throw new PrimitiveError(OffsetUnderflow);
}
Expand All @@ -85,7 +70,7 @@ export class Relocatable {

eq(other: MaybeRelocatable): boolean {
return (
!MaybeRelocatable.isFelt(other) &&
!isFelt(other) &&
other.offset === this.offset &&
other.segment === this.segment
);
Expand Down
34 changes: 18 additions & 16 deletions src/vm/virtualMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from 'errors/virtualMachine';
import { Felt } from 'primitives/felt';
import { Instruction, Opcode, PcUpdate, OpLogic } from './instruction';
import { MaybeRelocatable, Relocatable } from 'primitives/relocatable';
import { Relocatable } from 'primitives/relocatable';
import { InstructionError } from 'errors/memory';

import {
Expand All @@ -23,6 +23,11 @@ import { Memory } from 'memory/memory';
import { ProgramCounter, MemoryPointer } from 'primitives/relocatable';

import { Op1Source } from 'vm/instruction';
import {
MaybeRelocatable,
isFelt,
isRelocatable,
} from 'primitives/maybeRelocatable';

// operand 0 is the first operand in the right side of the computation
// operand 1 is the second operand in the right side of the computation
Expand Down Expand Up @@ -95,7 +100,7 @@ export class VirtualMachine {
throw new VirtualMachineError(Op0Undefined);
}

if (!MaybeRelocatable.isRelocatable(op0)) {
if (!isRelocatable(op0)) {
throw new VirtualMachineError(Op0NotRelocatable);
}
baseAddr = op0;
Expand All @@ -111,7 +116,7 @@ export class VirtualMachine {
throw new VirtualMachineError(EndOfInstructionsError);
}

if (!(maybeEncodedInstruction instanceof Felt)) {
if (!isFelt(maybeEncodedInstruction)) {
throw new VirtualMachineError(InstructionError);
}

Expand Down Expand Up @@ -246,7 +251,7 @@ export class VirtualMachine {
case 'op0 * op1':
if (dst !== undefined && op1 !== undefined) {
try {
if (!MaybeRelocatable.isFelt(dst)) {
if (!isFelt(dst)) {
throw new Error();
}
// op0 = res / op1
Expand Down Expand Up @@ -287,7 +292,7 @@ export class VirtualMachine {
// op1 = dst / op0
if (dst !== undefined && op0 !== undefined) {
try {
if (!MaybeRelocatable.isFelt(dst)) {
if (!isFelt(dst)) {
throw new Error();
}
return dst.div(op0);
Expand All @@ -313,7 +318,7 @@ export class VirtualMachine {
case 'op0 + op1':
return op0.add(op1);
case 'op0 * op1':
if (!MaybeRelocatable.isFelt(op0)) {
if (!isFelt(op0)) {
throw new VirtualMachineError(ExpectedFelt);
}
return op0.mul(op1);
Expand Down Expand Up @@ -363,7 +368,7 @@ export class VirtualMachine {
if (operands.res === undefined) {
throw new VirtualMachineError(UnconstrainedResError);
}
if (!MaybeRelocatable.isRelocatable(operands.res)) {
if (!isRelocatable(operands.res)) {
throw new VirtualMachineError(ExpectedRelocatable);
}
this.pc = operands.res;
Expand All @@ -375,7 +380,7 @@ export class VirtualMachine {
throw new VirtualMachineError(UnconstrainedResError);
}

if (!MaybeRelocatable.isFelt(operands.res)) {
if (!isFelt(operands.res)) {
throw new VirtualMachineError(ExpectedFelt);
}
this.pc = this.pc.add(operands.res);
Expand All @@ -387,16 +392,13 @@ export class VirtualMachine {
if (operands.dst === undefined) {
throw new VirtualMachineError(InvalidDstOperand);
}
if (
MaybeRelocatable.isFelt(operands.dst) &&
operands.dst.eq(Felt.ZERO)
) {
if (isFelt(operands.dst) && operands.dst.eq(Felt.ZERO)) {
this.incrementPc(instruction.size());
} else {
if (operands.op1 === undefined) {
throw new VirtualMachineError(InvalidOperand1);
}
if (!MaybeRelocatable.isFelt(operands.op1)) {
if (!isFelt(operands.op1)) {
throw new VirtualMachineError(ExpectedFelt);
}
this.pc = this.pc.add(operands.op1);
Expand All @@ -419,10 +421,10 @@ export class VirtualMachine {
if (operands.dst === undefined) {
throw new VirtualMachineError(InvalidDstOperand);
}
if (MaybeRelocatable.isFelt(operands.dst)) {
if (isFelt(operands.dst)) {
this.fp = this.fp.add(operands.dst);
}
if (MaybeRelocatable.isRelocatable(operands.dst)) {
if (isRelocatable(operands.dst)) {
this.fp = operands.dst;
}
break;
Expand All @@ -437,7 +439,7 @@ export class VirtualMachine {
if (operands.res === undefined) {
throw new VirtualMachineError(UnconstrainedResError);
}
if (!MaybeRelocatable.isFelt(operands.res)) {
if (!isFelt(operands.res)) {
throw new VirtualMachineError(ExpectedFelt);
}

Expand Down
Loading