static L: 32 = 32 // field / group byte length
static L2: 64 = 64
- static publicKeyLength: Secp256k1Lengths = {
+ static pkLength: Secp256k1Lengths = {
compressed: 33,
uncompressed: 65
}
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)
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')
}
}
/** 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
}