Skip to content

Commit

Permalink
Fix utreexo roots matching
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kus committed Oct 22, 2024
1 parent 2c6b36b commit 305f626
Show file tree
Hide file tree
Showing 16 changed files with 221 additions and 178 deletions.
260 changes: 136 additions & 124 deletions packages/utreexo/src/stump/accumulator.cairo

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions packages/utreexo/src/test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ fn main(mut arguments: Span<felt252>, _flags: felt252) {
)
.expect('Failed to deserialize');

match state.verify(@proof, leaves_to_del.span()) {
Result::Ok(_) => {},
Result::Err(err) => {
println!("FAIL: gas_spent={} error='{:?}'", gas_before - get_available_gas(), err);
panic!();
}
}

match state.verify_and_delete(@proof, leaves_to_del.span()) {
Result::Ok(new_state) => { state = new_state; },
Result::Err(err) => {
Expand Down
23 changes: 11 additions & 12 deletions packages/utreexo/src/vanilla/accumulator.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,19 @@ pub impl VanillaUtreexoAccumulatorImpl of VanillaUtreexoAccumulator {
let mut first_none_found: bool = false;
let mut node = hash;

for root in *self
.roots {
if !first_none_found {
if let Option::Some(root) = root {
node = parent_hash(*root, node);
new_roots.append(Option::None);
} else {
first_none_found = true;
new_roots.append(Option::Some(node));
}
for root in *self.roots {
if !first_none_found {
if let Option::Some(root) = root {
node = parent_hash(*root, node);
new_roots.append(Option::None);
} else {
new_roots.append(*root);
first_none_found = true;
new_roots.append(Option::Some(node));
}
};
} else {
new_roots.append(*root);
}
};

// Checks if terminates with Option::None
if (new_roots[new_roots.len() - 1].is_some()) {
Expand Down
19 changes: 9 additions & 10 deletions packages/utreexo/src/vanilla/proof.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,17 @@ pub impl UtreexoProofImpl of UtreexoProofTrait {
let mut curr_node = leaf_hash;
let mut node_index = *self.leaf_index;

for sibling in *self
.proof {
let (next_node_index, is_right) = DivRem::div_rem(node_index, 2);
for sibling in *self.proof {
let (next_node_index, is_right) = DivRem::div_rem(node_index, 2);

let (left, right) = if is_right == 0 {
(curr_node, *sibling)
} else {
(*sibling, curr_node)
};
curr_node = parent_hash(left, right);
node_index = next_node_index;
let (left, right) = if is_right == 0 {
(curr_node, *sibling)
} else {
(*sibling, curr_node)
};
curr_node = parent_hash(left, right);
node_index = next_node_index;
};
// Returns the computed root (or the node itself if the proof is empty).
curr_node
}
Expand Down
5 changes: 4 additions & 1 deletion packages/utreexo/tests/data/cached_proof_test_case_0.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
1
]
},
"leaves_to_del": [],
"leaves_to_del": [
0,
1
],
"leaves_to_add": [
20,
21,
Expand Down
6 changes: 5 additions & 1 deletion packages/utreexo/tests/data/cached_proof_test_case_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
4
]
},
"leaves_to_del": [],
"leaves_to_del": [
0,
1,
4
],
"leaves_to_add": [
8
],
Expand Down
4 changes: 3 additions & 1 deletion packages/utreexo/tests/data/cached_proof_test_case_2.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
4
]
},
"leaves_to_del": [],
"leaves_to_del": [
4
],
"leaves_to_add": [
8
],
Expand Down
7 changes: 6 additions & 1 deletion packages/utreexo/tests/data/cached_proof_test_case_3.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
7
]
},
"leaves_to_del": [],
"leaves_to_del": [
4,
5,
6,
7
],
"leaves_to_add": [],
"expected_state": {
"roots": [
Expand Down
4 changes: 3 additions & 1 deletion packages/utreexo/tests/data/cached_proof_test_case_4.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
0
]
},
"leaves_to_del": [],
"leaves_to_del": [
0
],
"leaves_to_add": [],
"expected_state": {
"roots": [
Expand Down
5 changes: 4 additions & 1 deletion packages/utreexo/tests/data/cached_proof_test_case_5.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
6
]
},
"leaves_to_del": [],
"leaves_to_del": [
3,
6
],
"leaves_to_add": [],
"expected_state": {
"roots": [
Expand Down
5 changes: 4 additions & 1 deletion packages/utreexo/tests/data/cached_proof_test_case_6.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
6
]
},
"leaves_to_del": [],
"leaves_to_del": [
3,
6
],
"leaves_to_add": [],
"expected_state": {
"roots": [
Expand Down
5 changes: 4 additions & 1 deletion packages/utreexo/tests/data/cached_proof_test_case_7.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
5
]
},
"leaves_to_del": [],
"leaves_to_del": [
4,
5
],
"leaves_to_add": [
6,
7,
Expand Down
22 changes: 1 addition & 21 deletions packages/utreexo/tests/data/ignore
Original file line number Diff line number Diff line change
@@ -1,21 +1 @@
deletion_test_case_3.json
deletion_test_case_4.json
deletion_test_case_5.json
deletion_test_case_6.json
deletion_test_case_10.json
update_data_test_case_0.json
update_data_test_case_1.json
update_data_test_case_2.json
update_data_test_case_3.json
update_data_test_case_4.json
update_data_test_case_5.json
update_data_test_case_6.json
update_data_test_case_7.json
cached_proof_test_case_0.json
cached_proof_test_case_1.json
cached_proof_test_case_2.json
cached_proof_test_case_3.json
cached_proof_test_case_4.json
cached_proof_test_case_5.json
cached_proof_test_case_6.json
cached_proof_test_case_7.json

7 changes: 6 additions & 1 deletion packages/utreexo/tests/data/update_data_test_case_5.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
3
]
},
"leaves_to_del": [],
"leaves_to_del": [
0,
1,
2,
3
],
"leaves_to_add": [
0,
1,
Expand Down
8 changes: 7 additions & 1 deletion packages/utreexo/tests/data/update_data_test_case_6.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@
14
]
},
"leaves_to_del": [],
"leaves_to_del": [
0,
2,
7,
12,
14
],
"leaves_to_add": [
0,
1,
Expand Down
11 changes: 10 additions & 1 deletion packages/utreexo/tests/data/update_data_test_case_7.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,16 @@
16
]
},
"leaves_to_del": [],
"leaves_to_del": [
0,
2,
7,
10,
12,
14,
15,
16
],
"leaves_to_add": [
0,
1,
Expand Down

0 comments on commit 305f626

Please sign in to comment.