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
use std::marker::{Send, Sync};
use winter_crypto::Hasher;
use crate::{
errors::{AkdError, AuditorError, AzksError},
proof_structs::{AppendOnlyProof, SingleAppendOnlyProof},
storage::{manager::StorageManager, memory::AsyncInMemoryDatabase},
Azks,
};
pub async fn audit_verify<H: Hasher + Send + Sync>(
hashes: Vec<H::Digest>,
proof: AppendOnlyProof<H>,
) -> Result<(), AkdError> {
if proof.epochs.len() + 1 != hashes.len() {
return Err(AkdError::AuditErr(AuditorError::VerifyAuditProof(format!(
"The proof has a different number of epochs than needed for hashes.
The number of hashes you provide should be one more than the number of epochs!
Number of epochs = {}, number of hashes = {}",
proof.epochs.len(),
hashes.len()
))));
}
if proof.epochs.len() != proof.proofs.len() {
return Err(AkdError::AuditErr(AuditorError::VerifyAuditProof(format!(
"The proof has {} epochs and {} proofs. These should be equal!",
proof.epochs.len(),
proof.proofs.len()
))));
}
for i in 0..hashes.len() - 1 {
let start_hash = hashes[i];
let end_hash = hashes[i + 1];
verify_consecutive_append_only::<H>(
&proof.proofs[i],
start_hash,
end_hash,
proof.epochs[i] + 1,
)
.await?;
}
Ok(())
}
pub async fn verify_consecutive_append_only<H: Hasher + Send + Sync>(
proof: &SingleAppendOnlyProof<H>,
start_hash: H::Digest,
end_hash: H::Digest,
epoch: u64,
) -> Result<(), AkdError> {
let unchanged_nodes = proof.unchanged_nodes.clone();
let inserted = proof.inserted.clone();
let db = AsyncInMemoryDatabase::new();
let manager = StorageManager::new_no_cache(&db);
let mut azks = Azks::new::<_, H>(&manager).await?;
azks.batch_insert_leaves_helper::<_, H>(&manager, unchanged_nodes, true)
.await?;
let computed_start_root_hash: H::Digest = azks.get_root_hash::<_, H>(&manager).await?;
let mut verified = computed_start_root_hash == start_hash;
azks.latest_epoch = epoch - 1;
let updated_inserted = inserted
.iter()
.map(|x| {
let mut y = *x;
y.hash = H::merge_with_int(x.hash, epoch);
y
})
.collect();
azks.batch_insert_leaves_helper::<_, H>(&manager, updated_inserted, true)
.await?;
let computed_end_root_hash: H::Digest = azks.get_root_hash::<_, H>(&manager).await?;
verified = verified && (computed_end_root_hash == end_hash);
if !verified {
return Err(AkdError::AzksErr(AzksError::VerifyAppendOnlyProof));
}
Ok(())
}