Secp256k1Curve
Minimal secp256k1 elliptic-curve arithmetic, in pure Kotlin + BigInteger, sufficient to verify an ECDSA signature and to decode a SEC 1 public key point.
Why this exists. Secp256k1SignatureAudit needs to answer "does this signature verify" offline, against an export of persisted signatures, with no KMS provider and no network access. secp256k1 is not guaranteed to be registered with the JDK's built-in SunEC provider on every JVM, and pulling in a crypto library (BouncyCastle, as the KMS plugin modules do) only for this one read-only check would add a dependency to a module (EcdsaSignatureCodec included) that has deliberately stayed dependency-free. So this implements exactly the curve operations ECDSA verification needs — point addition, doubling, scalar multiplication, and point decoding — and nothing else.
This is not a general-purpose EC library. There is no signing here (it never needs a private key), no constant-time guarantees, and no side-channel hardening. That is an acceptable trade for an offline audit tool that only ever handles public data, but this object must not be reused anywhere that signs, or anywhere latency/timing matters against an adversary.
Curve parameters are the SEC 2 (v2.0) recommendation for secp256k1: y^2 = x^3 + 7 (mod p).
This is public rather than internal purely so it can be unit-tested directly (this module builds with kotlin.build.archivesTaskOutputAsFriendModule=false, so internal is not visible from src/test) — it is still a low-level primitive, not part of the intended consumer API.
Types
A secp256k1 point in affine coordinates. There is no dedicated point-at-infinity instance: infinity is represented as null wherever a computed point is optional (see add).
Properties
Functions
Adds two points, or doubles p1 when p1 == p2. Returns null for the point at infinity, i.e. when the two points are inverses of each other (same x, y values sum to 0 mod p).
Decodes a SEC 1 elliptic-curve point: uncompressed (65 bytes, 0x04 || X || Y) or compressed (33 bytes, 0x02/0x03 || X, sign of Y in the prefix). Returns null if bytes is not a validly-encoded, on-curve point — wrong length, unrecognized prefix, x >= p, or (compressed form only) x has no square root mod p.
Returns true if point satisfies y^2 = x^3 + 7 (mod p) with both coordinates in [0, p). Every point this object produces satisfies this by construction; callers use this to validate externally-supplied points (i.e. decoded public keys).
Scalar multiplication k * point via double-and-add. Returns null (point at infinity) if k mod n == 0.
Checks the ECDSA verification equation: with w = s^-1 mod n, u1 = z*w mod n, u2 = r*w mod n, the signature is valid iff (u1*G + u2*Q).x mod n == r.