-
Notifications
You must be signed in to change notification settings - Fork 114
Upgrade rand dependency, use os_rng for seed generation
#683
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,7 +35,8 @@ use lightning::util::persist::{ | |
| }; | ||
| use lightning::util::ser::{Readable, ReadableArgs, Writeable}; | ||
| use lightning_types::string::PrintableString; | ||
| use rand::{thread_rng, RngCore}; | ||
| use rand::rngs::OsRng; | ||
| use rand::TryRngCore; | ||
|
|
||
| use super::*; | ||
| use crate::chain::ChainSource; | ||
|
|
@@ -63,7 +64,7 @@ pub const EXTERNAL_PATHFINDING_SCORES_CACHE_KEY: &str = "external_pathfinding_sc | |
| pub fn generate_entropy_mnemonic() -> Mnemonic { | ||
| // bip39::Mnemonic supports 256 bit entropy max | ||
| let mut entropy = [0; 32]; | ||
| thread_rng().fill_bytes(&mut entropy); | ||
| OsRng.try_fill_bytes(&mut entropy).expect("Failed to generate entropy"); | ||
| Mnemonic::from_entropy(&entropy).unwrap() | ||
| } | ||
|
|
||
|
|
@@ -96,7 +97,10 @@ where | |
| Ok(key) | ||
| } else { | ||
| let mut key = [0; WALLET_KEYS_SEED_LEN]; | ||
| thread_rng().fill_bytes(&mut key); | ||
| OsRng.try_fill_bytes(&mut key).map_err(|e| { | ||
| log_error!(logger, "Failed to generate entropy: {}", e); | ||
| std::io::Error::new(std::io::ErrorKind::Other, "Failed to generate seed bytes") | ||
| })?; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we propagate the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I am looking for consistency here, but maybe the difference is a public API function vs a function internal to the crate ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Well, we would need to introduce an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm ok would you consider panicking here ? sounds to me like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Well, the other path will happen on |
||
|
|
||
| if let Some(parent_dir) = Path::new(&keys_seed_path).parent() { | ||
| fs::create_dir_all(parent_dir).map_err(|e| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -592,7 +592,7 @@ pub(crate) struct RandEntropySource; | |
|
|
||
| impl EntropySource for RandEntropySource { | ||
| fn fill_bytes(&self, buffer: &mut [u8]) { | ||
| rand::thread_rng().fill_bytes(buffer); | ||
| rand::rng().fill_bytes(buffer); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the next commit, I would have used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am conscious of the performance tradeoff here as this is a frequent operation, maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's why I kept it with the thread RNG, which should be perfectly fine for cryptographic operations. I guess if we'd want to be super cautious we could look into periodic re-seeding via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looked into the current They do recommend to explicitly reseed on any fork, but I don't think we ever fork right ? If so I think I am good with the current choice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, was also tripping over that and considered reseeding on any new thread spawned by tokio. But, IIUC, that should happen automatically in the |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -604,8 +604,8 @@ impl RefUnwindSafe for VssStore {} | |
| mod tests { | ||
| use std::collections::HashMap; | ||
|
|
||
| use rand::distributions::Alphanumeric; | ||
| use rand::{thread_rng, Rng, RngCore}; | ||
| use rand::distr::Alphanumeric; | ||
| use rand::{rng, Rng, RngCore}; | ||
| use vss_client::headers::FixedHeaders; | ||
|
|
||
| use super::*; | ||
|
|
@@ -615,7 +615,7 @@ mod tests { | |
| #[test] | ||
| fn vss_read_write_remove_list_persist() { | ||
| let vss_base_url = std::env::var("TEST_VSS_BASE_URL").unwrap(); | ||
| let mut rng = thread_rng(); | ||
| let mut rng = rng(); | ||
| let rand_store_id: String = (0..7).map(|_| rng.sample(Alphanumeric) as char).collect(); | ||
| let mut vss_seed = [0u8; 32]; | ||
| rng.fill_bytes(&mut vss_seed); | ||
|
|
@@ -631,7 +631,7 @@ mod tests { | |
| #[tokio::test(flavor = "multi_thread", worker_threads = 1)] | ||
| async fn vss_read_write_remove_list_persist_in_runtime_context() { | ||
| let vss_base_url = std::env::var("TEST_VSS_BASE_URL").unwrap(); | ||
| let mut rng = thread_rng(); | ||
| let mut rng = rng(); | ||
| let rand_store_id: String = (0..7).map(|_| rng.sample(Alphanumeric) as char).collect(); | ||
| let mut vss_seed = [0u8; 32]; | ||
| rng.fill_bytes(&mut vss_seed); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.