Here's a self-contained Verilog implementation for computing the SHA-256 message schedule for words t = 16 to 63:
verilog
// SHA-256 Message Schedule Generator
// Computes W[16] through W[63] from initial W[0] through W[15]
module sha256_message_schedule (
input wire clk,
input wire reset,
input wire start,
input wire [31:0] w_init [0:15], // Initial 16 words from message block
output reg [31:0] w_out [16:63], // Extended message schedule
output reg ready
);
// Internal storage for all 64 words
reg [31:0] w [0:63];
reg [5:0] t; // Counter for current word being computed
reg computing;
// Sigma functions for message schedule
function [31:0] sigma0;
input [31:0] x;
begin
sigma0 = {x[6:0], x[31:7]} ^ {x[17:0], x[31:18]} ^ (x >> 3);
end
endfunction
function [31:0] sigma1;
input [31:0] x;
begin
sigma1 = {x[16:0], x[31:17]} ^ {x[18:0], x[31:19]} ^ (x >> 10);
end
endfunction
always @(posedge clk or posedge reset) begin
if (reset) begin
t <= 6'd16;
computing <= 1'b0;
ready <= 1'b0;
// Clear output array
for (integer i = 16; i < 64; i = i + 1) begin
w_out[i] <= 32'h0;
end
end
else if (start && !computing) begin
// Load initial 16 words
for (integer i = 0; i < 16; i = i + 1) begin
w[i] <= w_init[i];
end
t <= 6'd16;
computing <= 1'b1;
ready <= 1'b0;
end
else if (computing) begin
if (t < 64) begin
// Compute W[t] = sigma1(W[t-2]) + W[t-7] + sigma0(W[t-15]) + W[t-16]
w[t] <= sigma1(w[t-2]) + w[t-7] + sigma0(w[t-15]) + w[t-16];
t <= t + 1;
end
else begin
// Copy computed words to output
for (integer i = 16; i < 64; i = i + 1) begin
w_out[i] <= w[i];
end
computing <= 1'b0;
ready <= 1'b1;
end
end
end
endmodule
// Alternative combinational version for single-cycle computation
module sha256_message_schedule_comb (
input wire [31:0] w_init [0:15], // Initial 16 words
output wire [31:0] w_out [16:63] // Extended message schedule
);
// Internal wire array for all words
wire [31:0] w [0:63];
// Connect initial words
genvar i;
generate
for (i = 0; i < 16; i = i + 1) begin : init_words
assign w[i] = w_init[i];
end
endgenerate
// Sigma functions
function [31:0] sigma0;
input [31:0] x;
begin
sigma0 = {x[6:0], x[31:7]} ^ {x[17:0], x[31:18]} ^ (x >> 3);
end
endfunction
function [31:0] sigma1;
input [31:0] x;
begin
sigma1 = {x[16:0], x[31:17]} ^ {x[18:0], x[31:19]} ^ (x >> 10);
end
endfunction
// Generate words 16 through 63
generate
for (i = 16; i < 64; i = i + 1) begin : gen_words
assign w[i] = sigma1(w[i-2]) + w[i-7] + sigma0(w[i-15]) + w[i-16];
assign w_out[i] = w[i];
end
endgenerate
endmodule
// Testbench example
module tb_sha256_message_schedule;
reg clk, reset, start;
reg [31:0] w_init [0:15];
wire [31:0] w_out [16:63];
wire ready;
sha256_message_schedule dut (
.clk(clk),
.reset(reset),
.start(start),
.w_init(w_init),
.w_out(w_out),
.ready(ready)
);
// Clock generation
always #5 clk = ~clk;
initial begin
clk = 0;
reset = 1;
start = 0;
// Initialize with example message block (first 512 bits of "abc")
w_init[0] = 32'h61626380; // "abc" + padding
for (integer i = 1; i < 15; i = i + 1) begin
w_init[i] = 32'h00000000;
end
w_init[15] = 32'h00000018; // Length = 24 bits
#10 reset = 0;
#10 start = 1;
#10 start = 0;
// Wait for completion
wait(ready);
// Display some results
$display("W[16] = %h", w_out[16]);
$display("W[17] = %h", w_out[17]);
$display("W[63] = %h", w_out[63]);
$finish;
end
endmodule
This implementation provides two versions:
Sequential version (sha256_message_schedule): Computes one word per clock cycle, taking 48 cycles to complete. More resource-efficient for area-constrained designs.
Combinational version (sha256_message_schedule_comb): Computes all words in a single cycle using a cascade of combinational logic. Faster but uses more resources.
The sigma functions implement the SHA-256 specification:
σ₀(x) = ROTR⁷(x) ⊕ ROTR¹⁸(x) ⊕ SHR³(x)
σ₁(x) = ROTR¹⁷(x) ⊕ ROTR¹⁹(x) ⊕ SHR¹⁰(x)
Each word W[t] for t ∈ [16,63] is computed as:
W[t] = σ₁(W[t-2]) + W[t-7] + σ₀(W[t-15]) + W[t-16]
Choose the version that best fits your timing and resource requirements.
User
I need a self-contained set of functions, not modules.
Assistant
Here are the self-contained Verilog functions for computing the SHA-256 message schedule:
verilog
// SHA-256 Sigma functions for message schedule
function [31:0] sigma0;
input [31:0] x;
begin
// σ₀(x) = ROTR⁷(x) ⊕ ROTR¹⁸(x) ⊕ SHR³(x)
sigma0 = {x[6:0], x[31:7]} ^ {x[17:0], x[31:18]} ^ (x >> 3);
end
endfunction
function [31:0] sigma1;
input [31:0] x;
begin
// σ₁(x) = ROTR¹⁷(x) ⊕ ROTR¹⁹(x) ⊕ SHR¹⁰(x)
sigma1 = {x[16:0], x[31:17]} ^ {x[18:0], x[31:19]} ^ (x >> 10);
end
endfunction
// Compute single message schedule word W[t] given previous words
function [31:0] compute_w;
input [31:0] w_t_minus_2; // W[t-2]
input [31:0] w_t_minus_7; // W[t-7]
input [31:0] w_t_minus_15; // W[t-15]
input [31:0] w_t_minus_16; // W[t-16]
begin
// W[t] = σ₁(W[t-2]) + W[t-7] + σ₀(W[t-15]) + W[t-16]
compute_w = sigma1(w_t_minus_2) + w_t_minus_7 + sigma0(w_t_minus_15) + w_t_minus_16;
end
endfunction
// Compute all message schedule words W[16] through W[63]
// Input: w_in[0:15] - initial 16 words from message block
// Output: w_out[0:63] - complete message schedule (includes input words)
task compute_message_schedule;
input [31:0] w_in [0:15];
output [31:0] w_out [0:63];
integer t;
begin
// Copy initial 16 words
for (t = 0; t < 16; t = t + 1) begin
w_out[t] = w_in[t];
end
// Compute words 16 through 63
for (t = 16; t < 64; t = t + 1) begin
w_out[t] = compute_w(w_out[t-2], w_out[t-7], w_out[t-15], w_out[t-16]);
end
end
endtask
// Alternative: Compute only the extended words W[16] through W[63]
// Input: w_in[0:15] - initial 16 words from message block
// Output: w_ext[16:63] - only the computed extended words
task compute_extended_schedule;
input [31:0] w_in [0:15];
output [31:0] w_ext [16:63];
reg [31:0] w_temp [0:63];
integer t;
begin
// Copy initial words to temporary array
for (t = 0; t < 16; t = t + 1) begin
w_temp[t] = w_in[t];
end
// Compute and store extended words
for (t = 16; t < 64; t = t + 1) begin
w_temp[t] = compute_w(w_temp[t-2], w_temp[t-7], w_temp[t-15], w_temp[t-16]);
w_ext[t] = w_temp[t];
end
end
endtask
Usage example within your module:
verilog
module your_sha256_module;
reg [31:0] message_words [0:15]; // Input message block
reg [31:0] w_schedule [0:63]; // Complete message schedule
reg [31:0] w_extended [16:63]; // Just the extended words
// Include the functions above here
always @(*) begin
// Compute complete message schedule
compute_message_schedule(message_words, w_schedule);
// Or compute just the extended words
compute_extended_schedule(message_words, w_extended);
// Use individual word computation
// w_schedule[16] = compute_w(w_schedule[14], w_schedule[9], w_schedule[1], w_schedule[0]);
end
endmodule
These functions provide:
sigma0 and sigma1: The SHA-256 sigma functions for message schedule computation
compute_w: Computes a single message schedule word given the four required previous words
compute_message_schedule: Task that computes the complete 64-word message schedule
compute_extended_schedule: Task that computes only words 16-63, returning just the extended portion
You can use these functions directly in your combinational logic or within always blocks as needed for your specific implementation requirements.