feat(tb): add KAT testbench for mlkem_top (ML-KEM-512)

- gen_vectors.py: parse kat_MLKEM_512.rsp, generate hex vectors
- tb_mlkem_top_xsim.v: force-inject d/msg/z for KAT testing
- mlkem_top_input.hex: 5 vectors (d + msg + z)
- mlkem_top_expected.hex: 5 vectors (pk + sk + ct + ss)
- xsim_run.tcl: full dependency chain compilation

Known issue: mlkem_top FSM has combinational race on rng_valid_i
- rng_valid_i driven by state_r (registered) causes rng_sync
  to miss valid_i pulse when state transitions at posedge
- Fix: change rng_valid_i to use state_next pattern
  (same as sha3_top uses state_next for kc_valid_i)
This commit is contained in:
2026-06-27 01:07:34 +08:00
parent e3e02fc7ee
commit 0e6798beb5
5 changed files with 852 additions and 0 deletions

View File

@@ -0,0 +1,187 @@
# NOTE: On some systems, you may need:
# export LD_PRELOAD=/usr/lib64/libtinfo.so.5
# before running this script.
# xsim_run.tcl - Vivado xsim compilation and simulation for mlkem_top KAT testbench
#
# Compiles ALL RTL dependencies for mlkem_top plus the testbench.
# Run from the project root: ~/Dev/mlkem/
#
# Prerequisites:
# source /opt/Xilinx/Vivado/2019.2/settings64.sh
#
# Usage examples:
# # Run mlkem_top KAT testbench
# xsim tb_mlkem_top_xsim -R
#
# # Step-by-step:
# vivado -mode batch -source sync_rtl/top/TB/xsim_run.tcl
#
# # Or via run_tb.sh:
# ./run_tb.sh mlkem_top
# ================================================================
# Configuration
# ================================================================
set COMMON_DIR sync_rtl/common
set SHA3_DIR sync_rtl/sha3
set SHA3C_DIR sync_rtl/sha3_chain
set RNG_DIR sync_rtl/rng
set NTT_DIR sync_rtl/ntt
set PA_DIR sync_rtl/poly_arith
set PM_DIR sync_rtl/poly_mul
set CBD_DIR sync_rtl/sample_cbd
set SNT_DIR sync_rtl/sample_ntt
set CD_DIR sync_rtl/comp_decomp
set MA_DIR sync_rtl/mod_add
set STOR_DIR sync_rtl/storage
set TOP_DIR sync_rtl/top
set TB_DIR sync_rtl/top/TB
# ================================================================
# Step 1: Compile common infrastructure
# ================================================================
puts "=== Compiling common infrastructure ==="
# Pipeline register (used by many modules)
xvlog -sv -i . ${COMMON_DIR}/pipeline_reg.v
# Skid buffer (backpressure buffer)
xvlog -sv -i . ${COMMON_DIR}/skid_buffer.v
# ================================================================
# Step 2: Compile SHA3 / Keccak core
# ================================================================
puts "=== Compiling SHA3/Keccak core ==="
# Keccak round (combinational, θ/ρ/π/χ/ι)
xvlog -sv ${SHA3_DIR}/keccak_round.v
# Keccak core (24-round sequential keccak-f[1600])
xvlog -sv ${SHA3_DIR}/keccak_core.v
# SHA3 top wrapper (G/H/J modes, with internal keccak_core)
xvlog -sv -i . ${SHA3_DIR}/sha3_top.v
# ================================================================
# Step 3: Compile SHA3 chain (G function)
# ================================================================
puts "=== Compiling SHA3 chain ==="
# sha3_chain_top_shared (G: d → rho, sigma, with external keccak_core)
xvlog -sv -i . ${SHA3C_DIR}/sha3_chain_top_shared.v
# ================================================================
# Step 4: Compile RNG
# ================================================================
puts "=== Compiling RNG ==="
# rng_sync (256-bit Galois LFSR)
xvlog -sv ${RNG_DIR}/rng_sync.v
# ================================================================
# Step 5: Compile NTT core and dependencies
# ================================================================
puts "=== Compiling NTT core ==="
# Zeta ROM (twiddle factors, 128 × 12-bit)
xvlog -sv ${NTT_DIR}/zeta_rom.v
# Barrett modular multiplier (a·b mod q)
xvlog -sv ${NTT_DIR}/barrett_mul.v
# Butterfly unit (CT/GS butterfly for NTT/INTT)
xvlog -sv ${NTT_DIR}/butterfly_unit.v
# NTT core (256-coeff NTT/INTT FSM)
xvlog -sv -i . ${NTT_DIR}/ntt_core.v
# ================================================================
# Step 6: Compile polynomial arithmetic
# ================================================================
puts "=== Compiling polynomial arithmetic ==="
# poly_arith_sync (element-wise poly add/sub)
xvlog -sv ${PA_DIR}/poly_arith_sync.v
# ================================================================
# Step 7: Compile polynomial multiplication
# ================================================================
puts "=== Compiling polynomial multiplication ==="
# Zeta ROM for poly_mul (degree-1 basecase)
xvlog -sv ${PM_DIR}/poly_mul_zeta_rom.v
# Basecase multiplier (degree-1 Karatsuba)
xvlog -sv ${PM_DIR}/basecase_mul.v
# poly_mul_sync (NTT-domain polynomial multiplier)
xvlog -sv -i . ${PM_DIR}/poly_mul_sync.v
# ================================================================
# Step 8: Compile sampling modules
# ================================================================
puts "=== Compiling sampling modules ==="
# sample_cbd_sync_shared (CBD sampling with external keccak)
xvlog -sv -i . ${CBD_DIR}/sample_cbd_sync_shared.v
# sample_ntt_sync_shared (SampleNTT with external keccak)
xvlog -sv -i . ${SNT_DIR}/sample_ntt_sync_shared.v
# ================================================================
# Step 9: Compile compression and modular arithmetic
# ================================================================
puts "=== Compiling compression and modular arithmetic ==="
# comp_decomp_sync (Compress_q / Decompress_q)
xvlog -sv ${CD_DIR}/comp_decomp_sync.v
# mod_add_sync ((a + b) mod q, streaming)
xvlog -sv ${MA_DIR}/mod_add_sync.v
# ================================================================
# Step 10: Compile storage (BRAM)
# ================================================================
puts "=== Compiling storage BRAMs ==="
# Single-port BRAM
xvlog -sv ${STOR_DIR}/s_bram.v
# Simple dual-port BRAM
xvlog -sv ${STOR_DIR}/sd_bram.v
# ================================================================
# Step 11: Compile top-level integration
# ================================================================
puts "=== Compiling top-level integration ==="
# keccak_arbiter (round-robin arbiter for shared keccak)
xvlog -sv -i . ${TOP_DIR}/keccak_arbiter.v
# mlkem_top (top-level KeyGen/Encaps/Decaps FSM)
xvlog -sv -i . ${TOP_DIR}/mlkem_top.v
# ================================================================
# Step 12: Compile testbench
# ================================================================
puts "=== Compiling testbench ==="
# tb_mlkem_top_xsim (KAT vector testbench)
xvlog -sv ${TB_DIR}/tb_mlkem_top_xsim.v
# ================================================================
# Step 13: Elaborate snapshot (xelab)
# ================================================================
puts "=== Elaborating snapshot ==="
xelab tb_mlkem_top_xsim -s tb_mlkem_top_xsim --timescale 1ns/1ps
# ================================================================
# Step 14: Run simulation
# ================================================================
puts ""
puts "=== Running mlkem_top KAT simulation ==="
xsim tb_mlkem_top_xsim -R
puts ""
puts "=== mlkem_top simulation complete ==="