decaf377/ark_curve/
rand.rs

1use ark_ff::UniformRand;
2use ark_serialize::CanonicalSerialize;
3use ark_std::rand::{
4    distributions::{Distribution, Standard},
5    Rng,
6};
7
8use crate::ark_curve::{edwards::EdwardsProjective, AffinePoint, Element, Encoding};
9
10impl Distribution<Element> for Standard {
11    #[inline]
12    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Element {
13        loop {
14            // 1. Generate random underlying elliptic curve point.
15            let test_point = EdwardsProjective::rand(rng);
16            let mut bytes = [0u8; 32];
17            test_point
18                .serialize_compressed(&mut bytes[..])
19                .expect("serialization into array should be infallible");
20
21            // 2. Check if valid decaf point. If not continue.
22            if let Ok(p) = Encoding(bytes).vartime_decompress() {
23                return p;
24            }
25        }
26    }
27}
28
29impl Distribution<AffinePoint> for Standard {
30    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> AffinePoint {
31        let point: Element = self.sample(rng);
32        point.into()
33    }
34}