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

dev: remove custom integer classes #47

Merged
merged 10 commits into from
Apr 25, 2024
16 changes: 7 additions & 9 deletions src/primitives/felt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ describe('Felt', () => {
});

describe('conversions', () => {
test('should convert correctly a felt to a number if inner is below Javascript max safe integer', () => {
test('should convert correctly a felt to a bigint', () => {
const felt = new Felt(10n);
const result = felt.toUint64();
const result = felt.toBigInt();
expect(result).toEqual(10n);
});
test('should convert correctly a felt to a number', () => {
const felt = new Felt(10n);
const result = Number(felt);
expect(result).toEqual(10);
});
test('should convert correctly a felt to its string representation', () => {
const felt = new Felt(10n);
expect(felt.toString()).toEqual('10');
Expand Down Expand Up @@ -116,11 +121,4 @@ describe('Felt', () => {
expect(result.eq(expected)).toBeTrue();
});
});

describe('toUint53', () => {
test('should throw an error if the felt is larger than the max safe integer', () => {
const a = new Felt(2n ** 53n);
expect(() => a.toUint53()).toThrow(new PrimitiveError(OutOfRangeBigInt));
});
});
});
12 changes: 1 addition & 11 deletions src/primitives/felt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { UnsignedInteger } from './uint';
import { MaybeRelocatable, Relocatable } from './relocatable';

import {
Expand Down Expand Up @@ -64,16 +63,7 @@ export class Felt {
return this.inner.toString();
}

toUint53(): number {
if (this.inner > Number.MAX_SAFE_INTEGER) {
throw new PrimitiveError(OutOfRangeBigInt);
}

return Number(this.inner);
}

toUint64(): bigint {
UnsignedInteger.ensureUint64(this.inner);
toBigInt(): bigint {
return this.inner;
}

Expand Down
29 changes: 0 additions & 29 deletions src/primitives/int.test.ts

This file was deleted.

36 changes: 0 additions & 36 deletions src/primitives/int.ts

This file was deleted.

7 changes: 2 additions & 5 deletions src/primitives/relocatable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Felt } from './felt';
import { UnsignedInteger } from './uint';
import {
ForbiddenOperation,
OffsetUnderflow,
Expand All @@ -14,8 +13,6 @@ export class Relocatable {
offset: number;

constructor(segment: number, offset: number) {
UnsignedInteger.ensureUint53(segment);
UnsignedInteger.ensureUint53(offset);
this.segment = segment;
this.offset = offset;
}
Expand All @@ -27,7 +24,7 @@ export class Relocatable {
add(other: MaybeRelocatable | number): MaybeRelocatable {
if (other instanceof Felt) {
const offset = new Felt(BigInt(this.offset));
const newOffset = offset.add(other).toUint53();
const newOffset = Number(offset.add(other));

return new Relocatable(this.segment, newOffset);
}
Expand All @@ -45,7 +42,7 @@ export class Relocatable {
sub(other: MaybeRelocatable): MaybeRelocatable;
sub(other: MaybeRelocatable | number): MaybeRelocatable {
if (other instanceof Felt) {
const delta = other.toUint53();
const delta = Number(other);

if (this.offset < delta) {
throw new PrimitiveError(OffsetUnderflow);
Expand Down
97 changes: 0 additions & 97 deletions src/primitives/uint.test.ts

This file was deleted.

62 changes: 0 additions & 62 deletions src/primitives/uint.ts

This file was deleted.

30 changes: 13 additions & 17 deletions src/vm/instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {
InvalidPcUpdate,
InvalidOpLogic,
} from 'errors/instruction';
import { SignedInteger16 } from 'primitives/int';
import { UnsignedInteger } from 'primitives/uint';

// Structure of the 63-bit that form the first word of each instruction.
// See Cairo whitepaper, page 32 - https://eprint.iacr.org/2021/1063.pdf.
Expand Down Expand Up @@ -73,6 +71,12 @@ export class Instruction {
// The opcode
public opcode: Opcode;

/** Value added to the offsets on encoded instructions
*
* offset = encodedOffset - BIAS
*/
static readonly BIAS = BigInt(2 ** 15);

static default(): Instruction {
return new Instruction(
0,
Expand Down Expand Up @@ -102,11 +106,6 @@ export class Instruction {
fpUpdate: FpUpdate,
opcode: Opcode
) {
// Check that the offsets are 16-bit signed integers
SignedInteger16.ensureInt16(dstOffset);
SignedInteger16.ensureInt16(op0Offset);
SignedInteger16.ensureInt16(op1Offset);

this.dstOffset = dstOffset;
this.op0Offset = op0Offset;
this.op1Offset = op1Offset;
Expand All @@ -120,10 +119,11 @@ export class Instruction {
this.opcode = opcode;
}

static decodeInstruction(encodedInstruction: bigint): Instruction {
// Check that the encoded instruction fits in a 64-bit unsigned integer
UnsignedInteger.ensureUint64(encodedInstruction);
static fromBiased(value: bigint): number {
return Number(value - this.BIAS);
}

static decodeInstruction(encodedInstruction: bigint): Instruction {
// INVARIANT: The high bit of the encoded instruction must be 0
const highBit = 1n << 63n;
if ((highBit & encodedInstruction) !== 0n) {
Expand All @@ -138,17 +138,13 @@ export class Instruction {
const shift = 16n;

// Get the offset by masking and shifting the encoded instruction
const dstOffset = SignedInteger16.fromBiased(encodedInstruction & mask);
const dstOffset = this.fromBiased(encodedInstruction & mask);

let shiftedEncodedInstruction = encodedInstruction >> shift;
const op0Offset = SignedInteger16.fromBiased(
shiftedEncodedInstruction & mask
);
const op0Offset = this.fromBiased(shiftedEncodedInstruction & mask);

shiftedEncodedInstruction = shiftedEncodedInstruction >> shift;
const op1Offset = SignedInteger16.fromBiased(
shiftedEncodedInstruction & mask
);
const op1Offset = this.fromBiased(shiftedEncodedInstruction & mask);

// Get the flags by shifting the encoded instruction
const flags = shiftedEncodedInstruction >> shift;
Expand Down
Loading
Loading