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
algebraic_attack_analysis Namespace Reference

Classes

class  AlgebraicAttackResult
 Result of an algebraic attack attempt. More...
 

Functions

 _bitrev7 (k)
 Bit-reversal of a 7-bit index (FIPS-203 NTT twiddle order).
 
AlgebraicAttackResult algebraic_attack_basic (np.ndarray secret_ntt, int num_queries=256)
 Basic algebraic attack exploiting NTT linearity.
 
AlgebraicAttackResult algebraic_attack_protected (np.ndarray secret_ntt, np.ndarray mixing_key, np.ndarray permutation, np.ndarray inverse_permutation, int num_queries=1000)
 Attempt the algebraic attack against the protected implementation.
 
Dict analyze_attack_complexity (int num_trials=10)
 Analyze attack complexity across unprotected/protected scenarios.
 
np.ndarray apply_nonlinear_mixing (np.ndarray ntt_poly, np.ndarray mixing_key)
 Apply nonlinear mixing (cubic map) to break linearity.
 
np.ndarray apply_random_shuffling (np.ndarray ntt_poly, np.ndarray permutation)
 Apply coefficient shuffling by a permutation.
 
Dict benchmark_countermeasure_overhead ()
 Benchmark runtime overhead of the countermeasures vs baseline.
 
np.ndarray compute_zeta_powers ()
 Compute powers of zeta for the NTT.
 
Tuple[np.ndarray, np.ndarray] extract_linear_equations (List[Tuple[np.ndarray, np.ndarray]] input_output_pairs, int target_coefficients=256)
 Extract linear equations from input-output pairs.
 
Dict full_module_recovery (int num_queries=3, int seed=42)
 Full ML-KEM-768 secret recovery on the unprotected white-box base multiply.
 
np.ndarray generate_secret_key ()
 Generate a secret key with CBD(eta=2) distribution.
 
 main ()
 Run the full analysis and write results JSON.
 
int mod_inverse (int a, int m)
 Modular inverse of a mod m (prime m, via Fermat).
 
np.ndarray ntt_forward (np.ndarray poly)
 Forward NTT: standard domain to NTT domain.
 
np.ndarray ntt_inverse (np.ndarray ntt_poly)
 Inverse NTT: NTT domain to standard domain.
 
np.ndarray ntt_multiply (np.ndarray a_ntt, np.ndarray b_ntt)
 Multiply two polynomials in the NTT domain.
 
np.ndarray simulate_protected_whitebox (np.ndarray secret_ntt, np.ndarray ciphertext_ntt, np.ndarray mixing_key, np.ndarray permutation, np.ndarray inverse_permutation)
 Simulate a protected white-box: mix and shuffle the tabulated secret, multiply.
 
np.ndarray simulate_whitebox_table_lookup (np.ndarray secret_ntt, np.ndarray ciphertext_ntt)
 Simulate a white-box table lookup returning s * c in the NTT domain.
 
Tuple[Optional[np.ndarray], bool] solve_linear_system_mod_q (np.ndarray A, np.ndarray b, int q=Q)
 Solve Ax = b mod q by Gaussian elimination.
 
 solve_mod_q (np.ndarray A, np.ndarray b, int q)
 Solve A x = b over Z_q by Gauss-Jordan elimination.
 

Variables

int ETA = 2
 
int K = 3
 
int N = 256
 
int Q = 3329
 
int ZETA = 17
 
np.ndarray ZETA_POWERS = compute_zeta_powers()
 

Function Documentation

◆ _bitrev7()

algebraic_attack_analysis._bitrev7 (   k)
protected

Bit-reversal of a 7-bit index (FIPS-203 NTT twiddle order).

Parameters
kIndex in [0, 128).
Returns
Bit-reversed 7-bit index.

Definition at line 66 of file algebraic_attack_analysis.py.

Referenced by extract_linear_equations(), full_module_recovery(), ntt_forward(), ntt_inverse(), and ntt_multiply().

