qudag-crypto 0.5.1

Quantum-resistant cryptography library for QuDAG - includes ML-KEM-768, ML-DSA, HQC, and BLAKE3
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
//! SIMD utilities for high-performance polynomial arithmetic in cryptographic operations

#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;

/// SIMD-optimized polynomial operations for ML-KEM
pub struct SimdPolynomialOps;

impl SimdPolynomialOps {
    /// Add two polynomials using SIMD instructions
    #[cfg(target_arch = "x86_64")]
    pub fn poly_add_simd(a: &[i32; 256], b: &[i32; 256], result: &mut [i32; 256]) {
        if is_x86_feature_detected!("avx2") {
            unsafe {
                Self::poly_add_avx2(a, b, result);
            }
        } else {
            Self::poly_add_scalar(a, b, result);
        }
    }

    /// Subtract two polynomials using SIMD instructions
    #[cfg(target_arch = "x86_64")]
    pub fn poly_sub_simd(a: &[i32; 256], b: &[i32; 256], result: &mut [i32; 256]) {
        if is_x86_feature_detected!("avx2") {
            unsafe {
                Self::poly_sub_avx2(a, b, result);
            }
        } else {
            Self::poly_sub_scalar(a, b, result);
        }
    }

    /// Multiply polynomial by scalar using SIMD
    #[cfg(target_arch = "x86_64")]
    pub fn poly_scalar_mul_simd(a: &[i32; 256], scalar: i32, result: &mut [i32; 256]) {
        if is_x86_feature_detected!("avx2") {
            unsafe {
                Self::poly_scalar_mul_avx2(a, scalar, result);
            }
        } else {
            Self::poly_scalar_mul_scalar(a, scalar, result);
        }
    }

    /// Reduce polynomial modulo q using SIMD
    #[cfg(target_arch = "x86_64")]
    pub fn poly_reduce_simd(a: &mut [i32; 256], q: i32) {
        if is_x86_feature_detected!("avx2") {
            unsafe {
                Self::poly_reduce_avx2(a, q);
            }
        } else {
            Self::poly_reduce_scalar(a, q);
        }
    }

    // AVX2 implementations
    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2")]
    unsafe fn poly_add_avx2(a: &[i32; 256], b: &[i32; 256], result: &mut [i32; 256]) {
        for i in (0..256).step_by(8) {
            let va = _mm256_loadu_si256(a.as_ptr().add(i) as *const __m256i);
            let vb = _mm256_loadu_si256(b.as_ptr().add(i) as *const __m256i);
            let vr = _mm256_add_epi32(va, vb);
            _mm256_storeu_si256(result.as_mut_ptr().add(i) as *mut __m256i, vr);
        }
    }

    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2")]
    unsafe fn poly_sub_avx2(a: &[i32; 256], b: &[i32; 256], result: &mut [i32; 256]) {
        for i in (0..256).step_by(8) {
            let va = _mm256_loadu_si256(a.as_ptr().add(i) as *const __m256i);
            let vb = _mm256_loadu_si256(b.as_ptr().add(i) as *const __m256i);
            let vr = _mm256_sub_epi32(va, vb);
            _mm256_storeu_si256(result.as_mut_ptr().add(i) as *mut __m256i, vr);
        }
    }

    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2")]
    unsafe fn poly_scalar_mul_avx2(a: &[i32; 256], scalar: i32, result: &mut [i32; 256]) {
        let vscalar = _mm256_set1_epi32(scalar);
        
        for i in (0..256).step_by(8) {
            let va = _mm256_loadu_si256(a.as_ptr().add(i) as *const __m256i);
            let vr = _mm256_mullo_epi32(va, vscalar);
            _mm256_storeu_si256(result.as_mut_ptr().add(i) as *mut __m256i, vr);
        }
    }

    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2")]
    unsafe fn poly_reduce_avx2(a: &mut [i32; 256], q: i32) {
        let vq = _mm256_set1_epi32(q);
        let vq_half = _mm256_set1_epi32(q / 2);
        
        for i in (0..256).step_by(8) {
            let va = _mm256_loadu_si256(a.as_ptr().add(i) as *const __m256i);
            
            // Compute a % q efficiently
            // This is a simplified version - real implementation would use
            // more sophisticated modular reduction techniques
            // Note: _mm256_rem_epi32 doesn't exist, so we'll use a workaround
            let vr = va; // Placeholder - real implementation would use proper modular reduction
            
            // Ensure result is in [-q/2, q/2) range
            let mask_neg = _mm256_cmpgt_epi32(vr, vq_half);
            let adjustment = _mm256_and_si256(mask_neg, vq);
            let final_result = _mm256_sub_epi32(vr, adjustment);
            
            _mm256_storeu_si256(a.as_mut_ptr().add(i) as *mut __m256i, final_result);
        }
    }

