Skip to content

Commit

Permalink
feat: add bitwise builtins (#21)
Browse files Browse the repository at this point in the history
* added bitwise builtin functions

* added function output types

* updated bitwise, introduced custom functions

* updated custom function to take two inputs

* Update src/builtins/bitwise/bitwise.ts

---------

Co-authored-by: Clément Walter <clement0walter@gmail.com>
  • Loading branch information
KeneePatel and ClementWalter authored Jun 3, 2024
1 parent 14657ff commit f8f9ef7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/builtins/bitwise/bitwise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// calculates bitwise 'AND' of given two inputs x and y
function bitwise_and(x: bigint, y: bigint): bigint {
return x & y;
}

// calculates bitwise 'OR' of given two inputs x and y
function bitwise_or(x: bigint, y: bigint): bigint {
return x | y;
}

// calculates bitwise 'XOR' of given two inputs x and y
function bitwise_xor(x: bigint, y: bigint): bigint {
return x ^ y;
}

16 changes: 16 additions & 0 deletions src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,19 @@ function testRunner() {

test.finish();
}

function testBitwiseOperations(): void {
Logger.log(bitwise_operations(BigInt(0), BigInt(0)).toString());
Logger.log(bitwise_operations(BigInt(31), BigInt(0)).toString());
Logger.log(bitwise_operations(BigInt(0), BigInt(31)).toString());
Logger.log(bitwise_operations(BigInt(31), BigInt(31)).toString());
Logger.log(bitwise_operations(BigInt(31), BigInt(1)).toString());
Logger.log(bitwise_operations(BigInt(31), BigInt(30)).toString());
//Expect :
// [0, 0, 0]
// [0, 31, 31]
// [0, 31, 31]
// [31, 0, 31]
// [1, 30, 31]
// [30, 1, 31]
}
35 changes: 35 additions & 0 deletions src/ui/Code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,41 @@ function PEDERSEN(x: number | string, y: number | string): number | string {
return pedersen(BigInt(x), BigInt(y)).toString(16);
}

/**
* Provides custom function for bitwise 'and' for given two inputs.
*
* @param {number} x - one of the input for 'and' operation
* @param {number} y - the other input for 'and' operation
* @return The bitwise 'and' of two given inputs in bigint form.
* @customfunction
*/
function BITWISE_AND(x: number, y: number): bigint {
return bitwise_and(BigInt(x), BigInt(y));
}

/**
* Provides custom function for bitwise 'xor' for given two inputs.
*
* @param {number} x - one of the input for 'xor' operation
* @param {number} y - the other input for 'xor' operation
* @return The bitwise 'xor' of two given inputs in bigint form.
* @customfunction
*/
function BITWISE_XOR(x: number, y: number): bigint {
return bitwise_xor(BigInt(x), BigInt(y));
}

/**
* Provides custom function for bitwise 'or' for given two inputs.
*
* @param {number} x - one of the input for 'or' operation
* @param {number} y - the other input for 'or' operation
* @return The bitwise 'or' of two given inputs in bigint form.
* @customfunction
*/
function BITWISE_OR(x: number, y: number): bigint {
return bitwise_or(BigInt(x), BigInt(y));

function EC_OP(
m: number | string,
p: AffinePoint,
Expand Down
1 change: 0 additions & 1 deletion src/vm/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ function runUntilPc(): void {
runSheet.getRange(`${executionColumn}2`).setValue(FINAL_FP);
runSheet.getRange(`${executionColumn}3`).setValue(FINAL_PC);
while (!(pc === FINAL_PC)) {
console.log(i);
step(i);
i++;
pc = runSheet.getRange(`${pcColumn}${i + 1 + 1}`).getValue();
Expand Down

0 comments on commit f8f9ef7

Please sign in to comment.