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