|
| 1 | +// Copyright © 2024 kyberlib. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 3 | + |
| 4 | +// Import necessary modules |
| 5 | +use kyberlib::*; |
| 6 | +use rand::rngs::OsRng; |
| 7 | + |
| 8 | +// Unit tests module |
| 9 | +#[cfg(test)] |
| 10 | +mod tests { |
| 11 | + // Import necessary items from the parent module |
| 12 | + use super::*; |
| 13 | + |
| 14 | + // Test for keypair generation |
| 15 | + #[test] |
| 16 | + fn test_keypair_generation() { |
| 17 | + // Initialize a random number generator |
| 18 | + let mut rng = OsRng; |
| 19 | + // Generate keypair |
| 20 | + let keypair = keypair(&mut rng).unwrap(); |
| 21 | + // Assert the length of the public and secret keys |
| 22 | + assert_eq!(keypair.public.len(), KYBER_PUBLIC_KEY_BYTES); |
| 23 | + assert_eq!(keypair.secret.len(), KYBER_SECRET_KEY_BYTES); |
| 24 | + } |
| 25 | + |
| 26 | + // Test for encapsulation and decapsulation |
| 27 | + #[test] |
| 28 | + fn test_encapsulate_decapsulate() { |
| 29 | + // Initialize a random number generator |
| 30 | + let mut rng = OsRng; |
| 31 | + // Generate keypair |
| 32 | + let keypair = keypair(&mut rng).unwrap(); |
| 33 | + // Encapsulate a shared secret |
| 34 | + let (ciphertext, shared_secret1) = encapsulate(&keypair.public, &mut rng).unwrap(); |
| 35 | + // Decapsulate the shared secret |
| 36 | + let shared_secret2 = decapsulate(&ciphertext, &keypair.secret).unwrap(); |
| 37 | + // Assert equality of the shared secrets |
| 38 | + assert_eq!(shared_secret1, shared_secret2); |
| 39 | + } |
| 40 | + |
| 41 | + // Test for keypair derivation |
| 42 | + #[test] |
| 43 | + fn test_derive_keypair() { |
| 44 | + // Create a seed |
| 45 | + let seed = [0u8; 64]; |
| 46 | + // Derive a keypair from the seed |
| 47 | + let keypair = derive(&seed).unwrap(); |
| 48 | + // Assert the length of the public and secret keys |
| 49 | + assert_eq!(keypair.public.len(), KYBER_PUBLIC_KEY_BYTES); |
| 50 | + assert_eq!(keypair.secret.len(), KYBER_SECRET_KEY_BYTES); |
| 51 | + } |
| 52 | + |
| 53 | + // Test for public key extraction |
| 54 | + #[test] |
| 55 | + fn test_public_key_extraction() { |
| 56 | + // Initialize a random number generator |
| 57 | + let mut rng = OsRng; |
| 58 | + // Generate keypair |
| 59 | + let keypair = keypair(&mut rng).unwrap(); |
| 60 | + // Extract public key from the secret key |
| 61 | + let extracted_pk = public(&keypair.secret); |
| 62 | + // Assert equality of the extracted public key and the original public key |
| 63 | + assert_eq!(extracted_pk, keypair.public); |
| 64 | + } |
| 65 | + |
| 66 | + // Test for handling of invalid inputs |
| 67 | + #[test] |
| 68 | + fn test_invalid_input_handling() { |
| 69 | + // Initialize a random number generator |
| 70 | + let mut rng = OsRng; |
| 71 | + // Generate keypair |
| 72 | + let keypair = keypair(&mut rng).unwrap(); |
| 73 | + // Define invalid public key, ciphertext, and secret key |
| 74 | + let invalid_pk = [0u8; KYBER_PUBLIC_KEY_BYTES - 1]; |
| 75 | + let invalid_ct = [0u8; KYBER_CIPHERTEXT_BYTES - 1]; |
| 76 | + let invalid_secret_key = [0u8; KYBER_SECRET_KEY_BYTES - 1]; |
| 77 | + |
| 78 | + // Assert error handling for encapsulation with invalid public key |
| 79 | + assert!(encapsulate(&invalid_pk, &mut rng).is_err()); |
| 80 | + // Assert error handling for decapsulation with invalid ciphertext and secret key |
| 81 | + assert!(decapsulate(&invalid_ct, &keypair.secret).is_err()); |
| 82 | + assert!(decapsulate(&invalid_ct, &invalid_secret_key).is_err()); |
| 83 | + // Assert error handling for keypair derivation with invalid secret key |
| 84 | + assert!(derive(&invalid_secret_key).is_err()); |
| 85 | + |
| 86 | + // Define invalid seed |
| 87 | + let invalid_seed = [0u8; 63]; |
| 88 | + // Assert error handling for keypair derivation with invalid seed |
| 89 | + assert!(derive(&invalid_seed).is_err()); |
| 90 | + } |
| 91 | +} |
0 commit comments