From aab77e8836dffdd5900a0c28b9a6e33b57b12b5c Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 25 Apr 2024 23:34:56 +0200 Subject: [PATCH 1/7] Some improvements on primitives --- bun.lockb | Bin 2061 -> 2061 bytes src/primitives/felt.ts | 28 ++++++++++------------------ src/primitives/relocatable.ts | 24 +++++++++++------------- src/vm/virtualMachine.ts | 22 +++++++++++----------- 4 files changed, 32 insertions(+), 42 deletions(-) diff --git a/bun.lockb b/bun.lockb index 86be7e67ac273e21e8e66a7a5eac5e2b1bc36d29..6ea7bb089d783d57b798a607e7d7b98df2f648d3 100755 GIT binary patch delta 62 zcmeAb=oOf7VzLC2#bgIYj?Em5zKo2Flf#(8f#fbOvM@jZ%jAd5{+k~# H>#_g<(})eD delta 138 zcmeAb=oOf7V)6|}o{48fcr}jQYu4prV^}c#Og7`juL+Ed43oo{!hz&EFv-Ro9>9PA u*+( Date: Thu, 25 Apr 2024 23:45:24 +0200 Subject: [PATCH 2/7] fmt --- src/primitives/relocatable.ts | 14 +++++++++++--- src/vm/virtualMachine.ts | 5 ++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/primitives/relocatable.ts b/src/primitives/relocatable.ts index cf5b55ca..abb68384 100644 --- a/src/primitives/relocatable.ts +++ b/src/primitives/relocatable.ts @@ -9,11 +9,15 @@ import { export type MaybeRelocatable = Relocatable | Felt; export namespace MaybeRelocatable { - export function isFelt(maybeRelocatable: MaybeRelocatable): maybeRelocatable is Felt { + export function isFelt( + maybeRelocatable: MaybeRelocatable + ): maybeRelocatable is Felt { return maybeRelocatable instanceof Felt; } - export function isRelocatable(maybeRelocatable: MaybeRelocatable): maybeRelocatable is Relocatable { + export function isRelocatable( + maybeRelocatable: MaybeRelocatable + ): maybeRelocatable is Relocatable { return maybeRelocatable instanceof Relocatable; } } @@ -80,7 +84,11 @@ export class Relocatable { } eq(other: MaybeRelocatable): boolean { - return !(MaybeRelocatable.isFelt(other)) && other.offset === this.offset && other.segment === this.segment; + return ( + !MaybeRelocatable.isFelt(other) && + other.offset === this.offset && + other.segment === this.segment + ); } toString(): string { diff --git a/src/vm/virtualMachine.ts b/src/vm/virtualMachine.ts index 9fe39d84..9d94438c 100644 --- a/src/vm/virtualMachine.ts +++ b/src/vm/virtualMachine.ts @@ -387,7 +387,10 @@ export class VirtualMachine { if (operands.dst === undefined) { throw new VirtualMachineError(InvalidDstOperand); } - if (MaybeRelocatable.isFelt(operands.dst) && operands.dst.eq(Felt.ZERO)) { + if ( + MaybeRelocatable.isFelt(operands.dst) && + operands.dst.eq(Felt.ZERO) + ) { this.incrementPc(instruction.size()); } else { if (operands.op1 === undefined) { From 7e1f3cd308e7c22fd841632db1b946a1c427a237 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 26 Apr 2024 12:42:50 +0200 Subject: [PATCH 3/7] fix comments --- src/primitives/felt.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/primitives/felt.ts b/src/primitives/felt.ts index d0bd6fc3..8b815a51 100644 --- a/src/primitives/felt.ts +++ b/src/primitives/felt.ts @@ -19,10 +19,10 @@ export class Felt { } add(other: MaybeRelocatable): Felt { - if (MaybeRelocatable.isFelt(other)) { - return new Felt((this.inner + other.inner) % Felt.PRIME); + if (!MaybeRelocatable.isFelt(other)) { + throw new PrimitiveError(ForbiddenOperation); } - throw new PrimitiveError(ForbiddenOperation); + return new Felt((this.inner + other.inner) % Felt.PRIME); } sub(other: MaybeRelocatable): Felt { @@ -38,10 +38,10 @@ export class Felt { } mul(other: MaybeRelocatable): Felt { - if (MaybeRelocatable.isFelt(other)) { - return new Felt((this.inner * other.inner) % Felt.PRIME); + if (!MaybeRelocatable.isFelt(other)) { + throw new PrimitiveError(ForbiddenOperation); } - throw new PrimitiveError(ForbiddenOperation); + return new Felt((this.inner * other.inner) % Felt.PRIME); } div(other: MaybeRelocatable): Felt { From 2edb690d83e1dd45981c45b41d1ea01b565ad28f Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 26 Apr 2024 18:51:01 +0200 Subject: [PATCH 4/7] fix comments --- src/memory/memory.ts | 3 ++- src/primitives/felt.ts | 13 ++++++------ src/primitives/maybeRelocatable.ts | 28 ++++++++++++++++++++++++ src/primitives/relocatable.ts | 27 ++++++------------------ src/vm/virtualMachine.ts | 34 ++++++++++++++++-------------- 5 files changed, 60 insertions(+), 45 deletions(-) create mode 100644 src/primitives/maybeRelocatable.ts diff --git a/src/memory/memory.ts b/src/memory/memory.ts index 845e3857..bc875b70 100644 --- a/src/memory/memory.ts +++ b/src/memory/memory.ts @@ -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>; diff --git a/src/primitives/felt.ts b/src/primitives/felt.ts index 8b815a51..f23c67f2 100644 --- a/src/primitives/felt.ts +++ b/src/primitives/felt.ts @@ -1,5 +1,4 @@ -import { MaybeRelocatable, Relocatable } from './relocatable'; - +import { MaybeRelocatable, isFelt, isRelocatable } from './maybeRelocatable'; import { ForbiddenOperation, OutOfRangeBigInt, @@ -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); } @@ -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 { diff --git a/src/primitives/maybeRelocatable.ts b/src/primitives/maybeRelocatable.ts new file mode 100644 index 00000000..739020ca --- /dev/null +++ b/src/primitives/maybeRelocatable.ts @@ -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 +): maybeRelocatable is Relocatable; +export function isRelocatable( + maybeRelocatable: MaybeRelocatable | number +): maybeRelocatable is Relocatable { + return maybeRelocatable instanceof Relocatable; +} diff --git a/src/primitives/relocatable.ts b/src/primitives/relocatable.ts index abb68384..0bdead8b 100644 --- a/src/primitives/relocatable.ts +++ b/src/primitives/relocatable.ts @@ -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; @@ -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); } @@ -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) { @@ -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); } @@ -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 ); diff --git a/src/vm/virtualMachine.ts b/src/vm/virtualMachine.ts index 9d94438c..163e6114 100644 --- a/src/vm/virtualMachine.ts +++ b/src/vm/virtualMachine.ts @@ -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 { @@ -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 @@ -95,7 +100,7 @@ export class VirtualMachine { throw new VirtualMachineError(Op0Undefined); } - if (!MaybeRelocatable.isRelocatable(op0)) { + if (!isRelocatable(op0)) { throw new VirtualMachineError(Op0NotRelocatable); } baseAddr = op0; @@ -111,7 +116,7 @@ export class VirtualMachine { throw new VirtualMachineError(EndOfInstructionsError); } - if (!(maybeEncodedInstruction instanceof Felt)) { + if (!isFelt(maybeEncodedInstruction)) { throw new VirtualMachineError(InstructionError); } @@ -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 @@ -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); @@ -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); @@ -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; @@ -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); @@ -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); @@ -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; @@ -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); } From 9e05a42651a68e9445ef70dcf276296398336899 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Mon, 29 Apr 2024 11:42:07 +0200 Subject: [PATCH 5/7] Update src/primitives/maybeRelocatable.ts Co-authored-by: Malatrax <71888134+zmalatrax@users.noreply.github.com> --- src/primitives/maybeRelocatable.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/primitives/maybeRelocatable.ts b/src/primitives/maybeRelocatable.ts index 739020ca..f34d85a7 100644 --- a/src/primitives/maybeRelocatable.ts +++ b/src/primitives/maybeRelocatable.ts @@ -19,7 +19,7 @@ export function isRelocatable( maybeRelocatable: MaybeRelocatable ): maybeRelocatable is Relocatable; export function isRelocatable( - maybeRelocatable: number | Relocatable + maybeRelocatable: Relocatable | number ): maybeRelocatable is Relocatable; export function isRelocatable( maybeRelocatable: MaybeRelocatable | number From 1f42f412617aea8aa03148f5d36da7725ad8ffa6 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Mon, 29 Apr 2024 12:10:44 +0200 Subject: [PATCH 6/7] Empty-Commit From 887024b03fbcea9920be1e8a78b66c3677217560 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Mon, 29 Apr 2024 12:33:41 +0200 Subject: [PATCH 7/7] Empty-Commit