penumbra_tct/
random.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! Generating random values of various types.

use decaf377::Fq;
use rand::{distributions::Distribution, Rng};

use super::StateCommitment;
use crate::{
    builder::{block, epoch},
    structure::Hash,
    Root,
};

struct UniformFq;

impl Distribution<Fq> for UniformFq {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Fq {
        let mut bytes = [0u8; 32];
        loop {
            rng.fill_bytes(&mut bytes);
            if let Ok(fq) = Fq::from_bytes_checked(&bytes) {
                return fq;
            }
        }
    }
}

impl Distribution<StateCommitment> for UniformFq {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> StateCommitment {
        StateCommitment(self.sample(rng))
    }
}

impl StateCommitment {
    /// Generate a random [`Commitment`].
    pub fn random(mut rng: impl Rng) -> Self {
        rng.sample(UniformFq)
    }
}

impl Distribution<Hash> for UniformFq {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Hash {
        Hash::new(self.sample(rng))
    }
}

impl Hash {
    /// Generate a random [`struct@Hash`].
    pub fn random(mut rng: impl Rng) -> Self {
        rng.sample(UniformFq)
    }
}

impl Distribution<Root> for UniformFq {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Root {
        Root(self.sample(rng))
    }
}

impl Root {
    /// Generate a random [`Root`].
    pub fn random(mut rng: impl Rng) -> Self {
        rng.sample(UniformFq)
    }
}

impl Distribution<epoch::Root> for UniformFq {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> epoch::Root {
        epoch::Root(self.sample(rng))
    }
}

impl epoch::Root {
    /// Generate a random [`epoch::Root`].
    pub fn random(mut rng: impl Rng) -> Self {
        rng.sample(UniformFq)
    }
}

impl Distribution<block::Root> for UniformFq {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> block::Root {
        block::Root(self.sample(rng))
    }
}

impl block::Root {
    /// Generate a random [`block::Root`].
    pub fn random(mut rng: impl Rng) -> Self {
        rng.sample(UniformFq)
    }
}