Skip to content

Commit

Permalink
Fix slice
Browse files Browse the repository at this point in the history
  • Loading branch information
hh committed Jan 30, 2024
1 parent 170d340 commit 971fa7d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/arrayUtils.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
7 changes: 4 additions & 3 deletions src/ec/secp256k1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Utils,
reverseByteString,
ByteString,
slice,
} from 'scrypt-ts'

import { Point, Signature } from './misc'
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions src/ec/secp256r1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Utils,
reverseByteString,
ByteString,
slice,
} from 'scrypt-ts'

import { Point, Signature } from './misc'
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions src/schnorr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
sha256,
SmartContractLib,
toByteString,
slice,
} from 'scrypt-ts'
import { Point } from './ec/misc'
import { SECP256K1 } from './ec/secp256k1'
Expand All @@ -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)
Expand Down

0 comments on commit 971fa7d

Please sign in to comment.