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
cache_effect.c
Go to the documentation of this file.
1/**
2 * @file cache_effect.c
3 * @brief Measures the cache-miss penalty of the PQ-WB-KEM white-box table lookups.
4 * @details Cache-penalty factor of the deployment-overhead projection (measured masking x cache penalty), measured on real hardware two ways rather than assumed.
5 * @note BENCH A builds the real construction-B per-component baked tables (7.67 MB, k=3; full-range NTT-domain secret, layout as whitebox_reference.py) and times table-lookup vs direct-arithmetic base multiplication. Host 36 MB L3 holds the whole table, so its ratio is a LOWER BOUND for embedded targets lacking an L3.
6 * @note BENCH B pointer-chases a Sattolo random cycle of 64-byte nodes over 16 KB..64 MB working sets for true random-access latency; the DRAM/L1 ratio is the penalty an L1/L2-only embedded target sees once the tables exceed cache.
7 * @note Deterministic (LCG seed 42). Writes results/cache_effect.json. Build: gcc -O2 -o cache_effect cache_effect.c.
8 */
9#include <stdio.h>
10#include <stdlib.h>
11#include <stdint.h>
12#include <string.h>
13
14#if defined(_WIN32)
15#include <windows.h>
16static double now_sec(void) {
17 LARGE_INTEGER f, c;
18 QueryPerformanceFrequency(&f);
19 QueryPerformanceCounter(&c);
20 return (double)c.QuadPart / (double)f.QuadPart;
21}
22#else
23#include <time.h>
24static double now_sec(void) {
25 struct timespec ts;
26 clock_gettime(CLOCK_MONOTONIC, &ts);
27 return (double)ts.tv_sec + (double)ts.tv_nsec * 1e-9;
28}
29#endif
30
31#define Q 3329
32#define NUM_COMPONENTS 128
33#define ZETA 17
34
35/* Reproducible LCG (Numerical Recipes constants); no libc rand() dependence. */
36static uint64_t lcg_state = 42;
37static uint32_t lcg_next(void) {
38 lcg_state = lcg_state * 6364136223846793005ULL + 1442695040888963407ULL;
39 return (uint32_t)(lcg_state >> 32);
40}
41
42static int pow_mod(int base, int exp, int mod) {
43 long long r = 1, b = base % mod;
44 while (exp > 0) {
45 if (exp & 1) r = (r * b) % mod;
46 b = (b * b) % mod;
47 exp >>= 1;
48 }
49 return (int)r;
50}
51
52/* BENCH A: real white-box tables (construction B), lookup vs direct base multiplication */
53static void bench_primitive(FILE *js) {
54 /* Construction B: per-component baked tables for the full-range NTT-domain
55 * secret shat = NTT(s). Per component i: h0_i[c]=shat0_i*c, h1_i[c]=shat1_i*c,
56 * h2_i[c]=gamma_i*shat1_i*c (q entries each). p0 = h0_i[c0] + h2_i[c1],
57 * p1 = h0_i[c1] + h1_i[c0]. Cache behaviour is value-independent, so the baked
58 * full-range secret coordinates are drawn uniformly over Z_q. */
59 int gammas[NUM_COMPONENTS], s0v[NUM_COMPONENTS], s1v[NUM_COMPONENTS];
60 for (int i = 0; i < NUM_COMPONENTS; i++) {
61 gammas[i] = pow_mod(ZETA, 2 * i + 1, Q);
62 s0v[i] = (int)(lcg_next() % Q); /* full-range shat coordinate */
63 s1v[i] = (int)(lcg_next() % Q);
64 }
65 /* H[(i*3+t)*Q + c] : t=0 -> h0, t=1 -> h1, t=2 -> h2 */
66 uint16_t *H = (uint16_t *)malloc((size_t)NUM_COMPONENTS * 3 * Q * sizeof(uint16_t));
67 if (!H) { fprintf(stderr, "alloc failed\n"); exit(1); }
68 for (int i = 0; i < NUM_COMPONENTS; i++) {
69 int gs1 = (int)(((long long)gammas[i] * s1v[i]) % Q);
70 for (int c = 0; c < Q; c++) {
71 H[((size_t)i * 3 + 0) * Q + c] = (uint16_t)(((long long)s0v[i] * c) % Q);
72 H[((size_t)i * 3 + 1) * Q + c] = (uint16_t)(((long long)s1v[i] * c) % Q);
73 H[((size_t)i * 3 + 2) * Q + c] = (uint16_t)(((long long)gs1 * c) % Q);
74 }
75 }
76 size_t table_bytes = (size_t)NUM_COMPONENTS * 3 * Q * sizeof(uint16_t);
77
78 const long ITERS = 40000000L; /* 40M base multiplications per variant */
79
80 /* Pre-generate random inputs into small arrays (indices reused cyclically so the
81 * input generation itself is cheap and identical for both variants). */
82 const int NIN = 4096;
83 int *ai = (int *)malloc(NIN * sizeof(int)); /* component index i */
84 int *c0 = (int *)malloc(NIN * sizeof(int)); /* ciphertext coeff */
85 int *c1 = (int *)malloc(NIN * sizeof(int));
86 for (int k = 0; k < NIN; k++) {
87 ai[k] = lcg_next() % NUM_COMPONENTS;
88 c0[k] = lcg_next() % Q;
89 c1[k] = lcg_next() % Q;
90 }
91
92 volatile uint32_t sink = 0;
93
94 /* Direct arithmetic baseline: p0 = s0*c0 + gamma*s1*c1, p1 = s0*c1 + s1*c0. */
95 double t0 = now_sec();
96 for (long k = 0; k < ITERS; k++) {
97 int j = (int)(k & (NIN - 1));
98 int i = ai[j], sv0 = s0v[i], sv1 = s1v[i], g = gammas[i];
99 int p0 = (int)(((long long)sv0 * c0[j] + (long long)g * sv1 * c1[j]) % Q);
100 int p1 = (int)(((long long)sv0 * c1[j] + (long long)sv1 * c0[j]) % Q);
101 sink += (uint32_t)(p0 + p1);
102 }
103 double t_direct = now_sec() - t0;
104
105 /* Table lookup (B): p0 = h0_i[c0] + h2_i[c1], p1 = h0_i[c1] + h1_i[c0]. */
106 t0 = now_sec();
107 for (long k = 0; k < ITERS; k++) {
108 int j = (int)(k & (NIN - 1));
109 int i = ai[j];
110 const uint16_t *h0 = &H[((size_t)i * 3 + 0) * Q];
111 const uint16_t *h1 = &H[((size_t)i * 3 + 1) * Q];
112 const uint16_t *h2 = &H[((size_t)i * 3 + 2) * Q];
113 int p0 = (h0[c0[j]] + h2[c1[j]]) % Q;
114 int p1 = (h0[c1[j]] + h1[c0[j]]) % Q;
115 sink += (uint32_t)(p0 + p1);
116 }
117 double t_table = now_sec() - t0;
118
119 double lookup_penalty = t_table / t_direct;
120
121 /* Realistic (cold) access: inputs generated in-loop, spread randomly over the
122 * full table. Identical gen appears in the gen-only, direct and table loops;
123 * subtracting gen-only isolates pure work including real cache misses. Each
124 * base-mul touches the per-component h0/h1/h2 tables at random ciphertext offsets. */
125 const long CITERS = 20000000L;
126 uint64_t g_state;
127
128 /* gen-only baseline: run the same LCG draws + cheap folding, no table/arith */
129 g_state = 12345;
130 double tg0 = now_sec();
131 for (long k = 0; k < CITERS; k++) {
132 g_state = g_state * 6364136223846793005ULL + 1442695040888963407ULL;
133 uint32_t r = (uint32_t)(g_state >> 32);
134 int i = r % NUM_COMPONENTS;
135 int cc0 = (r >> 13) % Q, cc1 = (r * 2654435761u) % Q;
136 sink += (uint32_t)(i + cc0 + cc1);
137 }
138 double t_gen = now_sec() - tg0;
139
140 g_state = 12345;
141 tg0 = now_sec();
142 for (long k = 0; k < CITERS; k++) {
143 g_state = g_state * 6364136223846793005ULL + 1442695040888963407ULL;
144 uint32_t r = (uint32_t)(g_state >> 32);
145 int i = r % NUM_COMPONENTS;
146 int cc0 = (r >> 13) % Q, cc1 = (r * 2654435761u) % Q;
147 int sv0 = s0v[i], sv1 = s1v[i], gg = gammas[i];
148 int p0 = (int)(((long long)sv0 * cc0 + (long long)gg * sv1 * cc1) % Q);
149 int p1 = (int)(((long long)sv0 * cc1 + (long long)sv1 * cc0) % Q);
150 sink += (uint32_t)(p0 + p1);
151 }
152 double t_direct_cold = now_sec() - tg0;
153
154 g_state = 12345;
155 tg0 = now_sec();
156 for (long k = 0; k < CITERS; k++) {
157 g_state = g_state * 6364136223846793005ULL + 1442695040888963407ULL;
158 uint32_t r = (uint32_t)(g_state >> 32);
159 int i = r % NUM_COMPONENTS;
160 int cc0 = (r >> 13) % Q, cc1 = (r * 2654435761u) % Q;
161 const uint16_t *h0 = &H[((size_t)i * 3 + 0) * Q];
162 const uint16_t *h1 = &H[((size_t)i * 3 + 1) * Q];
163 const uint16_t *h2 = &H[((size_t)i * 3 + 2) * Q];
164 int p0 = (h0[cc0] + h2[cc1]) % Q;
165 int p1 = (h0[cc1] + h1[cc0]) % Q;
166 sink += (uint32_t)(p0 + p1);
167 }
168 double t_table_cold = now_sec() - tg0;
169
170 double work_direct = t_direct_cold - t_gen;
171 double work_table = t_table_cold - t_gen;
172 double realistic_penalty = work_table / work_direct;
173
174 fprintf(js, " \"primitive\": {\n");
175 fprintf(js, " \"iterations\": %ld,\n", ITERS);
176 fprintf(js, " \"table_bytes\": %zu,\n", table_bytes);
177 fprintf(js, " \"table_MB\": %.2f,\n", table_bytes / 1e6);
178 fprintf(js, " \"hot_ns_per_direct\": %.3f,\n", t_direct / ITERS * 1e9);
179 fprintf(js, " \"hot_ns_per_table_lookup\": %.3f,\n", t_table / ITERS * 1e9);
180 fprintf(js, " \"hot_lookup_penalty_factor\": %.3f,\n", lookup_penalty);
181 fprintf(js, " \"hot_note\": \"4096 reused inputs -> touched table subset stays L1-hot; lookups beat arithmetic. Shows the overhead is a cache effect, not an instruction-count effect.\",\n");
182 fprintf(js, " \"cold_iterations\": %ld,\n", CITERS);
183 fprintf(js, " \"cold_ns_gen\": %.3f,\n", t_gen / CITERS * 1e9);
184 fprintf(js, " \"cold_ns_direct_work\": %.3f,\n", work_direct / CITERS * 1e9);
185 fprintf(js, " \"cold_ns_table_work\": %.3f,\n", work_table / CITERS * 1e9);
186 fprintf(js, " \"cold_realistic_penalty_factor\": %.3f,\n", realistic_penalty);
187 fprintf(js, " \"cold_note\": \"inputs random across the full table (gen cost subtracted). Construction B: each base-mul reads the per-component h0/h1/h2 tables (7.67 MB total, k=3) at random ciphertext offsets. Desktop L3-resident regime; embedded targets without L3 place the tables in DRAM (see working_set_sweep DRAM penalty).\",\n");
188 fprintf(js, " \"sink\": %u\n", (unsigned)sink);
189 fprintf(js, " },\n");
190
191 printf("BENCH A hot : direct %.3f ns table %.3f ns penalty %.3fx\n",
192 t_direct / ITERS * 1e9, t_table / ITERS * 1e9, lookup_penalty);
193 printf("BENCH A cold: gen %.3f direct-work %.3f table-work %.3f realistic penalty %.3fx\n",
194 t_gen / CITERS * 1e9, work_direct / CITERS * 1e9, work_table / CITERS * 1e9, realistic_penalty);
195
196 free(H); free(ai); free(c0); free(c1);
197}
198
199/* BENCH B: pointer-chase working-set sweep (true random-access latency) */
200typedef struct { uint32_t next; char pad[60]; } node_t; /* one 64-byte cache line */
201
202static double chase_latency_ns(size_t bytes, long steps) {
203 size_t count = bytes / sizeof(node_t);
204 if (count < 2) count = 2;
205 node_t *a = (node_t *)malloc(count * sizeof(node_t));
206 if (!a) { fprintf(stderr, "alloc failed (%zu bytes)\n", bytes); exit(1); }
207
208 /* Sattolo's algorithm: a single random cycle over all `count` nodes. */
209 for (size_t i = 0; i < count; i++) a[i].next = (uint32_t)i;
210 for (size_t i = count - 1; i > 0; i--) {
211 size_t j = lcg_next() % i; /* 0 <= j < i -> single cycle */
212 uint32_t t = a[i].next; a[i].next = a[j].next; a[j].next = t;
213 }
214
215 /* Warm + chase. Dependency chain defeats prefetch/ILP -> true miss latency. */
216 uint32_t idx = 0;
217 for (long w = 0; w < (long)count; w++) idx = a[idx].next;
218
219 double t0 = now_sec();
220 idx = 0;
221 for (long s = 0; s < steps; s++) idx = a[idx].next;
222 double dt = now_sec() - t0;
223
224 volatile uint32_t sink = idx; (void)sink;
225 free(a);
226 return dt / steps * 1e9;
227}
228
229static void bench_working_set(FILE *js) {
230 struct { const char *label; size_t bytes; } sizes[] = {
231 {"16KB", 16u * 1024},
232 {"48KB", 48u * 1024}, /* ~L1d */
233 {"256KB", 256u * 1024},
234 {"2MB", 2u * 1024 * 1024}, /* ~per-core L2 */
235 {"7.67MB", 7670000u}, /* the construction-B white-box table set (k=3) */
236 {"16MB", 16u * 1024 * 1024},
237 {"36MB", 36u * 1024 * 1024}, /* ~L3 */
238 {"64MB", 64u * 1024 * 1024}, /* > L3 -> DRAM */
239 };
240 int n = (int)(sizeof(sizes) / sizeof(sizes[0]));
241 const long STEPS = 30000000L;
242
243 double lat[16];
244 fprintf(js, " \"working_set_sweep\": {\n");
245 fprintf(js, " \"steps\": %ld,\n", STEPS);
246 fprintf(js, " \"points\": [\n");
247 for (int i = 0; i < n; i++) {
248 lat[i] = chase_latency_ns(sizes[i].bytes, STEPS);
249 printf("BENCH B: %-8s %.2f ns/access\n", sizes[i].label, lat[i]);
250 fprintf(js, " {\"label\": \"%s\", \"bytes\": %zu, \"ns_per_access\": %.3f}%s\n",
251 sizes[i].label, sizes[i].bytes, lat[i], (i < n - 1) ? "," : "");
252 }
253 fprintf(js, " ],\n");
254
255 /* index 0 = 16KB (L1-resident baseline), index 4 = 7.67 MB table working set */
256 double baseline = lat[0];
257 double at_table = lat[4];
258 double at_dram = lat[n - 1];
259 fprintf(js, " \"baseline_16KB_ns\": %.3f,\n", baseline);
260 fprintf(js, " \"table_7p67MB_ns\": %.3f,\n", at_table);
261 fprintf(js, " \"dram_64MB_ns\": %.3f,\n", at_dram);
262 fprintf(js, " \"penalty_table_vs_L1\": %.3f,\n", at_table / baseline);
263 fprintf(js, " \"penalty_dram_vs_L1\": %.3f,\n", at_dram / baseline);
264 fprintf(js, " \"note\": \"penalty_table_vs_L1 is desktop (table L3-resident). An embedded target with only L1/L2 sees the DRAM-regime penalty (penalty_dram_vs_L1) once the 7.67 MB construction-B tables exceed its cache.\"\n");
265 fprintf(js, " }\n");
266
267 printf("BENCH B: 7.67MB/L1 = %.2fx DRAM/L1 = %.2fx\n", at_table / baseline, at_dram / baseline);
268}
269
270int main(void) {
271 FILE *js = fopen("results/cache_effect.json", "w");
272 if (!js) {
273 /* try creating results/ then reopen */
274#if defined(_WIN32)
275 system("if not exist results mkdir results");
276#else
277 system("mkdir -p results");
278#endif
279 js = fopen("results/cache_effect.json", "w");
280 }
281 if (!js) { fprintf(stderr, "cannot open results/cache_effect.json\n"); return 1; }
282
283 fprintf(js, "{\n");
284 fprintf(js, " \"host\": \"i9-13980HX, L1d 48KB/core, L2 2MB/core, L3 36MB\",\n");
285 fprintf(js, " \"seed\": 42,\n");
286 fprintf(js, " \"compiler\": \"gcc -O2\",\n");
287 bench_primitive(js);
289 fprintf(js, "}\n");
290 fclose(js);
291 printf("wrote results/cache_effect.json\n");
292 return 0;
293}
static void bench_working_set(FILE *js)
static double now_sec(void)
#define NUM_COMPONENTS
uint32_t next
int main(void)
static int pow_mod(int base, int exp, int mod)
#define ZETA
static uint32_t lcg_next(void)
#define Q
static void bench_primitive(FILE *js)
static double chase_latency_ns(size_t bytes, long steps)
static uint64_t lcg_state