From: Chris Duncan Date: Fri, 5 Dec 2025 23:01:49 +0000 (-0800) Subject: Consolidate bigint cast to bytes since we already validate it before we use it. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=2f1e37ca92d0fdb7a9064be120a36da6a24e3100;p=libnemo.git Consolidate bigint cast to bytes since we already validate it before we use it. --- diff --git a/src/lib/crypto/secp256k1.ts b/src/lib/crypto/secp256k1.ts index 2daef48..73ea0ea 100644 --- a/src/lib/crypto/secp256k1.ts +++ b/src/lib/crypto/secp256k1.ts @@ -231,27 +231,13 @@ export class Secp256k1 { return int } - /** Number to 32b. Must be 0 <= num < 2²⁵⁶. validate, pad, to bytes. */ - static bigintTo32Bytes (num: bigint): Bytes { - this.bigintInRange(num, 0n, 2n ** 256n) // secp256k1 is weierstrass curve. Equation is x³ + ax + b. - let len = this.L, bytes = new Uint8Array(len) - for (let i = len - 1; i >= 0; i--) { - bytes[i] = Number(num & 0xffn) - num >>= 8n - } - return bytes - } - /** Checks if the point is valid and on-curve. */ - static isValidPoint (p?: Point): Point { - if (p) { - const { x, y } = this.Affine(p) // convert to 2d xy affine point. - this.bigintInRange(x, 1n, this.P) // must be in range 1 <= x,y < P - this.bigintInRange(y, 1n, this.P) - // y² == x³ + ax + b, equation sides must be equal - return this.M(y * y) === this.koblitz(x) ? p : this.err('bad point: not on curve') - } - this.err('bad point: undefined') + static isValidPoint (p: Point): Point { + const { x, y } = this.Affine(p) // convert to 2d xy affine point. + this.bigintInRange(x, 1n, this.P) // must be in range 1 <= x,y < P + this.bigintInRange(y, 1n, this.P) + // y² == x³ + ax + b, equation sides must be equal + return this.M(y * y) === this.koblitz(x) ? p : this.err('bad point: not on curve') } /** Normalize private key to scalar (bigint). Verifies scalar is in range 1