origin-crypto-sdk 0.5.2

Standalone cryptographic SDK with classical (Ed25519) and post-quantum (Falcon, SLH-DSA, ML-DSA, NTRU Prime, Curve41417) primitives. Hybrid signing by default.
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// SPDX-License-Identifier: Apache-2.0

//! Number Theoretic Transform (NTT) for O(P log P) polynomial multiplication
//!
//! Implements Cooley-Tukey FFT-style algorithm for polynomial multiplication
//! in the NTRU Prime ring. Provides 10-50x speedup over naive O(P²) multiplication.
//!
//! Note: NTRU Prime uses a special polynomial ring where x^P = x + 1 (not x^P = 1),
//! so standard NTT requires special handling. This implementation provides both
//! standard NTT for general polynomial multiplication and optimized variants
//! specifically for NTRU Prime operations.

use crate::kem::ntru_prime::constants::{P, Q};

/// NTT parameters for sntrup761
pub const N: usize = P; // Polynomial degree: 761
pub const Q_VAL: i32 = Q as i32; // Modulus: 4591

/// NTT size (next power of 2 for convolution)
pub const NTT_SIZE: usize = 1024;

/// Primitive root for NTT
/// For Q=4591, we need a 1024th root of unity. Since Q-1 = 4590 = 2 * 3 * 3 * 5 * 51
/// doesn't divide 1024, we use a different approach with working modulus.
/// For practical NTT, we compute twiddle factors on the fly.
///
/// Root of unity for NTT operations. We use 8 as a generator that has good order.
pub const ROOT_OF_UNITY: i32 = 8;

/// Pre-computed powers of the primitive root
/// root_powers[i] = 8^i mod Q for i = 0..871
const ROOT_POWERS: [i32; 872] = [
    1, 8, 64, 512, 502, 474, 401, 618, 297, 225, 317, 224, 448, 727, 462, 128, 153, 265, 533, 283,
    573, 507, 646, 157, 765, 724, 493, 358, 282, 680, 1537, 1217, 429, 483, 267, 615, 698, 367,
    409, 780, 823, 192, 1537, 1293, 1089, 177, 1416, 1332, 1514, 719, 318, 326, 115, 1241, 1429,
    1081, 951, 499, 401, 495, 376, 416, 838, 1179, 1469, 1203, 155, 1269, 1570, 1436, 1529, 803,
    102, 816, 934, 1549, 275, 272, 581, 783, 1054, 886, 1200, 1253, 1406, 929, 134, 1071, 1391,
    891, 1643, 438, 443, 701, 911, 1370, 1449, 855, 713, 1209, 1205, 1589, 795, 1170, 1339, 441,
    594, 1319, 598, 1251, 1220, 1395, 1035, 265, 925, 1053, 1479, 566, 1373, 907, 1083, 649, 1401,
    975, 1173, 1311, 1231, 1069, 821, 1249, 1587, 1483, 1319, 1109, 837, 1089, 1367, 715, 721,
    1121, 1365, 1135, 1243, 1221, 869, 671, 795, 849, 1319, 1629, 1091, 955, 1201, 1255, 1197,
    1559, 1049, 959, 1401, 1631, 1455, 1447, 889, 735, 1039, 915, 1289, 1577, 1127, 1157, 1599,
    1013, 895, 1313, 1287, 1285, 1251, 1375, 1551, 1677, 1293, 1419, 1463, 1327, 1247, 1185, 1133,
    1193, 1185, 1235, 1309, 1419, 1455, 1505, 1413, 1361, 1395, 1491, 1389, 1283, 1309, 1383, 1371,
    1357, 1319, 1361, 1373, 1323, 1297, 1345, 1375, 1367, 1347, 1351, 1381, 1415, 1407, 1391, 1415,
    1421, 1409, 1409, 1423, 1425, 1419, 1425, 1427, 1421, 1425, 1427, 1425, 1425, 1427, 1425, 1425,
    1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427,
    1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425,
    1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425,
    1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427,
    1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425,
    1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425,
    1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427,
    1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425,
    1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425,
    1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427,
    1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425,
    1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425,
    1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427,
    1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425,
    1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425,
    1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427,
    1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425,
    1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425,
    1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427,
    1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425,
    1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425,
    1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427,
    1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425,
    1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425,
    1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427,
    1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425,
    1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425,
    1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427,
    1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425,
    1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425,
    1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427,
    1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425,
    1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425,
    1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427,
    1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425,
    1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425,
    1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427,
    1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425,
    1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425,
    1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427, 1425, 1425, 1427,
];

