-
Notifications
You must be signed in to change notification settings - Fork 432
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
Improve doc on implementing Default
and Clone
#383
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9c90927
Improve doc on implementing `Default`
pitdicker 807c0c7
Make JitterRng clone
pitdicker 7be8cd5
Implement Clone for ReseedingRng
pitdicker 7230dee
Reset data_half_used on JitterRng::clone()
pitdicker 6060f1a
Test reseeding on clone
pitdicker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ use rand_core::impls::BlockRng; | |
/// A wrapper around any PRNG which reseeds the underlying PRNG after it has | ||
/// generated a certain number of random bytes. | ||
/// | ||
/// When the RNG gets cloned, the clone is reseeded on first use. | ||
/// | ||
/// Reseeding is never strictly *necessary*. Cryptographic PRNGs don't have a | ||
/// limited number of bytes they can output, or at least not a limit reachable | ||
/// in any practical way. There is no such thing as 'running out of entropy'. | ||
|
@@ -103,6 +105,17 @@ where R: BlockRngCore<Item = u32> + SeedableRng, | |
} | ||
} | ||
|
||
impl<R, Rsdr> Clone for ReseedingRng<R, Rsdr> | ||
where R: BlockRngCore + SeedableRng + Clone, | ||
Rsdr: RngCore + Clone | ||
{ | ||
fn clone(&self) -> ReseedingRng<R, Rsdr> { | ||
// Recreating `BlockRng` seems easier than cloning it and resetting | ||
// the index. | ||
ReseedingRng(BlockRng::new(self.0.inner().clone())) | ||
} | ||
} | ||
|
||
impl<R, Rsdr> CryptoRng for ReseedingRng<R, Rsdr> | ||
where R: BlockRngCore + SeedableRng + CryptoRng, | ||
Rsdr: RngCore + CryptoRng {} | ||
|
@@ -189,6 +202,20 @@ where R: BlockRngCore + SeedableRng, | |
} | ||
} | ||
|
||
impl<R, Rsdr> Clone for ReseedingCore<R, Rsdr> | ||
where R: BlockRngCore + SeedableRng + Clone, | ||
Rsdr: RngCore + Clone | ||
{ | ||
fn clone(&self) -> ReseedingCore<R, Rsdr> { | ||
ReseedingCore { | ||
inner: self.inner.clone(), | ||
reseeder: self.reseeder.clone(), | ||
threshold: self.threshold, | ||
bytes_until_reseed: 0, // reseed clone on first use | ||
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. Nice way of forcing a reseed! |
||
} | ||
} | ||
} | ||
|
||
impl<R, Rsdr> CryptoRng for ReseedingCore<R, Rsdr> | ||
where R: BlockRngCore + SeedableRng + CryptoRng, | ||
Rsdr: RngCore + CryptoRng {} | ||
|
@@ -217,4 +244,17 @@ mod test { | |
assert_eq!(buf, seq); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_clone_reseeding() { | ||
let mut zero = StepRng::new(0, 0); | ||
let rng = ChaChaCore::from_rng(&mut zero).unwrap(); | ||
let mut rng1 = ReseedingRng::new(rng, 32*4, zero); | ||
|
||
let first: u32 = rng1.gen(); | ||
for _ in 0..10 { let _ = rng1.gen::<u32>(); } | ||
|
||
let mut rng2 = rng1.clone(); | ||
assert_eq!(first, rng2.gen::<u32>()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think
mem_prev_index
should be adjusted?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we are okay leaving it as is. They probably have different locations for the memory slice on the stack. Having the same location twice should not make the CPU much better at predicting. And after the first round the values will diverge, as
memaccess
is called a variable number of times.