Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix indentation in MerkleProof.sol and remove mock contract
  • Loading branch information
yondonfu committed Jun 15, 2017
commit 30e202313d9c4f6bacac64b5a634f312424a7be7
52 changes: 26 additions & 26 deletions contracts/MerkleProof.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@ pragma solidity ^0.4.11;
* @note Based on https://github.com/ameensol/merkle-tree-solidity/blob/master/src/MerkleProof.sol
*/
library MerkleProof {
/*
* @dev 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.
* @param _proof Merkle proof containing sibling hashes on the branch from the leaf to the root of the Merkle tree
* @param _root Merkle root
* @param _leaf Leaf of Merkle tree
*/
function verifyProof(bytes _proof, bytes32 _root, bytes32 _leaf) constant returns (bool) {
bytes32 proofElement;
bytes32 computedHash = _leaf;
/*
* @dev 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.
* @param _proof Merkle proof containing sibling hashes on the branch from the leaf to the root of the Merkle tree
* @param _root Merkle root
* @param _leaf Leaf of Merkle tree
*/
function verifyProof(bytes _proof, bytes32 _root, bytes32 _leaf) constant returns (bool) {
bytes32 proofElement;
bytes32 computedHash = _leaf;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing this implementation to https://github.com/raiden-network/raiden/blob/master/raiden/smart_contracts/NettingChannelLibrary.sol#L268, it seems the only difference is whether to hash the leaf element before entering the loop.

I guess the difference is on how you build the tree: is it the tree who hashes every element initially to form the leaves, or should the tree's user do it (as is the case here)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense for _leaf to be the hash of the leaf element instead of the actual leaf element since it avoids an additional keccak256 operation on-chain - the more logic that can be pushed off-chain the better imo. Then, the comments/documentation can make it clearer that _leaf should be the hash of the leaf element and not the actual leaf element.


for (uint256 i = 32; i <= _proof.length; i += 32) {
assembly {
// Load the current element of the proof
proofElement := mload(add(_proof, i))
}
for (uint256 i = 32; i <= _proof.length; i += 32) {
assembly {
// Load the current element of the proof
proofElement := mload(add(_proof, i))
}

if (computedHash < proofElement) {
// Hash(current computed hash + current element of the proof)
computedHash = sha3(computedHash, proofElement);
} else {
// Hash(current element of the proof + current computed hash)
computedHash = sha3(proofElement, computedHash);
}
}

// Check if the computed hash (root) is equal to the provided root
return computedHash == _root;
if (computedHash < proofElement) {
// Hash(current computed hash + current element of the proof)
computedHash = sha3(computedHash, proofElement);
} else {
// Hash(current element of the proof + current computed hash)
computedHash = sha3(proofElement, computedHash);
}
}

// Check if the computed hash (root) is equal to the provided root
return computedHash == _root;
}
}
12 changes: 0 additions & 12 deletions test/helpers/MerkleProofMock.sol

This file was deleted.