From 3b13ab6464e17a342d1c1356a2cf7a8af3b997e1 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 4 Dec 2025 14:43:11 -0800 Subject: [PATCH] Rename public key length constants. --- src/lib/crypto/secp256k1.ts | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/lib/crypto/secp256k1.ts b/src/lib/crypto/secp256k1.ts index 7e1e448..cb0210e 100644 --- a/src/lib/crypto/secp256k1.ts +++ b/src/lib/crypto/secp256k1.ts @@ -42,8 +42,8 @@ type Add = Length<[...Tuple, ...Tupl /** Public key length definitions */ type Secp256k1Lengths = { - publicKey: Add - publicKeyUncompressed: Add + compressed: Add + uncompressed: Add } export class Secp256k1 { @@ -66,9 +66,9 @@ export class Secp256k1 { static L: 32 = 32 // field / group byte length static L2: 64 = 64 - static lengths: Secp256k1Lengths = { - publicKey: 33, - publicKeyUncompressed: 65 + static publicKeyLength: Secp256k1Lengths = { + compressed: 33, + uncompressed: 65 } // ## Helpers @@ -273,14 +273,13 @@ export class Secp256k1 { /** Convert Uint8Array or hex string to Point. */ static pointFromBytes (bytes: Bytes): Point { this.abytes(bytes) - const { publicKey: comp, publicKeyUncompressed: uncomp } = this.lengths // e.g. for 32-byte: 33, 65 let p: Point | undefined = undefined const length = bytes.length const head = bytes[0] const tail = bytes.subarray(1) const x = this.bytesToBigint(tail.subarray(0, this.L)) // No actual validation is done here: use .assertValidity() - if (length === comp && (head === 0x02 || head === 0x03)) { + if (length === this.publicKeyLength.compressed && (head === 0x02 || head === 0x03)) { // Equation is y² == x³ + ax + b. We calculate y from x. // y = √y²; there are two solutions: y, -y. Determine proper solution based on prefix let y = this.lift_x(x) @@ -290,7 +289,7 @@ export class Secp256k1 { p = this.Point(x, y, 1n) } // Uncompressed 65-byte point, 0x04 prefix - if (length === uncomp && head === 0x04) p = this.Point(x, this.bytesToBigint(tail.subarray(this.L, this.L2)), 1n) + if (length === this.publicKeyLength.uncompressed && head === 0x04) p = this.Point(x, this.bytesToBigint(tail.subarray(this.L, this.L2)), 1n) // Validate point return p ? p.assertValidity() : this.err('bad point: not on curve') } @@ -330,11 +329,10 @@ export class Secp256k1 { } static isValidPublicKey (publicKey: Bytes, isCompressed?: boolean): boolean { - const { publicKey: comp, publicKeyUncompressed } = this.lengths try { const l = publicKey.length - if (isCompressed === true && l !== comp) return false - if (isCompressed === false && l !== publicKeyUncompressed) return false + if (isCompressed === true && l !== this.publicKeyLength.compressed) return false + if (isCompressed === false && l !== this.publicKeyLength.uncompressed) return false return !!this.pointFromBytes(publicKey) } catch (error) { return false -- 2.47.3