Skip to reference

std.x.crypto.p256

wolf-std d58f9e1 · toolchain pin verified against wolf 0.2.41 (0261597). Source signatures and documentation; execution support is stated in each module.

All modules · Plain text · Stability tier: x

Module

std.x.crypto.p256 — the NIST P-256 curve (secp256r1) and ECDSA signatures: FIPS 186-5 (ECDSA), SEC1 v2 (point encodings and validation), NIST SP 800-186 (the domain parameters), RFC 6979 (deterministic nonces). Stability tier: x — the D53 crypto ladder's Weierstrass rung (campaign stdc-crypto, sprint sc23), the curve real-world certificate authorities issue: sc22's unsupported_alg answer for an ECDSA-P256 certificate becomes a real verification.

The nursery banner (D31). std.x.crypto.p256 is not std.crypto: its path, names and behaviour may change or vanish in one sprint; no other std module may depend on it. Its reason and graduation trigger live in std/x/README.md. ECDSA's SHA-256 and RFC 6979's HMAC are std.x.crypto.sha2's, called through the module boundary — a resident-on-resident edge the graduation move must resolve (named in the register beside curve25519's).

## The design ruling: table-free constant-time scalar multiplication

P-256 is a short Weierstrass curve, and the FAST implementations use secret-indexed table lookups — a cache-timing surface no vector set can see. The human-approved ruling (the sc23 contract): this module multiplies with a TABLE-FREE ladder — 256 fixed iterations of one complete doubling, one complete addition and one ARITHMETIC point selection (fe_select: the secret bit enters only as a multiplier, never a branch, never an index) — over the COMPLETE addition formulas of Renes–Costello–Batina (eprint 2015/1060, the a = -3 specialization), which are total: identity, doubling and inverse inputs take the same code path as a generic addition, so no exceptional-case branch exists for a timing channel to read. scalar_mul is c28's VERIFICATION TARGET for this module (marked in its header): slower than a windowed table, and provably constant-time by construction in portable wolf — c28's ct tier then VERIFIES the property instead of compensating for a table. The masked-table and hardware-intrinsic fast paths are named residue (their own D-question when a measured performance need appears).

## The field: 16 checked-int limbs of 16 bits, Solinas reduction

p = 2^256 - 2^224 + 2^192 + 2^96 - 1 (SP 800-186 §3.2.1.3, a Solinas prime). The fast reduction is the identity 2^256 ≡ 2^224 - 2^192 - 2^96 + 1 (mod p): product limb k (k >= 16) re-enters at k-16 (+), k-10 (-), k-4 (-), k-2 (+). Sixteen bits is the ONE uniform limb width that fits wolf's int story both ways (the sc18 bound-proof discipline, stated before the field was written): a 16x16 schoolbook column is at most 16 products < 2^32, so a column is < 2^36 (measured 2^35.4 over the fuzz corpus), and 96/192/224/256 are all multiples of 16, so the fold is limb-aligned (32-bit limbs overflow a column at 2^68; 24-bit limbs misalign 2^224). The carry tail adds the constant 2^32·p — which dominates any signed fold output, so no intermediate is ever negative-beyond- bound — then runs four carry passes folding the 2^256 overflow back through the identity; the largest value anywhere is the limb-15 constant addition at 2^48.0 (measured), 32768x under int_max, and X3's checked arithmetic enforces the whole proof at every operation: a violated bound is a trap the vectors catch, never a silent wrap. NO wrapping[T] anywhere; no bitwise-on-int (F-0026's spelling). Every algorithm here was fuzzed limb-for-limb against a big-integer reference before this file was written (3000 random + adversarial field cases, 60 point-formula rounds incl. the complete-formula edge cases, the RFC 6979 vectors end-to-end).

## The scalar field mod n: Montgomery, because n is not Solinas

n (the group order) has an irregular low half, so mod-n arithmetic is Montgomery multiplication in base 2^16 (CIOS): fixed schedule, no division, no data-dependent trip counts. Inversions mod n and mod p are Fermat powers to FIXED PUBLIC exponents (n-2, p-2) — the per-bit branch reads a compiled-in constant, never data.

## ECDSA: verify is public, sign is the secret path

VERIFY (FIPS 186-5 §6.4.2) handles only public data — a public key, a message, a signature — so it carries no constant-time obligation (the sc18 rule, stated per fn). The on-curve check of SEC1 §3.2.2 is MANDATORY on every ingested point: an attacker-supplied point off the curve is the invalid-curve attack class, rejected by name (invalid_curve, check_point) and answered false by verify. SIGN (§6.4.1) is the secret path: the nonce and the scalar multiplication are its obligations, enumerated for c28 in the fn headers. Nonces are RFC 6979 deterministic — derived from the key and message through HMAC-SHA-256, drawing NO entropy. The ruling's lean, taken, and justified: nonce REUSE (or bias) leaks the private key outright, deterministic derivation removes the hazard class entirely, it needs no os capability (this module stays pure and comptime-refusable only through its sha2 delegate), and it is byte-for-byte testable (RFC 6979 A.2.5 pins k, r and s). s118's os_random is the sanctioned entropy source when a consumer needs randomized signatures; that variant is named residue, not built.

## ct: obligations, enumerated (c28's checklist for this module)

- scalar_mul — THE target: fixed 256 iterations, complete formulas, arithmetic selection; the secret bit is a multiplier. - fe_select — the selection itself (sc18's fe_cswap class). - the RFC 6979 HMAC chain — fixed-schedule digest work (sha2's). - recorded exceptions, all shared with the ladder's precedent — and one of them RETIRED at sc35: the 0..255 ingestion guard branched per input byte (F-0035's class) and is gone with the byte type; floordiv's sign correction (sc18's note); the 6979 candidate-acceptance compare (bytes_geq_n) — the RFC's own rejection sampling, at most one extra HMAC round per 2^32 keys. - VERIFY and everything it calls: public data, no obligation.

