Skip to content

Commit

Permalink
Merge pull request #270 from yusufidimaina9989/master
Browse files Browse the repository at this point in the history
add modEXP
  • Loading branch information
xhliu authored Aug 24, 2023
2 parents a76c8d3 + f4a2b2e commit 94610fc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 44 deletions.
43 changes: 43 additions & 0 deletions src/contracts/modEXP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { SmartContract, assert, method, prop } from "scrypt-ts";

export class ModExp extends SmartContract {
// max # of bits for e = ceil(log2(y))
static readonly N : bigint = 232n;
// modulus
@prop()
readonly M : bigint;

constructor(M : bigint){
super(...arguments)
this.M = M
}
// x^y % M
@method()
modExp(x : bigint, y : bigint) : bigint {
let res : bigint = 1n;
x = x % this.M;

if (x != 0n) {
for (let i = 0; i < this.M; i ++) {
if (y >= 0n) {
// If y is odd, multiply x with result
if (y % 2n) res = (res * x) % this.M;

// y >> 1
y = y / 2n;
x = (x * x) % this.M;
}
}
}
else {
res = 0n;
}

return res;
}

@method()
public main(x : bigint, y : bigint, z : bigint) {
assert(z == this.modExp(x, y));
}
}
4 changes: 2 additions & 2 deletions tests/numberGuess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ async function main() {
console.log('NumberGuess contract deployed: ', deployTx.id)

const { tx: callTx, atInputIndex } = await numbergs.methods.guess(
(sigResps) => findSig(sigResps, publicKeyalice && publicKeybob),
(sigResps) => findSig(sigResps, publicKeybob),
{
pubKeyOrAddrToSign: publicKeyalice && publicKeybob,
pubKeyOrAddrToSign: publicKeybob,
} as MethodCallOptions<NumberGuess>
)
console.log('NumberGuess contract called: ', callTx.id)
Expand Down
42 changes: 0 additions & 42 deletions tests/numbergs.test.ts

This file was deleted.

0 comments on commit 94610fc

Please sign in to comment.