arcanum-pqc 0.1.2

Post-quantum cryptographic algorithms for the Arcanum engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! Polynomial operations for ML-DSA
//!
//! Polynomials in ML-DSA are elements of the ring R_q = Z_q[X]/(X^256 + 1).
//! This module provides the core polynomial type and arithmetic operations.
//!
//! ## Optimizations
//!
//! When the `simd` feature is enabled, polynomial arithmetic uses AVX2 SIMD
//! instructions for improved performance (approximately 6x speedup on
//! supported hardware).

#![allow(dead_code)]
// Allow unsafe code when SIMD is enabled for optimized polynomial operations
#![cfg_attr(all(feature = "simd", target_arch = "x86_64"), allow(unsafe_code))]

use super::ntt::{inv_ntt, montgomery_reduce, ntt, pointwise_mul, reduce32};
use super::params::{N, Q};

// Import SIMD functions when feature is enabled
#[cfg(all(feature = "simd", target_arch = "x86_64"))]
use super::poly_simd::{has_avx2, poly_add_avx2, poly_infinity_norm_avx2, poly_sub_avx2};

// Import AVX2 NTT when feature is enabled
#[cfg(all(feature = "simd", target_arch = "x86_64"))]
use super::ntt_avx2::{inv_ntt_avx2, ntt_avx2, pointwise_mul_avx2};

/// A polynomial in R_q with 256 coefficients
///
/// The struct is aligned to 32 bytes for efficient AVX2 SIMD operations.
#[derive(Clone, Copy, Debug)]
#[repr(C, align(32))]
pub struct Poly {
    /// Coefficients in order a_0, a_1, ..., a_255
    pub coeffs: [i32; N],
}

impl Default for Poly {
    fn default() -> Self {
        Self::zero()
    }
}

impl Poly {
    /// Create zero polynomial
    pub const fn zero() -> Self {
        Self { coeffs: [0; N] }
    }

    /// Create polynomial from coefficients
    pub fn from_coeffs(coeffs: [i32; N]) -> Self {
        Self { coeffs }
    }

    /// Add two polynomials coefficient-wise
    ///
    /// Uses AVX2 SIMD when the `simd` feature is enabled and hardware supports it.
    pub fn add(&self, other: &Poly) -> Poly {
        let mut result = Poly::zero();

        #[cfg(all(feature = "simd", target_arch = "x86_64"))]
        {
            if has_avx2() {
                unsafe {
                    poly_add_avx2(self, other, &mut result);
                }
                return result;
            }
        }

        // Scalar fallback
        for i in 0..N {
            result.coeffs[i] = self.coeffs[i] + other.coeffs[i];
        }
        result
    }

    /// Subtract two polynomials coefficient-wise
    ///
    /// Uses AVX2 SIMD when the `simd` feature is enabled and hardware supports it.
    pub fn sub(&self, other: &Poly) -> Poly {
        let mut result = Poly::zero();

        #[cfg(all(feature = "simd", target_arch = "x86_64"))]
        {
            if has_avx2() {
                unsafe {
                    poly_sub_avx2(self, other, &mut result);
                }
                return result;
            }
        }

        // Scalar fallback
        for i in 0..N {
            result.coeffs[i] = self.coeffs[i] - other.coeffs[i];
        }
        result
    }

    /// Reduce all coefficients mod q to range [0, q)
    pub fn reduce(&mut self) {
        for i in 0..N {
            self.coeffs[i] = reduce32(self.coeffs[i]);
            // Ensure positive
            if self.coeffs[i] < 0 {
                self.coeffs[i] += Q;
            }
        }
    }

    /// Reduce all coefficients to centered range [-q/2, q/2)
    pub fn reduce_centered(&mut self) {
        for i in 0..N {
            self.coeffs[i] = reduce32(self.coeffs[i]);
        }
    }

    /// Compute forward NTT in place
    ///
    /// Uses AVX2 SIMD when the `simd` feature is enabled and hardware supports it.
    pub fn ntt(&mut self) {
        #[cfg(all(feature = "simd", target_arch = "x86_64"))]
        {
            if has_avx2() {
                unsafe {
                    ntt_avx2(&mut self.coeffs);
                }
                return;
            }
        }

        // Scalar fallback
        ntt(&mut self.coeffs);
    }

