-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Merkle proof library with tests and docs #260
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 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3c4d0d0
MerkleProof library and initial stubbed out tests
yondonfu 2e0bd06
Add tests, docs and MerkleTree helper
yondonfu 30e2023
Fix indentation in MerkleProof.sol and remove mock contract
yondonfu bc3db5d
Fix weird indent issue for inline assembly
yondonfu 863ad48
Check proof length multiple of 32. Use keccak256 instead of sha3
yondonfu 24323d3
MerkleTree util class hashes elements
yondonfu bcda5bf
Merge branch 'master' into feature/merkleproof
yondonfu 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
Add tests, docs and MerkleTree helper
- Loading branch information
commit 2e0bd06da2bc10bf88d1952b211a4b3a7e06d78d
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| MerkleProof | ||
| ============================================= | ||
|
|
||
| Merkle proof verification for leaves of a Merkle tree. | ||
|
|
||
| verifyProof(bytes _proof, bytes32 _root, bytes32 _leaf) internal constant returns (bool) | ||
| """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" | ||
|
|
||
| Verifies a Merkle proof proving the existence of a leaf in a Merkle tree. Assumes that each pair of leaves and each pair of pre-images is sorted. |
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 |
|---|---|---|
| @@ -1,54 +1,43 @@ | ||
| var MerkleProofMock = artifacts.require("./helpers/MerkleProofMock.sol"); | ||
| var MerkleProof = artifacts.require("./MerkleProof.sol"); | ||
|
|
||
| contract('MerkleProof', function(accounts) { | ||
| let merkleProof; | ||
|
|
||
| before(async function() { | ||
| merkleProof = await MerkleProofMock.new(); | ||
| }); | ||
|
|
||
| describe("verifyProof", function() { | ||
| it("should return true for a valid Merkle proof given even number of leaves", async function() { | ||
| // const elements = ["a", "b", "c", "d"].map(el => sha3(el)); | ||
| // const merkleTree = new MerkleTree(elements); | ||
| import { sha3 } from "ethereumjs-util"; | ||
| import MerkleTree from "./helpers/merkleTree.js"; | ||
|
|
||
| // const root = merkleTree.getHexRoot(); | ||
|
|
||
| // const proof = merkleTree.getHexProof(elements[0]); | ||
|
|
||
| // const leaf = merkleTree.bufToHex(elements[0]); | ||
| contract('MerkleProof', function(accounts) { | ||
| let merkleProof; | ||
|
|
||
| // const validProof = await merkleProof.verifyProof(proof, root, leaf); | ||
| // assert.isOk(validProof, "verifyProof did not return true for a valid proof given even number of leaves"); | ||
| }); | ||
| before(async function() { | ||
| merkleProof = await MerkleProof.new(); | ||
| }); | ||
|
|
||
| it("should return true for a valid Merkle proof given odd number of leaves", async function () { | ||
| // const elements = ["a", "b", "c"].map(el => sha3(el)); | ||
| // const merkleTree = new MerkleTree(elements); | ||
| describe("verifyProof", function() { | ||
| it("should return true for a valid Merkle proof", async function() { | ||
| const elements = ["a", "b", "c", "d"].map(el => sha3(el)); | ||
| const merkleTree = new MerkleTree(elements); | ||
|
|
||
| // const root = merkleTree.getHexRoot(); | ||
| const root = merkleTree.getHexRoot(); | ||
|
|
||
| // const proof = merkleTree.getHexProof(elements[0]); | ||
| const proof = merkleTree.getHexProof(elements[0]); | ||
|
|
||
| // const leaf = merkleTree.bufToHex(elements[0]); | ||
| const leaf = merkleTree.bufToHex(elements[0]); | ||
|
|
||
| // const validProof = await merkleProof.verifyProof(proof, root, leaf); | ||
| // assert.isOk(validProof, "verifyProof did not return true for a valid proof given odd number of leaves"); | ||
| }); | ||
| const result = await merkleProof.verifyProof(proof, root, leaf); | ||
| assert.isOk(result, "verifyProof did not return true for a valid proof"); | ||
| }); | ||
|
|
||
| it("should return false for an invalid Merkle proof", async function() { | ||
| // const elements = ["a", "b", "c"].map(el => sha3(el)); | ||
| // const merkleTree = new MerkleTree(elements); | ||
| it("should return false for an invalid Merkle proof", async function() { | ||
| const elements = ["a", "b", "c"].map(el => sha3(el)); | ||
| const merkleTree = new MerkleTree(elements); | ||
|
|
||
| // const root = merkleTree.getHexRoot(); | ||
| const root = merkleTree.getHexRoot(); | ||
|
|
||
| // const proof = merkleTree.getHexProof(elements[0]); | ||
| // const badProof = proof.slice(0, proof.length - 32); | ||
| const proof = merkleTree.getHexProof(elements[0]); | ||
| const badProof = proof.slice(0, proof.length - 32); | ||
|
|
||
| // const leaf = merkleTree.bufToHex(elements[0]); | ||
| const leaf = merkleTree.bufToHex(elements[0]); | ||
|
|
||
| // const validProof = await merkleProof.verifyProof(badProof, root, leaf); | ||
| // assert.isNotOk(validProof, "verifyProof did not return false for an invalid proof"); | ||
| }); | ||
| const result = await merkleProof.verifyProof(badProof, root, leaf); | ||
| assert.isNotOk(result, "verifyProof did not return false for an invalid proof"); | ||
| }); | ||
| }); | ||
| }); | ||
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 |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { sha3 } from "ethereumjs-util"; | ||
|
|
||
| export default class MerkleTree { | ||
| constructor(elements) { | ||
| // Filter empty strings | ||
| this.elements = elements.filter(el => el); | ||
|
|
||
| // Check if elements are 32 byte buffers | ||
| if (this.elements.some(el => el.length !== 32 || !Buffer.isBuffer(el))) { | ||
| throw new Error("Elements must be 32 byte buffers"); | ||
| } | ||
|
|
||
| // Deduplicate elements | ||
| this.elements = this.bufDedup(this.elements); | ||
| // Sort elements | ||
| this.elements.sort(Buffer.compare); | ||
|
|
||
| // Create layers | ||
| this.layers = this.getLayers(this.elements); | ||
| } | ||
|
|
||
| getLayers(elements) { | ||
| if (elements.length == 0) { | ||
| return [[""]]; | ||
| } | ||
|
|
||
| const layers = []; | ||
| layers.push(elements); | ||
|
|
||
| // Get next layer until we reach the root | ||
| while (layers[layers.length - 1].length > 1) { | ||
| layers.push(this.getNextLayer(layers[layers.length - 1])); | ||
| } | ||
|
|
||
| return layers; | ||
| } | ||
|
|
||
| getNextLayer(elements) { | ||
| return elements.reduce((layer, el, idx, arr) => { | ||
| if (idx % 2 === 0) { | ||
| // Hash the current element with its pair element | ||
| layer.push(this.combinedHash(el, arr[idx + 1])); | ||
| } | ||
|
|
||
| return layer; | ||
| }, []); | ||
| } | ||
|
|
||
| combinedHash(first, second) { | ||
| if (!first) { return second; } | ||
| if (!second) { return first; } | ||
|
|
||
| return sha3(this.sortAndConcat(first, second)); | ||
| } | ||
|
|
||
| getRoot() { | ||
| return this.layers[this.layers.length - 1][0]; | ||
| } | ||
|
|
||
| getHexRoot() { | ||
| return this.bufToHex(this.getRoot()); | ||
| } | ||
|
|
||
| getProof(el) { | ||
| let idx = this.bufIndexOf(el, this.elements); | ||
|
|
||
| if (idx === -1) { | ||
| throw new Error("Element does not exist in Merkle tree"); | ||
| } | ||
|
|
||
| return this.layers.reduce((proof, layer) => { | ||
| const pairElement = this.getPairElement(idx, layer); | ||
|
|
||
| if (pairElement) { | ||
| proof.push(pairElement); | ||
| } | ||
|
|
||
| idx = Math.floor(idx / 2); | ||
|
|
||
| return proof; | ||
| }, []); | ||
| } | ||
|
|
||
| getHexProof(el) { | ||
| const proof = this.getProof(el); | ||
|
|
||
| return this.bufArrToHex(proof); | ||
| } | ||
|
|
||
| getPairElement(idx, layer) { | ||
| const pairIdx = idx % 2 === 0 ? idx + 1 : idx - 1; | ||
|
|
||
| if (pairIdx < layer.length) { | ||
| return layer[pairIdx]; | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| bufIndexOf(el, arr) { | ||
| for (let i = 0; i < arr.length; i++) { | ||
| if (el.equals(arr[i])) { | ||
| return i; | ||
| } | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| bufDedup(elements) { | ||
| return elements.filter((el, idx) => { | ||
| return this.bufIndexOf(el, elements) === idx; | ||
| }); | ||
| } | ||
|
|
||
| bufToHex(el) { | ||
| if (!Buffer.isBuffer(el)) { | ||
| throw new Error("Element is not a buffer"); | ||
| } | ||
|
|
||
| return "0x" + el.toString("hex"); | ||
| } | ||
|
|
||
| bufArrToHex(arr) { | ||
| if (arr.some(el => !Buffer.isBuffer(el))) { | ||
| throw new Error("Array is not an array of buffers"); | ||
| } | ||
|
|
||
| return "0x" + arr.map(el => el.toString("hex")).join(""); | ||
| } | ||
|
|
||
| sortAndConcat(...args) { | ||
| return Buffer.concat([...args].sort(Buffer.compare)); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the comment above. Shouldn't the
MerkleTreeclass be responsible for initially hashing theelements?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I think that makes sense from a usability standpoint. A user can generate a merkle tree off-chain by passing in an array of elements and then when the user wants to submit a proof the user uses the same merkle tree to generate a proof and submits it along with the hash of the element to be proved