Use a deterministic PRNG seeded from arb data for proptests.

This commit is contained in:
Kris Nuttycombe 2021-04-29 09:55:11 -06:00
parent f91088d35b
commit 3c12877f87
1 changed files with 13 additions and 9 deletions

View File

@ -459,7 +459,7 @@ impl<V> Bundle<PartiallyAuthorized, V> {
/// Generators for property testing. /// Generators for property testing.
#[cfg(any(test, feature = "test-dependencies"))] #[cfg(any(test, feature = "test-dependencies"))]
pub mod testing { pub mod testing {
use rand::rngs::OsRng; use rand::{rngs::StdRng, CryptoRng, SeedableRng};
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fmt::Debug; use std::fmt::Debug;
@ -494,16 +494,18 @@ pub mod testing {
/// from these inputs, but using a `ValueBalance` implementation that /// from these inputs, but using a `ValueBalance` implementation that
/// is defined by the end user. /// is defined by the end user.
#[derive(Debug)] #[derive(Debug)]
struct ArbitraryBundleInputs { struct ArbitraryBundleInputs<R, R7> {
rng: R,
rng_7: R7,
sk: SpendingKey, sk: SpendingKey,
anchor: Anchor, anchor: Anchor,
notes: Vec<Note>, notes: Vec<Note>,
recipient_amounts: Vec<(Address, NoteValue)>, recipient_amounts: Vec<(Address, NoteValue)>,
} }
impl ArbitraryBundleInputs { impl<R: RngCore + CryptoRng, R7: rand_7::RngCore + rand_7::CryptoRng> ArbitraryBundleInputs<R, R7> {
/// Create a bundle from the set of arbitrary bundle inputs. /// Create a bundle from the set of arbitrary bundle inputs.
fn into_bundle<V: TryFrom<i64>>(self) -> Bundle<Authorized, V> { fn into_bundle<V: TryFrom<i64>>(mut self) -> Bundle<Authorized, V> {
let fvk = FullViewingKey::from(&self.sk); let fvk = FullViewingKey::from(&self.sk);
let ovk = OutgoingViewingKey::from(&fvk); let ovk = OutgoingViewingKey::from(&fvk);
let flags = Flags::from_parts(true, true); let flags = Flags::from_parts(true, true);
@ -519,13 +521,12 @@ pub mod testing {
.unwrap(); .unwrap();
} }
let mut rng = OsRng;
let pk = ProvingKey::build(); let pk = ProvingKey::build();
builder builder
.build(&mut rng, &pk) .build(&mut self.rng, &pk)
.unwrap() .unwrap()
.prepare(rand_7::rngs::OsRng, [0; 32]) .prepare(&mut self.rng_7, [0; 32])
.sign(rand_7::rngs::OsRng, &SpendAuthorizingKey::from(&self.sk)) .sign(&mut self.rng_7, &SpendAuthorizingKey::from(&self.sk))
.finalize() .finalize()
.unwrap() .unwrap()
} }
@ -543,8 +544,11 @@ pub mod testing {
), ),
1..30 1..30
), ),
) -> ArbitraryBundleInputs { rng_seed in prop::array::uniform32(prop::num::u8::ANY)
) -> ArbitraryBundleInputs<StdRng, rand_7::rngs::StdRng> {
ArbitraryBundleInputs { ArbitraryBundleInputs {
rng: StdRng::from_seed(rng_seed),
rng_7: <rand_7::rngs::StdRng as rand_7::SeedableRng>::from_seed(rng_seed),
sk: sk.clone(), sk: sk.clone(),
anchor, anchor,
notes, notes,