Add `Node::random` and `Distribution<Node> for Standard` for testing.

This commit is contained in:
Kris Nuttycombe 2024-03-16 08:54:13 -06:00
parent 2122dbed71
commit dee71eee99
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))
}
}
}