fix proof (#77)

This commit is contained in:
Noah Citron 2022-11-01 20:40:37 -04:00 committed by GitHub
parent 4444148f71
commit d06fb73803
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 46 additions and 4 deletions

View File

@ -23,7 +23,6 @@ pub fn verify_proof(proof: &Vec<Bytes>, root: &Vec<u8>, path: &Vec<u8>, value: &
return true; return true;
} }
} else { } else {
// inclusion proof
let nibble = get_nibble(&path, path_offset); let nibble = get_nibble(&path, path_offset);
expected_hash = node_list[nibble as usize].clone(); expected_hash = node_list[nibble as usize].clone();
@ -32,15 +31,24 @@ pub fn verify_proof(proof: &Vec<Bytes>, root: &Vec<u8>, path: &Vec<u8>, value: &
} else if node_list.len() == 2 { } else if node_list.len() == 2 {
if i == proof.len() - 1 { if i == proof.len() - 1 {
// exclusion proof // exclusion proof
if &node_list[0][skip_length(&node_list[0])..] != &path[path_offset..] if !paths_match(
&& is_empty_value(value) &node_list[0],
skip_length(&node_list[0]),
&path,
path_offset,
) && is_empty_value(value)
{ {
return true; return true;
} }
// inclusion proof // inclusion proof
if &node_list[1] == value { if &node_list[1] == value {
return true; return paths_match(
&node_list[0],
skip_length(&node_list[0]),
&path,
path_offset,
);
} }
} else { } else {
let node_path = &node_list[0]; let node_path = &node_list[0];
@ -56,6 +64,36 @@ pub fn verify_proof(proof: &Vec<Bytes>, root: &Vec<u8>, path: &Vec<u8>, value: &
false false
} }
fn paths_match(p1: &Vec<u8>, s1: usize, p2: &Vec<u8>, s2: usize) -> bool {
let len1 = p1.len() * 2 - s1;
let len2 = p2.len() * 2 - s2;
if len1 != len2 {
return false;
}
for offset in 0..len1 {
let n1 = get_nibble(p1, s1 + offset);
let n2 = get_nibble(p2, s2 + offset);
if n1 != n2 {
return false;
}
}
true
}
#[allow(dead_code)]
fn get_rest_path(p: &Vec<u8>, s: usize) -> String {
let mut ret = String::new();
for i in s..p.len() * 2 {
let n = get_nibble(p, i);
ret += &format!("{:01x}", n);
}
ret
}
fn is_empty_value(value: &Vec<u8>) -> bool { fn is_empty_value(value: &Vec<u8>) -> bool {
let mut stream = RlpStream::new(); let mut stream = RlpStream::new();
stream.begin_list(4); stream.begin_list(4);
@ -104,6 +142,10 @@ fn shared_prefix_length(path: &Vec<u8>, path_offset: usize, node_path: &Vec<u8>)
} }
fn skip_length(node: &Vec<u8>) -> usize { fn skip_length(node: &Vec<u8>) -> usize {
if node.len() == 0 {
return 0;
}
let nibble = get_nibble(node, 0); let nibble = get_nibble(node, 0);
match nibble { match nibble {
0 => 2, 0 => 2,