falcon-rs 0.2.4

Native Rust implementation of FN-DSA (FIPS 206), the NIST post-quantum digital signature standard (formerly Falcon)
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
//! High-level Falcon API.
//! Ported from falcon.c.

use crate::{
    codec, common,
    fpr::Fpr,
    keygen,
    shake::{
        i_shake256_extract, i_shake256_flip, i_shake256_init, i_shake256_inject,
        InnerShake256Context,
    },
    sign, vrfy,
};

// ======================================================================
// Error codes
// ======================================================================

pub const FALCON_ERR_RANDOM: i32 = -1;
pub const FALCON_ERR_SIZE: i32 = -2;
pub const FALCON_ERR_FORMAT: i32 = -3;
pub const FALCON_ERR_BADSIG: i32 = -4;
pub const FALCON_ERR_BADARG: i32 = -5;
pub const FALCON_ERR_INTERNAL: i32 = -6;

// ======================================================================
// Signature format types
// ======================================================================

pub const FALCON_SIG_COMPRESSED: i32 = 1;
pub const FALCON_SIG_PADDED: i32 = 2;
pub const FALCON_SIG_CT: i32 = 3;

// ======================================================================
// Size computations
// ======================================================================

pub const fn falcon_privkey_size(logn: u32) -> usize {
    if logn <= 3 {
        (3 << logn) + 1
    } else {
        ((10 - (logn >> 1)) << (logn - 2)) as usize + (1 << logn) + 1
    }
}

pub const fn falcon_pubkey_size(logn: u32) -> usize {
    if logn <= 1 {
        5
    } else {
        (7 << (logn - 2)) + 1
    }
}

pub const fn falcon_sig_compressed_maxsize(logn: u32) -> usize {
    if logn < 1 || logn > 10 {
        return 0;
    }
    (((11 << logn) + (101 >> (10 - logn)) + 7) >> 3) + 41
}

pub const fn falcon_sig_padded_size(logn: u32) -> usize {
    if logn < 1 || logn > 10 {
        return 0;
    }
    44 + 3 * (256 >> (10 - logn))
        + 2 * (128 >> (10 - logn))
        + 3 * (64 >> (10 - logn))
        + 2 * (16 >> (10 - logn))
        - 2 * (2 >> (10 - logn))
        - 8 * (1 >> (10 - logn))
}

pub const fn falcon_sig_ct_size(logn: u32) -> usize {
    if logn < 1 || logn > 10 {
        return 0;
    }
    let base = (3 << (logn - 1)) + 41;
    if logn == 3 {
        base - 1
    } else {
        base
    }
}

pub const fn falcon_tmpsize_keygen(logn: u32) -> usize {
    (if logn <= 3 { 272 } else { 28 << logn }) + (3 << logn) + 7
}

pub const fn falcon_tmpsize_makepub(logn: u32) -> usize {
    (6 << logn) + 1
}

pub const fn falcon_tmpsize_signdyn(logn: u32) -> usize {
    (78 << logn) + 7
}

pub const fn falcon_tmpsize_signtree(logn: u32) -> usize {
    (50 << logn) + 7
}

pub const fn falcon_tmpsize_expandpriv(logn: u32) -> usize {
    (52 << logn) + 7
}

pub const fn falcon_expandedkey_size(logn: u32) -> usize {
    ((8 * (logn as usize) + 40) << logn) + 8
}

pub const fn falcon_tmpsize_verify(logn: u32) -> usize {
    (8 << logn) + 1
}

// ======================================================================
// Alignment helpers
// ======================================================================

fn align_u64(tmp: &mut [u8]) -> &mut [u8] {
    let addr = tmp.as_ptr() as usize;
    let off = addr & 7;
    if off != 0 {
        &mut tmp[8 - off..]
    } else {
        tmp
    }
}

fn align_u16(tmp: &mut [u8]) -> &mut [u8] {
    let addr = tmp.as_ptr() as usize;
    if addr & 1 != 0 {
        &mut tmp[1..]
    } else {
        tmp
    }
}

fn align_fpr(tmp: &mut [u8]) -> &mut [u8] {
    align_u64(tmp)
}

// ======================================================================
// SHAKE256 public API
// ======================================================================

/// Initialize a SHAKE256 context.
pub fn shake256_init(sc: &mut InnerShake256Context) {
    i_shake256_init(sc);
}

/// Inject data into a SHAKE256 context.
pub fn shake256_inject(sc: &mut InnerShake256Context, data: &[u8]) {
    i_shake256_inject(sc, data);
}

