Build SHA-256 and HMAC from scratch
Stage 3 of 7v2 · e462126c

Message schedule

Expand sixteen initial words into the sixty-four words for each block.

Build it in your language

The tester treats your program as a black box: any language works. These are just the most convenient ones for this project.

Message Schedule Expansion (sigma0sigma_0 and sigma1sigma_1)

From the initial 16 words (W0…W15W_0 \dots W_{15}), SHA-256 expands the schedule to 64 words (W0…W63W_0 \dots W_{63}) using bitwise rotations and shifts.

Mental Model: Schedule Recurrence Relation

For any index i≥16i \ge 16:

Wi=sigma1(Wi−2)+Wi−7+sigma0(Wi−15)+Wi−16pmod232W_i = sigma_1(W_{i-2}) + W_{i-7} + sigma_0(W_{i-15}) + W_{i-16} pmod{2^{32}}

Where:

  • sigma0(x)=ROTR7(x)⊕ROTR18(x)⊕SHR3(x)sigma_0(x) = \text{ROTR}^7(x) \oplus \text{ROTR}^{18}(x) \oplus \text{SHR}^3(x)
  • sigma1(x)=ROTR17(x)⊕ROTR19(x)⊕SHR10(x)sigma_1(x) = \text{ROTR}^{17}(x) \oplus \text{ROTR}^{19}(x) \oplus \text{SHR}^{10}(x) All additions are modulo 2322^{32}.

Your Goal in this Stage

Read a JSON object from stdin with op: "schedule-word", previous words, and target index. Compute WiW_i under "word".

Input Format

json
{
  "op": "schedule-word",
  "words": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24],
  "index": 16
}

Output Format

json
{
  "word": 0
}

Step-by-Step Implementation Guide

  1. Bitwise helpers: Define 32-bit ROTR and SHR.
  2. Define sigma0sigma_0 and sigma1sigma_1: Apply XOR bitwise combinations.
  3. Calculate word: Sum mod 2322^{32} using >>> 0.
  4. Emit output: Print JSON with word.

Acceptance Criteria

  • ✅ Exact implementation of sigma0sigma_0 and sigma1sigma_1.
  • ✅ Strict modulo 2322^{32} unsigned arithmetic.

Output and errors

Multiline examples are formatted for readability. Emit compact JSON without optional spaces, with properties in the order shown by the output examples, and a final newline. Keep logs off stdout. Tests compare exact text. Invalid inputs use stderr containing error, empty stdout, and exit 1. Valid inputs exit 0. Serialize integer results as JSON integers.