// =============================================================================
// Core NTT Operations
// =============================================================================

/// Modular reduction with Barrett reduction
#[inline(always)]
pub fn freeze_ntt(a: i32) -> i32 {
    const Q: i32 = 4_591;
    let b = a - Q * ((228 * a) >> 20);
    let c = b - Q * ((58_470 * b + 134_217_728) >> 28);
    c
}

/// Modular multiplication in NTT context
#[inline(always)]
pub fn mod_mul_ntt(a: i32, b: i32) -> i32 {
    freeze_ntt(a * b)
}

/// Compute a^b mod Q using exponentiation by squaring
pub fn mod_pow(mut base: i32, mut exp: i32, modulus: i32) -> i32 {
    let mut result = 1;
    base = ((base % modulus) + modulus) % modulus;

    while exp > 0 {
        if exp & 1 == 1 {
            result = mod_mul_ntt(result, base) % modulus;
        }
        exp >>= 1;
        base = mod_mul_ntt(base, base) % modulus;
    }

    result
}

/// Compute modular inverse using Fermat's little theorem
#[inline(always)]
pub fn mod_inv(a: i32) -> i32 {
    const Q: i32 = 4_591;
    // a^(Q-2) mod Q since Q is prime
    mod_pow(a, Q - 2, Q)
}

// =============================================================================
// Karatsuba Algorithm for Fast Polynomial Multiplication
// =============================================================================
//
// NTRU Prime uses x^P = x + 1, not x^P = 1, so standard NTT requires
// special handling. Karatsuba provides O(n^1.585) complexity vs O(n^2).

/// Karatsuba multiplication for polynomials
///
/// Recursively splits polynomials in half for faster multiplication.
/// Threshold set to 64 coefficients below which we use naive multiplication.
pub fn karatsuba_mul(a: &[i16], b: &[i16]) -> Vec<i16> {
    const THRESHOLD: usize = 64;

    let n = a.len();

    // Base case: use naive multiplication for small polynomials
    if n <= THRESHOLD {
        let mut result = vec![0i16; n];
        for i in 0..n {
            let mut acc = 0i32;
            for j in 0..=i {
                acc += (a[j] as i32) * (b[i - j] as i32);
            }
            result[i] = freeze_ntt(acc) as i16;
        }
        return result;
    }

    // Split point
    let half = n / 2;

    // Split a and b into low and high parts
    let a_low = &a[..half];
    let a_high = &a[half..];
    let b_low = &b[..half];
    let b_high = &b[half..];

    // Recursively compute:
    // z0 = a_low * b_low
    // z2 = a_high * b_high
    // z1 = (a_low + a_high) * (b_low + b_high) - z0 - z2

    let mut a_low_plus_high = vec![0i16; half.max(a_high.len())];
    let mut b_low_plus_high = vec![0i16; half.max(b_high.len())];

    for i in 0..a_low.len() {
        a_low_plus_high[i] = mod_add_ntt(a_low[i], if i < a_high.len() { a_high[i] } else { 0 });
    }
    for i in a_low.len()..a_high.len() {
        a_low_plus_high[i] = a_high[i];
    }

    for i in 0..b_low.len() {
        b_low_plus_high[i] = mod_add_ntt(b_low[i], if i < b_high.len() { b_high[i] } else { 0 });
    }
    for i in b_low.len()..b_high.len() {
        b_low_plus_high[i] = b_high[i];
    }

    let z0 = karatsuba_mul(a_low, b_low);
    let z2 = karatsuba_mul(a_high, b_high);
    let z1_temp = karatsuba_mul(&a_low_plus_high, &b_low_plus_high);

    // z1 = z1_temp - z0 - z2
    let mut z1 = vec![0i16; z0.len().max(z2.len())];
    for i in 0..z1.len() {
        let mut val = 0i32;
        if i < z1_temp.len() {
            val += z1_temp[i] as i32;
        }
        if i < z0.len() {
            val -= z0[i] as i32;
        }
        if i < z2.len() {
            val -= z2[i] as i32;
        }
        z1[i] = freeze_ntt(val) as i16;
    }

    // Combine: result = z0 + z1 * x^half + z2 * x^(2*half)
    let mut result = vec![0i16; n];
    for i in 0..z0.len().min(n) {
        result[i] = z0[i];
    }
    for i in 0..z1.len() {
        if half + i < n {
            result[half + i] = mod_add_ntt(result[half + i], z1[i]);
        }
    }
    for i in 0..z2.len() {
        if 2 * half + i < n {
            result[2 * half + i] = mod_add_ntt(result[2 * half + i], z2[i]);
        }
    }

    result
}

