1"""Reference SHA-256 (FIPS 180-4) with a configurable number of steps, exposing every internal word.23`compress(state, block, rounds)` applies `rounds` ≤ 64 steps of the compression function (the standard uses 64).4`sha256(msg, rounds)` is the full Merkle–Damgård hash with padding. Verified against hashlib for rounds = 64.5"""6from __future__ import annotations7import struct89MASK = 0xFFFFFFFF10K = [11 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,12 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,13 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,14 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,15 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,16 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,17 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,18 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,19]20H0 = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]212223def rotr(x: int, k: int) -> int:24 return ((x >> k) | (x << (32 - k))) & MASK252627def Sigma0(x): return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22)28def Sigma1(x): return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25)29def sigma0(x): return rotr(x, 7) ^ rotr(x, 18) ^ (x >> 3)30def sigma1(x): return rotr(x, 17) ^ rotr(x, 19) ^ (x >> 10)31def Ch(x, y, z): return (x & y) ^ (~x & z) & MASK32def Maj(x, y, z): return (x & y) ^ (x & z) ^ (y & z)333435def message_schedule(block: bytes, rounds: int = 64) -> list[int]:36 W = list(struct.unpack(">16L", block))37 for t in range(16, rounds):38 W.append((sigma1(W[t - 2]) + W[t - 7] + sigma0(W[t - 15]) + W[t - 16]) & MASK)39 return W[:max(rounds, 16)]404142def step(state: tuple, k: int, w: int) -> tuple:43 a, b, c, d, e, f, g, h = state44 T1 = (h + Sigma1(e) + Ch(e, f, g) + k + w) & MASK45 T2 = (Sigma0(a) + Maj(a, b, c)) & MASK46 return ((T1 + T2) & MASK, a, b, c, (d + T1) & MASK, e, f, g)474849def step_inverse(new_state: tuple, k: int, w: int) -> tuple:50 """Invert one step given the step's K and W (the step function is a bijection on the state)."""51 a1, a, b, c, e1, e, f, g = new_state52 T2 = (Sigma0(a) + Maj(a, b, c)) & MASK53 T1 = (a1 - T2) & MASK54 d = (e1 - T1) & MASK55 h = (T1 - Sigma1(e) - Ch(e, f, g) - k - w) & MASK56 return (a, b, c, d, e, f, g, h)575859def compress(state: list[int] | tuple, block: bytes, rounds: int = 64, feedforward: bool = True,60 trace: list | None = None) -> list[int]:61 W = message_schedule(block, rounds)62 s = tuple(state)63 for t in range(rounds):64 s = step(s, K[t], W[t])65 if trace is not None:66 trace.append(s)67 if not feedforward:68 return list(s)69 return [(x + y) & MASK for x, y in zip(state, s)]707172def pad(msg: bytes) -> bytes:73 ml = len(msg) * 874 msg += b"\x80"75 while (len(msg) % 64) != 56:76 msg += b"\x00"77 return msg + struct.pack(">Q", ml)787980def sha256(msg: bytes, rounds: int = 64) -> bytes:81 data = pad(msg)82 state = list(H0)83 for i in range(0, len(data), 64):84 state = compress(state, data[i:i + 64], rounds)85 return struct.pack(">8L", *state)868788def words_to_bytes(words) -> bytes:89 return struct.pack(">%dL" % len(words), *words)909192def bytes_to_words(b: bytes) -> list[int]:93 return list(struct.unpack(">%dL" % (len(b) // 4), b))94