Skip to content

Commit 5f585f6

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 73da722 commit 5f585f6

File tree

5 files changed

+70
-0
lines changed

5 files changed

+70
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ members = [
1111
"lightning-rapid-gossip-sync",
1212
"lightning-custom-message",
1313
"lightning-transaction-sync",
14+
"possiblyrandom",
1415
]
1516

1617
exclude = [
@@ -38,3 +39,6 @@ lto = "off"
3839
opt-level = 3
3940
lto = true
4041
panic = "abort"
42+
43+
[patch.crates-io.possiblyrandom]
44+
path = "possiblyrandom"

ci/check-cfg-flags.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ def check_feature(feature):
1515
pass
1616
elif feature == "ahash":
1717
pass
18+
elif feature == "getrandom":
19+
pass
1820
elif feature == "hashbrown":
1921
pass
2022
elif feature == "backtrace":

no-std-check/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ lightning-background-processor = { path = "../lightning-background-processor", f
1515
# Obviously lightning-transaction-sync doesn't support no-std, but it should build
1616
# even if lightning is built with no-std.
1717
lightning-transaction-sync = { path = "../lightning-transaction-sync", optional = true }
18+
19+
[patch.crates-io]
20+
possiblyrandom = { path = "../possiblyrandom" }

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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
#![no_std]
22+
23+
#[cfg(feature = "getrandom")]
24+
extern crate getrandom;
25+
26+
/// Possibly fills `dest` with random data. May fill it with zeros.
27+
#[cfg(feature = "getrandom")]
28+
#[inline]
29+
pub fn getpossiblyrandom(dest: &mut [u8]) {
30+
if getrandom::getrandom(dest).is_err() {
31+
dest.fill(0);
32+
}
33+
}
34+
35+
/// Possibly fills `dest` with random data. May fill it with zeros.
36+
#[cfg(not(feature = "getrandom"))]
37+
#[inline]
38+
pub fn getpossiblyrandom(dest: &mut [u8]) {
39+
dest.fill(0);
40+
}

0 commit comments

Comments
 (0)