**Byte sequences are List[byte] (sc35).** They were List[int] with a documented 0..255 element contract (F-0035) that this module policed with require_byte at every door; D72/s135's byte and s136's byte-typed builtins (wolf-lang#231) make it the TYPE's, so the guard is deleted and verify's two range-checking loops with it. All multi-byte values are BIG-endian (SEC1/FIPS convention — unlike curve25519's little-endian RFCs). to_hex is this module's own rendering (the ladder precedent).

Types and traits

No public declarations in this section.

Functions and methods

public_key

fn · Source line 2221

pub fn public_key(d: List[byte]) -> List[byte]

The ECDSA public key for a 32-byte big-endian private key d in [1, n): the uncompressed SEC1 encoding of [d]G (0x04 || X || Y). Traps assert on a private key that is not 32 bytes, carries a zero, or >= n (a caller contract violation: FIPS 186-5 §6.2.1 requires d in [1, n-1]). ct: SECRET scalar d drives the table-free ladder (scalar_mul, c28's target); the derived public key is public.

Example (native lane — a scalar multiplication is a table-free ladder past lupin's step budget, so this is prose not a fenced block; the pinned witnesses are rfc6979_p256.lu and sign_verify_smoke.lu): p256.public_key(d) returns 65 bytes for a 32-byte private key d in [1, n).

sign

fn · Source line 2277

pub fn sign(d: List[byte], message: List[byte]) -> List[byte]

An ECDSA signature over message under the 32-byte big-endian private key d, as the 64-byte fixed-width r || s (each 32 bytes, big-endian — the JOSE / IEEE P1363 form; DER is signature_der). The message is hashed with SHA-256 (ES256). The nonce is RFC 6979 DETERMINISTIC — derived from d and the message, drawing NO entropy (the design ruling: no nonce-reuse hazard, no os capability, byte- exact testable). Retries the RFC 6979 candidate on the ~2^-256 events r = 0 or s = 0. Traps assert on a bad private key (see public_key); the message is arbitrary bytes. ct: THE SECRET PATH. d and the nonce drive the RFC 6979 HMAC chain, scalar_mul (c28's target) and the mod-n arithmetic; the enumerated obligations are the ladder, fe_select, and the noted exceptions in the module header. No randomness, so no reuse hazard.

Example (native lane, prose — the sign path is a table-free ladder past lupin's step budget; pinned byte-exact in rfc6979_p256.lu): p256.sign(d, msg) returns a 64-byte r || s that p256.verify(p256.public_key(d), msg, sig) accepts.

verify

fn · Source line 2375

pub fn verify(pubkey: List[byte], message: List[byte], sig: List[byte]) -> bool

Verify an ECDSA signature (FIPS 186-5 §6.4.2) over message given a 65-byte uncompressed SEC1 public key and the 64-byte r || s signature. Returns true iff valid. Answers FALSE — never a trap — for a wrong-length key or signature, a public key that fails the SEC1 §3.2.2 on-curve check (the invalid-curve rejection), r or s outside [1, n), or a failed verification equation: a signature and a key are the CALLER'S data to check, not a contract they promised (§2's rule). The message is hashed with SHA-256 (ES256). ct: VERIFY IS PUBLIC DATA — the key, signature and message are all public, so no branch here protects a secret and this function carries no constant-time obligation (the sc18 rule).

Example (native lane, prose — a verify is two table-free ladders past lupin's step budget; pinned by the CAVP/Wycheproof suites and sign_verify_smoke.lu): p256.verify(p256.public_key(d), msg, p256.sign(d, msg)) is true; a flipped signature byte is false.

verify_der

fn · Source line 2582

pub fn verify_der(pubkey: List[byte], message: List[byte], der: List[byte]) -> bool

Verify an ECDSA signature supplied in DER form (SEQUENCE{INTEGER r, INTEGER s}) — the encoding X.509 certificates and TLS carry, and exactly what sc22's chain validator hands this module. Strict DER: a malformed encoding answers FALSE (never a trap), by the same rule as verify. ct: public data.

Example (native lane, prose — a verify is two table-free ladders past lupin's step budget; the strict-DER rejections are pinned three-lane in der_codec.lu): p256.verify_der(pk, msg, p256.signature_der(p256.sign(d, msg))) is true, and a malformed DER signature is false, never a trap.

signature_der

fn · Source line 2595

pub fn signature_der(sig: List[byte]) -> List[byte]

Encode a 64-byte r || s signature as strict DER (SEQUENCE{INTEGER r, INTEGER s}) — the form for an X.509/TLS consumer. Each half becomes a minimal nonnegative INTEGER (a leading 0x00 added when the high bit is set). Traps assert on a signature that is not 64 bytes. ct: public data.

field_self_test

fn · Source line 2700

pub fn field_self_test() -> bool

The FIELD's algebraic identities plus the cheap curve laws, checked ALONE before any ECDSA path observes them (the field-first discipline): additive inverse, multiply commutativity / associativity, distributivity, squaring as self-multiply, the Fermat inverse (a·a⁻¹ = 1), the byte round trip, that G is on the curve (SEC1 §3.2.2), and that the COMPLETE addition and doubling agree on [2]G. No 256-iteration scalar multiplication, so this fits the checked tier's step budget — THREE-LANE (the group-order check that needs a full ladder is in self_test). Returns true iff every identity holds. ct: a test predicate over public constants.

p256.field_self_test()

self_test

fn · Source line 2742

pub fn self_test() -> bool

The full self-test: field_self_test plus the GROUP-ORDER check that [n-1]G + G is the identity (so [n]G = O, n the group order) — a full table-free ladder, so this exceeds the checked tier's step budget and lupin's evaluation budget (native + slow, the ladder precedent). Returns true iff every identity and the group law hold. ct: a test predicate over public constants; no secret.

to_hex

fn · Source line 2778

pub fn to_hex(b: List[byte]) -> str

b as lowercase hexadecimal, two digits per byte. Total — the non-byte element it used to trap on is a type error since sc35. ct: NOT constant-time (the alphabet slice is indexed by each byte's value) — renders public values only; a constant-time encoder is c28-tier work.

p256.to_hex("abc".bytes()) == "616263"
p256.to_hex(List[byte]()) == ""

Constants

No public declarations in this section.

Errors and row tags

No public declarations in this section.