Skip to content

Commit 2feff5d

Browse files
committed
refactor: compare GROUP_ORDER as buffer (instead of using BN.js)
1 parent c987b0c commit 2feff5d

File tree

5 files changed

+14
-19
lines changed

5 files changed

+14
-19
lines changed

src/taproot.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ const LEAF_VERSION_TAPSCRIPT = 0xc0;
1313
const TAP_LEAF_TAG = buffer_1.Buffer.from('TapLeaf', 'utf8');
1414
const TAP_BRANCH_TAG = buffer_1.Buffer.from('TapBranch', 'utf8');
1515
const TAP_TWEAK_TAG = buffer_1.Buffer.from('TapTweak', 'utf8');
16-
// todo: compare buffers dirrectly
17-
const GROUP_ORDER = buffer_1.Buffer.from(
18-
'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141',
19-
'hex',
20-
);
21-
const GROUP_ORDER_BN = new BN(GROUP_ORDER);
2216
const EC_P_BN = new BN(types_1.EC_P);
2317
const EC_P_REDUCTION = BN.red(EC_P_BN);
2418
const EC_P_QUADRATIC_RESIDUE = EC_P_BN.addn(1).divn(4);
@@ -56,8 +50,7 @@ function tweakKey(pubKey, h) {
5650
TAP_TWEAK_TAG,
5751
buffer_1.Buffer.concat(h ? [pubKey, h] : [pubKey]),
5852
);
59-
const t = new BN(tweakHash);
60-
if (t.gte(GROUP_ORDER_BN)) {
53+
if (tweakHash.compare(types_1.GROUP_ORDER) >= 0) {
6154
// todo: add test for this case
6255
throw new Error('Tweak value over the SECP256K1 Order');
6356
}

src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Buffer as NBuffer } from 'buffer';
33
export declare const typeforce: any;
44
export declare const ZERO32: NBuffer;
55
export declare const EC_P: NBuffer;
6+
export declare const GROUP_ORDER: NBuffer;
67
export declare function isPoint(p: Buffer | number | undefined | null): boolean;
78
export declare function UInt31(value: number): boolean;
89
export declare function BIP32Path(value: string): boolean;

src/types.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
'use strict';
22
Object.defineProperty(exports, '__esModule', { value: true });
3-
exports.oneOf = exports.Null = exports.BufferN = exports.Function = exports.UInt32 = exports.UInt8 = exports.tuple = exports.maybe = exports.Hex = exports.Buffer = exports.String = exports.Boolean = exports.Array = exports.Number = exports.Hash256bit = exports.Hash160bit = exports.Buffer256bit = exports.TaprootNode = exports.TaprootLeaf = exports.Network = exports.ECPoint = exports.Satoshi = exports.Signer = exports.BIP32Path = exports.UInt31 = exports.isPoint = exports.EC_P = exports.ZERO32 = exports.typeforce = void 0;
3+
exports.oneOf = exports.Null = exports.BufferN = exports.Function = exports.UInt32 = exports.UInt8 = exports.tuple = exports.maybe = exports.Hex = exports.Buffer = exports.String = exports.Boolean = exports.Array = exports.Number = exports.Hash256bit = exports.Hash160bit = exports.Buffer256bit = exports.TaprootNode = exports.TaprootLeaf = exports.Network = exports.ECPoint = exports.Satoshi = exports.Signer = exports.BIP32Path = exports.UInt31 = exports.isPoint = exports.GROUP_ORDER = exports.EC_P = exports.ZERO32 = exports.typeforce = void 0;
44
const buffer_1 = require('buffer');
55
exports.typeforce = require('typeforce');
66
exports.ZERO32 = buffer_1.Buffer.alloc(32, 0);
77
exports.EC_P = buffer_1.Buffer.from(
88
'fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f',
99
'hex',
1010
);
11+
exports.GROUP_ORDER = buffer_1.Buffer.from(
12+
'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141',
13+
'hex',
14+
);
1115
function isPoint(p) {
1216
if (!buffer_1.Buffer.isBuffer(p)) return false;
1317
if (p.length < 33) return false;

ts_src/taproot.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const BN = require('bn.js');
44
import * as bcrypto from './crypto';
55
// todo: use varuint-bitcoin??
66
import * as varuint from 'bip174/src/lib/converter/varint';
7-
import { TweakedPublicKey, ZERO32, EC_P } from './types';
7+
import { TweakedPublicKey, ZERO32, EC_P, GROUP_ORDER } from './types';
88

99
// todo: !!!Temp, to be replaced. Only works because bip32 has it as dependecy. Linting will fail.
1010
const ecc = require('tiny-secp256k1');
@@ -14,13 +14,6 @@ const TAP_LEAF_TAG = NBuffer.from('TapLeaf', 'utf8');
1414
const TAP_BRANCH_TAG = NBuffer.from('TapBranch', 'utf8');
1515
const TAP_TWEAK_TAG = NBuffer.from('TapTweak', 'utf8');
1616

17-
// todo: compare buffers dirrectly
18-
const GROUP_ORDER = NBuffer.from(
19-
'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141',
20-
'hex',
21-
);
22-
const GROUP_ORDER_BN = new BN(GROUP_ORDER);
23-
2417
const EC_P_BN = new BN(EC_P);
2518
const EC_P_REDUCTION = BN.red(EC_P_BN);
2619
const EC_P_QUADRATIC_RESIDUE = EC_P_BN.addn(1).divn(4);
@@ -69,8 +62,8 @@ export function tweakKey(
6962
TAP_TWEAK_TAG,
7063
NBuffer.concat(h ? [pubKey, h] : [pubKey]),
7164
);
72-
const t = new BN(tweakHash);
73-
if (t.gte(GROUP_ORDER_BN)) {
65+
66+
if (tweakHash.compare(GROUP_ORDER) >= 0) {
7467
// todo: add test for this case
7568
throw new Error('Tweak value over the SECP256K1 Order');
7669
}

ts_src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export const EC_P = NBuffer.from(
77
'fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f',
88
'hex',
99
);
10+
export const GROUP_ORDER = NBuffer.from(
11+
'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141',
12+
'hex',
13+
);
1014

1115
export function isPoint(p: Buffer | number | undefined | null): boolean {
1216
if (!NBuffer.isBuffer(p)) return false;

0 commit comments

Comments
 (0)