From 971fa7dd4aa1ee6ecf251895f717cfee5534f9c6 Mon Sep 17 00:00:00 2001 From: hh Date: Tue, 30 Jan 2024 10:50:55 +0800 Subject: [PATCH] Fix slice --- src/arrayUtils.ts | 6 +++--- src/ec/secp256k1.ts | 7 ++++--- src/ec/secp256r1.ts | 7 ++++--- src/schnorr.ts | 5 +++-- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/arrayUtils.ts b/src/arrayUtils.ts index eb9eeef..a01acc3 100644 --- a/src/arrayUtils.ts +++ b/src/arrayUtils.ts @@ -1,16 +1,16 @@ -import { method, SmartContractLib, ByteString } from 'scrypt-ts' +import { method, SmartContractLib, ByteString, slice } from 'scrypt-ts' // A library that emulates an array interface on top of a ByteString. export class ArrayUtils extends SmartContractLib { // Get the byte at the given index. @method() static getElemAt(b: ByteString, idx: bigint): ByteString { - return b.slice(Number(idx) * 2, Number(idx) * 2 + 2) + return slice(b, idx, idx + 1n) } // Set the byte at the given index. @method() static setElemAt(b: ByteString, idx: bigint, val: ByteString): ByteString { - return b.slice(0, Number(idx) * 2) + val + b.slice(Number(idx) * 2 + 2) + return slice(b, 0n, idx) + val + slice(b, idx + 1n) } } diff --git a/src/ec/secp256k1.ts b/src/ec/secp256k1.ts index 21685fc..e32a239 100644 --- a/src/ec/secp256k1.ts +++ b/src/ec/secp256k1.ts @@ -11,6 +11,7 @@ import { Utils, reverseByteString, ByteString, + slice, } from 'scrypt-ts' import { Point, Signature } from './misc' @@ -1244,15 +1245,15 @@ export class SECP256K1 extends SmartContractLib { @method() static pubKey2Point(pubKey: PubKey): Point { assert( - pubKey.slice(0, 2) == toByteString('04'), + slice(pubKey, 0n, 1n) == toByteString('04'), 'Pub key isn\'t prefixed with "04". This likely means, that it\'s not in compressed form.' ) // Convert signed little endian to unsigned big endian. const x = Utils.fromLEUnsigned( - reverseByteString(pubKey.slice(2, 66), 32n) + reverseByteString(slice(pubKey, 1n, 33n), 32n) ) const y = Utils.fromLEUnsigned( - reverseByteString(pubKey.slice(66, 130), 32n) + reverseByteString(slice(pubKey, 33n, 65n), 32n) ) return { x: x, diff --git a/src/ec/secp256r1.ts b/src/ec/secp256r1.ts index 9c7c63c..d80e8d3 100644 --- a/src/ec/secp256r1.ts +++ b/src/ec/secp256r1.ts @@ -11,6 +11,7 @@ import { Utils, reverseByteString, ByteString, + slice, } from 'scrypt-ts' import { Point, Signature } from './misc' @@ -1247,15 +1248,15 @@ export class SECP256R1 extends SmartContractLib { @method() static pubKey2Point(pubKey: PubKey): Point { assert( - pubKey.slice(0, 2) == toByteString('04'), + slice(pubKey, 0n, 1n) == toByteString('04'), 'Pub key isn\'t prefixed with "04". This likely means, that it\'s not in compressed form.' ) // Convert signed little endian to unsigned big endian. const x = Utils.fromLEUnsigned( - reverseByteString(pubKey.slice(2, 66), 32n) + reverseByteString(slice(pubKey, 1n, 33n), 32n) ) const y = Utils.fromLEUnsigned( - reverseByteString(pubKey.slice(66, 130), 32n) + reverseByteString(slice(pubKey, 33n, 65n), 32n) ) return { x: x, diff --git a/src/schnorr.ts b/src/schnorr.ts index f6c9dee..4e3d38e 100644 --- a/src/schnorr.ts +++ b/src/schnorr.ts @@ -7,6 +7,7 @@ import { sha256, SmartContractLib, toByteString, + slice, } from 'scrypt-ts' import { Point } from './ec/misc' import { SECP256K1 } from './ec/secp256k1' @@ -21,9 +22,9 @@ export class Schnorr extends SmartContractLib { msg: ByteString, R: Point ): boolean { - const r: ByteString = sig.slice(0, 64) // First 32 bytes + const r: ByteString = slice(sig, 0n, 64n) // First 32 bytes const s = byteString2Int( - reverseByteString(sig.slice(64, 128), 32n) + toByteString('00') + reverseByteString(slice(sig, 32n, 64n), 32n) + toByteString('00') ) // e = Hash(r || P || msg)