/// Flip a SHAKE256 context to output mode.
pub fn shake256_flip(sc: &mut InnerShake256Context) {
    i_shake256_flip(sc);
}

/// Extract data from a SHAKE256 context.
pub fn shake256_extract(sc: &mut InnerShake256Context, out: &mut [u8]) {
    i_shake256_extract(sc, out);
}

/// Initialize a SHAKE256 PRNG from an explicit seed.
pub fn shake256_init_prng_from_seed(sc: &mut InnerShake256Context, seed: &[u8]) {
    shake256_init(sc);
    shake256_inject(sc, seed);
    shake256_flip(sc);
}

/// Initialize a SHAKE256 PRNG from the OS-provided RNG.
///
/// Returns 0 on success, or FALCON_ERR_RANDOM (-1) if the OS RNG
/// is unavailable or fails.
pub fn shake256_init_prng_from_system(sc: &mut InnerShake256Context) -> i32 {
    let mut seed = zeroize::Zeroizing::new([0u8; 48]);
    if !crate::rng::get_seed(&mut *seed) {
        return FALCON_ERR_RANDOM;
    }
    shake256_init(sc);
    shake256_inject(sc, &*seed);
    shake256_flip(sc);
    0
}

// ======================================================================
// Key pair generation
// ======================================================================

/// Generate a new Falcon key pair.
///
/// Returns 0 on success, or a negative error code.
pub fn falcon_keygen_make(
    rng: &mut InnerShake256Context,
    logn: u32,
    privkey: &mut [u8],
    pubkey: Option<&mut [u8]>,
    tmp: &mut [u8],
) -> i32 {
    if logn < 1 || logn > 10 {
        return FALCON_ERR_BADARG;
    }
    let sk_len = falcon_privkey_size(logn);
    if privkey.len() < sk_len {
        return FALCON_ERR_SIZE;
    }
    if let Some(ref pk) = pubkey {
        if pk.len() < falcon_pubkey_size(logn) {
            return FALCON_ERR_SIZE;
        }
    }
    if tmp.len() < falcon_tmpsize_keygen(logn) {
        return FALCON_ERR_SIZE;
    }

    let n: usize = 1 << logn;

    // Use tmp for f, g, F, then the rest for the keygen algorithm.
    let f_off = 0;
    let g_off = n;
    let big_f_off = 2 * n;
    let atmp_off = {
        let base = big_f_off + n;
        let addr = unsafe { tmp.as_ptr().add(base) as usize };
        let off = addr & 7;
        if off != 0 {
            base + 8 - off
        } else {
            base
        }
    };

    // Prepare i8 slices in tmp for keygen.
    {
        let ptr = tmp.as_mut_ptr();
        let f = unsafe { core::slice::from_raw_parts_mut(ptr.add(f_off) as *mut i8, n) };
        let g = unsafe { core::slice::from_raw_parts_mut(ptr.add(g_off) as *mut i8, n) };
        let big_f = unsafe { core::slice::from_raw_parts_mut(ptr.add(big_f_off) as *mut i8, n) };
        let atmp =
            unsafe { core::slice::from_raw_parts_mut(ptr.add(atmp_off), tmp.len() - atmp_off) };

        keygen::keygen(rng, f, g, big_f, None, None, logn, atmp);
    }

    // Encode private key.
    privkey[0] = 0x50 + logn as u8;
    let mut u: usize = 1;
    {
        let f = unsafe { core::slice::from_raw_parts(tmp[f_off..].as_ptr() as *const i8, n) };
        let v = codec::trim_i8_encode(
            Some(&mut privkey[u..]),
            f,
            logn,
            codec::MAX_FG_BITS[logn as usize] as u32,
        );
        if v == 0 {
            return FALCON_ERR_INTERNAL;
        }
        u += v;
    }
    {
        let g = unsafe { core::slice::from_raw_parts(tmp[g_off..].as_ptr() as *const i8, n) };
        let v = codec::trim_i8_encode(
            Some(&mut privkey[u..]),
            g,
            logn,
            codec::MAX_FG_BITS[logn as usize] as u32,
        );
        if v == 0 {
            return FALCON_ERR_INTERNAL;
        }
        u += v;
    }
    {
        let big_f =
            unsafe { core::slice::from_raw_parts(tmp[big_f_off..].as_ptr() as *const i8, n) };
        let v = codec::trim_i8_encode(
            Some(&mut privkey[u..]),
            big_f,
            logn,
            codec::MAX_FG_BITS_UPPER[logn as usize] as u32,
        );
        if v == 0 {
            return FALCON_ERR_INTERNAL;
        }
        u += v;
    }
    if u != sk_len {
        return FALCON_ERR_INTERNAL;
    }

    // Compute and encode public key if requested.
    if let Some(pk) = pubkey {
        let pk_len = falcon_pubkey_size(logn);
        let ptr = tmp.as_mut_ptr();
        let f = unsafe { core::slice::from_raw_parts(ptr.add(f_off) as *const i8, n) };
        let g = unsafe { core::slice::from_raw_parts(ptr.add(g_off) as *const i8, n) };

        // Use g+n area for h and atmp.
        let h_base = g_off + n;
        let h_addr = unsafe { ptr.add(h_base) as usize };
        let h_off = if h_addr & 1 != 0 { h_base + 1 } else { h_base };
        let h = unsafe { core::slice::from_raw_parts_mut(ptr.add(h_off) as *mut u16, n) };
        let atmp2 = unsafe {
            core::slice::from_raw_parts_mut(ptr.add(h_off + 2 * n), tmp.len() - h_off - 2 * n)
        };

        if !vrfy::compute_public(h, f, g, logn, atmp2) {
            return FALCON_ERR_INTERNAL;
        }
        pk[0] = logn as u8;
        let v = codec::modq_encode(Some(&mut pk[1..]), h, logn);
        if v != pk_len - 1 {
            return FALCON_ERR_INTERNAL;
        }
    }

    0
}

