feat: optimize startup and linking

This commit is contained in:
2026-07-05 17:27:26 +08:00
parent 423ff32b49
commit 7281df356f
11 changed files with 292 additions and 97 deletions

View File

@@ -16,6 +16,7 @@
* automatically wait-stated on the bus).
*/
#include <string.h>
#include <stdio.h>
#include <stdint.h>
@@ -149,15 +150,15 @@ static void readback(uint32_t sel, uint8_t *buf, int n)
/* PLACEHOLDER2 */
/* Arbitrary but fixed test seeds / message (32 bytes each). */
static const uint8_t SEED_D[32] = {
static uint8_t SEED_D[32] = {
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F
};
static const uint8_t SEED_Z[32] = {
static uint8_t SEED_Z[32] = {
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F
};
static const uint8_t MSG_M[32] = {
static uint8_t MSG_M[32] = {
0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,
0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF
};
@@ -206,6 +207,51 @@ int main(void)
read_block32(MLKEM_SS, ss_dec);
print_hex("SS_DEC=", ss_dec, 32);
/* ── 4. Verify shared secrets match ───────────────────────────── */
mismatch = 0;
for (i = 0; i < 32; i++)
if (ss_enc[i] != ss_dec[i]) mismatch++;
if (mismatch == 0)
printf("MLKEM_RESULT=PASS\n");
else
printf("MLKEM_RESULT=FAIL (%d byte mismatch)\n", mismatch);
printf("MLKEM_DONE\n");
SEED_D[2] = 0;
SEED_Z[2] = 0;
MSG_M[2] = 0;
memset(ek, 0, sizeof(ek));
memset(dk, 0, sizeof(dk));
memset(ct, 0, sizeof(ct));
memset(ss_enc, 0, sizeof(ss_enc));
memset(ss_dec, 0, sizeof(ss_dec));
/* ── 1. KeyGen: d,z -> ek, dk ─────────────────────────────────── */
write_block32(MLKEM_D, SEED_D);
write_block32(MLKEM_Z, SEED_Z);
run_op(OP_KEYGEN);
readback(RDB_EK, ek, EK_BYTES);
readback(RDB_DK, dk, DK_BYTES);
print_hex("EK32=", ek, 32); /* first 32 bytes as a fingerprint */
/* ── 2. Encaps: ek + msg -> ss, ct ────────────────────────────── */
preload(PRE_EK, ek, EK_BYTES);
write_block32(MLKEM_MSG, MSG_M);
run_op(OP_ENCAPS);
read_block32(MLKEM_SS, ss_enc);
readback(RDB_CT, ct, CT_BYTES);
print_hex("SS_ENC=", ss_enc, 32);
/* ── 3. Decaps: dk + ct -> ss' ────────────────────────────────── */
preload(PRE_DK, dk, DK_BYTES);
preload(PRE_C, ct, CT_BYTES);
run_op(OP_DECAPS);
read_block32(MLKEM_SS, ss_dec);
print_hex("SS_DEC=", ss_dec, 32);
/* ── 4. Verify shared secrets match ───────────────────────────── */
mismatch = 0;
for (i = 0; i < 32; i++)