Skip to content

Commit cc47e53

Browse files
committed
Simplify secp256k1 ops
1 parent af66ea2 commit cc47e53

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

src/hdkey.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ function numberToBytes(num: bigint): Uint8Array {
2222
return hexToBytes(num.toString(16).padStart(64, "0"));
2323
}
2424

25+
function modN(a: bigint, b: bigint = secp.CURVE.n): bigint {
26+
const result = a % b;
27+
return result >= 0 ? result : b + result;
28+
}
29+
2530
const MASTER_SECRET = utf8ToBytes("Bitcoin seed");
2631
// Bitcoin hardcoded by default
2732
const BITCOIN_VERSIONS: Versions = { private: 0x0488ade4, public: 0x0488b21e };
@@ -203,18 +208,15 @@ export class HDKey {
203208
const I = hmac(sha512, this.chainCode, data);
204209
const childTweak = bytesToNumber(I.slice(0, 32));
205210
const chainCode = I.slice(32);
206-
if (childTweak > secp.CURVE.n) {
211+
if (!secp.utils.isValidPrivateKey(childTweak)) {
207212
throw new Error("Tweak bigger than curve order");
208213
}
209214
const child = new HDKey(this.versions);
210215
try {
211216
// Private parent key -> private child key
212217
if (this.privateKey) {
213-
let added = this.privKey! + childTweak;
214-
if (added >= secp.CURVE.n) {
215-
added -= secp.CURVE.n;
216-
}
217-
if (added === 0n) {
218+
const added = modN(this.privKey! + childTweak);
219+
if (!secp.utils.isValidPrivateKey(added)) {
218220
throw new Error(
219221
"The tweak was out of range or the resulted private key is invalid"
220222
);

src/secp256k1.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function mod(a: bigint, b: bigint = secp.CURVE.P): bigint {
2323
const result = a % b;
2424
return result >= 0 ? result : b + result;
2525
}
26+
const ORDER = secp.CURVE.n;
2627

2728
type Output = Uint8Array | ((len: number) => Uint8Array);
2829
interface Signature {
@@ -146,7 +147,7 @@ export function ecdsaVerify(
146147
assertBytes(signature, 64);
147148
const r = bytesToNumber(signature.slice(0, 32));
148149
const s = bytesToNumber(signature.slice(32, 64));
149-
if (r >= secp.CURVE.n || s >= secp.CURVE.n) {
150+
if (r >= ORDER || s >= ORDER) {
150151
throw new Error("Cannot parse signature");
151152
}
152153
let sig;
@@ -165,12 +166,12 @@ export function privateKeyTweakAdd(
165166
assertBytes(privateKey, 32);
166167
assertBytes(tweak, 32);
167168
let bn = bytesToNumber(tweak);
168-
if (bn >= secp.CURVE.n) {
169+
if (bn >= ORDER) {
169170
throw new Error("Tweak bigger than curve order");
170171
}
171172
bn += bytesToNumber(privateKey);
172-
if (bn >= secp.CURVE.n) {
173-
bn -= secp.CURVE.n;
173+
if (bn >= ORDER) {
174+
bn -= ORDER;
174175
}
175176
if (bn === 0n) {
176177
throw new Error(
@@ -183,7 +184,7 @@ export function privateKeyTweakAdd(
183184

184185
export function privateKeyNegate(privateKey: Uint8Array): Uint8Array {
185186
assertBytes(privateKey, 32);
186-
const bn = mod(-bytesToNumber(privateKey), secp.CURVE.n);
187+
const bn = mod(-bytesToNumber(privateKey), ORDER);
187188
privateKey.set(hexToBytes(numberToHex(bn)));
188189
return privateKey;
189190
}
@@ -244,7 +245,7 @@ export function publicKeyTweakMul(
244245
assertBytes(tweak, 32);
245246
assertBool(compressed);
246247
const bn = bytesToNumber(tweak);
247-
if (bn <= 0 || bn >= secp.CURVE.n) {
248+
if (bn <= 0 || bn >= ORDER) {
248249
throw new Error("Tweak is zero or bigger than curve order");
249250
}
250251
const point = secp.Point.fromHex(publicKey).multiply(bn);
@@ -258,12 +259,12 @@ export function privateKeyTweakMul(
258259
assertBytes(privateKey, 32);
259260
assertBytes(tweak, 32);
260261
let bn = bytesToNumber(tweak);
261-
if (bn >= secp.CURVE.n) {
262+
if (bn >= ORDER) {
262263
throw new Error("Tweak bigger than curve order");
263264
}
264-
bn = mod(bn * bytesToNumber(privateKey), secp.CURVE.n);
265-
if (bn >= secp.CURVE.n) {
266-
bn -= secp.CURVE.n;
265+
bn = mod(bn * bytesToNumber(privateKey), ORDER);
266+
if (bn >= ORDER) {
267+
bn -= ORDER;
267268
}
268269
if (bn === 0n) {
269270
throw new Error(
@@ -296,8 +297,8 @@ export function signatureImport(
296297

297298
export function signatureNormalize(signature: Uint8Array): Uint8Array {
298299
const res = getSignature(signature);
299-
if (res.s > secp.CURVE.n / 2n) {
300-
signature.set(numberToBytes(secp.CURVE.n - res.s), 32);
300+
if (res.s > ORDER / 2n) {
301+
signature.set(numberToBytes(ORDER - res.s), 32);
301302
}
302303
return signature;
303304
}

0 commit comments

Comments
 (0)