Fresh valid/ready KeyGen FSM for ML-KEM-512 (K=2, eta1=3). Independent keccak per consumer (no arbiter). Verified stage-by-stage vs ml-kem-r golden: - 2a G(d||K): rho/sigma exact (d byte0-low, K at byte32, no reversal). - 2b SampleNTT: A_hat[i][j] from seed rho||j||i, 1024/1024 coeffs exact. - 2c CBD: s[i]=CBD3(PRF(sigma,i)), e[i]=CBD3(PRF(sigma,K+i)); signed->mod-q (+Q when negative); 2048/2048 (A+s+e) coeffs exact. polymem register array (10 slots x 256), debug readback tap (dbg_slot/idx -> coeff, rho/sigma taps) for stage TBs. a_busy/c_busy guards (defensive after sample_ntt fix). FSM: IDLE->G->A->C->DONE (datapath extended in later stages). Plan + progress doc in .claude/plans/keygen_plan.md.
61 lines
2.6 KiB
Verilog
61 lines
2.6 KiB
Verilog
// tb_mlkem_kg_2c_xsim.v - Stage 2c: verify A_hat + s + e stored in mlkem_top.
|
|
// Reads golden kg_c000_AsE.hex (8 polys x 256 = 2048 lines, mod-q) and checks
|
|
// polymem slots A00,A01,A10,A11,S0,S1,E0,E1 via the debug readback tap.
|
|
`timescale 1ns/1ps
|
|
module tb_mlkem_kg_2c_xsim;
|
|
reg clk=0, rst_n=0, start_i=0;
|
|
reg [255:0] d_i, z_i=0;
|
|
wire busy_o, done_o;
|
|
reg [3:0] dbg_slot_i=0; reg [7:0] dbg_idx_i=0; wire [11:0] dbg_coeff_o;
|
|
wire [255:0] dbg_rho_o, dbg_sigma_o;
|
|
|
|
mlkem_top #(.K(2)) dut (
|
|
.clk(clk), .rst_n(rst_n), .d_i(d_i), .z_i(z_i), .start_i(start_i),
|
|
.busy_o(busy_o), .done_o(done_o),
|
|
.dbg_slot_i(dbg_slot_i), .dbg_idx_i(dbg_idx_i), .dbg_coeff_o(dbg_coeff_o),
|
|
.dbg_rho_o(dbg_rho_o), .dbg_sigma_o(dbg_sigma_o)
|
|
);
|
|
always #5 clk = ~clk;
|
|
|
|
localparam [255:0] D_LIT = 256'h2426f1941779574d3f1b163bd57f7e173e229e630ec7f7073bdf365137c4bb6d;
|
|
|
|
reg [11:0] gold [0:2047];
|
|
// slot order matches golden file order
|
|
reg [3:0] slot_of [0:7];
|
|
integer c, p, idx, errors, gi;
|
|
|
|
initial begin
|
|
$readmemh("sync_rtl/top/TB/vectors/kg_c000_AsE.hex", gold);
|
|
slot_of[0]=4'd0; slot_of[1]=4'd1; slot_of[2]=4'd2; slot_of[3]=4'd3; // A00..A11
|
|
slot_of[4]=4'd4; slot_of[5]=4'd5; // S0,S1
|
|
slot_of[6]=4'd6; slot_of[7]=4'd7; // E0,E1
|
|
|
|
d_i = D_LIT;
|
|
rst_n=0; repeat(4) @(posedge clk); rst_n=1; @(posedge clk);
|
|
start_i=1; @(posedge clk); start_i=0;
|
|
c=0; while(!done_o && c<200000) begin @(posedge clk); c=c+1; end
|
|
if(!done_o) begin $display("FAIL: timeout after %0d cyc", c); $finish; end
|
|
$display("=== Stage 2c: A_hat + s + e (8 polys) === done in %0d cyc", c);
|
|
|
|
errors = 0;
|
|
for (p = 0; p < 8; p = p + 1) begin
|
|
for (idx = 0; idx < 256; idx = idx + 1) begin
|
|
dbg_slot_i = slot_of[p];
|
|
dbg_idx_i = idx[7:0];
|
|
@(posedge clk); @(posedge clk); // 2 cyc for registered readback
|
|
gi = p*256 + idx;
|
|
if (dbg_coeff_o !== gold[gi]) begin
|
|
if (errors < 8)
|
|
$display(" MISMATCH slot%0d[%0d]: got=%03x exp=%03x",
|
|
slot_of[p], idx, dbg_coeff_o, gold[gi]);
|
|
errors = errors + 1;
|
|
end
|
|
end
|
|
end
|
|
if (errors == 0) $display("ALL TESTS PASSED (2048/2048 coeffs)");
|
|
else $display("TESTS FAILED: %0d mismatches", errors);
|
|
$finish;
|
|
end
|
|
initial begin #5000000; $display("FAIL: global timeout"); $finish; end
|
|
endmodule
|