Skip to content
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

Add test with high branching factor #162

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 package/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.82
0.1.83
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "kontrol"
version = "0.1.82"
version = "0.1.83"
description = "Foundry integration for KEVM"
authors = [
"Runtime Verification, Inc. <contact@runtimeverification.com>",
Expand Down
3 changes: 2 additions & 1 deletion src/kontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
if TYPE_CHECKING:
from typing import Final

VERSION: Final = '0.1.82'

VERSION: Final = '0.1.83'
1 change: 1 addition & 0 deletions src/tests/integration/test-data/foundry-prove-all
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ LoopsTest.test_sum_100()
LoopsTest.test_sum_1000()
LoopsTest.testSumToN(uint256)
LoopsTest.testSumToNBroken(uint256)
MerkleProofTest.testValidateMerkleProof(bytes32,uint256,bytes32,bytes32,bytes32,bytes32)
MethodDisambiguateTest.test_method_call()
MockCallTest.testMockCall()
MockCallTest.testMockCalls()
Expand Down
1 change: 1 addition & 0 deletions src/tests/integration/test-data/foundry-prove-skip
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ LoopsTest.test_sum_100()
LoopsTest.test_sum_1000()
LoopsTest.testSumToN(uint256)
LoopsTest.testSumToNBroken(uint256)
MerkleProofTest.testValidateMerkleProof(bytes32,uint256,bytes32,bytes32,bytes32,bytes32)
MockCallTest.testMockCall()
MockCallTest.testMockCalls()
MockCallTest.testMockCallValue()
Expand Down
69 changes: 69 additions & 0 deletions src/tests/integration/test-data/foundry/test/MerkleProofTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity =0.8.13;

import "forge-std/Test.sol";

contract MerkleProofTest is Test {

/**
* The purpose of this test is to evaluate how well we handle branching.
* When we assume that _validateMerkleProof holds, the execution splits
* into 2 ** proof.length (in this case, 8) branches. We want to be able
* to handle this amount of branching without losing information, so we
* can still prove that it holds in the final assertTrue.
*
* Increase the length of the proof to evaluate scalability.
*/
function testValidateMerkleProof(
bytes32 leaf,
uint256 index,
bytes32 root,
bytes32 proofElement0,
bytes32 proofElement1,
bytes32 proofElement2
) external {
uint256 proofLength = 3;

bytes32[] memory proof = new bytes32[](proofLength);
proof[0] = proofElement0;
proof[1] = proofElement1;
proof[2] = proofElement2;

vm.assume(index < 2 ** proof.length);

vm.assume(_validateMerkleProof(leaf, index, root, proof));

assertTrue(_validateMerkleProof(leaf, index, root, proof));
}

/**
* Checks that the proof is valid for a Merkle tree with the given root
* where the given leaf is at the given index.
*/
function _validateMerkleProof(
bytes32 leaf,
uint256 index,
bytes32 root,
bytes32[] memory proof
) internal pure returns (bool) {
// Number of leaves is exponential on the tree depth
require(index < 2 ** proof.length);

bytes32 hash = leaf;

for (uint256 i; i < proof.length; i++) {
if (index % 2 == 0) {
// If index is even, proof element is to the right
hash = keccak256(abi.encodePacked(hash, proof[i]));
} else {
// If index is odd, proof element is to the left
hash = keccak256(abi.encodePacked(proof[i], hash));
}

// Go up one level in the tree
index = index / 2;
}

return hash == root;
}
}
Loading