4

When creating a StorageMap, a hashing algorithm needs to be chosen for the StorageHasher.

frame_support provides a few options:

  • Identity
  • Blake2_128
  • Blake2_128Concat
  • Blake2_256
  • Twox64Concat
  • Twox128
  • Twox256

(edit: the non-*Concat versions are being deprecated)

Where Identity means using the key directly (with no hashing), Blake2_* means using some variation of the BLAKE2 algorithm, and Twox* means using some variation of the xxHash algorithm.

The question here is: what criteria would one take into account when choosing BLAKE2 vs xxHash for their StorageHasher object? When to choose one over the other?

1
  • 1
    Commenting here as I'm not an expert...Blake2 is cryptographic and Twox is not. I'd say that's a matter of security x speed. Commented Aug 11, 2022 at 17:21

2 Answers 2

9

To complete the answers:

The deciding factor to choose Blake2 (cryptographic hashing function) over xxHash (simple hashing function) is whether the input to be hashed is set by the users or by the system. In other words, the part of the storage key that can be "choosen" by users has to be randomly and uniformly distributed (aka secure) and for that we need a cryptographic hashing function. We need this property to avoid having unbalanced state tries in the chain storage.

For example, if xxHash was used as the StorageHasher for Account IDs, an attacker could create an unbalanced state trie, with a vanity account generator and some tokens to spare, just creating accounts with account IDs sharing the same trie prefix.

This article by Shawn is a very good dive on the topic of storage keys.

3

From the documentation. Blake2 is a cryptographic hasher and it's slow while Twox is non-cryptographic and fast.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.