/// Recompute the public key from the private key.
pub fn falcon_make_public(pubkey: &mut [u8], privkey: &[u8], tmp: &mut [u8]) -> i32 {
    if privkey.is_empty() {
        return FALCON_ERR_FORMAT;
    }
    if privkey[0] & 0xF0 != 0x50 {
        return FALCON_ERR_FORMAT;
    }
    let logn = (privkey[0] & 0x0F) as u32;
    if logn < 1 || logn > 10 {
        return FALCON_ERR_FORMAT;
    }
    if privkey.len() != falcon_privkey_size(logn) {
        return FALCON_ERR_FORMAT;
    }
    let pk_len = falcon_pubkey_size(logn);
    if pubkey.len() < pk_len {
        return FALCON_ERR_SIZE;
    }
    if tmp.len() < falcon_tmpsize_makepub(logn) {
        return FALCON_ERR_SIZE;
    }

    let n: usize = 1 << logn;

    // Decode f and g into tmp.
    let ptr = tmp.as_mut_ptr();
    let f = unsafe { core::slice::from_raw_parts_mut(ptr as *mut i8, n) };
    let g = unsafe { core::slice::from_raw_parts_mut(ptr.add(n) as *mut i8, n) };

    let mut u: usize = 1;
    let v = codec::trim_i8_decode(
        f,
        logn,
        codec::MAX_FG_BITS[logn as usize] as u32,
        &privkey[u..],
    );
    if v == 0 {
        return FALCON_ERR_FORMAT;
    }
    u += v;
    let v = codec::trim_i8_decode(
        g,
        logn,
        codec::MAX_FG_BITS[logn as usize] as u32,
        &privkey[u..],
    );
    if v == 0 {
        return FALCON_ERR_FORMAT;
    }

    // Compute public key.
    let h_off = if (unsafe { ptr.add(2 * n) as usize }) & 1 != 0 {
        2 * n + 1
    } else {
        2 * n
    };
    let h = unsafe { core::slice::from_raw_parts_mut(ptr.add(h_off) as *mut u16, n) };
    let atmp = unsafe {
        core::slice::from_raw_parts_mut(ptr.add(h_off + 2 * n), tmp.len() - h_off - 2 * n)
    };
    if !vrfy::compute_public(h, f, g, logn, atmp) {
        return FALCON_ERR_FORMAT;
    }

    pubkey[0] = logn as u8;
    let v = codec::modq_encode(Some(&mut pubkey[1..]), h, logn);
    if v != pk_len - 1 {
        return FALCON_ERR_INTERNAL;
    }
    0
}

/// Get the Falcon degree from a header byte.
pub fn falcon_get_logn(obj: &[u8]) -> i32 {
    if obj.is_empty() {
        return FALCON_ERR_FORMAT;
    }
    let logn = (obj[0] & 0x0F) as i32;
    if logn < 1 || logn > 10 {
        return FALCON_ERR_FORMAT;
    }
    logn
}

// ======================================================================
// Signature generation
// ======================================================================

