Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update rand and rand_core #794

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ libcrux-ed25519 = { version = "=0.0.2-beta.2", path = "ed25519" }
libcrux-ecdh = { version = "=0.0.2-beta.2", path = "libcrux-ecdh" }
libcrux-ml-kem = { version = "=0.0.2-beta.2", path = "libcrux-ml-kem" }
libcrux-kem = { version = "=0.0.2-beta.2", path = "libcrux-kem" }
rand = { version = "0.8" }
rand = { version = "0.9" }
log = { version = "0.4", optional = true }
# WASM API
wasm-bindgen = { version = "0.2.87", optional = true }
Expand All @@ -95,8 +95,8 @@ hax-lib = { version = "0.1.0", git = "https://github.com/hacspec/hax/" }
[dev-dependencies]
libcrux = { path = ".", features = ["rand", "tests"] }
pretty_env_logger = "0.5"
rand = { version = "0.8" }
rand_core = { version = "0.6" }
rand = { version = "0.9" }
rand_core = { version = "0.9" }
quickcheck = "1"
quickcheck_macros = "1"
serde_json = { version = "1.0" }
Expand Down
5 changes: 3 additions & 2 deletions benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ publish = false
bench = false # so libtest doesn't eat the arguments for criterion

[dependencies]
rand = { version = "0.8" }
rand = { version = "0.9" }

[dev-dependencies]
libcrux = { path = "../", features = ["rand", "tests"] }
Expand All @@ -22,9 +22,10 @@ libcrux-ecdh = { path = "../libcrux-ecdh" }
libcrux-ed25519 = { path = "../ed25519" }
libcrux-kem = { path = "../libcrux-kem", features = ["tests"] }
libcrux-ml-kem = { path = "../libcrux-ml-kem" }
rand_core_old = { version = "0.6", package = "rand_core" }
libcrux-sha2 = { path = "../sha2" }
libcrux-rsa = { path = "../rsa" }
rand_core = { version = "0.6" }
rand_core = { version = "0.9" }
# Benchmarking "RustCrypto"
chacha20poly1305 = "0.10"
sha2 = "0.10"
Expand Down
9 changes: 6 additions & 3 deletions benchmarks/benches/chacha20poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use libcrux::{digest, drbg};
use libcrux_chacha20poly1305::*;

use benchmarks::util::*;
use rand_core::OsRng;
use ring::aead::UnboundKey;

