-
Notifications
You must be signed in to change notification settings - Fork 22
Add differential property tests #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,98 @@ | ||
| //! Differential property tests between the three merkleization implementations. | ||
| //! | ||
| //! The equivalence is checked as a chain: | ||
| //! | ||
| //! - `merkleize_padded` against `merkleize_standard` (the naive reference), and | ||
| //! - `MerkleHasher` and `merkle_root` against `merkleize_padded`. | ||
|
|
||
| use proptest::prelude::*; | ||
| use tree_hash::{ | ||
| merkle_root, merkleize_padded, merkleize_standard, Error, Hash256, MerkleHasher, | ||
| BYTES_PER_CHUNK, | ||
| }; | ||
|
|
||
| const MAX_BYTES: usize = 2048; | ||
| const MAX_MIN_CHUNKS: usize = 70; | ||
|
|
||
| /// Computes the root of `bytes` with the naive algorithm, padding the input out to `min_chunks` | ||
| /// (rounded to the next power of two) since `merkleize_standard` does not take a chunk count. | ||
| fn reference_root(bytes: &[u8], min_chunks: usize) -> Hash256 { | ||
| let mut padded = bytes.to_vec(); | ||
| padded.resize( | ||
| std::cmp::max( | ||
| bytes.len(), | ||
| min_chunks.next_power_of_two() * BYTES_PER_CHUNK, | ||
| ), | ||
| 0, | ||
| ); | ||
| merkleize_standard(&padded) | ||
| } | ||
|
|
||
| proptest! { | ||
| #[test] | ||
| fn merkleize_padded_matches_standard( | ||
| bytes in proptest::collection::vec(any::<u8>(), 0..=MAX_BYTES), | ||
| min_chunks in 0..=MAX_MIN_CHUNKS, | ||
| ) { | ||
| prop_assert_eq!( | ||
| merkleize_padded(&bytes, min_chunks), | ||
| reference_root(&bytes, min_chunks) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn merkle_hasher_matches_merkleize_padded( | ||
| bytes in proptest::collection::vec(any::<u8>(), 0..=MAX_BYTES), | ||
| extra_leaves in 0_usize..=8, | ||
| write_size in 1_usize..=64, | ||
| ) { | ||
| let num_leaves = bytes.len().div_ceil(BYTES_PER_CHUNK) + extra_leaves; | ||
|
|
||
| let mut hasher = MerkleHasher::with_leaves(num_leaves); | ||
| for chunk in bytes.chunks(write_size) { | ||
| hasher.write(chunk).expect("num_leaves is sufficient for bytes"); | ||
| } | ||
| let root = hasher.finish().expect("num_leaves is sufficient for bytes"); | ||
|
|
||
| prop_assert_eq!(root, merkleize_padded(&bytes, num_leaves)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn merkle_hasher_rejects_too_many_bytes( | ||
| num_leaves in 0_usize..=MAX_MIN_CHUNKS, | ||
| extra_bytes in 1_usize..=3 * BYTES_PER_CHUNK, | ||
| write_size in 1_usize..=64, | ||
| ) { | ||
| // `with_leaves` rounds the leaf count up to the next power of two, so that is the true | ||
| // capacity of the tree. | ||
| let capacity = num_leaves.next_power_of_two(); | ||
| let bytes = vec![0xff_u8; capacity * BYTES_PER_CHUNK + extra_bytes]; | ||
|
|
||
| // Any bytes beyond the tree's capacity must produce an error, never a root that silently | ||
| // ignores them. Depending on `extra_bytes` and `write_size` the error surfaces either in | ||
| // `write` (a whole excess leaf) or in `finish` (excess bytes still in the buffer). | ||
| let mut hasher = MerkleHasher::with_leaves(num_leaves); | ||
| let result = bytes | ||
| .chunks(write_size) | ||
| .try_for_each(|chunk| hasher.write(chunk)) | ||
| .and_then(|()| hasher.finish().map(|_| ())); | ||
|
|
||
| prop_assert_eq!( | ||
| result, | ||
| Err(Error::MaximumLeavesExceeded { max_leaves: capacity }) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn merkle_root_matches_merkleize_padded( | ||
| bytes in proptest::collection::vec(any::<u8>(), 0..=MAX_BYTES), | ||
| min_leaves in 0..=MAX_MIN_CHUNKS, | ||
| ) { | ||
| // This exercises the 0, 1 and 2-leaf fast-paths in `merkle_root` as well as the | ||
| // `MerkleHasher` path. | ||
| prop_assert_eq!( | ||
| merkle_root(&bytes, min_leaves), | ||
| merkleize_padded(&bytes, min_leaves) | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could change this to be depth = 0 at the root node instead of depth = 1 OR we could change this field name to
num_layersOr we can just keep as is, with an updated comment. I dont have a super strong opinion either way, though I think changing the value of depth might technically be a sketchier change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can come back for some cleanup here, I've opened an issue:
depthand make it consistent #49