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
use crate::{Commitment, Result, Tree};
use std::prelude::v1::*;
use zkp_hash::{Hash, Hashable};
#[cfg(feature = "mmap")]
use crate::mmap_vec::MmapVec;
pub trait VectorCommitment
where
Self: Sync + Sized,
Self::Leaf: Sync + Hashable,
{
type Leaf;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn leaf(&self, index: usize) -> Self::Leaf;
fn leaf_hash(&self, index: usize) -> Hash {
self.leaf(index).hash()
}
fn commit(self) -> Result<(Commitment, Tree<Self>)> {
let tree = Tree::from_leaves(self)?;
let commitment = tree.commitment().clone();
Ok((commitment, tree))
}
}
impl<Leaf: Hashable + Clone + Sync> VectorCommitment for Vec<Leaf> {
type Leaf = Leaf;
fn len(&self) -> usize {
Self::len(self)
}
fn leaf(&self, index: usize) -> Self::Leaf {
self[index].clone()
}
fn leaf_hash(&self, index: usize) -> Hash {
self[index].hash()
}
}
#[cfg(feature = "mmap")]
impl<Leaf: Hashable + Clone + Sync> VectorCommitment for MmapVec<Leaf> {
type Leaf = Leaf;
fn len(&self) -> usize {
Self::len(self)
}
fn leaf(&self, index: usize) -> Self::Leaf {
self[index].clone()
}
fn leaf_hash(&self, index: usize) -> Hash {
self[index].hash()
}
}