From be554eb3d9928f4a42b2bb5a368d194f3e22f6a8 Mon Sep 17 00:00:00 2001 From: hh Date: Wed, 24 Jan 2024 20:46:35 +0800 Subject: [PATCH] Fix pow error --- src/contracts/shift.ts | 47 +++++++++++++++++++++--------------------- tests/shift.test.ts | 20 +++++++----------- 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/contracts/shift.ts b/src/contracts/shift.ts index 82288510..1af05081 100644 --- a/src/contracts/shift.ts +++ b/src/contracts/shift.ts @@ -1,47 +1,48 @@ -import { SmartContractLib, byteString2Int, int2ByteString, method, assert, SmartContract} from "scrypt-ts"; +import { + SmartContractLib, + byteString2Int, + int2ByteString, + method, + assert, + SmartContract, + toByteString, + lshift, +} from 'scrypt-ts' // -export class Shift extends SmartContractLib{ - +export class Shift extends SmartContractLib { // return 2^n @method() - static pow2 (n : bigint) : bigint { - - return byteString2Int( - int2ByteString(0n, n/8n) - ); + static pow2(n: bigint): bigint { + return lshift(1n, n) } // binary left shift number x by n places @method() - static left (x : bigint, n : bigint) : bigint { - - return x * Shift.pow2(n); + static left(x: bigint, n: bigint): bigint { + return x * Shift.pow2(n) } // binary right shift number x by n places @method() - static right (x : bigint, n : bigint) : bigint { - - return x / Shift.pow2(n); + static right(x: bigint, n: bigint): bigint { + return x / Shift.pow2(n) } - } -export class ShiftTest extends SmartContract{ - +export class ShiftTest extends SmartContract { @method() - public pow2(n : bigint, x : bigint){ - assert(Shift.pow2(n) == x) + public pow2(n: bigint, x: bigint) { + assert(Shift.pow2(n) == x, 'pow2 method failed') } @method() - public left(x : bigint, y : bigint, z : bigint){ - assert(Shift.left(x,y) == z) + public left(x: bigint, y: bigint, z: bigint) { + assert(Shift.left(x, y) == z, 'left method failed') } @method() - public right(x : bigint, y : bigint, z : bigint){ - assert(Shift.right(x, y) == z) + public right(x: bigint, y: bigint, z: bigint) { + assert(Shift.right(x, y) == z, 'right method failed') } } diff --git a/tests/shift.test.ts b/tests/shift.test.ts index c8af280a..5e193a9f 100644 --- a/tests/shift.test.ts +++ b/tests/shift.test.ts @@ -1,4 +1,3 @@ -import { FixedArray } from 'scrypt-ts' import { ShiftTest } from '../src/contracts/shift' import { getDefaultSigner } from './utils/helper' import { expect, use } from 'chai' @@ -18,8 +17,8 @@ describe('Test SmartContract `ShiftTest`', () => { const callContract = async () => { await instance.methods.pow2(3n, 8n) - return expect(callContract()).not.be.rejected } + return expect(callContract()).not.be.rejected }) it('left should binary left shift number x by n places', async () => { @@ -27,8 +26,8 @@ describe('Test SmartContract `ShiftTest`', () => { const callContract = async () => { await instance.methods.left(2n, 2n, 8n) - return expect(callContract()).not.be.rejected } + return expect(callContract()).not.be.rejected }) it('right should binary right shift number x by n places', async () => { @@ -36,8 +35,8 @@ describe('Test SmartContract `ShiftTest`', () => { const callContract = async () => { await instance.methods.right(8n, 2n, 2n) - return expect(callContract()).not.be.rejected } + return expect(callContract()).not.be.rejected }) it('should throw when calling pow2 method ', async () => { @@ -45,10 +44,8 @@ describe('Test SmartContract `ShiftTest`', () => { const callContract = async () => { await instance.methods.pow2(3n, 0n) - return expect(callContract()).to.be.rejectedWith( - /pow2 method failed/ - ) } + return expect(callContract()).to.be.rejectedWith(/pow2 method failed/) }) it('should throw when calling left method ', async () => { @@ -56,10 +53,9 @@ describe('Test SmartContract `ShiftTest`', () => { const callContract = async () => { await instance.methods.left(3n, 0n, 0n) - return expect(callContract()).to.be.rejectedWith( - /left method failed/ - ) } + + return expect(callContract()).to.be.rejectedWith(/left method failed/) }) it('should throw when calling right method ', async () => { @@ -67,9 +63,7 @@ describe('Test SmartContract `ShiftTest`', () => { const callContract = async () => { await instance.methods.right(3n, 0n, 0n) - return expect(callContract()).to.be.rejectedWith( - /right method failed/ - ) } + return expect(callContract()).to.be.rejectedWith(/right method failed/) }) })