-
Notifications
You must be signed in to change notification settings - Fork 480
Use XXH32 instead of sha256 for const hashing #1393
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 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fbfd4e7
use much simpler fnv instead of sha256 for const hashing
xermicus 46beecb
Merge branch 'master' into cl/const-storage-key-with-stable-rust
xermicus df85b09
update key tests for fnv1a
xermicus aae9523
switch to xxhash32
xermicus 3256c28
update UI tests with new keys
xermicus 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,8 +12,8 @@ | |
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use const_fnv1a_hash::fnv1a_hash_32; | ||
| use ink_prelude::vec; | ||
| use sha2_const::Sha256; | ||
|
|
||
| /// A key into the smart contract storage. | ||
| /// | ||
|
|
@@ -33,18 +33,12 @@ impl KeyComposer { | |
| /// Concatenate two `Key` into one during compilation. | ||
| pub const fn concat(left: Key, right: Key) -> Key { | ||
| // If one of the keys is zero, then return another without hashing. | ||
| // If both keys are non-zero, return the hash of both keys. | ||
| // If both keys are non-zero, return the hash of the XOR difference of both keys. | ||
| match (left, right) { | ||
| (0, 0) => 0, | ||
| (0, _) => right, | ||
| (_, 0) => left, | ||
| (left, right) => { | ||
| let hash = Sha256::new() | ||
| .update(&left.to_be_bytes()) | ||
| .update(&right.to_be_bytes()) | ||
| .finalize(); | ||
| Key::from_be_bytes([hash[0], hash[1], hash[2], hash[3]]) | ||
| } | ||
| (left, right) => fnv1a_hash_32(&(left ^ right).to_be_bytes(), None), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -59,8 +53,7 @@ impl KeyComposer { | |
| return 0 | ||
| } | ||
|
|
||
| let hash = Sha256::new().update(bytes).finalize(); | ||
| Key::from_be_bytes([hash[0], hash[1], hash[2], hash[3]]) | ||
| fnv1a_hash_32(bytes, None) | ||
| } | ||
|
|
||
| /// Evaluates the storage key of the field in the structure, variant or union. | ||
|
|
@@ -69,8 +62,7 @@ impl KeyComposer { | |
| /// 1. If `variant_name` is not empty then computes the ASCII byte representation and call it `V`. | ||
| /// 1. Compute the ASCII byte representation of `field_name` and call it `F`. | ||
| /// 1. Concatenate (`S` and `F`) or (`S`, `V` and `F`) using `::` as separator and call it `C`. | ||
| /// 1. Apply the `SHA2` 256-bit hash `H` of `C`. | ||
| /// 1. The first 4 bytes of `H` make up the storage key. | ||
| /// 1. The `FNV1A` 32-bit hash of `C` is the storage key. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xermicus this should be
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks! 🙈 |
||
| /// | ||
| /// # Note | ||
| /// | ||
|
|
@@ -99,7 +91,6 @@ impl KeyComposer { | |
| } else { | ||
| vec![struct_name.as_bytes(), field_name.as_bytes()].join(separator) | ||
| }; | ||
|
|
||
| Ok(Self::from_bytes(composed_key.as_slice())) | ||
| } | ||
| } | ||
|
|
@@ -119,33 +110,33 @@ mod tests { | |
| fn concat_works_correct() { | ||
| assert_eq!(KeyComposer::concat(0, 13), 13); | ||
| assert_eq!(KeyComposer::concat(31, 0), 31); | ||
| assert_eq!(KeyComposer::concat(31, 13), 0xD83A5CD8); | ||
| assert_eq!(KeyComposer::concat(31, 13), 0x3995d8bf); | ||
| assert_eq!(KeyComposer::concat(0, 0), 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn from_str_works_correct() { | ||
| assert_eq!(KeyComposer::from_str(""), 0); | ||
| assert_eq!(KeyComposer::from_str("123"), 0xa665a459); | ||
| assert_eq!(KeyComposer::from_str("Hello world"), 0x64ec88ca); | ||
| assert_eq!(KeyComposer::from_str("123"), 0x7238631b); | ||
| assert_eq!(KeyComposer::from_str("Hello world"), 0x594d29c7); | ||
| } | ||
|
|
||
| #[test] | ||
| fn from_bytes_works_correct() { | ||
| assert_eq!(KeyComposer::from_bytes(b""), 0); | ||
| assert_eq!(KeyComposer::from_bytes(b"123"), 0xa665a459); | ||
| assert_eq!(KeyComposer::from_bytes(b"Hello world"), 0x64ec88ca); | ||
| assert_eq!(KeyComposer::from_bytes(b"123"), 0x7238631b); | ||
| assert_eq!(KeyComposer::from_bytes(b"Hello world"), 0x594d29c7); | ||
| } | ||
|
|
||
| #[test] | ||
| fn compute_key_works_correct() { | ||
| assert_eq!( | ||
| KeyComposer::compute_key("Contract", "", "balances"), | ||
| Ok(0x05e859ec) | ||
| Ok(0xe26930c6) | ||
| ); | ||
| assert_eq!( | ||
| KeyComposer::compute_key("Enum", "Variant", "0"), | ||
| Ok(0x9d029590) | ||
| Ok(0x9e253b67) | ||
| ); | ||
| assert_eq!( | ||
| KeyComposer::compute_key("", "Variant", "0"), | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.