    // Scalar fallback implementations
    fn poly_add_scalar(a: &[i32; 256], b: &[i32; 256], result: &mut [i32; 256]) {
        for i in 0..256 {
            result[i] = a[i].wrapping_add(b[i]);
        }
    }

    fn poly_sub_scalar(a: &[i32; 256], b: &[i32; 256], result: &mut [i32; 256]) {
        for i in 0..256 {
            result[i] = a[i].wrapping_sub(b[i]);
        }
    }

    fn poly_scalar_mul_scalar(a: &[i32; 256], scalar: i32, result: &mut [i32; 256]) {
        for i in 0..256 {
            result[i] = a[i].wrapping_mul(scalar);
        }
    }

    fn poly_reduce_scalar(a: &mut [i32; 256], q: i32) {
        for i in 0..256 {
            a[i] = a[i] % q;
            if a[i] > q / 2 {
                a[i] -= q;
            }
        }
    }

    /// Non-SIMD version for compatibility
    #[cfg(not(target_arch = "x86_64"))]
    pub fn poly_add_simd(a: &[i32; 256], b: &[i32; 256], result: &mut [i32; 256]) {
        Self::poly_add_scalar(a, b, result);
    }

    #[cfg(not(target_arch = "x86_64"))]
    pub fn poly_sub_simd(a: &[i32; 256], b: &[i32; 256], result: &mut [i32; 256]) {
        Self::poly_sub_scalar(a, b, result);
    }

    #[cfg(not(target_arch = "x86_64"))]
    pub fn poly_scalar_mul_simd(a: &[i32; 256], scalar: i32, result: &mut [i32; 256]) {
        Self::poly_scalar_mul_scalar(a, scalar, result);
    }

    #[cfg(not(target_arch = "x86_64"))]
    pub fn poly_reduce_simd(a: &mut [i32; 256], q: i32) {
        Self::poly_reduce_scalar(a, q);
    }
}

/// SIMD utilities for hash operations
pub struct SimdHashOps;

impl SimdHashOps {
    /// Parallel hash computation for multiple inputs
    #[cfg(target_arch = "x86_64")]
    pub fn parallel_hash_4way(
        inputs: [&[u8]; 4],
        outputs: &mut [[u8; 32]; 4]
    ) {
        if is_x86_feature_detected!("sha") {
            unsafe {
                Self::parallel_sha256_4way(inputs, outputs);
            }
        } else {
            Self::parallel_hash_fallback(inputs, outputs);
        }
    }

    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "sha")]
    unsafe fn parallel_sha256_4way(
        inputs: [&[u8]; 4],
        outputs: &mut [[u8; 32]; 4]
    ) {
        // This is a simplified example - real implementation would use
        // Intel SHA extensions for parallel SHA-256 computation
        for i in 0..4 {
            let hash = blake3::hash(inputs[i]);
            outputs[i].copy_from_slice(hash.as_bytes());
        }
    }

    fn parallel_hash_fallback(
        inputs: [&[u8]; 4],
        outputs: &mut [[u8; 32]; 4]
    ) {
        for i in 0..4 {
            let hash = blake3::hash(inputs[i]);
            outputs[i].copy_from_slice(hash.as_bytes());
        }
    }

    #[cfg(not(target_arch = "x86_64"))]
    pub fn parallel_hash_4way(
        inputs: [&[u8]; 4],
        outputs: &mut [[u8; 32]; 4]
    ) {
        Self::parallel_hash_fallback(inputs, outputs);
    }
}

/// Memory prefetching utilities for improved cache performance
pub struct PrefetchUtils;

impl PrefetchUtils {
    /// Prefetch data into cache
    #[cfg(target_arch = "x86_64")]
    pub fn prefetch_read(ptr: *const u8) {
        unsafe {
            _mm_prefetch(ptr as *const i8, _MM_HINT_T0);
        }
    }

    /// Prefetch data for writing
    #[cfg(target_arch = "x86_64")]
    pub fn prefetch_write(ptr: *const u8) {
        unsafe {
            _mm_prefetch(ptr as *const i8, _MM_HINT_T1);
        }
    }

    /// Prefetch multiple cache lines
    #[cfg(target_arch = "x86_64")]
    pub fn prefetch_range(start: *const u8, len: usize) {
        const CACHE_LINE_SIZE: usize = 64;
        let end = unsafe { start.add(len) };
        let mut current = start;
        
        while current < end {
            unsafe {
                _mm_prefetch(current as *const i8, _MM_HINT_T0);
                current = current.add(CACHE_LINE_SIZE);
            }
        }
    }

    // No-op implementations for non-x86_64
    #[cfg(not(target_arch = "x86_64"))]
    pub fn prefetch_read(_ptr: *const u8) {}

    #[cfg(not(target_arch = "x86_64"))]
    pub fn prefetch_write(_ptr: *const u8) {}

