From 9b54568ad52b1241ac582a480657672f484875d5 Mon Sep 17 00:00:00 2001 From: clabby Date: Fri, 22 Sep 2023 22:36:05 -0400 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Move=20bindings;=20Start?= =?UTF-8?q?=20`mipsevm`=20revm=20impl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitmodules | 3 + Cargo.lock | 46 +++++++ crates/mipsevm/Cargo.toml | 3 +- crates/mipsevm/bindings/README.md | 16 +++ crates/mipsevm/bindings/bindings.sh | 42 +++++++ crates/mipsevm/bindings/mips_creation.bin | 1 + crates/mipsevm/bindings/optimism | 1 + .../bindings/preimage_oracle_deployed.bin | 1 + crates/mipsevm/src/evm.rs | 118 ++++++++++++++++++ crates/mipsevm/src/lib.rs | 5 + crates/mipsevm/src/mips/instrumented.rs | 25 +--- crates/mipsevm/src/test_utils.rs | 29 +++++ 12 files changed, 266 insertions(+), 24 deletions(-) create mode 100644 .gitmodules create mode 100644 crates/mipsevm/bindings/README.md create mode 100755 crates/mipsevm/bindings/bindings.sh create mode 100644 crates/mipsevm/bindings/mips_creation.bin create mode 160000 crates/mipsevm/bindings/optimism create mode 100644 crates/mipsevm/bindings/preimage_oracle_deployed.bin create mode 100644 crates/mipsevm/src/evm.rs create mode 100644 crates/mipsevm/src/test_utils.rs diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..6d2556e --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "crates/mipsevm/bindings/optimism"] + path = crates/mipsevm/bindings/optimism + url = https://github.com/ethereum-optimism/optimism diff --git a/Cargo.lock b/Cargo.lock index 438ce4c..c375225 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,6 +67,33 @@ dependencies = [ "smol_str", ] +[[package]] +name = "alloy-sol-macro" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7720130c58db647103587b0c39aad4e669eea261e256040301d6c7983fdbb461" +dependencies = [ + "dunce", + "heck", + "proc-macro2", + "quote", + "syn 2.0.32", + "syn-solidity", + "tiny-keccak", +] + +[[package]] +name = "alloy-sol-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eaa7c9a4354b1ff9f1c85adf22802af046e20e4bb55e19b9dc6ca8cbc6f7f4e5" +dependencies = [ + "alloy-primitives", + "alloy-sol-macro", + "const-hex", + "serde", +] + [[package]] name = "anes" version = "0.1.6" @@ -373,6 +400,7 @@ name = "cannon-mipsevm" version = "0.1.0" dependencies = [ "alloy-primitives", + "alloy-sol-types", "anyhow", "criterion", "fnv", @@ -675,6 +703,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "dunce" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b" + [[package]] name = "ecdsa" version = "0.16.8" @@ -1917,6 +1951,18 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn-solidity" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "397f229dc34c7b8231b6ef85502f9ca4e3425b8625e6d403bb74779e6b1917b5" +dependencies = [ + "paste", + "proc-macro2", + "quote", + "syn 2.0.32", +] + [[package]] name = "tap" version = "1.0.1" diff --git a/crates/mipsevm/Cargo.toml b/crates/mipsevm/Cargo.toml index f9de436..d2c12d9 100644 --- a/crates/mipsevm/Cargo.toml +++ b/crates/mipsevm/Cargo.toml @@ -8,10 +8,12 @@ authors.workspace = true [dependencies] alloy-primitives = "0.3.3" +alloy-sol-types = "0.3.2" once_cell = "1.18.0" serde = { version = "1.0.188", features = ["derive"] } serde_json = "1.0.106" fnv = "1.0.7" +revm = { version = "3.3.0", features = ["no_gas_measuring"] } # misc anyhow = "1.0.75" @@ -20,7 +22,6 @@ tracing = "0.1.37" [dev-dependencies] preimage-oracle = { path = "../preimage" } rand = "0.8.5" -revm = "3.3.0" criterion = { version = "0.5.1", features = ["html_reports"] } [features] diff --git a/crates/mipsevm/bindings/README.md b/crates/mipsevm/bindings/README.md new file mode 100644 index 0000000..6eb3fc5 --- /dev/null +++ b/crates/mipsevm/bindings/README.md @@ -0,0 +1,16 @@ +# `cannon-contract-bindings` + +This folder contains the compiled bytecode of the Cannon contracts for deployment on the MIPSEVM. + +## Regenerating Bindings + +Dependencies: +* [`forge`][foundry] +* [`jq`][jq] + +```sh +./bindings.sh +``` + +[foundry]: https://github.com/foundry-rs/foundry +[jq]: https://github.com/jqlang/jq diff --git a/crates/mipsevm/bindings/bindings.sh b/crates/mipsevm/bindings/bindings.sh new file mode 100755 index 0000000..e2fa68c --- /dev/null +++ b/crates/mipsevm/bindings/bindings.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +# This script is used to generate the bindings for the MIPS contracts in the +# Optimism monorepo. + +# The current directory relative to the script. +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + +# Check if a folder with relative path `../optimism` exists and is not empty. +# If it doesn't exist, install the submodules. +if [ ! -d "$DIR/optimism" ]; then + echo "Error: Optimism monorepo not present. Initializing submodules..." + git submodule update --init --recursive +fi + +# Check if `forge` is installed +if ! command -v forge &> /dev/null +then + echo "Error: forge not found. Please install forge and try again." + exit +fi + +CTB="$DIR/optimism/packages/contracts-bedrock" + +cd $CTB && \ + forge install && \ + forge build + +MIPS_ARTIFACT="$CTB/forge-artifacts/MIPS.sol/MIPS.json" +PREIMAGE_ARTIFACT="$CTB/forge-artifacts/PreimageOracle.sol/PreimageOracle.json" + +MIPS_BIN=$(cat $MIPS_ARTIFACT | jq -r '.bytecode.object') +PREIMAGE_DEPLOYED_BIN=$(cat $PREIMAGE_ARTIFACT | jq -r '.deployedBytecode.object') + +echo "Removing old bindings..." +rm $DIR/*.bin +echo "Old bindings removed." + +echo -n "${MIPS_BIN:2}" > $DIR/mips_creation.bin +echo -n "${PREIMAGE_DEPLOYED_BIN:2}" >> $DIR/preimage_oracle_deployed.bin + +echo "Bindings generated successfully." diff --git a/crates/mipsevm/bindings/mips_creation.bin b/crates/mipsevm/bindings/mips_creation.bin new file mode 100644 index 0000000..41dfb42 --- /dev/null +++ b/crates/mipsevm/bindings/mips_creation.bin @@ -0,0 +1 @@ +60a060405234801561001057600080fd5b50604051611eb2380380611eb283398101604081905261002f91610040565b6001600160a01b0316608052610070565b60006020828403121561005257600080fd5b81516001600160a01b038116811461006957600080fd5b9392505050565b608051611e2161009160003960008181608501526115e70152611e216000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c8063155633fe146100465780637dc0d1d01461006b578063f8e0cb96146100af575b600080fd5b610051634000000081565b60405163ffffffff90911681526020015b60405180910390f35b60405173ffffffffffffffffffffffffffffffffffffffff7f0000000000000000000000000000000000000000000000000000000000000000168152602001610062565b6100c26100bd366004611d26565b6100d0565b604051908152602001610062565b60006100da611c53565b608081146100e757600080fd5b604051610600146100f757600080fd5b6064861461010457600080fd5b610184841461011257600080fd5b8535608052602086013560a052604086013560e090811c60c09081526044880135821c82526048880135821c61010052604c880135821c610120526050880135821c61014052605488013590911c61016052605887013560f890811c610180526059880135901c6101a052605a870135901c6101c0526102006101e0819052606287019060005b60208110156101bd57823560e01c8252600490920191602090910190600101610199565b505050806101200151156101db576101d3610619565b915050610611565b6101408101805160010167ffffffffffffffff16905260608101516000906102039082610735565b9050603f601a82901c16600281148061022257508063ffffffff166003145b156102775760006002836303ffffff1663ffffffff16901b846080015163f00000001617905061026c8263ffffffff1660021461026057601f610263565b60005b60ff16826107f1565b945050505050610611565b6101608301516000908190601f601086901c81169190601587901c16602081106102a3576102a3611d92565b602002015192508063ffffffff851615806102c457508463ffffffff16601c145b156102fb578661016001518263ffffffff16602081106102e6576102e6611d92565b6020020151925050601f600b86901c166103b7565b60208563ffffffff16101561035d578463ffffffff16600c148061032557508463ffffffff16600d145b8061033657508463ffffffff16600e145b15610347578561ffff1692506103b7565b6103568661ffff1660106108eb565b92506103b7565b60288563ffffffff1610158061037957508463ffffffff166022145b8061038a57508463ffffffff166026145b156103b7578661016001518263ffffffff16602081106103ac576103ac611d92565b602002015192508190505b60048563ffffffff16101580156103d4575060088563ffffffff16105b806103e557508463ffffffff166001145b15610404576103f68587848761095e565b975050505050505050610611565b63ffffffff6000602087831610610469576104248861ffff1660106108eb565b9095019463fffffffc861661043a816001610735565b915060288863ffffffff161015801561045a57508763ffffffff16603014155b1561046757809250600093505b505b600061047789888885610b6e565b63ffffffff9081169150603f8a1690891615801561049c575060088163ffffffff1610155b80156104ae5750601c8163ffffffff16105b1561058a578063ffffffff16600814806104ce57508063ffffffff166009145b15610505576104f38163ffffffff166008146104ea57856104ed565b60005b896107f1565b9b505050505050505050505050610611565b8063ffffffff16600a03610525576104f3858963ffffffff8a16156112f2565b8063ffffffff16600b03610546576104f3858963ffffffff8a1615156112f2565b8063ffffffff16600c0361055c576104f36113d8565b60108163ffffffff16101580156105795750601c8163ffffffff16105b1561058a576104f38189898861190c565b8863ffffffff1660381480156105a5575063ffffffff861615155b156105da5760018b61016001518763ffffffff16602081106105c9576105c9611d92565b63ffffffff90921660209290920201525b8363ffffffff1663ffffffff146105f7576105f784600184611b06565b610603858360016112f2565b9b5050505050505050505050505b949350505050565b60408051608051815260a051602082015260dc519181019190915260fc51604482015261011c51604882015261013c51604c82015261015c51605082015261017c5160548201526101805161019f5160588301526101a0516101bf5160598401526101d851605a840152600092610200929091606283019190855b60208110156106b857601c8601518452602090950194600490930192600101610694565b506000835283830384a06000945080600181146106d85760039550610700565b8280156106f057600181146106f957600296506106fe565b600096506106fe565b600196505b505b50505081900390207effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1660f89190911b17919050565b60008061074183611baa565b9050600384161561075157600080fd5b6020810190358460051c8160005b601b8110156107b75760208501943583821c6001168015610787576001811461079c576107ad565b600084815260208390526040902093506107ad565b600082815260208590526040902093505b505060010161075f565b5060805191508181146107d257630badf00d60005260206000fd5b5050601f94909416601c0360031b9390931c63ffffffff169392505050565b60006107fb611c53565b60809050806060015160040163ffffffff16816080015163ffffffff1614610884576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601260248201527f6a756d7020696e2064656c617920736c6f74000000000000000000000000000060448201526064015b60405180910390fd5b60608101805160808301805163ffffffff9081169093528583169052908516156108da57806008018261016001518663ffffffff16602081106108c9576108c9611d92565b63ffffffff90921660209290920201525b6108e2610619565b95945050505050565b600063ffffffff8381167fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80850183169190911c821615159160016020869003821681901b830191861691821b92911b018261094857600061094a565b815b90861663ffffffff16179250505092915050565b6000610968611c53565b608090506000816060015160040163ffffffff16826080015163ffffffff16146109ee576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601460248201527f6272616e636820696e2064656c617920736c6f74000000000000000000000000604482015260640161087b565b8663ffffffff1660041480610a0957508663ffffffff166005145b15610a855760008261016001518663ffffffff1660208110610a2d57610a2d611d92565b602002015190508063ffffffff168563ffffffff16148015610a5557508763ffffffff166004145b80610a7d57508063ffffffff168563ffffffff1614158015610a7d57508763ffffffff166005145b915050610b02565b8663ffffffff16600603610aa25760008460030b13159050610b02565b8663ffffffff16600703610abe5760008460030b139050610b02565b8663ffffffff16600103610b0257601f601087901c166000819003610ae75760008560030b1291505b8063ffffffff16600103610b005760008560030b121591505b505b606082018051608084015163ffffffff169091528115610b48576002610b2d8861ffff1660106108eb565b63ffffffff90811690911b8201600401166080840152610b5a565b60808301805160040163ffffffff1690525b610b62610619565b98975050505050505050565b6000603f601a86901c16801580610b9d575060088163ffffffff1610158015610b9d5750600f8163ffffffff16105b15610ff357603f86168160088114610be45760098114610bed57600a8114610bf657600b8114610bff57600c8114610c0857600d8114610c1157600e8114610c1a57610c1f565b60209150610c1f565b60219150610c1f565b602a9150610c1f565b602b9150610c1f565b60249150610c1f565b60259150610c1f565b602691505b508063ffffffff16600003610c465750505063ffffffff8216601f600686901c161b610611565b8063ffffffff16600203610c6c5750505063ffffffff8216601f600686901c161c610611565b8063ffffffff16600303610ca257601f600688901c16610c9863ffffffff8716821c60208390036108eb565b9350505050610611565b8063ffffffff16600403610cc45750505063ffffffff8216601f84161b610611565b8063ffffffff16600603610ce65750505063ffffffff8216601f84161c610611565b8063ffffffff16600703610d1957610d108663ffffffff168663ffffffff16901c876020036108eb565b92505050610611565b8063ffffffff16600803610d31578592505050610611565b8063ffffffff16600903610d49578592505050610611565b8063ffffffff16600a03610d61578592505050610611565b8063ffffffff16600b03610d79578592505050610611565b8063ffffffff16600c03610d91578592505050610611565b8063ffffffff16600f03610da9578592505050610611565b8063ffffffff16601003610dc1578592505050610611565b8063ffffffff16601103610dd9578592505050610611565b8063ffffffff16601203610df1578592505050610611565b8063ffffffff16601303610e09578592505050610611565b8063ffffffff16601803610e21578592505050610611565b8063ffffffff16601903610e39578592505050610611565b8063ffffffff16601a03610e51578592505050610611565b8063ffffffff16601b03610e69578592505050610611565b8063ffffffff16602003610e8257505050828201610611565b8063ffffffff16602103610e9b57505050828201610611565b8063ffffffff16602203610eb457505050818303610611565b8063ffffffff16602303610ecd57505050818303610611565b8063ffffffff16602403610ee657505050828216610611565b8063ffffffff16602503610eff57505050828217610611565b8063ffffffff16602603610f1857505050828218610611565b8063ffffffff16602703610f325750505082821719610611565b8063ffffffff16602a03610f63578460030b8660030b12610f54576000610f57565b60015b60ff1692505050610611565b8063ffffffff16602b03610f8b578463ffffffff168663ffffffff1610610f54576000610f57565b6040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601360248201527f696e76616c696420696e737472756374696f6e00000000000000000000000000604482015260640161087b565b50610f8b565b8063ffffffff16601c0361107757603f8616600281900361101957505050828202610611565b8063ffffffff166020148061103457508063ffffffff166021145b15610fed578063ffffffff1660200361104b579419945b60005b638000000087161561106d576401fffffffe600197881b16960161104e565b9250610611915050565b8063ffffffff16600f0361109957505065ffffffff0000601083901b16610611565b8063ffffffff166020036110cd576101d38560031660080260180363ffffffff168463ffffffff16901c60ff1660086108eb565b8063ffffffff16602103611102576101d38560021660080260100363ffffffff168463ffffffff16901c61ffff1660106108eb565b8063ffffffff1660220361113157505063ffffffff60086003851602811681811b198416918316901b17610611565b8063ffffffff166023036111485782915050610611565b8063ffffffff1660240361117a578460031660080260180363ffffffff168363ffffffff16901c60ff16915050610611565b8063ffffffff166025036111ad578460021660080260100363ffffffff168363ffffffff16901c61ffff16915050610611565b8063ffffffff166026036111df57505063ffffffff60086003851602601803811681811c198416918316901c17610611565b8063ffffffff1660280361121557505060ff63ffffffff60086003861602601803811682811b9091188316918416901b17610611565b8063ffffffff1660290361124c57505061ffff63ffffffff60086002861602601003811682811b9091188316918416901b17610611565b8063ffffffff16602a0361127b57505063ffffffff60086003851602811681811c198316918416901c17610611565b8063ffffffff16602b036112925783915050610611565b8063ffffffff16602e036112c457505063ffffffff60086003851602601803811681811b198316918416901b17610611565b8063ffffffff166030036112db5782915050610611565b8063ffffffff16603803610f8b5783915050610611565b60006112fc611c53565b506080602063ffffffff86161061136f576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152600e60248201527f76616c6964207265676973746572000000000000000000000000000000000000604482015260640161087b565b63ffffffff8516158015906113815750825b156113b557838161016001518663ffffffff16602081106113a4576113a4611d92565b63ffffffff90921660209290920201525b60808101805163ffffffff808216606085015260049091011690526108e2610619565b60006113e2611c53565b506101e051604081015160808083015160a084015160c09094015191936000928392919063ffffffff8616610ffa0361145c5781610fff81161561142b57610fff811661100003015b8363ffffffff166000036114525760e08801805163ffffffff838201169091529550611456565b8395505b506118cb565b8563ffffffff16610fcd0361147757634000000094506118cb565b8563ffffffff166110180361148f57600194506118cb565b8563ffffffff16611096036114c457600161012088015260ff83166101008801526114b8610619565b97505050505050505090565b8563ffffffff16610fa30361172e5763ffffffff8316156118cb577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb63ffffffff8416016116e857600061151f8363fffffffc166001610735565b60208901519091508060001a60010361158c5761158981600090815233602052604090207effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff167f01000000000000000000000000000000000000000000000000000000000000001790565b90505b6040808a015190517fe03110e10000000000000000000000000000000000000000000000000000000081526004810183905263ffffffff9091166024820152600090819073ffffffffffffffffffffffffffffffffffffffff7f0000000000000000000000000000000000000000000000000000000000000000169063e03110e1906044016040805180830381865afa15801561162d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906116519190611dc1565b91509150600386168060040382811015611669578092505b5081861015611676578591505b8260088302610100031c9250826008828460040303021b9250600180600883600403021b036001806008858560040303021b039150811981169050838119871617955050506116cd8663fffffffc16600186611b06565b60408b018051820163ffffffff169052975061172992505050565b7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd63ffffffff84160161171d578094506118cb565b63ffffffff9450600993505b6118cb565b8563ffffffff16610fa40361181f5763ffffffff831660011480611758575063ffffffff83166002145b80611769575063ffffffff83166004145b15611776578094506118cb565b7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa63ffffffff84160161171d5760006117b68363fffffffc166001610735565b602089015190915060038416600403838110156117d1578093505b83900360089081029290921c7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff600193850293841b0116911b176020880152600060408801529350836118cb565b8563ffffffff16610fd7036118cb578163ffffffff166003036118bf5763ffffffff83161580611855575063ffffffff83166005145b80611866575063ffffffff83166003145b1561187457600094506118cb565b63ffffffff83166001148061188f575063ffffffff83166002145b806118a0575063ffffffff83166006145b806118b1575063ffffffff83166004145b1561171d57600194506118cb565b63ffffffff9450601693505b6101608701805163ffffffff808816604090920191909152905185821660e09091015260808801805180831660608b015260040190911690526114b8610619565b6000611916611c53565b506080600063ffffffff8716601003611934575060c0810151611a9d565b8663ffffffff166011036119535763ffffffff861660c0830152611a9d565b8663ffffffff1660120361196c575060a0810151611a9d565b8663ffffffff1660130361198b5763ffffffff861660a0830152611a9d565b8663ffffffff166018036119bf5763ffffffff600387810b9087900b02602081901c821660c08501521660a0830152611a9d565b8663ffffffff166019036119f05763ffffffff86811681871602602081901c821660c08501521660a0830152611a9d565b8663ffffffff16601a03611a46578460030b8660030b81611a1357611a13611de5565b0763ffffffff1660c0830152600385810b9087900b81611a3557611a35611de5565b0563ffffffff1660a0830152611a9d565b8663ffffffff16601b03611a9d578463ffffffff168663ffffffff1681611a6f57611a6f611de5565b0663ffffffff90811660c084015285811690871681611a9057611a90611de5565b0463ffffffff1660a08301525b63ffffffff841615611ad857808261016001518563ffffffff1660208110611ac757611ac7611d92565b63ffffffff90921660209290920201525b60808201805163ffffffff80821660608601526004909101169052611afb610619565b979650505050505050565b6000611b1183611baa565b90506003841615611b2157600080fd5b6020810190601f8516601c0360031b83811b913563ffffffff90911b1916178460051c60005b601b811015611b9f5760208401933582821c6001168015611b6f5760018114611b8457611b95565b60008581526020839052604090209450611b95565b600082815260208690526040902094505b5050600101611b47565b505060805250505050565b60ff811661038002610184810190369061050401811015611c4d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602360248201527f636865636b207468617420746865726520697320656e6f7567682063616c6c6460448201527f6174610000000000000000000000000000000000000000000000000000000000606482015260840161087b565b50919050565b6040805161018081018252600080825260208201819052918101829052606081018290526080810182905260a0810182905260c0810182905260e08101829052610100810182905261012081018290526101408101919091526101608101611cb9611cbe565b905290565b6040518061040001604052806020906020820280368337509192915050565b60008083601f840112611cef57600080fd5b50813567ffffffffffffffff811115611d0757600080fd5b602083019150836020828501011115611d1f57600080fd5b9250929050565b60008060008060408587031215611d3c57600080fd5b843567ffffffffffffffff80821115611d5457600080fd5b611d6088838901611cdd565b90965094506020870135915080821115611d7957600080fd5b50611d8687828801611cdd565b95989497509550505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b60008060408385031215611dd457600080fd5b505080516020909101519092909150565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fdfea164736f6c634300080f000a \ No newline at end of file diff --git a/crates/mipsevm/bindings/optimism b/crates/mipsevm/bindings/optimism new file mode 160000 index 0000000..a7ff5a8 --- /dev/null +++ b/crates/mipsevm/bindings/optimism @@ -0,0 +1 @@ +Subproject commit a7ff5a811612fa338d0a6d6dd72dc2ec9badef6d diff --git a/crates/mipsevm/bindings/preimage_oracle_deployed.bin b/crates/mipsevm/bindings/preimage_oracle_deployed.bin new file mode 100644 index 0000000..3a73a73 --- /dev/null +++ b/crates/mipsevm/bindings/preimage_oracle_deployed.bin @@ -0,0 +1 @@ +608060405234801561001057600080fd5b506004361061007d5760003560e01c8063e03110e11161005b578063e03110e114610111578063e159261114610139578063fe4ac08e1461014e578063fef2b4ed146101c357600080fd5b806361238bde146100825780638542cf50146100c05780639a1f5e7f146100fe575b600080fd5b6100ad610090366004610551565b600160209081526000928352604080842090915290825290205481565b6040519081526020015b60405180910390f35b6100ee6100ce366004610551565b600260209081526000928352604080842090915290825290205460ff1681565b60405190151581526020016100b7565b6100ad61010c366004610573565b6101e3565b61012461011f366004610551565b6102b6565b604080519283526020830191909152016100b7565b61014c6101473660046105a5565b6103a7565b005b61014c61015c366004610573565b6000838152600260209081526040808320878452825280832080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001660019081179091558684528252808320968352958152858220939093559283529082905291902055565b6100ad6101d1366004610621565b60006020819052908152604090205481565b60006101ee856104b0565b90506101fb836008610669565b8211806102085750602083115b1561023f576040517ffe25498700000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b6000602081815260c085901b82526008959095528251828252600286526040808320858452875280832080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0016600190811790915584845287528083209483529386528382205581815293849052922055919050565b6000828152600260209081526040808320848452909152812054819060ff1661033f576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152601460248201527f7072652d696d616765206d757374206578697374000000000000000000000000604482015260640160405180910390fd5b506000838152602081815260409091205461035b816008610669565b610366856020610669565b106103845783610377826008610669565b6103819190610681565b91505b506000938452600160209081526040808620948652939052919092205492909150565b604435600080600883018611156103c65763fe2549876000526004601cfd5b60c083901b6080526088838682378087017ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80151908490207effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff167f02000000000000000000000000000000000000000000000000000000000000001760008181526002602090815260408083208b8452825280832080547fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0016600190811790915584845282528083209a83529981528982209390935590815290819052959095209190915550505050565b7f01000000000000000000000000000000000000000000000000000000000000007effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82161761054b81600090815233602052604090207effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff167f01000000000000000000000000000000000000000000000000000000000000001790565b92915050565b6000806040838503121561056457600080fd5b50508035926020909101359150565b6000806000806080858703121561058957600080fd5b5050823594602084013594506040840135936060013592509050565b6000806000604084860312156105ba57600080fd5b83359250602084013567ffffffffffffffff808211156105d957600080fd5b818601915086601f8301126105ed57600080fd5b8135818111156105fc57600080fd5b87602082850101111561060e57600080fd5b6020830194508093505050509250925092565b60006020828403121561063357600080fd5b5035919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000821982111561067c5761067c61063a565b500190565b6000828210156106935761069361063a565b50039056fea164736f6c634300080f000a \ No newline at end of file diff --git a/crates/mipsevm/src/evm.rs b/crates/mipsevm/src/evm.rs new file mode 100644 index 0000000..353615d --- /dev/null +++ b/crates/mipsevm/src/evm.rs @@ -0,0 +1,118 @@ +//! This module contains a wrapper around a [revm] inspector with an in-memory backend +//! that has the MIPS & PreimageOracle smart contracts deployed at deterministic addresses. + +use alloy_primitives::{hex, Address, U256}; +use revm::{ + db::{CacheDB, EmptyDB}, + primitives::{ + AccountInfo, Bytecode, Bytes, CreateScheme, Output, ResultAndState, TransactTo, TxEnv, + B160, B256, + }, + Database, EVM, +}; + +pub const MIPS_ADDR: [u8; 20] = hex!("000000000000000000000000000000000000C0DE"); +pub const PREIMAGE_ORACLE_ADDR: [u8; 20] = hex!("0000000000000000000000000000000000000420"); + +pub const MIPS_CREATION_CODE: &str = include_str!("../bindings/mips_creation.bin"); +pub const PREIMAGE_ORACLE_DEPLOYED_CODE: &str = + include_str!("../bindings/preimage_oracle_deployed.bin"); + +pub struct MipsEVM { + pub evm: EVM, +} + +impl MipsEVM> { + pub fn new() -> Self { + let mut evm = EVM::default(); + evm.database(CacheDB::default()); + + let Some(db) = evm.db() else { + panic!("Database needs to be set"); + }; + + db.insert_account_info( + B160::zero(), + AccountInfo { + balance: U256::from(u128::MAX), + nonce: 0, + code_hash: B256::zero(), + code: None, + }, + ); + + #[inline(always)] + fn deploy_contract(db: &mut CacheDB, addr: B160, code: Bytes) { + let mut acc_info = AccountInfo { + balance: U256::ZERO, + nonce: 0, + code_hash: B256::zero(), + code: Some(Bytecode::new_raw(code)), + }; + db.insert_contract(&mut acc_info); + db.insert_account_info(addr, acc_info); + } + + deploy_contract( + db, + B160::from_slice(PREIMAGE_ORACLE_ADDR.as_slice()), + Bytes::from(hex::decode(PREIMAGE_ORACLE_DEPLOYED_CODE).unwrap()), + ); + + // Deploy the MIPS contract prior to setting it manually. This contract has an immutable + // variable, so we let the creation code fill this in for us. + let encoded_preimage_addr = Address::from_slice(MIPS_ADDR.as_slice()).into_word(); + let mips_creation_heap = hex::decode(MIPS_CREATION_CODE) + .unwrap() + .into_iter() + .chain(encoded_preimage_addr) + .collect::>(); + evm.env.tx = TxEnv { + caller: 0.into(), + gas_limit: 0, + gas_price: U256::ZERO, + gas_priority_fee: None, + transact_to: TransactTo::Create(CreateScheme::Create), + value: U256::ZERO, + data: mips_creation_heap.into(), + chain_id: None, + nonce: None, + access_list: Vec::default(), + }; + if let Ok(ResultAndState { + result: + revm::primitives::ExecutionResult::Success { + reason: _, + gas_used: _, + gas_refunded: _, + logs: _, + output: Output::Create(code, _), + }, + state: _, + }) = evm.transact_ref() + { + // Deploy the MIPS contract manually. + deploy_contract( + evm.db().unwrap(), + B160::from_slice(MIPS_ADDR.as_slice()), + code, + ) + } else { + panic!("Failed to deploy MIPS contract"); + } + + Self { evm } + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn evm() { + let mut _mevm = MipsEVM::new(); + let acc = _mevm.evm.db().unwrap().load_account(MIPS_ADDR.into()); + dbg!(acc.unwrap()); + } +} diff --git a/crates/mipsevm/src/lib.rs b/crates/mipsevm/src/lib.rs index e0d4de9..afb30d9 100644 --- a/crates/mipsevm/src/lib.rs +++ b/crates/mipsevm/src/lib.rs @@ -24,3 +24,8 @@ pub use types::{Address, Fd, Gindex, Page, PageIndex, StateWitness, VMStatus}; mod mips; pub use mips::InstrumentedState; + +mod evm; + +#[cfg(test)] +mod test_utils; diff --git a/crates/mipsevm/src/mips/instrumented.rs b/crates/mipsevm/src/mips/instrumented.rs index 0fd01e0..e84191f 100644 --- a/crates/mipsevm/src/mips/instrumented.rs +++ b/crates/mipsevm/src/mips/instrumented.rs @@ -101,9 +101,7 @@ where #[cfg(test)] mod test { - use crate::PreimageOracle; - use alloy_primitives::B256; - use preimage_oracle::{Keccak256Key, Key}; + use crate::test_utils::StaticOracle; /// Used in tests to write the results to const BASE_ADDR_END: u32 = 0xBF_FF_FF_F0; @@ -111,23 +109,6 @@ mod test { /// Used as the return-address for tests const END_ADDR: u32 = 0xA7_EF_00_D0; - struct StaticOracle { - preimage_data: Vec, - } - - impl PreimageOracle for StaticOracle { - fn hint(&mut self, _value: &[u8]) { - // noop - } - - fn get(&self, key: B256) -> anyhow::Result<&[u8]> { - if key != (key as Keccak256Key).preimage_key() { - anyhow::bail!("Invalid preimage ") - } - Ok(self.preimage_data.as_slice()) - } - } - mod open_mips { use super::*; use crate::{Address, InstrumentedState, Memory, State}; @@ -175,9 +156,7 @@ mod test { let mut ins = InstrumentedState::new( state, - StaticOracle { - preimage_data: b"hello world".to_vec(), - }, + StaticOracle::new(b"hello world".to_vec()), io::stdout(), io::stderr(), ); diff --git a/crates/mipsevm/src/test_utils.rs b/crates/mipsevm/src/test_utils.rs new file mode 100644 index 0000000..a07ed5f --- /dev/null +++ b/crates/mipsevm/src/test_utils.rs @@ -0,0 +1,29 @@ +//! Testing utilities. + +use alloy_primitives::B256; +use preimage_oracle::{Keccak256Key, Key}; + +use crate::PreimageOracle; + +pub struct StaticOracle { + preimage_data: Vec, +} + +impl StaticOracle { + pub fn new(preimage_data: Vec) -> Self { + Self { preimage_data } + } +} + +impl PreimageOracle for StaticOracle { + fn hint(&mut self, _value: &[u8]) { + // noop + } + + fn get(&self, key: B256) -> anyhow::Result<&[u8]> { + if key != (key as Keccak256Key).preimage_key() { + anyhow::bail!("Invalid preimage ") + } + Ok(self.preimage_data.as_slice()) + } +}