-
Notifications
You must be signed in to change notification settings - Fork 3
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
feat: solidity verifier usage example #16
Merged
rkdud007
merged 6 commits into
HerodotusDev:main
from
merklefruit:nico/feat/sol-example
Sep 17, 2024
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cd0981d
chore: forge init
merklefruit c9a73a4
forge install: forge-std
merklefruit c5ba68c
feat: solidity verifier example
merklefruit 08ff94a
chore: rm unnecessary ci script
merklefruit 1a425b5
chore: added docs, rm unused fn
merklefruit efb3ea8
feat: customizable rpc
merklefruit 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
There are no files selected for viewing
This file contains 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,3 @@ | ||
[submodule "examples/eth/lib/forge-std"] | ||
path = examples/eth/lib/forge-std | ||
url = https://github.com/foundry-rs/forge-std |
This file contains 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 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,14 @@ | ||
# Compiler files | ||
cache/ | ||
out/ | ||
|
||
# Ignores development broadcast logs | ||
!/broadcast | ||
/broadcast/*/31337/ | ||
/broadcast/**/dry-run/ | ||
|
||
# Docs | ||
docs/ | ||
|
||
# Dotenv file | ||
.env |
This file contains 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 @@ | ||
# Ethereum Trie Proof: Solidity example |
This file contains 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,6 @@ | ||
[profile.default] | ||
src = "src" | ||
out = "out" | ||
libs = ["lib"] | ||
|
||
# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options |
This file contains 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,20 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.25; | ||
|
||
import {MerkleTrie} from "./lib/MerkleTrie.sol"; | ||
|
||
contract Prover { | ||
constructor() {} | ||
|
||
function get(bytes memory _key, bytes memory _proof, bytes32 _root) public pure returns (bool, bytes memory) { | ||
return MerkleTrie.get(_key, _proof, _root); | ||
} | ||
|
||
function verifyInclusionProof(bytes memory _key, bytes memory _value, bytes memory _proof, bytes32 _root) | ||
public | ||
pure | ||
returns (bool) | ||
{ | ||
return MerkleTrie.verifyInclusionProof(_key, _value, _proof, _root); | ||
} | ||
} |
This file contains 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,179 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.25; | ||
|
||
/** | ||
* @title BytesUtils | ||
*/ | ||
library BytesUtils { | ||
/** | ||
* | ||
* Internal Functions * | ||
* | ||
*/ | ||
function slice(bytes memory _bytes, uint256 _start, uint256 _length) internal pure returns (bytes memory) { | ||
unchecked { | ||
require(_length + 31 >= _length, "slice_overflow"); | ||
require(_start + _length >= _start, "slice_overflow"); | ||
require(_bytes.length >= _start + _length, "slice_outOfBounds"); | ||
|
||
bytes memory tempBytes; | ||
|
||
assembly { | ||
switch iszero(_length) | ||
case 0 { | ||
// Get a location of some free memory and store it in tempBytes as | ||
// Solidity does for memory variables. | ||
tempBytes := mload(0x40) | ||
|
||
// The first word of the slice result is potentially a partial | ||
// word read from the original array. To read it, we calculate | ||
// the length of that partial word and start copying that many | ||
// bytes into the array. The first word we copy will start with | ||
// data we don't care about, but the last `lengthmod` bytes will | ||
// land at the beginning of the contents of the new array. When | ||
// we're done copying, we overwrite the full first word with | ||
// the actual length of the slice. | ||
let lengthmod := and(_length, 31) | ||
|
||
// The multiplication in the next line is necessary | ||
// because when slicing multiples of 32 bytes (lengthmod == 0) | ||
// the following copy loop was copying the origin's length | ||
// and then ending prematurely not copying everything it should. | ||
let mc := add(add(tempBytes, lengthmod), mul(0x20, iszero(lengthmod))) | ||
let end := add(mc, _length) | ||
|
||
for { | ||
// The multiplication in the next line has the same exact purpose | ||
// as the one above. | ||
let cc := add(add(add(_bytes, lengthmod), mul(0x20, iszero(lengthmod))), _start) | ||
} lt(mc, end) { | ||
mc := add(mc, 0x20) | ||
cc := add(cc, 0x20) | ||
} { mstore(mc, mload(cc)) } | ||
|
||
mstore(tempBytes, _length) | ||
|
||
//update free-memory pointer | ||
//allocating the array padded to 32 bytes like the compiler does now | ||
mstore(0x40, and(add(mc, 31), not(31))) | ||
} | ||
//if we want a zero-length slice let's just return a zero-length array | ||
default { | ||
tempBytes := mload(0x40) | ||
|
||
//zero out the 32 bytes slice we are about to return | ||
//we need to do it because Solidity does not garbage collect | ||
mstore(tempBytes, 0) | ||
|
||
mstore(0x40, add(tempBytes, 0x20)) | ||
} | ||
} | ||
|
||
return tempBytes; | ||
} | ||
} | ||
|
||
function slice(bytes memory _bytes, uint256 _start) internal pure returns (bytes memory) { | ||
unchecked { | ||
if (_bytes.length - _start == 0) { | ||
return bytes(""); | ||
} | ||
|
||
return slice(_bytes, _start, _bytes.length - _start); | ||
} | ||
} | ||
|
||
function toBytes32PadLeft(bytes memory _bytes) internal pure returns (bytes32) { | ||
unchecked { | ||
bytes32 ret; | ||
uint256 len = _bytes.length <= 32 ? _bytes.length : 32; | ||
assembly { | ||
ret := shr(mul(sub(32, len), 8), mload(add(_bytes, 32))) | ||
} | ||
return ret; | ||
} | ||
} | ||
|
||
function toBytes32(bytes memory _bytes) internal pure returns (bytes32) { | ||
unchecked { | ||
if (_bytes.length < 32) { | ||
bytes32 ret; | ||
assembly { | ||
ret := mload(add(_bytes, 32)) | ||
} | ||
return ret; | ||
} | ||
|
||
return abi.decode(_bytes, (bytes32)); // will truncate if input length > 32 bytes | ||
} | ||
} | ||
|
||
function toUint256(bytes memory _bytes) internal pure returns (uint256) { | ||
return uint256(toBytes32(_bytes)); | ||
} | ||
|
||
function toUint24(bytes memory _bytes, uint256 _start) internal pure returns (uint24) { | ||
require(_start + 3 >= _start, "toUint24_overflow"); | ||
require(_bytes.length >= _start + 3, "toUint24_outOfBounds"); | ||
uint24 tempUint; | ||
|
||
assembly { | ||
tempUint := mload(add(add(_bytes, 0x3), _start)) | ||
} | ||
|
||
return tempUint; | ||
} | ||
|
||
function toUint8(bytes memory _bytes, uint256 _start) internal pure returns (uint8) { | ||
require(_start + 1 >= _start, "toUint8_overflow"); | ||
require(_bytes.length >= _start + 1, "toUint8_outOfBounds"); | ||
uint8 tempUint; | ||
|
||
assembly { | ||
tempUint := mload(add(add(_bytes, 0x1), _start)) | ||
} | ||
|
||
return tempUint; | ||
} | ||
|
||
function toAddress(bytes memory _bytes, uint256 _start) internal pure returns (address) { | ||
require(_start + 20 >= _start, "toAddress_overflow"); | ||
require(_bytes.length >= _start + 20, "toAddress_outOfBounds"); | ||
address tempAddress; | ||
|
||
assembly { | ||
tempAddress := div(mload(add(add(_bytes, 0x20), _start)), 0x1000000000000000000000000) | ||
} | ||
|
||
return tempAddress; | ||
} | ||
|
||
function toNibbles(bytes memory _bytes) internal pure returns (bytes memory) { | ||
unchecked { | ||
bytes memory nibbles = new bytes(_bytes.length * 2); | ||
|
||
for (uint256 i = 0; i < _bytes.length; i++) { | ||
nibbles[i * 2] = _bytes[i] >> 4; | ||
nibbles[i * 2 + 1] = bytes1(uint8(_bytes[i]) % 16); | ||
} | ||
|
||
return nibbles; | ||
} | ||
} | ||
|
||
function fromNibbles(bytes memory _bytes) internal pure returns (bytes memory) { | ||
unchecked { | ||
bytes memory ret = new bytes(_bytes.length / 2); | ||
|
||
for (uint256 i = 0; i < ret.length; i++) { | ||
ret[i] = (_bytes[i * 2] << 4) | (_bytes[i * 2 + 1]); | ||
} | ||
|
||
return ret; | ||
} | ||
} | ||
|
||
function equal(bytes memory _bytes, bytes memory _other) internal pure returns (bool) { | ||
return keccak256(_bytes) == keccak256(_other); | ||
} | ||
} |
Oops, something went wrong.
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.
seems this function is not used, can we call this function on the example? or can remove it
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.
yup, removed, it's doing the same thing under the hood