diff --git a/doc/reports/critical_path.png b/doc/reports/critical_path.png new file mode 100644 index 0000000..dcffc7a Binary files /dev/null and b/doc/reports/critical_path.png differ diff --git a/doc/reports/fsm.png b/doc/reports/fsm.png new file mode 100644 index 0000000..f3733ee Binary files /dev/null and b/doc/reports/fsm.png differ diff --git a/doc/reports/keccak_core.png b/doc/reports/keccak_core.png new file mode 100644 index 0000000..f3733ee Binary files /dev/null and b/doc/reports/keccak_core.png differ diff --git a/doc/reports/mlkem_ip_design_report.pdf b/doc/reports/mlkem_ip_design_report.pdf new file mode 100644 index 0000000..884384b Binary files /dev/null and b/doc/reports/mlkem_ip_design_report.pdf differ diff --git a/doc/reports/mlkem_ip_design_report.tex b/doc/reports/mlkem_ip_design_report.tex new file mode 100644 index 0000000..4aec977 --- /dev/null +++ b/doc/reports/mlkem_ip_design_report.tex @@ -0,0 +1,578 @@ +\documentclass[11pt,a4paper,twoside]{article} + +% ─── Packages ──────────────────────────────────────────────── +\usepackage{ctex} +\usepackage[top=2.5cm, bottom=2.5cm, left=2.8cm, right=2.8cm]{geometry} +\usepackage{graphicx} +\usepackage{float} +\usepackage{booktabs} +\usepackage{longtable} +\usepackage{multirow} +\usepackage{array} +\usepackage{amsmath,amssymb} +\usepackage{hyperref} +\usepackage{caption} +\usepackage{subcaption} +\usepackage{fancyhdr} +\usepackage{tikz} +\usepackage{enumitem} +\usepackage{listings} +\usepackage{xcolor} +\usepackage{algorithm} +\usepackage{algpseudocode} + +% ─── Styling ───────────────────────────────────────────────── +\hypersetup{colorlinks=true,linkcolor=black,citecolor=black,urlcolor=blue} +\pagestyle{fancy} +\fancyhead[LO,RE]{ML-KEM硬件IP设计报告} +\fancyhead[RO,LE]{\thepage} +\fancyfoot[C]{} +\setlength{\parskip}{6pt plus 2pt minus 1pt} +\setlength{\parindent}{2em} +\setlength{\headheight}{14pt} + +\lstset{basicstyle=\ttfamily\small,breaklines=true,frame=single,numbers=left,numberstyle=\tiny,xleftmargin=2em,language=Verilog} + +\newcommand{\Zq}{\mathbb{Z}_q} +\newcommand{\etal}{\textit{et al.}} + +\title{\textbf{ML-KEM后量子密钥封装硬件IP核\\设计与验证}} +\author{碳基生物\\[4pt] \small 团队编号:CICC1008439\\[8pt] \normalsize 龙芯中科杯·2026全国大学生集成电路创新创业大赛} +\date{\today} + +\begin{document} + +\maketitle +\tableofcontents +\newpage + +% ================================================================ +\section{绪论} + +\subsection{设计背景} + +ML-KEM(Module-Lattice-based Key-Encapsulation Mechanism,FIPS 203)是NIST于2024年8月发布的后量子密码标准,用于替代RSA和ECC在密钥交换中的角色。其安全性建立在模格上的带误差学习问题(MLWE)之上,多项式环为$\Zq[x]/(x^{256}+1)$,素数模$q=3329$,多项式次数$n=256$。 + +软件实现的ML-KEM在嵌入式处理器上存在显著性能瓶颈——以ML-KEM-768为例,一轮完整的密钥生成、封装和解封装在通用CPU上需要数百万个时钟周期。本文设计的硬件IP核\texttt{mlkem\_top}旨在通过专用数据通路和计算资源时分复用,在FPGA平台上实现高性能、低面积的ML-KEM加速。 + +\subsection{关键设计特性} + +本IP核在单一硬件模块内具有以下特性: + +\begin{enumerate}[label=(\arabic*)] + \item \textbf{运行时三等级支持}:通过$k\_i\in\{2,3,4\}$端口在运行时选择ML-KEM-512/768/1024安全等级,存储资源按$K_{MAX}=4$静态分配,无需重综合。 + \item \textbf{全操作覆盖}:支持KeyGen(FIPS 203算法16)、Encaps(算法17)和Decaps(算法18,含FO变换隐式拒绝)。 + \item \textbf{Keccak核跨阶段复用}:单一\texttt{keccak\_core}实例在G/H/J哈希、SampleNTT采样和CBD采样三个消费者之间时分复用,是面积优化的最大贡献因素。 + \item \textbf{Decaps重加密路径复用}:D6步的K-PKE重加密直接复用整条Encaps流水线的全部FSM状态,避免重复实现加密数据通路。 + \item \textbf{逐字节流式I/O}:公钥$ek$、私钥$dk$和密文$c$通过BRAM窗口逐字节装载与回读,免去超宽顶层总线。 + \item \textbf{NIST KAT完全验证}:三种等级、三种操作全部逐字节比对NIST FIPS 203 KAT标准答案通过。 +\end{enumerate} + +\subsection{代码规模} + +RTL代码总计约5,517行(不含测试平台),其中\texttt{mlkem\_top.v}(顶层FSM集成+数据通路+三库存储管理)约2,264行,各叶子算子模块合计约3,253行。全部采用SystemVerilog编写,统一使用\texttt{valid/ready}流式握手接口。 + +% ================================================================ +\section{IP模块规格} + +\subsection{算法参数} + +\texttt{mlkem\_top}的运行时参数由输入端口$k\_i$和$op\_i$在\texttt{start\_i}脉冲时刻锁存至内部寄存器$k\_r$和$op\_r$,后续所有数据通路宽度、存储子区间基地址和循环次数均由$k\_r$推导。表\ref{tab:params}列出三种安全等级的密钥和密文尺寸。 + +\begin{table}[H] +\centering +\caption{运行时安全等级参数} +\label{tab:params} +\begin{tabular}{cccccccc} +\toprule +$k\_i$ & 方案 & $K$ & $\eta_1$ & $(d_u,d_v)$ & ek(B) & dk(B) & ct(B) \\ +\midrule +2 & ML-KEM-512 & 2 & 3 & (10,4) & 800 & 1632 & 768 \\ +3 & ML-KEM-768 & 3 & 2 & (10,4) & 1184 & 2400 & 1088 \\ +4 & ML-KEM-1024 & 4 & 2 & (11,5) & 1568 & 3168 & 1568 \\ +\bottomrule +\end{tabular} +\end{table} + +固定参数:模数$q=3329$、多项式次数$n=256$、$\eta_2=2$(所有等级)、共享密钥32字节。 + +\subsection{顶层端口信号} + +\texttt{mlkem\_top}的端口按功能分为四组。表\ref{tab:ports}列出主要信号。 + +\begin{table}[H] +\centering +\caption{mlkem\_top主要端口} +\label{tab:ports} +\small +\begin{tabular}{lll} +\toprule +\textbf{信号} & \textbf{位宽} & \textbf{功能} \\ +\midrule +clk, rst\_n & 1, 1 & 时钟(25 MHz)与低电平异步复位 \\ +k\_i, op\_i & 3, 2 & 安全等级(2/3/4)与操作码(0=KG, 1=Enc, 2=Dec) \\ +d\_i, z\_i, msg\_i & 256×3 & KeyGen种子$d$、隐式拒绝种子$z$、Encaps消息$m$ \\ +start\_i & 1 & 启动脉冲,锁存$k\_i$和$op\_i$ \\ +busy\_o, done\_o & 1, 1 & 运行标志与完成脉冲 \\ +ss\_o & 256 & 共享密钥输出(done\_o时有效) \\ +ek\_in\_we/addr/byte & 1,11,8 & Encaps公钥流式预装载 \\ +dk\_in\_we/addr/byte & 1,12,8 & Decaps私钥流式预装载(自动路由至dkp\_bram/ek\_bram/寄存器) \\ +c\_in\_we/addr/byte & 1,11,8 & Decaps密文流式预装载 \\ +dbg\_* & 多种 & 调试抽头:系数、ek/dk/ct字节、中间哈希值 \\ +\bottomrule +\end{tabular} +\end{table} + +握手协议分三阶段:\texttt{ST\_IDLE}状态下通过流式端口逐字节预装载数据;\texttt{start\_i}脉冲启动并锁存参数;\texttt{busy\_o}拉高期间执行计算;\texttt{done\_o}脉冲时\texttt{ss\_o}和BRAM产物有效。所有256-bit端口采用字节0在低位的约定。 + +Decaps私钥预装载路径中,\texttt{dk\_in\_*}端口根据写入地址自动将字节流路由至不同目的地:地址$[0,384K)$写入\texttt{dkp\_bram}($\hat{s}$编码),$[384K,768K+32)$写入\texttt{ek\_bram}($\hat{t}$编码$\|\rho$),$[768K+32,768K+64)$和$[768K+64,768K+96)$分别写入$H(ek)$和$z$的寄存器。 + +% ================================================================ +\section{总体架构设计} + +\subsection{设计原则} + +\texttt{mlkem\_top}的架构遵循三条核心原则。 + +\textbf{原则一:存储按最大等级分配,运行时参数递推。} 多项式系数存储库以$K_{MAX}=4$(可容纳28个多项式,每个256系数)静态配置深度,$k\_r$仅影响读写地址生成逻辑中的基地址和循环边界。字节存储库(ek\_bram、dkp\_bram、ct\_bram)同样按最大密钥尺寸(1568/1536/1568字节)分配。运行时地址计算通过少量组合逻辑完成,无动态内存分配开销。 + +\textbf{原则二:Keccak核全阶段时分复用。} Keccak-f[1600]置换是设计中面积最大的单一算子(OOC综合约5,300 LUT)。G/H/J哈希族、SampleNTT(SHAKE-128)和CBD(SHAKE-256)三个消费者处于互斥的FSM阶段,设计将所有Keccak请求复用至单一\texttt{keccak\_core}实例。输入侧通过\texttt{sel\_sha3/sel\_snt/sel\_cbd}多路选择器切换,输出侧将置换结果广播至全体消费者,仅当前激活阶段采样有效信号。 + +\textbf{原则三:Decaps重加密完全复用Encaps流水线。} FIPS 203算法18的D6步需要以恢复的消息$m'$和随机种子$r'$重新执行一次K-PKE加密以产生参考密文$c'$。这一流程与算法17的Encaps操作的数据通路完全同构。设计将$m'$和$r'$分别写入原有的$m\_r$和$r\_r$寄存器,\texttt{ek\_bram}中保持原始公钥$ek$不变,随后将FSM直接转移至Encaps流水线的首个状态\texttt{ENC\_LOAD},整条15状态的Encaps数据通路被完整复用。 + +\subsection{模块层次结构} + +图\ref{fig:mlkem_modules}给出\texttt{mlkem\_top}的内部层次结构。设计分为三个逻辑层:控制调度层(FSM + 地址生成 + 数据通路复用)、算子计算层(7个已验证的叶子模块)和存储层(7个BRAM实例)。 + +\begin{figure}[H] +\centering +\includegraphics[width=\textwidth]{mlkem_modules.png} +\caption{mlkem\_top内部三层模块结构} +\label{fig:mlkem_modules} +\end{figure} + +\subsection{顶层FSM调度} + +全部运算由5-bit状态机集中调度,从\texttt{ST\_IDLE}出发按$op\_r$分支为三条数据通路。图\ref{fig:fsm}给出状态转移图。 + +\begin{figure}[H] +\centering +\includegraphics[width=\textwidth]{fsm.png} +\caption{mlkem\_top顶层FSM状态转移图} +\label{fig:fsm} +\end{figure} + +KeyGen路径对应FIPS 203算法16,共7个主状态(不含ST\_IDLE和ST\_DONE)。各状态间的数据传递通过多项式存储库完成:前序状态将结果写入BRAM,后续状态从BRAM读取。 + +\begin{algorithm}[H] +\caption{KeyGen数据通路(FIPS 203算法16)} +\begin{algorithmic}[1] +\State \textbf{ST\_G}:$G(d \| K) \rightarrow (\rho, \sigma)$,单块SHA3-512,约1,800周期 +\State \textbf{ST\_A}:对全部$K^2$个$(j,i)$索引执行SampleNTT$(\rho \| j \| i) \rightarrow \hat{A}[i][j]$,逐系数写入\texttt{bank\_a} +\State \textbf{ST\_C}:CBD$_{\eta_1}(\sigma, i) \rightarrow s[i]$,CBD$_{\eta_1}(\sigma, K+i) \rightarrow e[i]$,写入\texttt{bank\_se} +\State \textbf{ST\_N}:NTT$(s[i]) \rightarrow \hat{s}[i]$,NTT$(e[i]) \rightarrow \hat{e}[i]$,原位更新\texttt{bank\_se} +\State \textbf{ST\_M}:$\hat{t}[i] = \hat{e}[i] + \sum_{j=0}^{K-1} \hat{A}[i][j] \circ \hat{s}[j]$,逐点乘通过\texttt{poly\_mul\_sync},累加写入\texttt{bank\_t} +\State \textbf{ST\_E}:byteEncode$_{12}(\hat{t}) \| \rho \rightarrow ek$,byteEncode$_{12}(\hat{s}) \rightarrow dk_{pke}$,写入\texttt{ek\_bram}和\texttt{dkp\_bram} +\State \textbf{ST\_H}:SHA3-256$(ek) \rightarrow H(ek)$,多块吸收(块数$=\lceil(ek\_bytes+1)/136\rceil$) +\end{algorithmic} +\end{algorithm} + +Encaps路径对应FIPS 203算法17,共12个主状态。 + +\begin{algorithm}[H] +\caption{Encaps数据通路(FIPS 203算法17)} +\begin{algorithmic}[1] +\State \textbf{ST\_ENC\_H}:SHA3-256$(ek) \rightarrow H(ek)$ +\State \textbf{ST\_ENC\_G}:$G(m \| H(ek)) \rightarrow (K, r)$,单块SHA3-512 +\State \textbf{ST\_ENC\_A}:重新生成$\hat{A}^T$矩阵(SampleNTT,行列索引交换),写入\texttt{bank\_a} +\State \textbf{ST\_ENC\_TDEC}:byteDecode$_{12}(ek) \rightarrow \hat{t}[i]$,逐系数解码至\texttt{bank\_a} +\State \textbf{ST\_ENC\_C}:CBD$_{\eta_1}(r, 0) \rightarrow y$,CBD$_{\eta_2}(r, K) \rightarrow e_1$,CBD$_{\eta_2}(r, 2K) \rightarrow e_2$,写入\texttt{bank\_se}和\texttt{bank\_t} +\State \textbf{ST\_ENC\_N}:NTT$(y) \rightarrow \hat{y}$,原位更新\texttt{bank\_se} +\State \textbf{ST\_ENC\_U}:$\hat{A}^T \circ \hat{y}$逐点乘后INTT,再加$e_1$得$u$,写入\texttt{bank\_se} +\State \textbf{ST\_ENC\_C1}:Compress$_{d_u}(u)$后byteEncode$_{d_u} \rightarrow c_1$字节,写入\texttt{ct\_bram} +\State \textbf{ST\_ENC\_E2MV}:将$e_2$从\texttt{bank\_t}搬迁至\texttt{bank\_a},为V步累加准备 +\State \textbf{ST\_ENC\_V}:$\hat{t} \circ \hat{y}$逐点乘后INTT,加$e_2$和Decompress$_1(m)$得$v$,写入\texttt{bank\_t} +\State \textbf{ST\_ENC\_C2}:Compress$_{d_v}(v)$后byteEncode$_{d_v} \rightarrow c_2$字节,拼接至\texttt{ct\_bram} +\State \textbf{输出}:$ss = K$ +\end{algorithmic} +\end{algorithm} + +Decaps路径对应FIPS 203算法18,前5个主状态完成D1--D5步解密恢复,随后复用整条Encaps流水线(D6重加密),最后以一个比较状态(D7)完成隐式拒绝密钥选择。这是硬件设计中最复杂的控制流,也是Decaps流水线复用创新的集中体现。 + +\begin{algorithm}[H] +\caption{Decaps数据通路(FIPS 203算法18,含流水线复用)} +\begin{algorithmic}[1] +\State \textbf{ST\_DEC\_DECOMP}(D1):byteDecode$_{d_u}(c_1) \rightarrow$ Decompress$_{d_u} \rightarrow u'$,byteDecode$_{d_v}(c_2) \rightarrow$ Decompress$_{d_v} \rightarrow v'$,写入\texttt{bank\_se}和\texttt{bank\_a} +\State \textbf{ST\_DEC\_SDEC}(D2):byteDecode$_{12}(dk_{pke}) \rightarrow \hat{s}[i]$,逐系数解码至\texttt{bank\_a} +\State \textbf{ST\_DEC\_NTT}(D2续):NTT$(u'[i]) \rightarrow \hat{u}[i]$,原位更新\texttt{bank\_se} +\State \textbf{ST\_DEC\_W}(D3):$\hat{s}[j] \circ \hat{u}[j]$逐点乘后INTT,$v'$减去累加和得$w$,写入\texttt{bank\_t} +\State \textbf{ST\_DEC\_MENC}(D4):Compress$_1(w)$后byteEncode$_1 \rightarrow m'$ +\State \textbf{ST\_DEC\_G}(D5):$G(m' \| H(ek)) \rightarrow (K', r')$ +\State \textbf{ST\_DEC\_J}(D5续):$J(z \| c) \rightarrow \bar{K}$(隐式拒绝备用密钥) +\State \textbf{流水线复用}(D6):将$m'$写入$m\_r$、$r'$写入$r\_r$,跳转至\textbf{ST\_ENC\_LOAD},完整复用Encaps路径全部12个状态,产生参考密文$c'$(存入\texttt{ct\_bram}) +\State \textbf{ST\_DEC\_CMP}(D7):逐字节比较$c'$(\texttt{ct\_bram})与原始$c$(\texttt{c\_in\_bram}),常量时间无早退;$c' = c$时输出$K'$,否则输出$\bar{K}$ +\end{algorithmic} +\end{algorithm} + + +% ================================================================ +\section{各功能模块数据通路与控制逻辑} + +\subsection{Keccak-f[1600]置换核与哈希族} + +\texttt{keccak\_core}模块实现24轮连续Keccak-f[1600]置换,内部结构为简单的迭代流水线:1600-bit状态寄存器$\rightarrow$组合\texttt{keccak\_round}$\rightarrow$写回状态寄存器。\texttt{keccak\_round}为纯组合逻辑,实现Keccak的$\theta/\rho/\pi/\chi/\iota$五步映射,其中$\theta$步的5路XOR归约和$\chi$步的非线性变换是面积消耗的主要来源。 + +\begin{figure}[H] +\centering +\includegraphics[width=\textwidth]{keccak_core.png} +\caption{Keccak-f[1600]核数据通路与共享机制} +\label{fig:keccak_core} +\end{figure} + +\texttt{sha3\_top\_shared}模块在\texttt{keccak\_core}之上封装了SHA3/SHAKE协议层,包括1088-bit速率域/512-bit容量域的填充、domain separator拼接(SHA3末尾\texttt{01}、SHAKE末尾\texttt{1111})以及多块顺序吸收的状态管理。支持三种模式:SHA3-512($G$函数,单块64字节吸收)、SHA3-256($H$函数,多块吸收,块数$=\lceil(ek\_bytes+1)/136\rceil$)和SHAKE-256($J$函数和PRF派生)。 + +\textbf{共享Keccak的复用机制}通过两级多路选择实现。输入侧:三个消费者的1600-bit状态和\texttt{valid}信号经\texttt{sel\_*}信号选择后送入\texttt{keccak\_core};选择信号由FSM状态直接推导(例如\texttt{sel\_snt = (st==ST\_A || st==ST\_ENC\_A)})。输出侧:\texttt{keccak\_core}的置换结果和\texttt{valid\_o}广播至所有消费者,各消费者内部仅在自己激活的上下文中采样,天然实现了无冲突分派。此复用方案将Keccak的硬件开销从三份实例(约16,000 LUT)降至一份(约5,300 LUT),是IP核面积仅为25.5\% LUT利用率的关键设计决策。 + +\subsection{NTT核} + +\texttt{ntt\_core}模块实现256点前向/逆向数论变换,采用单蝶形流水线加256$\times$12-bit寄存器阵列的架构。图\ref{fig:ntt_core}给出数据通路与控制状态机。 + +\begin{figure}[H] +\centering +% [IMAGE_PROMPT: Data path diagram for ntt_core. Left side: 256-entry × 12-bit coefficient register file (cr[0..255]). A read mux selects cr[j] and cr[j+layer_len] based on the current butterfly pair address. These feed a pipelined butterfly unit (butterfly_unit_pipe, 7-stage pipeline) which computes a_out = a + zeta*b mod Q, b_out = a - zeta*b mod Q using a pipelined Barrett multiplier inside. Zeta comes from zeta_rom (128 entries × 12-bit). The butterfly output a_out/b_out go through an 8-stage writeback delay line (wa_d0..wa_d7, wb_d0..wb_d7) that matches pipeline depth, then write back to cr[wa] and cr[wb]. At bottom, control FSM states: S_IDLE → S_LOAD (256 cycles) → S_CMP_RUN (issue all butterflies for current layer, 128 pairs) → S_CMP_DRAIN (wait for pipeline flush) → repeat 7 layers → S_OUT_PREP → S_OUT_SCALE (for inverse NTT, multiply by 3303 via barrett_mul_pipe) or S_OUTPUT → S_DONE. Annotate total cycles: ~2260 for full forward NTT. Clean academic style.] +\caption{NTT核数据通路与控制状态机} +\label{fig:ntt_core} +\end{figure} + +NTT的7层蝶形网络按Cooley-Tukey位反转置换顺序执行。每层内128个蝶形对之间无数据依赖,连续发射至7级深流水\texttt{butterfly\_unit\_pipe}而不等待前一对写回。流水线排空等待周期(约7拍)远小于一层中的蝶形总数(128对),因此额外延迟占比较小。 + +\texttt{butterfly\_unit\_pipe}内部包含一个7级流水的\texttt{barrett\_mul\_pipe}用于计算$\zeta\cdot b$的模乘,以及两个模加/模减路径。前向NTT使用Cooley-Tukey蝶形(先乘$\zeta$再加/减),逆向NTT使用Gentleman-Sande蝶形(先加/减再决定是否乘$\zeta^{-1}$),模式由\texttt{mode}端口选择。 + +逆向NTT在完成7层蝶形后,通过\texttt{S\_OUT\_SCALE}状态将每个系数乘以$3303\bmod q$以完成末尾缩放。缩放操作复用同一个7级\texttt{barrett\_mul\_pipe}实例,256个系数顺序通过流水线后输出。 + +\texttt{zeta\_rom}存储128个12-bit旋转因子,位反转索引。ROM综合为LUT,无需额外BRAM。 + +\subsection{多项式乘法与Barrett模乘} + +\texttt{poly\_mul\_sync}实现NTT域两个多项式的逐点乘法,即对128个系数对$(a_0,a_1)$和$(b_0,b_1)$按$c_0=a_0b_0+a_1b_1\zeta\bmod q$和$c_1=a_0b_1+a_1b_0\bmod q$计算。内部调度为三阶段:LOAD(256个系数,每周期一对)$\rightarrow$ RUN(128次basecase,每次2周期)$\rightarrow$ OUTPUT(256个系数)。 + +\texttt{basecase\_mul}为纯组合逻辑,数据通路包含5个\texttt{barrett\_mul}实例。$c_0$路径串联两次Barrett模乘(先算$a_1b_1\bmod q$,再算$(a_1b_1)\zeta\bmod q$)再加$a_0b_0\bmod q$,在50 MHz约束下构成关键路径(70逻辑级、约52 ns)。 + +\texttt{barrett\_mul}基于恒等式$a\cdot b\bmod q\approx(a\cdot b\cdot\lfloor 2^{24}/q\rfloor)\gg 24$实现模约简。对于$q=3329$,$\lfloor 2^{24}/3329\rfloor=5039$。实现流程为:计算$p=a\cdot b$(24-bit乘积)$\rightarrow$计算$qe=(p\cdot 5039)\gg 24$(商估计)$\rightarrow$计算$r=p-qe\cdot 3329$(余数估计)$\rightarrow$两次条件减$q$修正(保证$r