Allocation-free Merkle in Motoko: independent validation + supporting research - #1
Open
KYounesMercatura wants to merge 3 commits into
Open
Allocation-free Merkle in Motoko: independent validation + supporting research#1KYounesMercatura wants to merge 3 commits into
KYounesMercatura wants to merge 3 commits into
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Allocation-free Merkle in Motoko - independent validation + supporting research
This PR adds a small, self-contained research subdirectory (
research/allocfree-merkle/).It's offered as a contribution and a thank-you, not a replacement for anything: Timo
Hanke has already demonstrated allocation-free on-chain Merkle hashing in his Bitcoin work,
and this is us reproducing it independently, confirming it against real chain data, and
writing down a couple of extra data points we hadn't seen recorded. The credit for the
approach is his - we're validating and exploring around it to see if any of it is useful to
hand back.
What's here
A double-SHA256 Merkle implementation with O(1) allocation (one fixed scratch buffer,
no per-hash garbage), plus the streaming and naive variants for comparison, full tests, a
codegen for the unrolled hasher, and an on-chain bench canister.
What we set out to confirm / add
0, 170, 546, 100000, and 350000 (1,795 tx - exercises odd-node duplication across several
levels), and the SHA-256 core matches NIST vectors. A Python
hashlibre-computationcross-checks the vectors against the chain before the Motoko tests run.
performanceCounter) -an angle complementing the allocation focus. The allocation result is the important part;
these are just extra reference numbers.
IC0505,since rolled back) flags fully-unrolled SHA-256 (~2,000+ locals). We include both an
unrolled build and a loop build (~50 locals) that stays under the cap at some instruction
cost - context in case that validation ever returns. We frame this as a speed / locals /
allocation trade-off in
RESULTS.md.mocleaves optimization to awasm-opt -O3post-pass (easy to misswith a prebuilt-wasm deploy); and keeping the message schedule in
letlocals rather thana
[var Nat32]array measurably helps in Motoko.Measurements (real mainnet IC, after
wasm-opt -O3, for reference)Both land in the same instruction ballpark; the property of interest is the flat 1,128-byte,
O(1) allocation - the zero-GC-pressure result the approach targets - reproduced here
independently. (Benchmarked against the public
sha20.2.1; we didn't have the latestMerkle work to compare against, so please read the numbers as validation, not a comparison.)
Use
Free to use, fork, or fold upstream into
sha2. Feedback and corrections very welcome -this only exists because of Timo's work, and we're happy to hand any of it back.