Skip to main content

tide_fn_dsa/
lib.rs

1#![no_std]
2#![allow(non_snake_case)]
3#![allow(non_upper_case_globals)]
4
5//! # FN-DSA implementation
6//!
7//! This crate is really a wrapper for the [tide-fn-dsa-kgen],
8//! [tide-fn-dsa-sign] and [tide-fn-dsa-vrfy] crates that implement the
9//! various elements of the
10//! FN-DSA signature algorithm. All the relevant types, functions and
11//! constants are re-exported here. Users of this implementation only
12//! need to import this crate; the division into sub-crates is meant to
13//! help with specialized situations where code footprint reduction is
14//! important (typically, embedded systems that only need to verify
15//! signatures, but not generate keys or signatures).
16//!
17//! ## Tidecoin compatibility
18//!
19//! Use `sign_falcon` / `verify_falcon` (on the corresponding signing or
20//! verifying type) with an explicit `FalconProfile`. `TidecoinStrict`
21//! requires canonical, exactly consumed compressed signatures for degrees
22//! 512 and 1024. `TidecoinLegacyFalcon512` preserves historical Falcon-512
23//! encodings and the exclusive legacy norm bound. `PqClean` retains its
24//! padded-format compatibility. The generic API described below is separate.
25//! The raw Falcon API consumes the supplied message bytes without adding
26//! FN-DSA context/domain metadata or a transaction sighash byte.
27//!
28//! ## WARNING
29//!
30//! **The generic FN-DSA API is experimental and is not claimed to conform
31//! to a final FIPS 206 standard. Its encodings, pre-hashing and domain
32//! separation may evolve. The explicit Tidecoin profiles and deterministic
33//! PQHD key mapping have a separate compatibility contract and must not be
34//! changed silently to follow future FN-DSA revisions.**
35//!
36//! ## Implementation notes
37//!
38//! The whole code is written in pure Rust and is compatible with `no_std`.
39//! Production dependencies include `rand_core`, `zeroize`, `sha2` and
40//! `cpufeatures`; unit tests use additional crates.
41//!
42//! On x86 (both 32-bit and 64-bit), AVX2 opcodes are automatically used
43//! for faster operations if their support is detected at runtime. No
44//! special compilation flag nor extra runtime check is needed for that;
45//! the compiled code remains compatible with plain non-AVX2-aware CPUs.
46//!
47//! On 64-bit x86 (`x86_64`) and ARMv8 (`aarch64`, `arm64ec`), native
48//! (hardware) floating-point support is used, since in both these cases
49//! the architecture ABI mandates a strict IEEE-754 unit and can more or
50//! less be assumed to operate in constant-time for non-exceptional
51//! inputs. This makes signature generation much faster on these
52//! platforms (on `x86_64`, this furthermore combines with AVX2
53//! optimizations if available in the current CPU). On other platforms, a
54//! portable emulation of floating-point operations is used (this
55//! emulation makes a best effort at operating in constant-time, though
56//! some recent compiler optimizations might introduce variable-time
57//! operations).
58//!
59//! Numeric behavior differs by subsystem:
60//!
61//!  - classic/native key generation uses integer and fixed-point arithmetic
62//!  - PQClean/Tidecoin deterministic key generation uses an integer-only
63//!    emulation of Falcon FLR/FFT semantics
64//!  - signature verification uses integer arithmetic
65//!
66//! The key pair generation implementation is a translation of the
67//! [ntrugen] code, which is faster than the originally submitted Falcon
68//! code. The signature generation engine follows the steps of the
69//! `sign_dyn` operations from the original [falcon] code (indeed, an
70//! internal unit tests checks that the sampler returns the same values
71//! for the same inputs). Achieved performance on `x86_64` is very close
72//! to that offered by the C code (signature verification performance is
73//! even better).
74//!
75//! ## Example usage
76//!
77//! For deterministic Falcon key derivation, there are now two explicit
78//! seeded key-generation families on the Falcon key generators:
79//!
80//!  - `keygen_from_seed_native()` uses the upstream `fn-dsa` / `ntrugen`
81//!    seeded Falcon path.
82//!  - `keygen_from_seed_pqclean()` and `keygen_from_stream_key_tidecoin()`
83//!    use PQClean/Tidecoin-compatible seeded Falcon key generation.
84//!
85//! These APIs are intentionally separate because the same 48-byte seed
86//! does not map to the same Falcon key pair in the two families.
87//! The PQClean/Tidecoin APIs are compatibility-oriented deterministic
88//! key-derivation entrypoints; they preserve the original seeded Falcon
89//! mapping and rejection behavior, but they do not imply that PQClean code
90//! is used for signing or verification.
91//!
92//! ```no_run
93//! use tide_fn_dsa::{
94//!     SIGN_KEY_SIZE_512, VRFY_KEY_SIZE_512, SIGNATURE_SIZE_512, FN_DSA_LOGN_512,
95//!     KeyPairGenerator, KeyPairGeneratorStandard,
96//!     SigningKey, SigningKeyStandard,
97//!     VerifyingKey, VerifyingKeyStandard,
98//!     DOMAIN_NONE, HASH_ID_RAW, CryptoRng, RngCore, RngError,
99//! };
100//! #
101//! # struct DemoRng(u64);
102//! # impl CryptoRng for DemoRng {}
103//! # impl RngCore for DemoRng {
104//! #     fn next_u32(&mut self) -> u32 {
105//! #         let x = self.0 as u32;
106//! #         self.0 = self.0.wrapping_mul(6364136223846793005).wrapping_add(1);
107//! #         x
108//! #     }
109//! #     fn next_u64(&mut self) -> u64 {
110//! #         let x = self.0;
111//! #         self.0 = self.0.wrapping_mul(6364136223846793005).wrapping_add(1);
112//! #         x
113//! #     }
114//! #     fn fill_bytes(&mut self, dest: &mut [u8]) {
115//! #         for chunk in dest.chunks_mut(8) {
116//! #             let bytes = self.next_u64().to_le_bytes();
117//! #             let len = chunk.len();
118//! #             chunk.copy_from_slice(&bytes[..len]);
119//! #         }
120//! #     }
121//! #     fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), RngError> {
122//! #         self.fill_bytes(dest);
123//! #         Ok(())
124//! #     }
125//! # }
126//!
127//! // Generate key pair.
128//! let mut rng = DemoRng(1);
129//! let mut kg = KeyPairGeneratorStandard::default();
130//! let mut sign_key = [0u8; SIGN_KEY_SIZE_512];
131//! let mut vrfy_key = [0u8; VRFY_KEY_SIZE_512];
132//! kg.keygen(FN_DSA_LOGN_512, &mut rng, &mut sign_key, &mut vrfy_key)
133//!     .unwrap();
134//!
135//! // Sign a message with the signing key.
136//! let mut sk = SigningKeyStandard::decode(&sign_key)
137//!     .expect("valid signing key bytes");
138//! let mut sig = [0u8; SIGNATURE_SIZE_512];
139//! sk.sign(&mut rng, &DOMAIN_NONE, &HASH_ID_RAW, b"message", &mut sig)
140//!     .unwrap();
141//!
142//! // Verify a signature with the verifying key.
143//! match VerifyingKeyStandard::decode(&vrfy_key) {
144//!     Some(vk) => {
145//!         if vk.verify(&sig, &DOMAIN_NONE, &HASH_ID_RAW, b"message") {
146//!             // signature is valid
147//!         } else {
148//!             // signature is not valid
149//!         }
150//!     }
151//!     _ => {
152//!         // could not decode verifying key
153//!     }
154//! }
155//! ```
156//!
157//! Falcon compatibility APIs are also exposed explicitly:
158//!
159//! ```no_run
160//! use tide_fn_dsa::{
161//!     SIGN_KEY_SIZE_512, VRFY_KEY_SIZE_512, TIDECOIN_LEGACY_FALCON512_SIG_MAX,
162//!     FN_DSA_LOGN_512, FalconProfile,
163//!     KeyPairGenerator, KeyPairGeneratorStandard,
164//!     SigningKey, SigningKeyStandard,
165//!     VerifyingKey, VerifyingKeyStandard,
166//!     CryptoRng, RngCore, RngError,
167//! };
168//! #
169//! # struct DemoRng(u64);
170//! # impl CryptoRng for DemoRng {}
171//! # impl RngCore for DemoRng {
172//! #     fn next_u32(&mut self) -> u32 {
173//! #         let x = self.0 as u32;
174//! #         self.0 = self.0.wrapping_mul(6364136223846793005).wrapping_add(1);
175//! #         x
176//! #     }
177//! #     fn next_u64(&mut self) -> u64 {
178//! #         let x = self.0;
179//! #         self.0 = self.0.wrapping_mul(6364136223846793005).wrapping_add(1);
180//! #         x
181//! #     }
182//! #     fn fill_bytes(&mut self, dest: &mut [u8]) {
183//! #         for chunk in dest.chunks_mut(8) {
184//! #             let bytes = self.next_u64().to_le_bytes();
185//! #             let len = chunk.len();
186//! #             chunk.copy_from_slice(&bytes[..len]);
187//! #         }
188//! #     }
189//! #     fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), RngError> {
190//! #         self.fill_bytes(dest);
191//! #         Ok(())
192//! #     }
193//! # }
194//!
195//! let mut rng = DemoRng(7);
196//! let mut kg = KeyPairGeneratorStandard::default();
197//! let mut sign_key = [0u8; SIGN_KEY_SIZE_512];
198//! let mut vrfy_key = [0u8; VRFY_KEY_SIZE_512];
199//! kg.keygen(FN_DSA_LOGN_512, &mut rng, &mut sign_key, &mut vrfy_key)
200//!     .unwrap();
201//!
202//! let mut sk = SigningKeyStandard::decode(&sign_key).unwrap();
203//! let vk = VerifyingKeyStandard::decode(&vrfy_key).unwrap();
204//! let mut sig = [0u8; TIDECOIN_LEGACY_FALCON512_SIG_MAX];
205//! let sig_len = sk.sign_falcon(
206//!     &mut rng,
207//!     FalconProfile::TidecoinLegacyFalcon512,
208//!     b"message",
209//!     &mut sig,
210//! ).unwrap();
211//! assert!(vk.verify_falcon(
212//!     FalconProfile::TidecoinLegacyFalcon512,
213//!     &sig[..sig_len],
214//!     b"message",
215//! ));
216//! ```
217//!
218//! [tide-fn-dsa-kgen]: https://crates.io/crates/tide-fn-dsa-kgen
219//! [tide-fn-dsa-sign]: https://crates.io/crates/tide-fn-dsa-sign
220//! [tide-fn-dsa-vrfy]: https://crates.io/crates/tide-fn-dsa-vrfy
221//! [falcon]: https://falcon-sign.info/
222//! [ntrugen]: https://eprint.iacr.org/2023/290
223//! [rand_core]: https://crates.io/crates/rand_core
224//! [zeroize]: https://crates.io/crates/zeroize
225
226pub use tide_fn_dsa_comm::shake::{
227    ShakeError, SHA3_224, SHA3_256, SHA3_384, SHA3_512, SHAKE, SHAKE128, SHAKE256,
228};
229pub use tide_fn_dsa_comm::{
230    hash_to_point, hash_to_point_falcon, sign_key_size, signature_size, vrfy_key_size, CryptoRng,
231    DomainContext, DomainContextError, FalconProfile, HashIdentifier, HashIdentifierError,
232    HashToPointError, LogNError, RngCore, RngError, DOMAIN_NONE, FALCON_NONCE_LEN,
233    FN_DSA_LOGN_1024, FN_DSA_LOGN_512, HASH_ID_RAW, HASH_ID_SHA256, HASH_ID_SHA384,
234    HASH_ID_SHA3_256, HASH_ID_SHA3_384, HASH_ID_SHA3_512, HASH_ID_SHA512, HASH_ID_SHA512_256,
235    HASH_ID_SHAKE128, HASH_ID_SHAKE256, SIGNATURE_SIZE_1024, SIGNATURE_SIZE_512,
236    SIGN_KEY_SIZE_1024, SIGN_KEY_SIZE_512, TIDECOIN_LEGACY_FALCON512_SIG_BODY_MAX,
237    TIDECOIN_LEGACY_FALCON512_SIG_MAX, VRFY_KEY_SIZE_1024, VRFY_KEY_SIZE_512,
238};
239pub use tide_fn_dsa_kgen::{
240    DeterministicKeyGenError, FALCON_KEYGEN_SEED_SIZE, PQHD_KEYGEN_STREAM_SIZE,
241    PQHD_MAX_DETERMINISTIC_ATTEMPTS,
242};
243pub use tide_fn_dsa_kgen::{
244    KeyGenError, KeyPairGenerator, KeyPairGenerator1024, KeyPairGenerator512,
245    KeyPairGeneratorStandard, KeyPairGeneratorWeak,
246};
247pub use tide_fn_dsa_sign::{
248    SigningKey, SigningKey1024, SigningKey512, SigningKeyError, SigningKeyStandard, SigningKeyWeak,
249};
250pub use tide_fn_dsa_vrfy::{
251    VerifyingKey, VerifyingKey1024, VerifyingKey512, VerifyingKeyStandard, VerifyingKeyWeak,
252};
253
254/// Format-only classifier for historical Tidecoin Falcon-512 encodings.
255pub use tide_fn_dsa_vrfy::is_noncanonical_legacy_falcon512;
256
257#[cfg(test)]
258mod tests {
259    use super::*;
260
261    // We use two fake RNGs for tests; they have been designed to allow
262    // reproducing vectors in the C implementation:
263    //
264    //  - FakeRng1: this is simply SHAKE256 over the provided seed
265    //
266    //  - FakeRng2: for the given seed, 96-byte blocks are obtained, each
267    //    as SHAKE256(seed || ctr), with ctr being a counter that starts at
268    //    0, and is encoded over 4 bytes (little-endian). The RNG output
269    //    consists of the concatenation of these 96-byte blocks.
270
271    struct FakeRng1(SHAKE256);
272    impl FakeRng1 {
273        fn new(seed: &[u8]) -> Self {
274            let mut sh = SHAKE256::new();
275            sh.inject(seed).unwrap();
276            sh.flip().unwrap();
277            Self(sh)
278        }
279    }
280    impl CryptoRng for FakeRng1 {}
281    impl RngCore for FakeRng1 {
282        fn next_u32(&mut self) -> u32 {
283            unimplemented!();
284        }
285        fn next_u64(&mut self) -> u64 {
286            unimplemented!();
287        }
288        fn fill_bytes(&mut self, dest: &mut [u8]) {
289            self.0.extract(dest).unwrap();
290        }
291        fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), RngError> {
292            self.fill_bytes(dest);
293            Ok(())
294        }
295    }
296
297    struct FakeRng2 {
298        sh: SHAKE256,
299        buf: [u8; 96],
300        ptr: usize,
301        ctr: u32,
302    }
303    impl FakeRng2 {
304        fn new(seed: &[u8]) -> Self {
305            let mut sh = SHAKE256::new();
306            sh.inject(seed).unwrap();
307            Self {
308                sh,
309                buf: [0u8; 96],
310                ptr: 96,
311                ctr: 0,
312            }
313        }
314    }
315    impl CryptoRng for FakeRng2 {}
316    impl RngCore for FakeRng2 {
317        fn next_u32(&mut self) -> u32 {
318            unimplemented!();
319        }
320        fn next_u64(&mut self) -> u64 {
321            unimplemented!();
322        }
323        fn fill_bytes(&mut self, dest: &mut [u8]) {
324            let mut j = 0;
325            let mut ptr = self.ptr;
326            while j < dest.len() {
327                if ptr == self.buf.len() {
328                    let mut sh = self.sh;
329                    sh.inject(&self.ctr.to_le_bytes()).unwrap();
330                    sh.flip().unwrap();
331                    sh.extract(&mut self.buf).unwrap();
332                    self.ctr += 1;
333                    ptr = 0;
334                }
335                let clen = core::cmp::min(dest.len() - j, self.buf.len() - ptr);
336                dest[j..j + clen].copy_from_slice(&self.buf[ptr..ptr + clen]);
337                ptr += clen;
338                j += clen;
339            }
340            self.ptr = ptr;
341        }
342        fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), RngError> {
343            self.fill_bytes(dest);
344            Ok(())
345        }
346    }
347
348    fn self_test_inner<KG: KeyPairGenerator, SK: SigningKey, VK: VerifyingKey>(logn: u32) {
349        let mut kg = KG::default();
350        let mut sk_buf = [0u8; SIGN_KEY_SIZE_1024];
351        let mut vk_buf = [0u8; VRFY_KEY_SIZE_1024];
352        let mut vk2_buf = [0u8; VRFY_KEY_SIZE_1024];
353        let mut sig_buf = [0u8; SIGNATURE_SIZE_1024];
354        let sk_e = &mut sk_buf[..sign_key_size(logn).unwrap()];
355        let vk_e = &mut vk_buf[..vrfy_key_size(logn).unwrap()];
356        let vk2_e = &mut vk2_buf[..vrfy_key_size(logn).unwrap()];
357        let sig = &mut sig_buf[..signature_size(logn).unwrap()];
358        for t in 0..2 {
359            // We use a reproducible source of random bytes.
360            let mut rng = FakeRng1::new(&[logn as u8, t]);
361
362            // Generate key pair.
363            kg.keygen(logn, &mut rng, sk_e, vk_e).unwrap();
364
365            // Decode private key and check that it matches the public key.
366            let mut sk = SK::decode(sk_e).unwrap();
367            assert!(sk.get_logn() == logn);
368            sk.to_verifying_key(vk2_e).unwrap();
369            assert!(vk_e == vk2_e);
370
371            // Sign a test message.
372            sk.sign(&mut rng, &DOMAIN_NONE, &HASH_ID_RAW, &b"test1"[..], sig)
373                .unwrap();
374
375            // Verify the signature. Check that modifying the context,
376            // message or signature results in a verification failure.
377            let vk = VK::decode(vk_e).unwrap();
378            assert!(vk.verify(sig, &DOMAIN_NONE, &HASH_ID_RAW, &b"test1"[..]));
379            assert!(!vk.verify(sig, &DOMAIN_NONE, &HASH_ID_RAW, &b"test2"[..]));
380            assert!(!vk.verify(
381                sig,
382                &DomainContext::new(b"other").unwrap(),
383                &HASH_ID_RAW,
384                &b"test1"[..]
385            ));
386            sig[sig.len() >> 1] ^= 0x40;
387            assert!(!vk.verify(sig, &DOMAIN_NONE, &HASH_ID_RAW, &b"test1"[..]));
388        }
389    }
390
391    #[test]
392    fn self_test() {
393        for logn in 9..=10 {
394            self_test_inner::<KeyPairGeneratorStandard, SigningKeyStandard, VerifyingKeyStandard>(
395                logn,
396            );
397        }
398        for logn in 2..=8 {
399            self_test_inner::<KeyPairGeneratorWeak, SigningKeyWeak, VerifyingKeyWeak>(logn);
400        }
401    }
402
403    // Test vectors:
404    // KAT[j] contains 10 vectors for logn = j + 2.
405    // For test vector KAT[j][n]:
406    //    Let seed1 = 0x00 || logn || n
407    //    Let seed2 = 0x01 || logn || n
408    //    (logn over one byte, n over 4 bytes, little-endian)
409    //    seed1 is used with FakeRng1 in keygen.
410    //    seed2 is used with FakeRng2 for signing.
411    //    A key pair (sk, vk) is generated. A message is signed:
412    //        domain context: "domain" (6 bytes)
413    //        message: "message" (7 bytes)
414    //        if n is odd, message is pre-hashed with SHA3-256; raw otherwise
415    //    KAT[j][n] is SHA3-256(sk || vk || sig)
416    #[cfg(feature = "shake256x4")]
417    const KAT: [[&str; 10]; 9] = [
418        [
419            "feeb4bde204cb40cbe06c7e5834abdfcec199219197e603883dbe47028bbfbf2",
420            "4f7d1867e9e02ee571a45b6d6d24b8f02b68b2e59441d1e341d06bbf36bf668e",
421            "8bd38088f833b66d1a5a4319e48c0efd2b1578fd7fc3bb7d20e167f4cd52e8de",
422            "24e37763e19942bb1acc6b5e5a4867170d07741fe055e8e3c2411f1b754bbd1b",
423            "9679a55739e76b66a475fe94053606bf07b930d47cc05377444f19f2c85ef2e6",
424            "3435ac75ffeb8c72df5e5d2c8619ef2a991de0fe9864014306a9af16630b41f3",
425            "8913b2791a76a746242160a800737459dc6457d1420317d7b21043ae286c5798",
426            "56413a0307b574b7bff2b6f9f9b59e346f6ab16c2c75fe1c64949a025dc40534",
427            "570e6fe189c45ab50e039eaa0ac3c5f2f50efbffa08e006368d3364e4d49f7fd",
428            "60b307e72b295b3fb13bd7c2f5926b521c34fbbd4d9ee3cdfe89eed9ffb2d2af",
429        ],
430        [
431            "956766887db48fd1f9cac47a93a12c9e55de6e47006457eceee523d3566f3dec",
432            "9f41d30fad1bee288928b1f78a376a46dc06a0edc869bdb6cce0acc36583e92f",
433            "8389ba7095343bd222c9818da07ac7e66b73dfdeafb6cdc10377242874c27ece",
434            "7fd7ba114d952c9afe2c1dd4ee30e644b2e6caed13aed4e7e969260962a25c58",
435            "5a65e67783352ade4a5cfc7d0a48849fecbdbefffdcd8d25d425c3f013f9f019",
436            "f3044d077d30621ac7735fb3f95c35a58e15a3aa1c391467b6c33e05d8240c28",
437            "cf012db9b469ada96be790b8050b68d531fbdd2f4940d0ac07b8ffc02310f8e3",
438            "1e1c251797a4b27f4849ab34dfb9b21b3a84a52c4b0c11b93cf07305da26134f",
439            "6e051f873582f6d94c93b335f059588acb00722a40e09b310a0c00894fdf05af",
440            "d025acba6daf2b1de7d82d423b6eecb946e98cd7f7125f150e302ac8fccc3af2",
441        ],
442        [
443            "7e2561ddd8664383b2e03bcb4da2409d4c43676ed021dee59766e72890a4509b",
444            "7f284169006a71440cc27cace9cfeab56440d357ee42b47609e1b76513281b21",
445            "46c05f015b609826c310a098a2105a0e94ad271313031b307a5ff6af09b14de2",
446            "ed689cbfd26b8d3f4785d2622df343ef6ef11bf7d883d41f570416a632213fe1",
447            "3b8c717aa4b2c5ea95b8df2af003e97d982e20230058ccaaa3d465a3239b05ca",
448            "71e64b14011712731f7e02dee789d8c76cbc0d5f16c983b044067b30d47971d1",
449            "3bc7443e28014cd78cb31eb7e5283aa9e23827d21b1317a8fe4fbb031cedcac5",
450            "4ffe1c59cfe27ecbf233710bdc535a4a332c68e741a0a9a1b684d773cfc031f3",
451            "49adb0cb6ed7af916adb4f213016d862a88ab284f9a61fc11e12a1828540b1b4",
452            "27d2d2558117e4861207851dcc5f51322fb5e21cad7ace06390f5132f4c0ec17",
453        ],
454        [
455            "97517f9cfe9641fbb06b08afa09be14096b13573960f6790ba1119eb01a8f723",
456            "66c8669fe31f434582a465705dafea2a09c4acaf5c2c9d5975b4ec72d556c80b",
457            "7207b9f036d9b7a40f5d3647f03fb4ebc373719f240791cd65f9f35fc471ef35",
458            "bb9f9ebe61c5db1d72ebfaf2d699cc4c70e4c899f896b4f331fae004cd7a9b59",
459            "90484e94c5bb5c6c2f5c48bfeec4ce15b4935d09bc55b1fdaa6ad71e3e03e194",
460            "ea8822e989b8bf3484eaac010d77275d7d953cd0d16a51dbde9dc43ccf4bed0a",
461            "afb52381c81b8b5fa7b48bbd8262e450bc69161e6c31112678a3743b5efbf58b",
462            "96be92ccc265fa68564593baa4fe4f3cbac2f4a0c85c81f80ca28b2f3a3c099b",
463            "05af0cb90b923f778c7f88b0e6747861da0a0f73481fe2b1587b16417ed7101f",
464            "fc3201c8a5763e6b9919c54044aa7c302dc11344ab629917ef14680d3dce82fa",
465        ],
466        [
467            "dc6efcd8382f2ec32a5d0048ccfecd7d0aa2804ed31f9ca7b3b7fe80a1f278d6",
468            "8b96fe42791a4ddd3f426ea35d278830d0d688a2259355e568e63a88afe8093a",
469            "6fd98e52e33c89a20dda23f4f25744350fd69f3fec640c06590866b004f3799d",
470            "b0696877b0de7a9b82b74038b4be03d8a4669de8aa39845c36bc969ec8cdd4a5",
471            "e0047e262bfd3df4874587d3966d12191835d27a84935d4f28ee6551c4d56db9",
472            "3b61bc4d990adf23afbef5e0366d4d3328f776e74173792de0ebb1ac9d87412b",
473            "358eeb3cfa720339970489378e1418cb618f927b47065e580f8c56b74f92f46b",
474            "4384664a9f6ef03ddb96b77e09349ce951480ac0e0666e9f4236b213c69cfb2f",
475            "8cd018e4d9add2fd5f12dc3015e9ff5ef6195154d4c09f4dfa8436681899db6d",
476            "37e523a85668c4ea1ea59eb44e44bb1872d0ce8ec9571e329a1b2a9a60eacd05",
477        ],
478        [
479            "22195e02f65e0906245eaedd12bedd89a89afcb68c62d27ded954a72fdfa1547",
480            "dc2051d21719a1276c7a1f860e334c632ea0b1b15ff5203aac6fb93fe11ee123",
481            "adc6b4de01547c5d6b382534fbc715fe7c434cd5c213f7bfd2d1d5056e7618a6",
482            "191a5490b1a8fb166e3337ffd15b2b9d99dc31ebb07f69c8fa527e5e4878edf6",
483            "60592b75cfb9bc459b99ada2e6b357b8b2a0796316a97efdf7d42d49ac8a20ac",
484            "ff9660ef3e4f918ed588bc315e5f295421e0e8ff88d3c787d8c587396ab8e881",
485            "2dd9b7c1632f64ad88da054db0488324d00f4ef550bddbd5961b963400f824b9",
486            "c3d68100de903315d7a7ae47ce3d33ba9da7f9d6a27d563ccce997771a13974f",
487            "b793a6fec199c60455ea22cf3b9cf0987a3c1157b4729f522498fdfe1e8f6043",
488            "23f66127ac55cfab218a9a4b199fa42bc64056bb040ae653e90e63cc882eff60",
489        ],
490        [
491            "0e19693ced586519efd7ff4cb45b8013d2f300b60eba2d291599d366bb03f1d9",
492            "30c926ee6237d407cff189c2baeb3171872aebc461b919484cf30d93250fcde0",
493            "3d4268db567841caa0e360e2d6c79c354b659f521509243381b494b4eec2b4af",
494            "0c504032ffbdd2f2b26cb8d0c478fbf645e2fe3bdacd1fe25a5d15fd3830edc0",
495            "3c9bcb09a3b6b54264068bf1df32051065f1099d4fa0b90ffb14e5391e7af564",
496            "2c2efd441d9733dc3c14b1e62444856d9ffe12e4ef5104dd30e891c2c16237f0",
497            "5b87a753c041dc60f938b0971e066d6feb6055f1a021db3036ca64741280a116",
498            "f38f57b7cda123e36a03ebb7c0bb196a86dda4abd66a038cd054f7a4bd61e50a",
499            "755a29fb4dcf7808399f501fde4c0e23d11b9face58c9f6681f1c636b2256989",
500            "af091e60104821510b28599068fa84fd814af62d978f6830e7fa2fc51fedcf9b",
501        ],
502        [
503            "a32f07baf6b7ff6bc7c3c4f8c638871ff8c4803b0e54bedb9363f5672011077b",
504            "1794cfad199c20879d1ffe10ce263334095e51f0ed191ed74e4cba635e233d80",
505            "d16188abb5502eae81e6e03750123e156d8ed7dfa830a0c879560b383a5dc53a",
506            "14c03d690bf39bed73ac024a2b94adc1ff276d0c11e35d3455b9ea13c361b96c",
507            "c3bdbab8e434c5264c1d6fb523777d5bab1e23a1c292066e3cb731742230b042",
508            "d315c931fde38bdaeb83e6378d322f33ec9a36915ea5ed05e84ec3debddafa55",
509            "3587e5d75e2f0de5e2116c3a136d1a559e58ffd4a10328060ce9a430e47bd87c",
510            "b622711852cdc9893aec144ed635d2ae775778c6f4152e106b7b6b2842c8055d",
511            "32b3c2ed31f11795dde312b0574164dc4d00712f4736d1c5142a49cab4261ed4",
512            "e2bd2350de9bdab72d3a517251217d8fdbd7ea6e386ad2ff1da19c7c2111bcb2",
513        ],
514        [
515            "16ef63f9dc51b66565bb05ac525f3668fa48186b973a95599e0c963cfd6a4297",
516            "f62ac74368b2f8b80b6e12f13e026c9ba493c59b9eb2225a2626dc773e257dba",
517            "3f4de163f9a44137c52b0d9d6042a236fb8a05f9bd6617e12fbbd32bb0f2120c",
518            "77d567ae787dae191cdcf406f5e6a88e16b6a3729b814ac49f7d182b6cd624d8",
519            "d19a28ba50359df8d119fa4557116d45dffec6f422ae9aa563186270a6a36ee0",
520            "cbda1bcc23e33ff63864cbb44db9e618c76214a91e8a4f57ea1170b468181728",
521            "ebf8388ba558660ffc67ac6d14709b7ffd096603ba23660c761b603767b469d7",
522            "233f0de0b9f70c2b7de870fc2f3d0b0d1fa37224a3264525d2d8537862c353d8",
523            "9fdf2626bcb2e5a8622dd1fcc78ce78db3a2aceeff030def85574259ae41e555",
524            "979346e3d31abf04f815ffd1d7bd44da03c636172b46ab260e365c4a4672445e",
525        ],
526    ];
527
528    #[cfg(not(feature = "shake256x4"))]
529    const KAT: [[&str; 10]; 9] = [
530        [
531            "517e169d05b8cd4b6afa81847d5f1ed47309650a9ccff39c4445ae57914a2058",
532            "ce8f23924e463c769cedbd034eb0f11574c1cb8a453949c6c36b34e09e41d06c",
533            "65c2b2a6f2054faf7dbe97454d68b66768ca2ca5f65e7cbea5a91cdc1c6549a4",
534            "568e6ad817ba21b555808255db94a710ebbd5005284585365dc5308046e23d66",
535            "8ec696eaee01f9ec43bdb04d9dab7ff43d8bd80c7081134b9863f7c6ebbbe284",
536            "96e95da3c03426bbb3448084cbb54d83392acae745e3781f890dcddb030572cb",
537            "469be4112615d98308ef9df8cb8b0f3da1ca2558d79b7a867530de7d4000fb9b",
538            "80d7cc5c779ae2a590249169e10e935a1e8bf481d1c8babf3487acc0838b99d1",
539            "faac905c850eeb978e776f1e7fb1bf7ad40dd9618792f25fc3fae9ee47d8ce15",
540            "fc484c374ab40a4dbb5ea62b04a10f0c945105cddd48c4a90e729fc07680e88b",
541        ],
542        [
543            "bef4b8dc62d8e0b5eca9bc09366b1dcf7327dfbac10042406cc2217e9d0791f5",
544            "f75a3392c69345b6f5355d104305efae9a9d90fd5dfaa03120a12e02356b34fd",
545            "e3c8a660f6ab7102d9a975c94d6c0206e0835cc88ab36dd63556540c15b32ac3",
546            "1cd5bcadfc883540211d7803a2d6ff47474e4d5bc8a42b79ff97327f6e75b574",
547            "671b901f1535f58e198eab7fb9e84525f4315337abe30f902e6c0305501b0709",
548            "680d8b79f8d91ce1ae6a070baebd8f3f99472efe1c14efa35e1a653472ac98c2",
549            "3d9c77564d8ac733fd20bbff61d078c0ee094dc50ef2a0d8b53238263bd9f0d9",
550            "5b96561ca0cb41b1c09dc569ee48e596067df5a287a838f88b98e2375880d053",
551            "b306079cd1f2f4029cee72988ace631572ad7f2620e020cf5ad4b1ae598424e1",
552            "9a8cf1dc6b62c9f8b7790943ca9e48beef24aa8326b9002146e1858bc61103d2",
553        ],
554        [
555            "e3f4742be370ea418feab49c3fee6a98ac52c1f1cb39138a90092449595b3f81",
556            "d6659d9895e4b59c1001bf8354319889ef89f9c42570147dc86d615db94ade09",
557            "f4b95d6d27a64fbd303a7091625bd8daf61c3301376d512203c2fb53fb726317",
558            "8c94c3df7eb93a31ce32b756ed0279c8c36e8cbe9a76901823d61f64244be8a9",
559            "c2ff7bff549c1f050c81dc3ca6ccd0537f0345b304f9271457405ac5b0ff1bb6",
560            "d03a17462c30a7bd7d594cd0ac209d4f3704a96934e0c12e31010e8bcd58f472",
561            "e694f16019b9ed9afa360db94a29bf5036ef88fb4ff5fe8315bcb0cfd9b65bad",
562            "89cd099d215ed66c93586484c48994e3d772511768561e0465c74852ff921ac0",
563            "366844a98fa179f96019f6f930829c960990ac438da24a945f40791b4ab3771c",
564            "736d879d25597e39878548a2efc2b37d7d48744cf621803a97fa84d1337d8478",
565        ],
566        [
567            "f13154701ddb47458310e09b3bd20350fc8ec13b42dfb2f3414fe49dc21054e5",
568            "dccc7800c714f9b1e28f6f414ad0760619bf19d319eee3e7a2f7cbae16ec44f4",
569            "0a7c0f023c83e81fce3aa8737aa999131f9b1c78db813c8471af2bf41af34959",
570            "65afc54bf77ab9a82c306d15e66a993e4af999a9edb08d822cc70e05d4d73866",
571            "b05a93ffe94fb3ea4da3430929ea577310c748744e0b9595bd8c4243ac8ffe7a",
572            "7ef713863e94b608f95cf4bb63214fb2fd23812d98d88cd06213e2d9d9dce4c0",
573            "39a2b9a5411ae63a80a75725abaab5191d394229d2dd44c2476bc2fee88a29b6",
574            "354bd2ccf6b8c814bfa644e4bc9e5610d42d1516e3fe342d9878ba7d03033a5c",
575            "9e93e4e05072a624170bccb231caacf69faaaaced8b636eed522ac031eb1e25d",
576            "104a62c58b72b70f1fe0eac8ca4b8775ddcbd2ec62f07f91e6b54dd578c1a65c",
577        ],
578        [
579            "7a65360991f32d38d8267e9b8fa29f21f59923efa27a39214abb3412d316cf13",
580            "13c0a7f42988e36cc0440f056341791fab717a0b1f9a62954489388a77c1447f",
581            "5aef7941bda5f7ec4b12141eaf09dcafe741e4aac536f232e167b7c154196999",
582            "60a910388695d6aa8d1194e70fa7a502e21e98f50017b8282cab0c6c18e82c62",
583            "304ffc725fc4b6515f3ca5b45dc7b86155cab3de57657efe9c647643d93e0d69",
584            "e2ce9a2d7acdb4d0cf906e1d072d4448537fa42ac2f3e0d0531a6e6336196eee",
585            "081848b14fc7dd83df56c2f2379bdb20266241c428ce09cee65c3b4dd1965989",
586            "4c9066103f99d6aaac3dbe5be9ef0dd06f090a8479e458aa3e83e6b26c6d19f3",
587            "ac6fb3acbae450b3d75927532a462fadd64a1c0f025b1c416f27b2f41f945567",
588            "65190b3eb43adbaa09d9bf2ccf971b70fb382c56e8676dc4a579f8d666f953c2",
589        ],
590        [
591            "4850dc7976386e64a98cdbbb8a886e6d8cdb52c496e9d626f4c8fdd915658494",
592            "9d17107e409910f16a43ac73068c2d656db2ca684ba86c0ad7a4cb14ca1bf931",
593            "64adc2f8d3594064a32cb1f00ab8ddcfbad33fde6d2398f829c1923cb38d52c5",
594            "edad1b81ff0a71bd76ed7984450030ac9cc861e62d432f85ed0ed83b2d463c8a",
595            "9d9ae5f83c1a768eb8cd3b3a09aa5d8ba9d659c43fbf00892f0c32b0ead076c2",
596            "f27c0674120eace270ae47852a38596ed1867727f6748cd7128682574d4b3a05",
597            "d2ee57706149d6838df63dda7b122e32445f0ed2b495fd336c46ce3384a3c0ee",
598            "3774f7f8c948792aa4c480d3b9300b5cb91cbb619327a18ee66a89511cad2d92",
599            "b915349483e3a85db14fa612327b7eaa1ab201f47d0795a05d06bd5b2d92def2",
600            "daed7bab12476abb916a22f092ba52a93da5540506188fb982f538ad98d3db11",
601        ],
602        [
603            "1c3196947f64e22696a151f75ce97f67626ffc089abc4681adc2caf3c4828b1a",
604            "2749938e3936a4ffb83583da86f3506a2a88f73248e80f14c50c8c92cd9d4ede",
605            "8884f415f33b2d6a91a12b8d4fa0f9641178b62f2a623ff2f9cdff74cf88cf87",
606            "8129eb52137edc7c56accf0b9d273b29085f77548693596075db48106c550c0a",
607            "2f1b7292868b1e10cceca079f60a065431f1381ce5046d9f6ae7191822110c40",
608            "b040ff3ec202020a879c69fc6f51c350d256fb02691aeaa77b1abfd9df6af42b",
609            "0f3c4a77457d920a0c87cf5dbfef8899a67aabda08ed5d8f2c5c5e9eb99fff41",
610            "1c51ce7bb6fa8b37a3c5ee99a09138bf9fd8310071d8adb91cd692d34e212daf",
611            "c3de8a295ef2ed1dfdc4c6a21f6c989c55435890d40e2706424660cb798befdb",
612            "230f62ab8fc0d086140584fc8977597f2c591da1f9627aef502c9e9eee9f4abc",
613        ],
614        [
615            "53748d0bda7a655b160d1237687f606fc6d85a768af7e52accb320cdc02fea56",
616            "566ff306b9e7a8509252fcbd315faa1c7d9a99e90a6e5a1e211dca0492fd2422",
617            "3927502da6d66d09c71baad0fb307e767287bca9defd3e5658093758dd6f4eb6",
618            "7d00e218c02ca8e2b0b475c67f06b544d74b24a0c79e775066f6d35b85bba168",
619            "5c7e80b3b95bbb04272cf6cb5482f98c5f48303119be7c1864fda7d183cf8dd2",
620            "8f8238dc09555fb6a06505af7d08ea909829bab3443c651791e91444d4f9ea29",
621            "27dc1593aea3529c25112b536ba38bf7ff26796f7199aff8597db61c013316c4",
622            "92770a08ede1e89721661b8812879ab2c1cae3ffe66056fa23e2ac4cc984998b",
623            "31e9907ba30080033d48535b1ecbc3e25e6b6b450fb1b310935e8b278654700e",
624            "779fb106eb89f09e1d09a7c3c3295d8b63fa93ca3e59de9a9adcc1eb3f392c0c",
625        ],
626        [
627            "c04a645eb9e60d117d29fe4a0d5314bedf1392cbad20bb15f9cc88ac25cd78e2",
628            "1f0e8af75f9abfed60ddafda6286c6fb27395188d3191763eedb05c00c908b39",
629            "669de6300e9fa19fbb9675769525d1f68d166297f6a67753c4dda74927c83286",
630            "d3990a8b5790cf298949bfae84f0fdee9898c95e56d5c54a8a315b81f521ac41",
631            "579d09da76792fcd7047cd3a271b3bddca1f8f0e753b1064466af4c297ca82aa",
632            "b0154f77732cf43763902e15e6683525841438343e4423f038990d923ee5e9a8",
633            "ece2780b43ae0744b5269730688d41871e5280c2ec6ed66535d9b0ece4a3aca5",
634            "51ca0dc5ccae2abb38e39eb2fa8bbc1bbfa46e4dca62bf6bd9666fe1eeb22803",
635            "8ae1591a9827357670f983a22cca71e754ede9ccae51f9a2f4bff89354903d0f",
636            "cde3f08ca0f7dcf93398bcbed80575c114dc1ddc046cb989385149e6a5deba13",
637        ],
638    ];
639
640    fn inner_kat<KG: KeyPairGenerator, SK: SigningKey, VK: VerifyingKey>(
641        logn: u32,
642        num: u32,
643    ) -> [u8; 32] {
644        let seed1 = [
645            0x00u8,
646            logn as u8,
647            num as u8,
648            (num >> 8) as u8,
649            (num >> 16) as u8,
650            (num >> 24) as u8,
651        ];
652        let mut rng1 = FakeRng1::new(&seed1);
653        let seed2 = [
654            0x01u8,
655            logn as u8,
656            num as u8,
657            (num >> 8) as u8,
658            (num >> 16) as u8,
659            (num >> 24) as u8,
660        ];
661        let mut rng2 = FakeRng2::new(&seed2);
662
663        let mut sk_buf = [0u8; SIGN_KEY_SIZE_1024];
664        let mut vk_buf = [0u8; VRFY_KEY_SIZE_1024];
665        let mut sig_buf = [0u8; SIGNATURE_SIZE_1024];
666        let sk = &mut sk_buf[..sign_key_size(logn).unwrap()];
667        let vk = &mut vk_buf[..vrfy_key_size(logn).unwrap()];
668        let sig = &mut sig_buf[..signature_size(logn).unwrap()];
669
670        KG::default().keygen(logn, &mut rng1, sk, vk).unwrap();
671        let mut s = SK::decode(sk).unwrap();
672        let v = VK::decode(vk).unwrap();
673        let dom = DomainContext::new(b"domain").unwrap();
674        if (num & 1) == 0 {
675            s.sign(&mut rng2, &dom, &HASH_ID_RAW, b"message", sig)
676                .unwrap();
677            assert!(v.verify(sig, &dom, &HASH_ID_RAW, b"message"));
678        } else {
679            let mut sh = SHA3_256::new();
680            sh.update(&b"message"[..]);
681            let hv = sh.digest();
682            s.sign(&mut rng2, &dom, &HASH_ID_SHA3_256, &hv, sig)
683                .unwrap();
684            assert!(v.verify(sig, &dom, &HASH_ID_SHA3_256, &hv));
685        }
686        let mut sh = SHA3_256::new();
687        sh.update(sk);
688        sh.update(vk);
689        sh.update(sig);
690        sh.digest()
691    }
692
693    #[test]
694    fn test_kat() {
695        for (i, kat_rows) in KAT.iter().enumerate() {
696            let logn = (i as u32) + 2;
697            for (j, kat) in kat_rows.iter().enumerate() {
698                let r = if logn <= 8 {
699                    inner_kat::<KeyPairGeneratorWeak, SigningKeyWeak, VerifyingKeyWeak>(
700                        logn, j as u32,
701                    )
702                } else {
703                    inner_kat::<KeyPairGeneratorStandard, SigningKeyStandard, VerifyingKeyStandard>(
704                        logn, j as u32,
705                    )
706                };
707                assert!(r[..] == hex::decode(kat).unwrap());
708            }
709        }
710    }
711
712    #[test]
713    fn keygen_reports_validation_errors() {
714        let mut kg = KeyPairGeneratorStandard::default();
715        let mut rng = FakeRng1::new(b"keygen");
716        let mut sk = [0u8; SIGN_KEY_SIZE_512];
717        let mut vk = [0u8; VRFY_KEY_SIZE_512];
718        let mut short_sk = [0u8; SIGN_KEY_SIZE_512 - 1];
719        let mut short_vk = [0u8; VRFY_KEY_SIZE_512 - 1];
720
721        assert_eq!(
722            kg.keygen(8, &mut rng, &mut sk, &mut vk),
723            Err(KeyGenError::UnsupportedLogN { logn: 8 }),
724        );
725        assert_eq!(
726            kg.keygen(FN_DSA_LOGN_512, &mut rng, &mut short_sk, &mut vk),
727            Err(KeyGenError::InvalidSigningKeyBufferLen {
728                expected: SIGN_KEY_SIZE_512,
729                actual: SIGN_KEY_SIZE_512 - 1,
730            }),
731        );
732        assert_eq!(
733            kg.keygen(FN_DSA_LOGN_512, &mut rng, &mut sk, &mut short_vk),
734            Err(KeyGenError::InvalidVerifyingKeyBufferLen {
735                expected: VRFY_KEY_SIZE_512,
736                actual: VRFY_KEY_SIZE_512 - 1,
737            }),
738        );
739    }
740
741    #[test]
742    fn size_helpers_report_validation_errors() {
743        assert_eq!(
744            sign_key_size(1),
745            Err(LogNError::UnsupportedLogN { logn: 1 }),
746        );
747        assert_eq!(
748            vrfy_key_size(11),
749            Err(LogNError::UnsupportedLogN { logn: 11 }),
750        );
751        assert_eq!(
752            signature_size(0),
753            Err(LogNError::UnsupportedLogN { logn: 0 }),
754        );
755    }
756
757    #[test]
758    fn sign_reports_validation_errors() {
759        let mut kg = KeyPairGeneratorStandard::default();
760        let mut rng = FakeRng1::new(b"sign");
761        let mut sk_buf = [0u8; SIGN_KEY_SIZE_512];
762        let mut vk_buf = [0u8; VRFY_KEY_SIZE_512];
763        kg.keygen(FN_DSA_LOGN_512, &mut rng, &mut sk_buf, &mut vk_buf)
764            .unwrap();
765
766        let mut sk = SigningKeyStandard::decode(&sk_buf).unwrap();
767        let vk = VerifyingKeyStandard::decode(&vk_buf).unwrap();
768        let mut short_vk = [0u8; VRFY_KEY_SIZE_512 - 1];
769        let mut short_sig = [0u8; SIGNATURE_SIZE_512 - 1];
770        let mut sig = [0u8; SIGNATURE_SIZE_512];
771        let long_ctx_bytes = [0u8; 256];
772        let long_ctx = DomainContext::new(&long_ctx_bytes);
773        let good_ctx = DomainContext::new(b"ctx").unwrap();
774
775        assert_eq!(
776            sk.to_verifying_key(&mut short_vk),
777            Err(SigningKeyError::InvalidVerifyingKeyBufferLen {
778                expected: VRFY_KEY_SIZE_512,
779                actual: VRFY_KEY_SIZE_512 - 1,
780            }),
781        );
782        assert_eq!(
783            sk.sign(
784                &mut rng,
785                &good_ctx,
786                &HASH_ID_RAW,
787                b"message",
788                &mut short_sig
789            ),
790            Err(SigningKeyError::InvalidSignatureBufferLen {
791                expected: SIGNATURE_SIZE_512,
792                actual: SIGNATURE_SIZE_512 - 1,
793            }),
794        );
795        assert_eq!(long_ctx, Err(DomainContextError::Oversized { actual: 256 }),);
796
797        sk.sign(&mut rng, &good_ctx, &HASH_ID_RAW, b"message", &mut sig)
798            .unwrap();
799        assert!(!vk.verify(&sig, &DOMAIN_NONE, &HASH_ID_RAW, b"other"));
800    }
801
802    #[test]
803    fn hash_to_point_reports_validation_errors() {
804        let nonce = [0u8; 40];
805        let short_nonce = [0u8; 39];
806        let hashed_key = [0u8; 64];
807        let short_hashed_key = [0u8; 63];
808        let mut c = [0u16; 4];
809        let long_ctx_bytes = [0u8; 256];
810        let long_ctx = DomainContext::new(&long_ctx_bytes);
811
812        assert_eq!(
813            hash_to_point(
814                &short_nonce,
815                &hashed_key,
816                &DOMAIN_NONE,
817                &HASH_ID_RAW,
818                b"m",
819                &mut c
820            ),
821            Err(HashToPointError::InvalidNonceLength { actual: 39 }),
822        );
823        assert_eq!(
824            hash_to_point(
825                &nonce,
826                &short_hashed_key,
827                &DOMAIN_NONE,
828                &HASH_ID_RAW,
829                b"m",
830                &mut c
831            ),
832            Err(HashToPointError::InvalidHashedVerifyingKeyLength { actual: 63 }),
833        );
834        assert_eq!(long_ctx, Err(DomainContextError::Oversized { actual: 256 }),);
835    }
836
837    #[test]
838    fn hash_identifier_reports_validation_errors() {
839        assert_eq!(HashIdentifier::new(b""), Err(HashIdentifierError::Empty));
840        assert_eq!(
841            HashIdentifier::new(&[0x01]),
842            Err(HashIdentifierError::InvalidSingleByteValue { actual: 0x01 }),
843        );
844
845        assert_eq!(HashIdentifier::new(HASH_ID_RAW.as_bytes()), Ok(HASH_ID_RAW));
846        assert_eq!(
847            HashIdentifier::new(HASH_ID_SHA3_256.as_bytes()),
848            Ok(HASH_ID_SHA3_256),
849        );
850    }
851
852    #[test]
853    fn falcon_profiles_roundtrip() {
854        let mut kg = KeyPairGeneratorStandard::default();
855        let mut rng = FakeRng1::new(b"falcon-profile");
856        let mut sk_buf = [0u8; SIGN_KEY_SIZE_512];
857        let mut vk_buf = [0u8; VRFY_KEY_SIZE_512];
858        kg.keygen(FN_DSA_LOGN_512, &mut rng, &mut sk_buf, &mut vk_buf)
859            .unwrap();
860
861        let mut sk = SigningKeyStandard::decode(&sk_buf).unwrap();
862        let vk = VerifyingKeyStandard::decode(&vk_buf).unwrap();
863        let message = b"profiled message";
864
865        let mut pqclean_sig = [0u8; SIGNATURE_SIZE_512];
866        let pqclean_len = sk
867            .sign_falcon(&mut rng, FalconProfile::PqClean, message, &mut pqclean_sig)
868            .unwrap();
869        assert!(pqclean_len > 1 + FALCON_NONCE_LEN);
870        assert!(pqclean_len <= SIGNATURE_SIZE_512);
871        assert!(vk.verify_falcon(FalconProfile::PqClean, &pqclean_sig[..pqclean_len], message,));
872
873        let mut tidecoin_sig = [0u8; TIDECOIN_LEGACY_FALCON512_SIG_MAX];
874        let tidecoin_len = sk
875            .sign_falcon(
876                &mut rng,
877                FalconProfile::TidecoinLegacyFalcon512,
878                message,
879                &mut tidecoin_sig,
880            )
881            .unwrap();
882        assert!(tidecoin_len > 1 + FALCON_NONCE_LEN);
883        assert!(tidecoin_len <= TIDECOIN_LEGACY_FALCON512_SIG_MAX);
884        assert!(vk.verify_falcon(
885            FalconProfile::TidecoinLegacyFalcon512,
886            &tidecoin_sig[..tidecoin_len],
887            message,
888        ));
889    }
890}