From 5175e9813f3875436236bcf45fdf27ad5cd09740 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Wed, 15 Jun 2022 14:31:45 +0400 Subject: [PATCH 1/2] Small refactor: compat, scrypt --- src/scrypt.ts | 8 ++++---- src/secp256k1-compat.ts | 34 +++++++++++++++------------------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/scrypt.ts b/src/scrypt.ts index 9049fbb..484797d 100644 --- a/src/scrypt.ts +++ b/src/scrypt.ts @@ -10,11 +10,11 @@ export async function scrypt( n: number, p: number, r: number, - dklen: number + dkLen: number ): Promise { assertBytes(password); assertBytes(salt); - return _scryptAsync(password, salt, { N: n, r, p, dkLen: dklen }); + return _scryptAsync(password, salt, { N: n, r, p, dkLen }); } export function scryptSync( @@ -23,9 +23,9 @@ export function scryptSync( n: number, p: number, r: number, - dklen: number + dkLen: number ): Uint8Array { assertBytes(password); assertBytes(salt); - return _scrypt(password, salt, { N: n, r, p, dkLen: dklen }); + return _scrypt(password, salt, { N: n, r, p, dkLen }); } diff --git a/src/secp256k1-compat.ts b/src/secp256k1-compat.ts index d01bd34..8704bbf 100644 --- a/src/secp256k1-compat.ts +++ b/src/secp256k1-compat.ts @@ -5,12 +5,6 @@ import { assertBool, assertBytes, hexToBytes, toHex } from "./utils"; // Use `secp256k1` module directly. // This is a legacy compatibility layer for the npm package `secp256k1` via noble-secp256k1 -// Copy-paste from secp256k1, maybe export it? -const bytesToNumber = (bytes: Uint8Array) => hexToNumber(toHex(bytes)); -const numberToHex = (num: number | bigint) => - num.toString(16).padStart(64, "0"); -const numberToBytes = (num: number | bigint) => hexToBytes(numberToHex(num)); - function hexToNumber(hex: string): bigint { if (typeof hex !== "string") { throw new TypeError("hexToNumber: expected string, got " + typeof hex); @@ -18,11 +12,13 @@ function hexToNumber(hex: string): bigint { return BigInt(`0x${hex}`); } -// Calculates a modulo b -function mod(a: bigint, b: bigint = secp.CURVE.P): bigint { - const result = a % b; - return result >= 0 ? result : b + result; -} +// Copy-paste from secp256k1, maybe export it? +const bytesToNumber = (bytes: Uint8Array) => hexToNumber(toHex(bytes)); +const numberToHex = (num: number | bigint) => + num.toString(16).padStart(64, "0"); +const numberToBytes = (num: number | bigint) => hexToBytes(numberToHex(num)); +const { mod } = secp.utils; + const ORDER = secp.CURVE.n; type Output = Uint8Array | ((len: number) => Uint8Array); @@ -165,23 +161,23 @@ export function privateKeyTweakAdd( ): Uint8Array { assertBytes(privateKey, 32); assertBytes(tweak, 32); - let bn = bytesToNumber(tweak); - if (bn === 0n) { + let t = bytesToNumber(tweak); + if (t === 0n) { throw new Error("Tweak must not be zero"); } - if (bn >= ORDER) { + if (t >= ORDER) { throw new Error("Tweak bigger than curve order"); } - bn += bytesToNumber(privateKey); - if (bn >= ORDER) { - bn -= ORDER; + t += bytesToNumber(privateKey); + if (t >= ORDER) { + t -= ORDER; } - if (bn === 0n) { + if (t === 0n) { throw new Error( "The tweak was out of range or the resulted private key is invalid" ); } - privateKey.set(hexToBytes(numberToHex(bn))); + privateKey.set(hexToBytes(numberToHex(t))); return privateKey; } From 66bf7086a931e75a6f516e58b6230bc270b705ab Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Wed, 15 Jun 2022 14:34:02 +0400 Subject: [PATCH 2/2] Remove 0x prefix in hexToBytes. --- src/utils.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index e5328b4..7ba16a5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,6 @@ // buf.toString('hex') -> toHex(buf) import assert from "@noble/hashes/_assert"; +import { hexToBytes as _hexToBytes } from "@noble/hashes/utils"; const assertBool = assert.bool; const assertBytes = assert.bytes; export { assertBool, assertBytes }; @@ -7,7 +8,6 @@ export { bytesToHex, bytesToHex as toHex, concatBytes, - hexToBytes, createView, utf8ToBytes } from "@noble/hashes/utils"; @@ -20,6 +20,11 @@ export function bytesToUtf8(data: Uint8Array): string { return new TextDecoder().decode(data); } +export function hexToBytes(data: string): Uint8Array { + const sliced = data.startsWith("0x") ? data.substring(2) : data; + return _hexToBytes(sliced); +} + // buf.equals(buf2) -> equalsBytes(buf, buf2) export function equalsBytes(a: Uint8Array, b: Uint8Array): boolean { if (a.length !== b.length) {