Skip to content
forked from rozbb/saber-rs

A pure-Rust implementation of the Saber key encapsulation mechanism (KEM)

License

Notifications You must be signed in to change notification settings

pinkforest/saber-rs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

saber_kem

This crate is a pure-Rust, no-std implementation of draft 3 of the Saber key encapsulation mechanism (KEM). Saber is a lattice-based KEM that is designed to be secure against classical and quantum adversaries. It comes three variants:

  • LightSaber, which is designed to have security roughly equivalent to AES-128
  • Saber, which is designed to have security roughly equivalent to AES-192
  • FireSaber, which is designed to have security roughly equivalent to AES-256

Warning

This crate has not been audited in any sense of the word. Use at your own risk.

Why Saber?

In general, if you are looking to use a post-quantum KEM and have no other requirements, you should use ML-KEM (aka "Kyber", its pre-standardization name), since it is faster and more standardized than Saber. However, Saber has two small benefits over Kyber:

  • All Saber public keys and ciphertexts pack perfectly into bytes. So if you need to perform a keyed permutation on a KEM's public keys, as is required in some ideal-cipher-based constructions such as CAKE, you can simply use a wide-block cipher over a serialized Saber public key. In comparison Kyber requires you to define a permutation over arrays of mod-q values (note: Kyber public keys actually can be compressed to pack into bytes, but nobody has proven it secure; Theorem 2 of the original paper only considers the uncompressed scheme).
  • Relatedly, all Saber arithmetic is modulo a power of two, which is extremely simple for CPUs to work with. Arithmetic modulo a prime can yield much faster computations, but it can also cause accidental timing leaks due to compilers being too smart. Such vulnerabilities have affected Kyber and curve25519. I don't claim any of these other projects are insecure, just that this is a specific issue they must contend with going forward, that Saber does not have to.

Compatibility

This crate is compatible with Saber's C reference implementation. Known-answer tests (KATs) test vectors can be found in tests/. Test vectors were taken directly from the previously linked repo, and converted to JSON using tests/convert_rsp_to_json.py.

Example code

The following code can be found in examples/simple.rs.

use saber_kem::{
    kem_traits::{Decapsulate, Encapsulate},
    lightsaber::{LightsaberCiphertext, LightsaberPublicKey, LightsaberSecretKey},
};

let mut rng = rand::thread_rng();

// Generate a keypair
let sk = LightsaberSecretKey::generate(&mut rng);
let pk = sk.public_key();

// Serialize the secret key, maybe to save on disk
let mut sk_bytes = [0u8; LightsaberSecretKey::SERIALIZED_LEN];
sk.to_bytes(&mut sk_bytes);

// Deserialize the secret key
let sk = LightsaberSecretKey::from_bytes(&sk_bytes);

// Also serialize and deserialize the public key
let mut pk_bytes = [0u8; LightsaberPublicKey::SERIALIZED_LEN];
pk.to_bytes(&mut pk_bytes);
let pk = LightsaberPublicKey::from_bytes(&pk_bytes);

// Encapsulate a shared secret, ss1, to pk
let (ct, ss1) = pk.encapsulate(&mut rng).unwrap();
// The ciphertext is just bytes, so serializing is straightforward
let ct_bytes = ct.as_ref();

// Deserializing is also straightforward
assert_eq!(ct_bytes.len(), LightsaberCiphertext::LEN);
let mut receiver_ct = LightsaberCiphertext::default();
receiver_ct.as_mut().copy_from_slice(ct_bytes);

// Use the secret key to decapsulate the ciphertext
let ss2 = sk.decapsulate(&receiver_ct).unwrap();

// Ensure the shared secrets are equal
assert_eq!(ss1, ss2);

println!("KEM ran successfully");

Benchmarks

We have implemented benchmarks for key generation, encapsulation, and decapsulation for all variants. Simply run cargo bench.

License

Licensed under either of

at your option.

About

A pure-Rust implementation of the Saber key encapsulation mechanism (KEM)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 98.6%
  • Python 1.4%