Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
ece598pv-sp2020
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
weirang2
ece598pv-sp2020
Commits
cdcb97be
Commit
cdcb97be
authored
5 years ago
by
Guanweiran
Browse files
Options
Downloads
Patches
Plain Diff
mp1_v2
parent
5bbb9b47
Branches
master
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/crypto/merkle.rs
+33
-33
33 additions, 33 deletions
src/crypto/merkle.rs
with
33 additions
and
33 deletions
src/crypto/merkle.rs
+
33
−
33
View file @
cdcb97be
...
...
@@ -4,29 +4,30 @@ use ring::digest;
/// A Merkle tree.
#[derive(Debug,
Default)]
pub
struct
MerkleTree
{
//
Troot: Node,
Troot
:
Node
,
/// The number of leaf nodes in the tree
//
depth: u32,
//
count: usize,
val
:
Vec
<
H256
>
,
lev
:
Vec
<
usize
>
,
depth
:
u32
,
count
:
usize
,
/*
val: Vec<H256>,
lev: Vec<usize>,
*/
}
/*
#[derive(Debug, Default)]
#[derive(Debug,
Default)]
pub
struct
Node
{
val
:
H256
,
left
:
Option
<
Box
<
Node
>>
,
right
:
Option
<
Box
<
Node
>>
,
}
*/
}
impl
MerkleTree
{
pub
fn
new
<
T
>
(
data
:
&
[
T
])
->
Self
where
T
:
Hashable
,
{
//unimplemented!()
let
mut
dep
=
0
;
let
mut
cnt
=
0
;
let
mut
level
=
Vec
::
new
();
//
let mut level = Vec::new();
let
mut
cur
=
Vec
::
new
();
let
mut
save
=
Vec
::
new
();
for
d
in
data
.iter
(){
//
let mut save = Vec::new();
/*
for d in data.iter(){
cur.push(d.hash());
save.push(d.hash());
cnt += 1;
...
...
@@ -51,8 +52,8 @@ impl MerkleTree {
level.push(cnt);
cnt = 0;
}
MerkleTree
{
val
:
save
,
lev
:
level
}
/*
for d in data.iter(){
MerkleTree{val:save,lev:level}
*/
for
d
in
data
.iter
(){
cur
.push
(
Node
{
left
:
None
,
right
:
None
,
val
:
d
.hash
()});
dep
+=
1
;
}
...
...
@@ -70,9 +71,9 @@ impl MerkleTree {
cat
.update
(
&<
[
u8
;
32
]
>
::
from
(
r
.val
));
let
fin
=
cat
.finish
();
nxt
.push
(
Node
{
val
:
<
H256
>
::
from
(
fin
),
left
:
Some
(
Box
::
new
(
l
)),
right
:
Some
(
Box
::
new
(
r
)),
val: <H256>::from(fin),
});
}
cur
=
nxt
;
...
...
@@ -80,42 +81,41 @@ impl MerkleTree {
}
let
root
=
cur
.remove
(
0
);
let
size
=
data
.len
();
MerkleTree{Troot:root,depth:dep,count:size}
*/
MerkleTree
{
Troot
:
root
,
depth
:
dep
,
count
:
size
}
}
pub
fn
root
(
&
self
)
->
H256
{
//unimplemented!()
//
self.Troot.val
self
.val
[
self
.val
.len
()
-
1
]
self
.Troot.val
//
self.val[self.val.len()-1]
}
/*pub fn get_proof(tree: &Node,index: usize,count:usize)->Vec<H256>{
pub
fn
get_proof
(
tree
:
&
Node
,
index
:
usize
,
count
:
usize
,
res
:
&
mut
Vec
<
H256
>
){
match
*
tree
{
Node{
ref
val,ref left,ref right}=>{
Node
{
val
,
left
:
Some
(
ref
left
)
,
right
:
Some
(
ref
right
)
}
=>
{
let
left_count
=
count
.next_power_of_two
()
/
2
;
let
(
sub_lem_val
);
let sibling_hash = Vec::new();
if
index
<
left_count
{
sub_lem_val = MerkleTree::get_proof(left, index, left_count
);
s
ibling_hash.push(right.val
);
res
.push
(
right
.val
);
s
ub_lem_val
=
MerkleTree
::
get_proof
(
left
,
index
,
left_count
,
res
);
}
else
{
sub_lem_val = MerkleTree::get_proof(Some(right), index - left_count, count - left_count
);
s
ibling_hash.push(left.val
);
res
.push
(
left
.val
);
s
ub_lem_val
=
MerkleTree
::
get_proof
(
right
,
index
-
left_count
,
count
-
left_count
,
res
);
}
sibling_hash
}
_
=>
{}
}
}
*/
}
/// Returns the Merkle Proof of data at index i
pub
fn
proof
(
&
self
,
index
:
usize
)
->
Vec
<
H256
>
{
/*let mut mt_proof = Vec::new();
let mut root = self.Troot;
mt_proof = MerkleTree::get_proof(root,index,self.count);
mt_proof*/
let
mut
mt_proof
=
Vec
::
<
H256
>
::
new
();
MerkleTree
::
get_proof
(
&
self
.Troot
,
index
,
self
.count
,
&
mut
mt_proof
);
mt_proof
//unimplemented!()
let
mut
idx
=
index
;
/*
let mut idx = index;
let mut hash_val=Vec::new();
let mut count = self.lev[0];
let mut left_count = self.lev[0].next_power_of_two() / 2;
...
...
@@ -137,13 +137,13 @@ impl MerkleTree {
else{
idx -= left_count;
count -= left_count;
pos = pos*2;
left_count = count.next_power_of_two()/2;
cur_lev -= 1;
hash_val
.push
(
self
.val
[
start
[
cur_lev
]
+
pos
-
1
]);
pos
=
pos
*
2
;
hash_val.push(self.val[start[cur_lev]+pos-2]);
}
}
hash_val
hash_val
*/
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment