Below is one acceptable solution. In SHA‑256 the message schedule words ( W[t] ) (for ( t \ge 16 )) are computed as follows:
[
W[t] = \sigma_1(W[t-2]) + W[t-7] + \sigma_0(W[t-15]) + W[t-16]
]
where the functions (\sigma_0) and (\sigma_1) are defined by
[
\sigma_0(x) = \text{ROTR}^7(x) \oplus \text{ROTR}^{18}(x) \oplus \text{SHR}^3(x)
]
[
\sigma_1(x) = \text{ROTR}^{17}(x) \oplus \text{ROTR}^{19}(x) \oplus \text{SHR}^{10}(x)
]
and
- ROTR(x, n) is a circular right rotation of x by n bits, and
- SHR(x, n) is a right shift (logical).
One way to implement this in Verilog is to write helper functions for the rotations and the two sigma functions. Then you can create a function that computes one word of the message schedule given the four previous words. (A designer might call the arguments, for example, w_m16, w_m15, w_m7, and w_m2 representing (W[t-16]), (W[t-15]), (W[t-7]) and (W[t-2]) respectively.)
Below is the complete Verilog code with these functions:
// Compute a right rotation of a 32-bit word.
function automatic [31:0] rotr(input [31:0] x, input [4:0] shift);
begin
rotr = (x >> shift) | (x << (32 - shift));
end
endfunction
// Implements σ0(x) = ROTR7(x) XOR ROTR18(x) XOR SHR3(x)
function automatic [31:0] sigma0(input [31:0] x);
begin
sigma0 = rotr(x, 7) ^ rotr(x, 18) ^ (x >> 3);
end
endfunction
// Implements σ1(x) = ROTR17(x) XOR ROTR19(x) XOR SHR10(x)
function automatic [31:0] sigma1(input [31:0] x);
begin
sigma1 = rotr(x, 17) ^ rotr(x, 19) ^ (x >> 10);
end
endfunction
// Computes one word of the message schedule.
// Inputs:
// w_m16 : W[t-16]
// w_m15 : W[t-15]
// w_m7 : W[t-7]
// w_m2 : W[t-2]
//
// According to the SHA-256 standard:
// W[t] = σ1(W[t-2]) + W[t-7] + σ0(W[t-15]) + W[t-16]
function automatic [31:0] compute_message_schedule(
input [31:0] w_m16,
input [31:0] w_m15,
input [31:0] w_m7,
input [31:0] w_m2
);
begin
compute_message_schedule = sigma1(w_m2) + w_m7 + sigma0(w_m15) + w_m16;
end
endfunction
Explanation
-
rotr Function:
This function rotates the 32-bit input x to the right by the number of positions specified in shift. It uses the trick of right-shifting and left-shifting by the complementary amount, then OR’ing the results.
-
sigma0 and sigma1 Functions:
Each of these implements the non-linear functions used in the SHA‑256 schedule. They use the rotr function along with a logical shift right (using the >> operator).
-
compute_message_schedule Function:
This function takes four previously computed schedule words and returns the new word ( W[t] ) using the SHA‑256 formula:
[
W[t] = \sigma_1(W[t-2]) + W[t-7] + \sigma_0(W[t-15]) + W[t-16].
]
Any functionally equivalent solution is acceptable as an answer. This code can be placed within a Verilog module where you need to compute the message schedule.