Skip to content

Commit 6181758

Browse files
committed
Add a crate which wraps getrandom but always compiles
In the next commit we'll drop the `ahash` dependency in favor of directly calling `getrandom` to seed our hash tables. However, we'd like to depend on `getrandom` only on certain platforms *and* only when certain features (no-std) are set. This introduces an indirection crate to do so, allowing us to depend on it only when `no-std` is set but only depending on `getrandom` on platforms which it supports.
1 parent 0c2a715 commit 6181758

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ members = [
1010
"lightning-background-processor",
1111
"lightning-rapid-gossip-sync",
1212
"lightning-custom-message",
13+
"possiblyrandom",
1314
]
1415

1516
exclude = [

possiblyrandom/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "possiblyrandom"
3+
version = "0.1.0"
4+
authors = ["Matt Corallo"]
5+
license = "MIT OR Apache-2.0"
6+
repository = "https://github.com/lightningdevkit/rust-lightning/"
7+
description = """
8+
A crate which wraps getrandom and always compiles, returning 0s when no randomness is available.
9+
"""
10+
edition = "2021"
11+
12+
[package.metadata.docs.rs]
13+
all-features = true
14+
rustdoc-args = ["--cfg", "docsrs"]
15+
16+
[dependencies]
17+
getrandom = { version = "0.2", optional = true, default-features = false }
18+
19+
# Enable getrandom if we are on a platform that (likely) supports it
20+
[target.'cfg(not(any(target_os = "unknown", target_os = "none")))'.dependencies]
21+
getrandom = { version = "0.2", default-features = false }

possiblyrandom/src/lib.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
//! [`getrandom`] provides access to OS randomness, but will fail to compile on platforms which do
11+
//! not support fetching OS randomness. This is exactly what you want when you're doing
12+
//! cryptographic operations, but when you're just opportunistically randomizing, we're fine with
13+
//! compiling and simply disabling randomization.
14+
//!
15+
//! This crate does that, returning only possibly-random data.
16+
//!
17+
//! Note that this crate only enables getrandom on a subset of platforms it supports. As getrandom
18+
//! evolves this crate is unlikely to carefully track all getrandom-supported platforms, however
19+
//! will use random data on popular platforms.
20+
21+
#[cfg(feature = "getrandom")]
22+
extern crate getrandom;
23+
24+
/// Possibly fills `dest` with random data. May fill it with zeros.
25+
#[cfg(feature = "getrandom")]
26+
#[inline]
27+
pub fn getpossiblyrandom(dest: &mut [u8]) {
28+
if getrandom::getrandom(dest).is_err() {
29+
dest.fill(0);
30+
}
31+
}
32+
33+
/// Possibly fills `dest` with random data. May fill it with zeros.
34+
#[cfg(not(feature = "getrandom"))]
35+
#[inline]
36+
pub fn getpossiblyrandom(dest: &mut [u8]) {
37+
dest.fill(0);
38+
}

0 commit comments

Comments
 (0)