# SHA-256 Input and Processing

{% embed url="<https://youtu.be/dR-wIFmAd3Q>" %}

#### Input

Hash functions are only useful if we can provide them with an **input value**.

* In our GoLang command-line implementation, the input is a **string**, which we then represent as an **array of bytes**.

Example:

```
message := []byte("abc")
fmt.Printf("Output: %b", message) 
// Output: [1100001 1100010 1100011]
```

👉 Here, the string `"abc"` is converted into its **binary representation**.

***

#### Message Block Creation

Next, we turn the input into a **message block** (or multiple blocks).

* Each block must be **448 bits long (congruent to 512 bits)**.
* To achieve this, we **pad the input** so that it’s exactly **64 bits short of a multiple of 512**.

**Padding process:**

1. Append a **single 1 bit** to the message.
2. Add **zero bits** until the total length is 64 bits short of 512.
3. Reserve the final 64 bits to store the **message length**.

Example logic in Go:

```
func AddZeroPaddingBits(messageBlock []byte, message []byte) []byte {
    zeros := make([]byte, HowMuchPadding(message))
    zeros[0] += 0x80 // add separator bit
    return append(messageBlock, zeros...)
}
```

#### Adding the Message Length

The last step in building the message block is to append the **length of the input message** (in bits).

* For `"abc"`, length = **3 bytes = 24 bits**.
* This value is appended as a **64-bit unsigned integer**.

```
func AddMessageLength(messageBlock []byte, message []byte) []byte {
    messageLength := len(message) << 3
    messageLengthInBits := make([]byte, 8)
    binary.BigEndian.PutUint64(messageLengthInBits[:], uint64(messageLength))
    return append(messageBlock, messageLengthInBits...)
}
```

👉 The result is a **padded message block**, exactly **512 bits (64 bytes)** long.

***

#### Message Schedule Creation

Once the message block is ready, SHA-256 constructs a **message schedule** of **64 words** (each 32 bits).

* The first **16 words** come directly from the message block.
* The remaining **48 words** are computed using **bitwise logical functions**.
* All sums are computed **modulo 2³²**, which is handled automatically when using `uint32` in Go.

Formula:

```
Wt = σ1(Wt-2) + Wt-7 + σ0(Wt-15) + Wt-16
```

&#x20;

Bitwise functions:

* **σ0(x) = (ROTR 7) XOR (ROTR 18) XOR (SHR 3)**
* **σ1(x) = (ROTR 17) XOR (ROTR 19) XOR (SHR 10)**

👉 ROTR = rotate right, SHR = shift right, XOR = exclusive OR.

Example Go function:

```
func BuildMessageSchedule(message []byte) [64]uint32 {
    schedule := getFirst16Words(BuildMessageBlocks(message))
    schedule = getRemaining48Words(schedule)
    return schedule
}
```

#### Example Output

After parsing `"abc"` and building the schedule:

* **First 16 words** match the message block.
* **Remaining 48 words** are generated by logical operations.

👉 This schedule is then used in the **compression step** of SHA-256.

***

✅ **Summary:**

1. **Input** string → bytes.
2. **Message block creation** → padded to 512 bits with a 64-bit length suffix.
3. **Message schedule creation** → 64 words, combining original message + derived words.

This process prepares the data for the **compression function**, where the actual SHA-256 digest is computed.


---

# 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-input-and-processing.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.
