28#define BENCH_COMPILER "unknown"
31#define BENCH_TARGET "unknown"
34#define BENCH_PLATFORM "unknown"
37#define BENCH_OPENSSL 0
45 clock_gettime(CLOCK_MONOTONIC, &ts);
46 return (
double)ts.tv_sec + (double)ts.tv_nsec * 1e-9;
52 if (s->
n == s->
cap) { s->
cap = s->
cap ? s->
cap * 2 : 65536; s->
t = realloc(s->
t, (
size_t)s->
cap *
sizeof(
double));
if (!s->
t) { perror(
"realloc"); exit(2); } }
60 for (
long i = 0; i < s->
n; i++) sum += s->
t[i];
63 for (
long i = 0; i < s->
n; i++) {
double d = s->
t[i] - o.
mean_us; var += d * d; }
64 o.
stdev_us = (s->
n > 1) ? sqrt(var / (
double)(s->
n - 1)) : 0.0;
69 printf(
" \"%s\": {\n", name);
70 printf(
" \"iterations\": %ld,\n", o.
n);
71 printf(
" \"mean_us\": %.3f,\n", o.
mean_us);
72 printf(
" \"stdev_us\": %.3f,\n", o.
stdev_us);
73 printf(
" \"mean_ms\": %.3f\n", o.
mean_us / 1000.0);
74 printf(
" }%s\n", last ?
"" :
",");
77int main(
int argc,
char **argv) {
78 double budget = (argc > 1) ? atof(argv[1]) : 3.0;
79 if (budget <= 0.0) { fprintf(stderr,
"budget must be > 0\n");
return 2; }
82 OQS_KEM *kem = OQS_KEM_new(OQS_KEM_alg_ml_kem_768);
85 "liboqs could not instantiate ML-KEM-768: this build was not compiled with "
86 "OQS_ENABLE_KEM_ml_kem_768. Rebuild liboqs with ML-KEM-768 enabled; the harness "
87 "does not substitute a different algorithm.\n");
91 uint8_t *pk = malloc(kem->length_public_key);
92 uint8_t *sk = malloc(kem->length_secret_key);
93 uint8_t *ct = malloc(kem->length_ciphertext);
94 uint8_t *ss = malloc(kem->length_shared_secret);
95 uint8_t *ss2 = malloc(kem->length_shared_secret);
96 if (!pk || !sk || !ct || !ss || !ss2) { perror(
"malloc");
return 2; }
99 if (OQS_KEM_keypair(kem, pk, sk) != OQS_SUCCESS) { fprintf(stderr,
"keypair failed\n");
return 4; }
100 if (OQS_KEM_encaps(kem, ct, ss, pk) != OQS_SUCCESS) { fprintf(stderr,
"encaps failed\n");
return 4; }
102 samples kg = {0}, en = {0}, de = {0};
103 double t0, t1, deadline;
105 for (
long w = 0; w <
WARMUP; w++) OQS_KEM_keypair(kem, pk, sk);
107 do { t0 =
now_sec(); OQS_KEM_keypair(kem, pk, sk); t1 =
now_sec();
push(&kg, (t1 - t0) * 1e6); }
while (t1 < deadline);
109 for (
long w = 0; w <
WARMUP; w++) OQS_KEM_encaps(kem, ct, ss, pk);
111 do { t0 =
now_sec(); OQS_KEM_encaps(kem, ct, ss, pk); t1 =
now_sec();
push(&en, (t1 - t0) * 1e6); }
while (t1 < deadline);
113 for (
long w = 0; w <
WARMUP; w++) OQS_KEM_decaps(kem, ss2, ct, sk);
115 do { t0 =
now_sec(); OQS_KEM_decaps(kem, ss2, ct, sk); t1 =
now_sec();
push(&de, (t1 - t0) * 1e6); }
while (t1 < deadline);
120 static const struct { OQS_CPU_EXT ext;
const char *name; } arm_ext[] = {
121 {OQS_CPU_EXT_ARM_AES,
"AES"}, {OQS_CPU_EXT_ARM_SHA2,
"SHA2"},
122 {OQS_CPU_EXT_ARM_SHA3,
"SHA3"}, {OQS_CPU_EXT_ARM_NEON,
"NEON"},
129 printf(
" \"algorithm\": \"ML-KEM-768\",\n");
130 printf(
" \"oqs_version\": \"%s\",\n", OQS_version());
131 printf(
" \"openssl_enabled\": %s,\n",
BENCH_OPENSSL ?
"true" :
"false");
132 printf(
" \"cpu_extensions\": [");
135 for (
size_t i = 0; i <
sizeof(arm_ext) /
sizeof(arm_ext[0]); i++) {
136 if (OQS_CPU_has_extension(arm_ext[i].ext)) {
137 printf(
"%s\"%s\"", first ?
"" :
", ", arm_ext[i].name);
143 printf(
" \"results\": {\n");
148 printf(
" \"provenance\": \"Native aarch64 liboqs ML-KEM-768 KeyGen/Encaps/Decaps benchmark: bench_liboqs.c cross-compiled with aarch64-linux-gnu-gcc against a static ML-KEM-768 aarch64 build of liboqs, executed under qemu-aarch64 user-mode emulation on an x86-64 host. QEMU emulates the aarch64 instruction set while the host CPU performs the work, so these figures characterise instruction-level behaviour of the emulated ISA, not any ARM device's wall-clock; absolute times depend on host load and CPU frequency, so the comparable metric is the ratio. Regenerated by code/c/build_arm64_benchmark.sh. Consumed by overhead_calculation.py as the aarch64 decapsulation baseline.\"\n");
151 free(kg.
t); free(en.t); free(de.t);
152 free(pk); free(sk); free(ct); free(ss); free(ss2);
static void emit_op(const char *name, opstat o, int last)
static opstat reduce(const samples *s)
static double now_sec(void)
static void push(samples *s, double v)
#define WARMUP
Warmup iterations discarded before timing.
Grow-on-demand sample buffer so the loop is bounded by wall-clock, not a fixed count.