negate: () => Point
double: () => Point
add: (other: Point) => Point
- subtract: (other: Point) => Point
multiply: (n: bigint, safe?: boolean) => Point
multiplyUnsafe: (scalar: bigint) => Point
toAffine: () => AffinePoint
assertValidity: () => Point
toBytes: (isCompressed?: boolean) => Bytes
- toHex: (isCompressed?: boolean) => string
}
/** Alias to Uint8Array. */
const r = a % b
return r >= 0n ? r : b + r
}
- static modN = (a: bigint) => this.M(a, this.N)
static modP = (a: bigint) => this.M(a, this.P)
/** Modular inversion using eucledian GCD (non-CT). No negative exponent for now. */
/** assert is element of field mod N (excl. 0) */
static FnIsValidNot0 = (n: bigint) => this.bigintInRange(n, 1n, this.N)
static isEven = (y: bigint) => (y & 1n) === 0n
- /** create Uint8Array of byte n */
- static u8of = (n: number): Bytes => Uint8Array.of(n)
- static getPrefix = (y: bigint) => this.u8of(this.isEven(y) ? 0x02 : 0x03)
+ static getPrefix = (y: bigint) => Uint8Array.of(this.isEven(y) ? 0x02 : 0x03)
/** lift_x from BIP340 calculates square root. Validates x, then validates root*root. */
static lift_x (x: bigint) {
// Let c = x³ + 7 mod p. Fail if x ≥ p. (also fail if x < 1)
Z3 = M(Z3 + t0) // step 40
return secp256k1.Point(X3, Y3, Z3)
},
- subtract (other: Point): Point {
- return this.add(other.negate())
- },
/**
* Point-by-scalar multiplication. Scalar must be in range 1 <= n < CURVE.n.
* Uses {@link wNAF} for base point.
const { x, y } = this.assertValidity().toAffine()
const x32b = secp256k1.bigintTo32Bytes(x)
if (isCompressed) return secp256k1.concatBytes(secp256k1.getPrefix(y), x32b)
- return secp256k1.concatBytes(secp256k1.u8of(0x04), x32b, secp256k1.bigintTo32Bytes(y))
- },
- /** Converts bytes to hex string */
- toHex (isCompressed?: boolean): string {
- return Array.from(secp256k1.abytes(this.toBytes(isCompressed))).map((e) => e.toString(16).padStart(2, '0')).join('')
+ return secp256k1.concatBytes(Uint8Array.of(0x04), x32b, secp256k1.bigintTo32Bytes(y))
}
})
}
* Any time `G.multiply` is done, precomputes are used.
*
* w-ary non-adjacent form (wNAF) precomputation method is 10% slower than windowed method,
- * but takes 2x less RAM. RAM reduction is possible by utilizing `.subtract`.
+ * but takes 2x less RAM. RAM reduction is possible by utilizing subtraction.
*
* !! Precomputes can be disabled by commenting-out call of the wNAF() inside Point#multiply().
*/