-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
31 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters