]> git.codecow.com Git - libnemo.git/commitdiff
Abbreviate public key and secret key.
authorChris Duncan <chris@zoso.dev>
Thu, 4 Dec 2025 22:44:27 +0000 (14:44 -0800)
committerChris Duncan <chris@zoso.dev>
Thu, 4 Dec 2025 22:44:27 +0000 (14:44 -0800)
src/lib/crypto/secp256k1.ts

index cb0210e264ce24539a5540a3de8a176142ea2e0e..98577604c1533dbb2ca52e04fff99402670aab66 100644 (file)
@@ -66,7 +66,7 @@ export class Secp256k1 {
 
        static L: 32 = 32 // field / group byte length
        static L2: 64 = 64
-       static publicKeyLength: Secp256k1Lengths = {
+       static pkLength: Secp256k1Lengths = {
                compressed: 33,
                uncompressed: 65
        }
@@ -279,7 +279,7 @@ export class Secp256k1 {
                const tail = bytes.subarray(1)
                const x = this.bytesToBigint(tail.subarray(0, this.L))
                // No actual validation is done here: use .assertValidity()
-               if (length === this.publicKeyLength.compressed && (head === 0x02 || head === 0x03)) {
+               if (length === this.pkLength.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)
@@ -289,7 +289,7 @@ export class Secp256k1 {
                        p = this.Point(x, y, 1n)
                }
                // Uncompressed 65-byte point, 0x04 prefix
-               if (length === this.publicKeyLength.uncompressed && head === 0x04) p = this.Point(x, this.bytesToBigint(tail.subarray(this.L, this.L2)), 1n)
+               if (length === this.pkLength.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')
        }
@@ -318,22 +318,22 @@ export class Secp256k1 {
        }
 
        /** Normalize private key to scalar (bigint). Verifies scalar is in range 1<s<N */
-       static secretKeyToScalar (secretKey: Bytes): bigint {
-               const num = this.bytesToBigint(this.abytes(secretKey, this.L, 'secret key'))
+       static secretKeyToScalar (sk: Bytes): bigint {
+               const num = this.bytesToBigint(this.abytes(sk, this.L, 'secret key'))
                return this.bigintInRange(num, 1n, this.N, 'invalid secret key: outside of range')
        }
 
        /** Creates 33/65-byte public key from 32-byte private key. */
-       static getPublicKey (privKey: Bytes, isCompressed = true): Bytes {
-               return this.G.multiply(this.secretKeyToScalar(privKey)).toBytes(isCompressed)
+       static getPublicKey (sk: Bytes, isCompressed = true): Bytes {
+               return this.G.multiply(this.secretKeyToScalar(sk)).toBytes(isCompressed)
        }
 
-       static isValidPublicKey (publicKey: Bytes, isCompressed?: boolean): boolean {
+       static isValidPublicKey (pk: Bytes, isCompressed?: boolean): boolean {
                try {
-                       const l = publicKey.length
-                       if (isCompressed === true && l !== this.publicKeyLength.compressed) return false
-                       if (isCompressed === false && l !== this.publicKeyLength.uncompressed) return false
-                       return !!this.pointFromBytes(publicKey)
+                       const l = pk.length
+                       if (isCompressed === true && l !== this.pkLength.compressed) return false
+                       if (isCompressed === false && l !== this.pkLength.uncompressed) return false
+                       return !!this.pointFromBytes(pk)
                } catch (error) {
                        return false
                }