forked from Argyle-Software/kyber
-
Notifications
You must be signed in to change notification settings - Fork 3
/
kem.rs
48 lines (42 loc) · 1.38 KB
/
kem.rs
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
use safe_pqc_kyber::*;
#[test]
fn keypair_encap_decap() {
let mut rng = rand::thread_rng();
let keys = keypair(&mut rng);
let (ct, ss1) = encapsulate(&keys.public, &mut rng).unwrap();
let ss2 = decapsulate(&ct, &keys.secret).unwrap();
assert_eq!(ss1, ss2);
}
#[test]
fn keypair_encap_decap_invalid_ciphertext() {
let mut rng = rand::thread_rng();
let keys = keypair(&mut rng);
let (mut ct, ss) = encapsulate(&keys.public, &mut rng).unwrap();
ct[..4].copy_from_slice(&[255u8;4]);
assert!(decapsulate(&ct, &keys.secret).unwrap() != ss);
}
#[test]
fn keypair_encap_pk_wrong_size() {
let mut rng = rand::thread_rng();
let pk: [u8; KYBER_PUBLICKEYBYTES + 3] = [1u8; KYBER_PUBLICKEYBYTES + 3];
assert!(encapsulate(&pk, &mut rng).is_err());
}
#[test]
fn keypair_decap_ct_wrong_size() {
let ct: [u8; KYBER_CIPHERTEXTBYTES + 3] = [1u8; KYBER_CIPHERTEXTBYTES + 3];
let sk: [u8; KYBER_SECRETKEYBYTES] = [1u8; KYBER_SECRETKEYBYTES];
assert!(decapsulate(&ct, &sk).is_err());
}
#[test]
fn keypair_decap_sk_wrong_size() {
let ct: [u8; KYBER_CIPHERTEXTBYTES] = [1u8; KYBER_CIPHERTEXTBYTES];
let sk: [u8; KYBER_SECRETKEYBYTES + 3] = [1u8; KYBER_SECRETKEYBYTES + 3];
assert!(decapsulate(&ct, &sk).is_err());
}
#[test]
fn public_from_private() {
let mut rng = rand::thread_rng();
let keys = keypair(&mut rng);
let pk2 = public(&keys.secret);
assert_eq!(pk2, keys.public);
}