/// Start a signature: generate a nonce and initialize hash_data.
pub fn falcon_sign_start(
    rng: &mut InnerShake256Context,
    nonce: &mut [u8],
    hash_data: &mut InnerShake256Context,
) -> i32 {
    shake256_extract(rng, &mut nonce[..40]);
    shake256_init(hash_data);
    shake256_inject(hash_data, &nonce[..40]);
    0
}

/// Finish a signature using the raw private key ("dynamic" mode).
pub fn falcon_sign_dyn_finish(
    rng: &mut InnerShake256Context,
    sig: &mut [u8],
    sig_len: &mut usize,
    sig_type: i32,
    privkey: &[u8],
    hash_data: &mut InnerShake256Context,
    nonce: &[u8],
    tmp: &mut [u8],
) -> i32 {
    if privkey.is_empty() {
        return FALCON_ERR_FORMAT;
    }
    if privkey[0] & 0xF0 != 0x50 {
        return FALCON_ERR_FORMAT;
    }
    let logn = (privkey[0] & 0x0F) as u32;
    if logn < 1 || logn > 10 {
        return FALCON_ERR_FORMAT;
    }
    if privkey.len() != falcon_privkey_size(logn) {
        return FALCON_ERR_FORMAT;
    }
    if tmp.len() < falcon_tmpsize_signdyn(logn) {
        return FALCON_ERR_SIZE;
    }
    let es_len = *sig_len;
    if es_len < 41 {
        return FALCON_ERR_SIZE;
    }

    match sig_type {
        FALCON_SIG_COMPRESSED => {}
        FALCON_SIG_PADDED => {
            if es_len < falcon_sig_padded_size(logn) {
                return FALCON_ERR_SIZE;
            }
        }
        FALCON_SIG_CT => {
            if es_len < falcon_sig_ct_size(logn) {
                return FALCON_ERR_SIZE;
            }
        }
        _ => return FALCON_ERR_BADARG,
    }

    let n: usize = 1 << logn;
    let ptr = tmp.as_mut_ptr();

    // Decode private key into tmp: f, g, F, G, hm, then atmp.
    let f = unsafe { core::slice::from_raw_parts_mut(ptr as *mut i8, n) };
    let g = unsafe { core::slice::from_raw_parts_mut(ptr.add(n) as *mut i8, n) };
    let big_f = unsafe { core::slice::from_raw_parts_mut(ptr.add(2 * n) as *mut i8, n) };
    let big_g = unsafe { core::slice::from_raw_parts_mut(ptr.add(3 * n) as *mut i8, n) };

    let mut u: usize = 1;
    let v = codec::trim_i8_decode(
        f,
        logn,
        codec::MAX_FG_BITS[logn as usize] as u32,
        &privkey[u..],
    );
    if v == 0 {
        return FALCON_ERR_FORMAT;
    }
    u += v;
    let v = codec::trim_i8_decode(
        g,
        logn,
        codec::MAX_FG_BITS[logn as usize] as u32,
        &privkey[u..],
    );
    if v == 0 {
        return FALCON_ERR_FORMAT;
    }
    u += v;
    let v = codec::trim_i8_decode(
        big_f,
        logn,
        codec::MAX_FG_BITS_UPPER[logn as usize] as u32,
        &privkey[u..],
    );
    if v == 0 {
        return FALCON_ERR_FORMAT;
    }
    u += v;
    if u != privkey.len() {
        return FALCON_ERR_FORMAT;
    }

    // Complete the private key (recover G).
    let hm_off = 4 * n;

    let atmp_off = {
        let base = hm_off + 2 * n; // sv overlaps hm
        let addr = unsafe { ptr.add(base) as usize };
        let off = addr & 7;
        if off != 0 {
            base + 8 - off
        } else {
            base
        }
    };
    let atmp = unsafe { core::slice::from_raw_parts_mut(ptr.add(atmp_off), tmp.len() - atmp_off) };
    if !vrfy::complete_private(big_g, f, g, big_f, logn, atmp) {
        return FALCON_ERR_FORMAT;
    }

    // Hash message to a point.
    shake256_flip(hash_data);
    let sav_hash_data = hash_data.clone();

    // Sign loop (may need to retry for PADDED format).
    loop {
        *hash_data = sav_hash_data.clone();

        // Use separate scopes for hm (u16 view) and sv (i16 view) of the same
        // memory to avoid aliased &mut references (Stacked Borrows UB).
        {
            let hm = unsafe { core::slice::from_raw_parts_mut(ptr.add(hm_off) as *mut u16, n) };
            if sig_type == FALCON_SIG_CT {
                common::hash_to_point_ct(hash_data, hm, logn, atmp);
            } else {
                common::hash_to_point_vartime(hash_data, hm, logn);
            }
        }
        // hm is now dropped; safe to create sv over the same memory.
        let sv: &mut [i16] =
            unsafe { core::slice::from_raw_parts_mut(ptr.add(hm_off) as *mut i16, n) };
        // Re-borrow hm as *immutable* u16 slice for sign_dyn (no aliasing).
        let hm = unsafe { core::slice::from_raw_parts(ptr.add(hm_off) as *const u16, n) };

        let atmp_full =
            unsafe { core::slice::from_raw_parts_mut(ptr.add(atmp_off), tmp.len() - atmp_off) };
        sign::sign_dyn(sv, rng, f, g, big_f, big_g, hm, logn, atmp_full);

        // Encode signature.
        sig[1..41].copy_from_slice(&nonce[..40]);
        let u_sig: usize = 41;
        match sig_type {
            FALCON_SIG_COMPRESSED => {
                sig[0] = 0x30 + logn as u8;
                let v = codec::comp_encode(Some(&mut sig[u_sig..]), sv, logn);
                if v == 0 {
                    return FALCON_ERR_SIZE;
                }
                *sig_len = u_sig + v;
                return 0;
            }
            FALCON_SIG_PADDED => {
                sig[0] = 0x30 + logn as u8;
                let tu = falcon_sig_padded_size(logn);
                let v = codec::comp_encode(Some(&mut sig[u_sig..tu]), sv, logn);
                if v == 0 {
                    // Signature too large for padded format, retry.
                    continue;
                }
                if u_sig + v < tu {
                    for i in u_sig + v..tu {
                        sig[i] = 0;
                    }
                }
                *sig_len = tu;
                return 0;
            }
            FALCON_SIG_CT => {
                sig[0] = 0x50 + logn as u8;
                let v = codec::trim_i16_encode(
                    Some(&mut sig[u_sig..]),
                    sv,
                    logn,
                    codec::MAX_SIG_BITS[logn as usize] as u32,
                );
                if v == 0 {
                    return FALCON_ERR_SIZE;
                }
                *sig_len = u_sig + v;
                return 0;
            }
            _ => return FALCON_ERR_BADARG,
        }
    }
}