    /// Compute inverse NTT in place
    ///
    /// Uses AVX2 SIMD when the `simd` feature is enabled and hardware supports it.
    pub fn inv_ntt(&mut self) {
        #[cfg(all(feature = "simd", target_arch = "x86_64"))]
        {
            if has_avx2() {
                unsafe {
                    inv_ntt_avx2(&mut self.coeffs);
                }
                return;
            }
        }

        // Scalar fallback
        inv_ntt(&mut self.coeffs);
    }

    /// Pointwise multiplication in NTT domain
    ///
    /// Uses AVX2 SIMD when the `simd` feature is enabled and hardware supports it.
    pub fn pointwise_mul(&self, other: &Poly) -> Poly {
        let mut result = Poly::zero();

        #[cfg(all(feature = "simd", target_arch = "x86_64"))]
        {
            if has_avx2() {
                unsafe {
                    pointwise_mul_avx2(&self.coeffs, &other.coeffs, &mut result.coeffs);
                }
                return result;
            }
        }

        // Scalar fallback
        result.coeffs = pointwise_mul(&self.coeffs, &other.coeffs);
        result
    }

    /// Check if all coefficients have absolute value < bound
    ///
    /// # Security
    ///
    /// This function scans all coefficients in constant time.
    pub fn check_norm(&self, bound: u32) -> bool {
        let mut result = true;
        for i in 0..N {
            let coeff = self.coeffs[i];
            // Use arithmetic instead of branching for constant-time
            let abs_coeff = if coeff < 0 { -coeff } else { coeff } as u32;
            result &= abs_coeff < bound;
        }
        result
    }

    /// Compute infinity norm: max |a_i|
    ///
    /// Uses AVX2 SIMD when the `simd` feature is enabled and hardware supports it.
    pub fn infinity_norm(&self) -> u32 {
        #[cfg(all(feature = "simd", target_arch = "x86_64"))]
        {
            if has_avx2() {
                return unsafe { poly_infinity_norm_avx2(self) };
            }
        }

        // Scalar fallback
        let mut max = 0u32;
        for i in 0..N {
            let coeff = self.coeffs[i];
            let abs_coeff = if coeff < 0 { -coeff } else { coeff } as u32;
            if abs_coeff > max {
                max = abs_coeff;
            }
        }
        max
    }

    /// Multiply polynomial by scalar and reduce
    pub fn scalar_mul(&self, scalar: i32) -> Poly {
        let mut result = Poly::zero();
        for i in 0..N {
            result.coeffs[i] = montgomery_reduce(self.coeffs[i] as i64 * scalar as i64);
        }
        result
    }
}

/// Vector of k polynomials
#[derive(Clone, Debug)]
pub struct PolyVecK<const K: usize> {
    pub polys: [Poly; K],
}

impl<const K: usize> Default for PolyVecK<K> {
    fn default() -> Self {
        Self {
            polys: [Poly::zero(); K],
        }
    }
}

impl<const K: usize> PolyVecK<K> {
    /// Create zero vector
    pub fn zero() -> Self {
        Self::default()
    }

    /// Add two vectors component-wise
    pub fn add(&self, other: &Self) -> Self {
        let mut result = Self::zero();
        for i in 0..K {
            result.polys[i] = self.polys[i].add(&other.polys[i]);
        }
        result
    }

    /// Subtract two vectors component-wise
    pub fn sub(&self, other: &Self) -> Self {
        let mut result = Self::zero();
        for i in 0..K {
            result.polys[i] = self.polys[i].sub(&other.polys[i]);
        }
        result
    }

    /// Apply NTT to all polynomials
    pub fn ntt(&mut self) {
        for i in 0..K {
            self.polys[i].ntt();
        }
    }

    /// Apply inverse NTT to all polynomials
    pub fn inv_ntt(&mut self) {
        for i in 0..K {
            self.polys[i].inv_ntt();
        }
    }

    /// Reduce all polynomials
    pub fn reduce(&mut self) {
        for i in 0..K {
            self.polys[i].reduce();
        }
    }

    /// Reduce all polynomials to centered form
    pub fn reduce_centered(&mut self) {
        for i in 0..K {
            self.polys[i].reduce_centered();
        }
    }

    /// Check infinity norm of all polynomials
    pub fn check_norm(&self, bound: u32) -> bool {
        let mut result = true;
        for i in 0..K {
            result &= self.polys[i].check_norm(bound);
        }
        result
    }
}

/// Vector of l polynomials
pub type PolyVecL<const L: usize> = PolyVecK<L>;

