-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathnullifier_inclusion.nr
73 lines (64 loc) · 2.52 KB
/
nullifier_inclusion.nr
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
use dep::protocol_types::block_header::BlockHeader;
use dep::protocol_types::merkle_tree::root::root_from_sibling_path;
use crate::{
context::PrivateContext,
note::{
note_interface::NoteHash, retrieved_note::RetrievedNote,
utils::compute_siloed_note_nullifier,
},
oracle::get_nullifier_membership_witness::get_nullifier_membership_witness,
};
mod test;
trait ProveNullifierInclusion {
fn prove_nullifier_inclusion(header: BlockHeader, nullifier: Field);
}
impl ProveNullifierInclusion for BlockHeader {
fn prove_nullifier_inclusion(self, nullifier: Field) {
// 1) Get the membership witness of the nullifier
// Safety: The witness is only used as a "magical value" that makes the proof below pass. Hence it's safe.
let witness = unsafe {
get_nullifier_membership_witness(self.global_variables.block_number as u32, nullifier)
};
// 2) First we prove that the tree leaf in the witness is present in the nullifier tree. This is expected to be
// the leaf that contains the nullifier we're proving inclusion for.
assert_eq(
self.state.partial.nullifier_tree.root,
root_from_sibling_path(witness.leaf_preimage.hash(), witness.index, witness.path),
"Proving nullifier inclusion failed",
);
// 3) Then we simply check that the value in the leaf is the expected one. Note that we don't need to perform
// any checks on the rest of the values in the leaf preimage (the next index or next nullifier), since all we
// care about is showing that the tree contains an entry with the expected nullifier.
assert_eq(
witness.leaf_preimage.nullifier,
nullifier,
"Nullifier does not match value in witness",
);
}
}
trait ProveNoteIsNullified {
fn prove_note_is_nullified<Note>(
header: BlockHeader,
retrieved_note: RetrievedNote<Note>,
storage_slot: Field,
context: &mut PrivateContext,
)
where
Note: NoteHash;
}
impl ProveNoteIsNullified for BlockHeader {
// docs:start:prove_note_is_nullified
fn prove_note_is_nullified<Note>(
self,
retrieved_note: RetrievedNote<Note>,
storage_slot: Field,
context: &mut PrivateContext,
)
where
Note: NoteHash,
{
let nullifier = compute_siloed_note_nullifier(retrieved_note, storage_slot, context);
self.prove_nullifier_inclusion(nullifier);
}
// docs:end:prove_note_is_nullified
}