zkplmt_test_helpers/
lib.rs

1use proptest::{
2    collection::vec,
3    prelude::{
4        prop::test_runner::{RngAlgorithm, TestRng},
5        RngCore, *,
6    },
7};
8
9use rand::{CryptoRng, Error};
10
11pub use zkplmt::core::verify_zkplmt;
12use zkplmt::models::{CurveVector, Proof, VectorTuple};
13
14prop_compose! {
15    pub fn arb_message()(bytes: Vec<u8>) -> Vec<u8> {
16        bytes
17    }
18}
19
20prop_compose! {
21    pub fn arb_curve_vector()(bytes: [u8; 32]) -> CurveVector {
22        let mut rng = FakeCsprng::from_seed(bytes);
23        CurveVector::random(&mut rng)
24    }
25}
26
27prop_compose! {
28    pub fn arb_vector_tuple()(curve_vectors in vec(arb_curve_vector(), ..20)) -> VectorTuple {
29        VectorTuple{ values: curve_vectors }
30    }
31}
32
33prop_compose! {
34    pub fn arb_tuples()(tuples in vec(arb_vector_tuple(), ..20)) -> Vec<VectorTuple> {
35        tuples
36    }
37}
38
39prop_compose! {
40    pub fn arb_proof()(_bytes: [u8; 32], ) -> Proof {
41        Proof::new(Vec::new(), Vec::new())
42    }
43}
44
45/// The Dalek types require the `CryptoRng` trait for random number generators. While this may
46/// be important for cryptographic safety in some cases, this prevents deterministic RNG
47/// needed for proptesting. We are using a new-type which impl's the trait
48/// to trick Dalek that the RNG is cryptographically secure.
49#[derive(Debug)]
50pub struct FakeCsprng(TestRng);
51impl CryptoRng for FakeCsprng {}
52impl RngCore for FakeCsprng {
53    fn next_u32(&mut self) -> u32 {
54        self.0.next_u32()
55    }
56
57    fn next_u64(&mut self) -> u64 {
58        self.0.next_u64()
59    }
60
61    fn fill_bytes(&mut self, dest: &mut [u8]) {
62        self.0.fill_bytes(dest)
63    }
64
65    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
66        self.0.try_fill_bytes(dest)
67    }
68}
69
70impl FakeCsprng {
71    pub fn from_seed(seed: [u8; 32]) -> Self {
72        FakeCsprng(TestRng::from_seed(RngAlgorithm::ChaCha, &seed))
73    }
74}