fn randbuf<const LEN: usize>(drbg: &mut drbg::Drbg) -> Result<[u8; LEN], drbg::Error> {
Expand Down Expand Up @@ -74,11 +73,13 @@ fn comparisons_encrypt(c: &mut Criterion) {
BenchmarkId::new("RustCrypto", fmt(*payload_size)),
payload_size,
|b, payload_size| {
use rand_core_old::OsRng;
b.iter_batched(
|| {
let key = chacha20poly1305::ChaCha20Poly1305::generate_key(&mut OsRng);
let mut rng = OsRng;
let key = chacha20poly1305::ChaCha20Poly1305::generate_key(&mut rng);
let cipher = chacha20poly1305::ChaCha20Poly1305::new(&key);
let nonce = chacha20poly1305::ChaCha20Poly1305::generate_nonce(&mut OsRng); // 96-bits; unique per message
let nonce = chacha20poly1305::ChaCha20Poly1305::generate_nonce(&mut rng); // 96-bits; unique per message
let aad = randombytes(1_000);
let data = randombytes(*payload_size);
(data, cipher, nonce, aad)
Expand Down Expand Up @@ -200,6 +201,8 @@ fn comparisons_decrypt(c: &mut Criterion) {
BenchmarkId::new("RustCrypto", fmt(*payload_size)),
payload_size,
|b, payload_size| {
// Using older version of traits as required by library
use rand_core_old::OsRng;
b.iter_batched(
|| {
let key = chacha20poly1305::ChaCha20Poly1305::generate_key(&mut OsRng);
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/benches/drbg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn generate(c: &mut Criterion) {
|| {},
|()| {
let mut random_bytes = vec![0u8; 32];
OsRng.fill_bytes(&mut random_bytes);
OsRng.try_fill_bytes(&mut random_bytes).unwrap();
},
BatchSize::SmallInput,
)
Expand Down
7 changes: 5 additions & 2 deletions benchmarks/benches/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ fn sign(c: &mut Criterion) {
});
group.bench_function("Dalek", |b| {
use ed25519_dalek::{Signer, SigningKey};
use rand_core::OsRng;
// Using older version of `rand_core` traits as required by `ed25519_dalek`
use rand_core_old::OsRng;

b.iter_batched(
|| {
Expand Down Expand Up @@ -170,7 +171,9 @@ fn verify(c: &mut Criterion) {
});
group.bench_function("Dalek", |b| {
use ed25519_dalek::{Signer, SigningKey};
use rand_core::OsRng;

// Using older version of `rand_core` traits as required by `ed25519_dalek`
use rand_core_old::OsRng;

b.iter_batched(
|| {
Expand Down
20 changes: 10 additions & 10 deletions benchmarks/benches/hpke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use libcrux::hpke::aead::*;
use libcrux::hpke::kdf::KDF;
use libcrux::hpke::kem::{GenerateKeyPair, KEM};
use libcrux::{aes_ni_support, hpke::*};
use rand::{rngs::OsRng, RngCore};
use rand::{rngs::OsRng, TryRngCore};

pub(crate) fn hex_str_to_bytes(val: &str) -> Vec<u8> {
let b: Result<Vec<u8>, ParseIntError> = (0..val.len())
Expand Down Expand Up @@ -56,10 +56,10 @@ fn benchmark() {
println!("{}", label);

let mut randomness = [0u8; 32];
OsRng.fill_bytes(&mut randomness);
OsRng.try_fill_bytes(&mut randomness).unwrap();
let (_sk, enc) = GenerateKeyPair(kem_mode, randomness.to_vec()).unwrap();
let mut randomness = [0u8; 32];
OsRng.fill_bytes(&mut randomness);
OsRng.try_fill_bytes(&mut randomness).unwrap();
let (sk_rm, pk_rm) = GenerateKeyPair(kem_mode, randomness.to_vec()).unwrap();
let info = hex_str_to_bytes("4f6465206f6e2061204772656369616e2055726e");
let psk = if hpke_mode == Mode::mode_auth_psk || hpke_mode == Mode::mode_psk {
Expand All @@ -76,7 +76,7 @@ fn benchmark() {
let (pk_sm, sk_sm) =
if hpke_mode == Mode::mode_auth_psk || hpke_mode == Mode::mode_auth {
let mut randomness = [0u8; 32];
OsRng.fill_bytes(&mut randomness);
OsRng.try_fill_bytes(&mut randomness).unwrap();
let (sk, pk) = GenerateKeyPair(kem_mode, randomness.to_vec()).unwrap();
(Some(pk), Some(sk))
} else {
Expand All @@ -86,7 +86,7 @@ fn benchmark() {
let config = HPKEConfig(hpke_mode, kem_mode, kdf_mode, aead_mode);

let mut randomness = [0u8; 32];
OsRng.fill_bytes(&mut randomness);
OsRng.try_fill_bytes(&mut randomness).unwrap();
let randomness = randomness.to_vec();
let start = Instant::now();
for _ in 0..ITERATIONS {
Expand Down Expand Up @@ -186,10 +186,10 @@ fn benchmark() {
.unwrap();

let mut aad = vec![0u8; AEAD_AAD];
OsRng.fill_bytes(&mut aad);
OsRng.try_fill_bytes(&mut aad).unwrap();
let aad = aad.to_vec();
let mut ptxt = vec![0u8; AEAD_PAYLOAD];
OsRng.fill_bytes(&mut ptxt);
OsRng.try_fill_bytes(&mut ptxt).unwrap();
let ptxt = ptxt.to_vec();

let mut ctxts = Vec::with_capacity((AEAD_PAYLOAD + 16) * ITERATIONS);
Expand Down Expand Up @@ -253,13 +253,13 @@ fn benchmark() {
assert_eq!(ptxts[0], ptxt);

let mut aad = vec![0u8; AEAD_AAD];
OsRng.fill_bytes(&mut aad);
OsRng.try_fill_bytes(&mut aad).unwrap();
let aad = aad.to_vec();
let mut ptxt = vec![0u8; AEAD_PAYLOAD];
OsRng.fill_bytes(&mut ptxt);
OsRng.try_fill_bytes(&mut ptxt).unwrap();
let ptxt = ptxt.to_vec();
let mut randomness = [0u8; 32];
OsRng.fill_bytes(&mut randomness);
OsRng.try_fill_bytes(&mut randomness).unwrap();
let randomness = randomness.to_vec();

let mut ctxt = HPKECiphertext(vec![], vec![]);
Expand Down
37 changes: 20 additions & 17 deletions benchmarks/benches/kyber768.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use criterion::{criterion_group, criterion_main, BatchSize, Criterion};
use libcrux::digest;
use libcrux::drbg::Drbg;
use libcrux::kem::Algorithm;
use rand_core::OsRng;
use rand_core::RngCore;
use rand_core::{OsRng, RngCore, TryRngCore};

pub fn comparisons_key_generation(c: &mut Criterion) {
let mut drbg = Drbg::new(digest::Algorithm::Sha256).unwrap();
let mut rng = OsRng;
let mut os_rng = OsRng;
let mut rng = os_rng.unwrap_mut();
let mut group = c.benchmark_group("Kyber768 Key Generation");
group.measurement_time(Duration::from_secs(10));

Expand All @@ -26,7 +26,7 @@ pub fn comparisons_key_generation(c: &mut Criterion) {
// group.bench_function("libcrux portable unpacked (external random)", |b| {
// b.iter(|| {
// let mut seed = [0; 64];
// rng.fill_bytes(&mut seed);
// rng.try_fill_bytes(&mut seed).unwrap();
// let _tuple = libcrux_ml_kem::mlkem768::generate_key_pair_unpacked(seed);
// })
// });
Expand Down Expand Up @@ -55,7 +55,7 @@ pub fn comparisons_key_generation(c: &mut Criterion) {
group.bench_function("libjade kyber avx2", |b| {
b.iter(|| {
let mut seed = [0; 64];
rng.fill_bytes(&mut seed);
rng.try_fill_bytes(&mut seed).unwrap();
let mut public_key = [0u8; 1184];
let mut secret_key = [0u8; 2400];
unsafe {
Expand All @@ -76,7 +76,7 @@ pub fn comparisons_pk_validation(c: &mut Criterion) {

group.bench_function("libcrux portable", |b| {
let mut seed = [0; 64];
rng.fill_bytes(&mut seed);
rng.try_fill_bytes(&mut seed).unwrap();
b.iter_batched(
|| libcrux_kem::deterministic::mlkem768_generate_keypair_derand(seed),
|key_pair| {
Expand All @@ -93,9 +93,9 @@ pub fn comparisons_encapsulation(c: &mut Criterion) {

group.bench_function("libcrux portable (external random)", |b| {
let mut seed1 = [0; 64];
OsRng.fill_bytes(&mut seed1);
OsRng.try_fill_bytes(&mut seed1).unwrap();
let mut seed2 = [0; 32];
OsRng.fill_bytes(&mut seed2);
OsRng.try_fill_bytes(&mut seed2).unwrap();
b.iter_batched(
|| libcrux_kem::deterministic::mlkem768_generate_keypair_derand(seed1),
|keypair| {
Expand Down Expand Up @@ -128,13 +128,16 @@ pub fn comparisons_encapsulation(c: &mut Criterion) {
group.bench_function("libcrux portable OsRng", |b| {
b.iter_batched(
|| {
let mut drbg = OsRng;
let mut os_rng = OsRng;
let mut drbg = os_rng.unwrap_mut();
let (_secret_key, public_key) =
libcrux_kem::key_gen(Algorithm::MlKem768, &mut drbg).unwrap();

(drbg, public_key)
public_key
},
|(mut rng, public_key)| {
|public_key| {
let mut os_rng = OsRng;
let mut rng = os_rng.unwrap_mut();
let (_shared_secret, _ciphertext) = public_key.encapsulate(&mut rng).unwrap();
},
BatchSize::SmallInput,
Expand Down Expand Up @@ -162,7 +165,7 @@ pub fn comparisons_encapsulation(c: &mut Criterion) {
|| {
let mut rng = OsRng;
let mut seed = [0; 64];
rng.fill_bytes(&mut seed);
rng.try_fill_bytes(&mut seed).unwrap();
let mut public_key = [0u8; 1184];
let mut secret_key = [0u8; 2400];
unsafe {
Expand All @@ -177,7 +180,7 @@ pub fn comparisons_encapsulation(c: &mut Criterion) {
},
|(mut rng, public_key)| {
let mut seed = [0; 32];
rng.fill_bytes(&mut seed);
rng.try_fill_bytes(&mut seed).unwrap();

let mut ciphertext = [0u8; 1088];
let mut shared_secret = [0u8; 32];
Expand Down Expand Up @@ -221,11 +224,11 @@ pub fn comparisons_decapsulation(c: &mut Criterion) {
// b.iter_batched(
// || {
// let mut seed = [0; 64];
// OsRng.fill_bytes(&mut seed);
// OsRng.try_fill_bytes(&mut seed).unwrap();
// let (sk_state, pubkey) = libcrux_ml_kem::mlkem768::generate_key_pair_unpacked(seed);

// let mut rand = [0; 32];
// OsRng.fill_bytes(&mut rand);
// OsRng.try_fill_bytes(&mut rand).unwrap();
// let (ciphertext, _) = libcrux_ml_kem::mlkem768::encapsulate(&pubkey, rand);
// (sk_state, ciphertext)
// },
Expand Down Expand Up @@ -260,7 +263,7 @@ pub fn comparisons_decapsulation(c: &mut Criterion) {
|| {
let mut rng = OsRng;
let mut seed = [0; 64];
rng.fill_bytes(&mut seed);
rng.try_fill_bytes(&mut seed).unwrap();
let mut public_key = [0u8; 1184];
let mut secret_key = [0u8; 2400];
unsafe {
Expand All @@ -272,7 +275,7 @@ pub fn comparisons_decapsulation(c: &mut Criterion) {
};

let mut seed = [0; 32];
rng.fill_bytes(&mut seed);
rng.try_fill_bytes(&mut seed).unwrap();

let mut ciphertext = [0u8; 1088];
let mut shared_secret = [0u8; 32];
Expand Down
20 changes: 15 additions & 5 deletions benchmarks/benches/p256.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use criterion::{criterion_group, criterion_main, BatchSize, Criterion};

use rand_core::OsRng;

fn derive(c: &mut Criterion) {
// Comparing libcrux performance for different payload sizes and other implementations.
let mut group = c.benchmark_group("P256 derive");

group.bench_function("libcrux", |b| {
b.iter_batched(
|| {
use rand_core::{OsRng, TryRngCore};
let mut os_rng = OsRng;
let mut rng = os_rng.unwrap_mut();
let (_, pk1) =
libcrux_ecdh::key_gen(libcrux_ecdh::Algorithm::P256, &mut OsRng).unwrap();
libcrux_ecdh::key_gen(libcrux_ecdh::Algorithm::P256, &mut rng).unwrap();
let (sk2, _) =
libcrux_ecdh::key_gen(libcrux_ecdh::Algorithm::P256, &mut OsRng).unwrap();
libcrux_ecdh::key_gen(libcrux_ecdh::Algorithm::P256, &mut rng).unwrap();
(pk1, sk2)
},
|(pk1, sk2)| {
Expand Down Expand Up @@ -51,6 +52,9 @@ fn derive(c: &mut Criterion) {
group.bench_function("RustCrypto", |b| {
use p256::{ecdh::EphemeralSecret, PublicKey};

// Using older version of `rand_core` traits as required by `p256`
use rand_core_old::OsRng;

b.iter_batched(
|| {
let sk1 = EphemeralSecret::random(&mut OsRng);
Expand All @@ -69,12 +73,15 @@ fn derive(c: &mut Criterion) {
fn secret_to_public(c: &mut Criterion) {
// Comparing libcrux performance for different payload sizes and other implementations.
let mut group = c.benchmark_group("P256 secret to public");
use rand_core::{OsRng, TryRngCore};
let mut os_rng = OsRng;
let mut rng = os_rng.unwrap_mut();

group.bench_function("libcrux", |b| {
b.iter_batched(
|| {
let (sk, _) =
libcrux_ecdh::key_gen(libcrux_ecdh::Algorithm::P256, &mut OsRng).unwrap();
libcrux_ecdh::key_gen(libcrux_ecdh::Algorithm::P256, &mut rng).unwrap();
sk
},
|sk| {
Expand Down Expand Up @@ -106,6 +113,9 @@ fn secret_to_public(c: &mut Criterion) {
group.bench_function("RustCrypto", |b| {
use p256::{ecdh::EphemeralSecret, PublicKey};

// Using older version of `rand_core` traits as required by `p256`
use rand_core_old::OsRng;

b.iter_batched(
|| {
let sk = EphemeralSecret::random(&mut OsRng);
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/benches/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use criterion::{criterion_group, criterion_main, BatchSize, Criterion};

use der::Decode as _;
use pkcs1::DecodeRsaPrivateKey;
use rand_core::OsRng;
use rand_core_old::OsRng;
use ring::signature::KeyPair;

const MSG_STR: &str = "the test message is 32 byte long";
Expand Down
Loading
Loading