dcrypt-algorithms 1.2.3

Cryptographic primitives for the dcrypt library
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
//! polynomial.rs - Enhanced implementation with arithmetic operations

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;

use super::ntt::montgomery_reduce;
use super::params::{Modulus, NttModulus}; // FIXED: Import NttModulus from params
use crate::error::{Error, Result};
use core::marker::PhantomData;
use core::ops::{Add, Neg, Sub};
use zeroize::Zeroize;

/// Convert a value from standard domain to Montgomery domain
#[inline(always)]
fn to_montgomery<M: NttModulus>(val: u32) -> u32 {
    ((val as u64 * M::MONT_R as u64) % M::Q as u64) as u32
}

/// A polynomial in a ring `R_Q = Z_Q[X]/(X^N + 1)`
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Polynomial<M: Modulus> {
    /// Coefficients of the polynomial, stored in standard representation
    #[cfg(feature = "alloc")]
    pub coeffs: Vec<u32>,
    /// Coefficients of the polynomial, stored in standard representation
    #[cfg(not(feature = "alloc"))]
    pub coeffs: [u32; 256], // Will need const generics for proper support
    _marker: PhantomData<M>,
}

// Custom Zeroize implementation that preserves vector length
impl<M: Modulus> Zeroize for Polynomial<M> {
    fn zeroize(&mut self) {
        // Zero all coefficients without changing the length
        #[cfg(feature = "alloc")]
        {
            for coeff in self.coeffs.iter_mut() {
                coeff.zeroize();
            }
        }
        #[cfg(not(feature = "alloc"))]
        {
            self.coeffs.zeroize();
        }
    }
}

impl<M: Modulus> Polynomial<M> {
    /// Creates a new polynomial with all coefficients set to zero
    pub fn zero() -> Self {
        Self {
            coeffs: vec![0; M::N], // length = 256, every coeff = 0
            _marker: PhantomData,
        }
    }

    /// Creates a polynomial from a slice of coefficients
    pub fn from_coeffs(coeffs_slice: &[u32]) -> Result<Self> {
        if coeffs_slice.len() != M::N {
            return Err(Error::Parameter {
                name: "coeffs_slice".into(),
                reason: "Incorrect number of coefficients for polynomial degree N".into(),
            });
        }

        #[cfg(feature = "alloc")]
        let coeffs = coeffs_slice.to_vec();

        #[cfg(not(feature = "alloc"))]
        let mut coeffs = [0u32; 256];
        #[cfg(not(feature = "alloc"))]
        coeffs[..M::N].copy_from_slice(coeffs_slice);

        Ok(Self {
            coeffs,
            _marker: PhantomData,
        })
    }

    /// Returns the degree N of the polynomial
    pub fn degree() -> usize {
        M::N
    }

    /// Returns the modulus Q for coefficient arithmetic
    pub fn modulus_q() -> u32 {
        M::Q
    }

    /// Returns a slice view of the coefficients
    pub fn as_coeffs_slice(&self) -> &[u32] {
        &self.coeffs[..M::N]
    }

    /// Returns a mutable slice view of the coefficients
    pub fn as_mut_coeffs_slice(&mut self) -> &mut [u32] {
        &mut self.coeffs[..M::N]
    }

    /// Branch-free modular reduction of a single coefficient
    #[inline(always)]
    fn reduce_coefficient(a: u32) -> u32 {
        // Branch-free reduction: a - Q if a >= Q else a
        let q = M::Q;
        let mask = ((a >= q) as u32).wrapping_neg();
        a.wrapping_sub(q & mask)
    }

    /// Branch-free conditional subtraction for signed results
    /// FIXED: Simplified to use rem_euclid for proper modular arithmetic
    #[inline(always)]
    fn conditional_sub_q(a: i64) -> u32 {
        let q = M::Q as i64;
        // Use rem_euclid for proper modular arithmetic
        a.rem_euclid(q) as u32
    }

    /// Polynomial addition modulo Q
    pub fn add(&self, other: &Self) -> Self {
        let mut result = Self::zero();
        for i in 0..M::N {
            let sum = self.coeffs[i].wrapping_add(other.coeffs[i]);
            result.coeffs[i] = Self::reduce_coefficient(sum);
        }
        result
    }

    /// Polynomial subtraction modulo Q
    pub fn sub(&self, other: &Self) -> Self {
        let mut result = Self::zero();
        for i in 0..M::N {
            let diff = (self.coeffs[i] as i64) - (other.coeffs[i] as i64);
            result.coeffs[i] = Self::conditional_sub_q(diff);
        }
        result
    }

