PQ-WB-KEM: White-Box ML-KEM-768 1.0
PQ-WB-KEM: Toward a White-Box Construction of ML-KEM-768 with Arithmetic Masking for M2M Communications - Mathematics (MDPI)
Loading...
Searching...
No Matches
bench_liboqs.c
Go to the documentation of this file.
1/**
2 * @file bench_liboqs.c
3 * @brief liboqs ML-KEM-768 KeyGen/Encaps/Decaps wall-clock benchmark; emits the full
4 * arm64_benchmark.json / baseline schema to stdout so every field is measured or
5 * computed, never transcribed.
6 * @details Each operation runs a fixed wall-clock budget (default 3.0 s, argv[1]) after a warmup,
7 * and the per-iteration times are reduced to iteration count, mean and standard deviation.
8 * A time budget rather than a fixed iteration count keeps the aarch64/QEMU and x86-64 runs
9 * at equal wall-clock per operation, so the iteration count itself reports relative op cost.
10 * An unavailable ML-KEM-768 is a hard error: the harness never substitutes a different
11 * algorithm or emits a partial result. cpu_extensions is read from liboqs runtime detection;
12 * compiler and openssl_enabled are the build facts passed in as macros; oqs_version is the
13 * linked library's own string.
14 * @note Under qemu-aarch64 the absolute times are functional-emulation figures dependent on host load
15 * and CPU frequency, not real silicon; provenance says so and the comparable metric is the ratio.
16 * @author Ugur Coruh
17 * @copyright Academic and research use; see LICENSE.
18 */
19#include <stdio.h>
20#include <stdlib.h>
21#include <stdint.h>
22#include <string.h>
23#include <time.h>
24#include <math.h>
25#include <oqs/oqs.h>
26
27#ifndef BENCH_COMPILER
28#define BENCH_COMPILER "unknown"
29#endif
30#ifndef BENCH_TARGET
31#define BENCH_TARGET "unknown"
32#endif
33#ifndef BENCH_PLATFORM
34#define BENCH_PLATFORM "unknown"
35#endif
36#ifndef BENCH_OPENSSL
37#define BENCH_OPENSSL 0
38#endif
39
40/** @brief Warmup iterations discarded before timing. */
41#define WARMUP 1000
42
43static double now_sec(void) {
44 struct timespec ts;
45 clock_gettime(CLOCK_MONOTONIC, &ts);
46 return (double)ts.tv_sec + (double)ts.tv_nsec * 1e-9;
47}
48
49/** @brief Grow-on-demand sample buffer so the loop is bounded by wall-clock, not a fixed count. */
50typedef struct { double *t; long n, cap; } samples;
51static void push(samples *s, double v) {
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); } }
53 s->t[s->n++] = v;
54}
55
56typedef struct { long n; double mean_us, stdev_us; } opstat;
57static opstat reduce(const samples *s) {
58 opstat o = {s->n, 0.0, 0.0};
59 double sum = 0.0;
60 for (long i = 0; i < s->n; i++) sum += s->t[i];
61 o.mean_us = sum / (double)s->n;
62 double var = 0.0;
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;
65 return o;
66}
67
68static void emit_op(const char *name, opstat o, int last) {
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 ? "" : ",");
75}
76
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; }
80
81 OQS_init();
82 OQS_KEM *kem = OQS_KEM_new(OQS_KEM_alg_ml_kem_768);
83 if (kem == NULL) {
84 fprintf(stderr,
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");
88 return 3;
89 }
90
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; }
97
98 /* Fixed key pair + ciphertext so encaps/decaps time the operation, not key setup. */
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; }
101
102 samples kg = {0}, en = {0}, de = {0};
103 double t0, t1, deadline;
104
105 for (long w = 0; w < WARMUP; w++) OQS_KEM_keypair(kem, pk, sk);
106 deadline = now_sec() + budget;
107 do { t0 = now_sec(); OQS_KEM_keypair(kem, pk, sk); t1 = now_sec(); push(&kg, (t1 - t0) * 1e6); } while (t1 < deadline);
108
109 for (long w = 0; w < WARMUP; w++) OQS_KEM_encaps(kem, ct, ss, pk);
110 deadline = now_sec() + budget;
111 do { t0 = now_sec(); OQS_KEM_encaps(kem, ct, ss, pk); t1 = now_sec(); push(&en, (t1 - t0) * 1e6); } while (t1 < deadline);
112
113 for (long w = 0; w < WARMUP; w++) OQS_KEM_decaps(kem, ss2, ct, sk);
114 deadline = now_sec() + budget;
115 do { t0 = now_sec(); OQS_KEM_decaps(kem, ss2, ct, sk); t1 = now_sec(); push(&de, (t1 - t0) * 1e6); } while (t1 < deadline);
116
117 opstat kgs = reduce(&kg), ens = reduce(&en), des = reduce(&de);
118
119 /* Runtime CPU-feature detection from liboqs; under qemu-aarch64 this reflects the emulated ISA. */
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"},
123 };
124
125 printf("{\n");
126 printf(" \"platform\": \"%s\",\n", BENCH_PLATFORM);
127 printf(" \"target\": \"%s\",\n", BENCH_TARGET);
128 printf(" \"compiler\": \"%s\",\n", BENCH_COMPILER);
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\": [");
133 {
134 int first = 1;
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);
138 first = 0;
139 }
140 }
141 }
142 printf("],\n");
143 printf(" \"results\": {\n");
144 emit_op("keygen", kgs, 0);
145 emit_op("encaps", ens, 0);
146 emit_op("decaps", des, 1);
147 printf(" },\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");
149 printf("}\n");
150
151 free(kg.t); free(en.t); free(de.t);
152 free(pk); free(sk); free(ct); free(ss); free(ss2);
153 OQS_KEM_free(kem);
154 OQS_destroy();
155 return 0;
156}
static void emit_op(const char *name, opstat o, int last)
static opstat reduce(const samples *s)
#define BENCH_COMPILER
#define BENCH_PLATFORM
static double now_sec(void)
double stdev_us
static void push(samples *s, double v)
double * t
#define WARMUP
Warmup iterations discarded before timing.
#define BENCH_TARGET
#define BENCH_OPENSSL
double mean_us
Grow-on-demand sample buffer so the loop is bounded by wall-clock, not a fixed count.
int main(void)