zebra_chain/orchard/
arbitrary.rs1use group::{
4 ff::{FromUniformBytes, PrimeField},
5 CurveAffine,
6};
7use halo2::pasta::pallas;
8use reddsa::{orchard::SpendAuth, Signature, SigningKey, VerificationKey, VerificationKeyBytes};
9
10use proptest::{array, collection::vec, prelude::*};
11
12use super::{keys::*, note, tree, Action, AuthorizedAction, Flags, ValueCommitment};
13
14impl Arbitrary for Action {
15 type Parameters = ();
16
17 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
18 (
19 any::<note::Nullifier>(),
20 any::<SpendAuthVerificationKeyBytes>(),
21 any::<note::EncryptedNote>(),
22 any::<note::WrappedNoteKey>(),
23 )
24 .prop_map(|(nullifier, rk, enc_ciphertext, out_ciphertext)| Self {
25 cv: ValueCommitment(pallas::Affine::identity()),
26 nullifier,
27 rk: rk.0,
28 cm_x: pallas::Base::zero(),
29 ephemeral_key: EphemeralPublicKey(pallas::Affine::generator()),
30 enc_ciphertext,
31 out_ciphertext,
32 })
33 .boxed()
34 }
35
36 type Strategy = BoxedStrategy<Self>;
37}
38
39impl Arbitrary for note::Nullifier {
40 type Parameters = ();
41
42 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
43 (vec(any::<u8>(), 64))
44 .prop_map(|bytes| {
45 let bytes = bytes.try_into().expect("vec is the correct length");
46 Self::try_from(pallas::Scalar::from_uniform_bytes(&bytes).to_repr())
47 .expect("a valid generated nullifier")
48 })
49 .boxed()
50 }
51
52 type Strategy = BoxedStrategy<Self>;
53}
54
55impl Arbitrary for AuthorizedAction {
56 type Parameters = ();
57
58 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
59 (any::<Action>(), any::<SpendAuthSignature>())
60 .prop_map(|(action, spend_auth_sig)| Self {
61 action,
62 spend_auth_sig: spend_auth_sig.0,
63 })
64 .boxed()
65 }
66
67 type Strategy = BoxedStrategy<Self>;
68}
69
70#[derive(Copy, Clone, Debug, Eq, PartialEq)]
71struct SpendAuthSignature(pub(crate) Signature<SpendAuth>);
72
73impl Arbitrary for SpendAuthSignature {
74 type Parameters = ();
75
76 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
77 (array::uniform32(any::<u8>()), array::uniform32(any::<u8>()))
78 .prop_map(|(r_bytes, s_bytes)| {
79 let mut bytes = [0; 64];
80 bytes[0..32].copy_from_slice(&r_bytes[..]);
81 bytes[32..64].copy_from_slice(&s_bytes[..]);
82 SpendAuthSignature(Signature::<SpendAuth>::from(bytes))
83 })
84 .boxed()
85 }
86
87 type Strategy = BoxedStrategy<Self>;
88}
89
90#[derive(Copy, Clone, PartialEq, Eq, Debug)]
91struct SpendAuthVerificationKeyBytes(pub(crate) VerificationKeyBytes<SpendAuth>);
92
93impl Arbitrary for SpendAuthVerificationKeyBytes {
94 type Parameters = ();
95
96 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
97 (vec(any::<u8>(), 64))
99 .prop_map(|bytes| {
100 let bytes = bytes.try_into().expect("vec is the correct length");
101 let sk_scalar = pallas::Scalar::from_uniform_bytes(&bytes);
103 let sk_bytes = sk_scalar.to_repr();
105 let sk = SigningKey::from_bytes(&sk_bytes).unwrap();
107 let pk = VerificationKey::<SpendAuth>::from(&sk);
108 SpendAuthVerificationKeyBytes(pk.into())
109 })
110 .boxed()
111 }
112
113 type Strategy = BoxedStrategy<Self>;
114}
115
116impl Arbitrary for Flags {
117 type Parameters = ();
118
119 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
120 let pre_nu6_3 = Self::ENABLE_SPENDS.bits() | Self::ENABLE_OUTPUTS.bits();
125 (any::<u8>())
126 .prop_map(move |bits| Self::from_bits_truncate(bits & pre_nu6_3))
127 .boxed()
128 }
129
130 type Strategy = BoxedStrategy<Self>;
131}
132
133fn pallas_base_strat() -> BoxedStrategy<pallas::Base> {
134 (vec(any::<u8>(), 64))
135 .prop_map(|bytes| {
136 let bytes = bytes.try_into().expect("vec is the correct length");
137 pallas::Base::from_uniform_bytes(&bytes)
138 })
139 .boxed()
140}
141
142impl Arbitrary for tree::Root {
143 type Parameters = ();
144
145 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
146 pallas_base_strat()
147 .prop_map(|base| {
148 Self::try_from(base.to_repr())
149 .expect("a valid generated Orchard note commitment tree root")
150 })
151 .boxed()
152 }
153
154 type Strategy = BoxedStrategy<Self>;
155}
156
157impl Arbitrary for tree::Node {
158 type Parameters = ();
159
160 fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
161 pallas_base_strat()
162 .prop_map(|base| {
163 Self::try_from(base.to_repr())
164 .expect("a valid generated Orchard note commitment tree root")
165 })
166 .boxed()
167 }
168
169 type Strategy = BoxedStrategy<Self>;
170}