/// Finish a signature using an expanded key ("tree" mode).
pub fn falcon_sign_tree_finish(
    rng: &mut InnerShake256Context,
    sig: &mut [u8],
    sig_len: &mut usize,
    sig_type: i32,
    expanded_key: &[u8],
    hash_data: &mut InnerShake256Context,
    nonce: &[u8],
    tmp: &mut [u8],
) -> i32 {
    if expanded_key.is_empty() {
        return FALCON_ERR_FORMAT;
    }
    let logn = expanded_key[0] as u32;
    if logn < 1 || logn > 10 {
        return FALCON_ERR_FORMAT;
    }
    if tmp.len() < falcon_tmpsize_signtree(logn) {
        return FALCON_ERR_SIZE;
    }
    let es_len = *sig_len;
    if es_len < 41 {
        return FALCON_ERR_SIZE;
    }

    // Get expanded key pointer (aligned to 8 bytes).
    let ek_ptr = &expanded_key[1..];
    let ek_addr = ek_ptr.as_ptr() as usize;
    let ek_off = if ek_addr & 7 != 0 {
        8 - (ek_addr & 7)
    } else {
        0
    };
    let expkey: &[Fpr] = unsafe {
        let p = ek_ptr.as_ptr().add(ek_off) as *const Fpr;
        let len = (expanded_key.len() - 1 - ek_off) / core::mem::size_of::<Fpr>();
        core::slice::from_raw_parts(p, len)
    };

    match sig_type {
        FALCON_SIG_COMPRESSED => {}
        FALCON_SIG_PADDED => {
            if es_len < falcon_sig_padded_size(logn) {
                return FALCON_ERR_SIZE;
            }
        }
        FALCON_SIG_CT => {
            if es_len < falcon_sig_ct_size(logn) {
                return FALCON_ERR_SIZE;
            }
        }
        _ => return FALCON_ERR_BADARG,
    }

    let n: usize = 1 << logn;
    let ptr = tmp.as_mut_ptr();

    // Align hm/sv.
    let hm_addr = ptr as usize;
    let hm_off = if hm_addr & 1 != 0 { 1usize } else { 0usize };
    let atmp_off = {
        let base = hm_off + 2 * n;
        let addr = unsafe { ptr.add(base) as usize };
        let off = addr & 7;
        if off != 0 {
            base + 8 - off
        } else {
            base
        }
    };

    shake256_flip(hash_data);
    let sav_hash_data = hash_data.clone();

    loop {
        *hash_data = sav_hash_data.clone();

        // Use separate scopes for hm (u16 view) and sv (i16 view) of the same
        // memory to avoid aliased &mut references (Stacked Borrows UB).
        {
            let hm = unsafe { core::slice::from_raw_parts_mut(ptr.add(hm_off) as *mut u16, n) };
            if sig_type == FALCON_SIG_CT {
                let atmp = unsafe {
                    core::slice::from_raw_parts_mut(ptr.add(atmp_off), tmp.len() - atmp_off)
                };
                common::hash_to_point_ct(hash_data, hm, logn, atmp);
            } else {
                common::hash_to_point_vartime(hash_data, hm, logn);
            }
        }
        // hm is now dropped; safe to create sv over the same memory.
        let sv = unsafe { core::slice::from_raw_parts_mut(ptr.add(hm_off) as *mut i16, n) };
        // Re-borrow hm as *immutable* u16 slice for sign_tree (no aliasing).
        let hm = unsafe { core::slice::from_raw_parts(ptr.add(hm_off) as *const u16, n) };

        let atmp =
            unsafe { core::slice::from_raw_parts_mut(ptr.add(atmp_off), tmp.len() - atmp_off) };
        sign::sign_tree(sv, rng, expkey, hm, logn, atmp);

        sig[1..41].copy_from_slice(&nonce[..40]);
        let u_sig: usize = 41;
        match sig_type {
            FALCON_SIG_COMPRESSED => {
                sig[0] = 0x30 + logn as u8;
                let v = codec::comp_encode(Some(&mut sig[u_sig..]), sv, logn);
                if v == 0 {
                    return FALCON_ERR_SIZE;
                }
                *sig_len = u_sig + v;
                return 0;
            }
            FALCON_SIG_PADDED => {
                sig[0] = 0x30 + logn as u8;
                let tu = falcon_sig_padded_size(logn);
                let v = codec::comp_encode(Some(&mut sig[u_sig..tu]), sv, logn);
                if v == 0 {
                    continue;
                }
                if u_sig + v < tu {
                    for i in u_sig + v..tu {
                        sig[i] = 0;
                    }
                }
                *sig_len = tu;
                return 0;
            }
            FALCON_SIG_CT => {
                sig[0] = 0x50 + logn as u8;
                let v = codec::trim_i16_encode(
                    Some(&mut sig[u_sig..]),
                    sv,
                    logn,
                    codec::MAX_SIG_BITS[logn as usize] as u32,
                );
                if v == 0 {
                    return FALCON_ERR_SIZE;
                }
                *sig_len = u_sig + v;
                return 0;
            }
            _ => return FALCON_ERR_BADARG,
        }
    }
}

