Skip to content

Commit 06f11ae

Browse files
Merge pull request #19 from lattice-based-cryptography/fix_overflow_issue_for_q_squared
fix overflow issue for large primes
2 parents 6ae50e9 + 3b672ad commit 06f11ae

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,12 @@ fn factorize(n: i64) -> HashMap<i64, u32> {
156156

157157
/// Fast computation of a primitive root mod p^e
158158
pub fn primitive_root(p: i64, e: u32) -> i64 {
159+
println!("primitive_root called");
159160
let g = primitive_root_mod_p(p);
160161
let mut g_lifted = g; // Lift it to p^e
161162
for _ in 1..e {
162-
if g_lifted.pow((p - 1) as u32) % p.pow(e) == 1 {
163+
println!("g_lifted: {}", g_lifted);
164+
if mod_exp(g_lifted, p-1, p.pow(e)) == 1 {
163165
g_lifted += p.pow(e - 1);
164166
}
165167
}

src/test.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,19 @@ mod tests {
2626

2727
#[test]
2828
fn test_polymul_ntt_square_modulus() {
29-
let modulus: i64 = 17*17; // Prime modulus
29+
let moduli = [17*17, 12289*12289]; // Different moduli to test
3030
let n: usize = 8; // Length of the NTT (must be a power of 2)
31-
let omega = omega(modulus, n); // n-th root of unity
32-
33-
// Input polynomials (padded to length `n`)
34-
let mut a = vec![1, 2, 3, 4];
35-
let mut b = vec![5, 6, 7, 8];
36-
a.resize(n, 0);
37-
b.resize(n, 0);
38-
39-
// Perform the standard polynomial multiplication
40-
let c_std = polymul(&a, &b, n as i64, modulus);
41-
42-
// Perform the NTT-based polynomial multiplication
43-
let c_fast = polymul_ntt(&a, &b, n, modulus, omega);
4431

45-
// Ensure both methods produce the same result
46-
assert_eq!(c_std, c_fast, "The results of polymul and polymul_ntt do not match");
32+
for &modulus in &moduli {
33+
let omega = omega(modulus, n); // n-th root of unity
34+
let mut a = vec![1, 2, 3, 4];
35+
let mut b = vec![5, 6, 7, 8];
36+
a.resize(n, 0);
37+
b.resize(n, 0);
38+
let c_std = polymul(&a, &b, n as i64, modulus);
39+
let c_fast = polymul_ntt(&a, &b, n, modulus, omega);
40+
assert_eq!(c_std, c_fast, "The results of polymul and polymul_ntt do not match");
41+
}
4742
}
4843

4944
#[test]

0 commit comments

Comments
 (0)