    /// Polynomial negation modulo Q
    pub fn neg(&self) -> Self {
        let mut result = Self::zero();
        for i in 0..M::N {
            // Mask is 0xFFFF_FFFF when coeff ≠ 0, 0 otherwise
            let mask = ((self.coeffs[i] != 0) as u32).wrapping_neg();
            result.coeffs[i] = (M::Q - self.coeffs[i]) & mask;
        }
        result
    }

    /// Scalar multiplication
    pub fn scalar_mul(&self, scalar: u32) -> Self {
        let mut result = Self::zero();
        for i in 0..M::N {
            let prod = (self.coeffs[i] as u64) * (scalar as u64);
            result.coeffs[i] = (prod % M::Q as u64) as u32;
        }
        result
    }

    /// Schoolbook polynomial multiplication with NEGACYCLIC reduction for Dilithium
    /// In ring `R_q[x]/(x^N + 1)`, when degree >= N, we have `x^N ≡ -1`
    pub fn schoolbook_mul(&self, other: &Self) -> Self {
        let mut result = Self::zero();
        let n = M::N;
        let q = M::Q as u64;

        // Use a temporary array to accumulate products without modular reduction
        // This prevents overflow: max value is n * (q-1)^2 < 2^64 for Dilithium
        let mut tmp = vec![0u64; 2 * n];

        // Step 1: Compute full convolution without modular reduction
        // FIXED: Use iterator instead of indexing
        for (i, &ai_u32) in self.coeffs.iter().enumerate().take(n) {
            let ai = ai_u32 as u64;
            for (j, &bj_u32) in other.coeffs.iter().enumerate().take(n) {
                let bj = bj_u32 as u64;
                tmp[i + j] = tmp[i + j].wrapping_add(ai * bj);
            }
        }

        // Step 2: Apply negacyclic reduction (x^N = -1)
        // Fold upper half back with negation
        for k in n..(2 * n) {
            // When reducing x^k where k >= n, we use x^n = -1
            // So x^k = -x^(k-n)
            let upper_val = tmp[k] % q;
            if upper_val > 0 {
                // Subtract from lower coefficient (equivalent to adding the negative)
                tmp[k - n] = (tmp[k - n] + q - upper_val) % q;
            }
        }

        // Step 3: Final reduction to [0, q)
        #[allow(clippy::needless_range_loop)]
        // We need indexed access here to match tmp and result.coeffs indices
        for i in 0..n {
            result.coeffs[i] = (tmp[i] % q) as u32;
        }

        result
    }

    /// In-place coefficient reduction to ensure all coefficients are < Q
    pub fn reduce_coeffs(&mut self) {
        for i in 0..M::N {
            self.coeffs[i] = Self::reduce_coefficient(self.coeffs[i]);
        }
    }
}

// NTT operations are implemented in ntt.rs as extension methods

/// Extension trait for polynomials with NTT-enabled modulus
pub trait PolynomialNttExt<M: NttModulus> {
    // FIXED: Now uses params::NttModulus
    /// Fast scalar multiplication using Montgomery reduction
    fn scalar_mul_montgomery(&self, scalar: u32) -> Polynomial<M>;
}

impl<M: NttModulus> PolynomialNttExt<M> for Polynomial<M> {
    // FIXED: Now uses params::NttModulus
    fn scalar_mul_montgomery(&self, scalar: u32) -> Polynomial<M> {
        let mut result = Polynomial::<M>::zero();
        // FIXED: Convert scalar to Montgomery form before multiplication
        let scalar_mont = to_montgomery::<M>(scalar);
        for i in 0..M::N {
            // Now both operands are in Montgomery form, so the result stays in Montgomery form
            let prod = (self.coeffs[i] as u64) * (scalar_mont as u64);
            result.coeffs[i] = montgomery_reduce::<M>(prod);
        }
        result
    }
}

/// Barrett reduction for fast modular arithmetic
#[inline(always)]
pub fn barrett_reduce<M: Modulus>(a: u32) -> u32 {
    // Simplified Barrett reduction
    // In production, would use precomputed Barrett constant
    a % M::Q
}

// Implement standard ops traits for ergonomic usage
// Define reference implementations first
impl<M: Modulus> Add for &Polynomial<M> {
    type Output = Polynomial<M>;