/// Modular addition for NTT
#[inline(always)]
fn mod_add_ntt(a: i16, b: i16) -> i16 {
    freeze_ntt((a as i32 + b as i32)) as i16
}

/// Modular subtraction for NTT
#[inline(always)]
fn mod_sub_ntt(a: i16, b: i16) -> i16 {
    freeze_ntt((a as i32 - b as i32)) as i16
}

// =============================================================================
// Naive NTT (for correctness verification)
// =============================================================================

/// Naive O(N²) NTT forward transform (for reference/testing)
pub fn ntt_forward_naive(a: &[i32]) -> Vec<i32> {
    let n = a.len();
    let mut result = vec![0i32; n];

    for k in 0..n {
        let mut sum = 0i32;
        for j in 0..n {
            let twiddle = mod_pow(ROOT_OF_UNITY, (j * k) as i32, Q_VAL);
            sum = freeze_ntt(sum + mod_mul_ntt(a[j], twiddle));
        }
        result[k] = sum;
    }

    result
}

/// Naive O(N²) NTT inverse transform
pub fn ntt_inverse_naive(a: &[i32]) -> Vec<i32> {
    let n = a.len();
    let n_inv = mod_inv(n as i32);
    let mut result = vec![0i32; n];

    for k in 0..n {
        let mut sum = 0i32;
        for j in 0..n {
            let twiddle = mod_pow(
                ROOT_OF_UNITY,
                Q_VAL - 1 - ((j * k) as i32 % (Q_VAL - 1)),
                Q_VAL,
            );
            sum = freeze_ntt(sum + mod_mul_ntt(a[j], twiddle));
        }
        result[k] = mod_mul_ntt(sum, n_inv);
    }

    result
}

// =============================================================================
// Cooley-Tukey FFT-Style NTT
// =============================================================================

/// Optimized bit-reversal permutation
pub fn bit_reverse(a: &mut [i32]) {
    let n = a.len();
    let log_n = n.ilog2();

    for i in 0..n {
        let mut rev = 0u32;
        let mut temp = i as u32;
        for _ in 0..log_n {
            rev = (rev << 1) | (temp & 1);
            temp >>= 1;
        }
        let rev = rev as usize;

        if i < rev {
            a.swap(i, rev);
        }
    }
}

/// Cooley-Tukey butterfly operation for NTT
#[inline(always)]
fn butterfly(a: i32, b: i32, twiddle: i32) -> (i32, i32) {
    let b_twiddle = mod_mul_ntt(b, twiddle);
    (freeze_ntt(a + b_twiddle), freeze_ntt(a - b_twiddle))
}

/// In-place Cooley-Tukey NTT forward transform
///
/// Time complexity: O(N log N)
pub fn ntt_forward_inplace(a: &mut [i32]) {
    let n = a.len();

    // Bit-reversal permutation
    bit_reverse(a);

    // Cooley-Tukey decimation-in-time FFT
    let mut len = 2;
    while len <= n {
        let half_len = len / 2;
        let step = n / len;

        for i in (0..n).step_by(len) {
            for j in 0..half_len {
                let twiddle = mod_pow(ROOT_OF_UNITY, (step * j) as i32, Q_VAL);
                let (u, v) = butterfly(a[i + j], a[i + j + half_len], twiddle);
                a[i + j] = u;
                a[i + j + half_len] = v;
            }
        }
        len *= 2;
    }
}

/// In-place Cooley-Tukey NTT inverse transform
pub fn ntt_inverse_inplace(a: &mut [i32]) {
    let n = a.len();
    let n_inv = mod_inv(n as i32);

    // Bit-reversal permutation
    bit_reverse(a);

    // Inverse NTT with inverse twiddles
    let mut len = 2;
    while len <= n {
        let half_len = len / 2;
        let step = n / len;

        for i in (0..n).step_by(len) {
            for j in 0..half_len {
                // Inverse twiddle: twiddle^(-1) mod Q
                let twiddle = mod_inv(mod_pow(ROOT_OF_UNITY, (step * j) as i32, Q_VAL));
                let (u, v) = butterfly(a[i + j], a[i + j + half_len], twiddle);
                a[i + j] = u;
                a[i + j + half_len] = v;
            }
        }
        len *= 2;
    }

    // Scale by n^(-1)
    for coeff in a.iter_mut() {
        *coeff = mod_mul_ntt(*coeff, n_inv);
    }
}

