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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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 @@ -83,7 +83,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 @@ -93,8 +93,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
4 changes: 2 additions & 2 deletions benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ 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"] }
libcrux-kem = { path = "../libcrux-kem", features = ["tests"] }
libcrux-ml-kem = { path = "../libcrux-ml-kem" }
rand_core = { version = "0.6" }
rand_core = { version = "0.9" }
# Benchmarking "RustCrypto"
chacha20poly1305 = "0.10"
sha2 = "0.10"
Expand Down
2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"
rand = { version = "0.8" }
rand = { version = "0.9" }

[dependencies.libcrux]
path = "../"
Expand Down
5 changes: 0 additions & 5 deletions fuzz/fuzz_targets/fuzz_rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ impl RngCore for FuzzRng {
dest.copy_from_slice(&self.data[0..dest.len()]);
self.data = self.data.drain(dest.len()..).collect();
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
self.fill_bytes(dest);
Ok(())
}
}

impl CryptoRng for FuzzRng {}
4 changes: 2 additions & 2 deletions libcrux-ecdh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ description = "Libcrux ECDH implementation"
path = "src/ecdh.rs"

[dependencies]
rand = { version = "0.8" }
rand = { version = "0.9" }
libcrux-hacl = { version = "=0.0.2-beta.2", path = "../sys/hacl" }
libcrux-curve25519 = { version = "=0.0.2-beta.2", path = "../curve25519" }