    fn add(self, other: Self) -> Self::Output {
        self.add(other)
    }
}

impl<M: Modulus> Sub for &Polynomial<M> {
    type Output = Polynomial<M>;

    fn sub(self, other: Self) -> Self::Output {
        self.sub(other)
    }
}

impl<M: Modulus> Neg for &Polynomial<M> {
    type Output = Polynomial<M>;

    fn neg(self) -> Self::Output {
        self.neg()
    }
}

// Now owned implementations can use the reference implementations
impl<M: Modulus> Add for Polynomial<M> {
    type Output = Self;

    fn add(self, other: Self) -> Self::Output {
        &self + &other
    }
}

impl<M: Modulus> Sub for Polynomial<M> {
    type Output = Self;

    fn sub(self, other: Self) -> Self::Output {
        &self - &other
    }
}

impl<M: Modulus> Neg for Polynomial<M> {
    type Output = Self;

    fn neg(self) -> Self::Output {
        -&self
    }
}

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

    // Test modulus for unit tests
    #[derive(Clone)]
    struct TestModulus;
    impl Modulus for TestModulus {
        const Q: u32 = 3329; // Kyber's Q
        const N: usize = 4; // Small for testing
    }

    #[test]
    fn test_polynomial_creation() {
        let poly = Polynomial::<TestModulus>::zero();
        assert_eq!(poly.as_coeffs_slice(), &[0, 0, 0, 0]);

        let coeffs = vec![1, 2, 3, 4];
        let poly = Polynomial::<TestModulus>::from_coeffs(&coeffs).unwrap();
        assert_eq!(poly.as_coeffs_slice(), &[1, 2, 3, 4]);
    }

    #[test]
    fn test_polynomial_addition() {
        let a = Polynomial::<TestModulus>::from_coeffs(&[1, 2, 3, 4]).unwrap();
        let b = Polynomial::<TestModulus>::from_coeffs(&[5, 6, 7, 8]).unwrap();
        // Use the + operator directly to avoid explicit borrows
        let c = a + b;
        assert_eq!(c.as_coeffs_slice(), &[6, 8, 10, 12]);
    }

    #[test]
    fn test_polynomial_subtraction() {
        let a = Polynomial::<TestModulus>::from_coeffs(&[10, 20, 30, 40]).unwrap();
        let b = Polynomial::<TestModulus>::from_coeffs(&[5, 6, 7, 8]).unwrap();
        // Use the - operator directly to avoid explicit borrows
        let c = a - b;
        assert_eq!(c.as_coeffs_slice(), &[5, 14, 23, 32]);
    }

    #[test]
    fn test_polynomial_negation() {
        let a = Polynomial::<TestModulus>::from_coeffs(&[1, 2, 0, 4]).unwrap();
        // Use the - operator directly to avoid explicit borrows
        let neg_a = -a;
        assert_eq!(neg_a.as_coeffs_slice(), &[3328, 3327, 0, 3325]);
    }

    #[test]
    fn test_modular_reduction() {
        let a = Polynomial::<TestModulus>::from_coeffs(&[3330, 3331, 3328, 0]).unwrap();
        let mut b = a.clone();
        b.reduce_coeffs();
        assert_eq!(b.as_coeffs_slice(), &[1, 2, 3328, 0]);
    }

    #[test]
    fn test_zeroization() {
        let mut poly = Polynomial::<TestModulus>::from_coeffs(&[1, 2, 3, 4]).unwrap();
        poly.zeroize();
        assert_eq!(poly.as_coeffs_slice(), &[0, 0, 0, 0]);
        assert_eq!(poly.coeffs.len(), 4); // Length preserved
    }

    #[test]
    fn test_schoolbook_mul_negacyclic() {
        // Test negacyclic property: x^N = -1
        // For N=4, x^4 = -1, so x^3 * x = -1
        let mut x_cubed = Polynomial::<TestModulus>::zero();
        x_cubed.coeffs[3] = 1; // x^3

        let mut x = Polynomial::<TestModulus>::zero();
        x.coeffs[1] = 1; // x

        let result = x_cubed.schoolbook_mul(&x);
        // x^3 * x = x^4 = -1 mod q = q-1
        assert_eq!(result.coeffs[0], TestModulus::Q - 1);
        assert_eq!(result.coeffs[1], 0);
        assert_eq!(result.coeffs[2], 0);
        assert_eq!(result.coeffs[3], 0);

        // Test a more complex example
        let a = Polynomial::<TestModulus>::from_coeffs(&[1, 2, 3, 4]).unwrap();
        let b = Polynomial::<TestModulus>::from_coeffs(&[5, 6, 7, 8]).unwrap();
        let c = a.schoolbook_mul(&b);

        // Manually compute expected result with negacyclic reduction
        // (1 + 2x + 3x^2 + 4x^3)(5 + 6x + 7x^2 + 8x^3)
        //
        // Full expansion (before reduction):
        // 1*5 = 5
        // 1*6x + 2*5x = 6x + 10x = 16x
        // 1*7x^2 + 2*6x^2 + 3*5x^2 = 7x^2 + 12x^2 + 15x^2 = 34x^2
        // 1*8x^3 + 2*7x^3 + 3*6x^3 + 4*5x^3 = 8x^3 + 14x^3 + 18x^3 + 20x^3 = 60x^3
        // 2*8x^4 + 3*7x^4 + 4*6x^4 = 16x^4 + 21x^4 + 24x^4 = 61x^4
        // 3*8x^5 + 4*7x^5 = 24x^5 + 28x^5 = 52x^5
        // 4*8x^6 = 32x^6
        //
        // Now apply x^4 = -1:
        // x^4 = -1
        // x^5 = -x
        // x^6 = -x^2
        //
        // So:
        // Constant: 5 - 61 = -56 → 3329 - 56 = 3273
        // x: 16 - 52 = -36 → 3329 - 36 = 3293
        // x^2: 34 - 32 = 2
        // x^3: 60

        let expected_0 = ((5i32 - 61i32).rem_euclid(TestModulus::Q as i32)) as u32;
        let expected_1 = ((16i32 - 52i32).rem_euclid(TestModulus::Q as i32)) as u32;
        let expected_2 = ((34i32 - 32i32).rem_euclid(TestModulus::Q as i32)) as u32;
        let expected_3 = 60u32;

        assert_eq!(c.coeffs[0], expected_0);
        assert_eq!(c.coeffs[1], expected_1);
        assert_eq!(c.coeffs[2], expected_2);
        assert_eq!(c.coeffs[3], expected_3);
    }

    #[test]
    fn test_dilithium_negacyclic() {
        // Test with Dilithium-like parameters
        #[derive(Clone)]
        struct DilithiumTestModulus;
        impl Modulus for DilithiumTestModulus {
            const Q: u32 = 8380417; // Dilithium's Q
            const N: usize = 4; // Small for testing, but same negacyclic property
        }

        // Test that x^N = -1 in the ring
        let mut x_to_n_minus_1 = Polynomial::<DilithiumTestModulus>::zero();
        x_to_n_minus_1.coeffs[3] = 1; // x^3

        let mut x = Polynomial::<DilithiumTestModulus>::zero();
        x.coeffs[1] = 1; // x

        let result = x_to_n_minus_1.schoolbook_mul(&x);
        // x^3 * x = x^4 = -1 mod q = q-1
        assert_eq!(result.coeffs[0], DilithiumTestModulus::Q - 1);
        assert_eq!(result.coeffs[1], 0);
        assert_eq!(result.coeffs[2], 0);
        assert_eq!(result.coeffs[3], 0);

        // Test with sparse polynomial (like challenge polynomial c)
        let mut sparse = Polynomial::<DilithiumTestModulus>::zero();
        sparse.coeffs[0] = 1; // +1
        sparse.coeffs[2] = DilithiumTestModulus::Q - 1; // -1

        let dense = Polynomial::<DilithiumTestModulus>::from_coeffs(&[100, 200, 300, 400]).unwrap();
        let result = sparse.schoolbook_mul(&dense);

        // (1 - x^2) * (100 + 200x + 300x^2 + 400x^3)
        // = 100 + 200x + 300x^2 + 400x^3 - 100x^2 - 200x^3 - 300x^4 - 400x^5
        // With x^4 = -1, x^5 = -x:
        // = 100 + 200x + (300-100)x^2 + (400-200)x^3 + 300 + 400x
        // = (100+300) + (200+400)x + 200x^2 + 200x^3
        // = 400 + 600x + 200x^2 + 200x^3

        assert_eq!(result.coeffs[0], 400);
        assert_eq!(result.coeffs[1], 600);
        assert_eq!(result.coeffs[2], 200);
        assert_eq!(result.coeffs[3], 200);
    }
}