Skip to content

fix: sha256 leaves to avoid 2nd pre-image attack #77

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 2 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion contracts/src/bridge/merkle/MerkleProof.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contract MerkleProof {
bytes memory data,
bytes32 merkleRoot
) public pure returns (bool) {
return validateProof(proof, keccak256(data), merkleRoot);
return validateProof(proof, sha256(data), merkleRoot);
}

/** @dev Calculates merkle root from proof.
Expand Down
24 changes: 13 additions & 11 deletions contracts/src/bridge/merkle/MerkleTreeHistory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,34 @@ contract MerkleTreeHistory {
* @param data The data to insert in the merkle tree.
*/
function append(bytes memory data) public {
bytes32 leaf = keccak256(data);
count += 1;
uint256 size = count;
// Differentiate leaves from interior nodes with different
// hash functions to prevent 2nd order pre-image attack.
// https://flawed.net.nz/2018/02/21/attacking-merkle-trees-with-a-second-preimage-attack/
bytes32 leaf = sha256(data);
uint256 size = count + 1;
count = size;
uint256 hashBitField = (size ^ (size - 1)) & size;

for (uint256 height = 0; height < 64; height++) {
if ((hashBitField & 1) == 1) {
branch[height] = leaf;
return;
}
uint256 height;
while ((hashBitField & 1) == 0) {
bytes32 node = branch[height];
// effecient hash
if (node > leaf)
assembly {
// effecient hash
mstore(0x00, leaf)
mstore(0x20, node)
leaf := keccak256(0x00, 0x40)
}
else
assembly {
// effecient hash
mstore(0x00, node)
mstore(0x20, leaf)
leaf := keccak256(0x00, 0x40)
}
hashBitField /= 2;
height = height + 1;
}
branch[height] = leaf;
}

/** @dev Saves the merkle root state in history and resets.
Expand Down Expand Up @@ -98,8 +100,8 @@ contract MerkleTreeHistory {
uint256 height = 0;
bool isFirstHash = true;
while (size > 0) {
// avoid redundant calculation
if ((size & 1) == 1) {
// avoid redundant calculation
if (isFirstHash) {
node = branch[height];
isFirstHash = false;
Expand Down
2 changes: 1 addition & 1 deletion contracts/test/bridge/merkle/MerkleTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class MerkleTree {
* @return node The `sha3` (A.K.A. `keccak256`) hash of `first, ...params` as a 32-byte hex string.
*/
public static makeLeafNode(data: string): string {
const result = ethers.utils.keccak256(data);
const result = ethers.utils.sha256(data);

if (!result) {
throw new Error("Leaf node must not be empty");
Expand Down
2 changes: 1 addition & 1 deletion contracts/test/bridge/merkle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe("Merkle", function () {
});
it("Should correctly verify all nodes in the tree", async () => {
for (var message of data) {
const leaf = ethers.utils.keccak256(message);
const leaf = ethers.utils.sha256(message);
proof = mt.getHexProof(leaf);
const validation = await merkleProof.validateProof(proof, message,rootOnChain);
expect(validation).equal(true);
Expand Down