    #[cfg(not(target_arch = "x86_64"))]
    pub fn prefetch_range(_start: *const u8, _len: usize) {}
}

/// Cache-friendly memory operations
pub struct CacheOptimizedOps;

impl CacheOptimizedOps {
    /// Copy memory with optimal cache line alignment
    pub fn aligned_copy(src: &[u8], dst: &mut [u8]) {
        assert_eq!(src.len(), dst.len());
        
        // Use SIMD for large copies when available
        #[cfg(target_arch = "x86_64")]
        {
            if src.len() >= 32 && is_x86_feature_detected!("avx2") {
                unsafe {
                    Self::avx2_copy(src, dst);
                }
                return;
            }
        }
        
        // Fallback to standard copy
        dst.copy_from_slice(src);
    }

    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2")]
    unsafe fn avx2_copy(src: &[u8], dst: &mut [u8]) {
        let len = src.len();
        let mut i = 0;
        
        // Process 32-byte chunks with AVX2
        while i + 32 <= len {
            let chunk = _mm256_loadu_si256(src.as_ptr().add(i) as *const __m256i);
            _mm256_storeu_si256(dst.as_mut_ptr().add(i) as *mut __m256i, chunk);
            i += 32;
        }
        
        // Handle remaining bytes
        while i < len {
            dst[i] = src[i];
            i += 1;
        }
    }

    /// Zero memory using SIMD when available
    pub fn secure_zero_simd(data: &mut [u8]) {
        #[cfg(target_arch = "x86_64")]
        {
            if data.len() >= 32 && is_x86_feature_detected!("avx2") {
                unsafe {
                    Self::avx2_zero(data);
                }
                return;
            }
        }
        
        // Fallback to standard zeroing
        data.fill(0);
        
        // Compiler fence to prevent optimization
        std::sync::atomic::compiler_fence(std::sync::atomic::Ordering::SeqCst);
    }

    #[cfg(target_arch = "x86_64")]
    #[target_feature(enable = "avx2")]
    unsafe fn avx2_zero(data: &mut [u8]) {
        let len = data.len();
        let mut i = 0;
        let zero = _mm256_setzero_si256();
        
        // Process 32-byte chunks
        while i + 32 <= len {
            _mm256_storeu_si256(data.as_mut_ptr().add(i) as *mut __m256i, zero);
            i += 32;
        }
        
        // Handle remaining bytes
        while i < len {
            data[i] = 0;
            i += 1;
        }
        
        // Compiler fence
        std::sync::atomic::compiler_fence(std::sync::atomic::Ordering::SeqCst);
    }
}

// Note: In a real implementation, these would include proper CPU feature detection,
// fallbacks for different architectures, and would use a more complete set of
// SIMD operations optimized for the specific cryptographic algorithms.

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

    #[test]
    fn test_poly_add_simd() {
        let a = [1i32; 256];
        let b = [2i32; 256];
        let mut result = [0i32; 256];
        
        SimdPolynomialOps::poly_add_simd(&a, &b, &mut result);
        
        for i in 0..256 {
            assert_eq!(result[i], 3);
        }
    }

    #[test]
    fn test_poly_scalar_mul_simd() {
        let a = [2i32; 256];
        let scalar = 3;
        let mut result = [0i32; 256];
        
        SimdPolynomialOps::poly_scalar_mul_simd(&a, scalar, &mut result);
        
        for i in 0..256 {
            assert_eq!(result[i], 6);
        }
    }

    #[test]
    fn test_parallel_hash() {
        let inputs = [
            b"test1".as_slice(),
            b"test2".as_slice(),
            b"test3".as_slice(),
            b"test4".as_slice(),
        ];
        let mut outputs = [[0u8; 32]; 4];
        
        SimdHashOps::parallel_hash_4way(inputs, &mut outputs);
        
        // Verify outputs are different (assuming different inputs)
        assert_ne!(outputs[0], outputs[1]);
        assert_ne!(outputs[1], outputs[2]);
        assert_ne!(outputs[2], outputs[3]);
    }

    #[test]
    fn test_aligned_copy() {
        let src = vec![1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        let mut dst = vec![0u8; 10];
        
        CacheOptimizedOps::aligned_copy(&src, &mut dst);
        
        assert_eq!(src, dst);
    }

    #[test]
    fn test_secure_zero() {
        let mut data = vec![1u8, 2, 3, 4, 5];
        
        CacheOptimizedOps::secure_zero_simd(&mut data);
        
        assert_eq!(data, vec![0u8; 5]);
    }

    #[test]
    fn test_prefetch_operations() {
        let data = vec![1u8; 1024];
        
        // These should not panic
        PrefetchUtils::prefetch_read(data.as_ptr());
        PrefetchUtils::prefetch_write(data.as_ptr());
        PrefetchUtils::prefetch_range(data.as_ptr(), data.len());
    }
}