/// Sign data using the raw private key ("dynamic" mode).
pub fn falcon_sign_dyn(
    rng: &mut InnerShake256Context,
    sig: &mut [u8],
    sig_len: &mut usize,
    sig_type: i32,
    privkey: &[u8],
    data: &[u8],
    tmp: &mut [u8],
) -> i32 {
    let mut hd = InnerShake256Context::new();
    let mut nonce = [0u8; 40];
    let r = falcon_sign_start(rng, &mut nonce, &mut hd);
    if r != 0 {
        return r;
    }
    shake256_inject(&mut hd, data);
    falcon_sign_dyn_finish(rng, sig, sig_len, sig_type, privkey, &mut hd, &nonce, tmp)
}

/// Sign data using an expanded key ("tree" mode).
pub fn falcon_sign_tree(
    rng: &mut InnerShake256Context,
    sig: &mut [u8],
    sig_len: &mut usize,
    sig_type: i32,
    expanded_key: &[u8],
    data: &[u8],
    tmp: &mut [u8],
) -> i32 {
    let mut hd = InnerShake256Context::new();
    let mut nonce = [0u8; 40];
    let r = falcon_sign_start(rng, &mut nonce, &mut hd);
    if r != 0 {
        return r;
    }
    shake256_inject(&mut hd, data);
    falcon_sign_tree_finish(
        rng,
        sig,
        sig_len,
        sig_type,
        expanded_key,
        &mut hd,
        &nonce,
        tmp,
    )
}

