Skip to content

Commit

Permalink
feat: add RANGE_CHECK builtins (#19)
Browse files Browse the repository at this point in the history
* added RANGE_CHECK function

* changed initialization format of RC_BOUND bigint

* add comment

* rebased latest updates, refacto, added custom function in Code.ts file
  • Loading branch information
KeneePatel authored Jun 3, 2024
1 parent f8f9ef7 commit 67ef53b
Showing 3 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/builtins/range_check/range_check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const RC_BOUND: bigint = BigInt(2 ** 128);

// performs a range check for the input num of type bigint.
function range_check(num: bigint): bigint {
if (num < RC_BOUND) {
return num;
} else {
throw new InvalidRangeError();
}
}
12 changes: 12 additions & 0 deletions src/ui/Code.ts
Original file line number Diff line number Diff line change
@@ -103,6 +103,7 @@ function BITWISE_XOR(x: number, y: number): bigint {
*/
function BITWISE_OR(x: number, y: number): bigint {
return bitwise_or(BigInt(x), BigInt(y));
}

function EC_OP(
m: number | string,
@@ -111,3 +112,14 @@ function EC_OP(
): AffinePoint {
return ec_op(BigInt(m), p, q);
}

/**
* Provides custom function for range checking a given input.
*
* @param {number} num - The number which is to be validated.
* @return The number itself in bigint form if it is in range, else throws InvalidRangeError.
* @customfunction
*/
function RANGE_CHECK(num: number): bigint {
return range_check(BigInt(num));
}
1 change: 1 addition & 0 deletions src/vm/errors.ts
Original file line number Diff line number Diff line change
@@ -6,4 +6,5 @@ class InvalidApUpdateError extends Error {}
class InvalidOp0RegisterError extends Error {}
class InvalidOp1RegisterError extends Error {}
class InvalidDstRegisterError extends Error {}
class InvalidRangeError extends Error {}
class AssertEqError extends Error {}

0 comments on commit 67ef53b

Please sign in to comment.