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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use crate::{keys::FullViewingKey, value::ValueCommitment, Note, ViewingKey};
impl super::Spend {
/// Verifies that the `cv` field is consistent with the note fields.
///
/// Requires that the following optional fields are set:
/// - `value`
/// - `rcv`
pub fn verify_cv(&self) -> Result<(), VerifyError> {
let value = self.value.ok_or(VerifyError::MissingValue)?;
let rcv = self
.rcv
.clone()
.ok_or(VerifyError::MissingValueCommitTrapdoor)?;
let cv_net = ValueCommitment::derive(value, rcv);
if cv_net.to_bytes() == self.cv.to_bytes() {
Ok(())
} else {
Err(VerifyError::InvalidValueCommitment)
}
}
/// Returns the [`ViewingKey`] to use when validating this note.
///
/// Handles dummy notes when the `value` field is set.
fn vk_for_validation(
&self,
expected_fvk: Option<&FullViewingKey>,
) -> Result<ViewingKey, VerifyError> {
let vk = self
.proof_generation_key
.as_ref()
.map(|proof_generation_key| proof_generation_key.to_viewing_key());
match (expected_fvk, vk, self.value.as_ref()) {
(Some(expected_fvk), Some(vk), _)
if vk.ak == expected_fvk.vk.ak && vk.nk == expected_fvk.vk.nk =>
{
Ok(vk)
}
// `expected_fvk` is ignored if the spent note is a dummy note.
(Some(_), Some(vk), Some(value)) if value.inner() == 0 => Ok(vk),
(Some(_), Some(_), _) => Err(VerifyError::MismatchedFullViewingKey),
(Some(expected_fvk), None, _) => Ok(expected_fvk.vk.clone()),
(None, Some(vk), _) => Ok(vk),
(None, None, _) => Err(VerifyError::MissingProofGenerationKey),
}
}
/// Verifies that the `nullifier` field is consistent with the note fields.
///
/// Requires that the following optional fields are set:
/// - `recipient`
/// - `value`
/// - `rseed`
/// - `witness`
///
/// In addition, at least one of the `proof_generation_key` field or `expected_fvk`
/// must be provided.
///
/// The provided [`FullViewingKey`] is ignored if the spent note is a dummy note.
/// Otherwise, it will be checked against the `proof_generation_key` field (if both
/// are set).
pub fn verify_nullifier(
&self,
expected_fvk: Option<&FullViewingKey>,
) -> Result<(), VerifyError> {
let vk = self.vk_for_validation(expected_fvk)?;
let note = Note::from_parts(
self.recipient.ok_or(VerifyError::MissingRecipient)?,
self.value.ok_or(VerifyError::MissingValue)?,
self.rseed.ok_or(VerifyError::MissingRandomSeed)?,
);
// We need both the note and the VK to verify the nullifier; we have everything
// needed to also verify that the correct VK was provided (the nullifier check
// itself only constrains `nk` within the VK).
if vk.to_payment_address(*note.recipient().diversifier()) != Some(note.recipient()) {
return Err(VerifyError::WrongFvkForNote);
}
let merkle_path = self.witness().as_ref().ok_or(VerifyError::MissingWitness)?;
if note.nf(&vk.nk, merkle_path.position().into()) == self.nullifier {
Ok(())
} else {
Err(VerifyError::InvalidNullifier)
}
}
/// Verifies that the `rk` field is consistent with the given FVK.
///
/// Requires that the following optional fields are set:
/// - `alpha`
///
/// The provided [`FullViewingKey`] is ignored if the spent note is a dummy note
/// (which can only be determined if the `value` field is set). Otherwise, it will be
/// checked against the `proof_generation_key` field (if set).
pub fn verify_rk(&self, expected_fvk: Option<&FullViewingKey>) -> Result<(), VerifyError> {
let vk = self.vk_for_validation(expected_fvk)?;
let alpha = self
.alpha
.as_ref()
.ok_or(VerifyError::MissingSpendAuthRandomizer)?;
if vk.ak.randomize(alpha) == self.rk {
Ok(())
} else {
Err(VerifyError::InvalidRandomizedVerificationKey)
}
}
}
impl super::Output {
/// Verifies that the `cv` field is consistent with the note fields.
///
/// Requires that the following optional fields are set:
/// - `value`
/// - `rcv`
pub fn verify_cv(&self) -> Result<(), VerifyError> {
let value = self.value.ok_or(VerifyError::MissingValue)?;
let rcv = self
.rcv
.clone()
.ok_or(VerifyError::MissingValueCommitTrapdoor)?;
let cv_net = ValueCommitment::derive(value, rcv);
if cv_net.to_bytes() == self.cv.to_bytes() {
Ok(())
} else {
Err(VerifyError::InvalidValueCommitment)
}
}
/// Verifies that the `cmu` field is consistent with the note fields.
///
/// Requires that the following optional fields are set:
/// - `recipient`
/// - `value`
/// - `rseed`
pub fn verify_note_commitment(&self) -> Result<(), VerifyError> {
let note = Note::from_parts(
self.recipient.ok_or(VerifyError::MissingRecipient)?,
self.value.ok_or(VerifyError::MissingValue)?,
crate::Rseed::AfterZip212(self.rseed.ok_or(VerifyError::MissingRandomSeed)?),
);
if note.cmu() == self.cmu {
Ok(())
} else {
Err(VerifyError::InvalidExtractedNoteCommitment)
}
}
}
/// Errors that can occur while verifying a PCZT bundle.
#[derive(Debug)]
pub enum VerifyError {
/// The output note's components do not produce the expected `cmx`.
InvalidExtractedNoteCommitment,
/// The spent note's components do not produce the expected `nullifier`.
InvalidNullifier,
/// The Spend's FVK and `alpha` do not produce the expected `rk`.
InvalidRandomizedVerificationKey,
/// The action's `cv_net` does not match the provided note values and `rcv`.
InvalidValueCommitment,
/// The spend or output's `fvk` field does not match the provided FVK.
MismatchedFullViewingKey,
/// Dummy notes must have their `proof_generation_key` field set in order to be verified.
MissingProofGenerationKey,
/// `nullifier` verification requires `rseed` to be set.
MissingRandomSeed,
/// `nullifier` verification requires `recipient` to be set.
MissingRecipient,
/// `rk` verification requires `alpha` to be set.
MissingSpendAuthRandomizer,
/// Verification requires all `value` fields to be set.
MissingValue,
/// `cv_net` verification requires `rcv` to be set.
MissingValueCommitTrapdoor,
/// `nullifier` verification requires `witness` to be set.
MissingWitness,
/// The provided `fvk` does not own the spent note.
WrongFvkForNote,
}