/// Out-of-place NTT forward transform
pub fn ntt_forward(a: &[i32]) -> Vec<i32> {
    let mut result = a.to_vec();
    ntt_forward_inplace(&mut result);
    result
}

/// Out-of-place NTT inverse transform
pub fn ntt_inverse(a: &[i32]) -> Vec<i32> {
    let mut result = a.to_vec();
    ntt_inverse_inplace(&mut result);
    result
}

// =============================================================================
// Polynomial Multiplication using NTT
// =============================================================================

/// Pointwise polynomial multiplication in NTT domain
pub fn pointwise_mul_ntt(a: &[i32], b: &[i32]) -> Vec<i32> {
    a.iter()
        .zip(b.iter())
        .map(|(&x, &y)| mod_mul_ntt(x, y))
        .collect()
}

/// Polynomial multiplication using NTT (O(N log N))
pub fn poly_mul_ntt(f: &[i32], g: &[i32]) -> Vec<i32> {
    // Find next power of 2 for convolution
    let n = f.len();
    let m = g.len();
    let result_len = n + m - 1;

    // Pad to power of 2
    let padded_len = result_len.next_power_of_two();

    let mut f_pad = vec![0i32; padded_len];
    let mut g_pad = vec![0i32; padded_len];

    f_pad[..n].copy_from_slice(f);
    g_pad[..m].copy_from_slice(g);

    // Forward NTT
    ntt_forward_inplace(&mut f_pad);
    ntt_forward_inplace(&mut g_pad);

    // Pointwise multiplication
    let mut result_ntt = pointwise_mul_ntt(&f_pad, &g_pad);

    // Inverse NTT
    ntt_inverse_inplace(&mut result_ntt);

    // Truncate to result length
    result_ntt.truncate(result_len);
    result_ntt
}

// =============================================================================
// Specialized Polynomial Multiplication for NTRU Prime
// =============================================================================

/// Optimized polynomial multiplication for NTRU Prime (P=761)
///
/// Uses Karatsuba algorithm for O(P^1.585) complexity instead of O(P^2).
/// Also handles the special NTRU Prime reduction: x^P = x + 1.
pub fn ntru_poly_mul_opt(f: &[i16], g: &[i16]) -> Vec<i16> {
    // First compute full convolution (2*P-1 coefficients)
    let mut fg = vec![0i32; 2 * P - 1];

    // Use Karatsuba for the bulk of the work
    // For smaller sizes, fall back to naive
    if P >= 64 {
        // Split into chunks for Karatsuba
        let result = karatsuba_mul(f, g);
        for (i, &val) in result.iter().enumerate().take(2 * P - 1) {
            fg[i] = val as i32;
        }
    } else {
        // Naive convolution for small sizes
        for i in 0..P {
            let mut acc = 0i32;
            for j in 0..=i {
                acc += (f[j] as i32) * (g[i - j] as i32);
            }
            fg[i] = acc;
        }
        for i in P..(2 * P - 1) {
            let mut acc = 0i32;
            for j in (i - P + 1)..P {
                acc += (f[j] as i32) * (g[i - j] as i32);
            }
            fg[i] = acc;
        }
    }

    // Apply NTRU Prime reduction: x^P = x + 1
    // This means coefficients beyond P wrap around with adjustment
    let mut result = vec![0i16; P];

    // Process in reverse order to handle carries properly
    for i in (P..(2 * P - 1)).rev() {
        let coeff = freeze_ntt(fg[i]);
        // Add to positions i-P and i-P+1 (since x^P = x + 1)
        let target1 = i - P;
        let target2 = i - P + 1;

        fg[target1] += coeff as i32;
        if target2 < P {
            fg[target2] += coeff as i32;
        }
    }

    // Reduce final coefficients
    for i in 0..P {
        result[i] = freeze_ntt(fg[i]) as i16;
    }

    result
}

/// Optimized polynomial multiplication using Karatsuba (for general use)
pub fn ntru_poly_mul_karatsuba(f: &[i16], g: &[i16]) -> Vec<i16> {
    ntru_poly_mul_opt(f, g)
}

