#!/usr/bin/env python3 """0029 acceptance test 1 — validate the FLINT-free convolution primitive. (a) closed-form oracle at the certificate's own scale: for p = [A]*n, q = [B]*n the truncated product is exactly A*B*(j+1) at every j; (b) sampled coefficients against exact Python big-integer dot products on random dense data at 224-bit coefficient width (the certificate's `fixed_bits`). Both run at the real length n = 98264 and the real shift 224. """ from __future__ import annotations import random import sys import time import numpy as np sys.path.insert(0, "work/pg186") import importlib.util from pathlib import Path spec = importlib.util.spec_from_file_location("ntt0029", Path(__file__).with_name("0029_ntt.py")) N = importlib.util.module_from_spec(spec) spec.loader.exec_module(N) n = 98264 SHIFT = 224 eng = N.ConvolutionEngine(n, SHIFT) print("primes :", N.PRIMES) print("CRT modulus : 2^%.1f" % (N.CRT_MODULUS.bit_length() - 1)) print("numba :", N.HAVE_NUMBA) print() ok = True # ---------------------------------------------------------------- (a) closed form A = (1 << 223) + 12345 B = (1 << 222) + 999 t0 = time.time() pa = N.encode_ints([A] * n, n) pb = N.encode_ints([B] * n, n) raw = eng.mul_raw(pa, pb) t1 = time.time() vals = N.decode_ints(raw) t2 = time.time() bad = [] for j in range(n): if vals[j] != A * B * (j + 1): bad.append(j) print("(a) closed form A*B*(j+1): checked %d coefficients, %d wrong" % (n, len(bad))) if bad: print(" first bad:", bad[:5], "got", vals[bad[0]], "want", A * B * (bad[0] + 1)) ok = False print(" mul_raw %.2f s (incl. 2 forward + 1 inverse NTT over %d primes), decode %.2f s" % (t1 - t0, N.K, t2 - t1)) print() # ------------------------------------------------------------------ (b) random dense rng = random.Random(20290930) a_int = [rng.getrandbits(224) for _ in range(n)] b_int = [rng.getrandbits(224) for _ in range(n)] t0 = time.time() pa = N.encode_ints_fast(a_int, n) pb = N.encode_ints_fast(b_int, n) t1 = time.time() raw = eng.mul_raw(pa, pb) vals = N.decode_ints(raw) t2 = time.time() samples = [0, 1, 2, 17, 1000, 50000, 98262, 98263] worst = 0 for j in samples: exact = sum(a_int[i] * b_int[j - i] for i in range(j + 1)) if vals[j] != exact: print("(b) MISMATCH at j=%d: got %d want %d" % (j, vals[j], exact)) ok = False worst = max(worst, exact.bit_length()) print("(b) random dense: %d sampled coefficients exact, max sampled coefficient %d bits" % (len(samples), worst)) print(" encode %.2f s, mul_raw+decode %.2f s" % (t1 - t0, t2 - t1)) print() # ------------------------------------------------------------- full rescale timing t0 = time.time() res = eng.rescale(raw) t1 = time.time() # verify the rescale reproduces floor(x / 2**224) on the samples g = N.decode_g(res) recon = N.decode_ints(res) for j in samples: exact = sum(a_int[i] * b_int[j - i] for i in range(j + 1)) >> SHIFT if recon[j] != exact: print("(c) RESCALE MISMATCH at j=%d" % j) ok = False print("(c) rescale (floor by 2^%d, re-encode) %.2f s; sampled coefficients exact" % (SHIFT, t1 - t0)) print() print("test 1:", "PASS" if ok else "FAIL") sys.exit(0 if ok else 1)