+ Here is the caller graph for this function:

◆ algebraic_attack_basic()

AlgebraicAttackResult algebraic_attack_analysis.algebraic_attack_basic ( np.ndarray  secret_ntt,
int   num_queries = 256 
)

Basic algebraic attack exploiting NTT linearity.

Parameters
secret_nttTarget secret key in NTT domain.
num_queriesNumber of random ciphertext queries.
Returns
Attack result with recovered key and error norm.

Definition at line 311 of file algebraic_attack_analysis.py.

References extract_linear_equations(), simulate_whitebox_table_lookup(), and solve_linear_system_mod_q().

Referenced by analyze_attack_complexity().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ algebraic_attack_protected()

AlgebraicAttackResult algebraic_attack_analysis.algebraic_attack_protected ( np.ndarray  secret_ntt,
np.ndarray  mixing_key,
np.ndarray  permutation,
np.ndarray  inverse_permutation,
int   num_queries = 1000 
)

Attempt the algebraic attack against the protected implementation.

Parameters
secret_nttTarget secret key in NTT domain.
mixing_keyAdditive mixing key.
permutationShuffling permutation.
inverse_permutationInverse of the shuffling permutation.
num_queriesNumber of random ciphertext queries.
Returns
Attack result with recovered key and error norm.

Definition at line 424 of file algebraic_attack_analysis.py.

References extract_linear_equations(), simulate_protected_whitebox(), and solve_linear_system_mod_q().

Referenced by analyze_attack_complexity().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ analyze_attack_complexity()

Dict algebraic_attack_analysis.analyze_attack_complexity ( int   num_trials = 10)

Analyze attack complexity across unprotected/protected scenarios.

Parameters
num_trialsTrials per scenario.
Returns
Results dict with per-attack success rates and recommendations.

Definition at line 493 of file algebraic_attack_analysis.py.

References algebraic_attack_basic(), algebraic_attack_protected(), and generate_secret_key().

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ apply_nonlinear_mixing()

np.ndarray algebraic_attack_analysis.apply_nonlinear_mixing ( np.ndarray  ntt_poly,
np.ndarray  mixing_key 
)

Apply nonlinear mixing (cubic map) to break linearity.

Parameters
ntt_polyInput polynomial in NTT domain.
mixing_keyAdditive mixing key.
Returns
Mixed polynomial.

Definition at line 371 of file algebraic_attack_analysis.py.

Referenced by benchmark_countermeasure_overhead(), and simulate_protected_whitebox().

+ Here is the caller graph for this function:

◆ apply_random_shuffling()

np.ndarray algebraic_attack_analysis.apply_random_shuffling ( np.ndarray  ntt_poly,
np.ndarray  permutation 
)

Apply coefficient shuffling by a permutation.

Parameters
ntt_polyInput polynomial.
permutationIndex permutation.
Returns
Permuted polynomial.

Definition at line 386 of file algebraic_attack_analysis.py.

Referenced by benchmark_countermeasure_overhead(), and simulate_protected_whitebox().

+ Here is the caller graph for this function:

◆ benchmark_countermeasure_overhead()

Dict algebraic_attack_analysis.benchmark_countermeasure_overhead ( )

Benchmark runtime overhead of the countermeasures vs baseline.

Returns
Results dict with per-operation timings and overhead factors.

Definition at line 653 of file algebraic_attack_analysis.py.

References apply_nonlinear_mixing(), apply_random_shuffling(), generate_secret_key(), ntt_multiply(), and simulate_protected_whitebox().

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ compute_zeta_powers()

np.ndarray algebraic_attack_analysis.compute_zeta_powers ( )

Compute powers of zeta for the NTT.

Returns
Array of 256 zeta powers mod q.

Definition at line 51 of file algebraic_attack_analysis.py.

◆ extract_linear_equations()

Tuple[np.ndarray, np.ndarray] algebraic_attack_analysis.extract_linear_equations ( List[Tuple[np.ndarray, np.ndarray]]  input_output_pairs,
int   target_coefficients = 256 
)