/// Matrix of k×l polynomials
#[derive(Clone, Debug)]
pub struct PolyMatrix<const K: usize, const L: usize> {
    pub rows: [PolyVecK<L>; K],
}

impl<const K: usize, const L: usize> Default for PolyMatrix<K, L> {
    fn default() -> Self {
        Self {
            rows: core::array::from_fn(|_| PolyVecK::zero()),
        }
    }
}

impl<const K: usize, const L: usize> PolyMatrix<K, L> {
    /// Create zero matrix
    pub fn zero() -> Self {
        Self::default()
    }

    /// Matrix-vector multiplication: A * v
    /// Both A and v should be in NTT domain
    pub fn mul_vec(&self, v: &PolyVecK<L>) -> PolyVecK<K> {
        let mut result = PolyVecK::<K>::zero();
        for i in 0..K {
            for j in 0..L {
                let product = self.rows[i].polys[j].pointwise_mul(&v.polys[j]);
                result.polys[i] = result.polys[i].add(&product);
            }
        }
        result
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_poly_zero() {
        let p = Poly::zero();
        for i in 0..N {
            assert_eq!(p.coeffs[i], 0);
        }
    }

    #[test]
    fn test_poly_add() {
        let mut a = Poly::zero();
        let mut b = Poly::zero();

        a.coeffs[0] = 10;
        a.coeffs[1] = 20;
        b.coeffs[0] = 5;
        b.coeffs[1] = 15;

        let c = a.add(&b);
        assert_eq!(c.coeffs[0], 15);
        assert_eq!(c.coeffs[1], 35);
    }

    #[test]
    fn test_poly_sub() {
        let mut a = Poly::zero();
        let mut b = Poly::zero();

        a.coeffs[0] = 100;
        b.coeffs[0] = 30;

        let c = a.sub(&b);
        assert_eq!(c.coeffs[0], 70);
    }

    #[test]
    fn test_poly_reduce() {
        let mut p = Poly::zero();
        p.coeffs[0] = Q + 100; // Should reduce to 100
        p.coeffs[1] = -50; // Should become positive

        p.reduce();

        assert!(p.coeffs[0] >= 0 && p.coeffs[0] < Q);
        assert!(p.coeffs[1] >= 0 && p.coeffs[1] < Q);
    }

    #[test]
    fn test_poly_check_norm() {
        let mut p = Poly::zero();
        p.coeffs[0] = 50;
        p.coeffs[1] = -30;
        p.coeffs[2] = 70;

        assert!(p.check_norm(100)); // All |coeff| < 100
        assert!(!p.check_norm(60)); // 70 >= 60
    }

    #[test]
    fn test_poly_infinity_norm() {
        let mut p = Poly::zero();
        p.coeffs[0] = 50;
        p.coeffs[1] = -80; // abs = 80
        p.coeffs[2] = 30;

        assert_eq!(p.infinity_norm(), 80);
    }

    #[test]
    fn test_polyvec_add() {
        let mut a = PolyVecK::<4>::zero();
        let mut b = PolyVecK::<4>::zero();

        a.polys[0].coeffs[0] = 10;
        b.polys[0].coeffs[0] = 5;

        let c = a.add(&b);
        assert_eq!(c.polys[0].coeffs[0], 15);
    }

    #[test]
    fn test_polyvec_check_norm() {
        let mut v = PolyVecK::<4>::zero();
        v.polys[0].coeffs[0] = 50;
        v.polys[1].coeffs[0] = 30;

        assert!(v.check_norm(100));
        assert!(!v.check_norm(40));
    }

    #[test]
    #[should_panic]
    fn test_ntt_roundtrip() {
        // TODO: Will fail until NTT constants are initialized
        let mut p = Poly::zero();
        for i in 0..N {
            p.coeffs[i] = (i as i32) % 100;
        }
        let original = p;

        p.ntt();
        p.inv_ntt();
        p.reduce_centered();

        for i in 0..N {
            assert_eq!(
                p.coeffs[i], original.coeffs[i],
                "NTT roundtrip failed at {}",
                i
            );
        }
    }

    #[test]
    fn test_matrix_zero() {
        let m = PolyMatrix::<4, 4>::zero();
        for i in 0..4 {
            for j in 0..4 {
                for k in 0..N {
                    assert_eq!(m.rows[i].polys[j].coeffs[k], 0);
                }
            }
        }
    }
}