Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/scrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export async function scrypt(
n: number,
p: number,
r: number,
dklen: number
dkLen: number
): Promise<Uint8Array> {
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(
Expand All @@ -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 });
}
34 changes: 15 additions & 19 deletions src/secp256k1-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,20 @@ 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);
}
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);
Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 6 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// 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 };
export {
bytesToHex,
bytesToHex as toHex,
concatBytes,
hexToBytes,
createView,
utf8ToBytes
} from "@noble/hashes/utils";
Expand All @@ -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) {
Expand Down