mlkem_test: add mlkem-native
This commit is contained in:
@@ -1,8 +1,15 @@
|
|||||||
TARGET = mlkem_test
|
TARGET = mlkem_test
|
||||||
|
|
||||||
CFLAGS += -O3 -g
|
CFLAGS += -O3 -g
|
||||||
|
CFLAGS += -I./mlkem_native
|
||||||
|
CFLAGS += -DMLK_CONFIG_PARAMETER_SET=512
|
||||||
|
CFLAGS += -DMLK_CONFIG_NAMESPACE_PREFIX=swmlkem
|
||||||
|
CFLAGS += -DMLK_CONFIG_NO_RANDOMIZED_API
|
||||||
|
CFLAGS += -DMLK_CONFIG_NO_ASM
|
||||||
|
CFLAGS += -DMLK_CONFIG_FILE='"sw_mlkem_config.h"'
|
||||||
|
|
||||||
C_SRCS := $(wildcard ./*.c )
|
C_SRCS := $(wildcard ./*.c )
|
||||||
|
C_SRCS += ./mlkem_native/mlkem_native.c
|
||||||
|
|
||||||
OBJDIR = obj
|
OBJDIR = obj
|
||||||
COMMON_DIR = ../../bsp
|
COMMON_DIR = ../../bsp
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
#include <confreg_time.h>
|
#include <confreg_time.h>
|
||||||
|
|
||||||
#include "dma.h"
|
#include "dma.h"
|
||||||
|
#include "mlkem_native.h"
|
||||||
|
|
||||||
/* ─── BSP globals ─────────────────────────────────────────────────── */
|
/* ─── BSP globals ─────────────────────────────────────────────────── */
|
||||||
unsigned long UART_BASE = 0xbf000000;
|
unsigned long UART_BASE = 0xbf000000;
|
||||||
@@ -256,6 +257,7 @@ typedef struct {
|
|||||||
|
|
||||||
static mlkem_result_t cpu_res;
|
static mlkem_result_t cpu_res;
|
||||||
static mlkem_result_t dma_res;
|
static mlkem_result_t dma_res;
|
||||||
|
static mlkem_result_t sw_res;
|
||||||
|
|
||||||
static void print_hex(const char *label, const uint8_t *b, int n)
|
static void print_hex(const char *label, const uint8_t *b, int n)
|
||||||
{
|
{
|
||||||
@@ -287,6 +289,32 @@ static void clear_result_dma(mlkem_result_t *res)
|
|||||||
clear_uc(p, sizeof(*res));
|
clear_uc(p, sizeof(*res));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static unsigned long run_roundtrip_sw(const uint8_t *seed_d,
|
||||||
|
const uint8_t *seed_z,
|
||||||
|
const uint8_t *msg,
|
||||||
|
mlkem_result_t *res,
|
||||||
|
int *status)
|
||||||
|
{
|
||||||
|
unsigned long t0, t1;
|
||||||
|
uint8_t coins[2 * MLKEM_SYMBYTES];
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
clear_result_cpu(res);
|
||||||
|
memcpy(coins, seed_d, MLKEM_SYMBYTES);
|
||||||
|
memcpy(coins + MLKEM_SYMBYTES, seed_z, MLKEM_SYMBYTES);
|
||||||
|
|
||||||
|
t0 = get_clock_count();
|
||||||
|
rc = crypto_kem_keypair_derand(res->ek, res->dk, coins);
|
||||||
|
if (rc == 0)
|
||||||
|
rc = crypto_kem_enc_derand(res->ct, res->ss_enc, res->ek, msg);
|
||||||
|
if (rc == 0)
|
||||||
|
rc = crypto_kem_dec(res->ss_dec, res->ct, res->dk);
|
||||||
|
t1 = get_clock_count();
|
||||||
|
|
||||||
|
*status = rc;
|
||||||
|
return t1 - t0;
|
||||||
|
}
|
||||||
|
|
||||||
static unsigned long run_roundtrip_cpu(const uint8_t *seed_d,
|
static unsigned long run_roundtrip_cpu(const uint8_t *seed_d,
|
||||||
const uint8_t *seed_z,
|
const uint8_t *seed_z,
|
||||||
const uint8_t *msg,
|
const uint8_t *msg,
|
||||||
@@ -373,19 +401,27 @@ static unsigned long cycles_to_us(unsigned long cycles)
|
|||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
int mismatch_cpu, mismatch_dma, result_diff;
|
int mismatch_sw, mismatch_cpu, mismatch_dma, sw_status, cpu_vs_dma_diff;
|
||||||
unsigned long cpu_cycles, dma_cycles;
|
int sw_vs_cpu_diff, sw_vs_dma_diff;
|
||||||
|
unsigned long sw_cycles, cpu_cycles, dma_cycles;
|
||||||
|
|
||||||
printf("MLKEM_START\n");
|
printf("MLKEM_START\n");
|
||||||
generate_prng_inputs();
|
generate_prng_inputs();
|
||||||
|
|
||||||
|
sw_cycles = run_roundtrip_sw(SEED_D, SEED_Z, MSG_M, &sw_res, &sw_status);
|
||||||
cpu_cycles = run_roundtrip_cpu(SEED_D, SEED_Z, MSG_M, &cpu_res);
|
cpu_cycles = run_roundtrip_cpu(SEED_D, SEED_Z, MSG_M, &cpu_res);
|
||||||
dma_cycles = run_roundtrip_dma(SEED_D, SEED_Z, MSG_M, &dma_res);
|
dma_cycles = run_roundtrip_dma(SEED_D, SEED_Z, MSG_M, &dma_res);
|
||||||
|
|
||||||
|
mismatch_sw = verify_result(&sw_res);
|
||||||
mismatch_cpu = verify_result(&cpu_res);
|
mismatch_cpu = verify_result(&cpu_res);
|
||||||
mismatch_dma = verify_result(&dma_res);
|
mismatch_dma = verify_result(&dma_res);
|
||||||
result_diff = compare_results(&cpu_res, &dma_res);
|
cpu_vs_dma_diff = compare_results(&cpu_res, &dma_res);
|
||||||
|
sw_vs_cpu_diff = compare_results(&sw_res, &cpu_res);
|
||||||
|
sw_vs_dma_diff = compare_results(&sw_res, &dma_res);
|
||||||
|
|
||||||
|
print_hex("SW_EK32=", sw_res.ek, 32);
|
||||||
|
print_hex("SW_SS_ENC=", sw_res.ss_enc, 32);
|
||||||
|
print_hex("SW_SS_DEC=", sw_res.ss_dec, 32);
|
||||||
print_hex("CPU_EK32=", cpu_res.ek, 32);
|
print_hex("CPU_EK32=", cpu_res.ek, 32);
|
||||||
print_hex("CPU_SS_ENC=", cpu_res.ss_enc, 32);
|
print_hex("CPU_SS_ENC=", cpu_res.ss_enc, 32);
|
||||||
print_hex("CPU_SS_DEC=", cpu_res.ss_dec, 32);
|
print_hex("CPU_SS_DEC=", cpu_res.ss_dec, 32);
|
||||||
@@ -393,20 +429,34 @@ int main(void)
|
|||||||
print_hex("DMA_SS_ENC=", dma_res.ss_enc, 32);
|
print_hex("DMA_SS_ENC=", dma_res.ss_enc, 32);
|
||||||
print_hex("DMA_SS_DEC=", dma_res.ss_dec, 32);
|
print_hex("DMA_SS_DEC=", dma_res.ss_dec, 32);
|
||||||
|
|
||||||
|
printf("MLKEM_SW_STATUS=%d\n", sw_status);
|
||||||
|
printf("MLKEM_SW_CYCLES=%lu\n", sw_cycles);
|
||||||
printf("MLKEM_CPU_CYCLES=%lu\n", cpu_cycles);
|
printf("MLKEM_CPU_CYCLES=%lu\n", cpu_cycles);
|
||||||
printf("MLKEM_DMA_CYCLES=%lu\n", dma_cycles);
|
printf("MLKEM_DMA_CYCLES=%lu\n", dma_cycles);
|
||||||
|
printf("MLKEM_SW_US=%lu\n", cycles_to_us(sw_cycles));
|
||||||
printf("MLKEM_CPU_US=%lu\n", cycles_to_us(cpu_cycles));
|
printf("MLKEM_CPU_US=%lu\n", cycles_to_us(cpu_cycles));
|
||||||
printf("MLKEM_DMA_US=%lu\n", cycles_to_us(dma_cycles));
|
printf("MLKEM_DMA_US=%lu\n", cycles_to_us(dma_cycles));
|
||||||
|
if (cpu_cycles != 0) {
|
||||||
|
unsigned long hw_speedup_x100 = (sw_cycles * 100u) / cpu_cycles;
|
||||||
|
printf("MLKEM_HW_VS_SW_SPEEDUP_X100=%lu\n", hw_speedup_x100);
|
||||||
|
}
|
||||||
|
if (dma_cycles != 0) {
|
||||||
|
unsigned long hw_dma_speedup_x100 = (sw_cycles * 100u) / dma_cycles;
|
||||||
|
printf("MLKEM_HW_DMA_VS_SW_SPEEDUP_X100=%lu\n", hw_dma_speedup_x100);
|
||||||
|
}
|
||||||
if (dma_cycles != 0) {
|
if (dma_cycles != 0) {
|
||||||
unsigned long speedup_x100 = (cpu_cycles * 100u) / dma_cycles;
|
unsigned long speedup_x100 = (cpu_cycles * 100u) / dma_cycles;
|
||||||
printf("MLKEM_DMA_SPEEDUP_X100=%lu\n", speedup_x100);
|
printf("MLKEM_DMA_SPEEDUP_X100=%lu\n", speedup_x100);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mismatch_cpu == 0 && mismatch_dma == 0 && result_diff == 0)
|
if (sw_status == 0 && mismatch_sw == 0 && mismatch_cpu == 0 &&
|
||||||
|
mismatch_dma == 0 && cpu_vs_dma_diff == 0 &&
|
||||||
|
sw_vs_cpu_diff == 0 && sw_vs_dma_diff == 0)
|
||||||
printf("MLKEM_RESULT=PASS\n");
|
printf("MLKEM_RESULT=PASS\n");
|
||||||
else {
|
else {
|
||||||
printf("MLKEM_RESULT=FAIL cpu_mismatch=%d dma_mismatch=%d result_diff=%d\n",
|
printf("MLKEM_RESULT=FAIL sw_status=%d sw_mismatch=%d cpu_mismatch=%d dma_mismatch=%d cpu_dma_diff=%d sw_cpu_diff=%d sw_dma_diff=%d\n",
|
||||||
mismatch_cpu, mismatch_dma, result_diff);
|
sw_status, mismatch_sw, mismatch_cpu, mismatch_dma,
|
||||||
|
cpu_vs_dma_diff, sw_vs_cpu_diff, sw_vs_dma_diff);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("MLKEM_DONE\n");
|
printf("MLKEM_DONE\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user