Skip to main content

simple_saber/
lib.rs

1mod config;
2mod scheme;
3mod encapsulation;
4mod error;
5mod types;
6mod exportable_params;
7
8pub use config::{Saber};
9pub use scheme::{Key, SaberCiphertext};
10pub use crate::types::{SaberKeypair, SaberPublicKey, SaberSecretKey};
11pub use crate::encapsulation::SaberEncapsulated;
12pub use crate::encapsulation::ToSaberEncapsulated;
13pub use crate::encapsulation::SaberDecapsulate;
14
15
16/*
17This crate provides a simplified implementation of SABER KEM scheme. It was rebuilt to simplify implementation, and to use `simple-ring`. 
18For example :
19
20- SABER doesn't normally use the NTT nor a NTT-friendly ciphertext modulus q. In this implementation, I decided to use the NTT & 
21in consequence a compatible ciphertext modulus. 
22
23- SABER uses other parameters, like T (a factor used in compression and encoding)
24*/
25#[cfg(test)]
26#[test]
27fn test_encrypt_decrypt_roundtrip() {
28    use simple_ring::Polynomial;
29    let saber = Saber::light();
30    let kp = saber.keygen();
31    
32    let msg = Polynomial::random_binary(saber.params.n);
33    
34    let ct = saber.encrypt(&kp.public_key, &msg);
35    let decrypted = saber.decrypt(&kp, &ct);
36    
37    assert_eq!(msg.coeffs, decrypted.coeffs, "decrypt(encrypt(m)) != m");
38}
39
40#[cfg(test)]
41#[test]
42fn test_encapdecap() {
43    let saber = Saber::fire();
44    let kp = saber.keygen();
45    let (key, serialized_enc) = match saber.encapsulate(&kp.public_key) {
46        Ok((key, serialized)) => (key, serialized),
47        Err(e) => panic!("Error while encapsulating : {:?}", e),
48    };
49    let recovered = match serialized_enc.decapsulate(&kp) {
50        Ok(key) => key,
51        Err(e) => { panic!("Error while decapsulating : {:?}", e) }
52    };
53    assert_eq!(key, recovered, "Error : the encapsulated key and the decapsulated key doesn't match !")
54}
55
56#[test]
57fn test_encode_decode_roundtrip() {
58    use rand::{RngCore, rngs::OsRng};
59
60    use simple_ring::Polynomial;
61    let saber = Saber::light(); 
62    let params = &saber.params;
63    
64    let mut rng = OsRng;
65    let mut coeffs = vec![0u64; params.n];
66    for c in coeffs.iter_mut() {
67        *c = rng.next_u32() as u64 & 1;
68    }
69    let msg = Polynomial::new(coeffs);
70    
71    let encoded = saber.encode(params, &msg);
72    let decoded = saber.decode(params, &encoded);
73    
74    assert_eq!(msg.coeffs, decoded.coeffs, "encode/decode are not inverse");
75}
76
77
78