Skip to content

Commit e155e37

Browse files
committed
Disallow zero tweaks
1 parent 95e052d commit e155e37

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/secp256k1-compat.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as secp from "./secp256k1";
33
import { assertBool, assertBytes, hexToBytes, toHex } from "./utils";
44

55
// Legacy compatibility layer for elliptic via noble-secp256k1
6-
// Use `curve-secp256k1` module directly instead
6+
// Use `secp256k1` module directly instead
77

88
// Copy-paste from secp256k1, maybe export it?
99
const bytesToNumber = (bytes: Uint8Array) => hexToNumber(toHex(bytes));
@@ -166,6 +166,9 @@ export function privateKeyTweakAdd(
166166
assertBytes(privateKey, 32);
167167
assertBytes(tweak, 32);
168168
let bn = bytesToNumber(tweak);
169+
if (bn === 0n) {
170+
throw new Error("Tweak must not be zero");
171+
}
169172
if (bn >= ORDER) {
170173
throw new Error("Tweak bigger than curve order");
171174
}
@@ -231,6 +234,9 @@ export function publicKeyTweakAdd(
231234
assertBool(compressed);
232235
const p1 = secp.Point.fromHex(publicKey);
233236
const p2 = secp.Point.fromPrivateKey(tweak);
237+
if (p2.equals(secp.Point.ZERO)) {
238+
throw new Error("Tweak must not be zero");
239+
}
234240
const point = p1.add(p2);
235241
return output(out, compressed ? 33 : 65, point.toRawBytes(compressed));
236242
}
@@ -245,6 +251,9 @@ export function publicKeyTweakMul(
245251
assertBytes(tweak, 32);
246252
assertBool(compressed);
247253
const bn = bytesToNumber(tweak);
254+
if (bn === 0n) {
255+
throw new Error("Tweak must not be zero");
256+
}
248257
if (bn <= 0 || bn >= ORDER) {
249258
throw new Error("Tweak is zero or bigger than curve order");
250259
}
@@ -259,6 +268,9 @@ export function privateKeyTweakMul(
259268
assertBytes(privateKey, 32);
260269
assertBytes(tweak, 32);
261270
let bn = bytesToNumber(tweak);
271+
if (bn === 0n) {
272+
throw new Error("Tweak must not be zero");
273+
}
262274
if (bn >= ORDER) {
263275
throw new Error("Tweak bigger than curve order");
264276
}

0 commit comments

Comments
 (0)