Skip to content

Commit 5175e98

Browse files
committed
Small refactor: compat, scrypt
1 parent 29818dc commit 5175e98

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

Diff for: src/scrypt.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ export async function scrypt(
1010
n: number,
1111
p: number,
1212
r: number,
13-
dklen: number
13+
dkLen: number
1414
): Promise<Uint8Array> {
1515
assertBytes(password);
1616
assertBytes(salt);
17-
return _scryptAsync(password, salt, { N: n, r, p, dkLen: dklen });
17+
return _scryptAsync(password, salt, { N: n, r, p, dkLen });
1818
}
1919

2020
export function scryptSync(
@@ -23,9 +23,9 @@ export function scryptSync(
2323
n: number,
2424
p: number,
2525
r: number,
26-
dklen: number
26+
dkLen: number
2727
): Uint8Array {
2828
assertBytes(password);
2929
assertBytes(salt);
30-
return _scrypt(password, salt, { N: n, r, p, dkLen: dklen });
30+
return _scrypt(password, salt, { N: n, r, p, dkLen });
3131
}

Diff for: src/secp256k1-compat.ts

+15-19
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,20 @@ import { assertBool, assertBytes, hexToBytes, toHex } from "./utils";
55
// Use `secp256k1` module directly.
66
// This is a legacy compatibility layer for the npm package `secp256k1` via noble-secp256k1
77

8-
// Copy-paste from secp256k1, maybe export it?
9-
const bytesToNumber = (bytes: Uint8Array) => hexToNumber(toHex(bytes));
10-
const numberToHex = (num: number | bigint) =>
11-
num.toString(16).padStart(64, "0");
12-
const numberToBytes = (num: number | bigint) => hexToBytes(numberToHex(num));
13-
148
function hexToNumber(hex: string): bigint {
159
if (typeof hex !== "string") {
1610
throw new TypeError("hexToNumber: expected string, got " + typeof hex);
1711
}
1812
return BigInt(`0x${hex}`);
1913
}
2014

21-
// Calculates a modulo b
22-
function mod(a: bigint, b: bigint = secp.CURVE.P): bigint {
23-
const result = a % b;
24-
return result >= 0 ? result : b + result;
25-
}
15+
// Copy-paste from secp256k1, maybe export it?
16+
const bytesToNumber = (bytes: Uint8Array) => hexToNumber(toHex(bytes));
17+
const numberToHex = (num: number | bigint) =>
18+
num.toString(16).padStart(64, "0");
19+
const numberToBytes = (num: number | bigint) => hexToBytes(numberToHex(num));
20+
const { mod } = secp.utils;
21+
2622
const ORDER = secp.CURVE.n;
2723

2824
type Output = Uint8Array | ((len: number) => Uint8Array);
@@ -165,23 +161,23 @@ export function privateKeyTweakAdd(
165161
): Uint8Array {
166162
assertBytes(privateKey, 32);
167163
assertBytes(tweak, 32);
168-
let bn = bytesToNumber(tweak);
169-
if (bn === 0n) {
164+
let t = bytesToNumber(tweak);
165+
if (t === 0n) {
170166
throw new Error("Tweak must not be zero");
171167
}
172-
if (bn >= ORDER) {
168+
if (t >= ORDER) {
173169
throw new Error("Tweak bigger than curve order");
174170
}
175-
bn += bytesToNumber(privateKey);
176-
if (bn >= ORDER) {
177-
bn -= ORDER;
171+
t += bytesToNumber(privateKey);
172+
if (t >= ORDER) {
173+
t -= ORDER;
178174
}
179-
if (bn === 0n) {
175+
if (t === 0n) {
180176
throw new Error(
181177
"The tweak was out of range or the resulted private key is invalid"
182178
);
183179
}
184-
privateKey.set(hexToBytes(numberToHex(bn)));
180+
privateKey.set(hexToBytes(numberToHex(t)));
185181
return privateKey;
186182
}
187183

0 commit comments

Comments
 (0)