[dev-dependencies]
rand_core = { version = "0.6" }
rand_core = { version = "0.9" }
hex = { version = "0.4.3", features = ["serde"] }
serde_json = { version = "1.0" }
serde = { version = "1.0", features = ["derive"] }
Expand Down
4 changes: 2 additions & 2 deletions libcrux-ecdh/src/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub enum Algorithm {

pub(crate) mod x25519_internal {
use alloc::format;
use rand::{CryptoRng, Rng};
use rand::{CryptoRng, Rng, TryRngCore};

use super::Error;

Expand Down Expand Up @@ -257,7 +257,7 @@ pub mod curve25519 {

pub(crate) mod p256_internal {
use alloc::format;
use rand::{CryptoRng, Rng};
use rand::{CryptoRng, Rng, TryRngCore};

// P256 we only have in HACL
use crate::hacl::p256;
Expand Down
2 changes: 1 addition & 1 deletion libcrux-kem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ path = "src/kem.rs"
libcrux-ml-kem = { version = "=0.0.2-beta.2", path = "../libcrux-ml-kem" }
libcrux-sha3 = { version = "=0.0.2-beta.2", path = "../libcrux-sha3" }
libcrux-ecdh = { version = "=0.0.2-beta.2", path = "../libcrux-ecdh" }
rand = { version = "0.8" }
rand = { version = "0.9" }

[features]
tests = [] # Expose functions for testing.
Expand Down
2 changes: 1 addition & 1 deletion libcrux-kem/src/kem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extern crate alloc;

use alloc::{vec, vec::Vec};

use rand::{CryptoRng, Rng};
use rand::{CryptoRng, Rng, TryRngCore};

use libcrux_ecdh::{p256_derive, x25519_derive};
use libcrux_ecdh::{
Expand Down
2 changes: 1 addition & 1 deletion libcrux-ml-dsa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ libcrux-macros = { version = "0.0.2-beta.2", path = "../macros" }
hax-lib = { version = "0.1.0", git = "https://github.com/hacspec/hax/"}

[dev-dependencies]
rand = { version = "0.8" }
rand = { version = "0.9" }
hex = { version = "0.4.3", features = ["serde"] }
serde_json = { version = "1.0" }
serde = { version = "1.0", features = ["derive"] }
Expand Down
4 changes: 2 additions & 2 deletions libcrux-ml-kem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exclude = [
bench = false # so libtest doesn't eat the arguments to criterion

[dependencies]
rand = { version = "0.8", optional = true }
rand = { version = "0.9", optional = true }
libcrux-platform = { version = "0.0.2-beta.2", path = "../sys/platform" }
libcrux-sha3 = { version = "0.0.2-beta.2", path = "../libcrux-sha3" }
libcrux-intrinsics = { version = "0.0.2-beta.2", path = "../libcrux-intrinsics" }
Expand Down Expand Up @@ -53,7 +53,7 @@ rand = ["dep:rand"]
std = []

[dev-dependencies]
rand = { version = "0.8" }
rand = { version = "0.9" }
serde_json = { version = "1.0" }
serde = { version = "1.0", features = ["derive"] }
hex = { version = "0.4.3", features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion libcrux-psq/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ classic-mceliece-rust = { version = "2.0.0", features = [
"mceliece460896f",
"zeroize",
] }
rand = { version = "0.8" }
rand = { version = "0.9" }
libcrux-ecdh = { version = "0.0.2-beta.2", path = "../libcrux-ecdh" }
libcrux = { version = "0.0.2-beta.2", path = ".." }

Expand Down
2 changes: 1 addition & 1 deletion libcrux-sha3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ harness = false
[dev-dependencies]
criterion = "0.5.1"
hex = "0.4.3"
rand = "0.8.5"
rand = "0.9.0"
cavp = { version = "0.0.2-beta.2", path = "../cavp" }
pretty_env_logger = "0.5.0"

Expand Down
16 changes: 9 additions & 7 deletions src/drbg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use crate::hacl::drbg;
// re-export here for convenience
pub use rand::{CryptoRng, RngCore};
pub use rand::{CryptoRng, RngCore, TryRngCore};

use crate::std::{fmt, vec, vec::Vec};

Expand All @@ -16,6 +16,8 @@ pub enum Error {
UnsupportedAlgorithm,
/// Unable to generate the requested randomness.
UnableToGenerate,
/// Not enough OS randomness available, e.g. to initialize or reseed
InsufficientOSRandomness,
}

impl fmt::Display for Error {
Expand Down Expand Up @@ -44,7 +46,9 @@ impl Drbg {
#[cfg(feature = "rand")]
pub fn new(alg: super::digest::Algorithm) -> Result<Self, Error> {
let mut entropy = [0u8; 16];
rand::rngs::OsRng.fill_bytes(&mut entropy);
rand::rngs::OsRng
.try_fill_bytes(&mut entropy)
.map_err(|_| Error::InsufficientOSRandomness)?;
Self::personalized(alg, &entropy, &[], "os seeded libcrux")
}

Expand Down Expand Up @@ -88,7 +92,9 @@ impl Drbg {
fn auto_reseed(&mut self) -> Result<(), Error> {
if self.ctr > 512 {
let mut entropy = [0u8; 16];
rand::rngs::OsRng.fill_bytes(&mut entropy);
rand::rngs::OsRng
.try_fill_bytes(&mut entropy)
.map_err(|_| Error::InsufficientOSRandomness)?;
self.reseed(&entropy, b"reseed")?;
self.ctr = 0;
} else {
Expand Down Expand Up @@ -193,10 +199,6 @@ impl RngCore for Drbg {
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.generate(dest).unwrap()
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
self.generate(dest).map_err(rand::Error::new)
}
}

impl CryptoRng for Drbg {}
2 changes: 1 addition & 1 deletion src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
ecdh,
hacl::{self, ed25519},
};
use rand::{CryptoRng, Rng, RngCore};
use rand::{CryptoRng, Rng, RngCore, TryRngCore};

use self::rsa_pss::RsaPssSignature;

Expand Down
7 changes: 1 addition & 6 deletions tests/xwing_kem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use libcrux::{

#[cfg(not(target_arch = "wasm32"))]
use libcrux::drbg;
use rand::{CryptoRng, Error};
use rand::CryptoRng;
#[cfg(target_arch = "wasm32")]
use rand_core::OsRng;
use rand_core::RngCore;
Expand Down Expand Up @@ -251,11 +251,6 @@ impl RngCore for DeterministicRng {
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.fill_with_data(dest);
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.fill_with_data(dest);
Ok(())
}
}

impl CryptoRng for DeterministicRng {}
Loading