/// Expand a private key for use with sign_tree.
pub fn falcon_expand_privkey(expanded_key: &mut [u8], privkey: &[u8], tmp: &mut [u8]) -> i32 {
    if privkey.is_empty() {
        return FALCON_ERR_FORMAT;
    }
    if privkey[0] & 0xF0 != 0x50 {
        return FALCON_ERR_FORMAT;
    }
    let logn = (privkey[0] & 0x0F) as u32;
    if logn < 1 || logn > 10 {
        return FALCON_ERR_FORMAT;
    }
    if privkey.len() != falcon_privkey_size(logn) {
        return FALCON_ERR_FORMAT;
    }
    if expanded_key.len() < falcon_expandedkey_size(logn) {
        return FALCON_ERR_SIZE;
    }
    if tmp.len() < falcon_tmpsize_expandpriv(logn) {
        return FALCON_ERR_SIZE;
    }

    let n: usize = 1 << logn;
    let ptr = tmp.as_mut_ptr();

    // Decode private key.
    let f = unsafe { core::slice::from_raw_parts_mut(ptr as *mut i8, n) };
    let g = unsafe { core::slice::from_raw_parts_mut(ptr.add(n) as *mut i8, n) };
    let big_f = unsafe { core::slice::from_raw_parts_mut(ptr.add(2 * n) as *mut i8, n) };
    let big_g = unsafe { core::slice::from_raw_parts_mut(ptr.add(3 * n) as *mut i8, n) };

    let mut u: usize = 1;
    let v = codec::trim_i8_decode(
        f,
        logn,
        codec::MAX_FG_BITS[logn as usize] as u32,
        &privkey[u..],
    );
    if v == 0 {
        return FALCON_ERR_FORMAT;
    }
    u += v;
    let v = codec::trim_i8_decode(
        g,
        logn,
        codec::MAX_FG_BITS[logn as usize] as u32,
        &privkey[u..],
    );
    if v == 0 {
        return FALCON_ERR_FORMAT;
    }
    u += v;
    let v = codec::trim_i8_decode(
        big_f,
        logn,
        codec::MAX_FG_BITS_UPPER[logn as usize] as u32,
        &privkey[u..],
    );
    if v == 0 {
        return FALCON_ERR_FORMAT;
    }
    u += v;
    if u != privkey.len() {
        return FALCON_ERR_FORMAT;
    }

    // Complete private key (recover G).
    let atmp_off = {
        let base = 4 * n;
        let addr = unsafe { ptr.add(base) as usize };
        let off = addr & 7;
        if off != 0 {
            base + 8 - off
        } else {
            base
        }
    };
    let atmp = unsafe { core::slice::from_raw_parts_mut(ptr.add(atmp_off), tmp.len() - atmp_off) };
    if !vrfy::complete_private(big_g, f, g, big_f, logn, atmp) {
        return FALCON_ERR_FORMAT;
    }

    // Expand key.
    expanded_key[0] = logn as u8;
    let ek_addr = expanded_key[1..].as_ptr() as usize;
    let ek_off = if ek_addr & 7 != 0 {
        8 - (ek_addr & 7)
    } else {
        0
    };
    let expkey: &mut [Fpr] = unsafe {
        let p = expanded_key[1..].as_mut_ptr().add(ek_off) as *mut Fpr;
        let len = (expanded_key.len() - 1 - ek_off) / core::mem::size_of::<Fpr>();
        core::slice::from_raw_parts_mut(p, len)
    };

    let atmp2 = unsafe { core::slice::from_raw_parts_mut(ptr.add(atmp_off), tmp.len() - atmp_off) };
    sign::expand_privkey(expkey, f, g, big_f, big_g, logn, atmp2);
    0
}

// ======================================================================
// Signature verification
// ======================================================================

/// Start a streamed verification: extract nonce from signature and init hash.
pub fn falcon_verify_start(hash_data: &mut InnerShake256Context, sig: &[u8]) -> i32 {
    if sig.len() < 41 {
        return FALCON_ERR_FORMAT;
    }
    shake256_init(hash_data);
    shake256_inject(hash_data, &sig[1..41]);
    0
}

