Merge pull request #131 from nuttycom/node_random_dist

Add `Node::random` and `Distribution<Node> for Standard` for testing.
This commit is contained in:
Kris Nuttycombe 2024-03-18 08:53:34 -06:00 committed by GitHub
commit 22412ae076
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 15 additions and 0 deletions

View File

@ -171,7 +171,9 @@ impl From<Node> for bls12_381::Scalar {
#[cfg(any(test, feature = "test-dependencies"))]
pub(super) mod testing {
use ff::Field;
use proptest::prelude::*;
use rand::distributions::{Distribution, Standard};
use super::Node;
use crate::note::testing::arb_cmu;
@ -181,4 +183,17 @@ pub(super) mod testing {
Node::from_cmu(&cmu)
}
}
impl Node {
/// Return a random fake `MerkleHashOrchard`.
pub fn random(rng: &mut impl RngCore) -> Self {
Standard.sample(rng)
}
}
impl Distribution<Node> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Node {
Node::from_scalar(bls12_381::Scalar::random(rng))
}
}
}