Extract linear equations from input-output pairs.

Parameters
input_output_pairsList of (ciphertext_ntt, output_ntt) pairs.
target_coefficientsNumber of secret coefficients to solve for.
Returns
Tuple (A, b) of coefficient matrix and output vector.
Note
output[i] = sum_j A[i,j]*secret_ntt[j], linear in the secret key.

Definition at line 271 of file algebraic_attack_analysis.py.

References _bitrev7().

Referenced by algebraic_attack_basic(), and algebraic_attack_protected().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ full_module_recovery()

Dict algebraic_attack_analysis.full_module_recovery ( int   num_queries = 3,
int   seed = 42 
)

Full ML-KEM-768 secret recovery on the unprotected white-box base multiply.

Parameters
num_queriesNumber of full ciphertext queries used.
seedDeterministic seed.
Returns
Dict of unknowns, queries, recovered coefficients, and success flag.

Each degree-2 component i gives, per query, two Z_q-linear equations in its two NTT-domain secret coordinates (s0, s1): p0 = c0 s0 + gamma_i c1 s1 and p1 = c1 s0 + c0 s1. Stacking the queries and solving recovers (s0, s1); across all 128 components of all k = 3 secret polynomials this recovers every one of the k*n = 768 NTT-domain coefficients. Each recovery is checked against the known secret.

Definition at line 767 of file algebraic_attack_analysis.py.

References _bitrev7(), and solve_mod_q().

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generate_secret_key()

np.ndarray algebraic_attack_analysis.generate_secret_key ( )

Generate a secret key with CBD(eta=2) distribution.

Returns
Secret key coefficient vector.

Definition at line 161 of file algebraic_attack_analysis.py.

Referenced by analyze_attack_complexity(), and benchmark_countermeasure_overhead().

+ Here is the caller graph for this function:

◆ main()

algebraic_attack_analysis.main ( void  )

Run the full analysis and write results JSON.

Definition at line 819 of file algebraic_attack_analysis.py.

References analyze_attack_complexity(), benchmark_countermeasure_overhead(), full_module_recovery(), and main().

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mod_inverse()

int algebraic_attack_analysis.mod_inverse ( int  a,
int  m 
)

Modular inverse of a mod m (prime m, via Fermat).

Parameters
aValue to invert.
mPrime modulus.
Returns
Inverse of a mod m, or 0 if a is 0.

Definition at line 184 of file algebraic_attack_analysis.py.

Referenced by solve_linear_system_mod_q(), and solve_mod_q().

+ Here is the caller graph for this function:

◆ ntt_forward()

np.ndarray algebraic_attack_analysis.ntt_forward ( np.ndarray  poly)

Forward NTT: standard domain to NTT domain.

Parameters
polyPolynomial coefficients.
Returns
Coefficients in NTT domain.

Definition at line 75 of file algebraic_attack_analysis.py.

References _bitrev7().

+ Here is the call graph for this function:

◆ ntt_inverse()

np.ndarray algebraic_attack_analysis.ntt_inverse ( np.ndarray  ntt_poly)

Inverse NTT: NTT domain to standard domain.

Parameters
ntt_polyCoefficients in NTT domain.
Returns
Polynomial coefficients.

Definition at line 96 of file algebraic_attack_analysis.py.

References _bitrev7().

+ Here is the call graph for this function:

◆ ntt_multiply()

np.ndarray algebraic_attack_analysis.ntt_multiply ( np.ndarray  a_ntt,
np.ndarray  b_ntt 
)

Multiply two polynomials in the NTT domain.

Parameters
a_nttFirst operand in NTT domain.
b_nttSecond operand in NTT domain.
Returns
Product in NTT domain.
Note
Pairwise multiplication over degree-2 extensions.

Definition at line 120 of file algebraic_attack_analysis.py.

References _bitrev7().

