19#define NUM_COMPONENTS 128
24 clock_gettime(CLOCK_MONOTONIC, &ts);
25 return (
double)ts.tv_sec + (double)ts.tv_nsec * 1e-9;
28static uint64_t
st = 42;
29static uint32_t
nxt(
void) {
st =
st * 6364136223846793005ULL + 1442695040888963407ULL;
return (uint32_t)(
st >> 32); }
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; }
33int main(
int argc,
char **argv) {
34 const char *arch = (argc > 1) ? argv[1] :
"unknown";
38 uint16_t *H = malloc((
size_t)
NUM_COMPONENTS * 3 *
Q *
sizeof(uint16_t));
42 s0v[i] = (int)(
nxt() %
Q); s1v[i] = (int)(
nxt() %
Q);
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);
51 const long IT = 30000000L;
53 int *ai = malloc(NIN * 4), *c0 = malloc(NIN * 4), *c1 = malloc(NIN * 4);
55 volatile uint32_t sink = 0;
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);
65 double t_direct =
now_sec() - t0;
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;
74 int p1 = (h0[c1[j]] + h1[c0[j]]) %
Q;
75 uint32_t r1 =
nxt() %
Q, r2 =
nxt() %
Q;
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);
83 double ratio = t_prot / t_direct;
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);
95 free(H); free(ai); free(c0); free(c1);
static int pw(int b, int e, int m)
static double now_sec(void)
static uint32_t nxt(void)