From 93ab50b99e537ea08162729b6ba94a7a643cff6b Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 30 Nov 2025 22:23:11 -0800 Subject: [PATCH] Remove redundant BigInt functions. --- src/lib/crypto/secp256k1.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/lib/crypto/secp256k1.ts b/src/lib/crypto/secp256k1.ts index ad71f0c..a656a9b 100644 --- a/src/lib/crypto/secp256k1.ts +++ b/src/lib/crypto/secp256k1.ts @@ -155,7 +155,6 @@ export class Secp256k1 { this.captureTrace(e, this.err) throw e } - static isBig = (n: unknown): n is bigint => typeof n === 'bigint' // is big integer static isStr = (s: unknown): s is string => typeof s === 'string' // is string static isBytes = (a: unknown): a is Uint8Array => a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array') @@ -216,9 +215,8 @@ export class Secp256k1 { const c = this.cr() return c.getRandomValues(this.u8n(len)) } - static big = BigInt static arange = (n: bigint, min: bigint, max: bigint, msg = 'bad number: out of range'): bigint => - this.isBig(n) && min <= n && n < max ? n : this.err(msg) + typeof n === 'bigint' && min <= n && n < max ? n : this.err(msg) /** modular division */ static M = (a: bigint, b: bigint = this.P) => { const r = a % b @@ -423,7 +421,7 @@ export class Secp256k1 { // y = √y²; there are two solutions: y, -y. Determine proper solution based on prefix let y = this.lift_x(x) const evenY = this.isEven(y) - const evenH = this.isEven(this.big(head)) + const evenH = this.isEven(BigInt(head)) if (evenH !== evenY) y = this.M(-y) p = this.Point(x, y, 1n) } @@ -444,7 +442,7 @@ export class Secp256k1 { static doubleScalarMulUns = (R: Point, u1: bigint, u2: bigint): Point => { return this.G.multiply(u1, false).add(R.multiply(u2, false)).assertValidity() } - static bytesToNumBE = (b: Bytes): bigint => this.big('0x' + (this.bytesToHex(b) || '0')) + static bytesToNumBE = (b: Bytes): bigint => BigInt('0x' + (this.bytesToHex(b) || '0')) static sliceBytesNumBE = (b: Bytes, from: number, to: number) => this.bytesToNumBE(b.subarray(from, to)) static B256 = 2n ** 256n // secp256k1 is weierstrass curve. Equation is x³ + ax + b. /** Number to 32b. Must be 0 <= num < B256. validate, pad, to bytes. */ @@ -564,7 +562,7 @@ export class Secp256k1 { const delta = bytes.length * 8 - 256 if (delta > 1024) this.err('msg invalid') // our CUSTOM check, "just-in-case": prohibit long inputs const num = this.bytesToNumBE(bytes) - return delta > 0 ? num >> this.big(delta) : num + return delta > 0 ? num >> BigInt(delta) : num } /** int2octets can't be used; pads small msgs with 0: BAD for truncation as per RFC vectors */ static bits2int_modN = (bytes: Bytes): bigint => this.modN(this.bits2int(this.abytes(bytes))) @@ -857,7 +855,7 @@ export class Secp256k1 { const h = this.bits2int_modN(this.abytes(messageHash, this.L)) // Truncate hash const radj = recovery === 2 || recovery === 3 ? r + this.N : r this.FpIsValidNot0(radj) // ensure q.x is still a field element - const head = this.getPrefix(this.big(recovery!)) // head is 0x02 or 0x03 + const head = this.getPrefix(BigInt(recovery!)) // head is 0x02 or 0x03 const Rb = this.concatBytes(head, this.numTo32b(radj)) // concat head + r const R = this.pointFromBytes(Rb) const ir = this.invert(radj, this.N) // r^-1 @@ -1138,8 +1136,8 @@ export class Secp256k1 { let f = this.G // f must be G, or could become I in the end const pow_2_w = 2 ** this.W // 256 for W=8 const maxNum = pow_2_w // 256 for W=8 - const mask = this.big(pow_2_w - 1) // 255 for W=8 == mask 0b11111111 - const shiftBy = this.big(this.W) // 8 for W=8 + const mask = BigInt(pow_2_w - 1) // 255 for W=8 == mask 0b11111111 + const shiftBy = BigInt(this.W) // 8 for W=8 for (let w = 0; w < this.pwindows; w++) { let wbits = Number(n & mask) // extract W bits. n >>= shiftBy // shift number by W bits. -- 2.47.3