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
use crate::hashing_utils::scalar_storage::StorageScalar;
use crate::merkle_lvl_hash::hash;
use crate::ARITY;
use dusk_plonk::bls12_381::Scalar as BlsScalar;
use hades252::WIDTH;
use kelvin::{Branch, ByteHash, Compound};
use std::borrow::Borrow;
use std::mem;
#[derive(Debug, Clone, PartialEq)]
pub struct PoseidonBranch {
pub root: BlsScalar,
pub levels: Vec<PoseidonLevel>,
}
impl<C, H> From<&Branch<'_, C, H>> for PoseidonBranch
where
C: Compound<H>,
C::Annotation: Borrow<StorageScalar>,
H: ByteHash,
{
fn from(branch: &Branch<C, H>) -> PoseidonBranch {
let mut poseidon_branch = PoseidonBranch::new();
poseidon_branch.root = branch
.levels()
.first()
.expect("Unexpected Error: Kelvin Branch always has a root")
.annotation()
.expect("Unexpected Error: Kelvin Branch should always suceed applying annotations")
.borrow()
.to_owned()
.into();
for level in branch.levels().iter().rev() {
poseidon_branch.levels.push({
let mut pos_level = PoseidonLevel::default();
let mut level_bitflags = 0u64;
level
.children()
.iter()
.zip(pos_level.leaves.iter_mut().skip(1))
.enumerate()
.for_each(|(idx, (src, dest))| {
*dest = match src.annotation() {
Some(annotation) => {
let stor_scalar: &StorageScalar =
&(*annotation).borrow();
let scalar: &BlsScalar = stor_scalar.borrow();
level_bitflags |= 1u64 << ((ARITY - 1) - idx);
*scalar
}
None => BlsScalar::zero(),
};
});
pos_level.leaves[0] = BlsScalar::from(level_bitflags);
pos_level.offset = level.offset() + 1;
pos_level
})
}
poseidon_branch
}
}
impl PoseidonBranch {
pub fn new() -> Self {
PoseidonBranch {
root: BlsScalar::zero(),
levels: vec![],
}
}
pub fn with_capacity(n: usize) -> Self {
PoseidonBranch {
root: BlsScalar::zero(),
levels: Vec::with_capacity(n),
}
}
pub fn extend(&mut self, target_depth: usize) -> usize {
let n_extensions = target_depth - self.levels.len();
while self.levels.len() < target_depth {
let old_root = mem::take(&mut self.root);
let flag = BlsScalar::from(0b1000);
let mut leaves = [BlsScalar::zero(); ARITY + 1];
leaves[0] = flag;
leaves[1] = old_root;
let level = PoseidonLevel { leaves, offset: 1 };
self.root = hash::merkle_level_hash_without_bitflags(&level);
self.levels.push(level);
}
n_extensions
}
}
pub(crate) fn extend_scalar(mut scalar: BlsScalar, n: usize) -> BlsScalar {
for _ in 0..n {
let flag = BlsScalar::from(0b1000);
let mut leaves = [BlsScalar::zero(); ARITY + 1];
leaves[0] = flag;
leaves[1] = scalar;
let level = PoseidonLevel { leaves, offset: 1 };
scalar = hash::merkle_level_hash_without_bitflags(&level);
}
scalar
}
#[derive(Debug, Clone, PartialEq)]
pub struct PoseidonLevel {
pub offset: usize,
pub leaves: [BlsScalar; WIDTH],
}
impl Default for PoseidonLevel {
fn default() -> Self {
PoseidonLevel {
offset: 0usize,
leaves: [BlsScalar::zero(); WIDTH],
}
}
}