"""Reference SHA-256 (FIPS 180-4) with a configurable number of steps, exposing every internal word. `compress(state, block, rounds)` applies `rounds` ≤ 64 steps of the compression function (the standard uses 64). `sha256(msg, rounds)` is the full Merkle–Damgård hash with padding. Verified against hashlib for rounds = 64. """ from __future__ import annotations import struct MASK = 0xFFFFFFFF K = [ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, ] H0 = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19] def rotr(x: int, k: int) -> int: return ((x >> k) | (x << (32 - k))) & MASK def Sigma0(x): return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22) def Sigma1(x): return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25) def sigma0(x): return rotr(x, 7) ^ rotr(x, 18) ^ (x >> 3) def sigma1(x): return rotr(x, 17) ^ rotr(x, 19) ^ (x >> 10) def Ch(x, y, z): return (x & y) ^ (~x & z) & MASK def Maj(x, y, z): return (x & y) ^ (x & z) ^ (y & z) def message_schedule(block: bytes, rounds: int = 64) -> list[int]: W = list(struct.unpack(">16L", block)) for t in range(16, rounds): W.append((sigma1(W[t - 2]) + W[t - 7] + sigma0(W[t - 15]) + W[t - 16]) & MASK) return W[:max(rounds, 16)] def step(state: tuple, k: int, w: int) -> tuple: a, b, c, d, e, f, g, h = state T1 = (h + Sigma1(e) + Ch(e, f, g) + k + w) & MASK T2 = (Sigma0(a) + Maj(a, b, c)) & MASK return ((T1 + T2) & MASK, a, b, c, (d + T1) & MASK, e, f, g) def step_inverse(new_state: tuple, k: int, w: int) -> tuple: """Invert one step given the step's K and W (the step function is a bijection on the state).""" a1, a, b, c, e1, e, f, g = new_state T2 = (Sigma0(a) + Maj(a, b, c)) & MASK T1 = (a1 - T2) & MASK d = (e1 - T1) & MASK h = (T1 - Sigma1(e) - Ch(e, f, g) - k - w) & MASK return (a, b, c, d, e, f, g, h) def compress(state: list[int] | tuple, block: bytes, rounds: int = 64, feedforward: bool = True, trace: list | None = None) -> list[int]: W = message_schedule(block, rounds) s = tuple(state) for t in range(rounds): s = step(s, K[t], W[t]) if trace is not None: trace.append(s) if not feedforward: return list(s) return [(x + y) & MASK for x, y in zip(state, s)] def pad(msg: bytes) -> bytes: ml = len(msg) * 8 msg += b"\x80" while (len(msg) % 64) != 56: msg += b"\x00" return msg + struct.pack(">Q", ml) def sha256(msg: bytes, rounds: int = 64) -> bytes: data = pad(msg) state = list(H0) for i in range(0, len(data), 64): state = compress(state, data[i:i + 64], rounds) return struct.pack(">8L", *state) def words_to_bytes(words) -> bytes: return struct.pack(">%dL" % len(words), *words) def bytes_to_words(b: bytes) -> list[int]: return list(struct.unpack(">%dL" % (len(b) // 4), b))