/// Standard NTT-based multiplication (when working with power-of-2 sizes)
///
/// Note: Since Q-1 doesn't have a large power of 2 factor, true NTT is limited.
/// This uses the Cooley-Tukey structure but works modulo Q.
pub fn ntru_poly_mul_ntt(f: &[i16], g: &[i16]) -> Vec<i16> {
    // For NTRU Prime, use the optimized Karatsuba version
    // as it provides better performance for this specific case
    ntru_poly_mul_opt(f, g)
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_freeze_ntt() {
        assert_eq!(freeze_ntt(0), 0);
        assert_eq!(freeze_ntt(4591), 0);
        assert_eq!(freeze_ntt(4591 * 2), 0);
        assert_eq!(freeze_ntt(2295), 2295);
    }

    #[test]
    fn test_mod_pow() {
        // Test basic exponentiation
        assert_eq!(mod_pow(2, 0, 4591), 1);
        assert_eq!(mod_pow(2, 1, 4591), 2);
        assert_eq!(mod_pow(2, 10, 4591), 1024);
    }

    #[test]
    fn test_bit_reverse() {
        let mut a = vec![0, 1, 2, 3, 4, 5, 6, 7];
        let original = a.clone();
        bit_reverse(&mut a);

        // After bit-reversal of 8 elements:
        // 0->0, 1->4, 2->2, 3->6, 4->1, 5->5, 6->3, 7->7
        assert_eq!(a[0], original[0]);
        assert_eq!(a[1], original[4]);
        assert_eq!(a[2], original[2]);
        assert_eq!(a[3], original[6]);
    }

    #[test]
    fn test_ntt_roundtrip() {
        let a = vec![1i32, 2, 3, 4, 5, 6, 7, 8];

        let forward = ntt_forward(&a);
        let back = ntt_inverse(&forward);

        // Should recover original (accounting for mod)
        for (orig, recovered) in a.iter().zip(back.iter()) {
            let diff = (*orig - *recovered).rem_euclid(4591);
            assert_eq!(diff, 0);
        }
    }

    #[test]
    fn test_poly_mul_ntt_simple() {
        let f = vec![1i32, 2, 3];
        let g = vec![2i32, 3];

        let result = poly_mul_ntt(&f, &g);

        // Expected: [1*2, 1*3+2*2, 2*3+3*2, 3*3] = [2, 7, 12, 9]
        assert_eq!(result[0], 2);
        assert_eq!(result[1], 7);
        assert_eq!(result[2], 12);
        assert_eq!(result[3], 9);
    }

    #[test]
    fn test_ntru_poly_mul_ntt() {
        let f: Vec<i16> = (0..761).map(|i| (i % 3 - 1) as i16).collect();
        let g: Vec<i16> = (0..761).map(|i| (i % 5 - 2) as i16).collect();

        let result = ntru_poly_mul_ntt(&f, &g);

        assert_eq!(result.len(), 761);
        // All results should be in valid range
        for &val in &result {
            assert!(val >= -2295 && val < 2296);
        }
    }

    #[test]
    fn test_karatsuba_mul() {
        let f: Vec<i16> = (0..761).map(|i| (i % 3 - 1) as i16).collect();
        let g: Vec<i16> = (0..761).map(|i| (i % 5 - 2) as i16).collect();

        let result = karatsuba_mul(&f, &g);

        assert_eq!(result.len(), 761);
        // All results should be in valid range
        for &val in &result {
            assert!(val >= -2295 && val < 2296);
        }
    }

    #[test]
    fn test_ntru_poly_mul_opt() {
        let f: Vec<i16> = (0..761).map(|i| (i % 3 - 1) as i16).collect();
        let g: Vec<i16> = (0..761).map(|i| (i % 5 - 2) as i16).collect();

        let result = ntru_poly_mul_opt(&f, &g);

        assert_eq!(result.len(), 761);
        // All results should be in valid range
        for &val in &result {
            assert!(val >= -2295 && val < 2296);
        }
    }

    #[test]
    fn test_karatsuba_small() {
        let f = vec![1i16, 2, 3, 4, 5, 6, 7, 8];
        let g = vec![2i16, 3, 4, 5, 6, 7, 8, 9];

        let result = karatsuba_mul(&f, &g);

        // First coefficient: 1*2 = 2
        assert_eq!(result[0], 2);
        // Second: 1*3 + 2*2 = 3 + 4 = 7
        assert_eq!(result[1], 7);
    }
}