-
Notifications
You must be signed in to change notification settings - Fork 207
fv crate #683
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
Closed
Closed
fv crate #683
Changes from all commits
Commits
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 hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
[package] | ||
authors = ["The Fe Developers <snakecharmers@ethereum.org>"] | ||
categories = ["cryptography::cryptocurrencies", "command-line-utilities", "development-tools"] | ||
description = "Fe formal verification utilities" | ||
edition = "2021" | ||
keywords = ["ethereum", "fe", "yul", "smart", "contract", "compiler"] | ||
license = "GPL-3.0-or-later" | ||
name = "fv" | ||
readme = "README.md" | ||
repository = "https://github.com/ethereum/fe" | ||
version = "0.15.0-alpha" | ||
|
||
[features] | ||
solc-backend = ["fe-driver/solc-backend"] | ||
kevm-backend = [] | ||
|
||
[dependencies] | ||
fe-driver = {path = "../driver", version = "^0.15.0-alpha"} | ||
fe-yulgen = {path = "../yulgen", version = "^0.15.0-alpha"} | ||
fe-test-files = {path = "../test-files", version = "^0.15.0-alpha"} |
This file contains hidden or 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,41 @@ | ||
# Fe Fv | ||
|
||
This crate contains utilities for building and running kevm specs. | ||
|
||
## kevm setup | ||
|
||
A system installation of [evm-semantics](https://github.com/fe-lang/evm-semantics) is required to run the specs. Please clone the repository and follow the installation directions. | ||
|
||
While in the `evm-semantics` project, `kompile` the Fe verification module: | ||
|
||
```commandline | ||
$ export PATH=$PATH:/path/to/evm-semantics/.build/usr/bin | ||
$ kevm kompile --backend haskell tests/specs/fe/verification.k \ | ||
--directory tests/specs/fe/verification/haskell \ | ||
--main-module VERIFICATION \ | ||
--syntax-module VERIFICATION \ | ||
--concrete-rules-file tests/specs/fe/concrete-rules.txt \ | ||
-I /usr/lib/kevm/include/kframework -I /usr/lib/kevm/blockchain-k-plugin/include/kframework | ||
``` | ||
|
||
## running the proofs | ||
|
||
Once the evm-semantics project has been built, set the `KEVM_PATH` environment variable: | ||
|
||
```commandline | ||
$ export KEVM_PATH=/path/to/evm-semantics | ||
``` | ||
|
||
and then run the tests: | ||
|
||
```commandline | ||
$ cargo test --features "solc-backend, kevm-backend" | ||
``` | ||
|
||
*Note: If you are working on a resource constrained device, you may not be able to run all tests simultaneously. If issues are encountered, run tests in smaller groups.* | ||
|
||
e.g. | ||
|
||
```commandline | ||
$ cargo test sanity_returns_42 --features "solc-backend, kevm-backend" | ||
``` |
This file contains hidden or 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,63 @@ | ||
#![cfg(feature = "kevm-backend")] | ||
use fe_yulgen::Db; | ||
use std::path::Path; | ||
use std::process::Command; | ||
use std::{env, fs}; | ||
|
||
const SPECS_DIR: &str = "tests/specs/fe/"; | ||
|
||
pub fn kevm_path() -> String { | ||
env::var("KEVM_PATH").expect("`KEVM_PATH` not set") | ||
} | ||
|
||
pub fn run_spec(name: &str, src_path: &str, src: &str, spec: &str) -> Result<(), String> { | ||
let kevm_path = kevm_path(); | ||
|
||
let spec = build_spec(name, src_path, src, spec); | ||
let spec_path = Path::new(&kevm_path) | ||
.join(SPECS_DIR) | ||
.join(name) | ||
.with_extension("k"); | ||
fs::write(spec_path.clone(), spec).expect("unable to write file"); | ||
|
||
let path = env::var("PATH").expect("PATH is not set"); | ||
|
||
let out = Command::new("kevm") | ||
.arg("prove") | ||
.arg(spec_path.to_str().unwrap()) | ||
.arg("--backend") | ||
.arg("haskell") | ||
.arg("--format-failures") | ||
.arg("--directory") | ||
.arg("tests/specs/fe/verification/haskell") | ||
.env("PATH", format!(".build/usr/bin:{}", path)) | ||
.current_dir(&kevm_path) | ||
.output() | ||
.expect("failed to execute process"); | ||
|
||
if out.status.code() != Some(0) { | ||
Err(format!( | ||
"{}\n{}", | ||
String::from_utf8_lossy(&out.stderr), | ||
String::from_utf8_lossy(&out.stdout) | ||
)) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn build_spec(name: &str, src_path: &str, src: &str, spec: &str) -> String { | ||
let mut db = Db::default(); | ||
let module = fe_driver::compile_single_file(&mut db, src_path, src, true, true).unwrap(); | ||
|
||
// replace placeholders | ||
let mut new_spec = spec.to_owned().replace("$TEST_NAME", &name.to_uppercase()); | ||
for (name, contract) in module.contracts.iter() { | ||
new_spec = new_spec.replace( | ||
&format!("${}::RUNTIME", name), | ||
&format!("\"0x{}\"", contract.runtime_bytecode), | ||
) | ||
} | ||
|
||
new_spec | ||
} |
This file contains hidden or 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,49 @@ | ||
#![cfg(feature = "kevm-backend")] | ||
|
||
/// Checks if a contract spec is valid. | ||
macro_rules! test_spec { | ||
($name:ident, $src_path:expr, $spec_path:expr) => { | ||
#[test] | ||
fn $name() { | ||
let src = fe_test_files::fixture(concat!("kspecs/", $src_path)); | ||
let spec = fe_test_files::fixture(concat!("kspecs/", $spec_path)); | ||
|
||
if let Err(output) = | ||
fv::run_spec(&stringify!($name).replace("_", "-"), $src_path, &src, &spec) | ||
{ | ||
panic!("{}", output) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/// Checks if a contract spec is invalid. | ||
macro_rules! test_spec_invalid { | ||
($name:ident, $src_path:expr, $spec_path:expr) => { | ||
#[test] | ||
fn $name() { | ||
let src = fe_test_files::fixture(concat!("kspecs/", $src_path)); | ||
let spec = fe_test_files::fixture(concat!("kspecs/", $spec_path)); | ||
|
||
match fv::run_spec(&stringify!($name).replace("_", "-"), $src_path, &src, &spec) { | ||
Ok(()) => panic!("spec is valid"), | ||
Err(output) => { | ||
// we want to make sure it didn't fail for some other reason | ||
if !output.contains("the claimed implication is not valid") { | ||
panic!("spec claim was not checked {}", output) | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
test_spec! { sanity_returns_42, "sanity/foo.fe", "sanity/returns_42.k" } | ||
test_spec! { sanity_returns_in, "sanity/foo.fe", "sanity/returns_in.k" } | ||
test_spec! { sanity_returns_in_cond1, "sanity/foo.fe", "sanity/returns_in_cond1.k" } | ||
test_spec! { sanity_returns_in_cond2, "sanity/foo.fe", "sanity/returns_in_cond2.k" } | ||
|
||
// these are just for extra sanity | ||
test_spec_invalid! { sanity_returns_42_invalid, "sanity/foo.fe", "sanity/returns_42_invalid.k" } | ||
test_spec_invalid! { sanity_returns_in_invalid, "sanity/foo.fe", "sanity/returns_in_invalid.k" } | ||
test_spec_invalid! { sanity_returns_in_cond2_invalid, "sanity/foo.fe", "sanity/returns_in_cond2_invalid.k" } |
This file contains hidden or 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,31 @@ | ||
use std::evm | ||
|
||
# always returns 42 | ||
contract Returns42: | ||
pub fn __call__(): | ||
unsafe: | ||
evm::mstore(offset: 0, value: 42) | ||
evm::return_mem(offset: 0, len: 32) | ||
|
||
|
||
# always returns `input` | ||
contract ReturnsIn: | ||
pub fn __call__(): | ||
unsafe: | ||
let input: u256 = evm::call_data_load(offset: 0) | ||
evm::mstore(offset: 0, value: input) | ||
evm::return_mem(offset: 0, len: 32) | ||
|
||
|
||
# returns `input`, except when `input == 42`, in which case it will return `26` | ||
contract ReturnsInCond: | ||
pub fn __call__(): | ||
unsafe: | ||
let input: u256 = evm::call_data_load(offset: 0) | ||
let output: u256 = input | ||
|
||
if input == 42: | ||
output = 26 | ||
|
||
evm::mstore(offset: 0, value: output) | ||
evm::return_mem(offset: 0, len: 32) |
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.
Uh oh!
There was an error while loading. Please reload this page.