Referenced by benchmark_countermeasure_overhead(), simulate_protected_whitebox(), and simulate_whitebox_table_lookup().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ simulate_protected_whitebox()

np.ndarray algebraic_attack_analysis.simulate_protected_whitebox ( np.ndarray  secret_ntt,
np.ndarray  ciphertext_ntt,
np.ndarray  mixing_key,
np.ndarray  permutation,
np.ndarray  inverse_permutation 
)

Simulate a protected white-box: mix and shuffle the tabulated secret, multiply.

Parameters
secret_nttSecret key in NTT domain.
ciphertext_nttCiphertext in NTT domain.
mixing_keyAdditive mixing key.
permutationShuffling permutation applied to the tabulated secret.
inverse_permutationUnused; kept so callers need not change.
Returns
The internal wire the adversary reads, in NTT domain.
Note
Both countermeasures act on what the tables hold, so only the secret is permuted: the effective secret is a permutation of cube(s + kappa), and it multiplies the ciphertext as it arrives. Permuting the ciphertext too would move coordinates across the degree-2 pair boundaries, where ntt_multiply applies a per-pair gamma, and the wire would then be no product at all – making the solver fail for the wrong reason.

Definition at line 396 of file algebraic_attack_analysis.py.

References apply_nonlinear_mixing(), apply_random_shuffling(), and ntt_multiply().

Referenced by algebraic_attack_protected(), and benchmark_countermeasure_overhead().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ simulate_whitebox_table_lookup()

np.ndarray algebraic_attack_analysis.simulate_whitebox_table_lookup ( np.ndarray  secret_ntt,
np.ndarray   ciphertext_ntt 
)

Simulate a white-box table lookup returning s * c in the NTT domain.

Parameters
secret_nttSecret key in NTT domain.
ciphertext_nttCiphertext in NTT domain.
Returns
Product in NTT domain.
Note
In a real implementation this is a precomputed T[s][c] table.

Definition at line 171 of file algebraic_attack_analysis.py.

References ntt_multiply().

Referenced by algebraic_attack_basic().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ solve_linear_system_mod_q()

Tuple[Optional[np.ndarray], bool] algebraic_attack_analysis.solve_linear_system_mod_q ( np.ndarray  A,
np.ndarray  b,
int   q = Q 
)

Solve Ax = b mod q by Gaussian elimination.

Parameters
ACoefficient matrix.
bRight-hand side vector.
qPrime modulus.
Returns
Tuple (solution, success).

Definition at line 196 of file algebraic_attack_analysis.py.

References mod_inverse().

Referenced by algebraic_attack_basic(), and algebraic_attack_protected().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ solve_mod_q()

algebraic_attack_analysis.solve_mod_q ( np.ndarray  A,
np.ndarray  b,
int  q 
)

Solve A x = b over Z_q by Gauss-Jordan elimination.

Parameters
ACoefficient matrix (m x ncols).
bRight-hand side (length m).
qPrime modulus.
Returns
(x, rank): x is the solution when rank == ncols, else None.

Definition at line 727 of file algebraic_attack_analysis.py.

References mod_inverse().

Referenced by full_module_recovery().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ ETA

int algebraic_attack_analysis.ETA = 2

Definition at line 43 of file algebraic_attack_analysis.py.

◆ K

int algebraic_attack_analysis.K = 3

Definition at line 42 of file algebraic_attack_analysis.py.

◆ N

int algebraic_attack_analysis.N = 256

Definition at line 41 of file algebraic_attack_analysis.py.

◆ Q

int algebraic_attack_analysis.Q = 3329

Definition at line 40 of file algebraic_attack_analysis.py.

◆ ZETA

int algebraic_attack_analysis.ZETA = 17

Definition at line 44 of file algebraic_attack_analysis.py.

◆ ZETA_POWERS

np.ndarray algebraic_attack_analysis.ZETA_POWERS = compute_zeta_powers()

Definition at line 63 of file algebraic_attack_analysis.py.