hash module provides Blake3 hashing utilities used throughout the blockchain for cryptographic operations. It includes the core Hash type and functions for hashing arbitrary data.
Overview
Blake3 is a cryptographically secure hash function that produces 256-bit (32-byte) hashes. This module wraps Blake3 with convenience types and methods for blockchain operations.Types
H256
Hash
H256 with Display and Debug formatting, as well as serialization support.
Constants
The zero hash (all zeros).
Methods
Creates a new Hash from raw bytes.
Returns a reference to the underlying bytes.
Converts the hash to a hexadecimal string.
Parses a Hash from a hexadecimal string.Returns an error if the string is not valid hex or not exactly 32 bytes.
Hexadecimal string (64 characters)
Trait Implementations
- Clone, Copy, PartialEq, Eq, Hash, Default: Standard derivations
- Serialize, Deserialize: Serde support for JSON/binary serialization
- Display: Formats as “0x” followed by the full hex string
- Debug: Formats as “Hash(0x” followed by the first 8 hex characters
- From and Into: Conversions to/from raw bytes
- AsRef: Allows using Hash as a byte slice
Functions
hash
The data to hash
The Blake3 hash of the input data
Example
hash_concat
An array of byte slices to concatenate and hash
The Blake3 hash of the concatenated data
Example
Usage Examples
Basic Hashing
Hex String Conversion
Hashing Multiple Parts
Using the Zero Hash
Implementation Details
Blake3 Properties
- Speed: Blake3 is one of the fastest cryptographic hash functions
- Security: 256-bit output provides strong collision resistance
- Parallelism: Blake3 can utilize multiple CPU cores for large inputs
- Determinism: Same input always produces same output
Performance Considerations
- Hashing is a CPU-bound operation
- For large amounts of data, consider using
hash_concatinstead of allocating intermediate buffers - Blake3 is significantly faster than SHA-256 while maintaining equivalent security
Source Location
Defined incrates/core/src/hash.rs