/// Finish a streamed verification.
pub fn falcon_verify_finish(
    sig: &[u8],
    sig_type: i32,
    pubkey: &[u8],
    hash_data: &mut InnerShake256Context,
    tmp: &mut [u8],
) -> i32 {
    if sig.len() < 41 || pubkey.is_empty() {
        return FALCON_ERR_FORMAT;
    }
    if pubkey[0] & 0xF0 != 0x00 {
        return FALCON_ERR_FORMAT;
    }
    let logn = (pubkey[0] & 0x0F) as u32;
    if logn < 1 || logn > 10 {
        return FALCON_ERR_FORMAT;
    }
    if sig[0] & 0x0F != logn as u8 {
        return FALCON_ERR_BADSIG;
    }

    let mut ct = false;
    match sig_type {
        0 => match sig[0] & 0xF0 {
            0x30 => {}
            0x50 => {
                if sig.len() != falcon_sig_ct_size(logn) {
                    return FALCON_ERR_FORMAT;
                }
                ct = true;
            }
            _ => return FALCON_ERR_BADSIG,
        },
        FALCON_SIG_COMPRESSED => {
            if sig[0] & 0xF0 != 0x30 {
                return FALCON_ERR_FORMAT;
            }
        }
        FALCON_SIG_PADDED => {
            if sig[0] & 0xF0 != 0x30 {
                return FALCON_ERR_FORMAT;
            }
            if sig.len() != falcon_sig_padded_size(logn) {
                return FALCON_ERR_FORMAT;
            }
        }
        FALCON_SIG_CT => {
            if sig[0] & 0xF0 != 0x50 {
                return FALCON_ERR_FORMAT;
            }
            if sig.len() != falcon_sig_ct_size(logn) {
                return FALCON_ERR_FORMAT;
            }
            ct = true;
        }
        _ => return FALCON_ERR_BADARG,
    }

    if pubkey.len() != falcon_pubkey_size(logn) {
        return FALCON_ERR_FORMAT;
    }
    if tmp.len() < falcon_tmpsize_verify(logn) {
        return FALCON_ERR_SIZE;
    }

    let n: usize = 1 << logn;
    let ptr = tmp.as_mut_ptr();

    // Align for u16.
    let base_off = if (ptr as usize) & 1 != 0 {
        1usize
    } else {
        0usize
    };
    let h = unsafe { core::slice::from_raw_parts_mut(ptr.add(base_off) as *mut u16, n) };
    let hm = unsafe { core::slice::from_raw_parts_mut(ptr.add(base_off + 2 * n) as *mut u16, n) };
    let sv = unsafe { core::slice::from_raw_parts_mut(ptr.add(base_off + 4 * n) as *mut i16, n) };
    let atmp = unsafe {
        let off = base_off + 6 * n;
        core::slice::from_raw_parts_mut(ptr.add(off), tmp.len() - off)
    };

    // Decode public key.
    if codec::modq_decode(h, logn, &pubkey[1..]) != pubkey.len() - 1 {
        return FALCON_ERR_FORMAT;
    }

    // Decode signature.
    let u_sig: usize = 41;
    let v = if ct {
        codec::trim_i16_decode(
            sv,
            logn,
            codec::MAX_SIG_BITS[logn as usize] as u32,
            &sig[u_sig..],
        )
    } else {
        codec::comp_decode(sv, logn, &sig[u_sig..])
    };
    if v == 0 {
        return FALCON_ERR_FORMAT;
    }

    if u_sig + v != sig.len() {
        // Extra zero bytes tolerated only for padded format.
        if (sig_type == 0 && sig.len() == falcon_sig_padded_size(logn))
            || sig_type == FALCON_SIG_PADDED
        {
            let mut pos = u_sig + v;
            while pos < sig.len() {
                if sig[pos] != 0 {
                    return FALCON_ERR_FORMAT;
                }
                pos += 1;
            }
        } else {
            return FALCON_ERR_FORMAT;
        }
    }

    // Hash message to point.
    shake256_flip(hash_data);
    if ct {
        common::hash_to_point_ct(hash_data, hm, logn, atmp);
    } else {
        common::hash_to_point_vartime(hash_data, hm, logn);
    }

    // Verify signature.
    vrfy::to_ntt_monty(h, logn);
    if !vrfy::verify_raw(hm, sv, h, logn, atmp) {
        return FALCON_ERR_BADSIG;
    }
    0
}

/// Verify a signature against a public key and data.
pub fn falcon_verify(sig: &[u8], sig_type: i32, pubkey: &[u8], data: &[u8], tmp: &mut [u8]) -> i32 {
    let mut hd = InnerShake256Context::new();
    let r = falcon_verify_start(&mut hd, sig);
    if r < 0 {
        return r;
    }
    shake256_inject(&mut hd, data);
    falcon_verify_finish(sig, sig_type, pubkey, &mut hd, tmp)
}