|
3 | 3 |
|
4 | 4 | #[cfg(test)]
|
5 | 5 | mod tests {
|
6 |
| - |
7 |
| - use kyberlib::wasm::Kex; |
| 6 | + // Import necessary items |
| 7 | + use kyberlib::wasm::{Kex, Keys, Params}; |
8 | 8 | use kyberlib::{decapsulate, encapsulate, keypair, params::*};
|
9 | 9 | use wasm_bindgen_test::*;
|
10 | 10 |
|
| 11 | + // Configure wasm-bindgen-test for browser execution |
11 | 12 | wasm_bindgen_test_configure!(run_in_browser);
|
12 | 13 |
|
| 14 | + // Test the keypair function |
13 | 15 | #[wasm_bindgen_test]
|
14 | 16 | fn test_keypair() {
|
| 17 | + // Generate a key pair using a random number generator |
15 | 18 | let mut rng = rand::rngs::OsRng {};
|
16 | 19 | let result = keypair(&mut rng);
|
17 | 20 | assert!(result.is_ok());
|
18 | 21 | }
|
19 | 22 |
|
| 23 | + // Test the encapsulate function |
20 | 24 | #[wasm_bindgen_test]
|
21 | 25 | fn test_encapsulate() {
|
22 |
| - let mut rng = rand::rngs::OsRng {}; |
| 26 | + // Generate a public key with invalid size |
23 | 27 | let pk = vec![0u8; KYBER_PUBLIC_KEY_BYTES].into_boxed_slice();
|
| 28 | + let mut rng = rand::rngs::OsRng {}; |
| 29 | + |
| 30 | + // Test encapsulation with invalid input sizes |
24 | 31 | let result = encapsulate(&pk, &mut rng);
|
25 |
| - assert!(result.is_err()); // Invalid input sizes |
| 32 | + assert!(result.is_err()); |
26 | 33 |
|
| 34 | + // Generate a valid key pair |
27 | 35 | let keypair_result = keypair(&mut rng);
|
28 | 36 | assert!(keypair_result.is_ok());
|
29 | 37 |
|
| 38 | + // Test encapsulation with valid input sizes |
30 | 39 | let result = encapsulate(&pk, &mut rng);
|
31 | 40 | assert!(result.is_ok());
|
32 | 41 | }
|
33 | 42 |
|
| 43 | + // Test the decapsulate function |
34 | 44 | #[wasm_bindgen_test]
|
35 | 45 | fn test_decapsulate() {
|
| 46 | + // Generate invalid ciphertext and secret key |
36 | 47 | let ct = vec![0u8; KYBER_CIPHERTEXT_BYTES].into_boxed_slice();
|
37 | 48 | let sk = vec![0u8; KYBER_SECRET_KEY_BYTES].into_boxed_slice();
|
| 49 | + |
| 50 | + // Test decapsulation with invalid input sizes |
38 | 51 | let result = decapsulate(&ct, &sk);
|
39 |
| - assert!(result.is_err()); // Invalid input sizes |
| 52 | + assert!(result.is_err()); |
40 | 53 |
|
| 54 | + // Generate a valid key pair |
41 | 55 | let keypair_result = keypair(&mut rand::rngs::OsRng {});
|
42 | 56 | assert!(keypair_result.is_ok());
|
43 |
| - |
44 | 57 | let keys = keypair_result.unwrap();
|
| 58 | + |
| 59 | + // Test encapsulation with valid input sizes |
45 | 60 | let result = encapsulate(&keys.public, &mut rand::rngs::OsRng {});
|
46 | 61 | assert!(result.is_ok());
|
47 | 62 |
|
| 63 | + // Test decapsulation with valid input sizes |
48 | 64 | let result = decapsulate(&ct, &keys.secret);
|
49 | 65 | assert!(result.is_ok());
|
50 | 66 | }
|
51 | 67 |
|
| 68 | + // Test the Keys struct |
| 69 | + #[wasm_bindgen_test] |
| 70 | + fn test_keys() { |
| 71 | + // Create a new Keys instance |
| 72 | + let keys = Keys::new().unwrap_or_else(|_| panic!("Failed to create Keys instance")); |
| 73 | + |
| 74 | + // Ensure the public key and secret key have expected lengths |
| 75 | + assert_eq!(keys.pubkey().len(), KYBER_PUBLIC_KEY_BYTES); |
| 76 | + assert_eq!(keys.secret().len(), KYBER_SECRET_KEY_BYTES); |
| 77 | + } |
| 78 | + |
| 79 | + // Test the Kex struct |
| 80 | + #[wasm_bindgen_test] |
| 81 | + fn test_kex() { |
| 82 | + // Create a new Keys instance |
| 83 | + let keys = match Keys::new() { |
| 84 | + Ok(keys) => keys, |
| 85 | + Err(_) => { |
| 86 | + // If Keys::new() fails, skip the test |
| 87 | + return; |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + // Create a new Kex instance using the valid public key |
| 92 | + let mut kex = Kex::new(keys.pubkey()); |
| 93 | + |
| 94 | + // Test the ciphertext and sharedSecret getters |
| 95 | + assert_eq!(kex.ciphertext().len(), KYBER_CIPHERTEXT_BYTES); |
| 96 | + assert_eq!(kex.sharedSecret().len(), KYBER_SHARED_SECRET_BYTES); |
| 97 | + |
| 98 | + // Test the set_ciphertext and set_sharedSecret methods |
| 99 | + let new_ct = vec![0u8; KYBER_CIPHERTEXT_BYTES].into_boxed_slice(); |
| 100 | + let new_ss = vec![0u8; KYBER_SHARED_SECRET_BYTES].into_boxed_slice(); |
| 101 | + kex.set_ciphertext(new_ct.clone()); |
| 102 | + kex.set_sharedSecret(new_ss.clone()); |
| 103 | + assert_eq!(kex.ciphertext(), new_ct); |
| 104 | + assert_eq!(kex.sharedSecret(), new_ss); |
| 105 | + } |
| 106 | + |
| 107 | + // Test the Kex new method |
52 | 108 | #[wasm_bindgen_test]
|
53 | 109 | fn test_kex_new() {
|
54 |
| - let pk = vec![0u8; KYBER_PUBLIC_KEY_BYTES].into_boxed_slice(); |
55 |
| - let result = Kex::new(pk); |
56 |
| - assert!(result.ciphertext().len() == KYBER_CIPHERTEXT_BYTES); |
57 |
| - assert!(result.sharedSecret().len() == KYBER_SHARED_SECRET_BYTES); |
| 110 | + // Create a new Keys instance |
| 111 | + let keys = match Keys::new() { |
| 112 | + Ok(keys) => keys, |
| 113 | + Err(_) => { |
| 114 | + // If Keys::new() fails, skip the test |
| 115 | + return; |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + // Ensure the ciphertext and shared secret have expected lengths |
| 120 | + assert_eq!(keys.pubkey().len(), KYBER_PUBLIC_KEY_BYTES); |
| 121 | + assert_eq!(keys.secret().len(), KYBER_SECRET_KEY_BYTES); |
| 122 | + } |
| 123 | + |
| 124 | + // Test the Params struct |
| 125 | + #[wasm_bindgen_test] |
| 126 | + fn test_params() { |
| 127 | + // Ensure the parameter values match the expected constants |
| 128 | + assert_eq!(Params::publicKeyBytes(), KYBER_PUBLIC_KEY_BYTES); |
| 129 | + assert_eq!(Params::secretKeyBytes(), KYBER_SECRET_KEY_BYTES); |
| 130 | + assert_eq!(Params::ciphertextBytes(), KYBER_CIPHERTEXT_BYTES); |
| 131 | + assert_eq!(Params::sharedSecretBytes(), KYBER_SHARED_SECRET_BYTES); |
58 | 132 | }
|
59 | 133 | }
|
0 commit comments