Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'alex/iterative-mht-digest' into 'master'
perf(crypto): CRP-2019 iterative MixedHashTree::digest() `MixedHashTree::digest()` is a recursive function, which, naturally, can produce a stack overflow. Thus we had 2 options to mitigate that: 1) make the function iterative and 2) add a depth check and error handling. This MR implements both and sets 1) to be used in production, whereas 2) turned out to be ~15% less efficient, so let's count this as a performance improvement rather than a fix, since we don't expect to have trees that could produce a stack overflow in production. The recursive implementation with error handling is moved to test utils and this MR also adds a new proptest that compares that the outputs of both recursive and iterative functions are equal. See merge request dfinity-lab/public/ic!13918
- Loading branch information
Showing
8 changed files
with
141 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use assert_matches::assert_matches; | ||
use ic_crypto_tree_hash::MixedHashTree; | ||
use ic_crypto_tree_hash_test_utils::{mixed_hash_tree_digest_recursive, TooDeepRecursion}; | ||
|
||
const MAX_HASH_TREE_DEPTH: usize = 128; | ||
|
||
#[test] | ||
fn mixed_hash_tree_recursive_digest_errors_on_too_deep_trees() { | ||
let mut tree = MixedHashTree::Empty; | ||
for _ in 1..MAX_HASH_TREE_DEPTH { | ||
tree = MixedHashTree::Fork(Box::new((tree.clone(), MixedHashTree::Empty))); | ||
} | ||
|
||
assert_matches!(mixed_hash_tree_digest_recursive(&tree), Ok(_)); | ||
|
||
tree = MixedHashTree::Fork(Box::new((tree.clone(), MixedHashTree::Empty))); | ||
assert_eq!( | ||
mixed_hash_tree_digest_recursive(&tree), | ||
Err(TooDeepRecursion(MAX_HASH_TREE_DEPTH as u32 + 1)) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters