Module
std.x.crypto.sha2 — SHA-256, SHA-384 and SHA-512 (FIPS 180-4), HMAC (FIPS 198-1 / RFC 2104) and HKDF (RFC 5869). Stability tier: x — the D53 crypto ladder's first rung (campaign stdc-crypto, sprint sc16).
The nursery banner (D31). std.x.crypto.sha2 is not std.crypto: its path, names and behaviour may change or vanish in one sprint; no other std module may depend on it (it may depend on the facade, and does: nothing — the module is self-contained on purpose); its reason and graduation trigger live in std/x/README.md. HMAC and HKDF live IN this module rather than beside it because a nursery resident may not depend on another resident — the graduation move splits them.
Implemented from the FIPS 180-4 DOCUMENT, not ported. Every constant block and every schedule below cites its section of NIST FIPS 180-4 (August 2015), fetched into the planning repo's refs/specs/fips-180-4.pdf. The vectors (NIST CAVP byte-oriented sets, RFC 4231, RFC 5869, Wycheproof v1) catch transcription errors; the document is the authority.
**The word currency is wrapping[u64], for both cores.** SHA-256's 32-bit words are carried in the low half of a wrapping[u64] and masked after every add and rotation — the spelling sc16 chose when the native rung refused the narrowing int as wrapping[u32] (F-0089, CLOSED at the sc18 pin: s111 / wolf-lang#131 lowers it). The mask discipline stands as written: every value entering a round is 32-bit-clean, and every sum or rotation is masked before it is stored. SHA-512's words are the full wrapping[u64] and need none. Respelling the 256 core over wrapping[u32] is the recorded follow-on, not this sprint's edit (the sc18 bump ruling: the diff rewrites the storage layer, so it is a deliberate change with the 1210-vector gate around it, never a bump side-effect).
**Schedule words and round constants are stored as int.** A 32-bit word always fits an int. A 64-bit word does NOT (as int is a CHECKED conversion everywhere — a value with the top bit set traps overflow, measured at these pins), so every 64-bit table in this module is a PAIR of List[int] — high and low 32-bit halves — split by push_word_halves from the verbatim FIPS literal and recombined at the use site. List[wrapping[u64]] would spell it directly; the native refusal that forced the pairing (F-0090) CLOSED at the sc18 pin (s111 / wolf-lang#132), and the direct spelling is the same recorded follow-on as the 256 core's respell — the paired halves stay until a sprint retires them deliberately.
Lanes, measured at the sc18 pin (wolf 21b129e): ALL THREE run the cores now. The checked tier's F-0091 operator refusal — the module's dark lane from sc16, "this operator in checked execution" on every wrapping shift/bitwise op — closed with s111 (wolf-lang#130), and 18 sha2 rows flipped unsupported -> run at the bump. What remains dark there is CAPACITY, not semantics: the long-message, Monte Carlo and full-Wycheproof files exhaust the checked tier's step/shadow-memory budgets ("step budget exhausted" / "shadow-memory budget exhausted", measured per file at the bump), so those nine rows keep unsupported with the mechanism named in the ledger. Long-vector and Monte Carlo tests additionally record the interpreter lane as slow (the sc16 ledger word): semantically in-lane, skipped at the rig's per-test ceiling, re-measured at pin bumps (is20's fix is merged upstream, unreleased — the flip waits on the lupin release that carries it).
**Byte sequences are List[byte] (sc35), AND THE INGESTION GUARD IS GONE.** For eleven sprints the currency was List[int] with a documented 0..255 element contract (§11's interim, F-0035) and a require_byte that trapped assert on every element that broke it. D72/s135's byte and s136's byte-typed builtins (wolf-lang#231) make that contract the TYPE's, so the guard was unreachable code and it has been deleted rather than left standing as decoration. What the guard defended is unchanged and is now enforced one layer down: a List[byte] cannot hold 300 or -1, so no wrapping cast can digest a value the caller never meant. to_hex is this module's own hex rendering so that its tests and doc examples import exactly one module (the std.bytes.from_str precedent); std.hex.encode is the facade spelling of the same operation.
Constant-time obligations (D53's retrofit clause). c28's compiler-enforced tier does not exist yet; until it does, every pub fn below carries a ct: line stating its obligation class, so the claims are auditable now and verifiable later. The honest summary: the digest cores branch and index on PUBLIC quantities only (byte counts, round numbers) — and the one recorded exception is RETIRED (sc35). The 0..255 ingestion guard compared every input element, which is exactly the kind of value-dependent branch c28 will flag, and this header promised "it leaves with F-0035's real byte type". The type arrived and it left: update256/update512 now absorb their elements with no comparison at all, so the digest path has NO value-dependent branch left. to_hex is still NOT constant-time (it indexes an alphabet by the byte's value) and says so. HMAC's key path branches on the key's LENGTH alone, never on key bytes. These are source-level obligations, not a fixed-time certification of generated instructions, runtime operations or any execution tier. Review the exact compiler/backend/runtime pins separately; vectors prove outputs, not absence of timing, cache, allocation or residual-copy leakage.
No division (sc84, wolf-std#151). Nothing in this module divides: the HKDF block counter and to_hex's nibbles are masks and shifts. cargo xtask codegen builds this module's default and release objects on the host target and fails on any integer division instruction in them, with curve25519, chacha20 and bech32 beside it. It is not a timing measurement (no latency was measured on any CPU), it says nothing about the interpreters, and the compiler that makes the objects is not audited; a target the gate is not run on is not covered by it.
No error rows anywhere in this module: every function is total over its documented domain, and since sc35 there is exactly ONE failure mode left — an out-of-range HKDF length, a caller contract violation that traps assert. The other one (a non-byte element) is a type error now, caught before the program runs.
Types and traits
struct · Source line 396
pub struct Sha256 { h0: wrapping[u64], h1: wrapping[u64], h2: wrapping[u64], h3: wrapping[u64], h4: wrapping[u64], h5: wrapping[u64], h6: wrapping[u64], h7: wrapping[u64], buf: List[byte], total: int, k: List[int], w: List[int], }
An in-progress SHA-256 computation. The fields are the interim backing (F-0035's byte currency, and 32-bit words in wrapping[u64] halves per the header) — never part of the API. A caller can forge one; a forged state produces a wrong digest, never a trap, because every field value is arithmetically valid.
struct · Source line 606
pub struct Sha512 { h0: wrapping[u64], h1: wrapping[u64], h2: wrapping[u64], h3: wrapping[u64], h4: wrapping[u64], h5: wrapping[u64], h6: wrapping[u64], h7: wrapping[u64], buf: List[byte], total: int, khi: List[int], klo: List[int], whi: List[int], wlo: List[int], }
An in-progress SHA-512 computation. Same field posture as Sha256; the schedule constants ride as high/low halves (khi/klo) per the module header's storage rule.
struct · Source line 808
pub struct Sha384 { st: Sha512, }
An in-progress SHA-384 computation: a Sha512 core under §5.3.4's initial value. Its own type so that a 384 stream cannot be finished as a 512 one (or the reverse) — the digests would silently differ in more than length. update384 passes mut st.st straight through: the F-0092 refusal that made sc16 copy the core out and back (a mut field path off a mut parameter refused natively) closed at the sc18 pin (s111 / wolf-lang#133, the shape re-probed on all three lanes at 21b129e), and the copy dance retired with it.
Functions and methods
fn · Source line 421
pub fn new256() -> Sha256
A fresh SHA-256 state — FIPS 180-4 §5.3.3's initial hash value ("the first thirty-two bits of the fractional parts of the square roots of the first eight prime numbers"). Total: no rows, no traps. ct: no secret is present yet; construction is input-independent.
var st = sha2.new256()
sha2.update256(mut st, "abc".bytes())
sha2.to_hex(sha2.final256(take st)) == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
fn · Source line 533
pub fn update256(mut st: Sha256, data: List[byte])
Absorbs data into the running SHA-256 computation. Bytes are buffered until a 512-bit block is full, then compressed; calling update256 in any chunking produces the digest one call would (held by tests/x/crypto/sha2/incremental_chunking.lu). Total — the 0..255 element contract is the TYPE's since sc35, so the ingestion guard that used to trap here is gone. ct: branches and indexes on byte COUNTS only, never on byte values, with no exception left to record.
fn · Source line 557
pub fn final256(take st: Sha256) -> List[byte]
Pads (FIPS 180-4 §5.1.1: one 0x80 byte, zeros to 56 mod 64, the message length in BITS as a big-endian 64-bit word) and returns the 32 digest bytes, big-endian per word (§6.2.2 step 4, §3.1). Consumes the state — a finished computation cannot be updated again, and the mode system is what enforces it (take, the builders' finish precedent). Total for every state new256/update256 can produce. ct: padding branches on the buffered LENGTH (public); the length word is public by definition.
var st = sha2.new256()
sha2.update256(mut st, List[byte]())
sha2.to_hex(sha2.final256(take st)) == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
fn · Source line 593
pub fn sum256(msg: List[byte]) -> List[byte]
The SHA-256 digest of msg, one shot: new256 + update256 + final256. 32 bytes out. Total (sc35: List[byte] cannot carry an element outside 0..255, so there is nothing left to trap on). ct: as the incremental trio — public-count branching only.
sha2.to_hex(sha2.sum256("abc".bytes())) == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
sha2.sum256("abc".bytes()).len == 32
fn · Source line 633
pub fn new512() -> Sha512
A fresh SHA-512 state — FIPS 180-4 §5.3.5's initial hash value ("the first sixty-four bits of the fractional parts of the square roots of the first eight prime numbers"). Total: no rows, no traps. ct: no secret is present yet; construction is input-independent.
var st = sha2.new512()
sha2.update512(mut st, "abc".bytes())
sha2.final512(take st).len == 64
fn · Source line 732
pub fn update512(mut st: Sha512, data: List[byte])
Absorbs data into the running SHA-512 computation; 1024-bit blocks. Any chunking produces one digest. Total (sc35). ct: as update256 — public-count branching only, with no exception left to record.
fn · Source line 753
pub fn final512(take st: Sha512) -> List[byte]
Pads (FIPS 180-4 §5.1.2: one 0x80, zeros to 112 mod 128, the bit length as a big-endian 128-bit word — the high word is the byte count's top three bits, exact for every representable length) and returns the 64 digest bytes. Consumes the state (take). Total. ct: padding branches on the buffered LENGTH (public).
var st = sha2.new512()
sha2.update512(mut st, List[byte]())
sha2.to_hex(sha2.final512(take st)) == "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
fn · Source line 789
pub fn sum512(msg: List[byte]) -> List[byte]
The SHA-512 digest of msg, one shot. 64 bytes out. Total (sc35). ct: as the incremental trio.
sha2.to_hex(sha2.sum512("abc".bytes())) == "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"
fn · Source line 821
pub fn new384() -> Sha384
A fresh SHA-384 state — FIPS 180-4 §5.3.4's initial hash value ("the square roots of the ninth through sixteenth prime numbers"). Total. ct: no secret is present yet; construction is input-independent.
var st = sha2.new384()
sha2.update384(mut st, "abc".bytes())
sha2.final384(take st).len == 48
fn · Source line 844
pub fn update384(mut st: Sha384, data: List[byte])
Absorbs data into the running SHA-384 computation. Total (sc35). ct: as update512.
fn · Source line 851
pub fn final384(take st: Sha384) -> List[byte]
Pads exactly as SHA-512 and returns the LEFT-MOST 48 digest bytes (FIPS 180-4 §6.5 step 3 / §7 truncation). Consumes the state. Total. ct: as final512.
fn · Source line 863
pub fn sum384(msg: List[byte]) -> List[byte]
The SHA-384 digest of msg, one shot. 48 bytes out. Total (sc35). ct: as the incremental trio.
sha2.to_hex(sha2.sum384("abc".bytes())) == "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7"
fn · Source line 917
pub fn hmac256(key: List[byte], msg: List[byte]) -> List[byte]
HMAC-SHA-256 (FIPS 198-1; RFC 2104). Any key length; keys longer than the 64-byte block are reduced by one SHA-256 first (FIPS 198-1 §4 step 2). 32 bytes out. Total (sc35: a non-byte element is a type error now, not a trap). ct: see xor_pad — key-length-dependent control flow only; no branch or index reads a key byte, and since sc35 the module has no recorded exception at all.
sha2.to_hex(sha2.hmac256("key".bytes(), "The quick brown fox jumps over the lazy dog".bytes())) == "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"
fn · Source line 937
pub fn hmac384(key: List[byte], msg: List[byte]) -> List[byte]
HMAC-SHA-384 (FIPS 198-1; RFC 2104). The block is SHA-384's 128 bytes. 48 bytes out. Total (sc35). ct: as hmac256.
fn · Source line 957
pub fn hmac512(key: List[byte], msg: List[byte]) -> List[byte]
HMAC-SHA-512 (FIPS 198-1; RFC 2104). The block is SHA-512's 128 bytes. 64 bytes out. Total (sc35). ct: as hmac256.
fn · Source line 1008
pub fn hkdf256_expand(prk: List[byte], info: List[byte], n: int) -> List[byte]
HKDF-Expand over HMAC-SHA-256 (RFC 5869 §2.3). Traps assert when n is negative or exceeds 255 * 32 (the RFC's hard cap: the counter is one byte) — the length is the caller's parameter, not data (§2's trap rule). n == 0 yields empty output. prk is a pseudorandom key of at least HashLen octets (32 for SHA-256), normally Extract's output; this module cannot inspect entropy. The selected extract-then-expand path always supplies exactly 32 bytes. ct: the T(i) chain's shape depends on n and info.len (public); no branch reads a byte of prk or the running T.
let prk = sha2.hkdf256_extract("salt".bytes(), "ikm".bytes())
sha2.hkdf256_expand(prk, "info".bytes(), 42).len == 42
sha2.hkdf256_expand(prk, "info".bytes(), 0).len == 0
fn · Source line 1063
pub fn hkdf256(salt: List[byte], ikm: List[byte], info: List[byte], n: int) -> List[byte]
HKDF over HMAC-SHA-256, extract-then-expand in one call (RFC 5869 §2). Traps as hkdf256_expand on an out-of-range n. ct: the composition of the two halves' obligations.
sha2.hkdf256("salt".bytes(), "ikm".bytes(), "info".bytes(), 16).len == 16
fn · Source line 1077
pub fn hkdf384_expand(prk: List[byte], info: List[byte], n: int) -> List[byte]
HKDF-Expand over HMAC-SHA-384 (RFC 5869 §2.3); the cap is 255 * 48. Traps and totality as hkdf256_expand. ct: as hkdf256_expand.
fn · Source line 1109
pub fn hkdf384(salt: List[byte], ikm: List[byte], info: List[byte], n: int) -> List[byte]
HKDF over HMAC-SHA-384, extract-then-expand in one call. ct: the composition of the two halves' obligations.
fn · Source line 1123
pub fn hkdf512_expand(prk: List[byte], info: List[byte], n: int) -> List[byte]
HKDF-Expand over HMAC-SHA-512 (RFC 5869 §2.3); the cap is 255 * 64. Traps and totality as hkdf256_expand. ct: as hkdf256_expand.
fn · Source line 1155
pub fn hkdf512(salt: List[byte], ikm: List[byte], info: List[byte], n: int) -> List[byte]
HKDF over HMAC-SHA-512, extract-then-expand in one call. ct: the composition of the two halves' obligations.
fn · Source line 1178
pub fn to_hex(b: List[byte]) -> str
b as lowercase hexadecimal, two digits per byte — this module's own rendering so its tests and examples import exactly one module (the std.bytes.from_str precedent); std.hex.encode is the facade spelling. Total — the slice's own assert on a non-byte element is unreachable since sc35, and was already unreachable in practice because no digest this module returns could contain one. ct: NOT constant-time — the alphabet slice is indexed by each byte's VALUE, a secret-dependent memory index. Render public values with it; a constant-time encoder is c28-tier work.
sha2.to_hex(sha2.sum256("abc".bytes()))[0..8] == "ba7816bf"
sha2.to_hex(List[byte]()) == ""