qi.xyz — energy money

Inside SHA-256

The cryptographic hash function that secures proof-of-work. Type any message and watch SHA-256 transform it, step by step.

↳ Enter a message to hash
SHA-256 Click "Hash It" to begin...
0 Overview
1 Preprocessing
2 Constants
3 Message Schedule
4 Compression
5 Final Hash
6 Avalanche
Overview

How SHA-256 Works

SHA-256 (Secure Hash Algorithm 256-bit) takes any input and produces a fixed 256-bit (32-byte) hash. It's a one-way function — you can't reverse it. Every Bitcoin and Quai block header is hashed with SHA-256 to produce proof-of-work.

Input
any length
Pad to
512-bit blocks
Expand to
64 words
Compress
64 rounds
256-bit
hash output
Why does this matter for mining? In proof-of-work, miners repeatedly hash block headers with different nonces, searching for a hash below the target difficulty. The properties of SHA-256 — determinism, uniformity, pre-image resistance — make this an irreducible computational cost, which is what ties energy to security.
Step 1

Preprocessing & Padding

The input message is converted to binary, then padded so its total length is a multiple of 512 bits. Padding adds a 1 bit, then zeros, then the original message length as a 64-bit integer.

Original message → ASCII → Binary
After padding (512-bit block)
Padding rule: message + 1 + zeros + 64-bit length = multiple of 512 bits. This ensures every message, regardless of size, maps cleanly into 512-bit processing blocks.
Step 2

Initial Hash Values & Round Constants

SHA-256 uses 8 initial hash values (H₀–H₇) derived from the square roots of the first 8 primes, and 64 round constants (K₀–K₆₃) from the cube roots of the first 64 primes. These are nothing-up-my-sleeve numbers — you can verify them yourself.

Initial Hash Values (√ of primes 2,3,5,7,11,13,17,19)
64 Round Constants K (∛ of first 64 primes)
Nothing-up-my-sleeve: These constants are mathematically derived from primes, meaning the NSA (who designed SHA-2) couldn't have hidden a backdoor in the constant selection. Anyone can recompute them.
Step 3

Message Schedule (W₀ – W₆₃)

The 512-bit block is split into 16 words of 32 bits each (W₀–W₁₅). Then 48 more words are generated using bitwise operations, expanding 16 words into 64 words that feed the compression function.

Expansion formula: For i = 16..63:
W[i] = σ₁(W[i-2]) + W[i-7] + σ₀(W[i-15]) + W[i-16]

where σ₀(x) = ROTR⁷(x) ⊕ ROTR¹⁸(x) ⊕ SHR³(x)
and σ₁(x) = ROTR¹⁷(x) ⊕ ROTR¹⁹(x) ⊕ SHR¹⁰(x)
Step 4

Compression Function (64 Rounds)

This is the heart of SHA-256. Eight working variables (a–h) are initialized from the current hash values, then transformed through 64 rounds of mixing using bitwise operations.

Ch (e AND f) XOR (NOT e AND g)
Maj (a AND b) XOR (a AND c) XOR (b AND c)
Σ₀ ROTR²(a) ⊕ ROTR¹³(a) ⊕ ROTR²²(a)
Σ₁ ROTR⁶(e) ⊕ ROTR¹¹(e) ⊕ ROTR²⁵(e)
Each round:
T₁ = h + Σ₁(e) + Ch(e,f,g) + K[i] + W[i]
T₂ = Σ₀(a) + Maj(a,b,c)
h=g, g=f, f=e, e=d+T₁, d=c, c=b, b=a, a=T₁+T₂
0
Working Variables (a–h) at selected round
Step 5

Final Hash Output

After all 64 rounds, the working variables are added back to the initial hash values. The resulting 8 × 32-bit words are concatenated to produce the final 256-bit hash.

H₀–H₇ (initial) + a–h (after compression) = Final Hash Values
Concatenated 256-bit Hash
For multi-block messages: The output hash values become the initial hash values for processing the next 512-bit block. This chaining ensures every bit of the message affects the final hash.
Demonstration

The Avalanche Effect

Change a single bit in the input and roughly 50% of output bits flip. This is what makes SHA-256 secure — there's no way to predict how the hash will change from small input changes. This property is critical for mining: you can't shortcut the search for a valid nonce.

Original Input

Modified Input (1 char change)

Bit-level diff (red = flipped bits)