@merkleforge/hash
v0.2.3
Published
SHA-256, Keccak-256, and BLAKE3 hash functions for Merkle trees, with domain-separated leaf and node hashing. Pure JS on @noble/hashes. Part of MerkleForge.
Maintainers
Readme
@merkleforge/hash provides ready-made HashFunction adapters in pure
JavaScript. SHA-256 and BLAKE3 come from
@noble/hashes; Keccak-256 uses
MerkleForge's own unrolled sponge, about 2x faster than noble's on the short
inputs trees hash (measured) and tested against it byte for byte. Every tree in
@merkleforge/variants takes one, so switching algorithms is a one-word
change, and each produces the same digests as the Rust merkleforge-hash crate.
Formerly published as
@1888-labs/merkle-hash(deprecated). From 0.2.0, install@merkleforge/hash; the API is unchanged.
Install
npm i @merkleforge/hash @merkleforge/coreUsage
import { utf8 } from '@merkleforge/core'
import { Blake3, Keccak256, Sha256 } from '@merkleforge/hash'
import { BinaryMerkleTree } from '@merkleforge/variants'
Keccak256.hash(utf8('leaf')) // Keccak-256(0x00 || "leaf")
const tree = BinaryMerkleTree.fromLeaves(Blake3, [utf8('a'), utf8('b')])Adapters
| Adapter | Leaf hash | Node hash | Use it for |
|---------|-----------|-----------|------------|
| Sha256 | SHA-256(0x00 \|\| data) | SHA-256(0x01 \|\| left \|\| right) | General use, Bitcoin-style systems |
| Keccak256 | Keccak-256(0x00 \|\| data) | Keccak-256(0x01 \|\| left \|\| right) | Ethereum, Solidity verifiers, Patricia tries |
| Blake3 | BLAKE3 derive_key leaf context | BLAKE3 derive_key node context | Systems already built on BLAKE3 |
The 0x00 / 0x01 prefixes keep a leaf from being passed off as an internal
node (second-preimage protection). With Keccak256, MerklePatriciaTrie
hashes nodes with plain Keccak-256, so its roots match Ethereum.
Keccak256 also implements the optional batch methods hashNodePairs and
hashLeaves, which trees use to hash a whole layer without allocating a
digest per node.
keccak256(data, more?) is plain Keccak-256 with no domain prefix (of
data || more when more is given), the same as keccak_256 from
@noble/hashes.
For WebAssembly hashing that builds trees 3–7x faster again (6.5x at 131,072
leaves, see BENCHMARKS.md), use
@merkleforge/hash-wasm,
which returns the same digests.
Part of MerkleForge
MerkleForge is a set of authenticated data structures for Rust and TypeScript with matching roots and proofs across languages. All TypeScript packages share one version.
| Package | What it holds |
|---------|---------------|
| @merkleforge/core | Interfaces, proof types, errors, codecs, and byte helpers |
| @merkleforge/hash | SHA-256, Keccak-256, and BLAKE3 adapters |
| @merkleforge/hash-wasm | WebAssembly Keccak-256 and SHA-256, same digests, faster |
| @merkleforge/variants | Binary, Sparse, and Patricia trees, multiproofs, codecs |
| @merkleforge/ethereum | Verify eth_getProof account and storage proofs |
| @merkleforge/openzeppelin | OpenZeppelin-compatible StandardMerkleTree and SimpleMerkleTree |
Staging builds from develop are published under the next tag
(npm i @merkleforge/hash@next). MerkleForge has not been independently
audited; report vulnerabilities via the security policy.
License
Licensed under either of MIT or Apache-2.0 at your option.
Maintained by 1888 Labs and open-source contributors.
