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
primitive_bench.c
Go to the documentation of this file.
1/**
2 * @file primitive_bench.c
3 * @brief Protected/direct timing ratio of the PQ-WB-KEM base multiplication on x86-64 and aarch64.
4 * @details direct = arithmetic product (p0=s0c0+g s1c1, p1=s0c1+s1c0); protected = same product via
5 * per-component baked table lookups (construction B, full-range NTT-domain secret) plus
6 * 3-share masking of both output coordinates, over the 2.56 MB per-poly tables (k=3 -> 7.67 MB).
7 * Deterministic (LCG seed 42); build gcc / aarch64-linux-gnu-gcc -O2 -static;
8 * writes JSON to stdout.
9 * @note qemu-aarch64 is functional emulation, not cycle-accurate; absolute aarch64 timings do not
10 * reflect real silicon. Both variants run under the identical emulator, so the ratio is the
11 * comparable metric.
12 */
13#include <stdio.h>
14#include <stdlib.h>
15#include <stdint.h>
16#include <time.h>
17
18#define Q 3329
19#define NUM_COMPONENTS 128
20#define ZETA 17
21
22static double now_sec(void) {
23 struct timespec ts;
24 clock_gettime(CLOCK_MONOTONIC, &ts);
25 return (double)ts.tv_sec + (double)ts.tv_nsec * 1e-9;
26}
27
28static uint64_t st = 42;
29static uint32_t nxt(void) { st = st * 6364136223846793005ULL + 1442695040888963407ULL; return (uint32_t)(st >> 32); }
30
31static int pw(int b, int e, int m) { long long r = 1, x = b % m; while (e) { if (e & 1) r = r * x % m; x = x * x % m; e >>= 1; } return (int)r; }
32
33int main(int argc, char **argv) {
34 const char *arch = (argc > 1) ? argv[1] : "unknown";
35 /* Construction B: per-component baked tables for the full-range NTT-domain
36 * secret shat=NTT(s). h0_i[c]=shat0_i*c, h1_i[c]=shat1_i*c, h2_i[c]=gamma_i*shat1_i*c. */
38 uint16_t *H = malloc((size_t)NUM_COMPONENTS * 3 * Q * sizeof(uint16_t));
39 if (!H) return 1;
40 for (int i = 0; i < NUM_COMPONENTS; i++) {
41 g[i] = pw(ZETA, 2 * i + 1, Q);
42 s0v[i] = (int)(nxt() % Q); s1v[i] = (int)(nxt() % Q); /* full-range shat coords */
43 int gs1 = (int)((long long)g[i] * s1v[i] % Q);
44 for (int c = 0; c < Q; c++) {
45 H[((size_t)i * 3 + 0) * Q + c] = (uint16_t)((long long)s0v[i] * c % Q);
46 H[((size_t)i * 3 + 1) * Q + c] = (uint16_t)((long long)s1v[i] * c % Q);
47 H[((size_t)i * 3 + 2) * Q + c] = (uint16_t)((long long)gs1 * c % Q);
48 }
49 }
50
51 const long IT = 30000000L;
52 const int NIN = 4096;
53 int *ai = malloc(NIN * 4), *c0 = malloc(NIN * 4), *c1 = malloc(NIN * 4);
54 for (int k = 0; k < NIN; k++) { ai[k] = nxt() % NUM_COMPONENTS; c0[k] = nxt() % Q; c1[k] = nxt() % Q; }
55 volatile uint32_t sink = 0;
56
57 double t0 = now_sec();
58 for (long k = 0; k < IT; k++) {
59 int j = (int)(k & (NIN - 1)), i = ai[j];
60 int a = s0v[i], b = s1v[i], gg = g[i];
61 int p0 = (int)(((long long)a * c0[j] + (long long)gg * b * c1[j]) % Q);
62 int p1 = (int)(((long long)a * c1[j] + (long long)b * c0[j]) % Q);
63 sink += (uint32_t)(p0 + p1);
64 }
65 double t_direct = now_sec() - t0;
66
67 t0 = now_sec();
68 for (long k = 0; k < IT; k++) {
69 int j = (int)(k & (NIN - 1)), i = ai[j];
70 const uint16_t *h0 = &H[((size_t)i * 3 + 0) * Q];
71 const uint16_t *h1 = &H[((size_t)i * 3 + 1) * Q];
72 const uint16_t *h2 = &H[((size_t)i * 3 + 2) * Q];
73 int p0 = (h0[c0[j]] + h2[c1[j]]) % Q; /* table lookups (B) */
74 int p1 = (h0[c1[j]] + h1[c0[j]]) % Q;
75 uint32_t r1 = nxt() % Q, r2 = nxt() % Q; /* 3-share masking */
76 uint32_t s0a = r1, s0b = r2, s0c = ((uint32_t)p0 + 2 * Q - r1 - r2) % Q;
77 uint32_t r3 = nxt() % Q, r4 = nxt() % Q;
78 uint32_t s1a = r3, s1b = r4, s1c = ((uint32_t)p1 + 2 * Q - r3 - r4) % Q;
79 sink += (s0a + s0b + s0c + s1a + s1b + s1c);
80 }
81 double t_prot = now_sec() - t0;
82
83 double ratio = t_prot / t_direct;
84 printf("{\n");
85 printf(" \"arch\": \"%s\",\n", arch);
86 printf(" \"iterations\": %ld,\n", IT);
87 printf(" \"table_MB\": %.2f,\n", (size_t)NUM_COMPONENTS * 3 * Q * sizeof(uint16_t) / 1e6);
88 printf(" \"construction\": \"B (per-component baked, full-range NTT-domain secret); per-poly 2.56 MB, k=3 -> 7.67 MB\",\n");
89 printf(" \"ns_per_direct\": %.3f,\n", t_direct / IT * 1e9);
90 printf(" \"ns_per_protected\": %.3f,\n", t_prot / IT * 1e9);
91 printf(" \"protected_over_direct_ratio\": %.3f,\n", ratio);
92 printf(" \"note\": \"protected = table lookup + 3-share masking of both outputs; direct = arithmetic. On aarch64 this runs under qemu-aarch64 (functional emulation): absolute ns are NOT cycle-accurate, the ratio is the comparable metric.\",\n");
93 printf(" \"sink\": %u\n", (unsigned)sink);
94 printf("}\n");
95 free(H); free(ai); free(c0); free(c1);
96 return 0;
97}
int main(void)
static int pw(int b, int e, int m)
static double now_sec(void)
#define NUM_COMPONENTS
static uint32_t nxt(void)
#define ZETA
#define Q
static uint64_t st