Of course. Here is a self-contained Verilog module that implements the SHA-256 message schedule expansion for t = 16 to 63. This design encapsulates the required σ₀ (sigma0) and σ₁ (sigma1) functions and the final summation logic.
The logic is purely combinational, meaning it calculates the output W_t based on its inputs in the same clock cycle. You would typically integrate this module into a larger, sequential SHA-256 core that provides the necessary previous W words from a register file or shift register.
SHA-256 Message Schedule Formulas
First, let's recall the formulas from the FIPS 180-4 standard for 16 ≤ t ≤ 63:
ROTRⁿ(x) is a circular right shift (rotate) of the 32-bit word x by n bits.
SHRⁿ(x) is a logical right shift of the 32-bit word x by n bits.
⊕ is the bitwise XOR operation.
+ is addition modulo 2³². In Verilog, this is the default behavior for standard addition with fixed-size registers.
Self-Contained Verilog Module
This single file contains the sha256_message_schedule module and the two required functions, sigma0 and sigma1.
verilog
`timescale 1ns / 1ps
//
// Copyright 2023 - Your Name/Company
//
// This module is free software; you can redistribute it and/or modify it
// under the terms of the MIT license.
//
//
// Self-contained Verilog implementation of the SHA-256 Message Schedule Expansion.
//
// This module computes W_t for t = 16 to 63 based on the formula from FIPS 180-4:
// W_t = sigma1(W_{t-2}) + W_{t-7} + sigma0(W_{t-15}) + W_{t-16}
//
// This is a purely combinational block.
//
module sha256_message_schedule (
// Inputs: The four previous words required for the calculation.
input wire [31:0] w_t_minus_2,
input wire [31:0] w_t_minus_7,
input wire [31:0] w_t_minus_15,
input wire [31:0] w_t_minus_16,
// Output: The newly computed message schedule word.
output wire [31:0] w_t
);
//================================================================
// Internal Functions (as defined in FIPS 180-4, Section 4.1.2)
//================================================================
// sigma0(x) = ROTR^7(x) ^ ROTR^18(x) ^ SHR^3(x)
function [31:0] sigma0 (input [31:0] x);
begin
sigma0 = ( (x >> 7) | (x << (32-7)) ) ^
( (x >> 18) | (x << (32-18)) ) ^
( x >> 3 );
end
endfunction
// sigma1(x) = ROTR^17(x) ^ ROTR^19(x) ^ SHR^10(x)
function [31:0] sigma1 (input [31:0] x);
begin
sigma1 = ( (x >> 17) | (x << (32-17)) ) ^
( (x >> 19) | (x << (32-19)) ) ^
( x >> 10 );
end
endfunction
//================================================================
// Message Schedule Logic
//================================================================
// Intermediate wires for the results of the sigma functions.
// This improves readability.
wire [31:0] s0_result;
wire [31:0] s1_result;
// Apply the sigma functions to the appropriate input words.
assign s0_result = sigma0(w_t_minus_15);
assign s1_result = sigma1(w_t_minus_2);
// Compute the final W_t value.
// Verilog's '+' operator performs addition modulo 2^32 by default
// when using 32-bit operands, which is exactly what SHA-256 requires.
assign w_t = s1_result + w_t_minus_7 + s0_result + w_t_minus_16;
endmodule
How to Use This Module
You would instantiate this module within your main SHA-256 core. Your core will be responsible for managing the 64-word message schedule array, often denoted as W[0..63].
Initial Words (W[0] to W[15]): These 16 words are derived directly from the 512-bit message block you are processing. Your main controller will load these first.
Expanded Words (W[16] to W[63]): For each round t from 16 to 63, you will use this sha256_message_schedule module. The inputs to the module will be read from your message schedule storage (e.g., a register file or a carefully designed shift register).
Example Instantiation
Imagine your SHA-256 core has a register file reg [31:0] W[0:15]; that acts as a sliding window for the required words. On each clock cycle corresponding to round t (where t >= 16), you would wire it up like this:
verilog
// In your top-level SHA-256 core module...
// A register file to hold the necessary W words.
// In a real implementation, this would be a shift register for efficiency.
reg [31:0] w_reg_file [0:15];
// The word generated for the current round t.
wire [31:0] w_t_generated;
// Instantiate the message schedule calculation module
sha256_message_schedule w_gen_inst (
.w_t_minus_2 ( w_reg_file[14] ), // Corresponds to W[t-2]
.w_t_minus_7 ( w_reg_file[9] ), // Corresponds to W[t-7]
.w_t_minus_15 ( w_reg_file[1] ), // Corresponds to W[t-15]
.w_t_minus_16 ( w_reg_file[0] ), // Corresponds to W[t-16]
.w_t ( w_t_generated ) // Output W[t]
);
// In a clocked process (always @(posedge clk)), you would update
// the register file for the next round.
// always @(posedge clk) begin
// // Shift the registers left
// w_reg_file[0] <= w_reg_file[1];
// w_reg_file[1] <= w_reg_file[2];
// ...
// w_reg_file[14] <= w_reg_file[15];
// // Load the newly generated word
// w_reg_file[15] <= w_t_generated;
// end
This self-contained module provides a clean, readable, and verifiable implementation of the core message expansion logic, making it easy to integrate into a larger SHA-256 hardware design.
User
I need a self-contained set of functions, not modules.
Assistant
Of course. Using Verilog functions is an excellent way to encapsulate this logic, especially since it's purely combinational. Functions can be synthesized into logic gates just like module-based code but are often more convenient for reusable, stateless calculations.
Here is a self-contained set of Verilog functions, neatly packaged for easy reuse, that compute the SHA-256 message schedule expansion.
Verilog Package with SHA-256 Functions
It's a best practice in modern Verilog (SystemVerilog) to place related functions into a package. This allows you to compile them once and import them into any module that needs them, preventing code duplication and improving organization.
verilog
//
// Copyright 2023 - Your Name/Company
//
// This package is free software; you can redistribute it and/or modify it
// under the terms of the MIT license.
//
//
// This package provides a self-contained set of pure, combinational functions
// for the SHA-256 message schedule expansion (W_t for t=16..63).
//
// The functions are defined according to FIPS 180-4, Section 4.1.2.
//
package sha256_functions;
//================================================================
// Helper Functions: sigma0 and sigma1
//================================================================
// sigma0(x) = ROTR^7(x) ^ ROTR^18(x) ^ SHR^3(x)
// ROTR is a circular right shift (rotate).
// SHR is a logical right shift.
// ^ is a bitwise XOR.
function automatic [31:0] sigma0 (input [31:0] x);
return ( (x >> 7) | (x << (32-7)) ) ^
( (x >> 18) | (x << (32-18)) ) ^
( x >> 3 );
endfunction : sigma0
// sigma1(x) = ROTR^17(x) ^ ROTR^19(x) ^ SHR^10(x)
function automatic [31:0] sigma1 (input [31:0] x);
return ( (x >> 17) | (x << (32-17)) ) ^
( (x >> 19) | (x << (32-19)) ) ^
( x >> 10 );
endfunction : sigma1
//================================================================
// Main Message Schedule Expansion Function
//================================================================
// W_t = sigma1(W_{t-2}) + W_{t-7} + sigma0(W_{t-15}) + W_{t-16}
// Verilog's '+' on fixed-width operands performs addition modulo 2^32,
// which is the required behavior.
function automatic [31:0] sha256_expand_w (
input [31:0] w_t_minus_2,
input [31:0] w_t_minus_7,
input [31:0] w_t_minus_15,
input [31:0] w_t_minus_16
);
return sigma1(w_t_minus_2) + w_t_minus_7 + sigma0(w_t_minus_15) + w_t_minus_16;
endfunction : sha256_expand_w
endpackage : sha256_functions
Key Features and Explanation
package sha256_functions;: This defines a namespace for our functions, preventing name clashes with other parts of your design.
function automatic: The automatic keyword ensures the function is reentrant, meaning its internal variables are allocated dynamically for each concurrent call. This is essential for functions used in synthesizable hardware design.
sigma0 and sigma1: These are direct translations of the formulas from the FIPS 180-4 standard. They implement the required bitwise rotations (ROTR), shifts (SHR), and XORs.
sha256_expand_w: This is the primary function you will call. It takes the four required previous words from the message schedule (W[t-2], W[t-7], W[t-15], W[t-16]) as inputs. It then calls the sigma0 and sigma1 helper functions and performs the final addition to compute the new word, W[t].
Synthesizable: This code is fully synthesizable. A synthesis tool will convert these functions into a netlist of combinational logic gates (adders, XORs, and multiplexers from the shifters).
How to Use the Functions in Your Verilog Module
Save the code above into a file named sha256_functions.sv.
Compile this file along with your other design files.
In the module where you need to calculate the message schedule, import the package.
Here is a simple example of how you would use it:
verilog
// Import all items (*) from the package.
import sha256_functions::*;
module sha256_core (
input clk,
input rst_n,
// ... other inputs/outputs
// Example: Wires representing the required previous words
// In a real design, these would come from a register file or shift register.
input wire [31:0] w_t_m2,
input wire [31:0] w_t_m7,
input wire [31:0] w_t_m15,
input wire [31:0] w_t_m16,
// Output for the newly generated word
output wire [31:0] w_t_next
);
// Call the function directly in a continuous assignment.
// The synthesis tool will create the combinational logic for this.
assign w_t_next = sha256_expand_w(
.w_t_minus_2(w_t_m2),
.w_t_minus_7(w_t_m7),
.w_t_minus_15(w_t_m15),
.w_t_minus_16(w_t_m16)
);
// This w_t_next would then be used in the current round's calculation
// and stored to become a future w_t_mX value in a clocked process.
endmodule
This approach provides a clean, modular, and highly reusable solution for the SHA-256 message schedule logic.