In this repository, we provide a model of the EVM in K.
bash <(curl https://kframework.org/install): install kup package manager.kup install kevm: install KEVM.kup list kevm: list available KEVM versions.kup update kevm: update to latest KEVM version.
NOTE: The first run will take longer to fetch all the libraries and compile sources. (30m to 1h)
These may be useful for learning KEVM and K (newest to oldest):
- K, KEVM and Foundry Integration overview
- Jello Paper, a nice presentation of this repository.
- 20 minute tour of the semantics at 2017 Devcon3.
- KEVM 1.0 technical report, especially sections 3 and 5.
- KEVM Paper at CSF'18/FLoC.
To get support for KEVM, please join our Discord Channel.
If you want to start proving with KEVM, refer to VERIFICATION.md.
The following files constitute the KEVM semantics:
- network.md provides the status codes reported to an Ethereum client on execution exceptions.
- json-rpc.md is an implementation of JSON RPC in K.
- evm-types.md provides the (functional) data of EVM (256-bit words, wordstacks, etc...).
- serialization.md provides helpers for parsing and unparsing data (hex strings, recursive-length prefix, Merkle trees, etc.).
- evm.md is the main KEVM semantics, containing EVM’s configuration and transition rules.
- gas.md contains all information relevant to gas.
- schedule.md contains all information relevant to EVM schedules.
These additional files extend the semantics to make the repository more useful:
- buf.md defines the
#bufbyte-buffer abstraction for use during symbolic execution. - abi.md defines the Contract ABI Specification for use in proofs and easy contract/function specification.
- hashed-locations.md defines the
#hashedLocationabstraction used to specify Solidity-generated storage layouts. - edsl.md combines the previous three abstractions for ease-of-use.
These files are used for testing the semantics itself:
- state-utils.md provides functionality for EVM initialization, setup, and querying.
- driver.md is an execution harness for KEVM, providing a simple language for describing tests/programs.
There are two backends of K available: LLVM for concrete execution and Haskell for symbolic execution.
This repository generates the build-products for each backend in $XDG_CACHE_HOME/evm-semantics-<digest>.
First install the following tools:
KEVM requires Z3 version 4.12.1, which you may need to install from a source build if your package manager supplies a different version. To do so, follow the instructions here after checking out the correct tag in the Z3 repository:
git clone https://github.com/Z3Prover/z3.git
cd z3
git checkout z3-4.12.1
python scripts/mk_make.py
cd build
make
sudo make installOn macOS, it is easiest to install Z3 from Homebrew.
If you wish to install from the source, install it to an appropriate prefix (e.g. /usr/local on Intel machines).
On Ubuntu >= 22.04 (for example):
sudo apt-get install --yes \
autoconf bison clang-12 cmake curl flex gcc jq libboost-test-dev \
libcrypto++-dev libffi-dev libgflags-dev libjemalloc-dev libmpfr-dev \
libprocps-dev libsecp256k1-dev libssl-dev libtool libyaml-dev lld-12 \
llvm-12-tools make maven openjdk-11-jdk pkg-config python3 python3-dev \
python3-pip rapidjson-dev time zlib1g-dev libfmt-devOn Ubuntu < 18.04, you'll need to skip libsecp256k1-dev and instead build it from source (via our Makefile):
make libsecp256k1On ArchLinux:
sudo pacman -S \
base base-devel boost clang cmake crypto++ curl git gmp \
gflags jdk-openjdk jemalloc libsecp256k1 lld llvm maven \
mpfr poetry python stack yaml-cpp zlibAfter installing the Command Line Tools, Homebrew, and getting the blockchain plugin, run:
brew tap kframework/k
brew install java automake libtool gmp mpfr pkg-config maven libffi llvm@14 openssl python bash kframework/k/cryptopp@8.6.0 poetry solidity
make libsecp256k1NOTE: It is recommended to use the homebrew version of flex and XCode.
If you are building on an Apple Silicon machine, ensure that your PATH is set up correctly before running make deps or make k-deps.
You can do so using direnv by copying macos-envrc to .envrc, then running direnv allow.
If the build on macOS still fails, you can also try adding the following lines to the top of your Makefile under UNAME_S:
ifeq ($(UNAME_S), Darwin)
SHELL := /usr/local/bin/bash
PATH := $(pwd)/.build/usr/bin:$(PATH)
endif- Haskell Stack.
Note that the version of the
stacktool provided by your package manager might not be recent enough. Please follow installation instructions from the Haskell Stack website linked above.
To upgrade stack (if needed):
stack upgrade
export PATH=$HOME/.local/bin:$PATHYou need to install the K Framework on your system, see the instructions there. The fastest way is via the kup package manager, with which you can do to get the correct version of K:
kup install k.openssl.procps --version v$(cat deps/k_release)You can also drop into a single development shell with the correct version of K on path by doing:
kup shell k.openssl.procps --version v$(cat deps/k_release)First you need to set up a virtual environment using Poetry with the prerequisites python 3.8.*, pip >= 20.0.2, poetry >= 1.3.2:
make poetryYou also need to get the blockchain plugin submodule and install it.
git submodule update --init --recursive
poetry -C kevm-pyk run kevm-dist --verbose build pluginTo change the default compiler:
CXX=clang++-14 poetry -C kevm-pyk run kevm-dist --verbose build pluginOn Apple silicon:
APPLE_SILICON=true poetry -C kevm-pyk run kevm-dist --verbose build pluginFinally, you can build the semantics.
poetry -C kevm-pyk run kevm-dist --verbose build -j4You can build specific targets using options llvm, haskell, haskell-standalone or foundry, e.g.:
poetry -C kevm-pyk run kevm-dist build -j2 llvm haskellTargets can be cleaned with
poetry -C kevm-pyk run kevm-dist cleanFor more information, refer to kevm-dist --help and the dist.py module.
To execute tests from the Ethereum Test Set, the submodule needs to be fetched first.
git submodule update --init --recursive -- tests/ethereum-testsThe tests are run using the supplied Makefile.
The following subsume all other tests:
make test: All of the quick tests.make test-all: All of the quick and slow tests.
These are the individual test-suites (all of these can be suffixed with -all to also run slow tests):
make test-vm: VMTests from the Ethereum Test Set.make test-bchain: Subset of BlockchainTests from the Ethereum Test Set.make test-proof: Proofs from the Verified Smart Contracts.make test-interactive: Tests of thekevmcommand.
If built from the source, the kevm-pyk executable will be installed in a virtual environemtn handled by Poetry.
You can call kevm-pyk --help to get a quick summary of how to use the script.
Run the file tests/ethereum-tests/LegacyTests/Constantinople/VMTests/vmArithmeticTest/add0.json:
poetry -C kevm-pyk run kevm-pyk run tests/ethereum-tests/LegacyTests/Constantinople/VMTests/vmArithmeticTest/add0.json --schedule DEFAULT --mode VMTESTSTo enable the debug symbols for the llvm backend, build using this command:
poetry -C kevm-pyk run kevm-dist build llvm --enable-llvm-debugTo debug a conformance test, add the --debugger flag to the command:
poetry -C kevm-pyk run kevm-pyk run tests/ethereum-tests/BlockchainTests/GeneralStateTests/stExample/add11.json --target llvm --mode NORMAL --schedule SHANGHAI --chainid 1 --debuggerAlways have your build up-to-date.
- If using the kup package manager, run
kup install kevm --version .to install the local version. - If building from source:
make poetryneeds to be re-run if you touch any of thekevm-pykcode.poetry -C kevm-pyk run kevm-dist build <target> --forceneeds to be re-run if you touch any of this repos files.poetry -C kevm-pyk run kevm-dist cleanis a safe way to remove the target directory
We now support building KEVM using nix flakes.
To set up nix flakes you will need to be on nix 2.4 or higher and follow the instructions here.
For example, if you are on a standard Linux distribution, such as Ubuntu, first install nix
and then enable flakes by editing either ~/.config/nix/nix.conf or /etc/nix/nix.conf and adding:
experimental-features = nix-command flakes
This is needed to expose the Nix 2.0 CLI and flakes support that are hidden behind feature-flags.
By default, Nix will build the project and its transitive dependencies from source, which can take up to an hour. We recommend setting up the binary cache to speed up the build process significantly.
To build KEVM, run:
nix build .#kevmThis will build all of KEVM and K and put a link to the resulting binaries in the result/ folder.
NOTE: Mac users, especially those running M1/M2 Macs may find nix segfaulting on occasion. If this happens, try running the nix command like this: GC_DONT_GC=1 nix build .
If you want to temporarily add the kevm binary to the current shell, run
nix shell .#kevmNix can also be used to quickly profile different versions of the Haskell backend. Simply run:
nix build github:runtimeverification/evm-semantics#profile \
--override-input k-framework/haskell-backend github:runtimeverification/haskell-backend/<HASH> \
-o prof-<HASH>replacing <HASH> with the commit you want to run profiling against.
If you want to profile against a working version of the Haskell backend repository, simply cd into the root of the repo and run:
nix build github:runtimeverification/evm-semantics#profile \
--override-input k-framework/haskell-backend $(pwd) \
-o prof-my-featureTo compare profiles, you can use:
nix run github:runtimeverification/evm-semantics#compare-profiles -- prof-my-feature prof-<HASH>This will produce a nice table with the times for both versions of the haskell-backend.
Note that #profile pre-pends the output of kore-exec --version to the profile run, which is then used as a tag by the #compare-profiles script.
Therefore, any profiled local checkout of the haskell-backend will report as dirty-ghc8107 in the resulting table.
This repository can build two pieces of documentation for you, the Jello Paper and the 2017 Devcon3 presentation.
For the presentations in the media directory, you'll need pdflatex, commonly provided with texlive-full, and pandoc.
sudo apt install texlive-full pandocTo build all the PDFs (presentations and reports) available in the media/ directory, use:
make media- EVM Yellowpaper: Original specification of EVM.
- LEM Semantics of EVM
- EVM Opcode Interactive Reference
- Solidity ABI Encoding
For more information about the K Framework, refer to these sources:
- The K Tutorial
- Semantics-Based Program Verifiers for All Languages
- Reachability Logic Resources
- Matching Logic Resources
- Logical Frameworks: Discussion of logical frameworks.