Skip to content

Instantly share code, notes, and snippets.

@cre-mer
Last active November 20, 2024 15:25
Show Gist options
  • Save cre-mer/b6cdd9174dcb0d6f3d4ac0f22911090b to your computer and use it in GitHub Desktop.
Save cre-mer/b6cdd9174dcb0d6f3d4ac0f22911090b to your computer and use it in GitHub Desktop.
An imagination of how Solidity can implement a standalone bytes in bytes out function.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;
// @dev Standalone myVerifier function written in Solidity
function myVerifier(uint256 magicNumber) pure returns (uint256) {
return magicNumber;
}
// @note The compiler does not allow compiling such function, however, I wrote a Yul object that I can compile and deploy
// @dev This code compiles to the following deployment calldata: 0x60808060405234601757601c9081601d823930815050f35b600080fdfe6080806040526020361015601257600080fd5b6020906000358152f3
object "bytes_in_bytes_out" {
// function constructor
code {
{
let _1 := memoryguard(0x80)
mstore(64, _1)
if callvalue() { revert(0, 0) }
let _2 := datasize("myVerifier")
codecopy(_1, dataoffset("myVerifier"), _2)
setimmutable(_1, "funcrary_deploy_address", address())
return(_1, _2)
}
}
// the actual function object
object "myVerifier" {
code {
// take in a uint256 and return it
let _1 := memoryguard(0x80)
mstore(64, _1)
// make sure that the calldatasize is 32 bytes, i.e., ensure we passed a valid uint256
if iszero(lt(calldatasize(), 32)) {
// load calldata from pos 0
mstore(_1, calldataload(0))
// return 32 bytes from memory at 0x80
return(_1, 32)
}
// revert if calldata is invalid
revert(0,0)
}
}
}
// Now we can call the function from a contract like so
import { myVerifier } from "./myVerifier.sol";
contract Verifier {
// @dev returns 42
// @param myVerifier the address of the deployed standalone function
function isValidProof(myVerifier myVerifier_) external returns(uint256) {
return _myVerifier(42);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment