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
ntt_verification.sage.py
Go to the documentation of this file.
1
2
3# This file was *autogenerated* from the file ntt_verification.sage
4from sage.all_cmdline import * # import sage library
5
6_sage_const_60 = Integer(60); _sage_const_3329 = Integer(3329); _sage_const_256 = Integer(256); _sage_const_17 = Integer(17); _sage_const_1 = Integer(1); _sage_const_0 = Integer(0); _sage_const_128 = Integer(128); _sage_const_64 = Integer(64); _sage_const_512 = Integer(512); _sage_const_2 = Integer(2)#!/usr/bin/env sage
7# NTT domain structure verification for ML-KEM-768
8# Theorem 1: X^256 + 1 = prod_{i=0}^{127} (X^2 - zeta^{2i+1}) mod 3329
9
10import json
11import os
12from datetime import datetime
13
14print("=" * _sage_const_60 )
15print("NTT Domain Verification for ML-KEM-768")
16print("Paper 1: PQ-WB-KEM")
17print("=" * _sage_const_60 )
18
19# ML-KEM parameters
20q = _sage_const_3329
21n = _sage_const_256
22zeta = _sage_const_17
23
24results = {
25 "parameters": {"q": q, "n": n, "zeta": zeta},
26 "verifications": {}
27}
28
29# ============================================================
30# Verification 1: Group structure
31# ============================================================
32print("\n[1] Verifying group structure...")
33
34Zq = GF(q)
35group_order = q - _sage_const_1
36print(f" q = {q}")
37print(f" q - 1 = {group_order}")
38print(f" Factorization: {factor(group_order)}")
39
40# Check 256 divides q-1
41divides = (group_order % n == _sage_const_0 )
42print(f" 256 | (q-1) = {divides}")
43results["verifications"]["256_divides_q_minus_1"] = divides
44
45# ============================================================
46# Verification 2: zeta = 17 is primitive 256th root
47# ============================================================
48print("\n[2] Verifying zeta = 17 is primitive 256th root...")
49
50zeta_Zq = Zq(zeta)
51
52# Check zeta^256 = 1
53zeta_256 = zeta_Zq**_sage_const_256
54is_256th_root = (zeta_256 == _sage_const_1 )
55print(f" 17^256 mod 3329 = {zeta_256}")
56print(f" 17^256 = 1 (mod 3329): {is_256th_root}")
57results["verifications"]["zeta_256_equals_1"] = is_256th_root
58
59# Check zeta^128 = -1 (primitive, not 128th root)
60zeta_128 = zeta_Zq**_sage_const_128
61is_primitive = (zeta_128 == Zq(-_sage_const_1 ))
62print(f" 17^128 mod 3329 = {zeta_128}")
63print(f" 17^128 = -1 (mod 3329): {is_primitive}")
64results["verifications"]["zeta_128_equals_minus_1"] = is_primitive
65
66# Additional check: not smaller order
67zeta_64 = zeta_Zq**_sage_const_64
68not_64th_root = (zeta_64 != _sage_const_1 and zeta_64 != Zq(-_sage_const_1 ))
69print(f" 17^64 mod 3329 = {zeta_64}")
70print(f" 17^64 != +/- 1: {not_64th_root}")
71results["verifications"]["not_smaller_order"] = not_64th_root
72
73# ============================================================
74# Verification 3: 512th roots don't exist
75# ============================================================
76print("\n[3] Verifying 512th primitive roots don't exist...")
77
78divides_512 = (group_order % _sage_const_512 == _sage_const_0 )
79print(f" 512 | (q-1) = {group_order % 512 == 0}")
80print(f" No 512th roots exist: {not divides_512}")
81results["verifications"]["no_512th_roots"] = not divides_512
82
83# ============================================================
84# Verification 4: X^256 + 1 factorization
85# ============================================================
86print("\n[4] Factoring X^256 + 1 over Z_3329...")
87
88R = PolynomialRing(Zq, names=('X',)); (X,) = R._first_ngens(1)
89poly = X**_sage_const_256 + _sage_const_1
90factors = poly.factor()
91
92print(f" Number of factors: {len(factors)}")
93print(f" Expected: 128 degree-2 irreducible factors")
94
95# Check all factors are degree 2
96all_degree_2 = all(f[_sage_const_0 ].degree() == _sage_const_2 for f in factors)
97num_factors = len(factors)
98correct_factorization = (num_factors == _sage_const_128 and all_degree_2)
99
100print(f" All factors degree 2: {all_degree_2}")
101print(f" Correct factorization: {correct_factorization}")
102results["verifications"]["128_irreducible_factors"] = correct_factorization
103
104# ============================================================
105# Verification 5: Each zeta^(2i+1) is QNR
106# ============================================================
107print("\n[5] Verifying each zeta^(2i+1) is quadratic non-residue...")
108
109qnr_count = _sage_const_0
110qnr_failures = []
111
112for i in range(_sage_const_128 ):
113 gamma = zeta_Zq**(_sage_const_2 *i + _sage_const_1 )
114 # Check if gamma is QNR using Legendre symbol
115 legendre = kronecker(Integer(gamma), q)
116 is_qnr = (legendre == -_sage_const_1 )
117 if is_qnr:
118 qnr_count += _sage_const_1
119 else:
120 qnr_failures.append(i)
121
122print(f" QNR count: {qnr_count}/128")
123print(f" All 128 are QNR: {qnr_count == 128}")
124if qnr_failures:
125 print(f" Failures at indices: {qnr_failures}")
126results["verifications"]["all_128_qnr"] = (qnr_count == _sage_const_128 )
127results["qnr_count"] = qnr_count
128
129# ============================================================
130# Verification 6: Explicit irreducibility check
131# ============================================================
132print("\n[6] Explicit irreducibility check for each X^2 - gamma...")
133
134irreducible_count = _sage_const_0
135for i in range(_sage_const_128 ):
136 gamma = zeta_Zq**(_sage_const_2 *i + _sage_const_1 )
137 quad_poly = X**_sage_const_2 - gamma
138 is_irred = quad_poly.is_irreducible()
139 if is_irred:
140 irreducible_count += _sage_const_1
141
142print(f" Irreducible count: {irreducible_count}/128")
143print(f" All 128 irreducible: {irreducible_count == 128}")
144results["verifications"]["all_128_irreducible"] = (irreducible_count == _sage_const_128 )
145
146# ============================================================
147# Summary
148# ============================================================
149print("\n" + "=" * _sage_const_60 )
150print("VERIFICATION SUMMARY")
151print("=" * _sage_const_60 )
152
153all_passed = all(results["verifications"].values())
154results["all_passed"] = all_passed
155
156for key, value in results["verifications"].items():
157 status = "PASS" if value else "FAIL"
158 print(f" [{status}] {key}")
159
160print("\n" + "=" * _sage_const_60 )
161if all_passed:
162 print("ALL VERIFICATIONS PASSED")
163 print("NTT domain structure confirmed for white-box implementation")
164else:
165 print("SOME VERIFICATIONS FAILED - CHECK RESULTS")
166print("=" * _sage_const_60 )
167
168# Save results
169script_dir = os.path.dirname(os.path.abspath(__file__))
170results_dir = os.path.join(os.path.dirname(os.path.dirname(script_dir)), "results")
171os.makedirs(results_dir, exist_ok=True)
172output_file = os.path.join(results_dir, "ntt_verification.json")
173try:
174 with open(output_file, "w") as f:
175 json.dump(results, f, indent=_sage_const_2 , default=str)
176 print("\nResults saved to {}".format(output_file))
177except Exception as e:
178 print("\nError saving results: {}".format(e))
179 print("\nResults: {}".format(results))
180