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
#![allow(clippy::module_inception)]
pub mod merkle_path;
pub use merkle_path::*;
pub mod merkle_tree;
pub use merkle_tree::*;
#[cfg(test)]
pub mod tests;
use rand::{Rng, SeedableRng};
const PRNG_SEED: [u8; 32] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
pub fn prng() -> impl Rng {
rand_chacha::ChaChaRng::from_seed(PRNG_SEED)
}
#[macro_export]
macro_rules! define_merkle_tree_parameters {
($struct_name:ident, $hash:ty, $depth:expr) => {
#[allow(unused_imports)]
use snarkvm_models::algorithms::{LoadableMerkleParameters, MaskedMerkleParameters, MerkleParameters, CRH};
#[allow(unused_imports)]
use $crate::merkle_tree::MerkleTree;
#[allow(unused_imports)]
use rand::Rng;
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct $struct_name($hash);
impl MerkleParameters for $struct_name {
type H = $hash;
const DEPTH: usize = $depth;
fn setup<R: Rng>(rng: &mut R) -> Self {
Self(Self::H::setup(rng))
}
fn crh(&self) -> &Self::H {
&self.0
}
fn parameters(&self) -> &<Self::H as CRH>::Parameters {
self.crh().parameters()
}
}
impl From<$hash> for $struct_name {
fn from(crh: $hash) -> Self {
Self(crh)
}
}
impl LoadableMerkleParameters for $struct_name {}
impl Default for $struct_name {
fn default() -> Self {
Self(<Self as MerkleParameters>::H::setup(
&mut $crate::merkle_tree::prng(),
))
}
}
};
}
#[macro_export]
macro_rules! define_masked_merkle_tree_parameters {
($struct_name:ident, $hash:ty, $depth:expr) => {
#[allow(unused_imports)]
use snarkvm_models::algorithms::{CRHParameters, MaskedMerkleParameters, MerkleParameters, CRH};
#[allow(unused_imports)]
use $crate::merkle_tree::MerkleTree;
#[allow(unused_imports)]
use rand::Rng;
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct $struct_name($hash, <$hash as CRH>::Parameters);
impl MerkleParameters for $struct_name {
type H = $hash;
const DEPTH: usize = $depth;
fn setup<R: Rng>(rng: &mut R) -> Self {
Self(Self::H::setup(rng), <Self::H as CRH>::Parameters::setup(rng))
}
fn crh(&self) -> &Self::H {
&self.0
}
fn parameters(&self) -> &<Self::H as CRH>::Parameters {
self.crh().parameters()
}
}
impl MaskedMerkleParameters for $struct_name {
fn mask_parameters(&self) -> &<Self::H as CRH>::Parameters {
&self.1
}
}
impl Default for $struct_name {
fn default() -> Self {
Self(
<Self as MerkleParameters>::H::setup(&mut $crate::merkle_tree::prng()),
<<Self as MerkleParameters>::H as CRH>::Parameters::setup(&mut $crate::merkle_tree::prng()),
)
}
}
};
}