# SHA-256 Compression

{% embed url="<https://youtu.be/WANpKKYOF2o>" %}

The **compression step** of SHA-256 transforms each **512-bit message block** into an updated internal state. It consists of three main phases:

1. **Initialization of registers** (working variables)
2. **Computation of temporary words and register mutations**
3. **Integration of results** into the hash state

***

#### 1. Initialization of Registers

SHA-256 starts with **eight working variables (a–h)**.

* For the **first round**, these registers are initialized from **constants**: the fractional square roots of the first **eight prime numbers**.
* For **subsequent rounds**, they are set to the **results (chaining variables)** from the previous block.

Example in Go:

```
// First 8 primes: 2, 3, 5, 7, 11, 13, 17, 19
func InitializeRegisters() []uint32 {
    registerValues := []uint32{}
    for i := 0; i < 8; i++ {
        root := math.Pow(float64(First_eight_primes[i]), 0.5)
        value, _ := math.Modf(root * (1 << 32))
        registerValues = append(registerValues, uint32(value))
    }
    return registerValues
}
// Output: [6a09e667, bb67ae85, 3c6ef372, a54ff53a, 510e527f, 9b05688c, 1f83d9ab, 5be0cd19]
```

👉 **Key takeaway:** These values provide the **starting point** for SHA-256 compression.

***

#### 2. Temporary Words and Register Mutations

For each of the **64 words** in the message schedule, SHA-256 computes:

* tempWord1 = h + Σ1(e) + Choice(e,f,g) + K\[i] + W\[i]
* tempWord2 = Σ0(a) + Majority(a,b,c)

Then, the working variables are **rotated and updated**:

```
for i := 0; i < 64; i++ {
    tempWord1 := h + BigSigmaOne(e) + Choice(e,f,g) + constants[i] + messageSchedule[i]
    tempWord2 := BigSigmaZero(a) + Majority(a,b,c)

    h = g
    g = f
    f = e
    e = d + tempWord1
    d = c
    c = b
    b = a
    a = tempWord1 + tempWord2
}
```

&#x20;

The four **logical functions** that power SHA-256’s security are:

```
// Σ0(x) = ROTR2(x) XOR ROTR13(x) XOR ROTR22(x)
func BigSigmaZero(x uint32) uint32 {
    return bits.RotateLeft32(x, -2) ^ bits.RotateLeft32(x, -13) ^ bits.RotateLeft32(x, -22)
}

// Σ1(x) = ROTR6(x) XOR ROTR11(x) XOR ROTR25(x)
func BigSigmaOne(x uint32) uint32 {
    return bits.RotateLeft32(x, -6) ^ bits.RotateLeft32(x, -11) ^ bits.RotateLeft32(x, -25)
}

// Choice(x,y,z) = (x & y) ^ (^x & z)
func Choice(x,y,z uint32) uint32 {
    return (x & y) ^ (^x & z)
}

// Majority(x,y,z) = (x & y) ^ (x & z) ^ (y & z)
func Majority(x,y,z uint32) uint32 {
    return (x & y) ^ (x & z) ^ (y & z)
}
```

&#x20;

👉 These **bitwise operations** (rotate, shift, XOR, AND, OR) give SHA-256 its **preimage, second preimage, and collision resistance**.

***

#### 3. Final Integration

At the end of 64 rounds, the **working variables (a–h)** are added back to the **initial values** to produce the **chaining variables**:

```
hv0 := a + initialValues[0]
hv1 := b + initialValues[1]
hv2 := c + initialValues[2]
hv3 := d + initialValues[3]
hv4 := e + initialValues[4]
hv5 := f + initialValues[5]
hv6 := g + initialValues[6]
hv7 := h + initialValues[7]

fmt.Printf("Output: [%x, %x, %x, %x, %x, %x, %x, %x]\n", hv0,hv1,hv2,hv3,hv4,hv5,hv6,hv7)
```

&#x20;

For the input `"abc"`, the final digest is:

```
[ba7816bf, 8f01cfea, 414140de, 5dae2223, 
 b00361a3, 96177a9c, b410ff61, f20015ad]
```

This concatenated result is the **SHA-256 hash output**.

***

✅ **Summary:**

* Registers are **initialized** from prime-based constants (or chaining values).
* Each message schedule word passes through **64 rounds** of **bitwise mixing**.
* The updated registers are combined into the **final digest**.

👉 This iterative process makes SHA-256 both **efficient** and **secure**, powering Bitcoin’s **proof-of-work** and **transaction integrity**.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hub.bsvblockchain.org/higher-learning/bsv-academy/bitcoin-primitives-hash-functions/walkthrough-implementation-of-sha-256-in-golang-overview/sha-256-compression.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
