@@ -58,6 +58,18 @@ impl<H: Hasher> MerklePath<H> {
58
58
pub fn new ( path : Vec < H :: Hash > ) -> Self {
59
59
Self ( path)
60
60
}
61
+
62
+ pub fn serialize ( & self ) -> Option < Vec < u8 > > {
63
+ let mut serialized = vec ! [ ] ;
64
+ let proof_size: u8 = self . 0 . len ( ) . try_into ( ) . ok ( ) ?;
65
+ serialized. extend_from_slice ( & proof_size. to_be_bytes ( ) ) ;
66
+
67
+ for node in self . 0 . iter ( ) {
68
+ serialized. extend_from_slice ( node. as_ref ( ) ) ;
69
+ }
70
+
71
+ Some ( serialized)
72
+ }
61
73
}
62
74
63
75
/// A MerkleAccumulator maintains a Merkle Tree.
@@ -86,11 +98,12 @@ pub struct MerkleAccumulator<H: Hasher = Keccak256> {
86
98
// TODO: This code does not belong to MerkleAccumulator, we should be using the wire data types in
87
99
// calling code to wrap this value.
88
100
impl < ' a , H : Hasher + ' a > MerkleAccumulator < H > {
89
- pub fn serialize ( & self , storage : u32 ) -> Vec < u8 > {
101
+ pub fn serialize ( & self , slot : u64 , ring_size : u32 ) -> Vec < u8 > {
90
102
let mut serialized = vec ! [ ] ;
91
103
serialized. extend_from_slice ( 0x41555756u32 . to_be_bytes ( ) . as_ref ( ) ) ;
92
104
serialized. extend_from_slice ( 0u8 . to_be_bytes ( ) . as_ref ( ) ) ;
93
- serialized. extend_from_slice ( storage. to_be_bytes ( ) . as_ref ( ) ) ;
105
+ serialized. extend_from_slice ( slot. to_be_bytes ( ) . as_ref ( ) ) ;
106
+ serialized. extend_from_slice ( ring_size. to_be_bytes ( ) . as_ref ( ) ) ;
94
107
serialized. extend_from_slice ( self . root . as_ref ( ) ) ;
95
108
serialized
96
109
}
@@ -146,7 +159,7 @@ impl<H: Hasher> MerkleAccumulator<H> {
146
159
// Filling the leaf hashes
147
160
for i in 0 ..( 1 << depth) {
148
161
if i < items. len ( ) {
149
- tree[ ( 1 << depth) + i] = hash_leaf :: < H > ( items[ i] . as_ref ( ) ) ;
162
+ tree[ ( 1 << depth) + i] = hash_leaf :: < H > ( items[ i] ) ;
150
163
} else {
151
164
tree[ ( 1 << depth) + i] = hash_null :: < H > ( ) ;
152
165
}
@@ -171,7 +184,7 @@ impl<H: Hasher> MerkleAccumulator<H> {
171
184
fn find_path ( & self , mut index : usize ) -> MerklePath < H > {
172
185
let mut path = Vec :: new ( ) ;
173
186
while index > 1 {
174
- path. push ( self . nodes [ index ^ 1 ] . clone ( ) ) ;
187
+ path. push ( self . nodes [ index ^ 1 ] ) ;
175
188
index /= 2 ;
176
189
}
177
190
MerklePath :: new ( path)
@@ -369,7 +382,7 @@ mod test {
369
382
// Accumulate into a 2 level tree.
370
383
let accumulator = MerkleAccumulator :: < Keccak256 > :: from_set ( set. into_iter ( ) ) . unwrap ( ) ;
371
384
let proof = accumulator. prove ( & item_a) . unwrap ( ) ;
372
- assert ! ( accumulator. check( proof. clone ( ) , & item_a) ) ;
385
+ assert ! ( accumulator. check( proof, & item_a) ) ;
373
386
374
387
// We now have a 2 level tree with 4 nodes:
375
388
//
@@ -399,10 +412,10 @@ mod test {
399
412
let faulty_accumulator = MerkleAccumulator :: < Keccak256 > {
400
413
root : accumulator. root ,
401
414
nodes : vec ! [
402
- accumulator. nodes[ 0 ] . clone ( ) ,
403
- accumulator. nodes[ 1 ] . clone ( ) , // Root Stays the Same
404
- accumulator. nodes[ 2 ] . clone ( ) , // Left node hash becomes a leaf.
405
- accumulator. nodes[ 3 ] . clone ( ) , // Right node hash becomes a leaf.
415
+ accumulator. nodes[ 0 ] ,
416
+ accumulator. nodes[ 1 ] , // Root Stays the Same
417
+ accumulator. nodes[ 2 ] , // Left node hash becomes a leaf.
418
+ accumulator. nodes[ 3 ] , // Right node hash becomes a leaf.
406
419
] ,
407
420
} ;
408
421
@@ -415,13 +428,13 @@ mod test {
415
428
. concat ( ) ;
416
429
417
430
// Confirm our combined hash existed as a node pair in the original tree.
418
- assert_eq ! ( hash_leaf:: <Keccak256 >( & fake_leaf_A) , accumulator. nodes[ 2 ] ) ;
431
+ assert_eq ! ( hash_leaf:: <Keccak256 >( fake_leaf_A) , accumulator. nodes[ 2 ] ) ;
419
432
420
433
// Now we can try and prove leaf membership in the faulty accumulator. NOTE: this should
421
434
// fail but to confirm that the test is actually correct you can remove the PREFIXES from
422
435
// the hash functions and this test will erroneously pass.
423
- let proof = faulty_accumulator. prove ( & fake_leaf_A) . unwrap ( ) ;
424
- assert ! ( faulty_accumulator. check( proof, & fake_leaf_A) ) ;
436
+ let proof = faulty_accumulator. prove ( fake_leaf_A) . unwrap ( ) ;
437
+ assert ! ( faulty_accumulator. check( proof, fake_leaf_A) ) ;
425
438
}
426
439
427
440
proptest ! {
0 commit comments