Skip to content

Commit

Permalink
Generate EVM verifier contracts (#3)
Browse files Browse the repository at this point in the history
* add prover with basic cli

* add verifier contract generation

* refactor committee update circuit

* unit foundry subproject

* add alt  (slim) sha256 implementation

* sync step circuit with slim sha256

* evm proof generation cmd

* fix circuit default impls

* fix undeterministic hashing

* testing out different configs

* remove local deps

* clean up

* patch snark-verifier to bump revm version

* add Spectre contract
  • Loading branch information
nulltea authored Aug 29, 2023
1 parent f321203 commit 588c5ee
Show file tree
Hide file tree
Showing 51 changed files with 14,088 additions and 1,056 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ jobs:
components: rustfmt
- name: Check
run: cargo check --all
- name: Check
run: RUST_LOG="preprocessor,eth-types,gadgets,zkcasper-circuits=debug" cargo test --release test_super_circuit
# - name: Check
# run: RUST_LOG="preprocessor,eth-types,gadgets,zkcasper-circuits=debug" cargo test --release test_super_circuit
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ node_modules
ligthclient-circuits/params/

ligthclient-circuits/build/

build/

params/

.env
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["ligthclient-circuits", "eth-types"]
members = ["ligthclient-circuits", "prover", "eth-types"]

# Definition of benchmarks profile to use.
[profile.bench]
Expand Down Expand Up @@ -30,7 +30,7 @@ halo2-base = { git = "https://github.com/timoftime/halo2-lib", rev = "7cebe716bb
"halo2-pse",
"display",
] }
halo2-ecc = { git = "https://github.com/timoftime/halo2-lib",rev = "7cebe716bb0569a7cb741c44ef8e87b2eadbbafb", default-features = false }
halo2-ecc = { git = "https://github.com/timoftime/halo2-lib", rev = "7cebe716bb0569a7cb741c44ef8e87b2eadbbafb", default-features = false }
poseidon = { git = "https://github.com/timoftime/halo2-lib", rev = "7cebe716bb0569a7cb741c44ef8e87b2eadbbafb", default-features = false }

# halo2-base = { path = "../halo2-lib/halo2-base", default-features = false, features = [
Expand All @@ -40,6 +40,10 @@ poseidon = { git = "https://github.com/timoftime/halo2-lib", rev = "7cebe716bb05
# halo2-ecc = { path = "../halo2-lib/halo2-ecc", default-features = false }
# poseidon = { path = "../halo2-lib/hashes/poseidon", default-features = false }

[patch."https://github.com/axiom-crypto/snark-verifier.git"]
snark-verifier = { git = "https://github.com/timoftime/snark-verifier", branch = "timoftime/bump-revm", default-features = false }
snark-verifier-sdk = { git = "https://github.com/timoftime/snark-verifier", branch = "timoftime/bump-revm", default-features = false }


# [patch."https://github.com/timoftime/halo2curves"]
# halo2curves = { path = "../halo2curves" }
4 changes: 4 additions & 0 deletions contracts/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
ANVIL_PRIVATE_KEY=ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80
MAINNET_RPC_URL=<your mainnet rpc url, e.g. with alchemy or infura>
GOERLI_RPC_URL=<your goerli rpc url>
34 changes: 34 additions & 0 deletions contracts/.github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: test

on: workflow_dispatch

env:
FOUNDRY_PROFILE: ci

jobs:
check:
strategy:
fail-fast: true

name: Foundry project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Run Forge build
run: |
forge --version
forge build --sizes
id: build

- name: Run Forge tests
run: |
forge test -vvv
id: test
14 changes: 14 additions & 0 deletions contracts/.gitignore
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
3 changes: 3 additions & 0 deletions contracts/.gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
66 changes: 66 additions & 0 deletions contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
## Foundry

**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**

Foundry consists of:

- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools).
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network.
- **Chisel**: Fast, utilitarian, and verbose solidity REPL.

## Documentation

https://book.getfoundry.sh/

## Usage

### Build

```shell
$ forge build
```

### Test

```shell
$ forge test
```

### Format

```shell
$ forge fmt
```

### Gas Snapshots

```shell
$ forge snapshot
```

### Anvil

```shell
$ anvil
```

### Deploy

```shell
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
```

### Cast

```shell
$ cast <subcommand>
```

### Help

```shell
$ forge --help
$ anvil --help
$ cast --help
```
18 changes: 18 additions & 0 deletions contracts/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[profile.default]
src = 'contracts'
out = 'out'
ffi = true
libs = ['lib']
optimizer = true
optimizer_runs = 1_000_000
solc = "0.8.19"
fs_permissions = [{ access = "read", path = "./test/data/"}]

[profile.default.optimizer_details]
constantOptimizer = false
yul = false

# See more config options https://github.com/foundry-rs/foundry/tree/master/config

[rpc_endpoints]
mainnet = "${MAINNET_RPC_URL}"
37 changes: 37 additions & 0 deletions contracts/lib/YulDeployer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Source: https://github.com/CodeForcer/foundry-yul
// SPDX-License-Identifier: UNLICENSED
pragma solidity <0.9;

import "forge-std/Test.sol";

contract YulDeployer is Test {
///@notice Compiles a Yul contract and returns the address that the contract was deployeod to
///@notice If deployment fails, an error will be thrown
///@param fileName - The file name of the Yul contract. For example, the file name for "Example.yul" is "Example"
///@return deployedAddress - The address that the contract was deployed to
function deployContract(string memory fileName) public returns (address) {
string memory bashCommand = string.concat(
'cast abi-encode "f(bytes)" $(solc --yul snark-verifiers/', string.concat(fileName, ".yul --bin | tail -1)")
);

string[] memory inputs = new string[](3);
inputs[0] = "bash";
inputs[1] = "-c";
inputs[2] = bashCommand;

// abi.decode(vm.ffi(inputs), (bytes))
bytes memory bytecode = vm.ffi(inputs);

///@notice deploy the bytecode with the create instruction
address deployedAddress;
assembly {
deployedAddress := create(0, add(bytecode, 0x20), mload(bytecode))
}

///@notice check that the deployment was successful
require(deployedAddress != address(0), "YulDeployer could not deploy contract");

///@notice return the address that the contract was deployed to
return deployedAddress;
}
}
1 change: 1 addition & 0 deletions contracts/lib/forge-std
Submodule forge-std added at 74cfb7
2 changes: 2 additions & 0 deletions contracts/remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ds-test/=lib/forge-std/lib/ds-test/src/
forge-std/=lib/forge-std/src/
58 changes: 58 additions & 0 deletions contracts/script/SpectreDeployLocal.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.19;

import "forge-std/Script.sol";
import "forge-std/safeconsole.sol";
import {Spectre} from "../src/Spectre.sol";

contract SpectreDeployLocal is Script {
function deployContract(string memory fileName) public returns (address) {
string memory bashCommand = string.concat(
'cast abi-encode "f(bytes)" $(solc --yul snark-verifiers/', string.concat(fileName, ".yul --bin | tail -1)")
);

string[] memory inputs = new string[](3);
inputs[0] = "bash";
inputs[1] = "-c";
inputs[2] = bashCommand;

bytes memory bytecode = vm.ffi(inputs);

// bytes memory bytecode =
// hex"60808060405234610016576111dc908161001c8239f35b600080fdfe608060408181526004918236101561001657600080fd5b600090813560e01c908163092af81314610484575080632c27a01f1461044d57806374912cd2146103d5578063775c300c146103655780638fa3425c1461033c578063c455279114610301578063d632522114610242578063f2fde38b146100ff5763ffa1ad741461008757600080fd5b346100fc57806003193601126100fc575080519080820182811067ffffffffffffffff8211176100e7576100e393508152600c82526b342e302e302d626574612e3360a01b602083015251918291602083526020830190610584565b0390f35b604184634e487b7160e01b6000525260246000fd5b80fd5b50913461023e57602036600319011261023e5761011a610518565b6001600160a01b038082168086526001602052848620549094929082168061020f575050338552600160205281852054169182156101f95733855260016020528185206001600160601b0360a01b908181541690558486528383872091825416179055823b156101f557815163f2fde38b60e01b8152908101849052848160248183875af180156101eb576101d8575b50519081527f6954f1cdad46901994f29d9b1f78744c873c527bad04d294b4954cc8caf367da60203392a380f35b6101e4909491946105ce565b92386101aa565b82513d87823e3d90fd5b8480fd5b6024915190636b3d7d8760e01b82523390820152fd5b83516356352f0b60e11b81526001600160a01b0392831686820190815291909216602082015281900360400190fd5b8280fd5b5090346102fd5760603660031901126102fd5761025d610518565b6001600160a01b039390929060243590858216820361023e576044359567ffffffffffffffff87116102f95761029885969736908401610533565b95909482891681526001602052205416806102ca576100e3866102bd8787878c61074c565b92909151928392836105a9565b85516356352f0b60e11b81526001600160a01b0380891693820193845290911660208301529081906040010390fd5b8380fd5b5080fd5b5090346102fd5760203660031901126102fd576020916001600160a01b039082908261032b610518565b168152600185522054169051908152f35b5090346102fd57816003193601126102fd5760025490516001600160a01b039091168152602090f35b50346100fc57806003193601126100fc573381526001602052819020546001600160a01b0390811692836103a857602083836103a03361061a565b915191168152f35b91516356352f0b60e11b8152339281019283526001600160a01b0390931660208301525081906040010390fd5b50913461023e57602036600319011261023e5781906103f2610518565b6001600160a01b0380821686526001602052929094205482168061041d5750506103a060209361061a565b92516356352f0b60e11b81526001600160a01b039485169181019182529390921660208301525081906040010390fd5b5090346102fd5760203660031901126102fd5760209181906001600160a01b03610475610518565b16815280845220549051908152f35b9050346102fd57826003193601126102fd5761049e610518565b9060243567ffffffffffffffff81116102f9576104be9036908701610533565b33855260016020529385902054939590936001600160a01b031690816104ee576100e3866102bd8988883361074c565b6356352f0b60e11b8352339083019081526001600160a01b03909116602082015281906040010390fd5b600435906001600160a01b038216820361052e57565b600080fd5b9181601f8401121561052e5782359167ffffffffffffffff831161052e576020838186019501011161052e57565b60005b8381106105745750506000910152565b8181015183820152602001610564565b9060209161059d81518092818552858086019101610561565b601f01601f1916010190565b6001600160a01b0390911681526040602082018190526105cb92910190610584565b90565b67ffffffffffffffff81116105e257604052565b634e487b7160e01b600052604160045260246000fd5b90601f8019910116810190811067ffffffffffffffff8211176105e257604052565b32600081815260208181526040808320548151928301948552828201819052949594929390929161065881606081015b03601f1981018352826105f8565b5190209160018060a01b03809616936001600160601b0360a01b968588600254161760025582516108238082019082821067ffffffffffffffff8311176107385791809188936109b98339039084f5801561072e57907f6aafca263a35a9d2a6e4e4659a84688092f4ae153df2f95cd7659508d95c1870939291169780600254166002558682526001602052888383209182541617905532815280602052816001850191205551806107298833963296849192604091949360608401958452602084015260018060a01b0316910152565b0390a4565b83513d84823e3d90fd5b634e487b7160e01b85526041600452602485fd5b90939132600052600060205260406000205493604051602081019061078b8161064a89328660209093929193604081019460018060a01b031681520152565b51902091306001600160601b0360a01b6002541617600255604051610823908181019181831067ffffffffffffffff8411176105e257859282916109b9833903906000f590811561092257606460009260018060a01b0316986001600160601b0360a01b6002541660025560018060a01b03871684526001602052604084208a6001600160601b0360a01b82541617905532845283602052600189016040852055846040519586948593631cff79cd60e01b855260018060a01b0316600485015260406024850152816044850152848401378181018301859052601f01601f19168101030181838a5af19081156109225760009161092e575b5093853b1561052e5760405163f2fde38b60e01b81526001600160a01b0384166004820152600081602481838b5af1801561092257610913575b506040805191825260208201929092526001600160a01b0386811692820192909252911690339032907f6aafca263a35a9d2a6e4e4659a84688092f4ae153df2f95cd7659508d95c1870908060608101610729565b61091c906105ce565b386108be565b6040513d6000823e3d90fd5b90503d806000833e61094081836105f8565b81019060208183031261052e5780519067ffffffffffffffff821161052e570181601f8201121561052e57805167ffffffffffffffff81116105e25760405192610994601f8301601f1916602001856105f8565b8184526020828401011161052e576109b29160208085019101610561565b3861088456fe60a080604052346100f357611388600155336080526323e8d09760e21b8152602081600481335afa9081156100e757600091610076575b50600080546001600160a01b0319166001600160a01b039290921691909117905560405161072a90816100f9823960805181818161023601526102d00152f35b60203d81116100e0575b601f8101601f191682016001600160401b038111838210176100cc576020918391604052810103126100c85751906001600160a01b03821682036100c5575038610036565b80fd5b5080fd5b634e487b7160e01b84526041600452602484fd5b503d610080565b6040513d6000823e3d90fd5b600080fdfe60806040526004361015610027575b36156100255761001d36610463565b602081519101f35b005b6000803560e01c9081631cff79cd1461009a575080631f9838b51461009557806338a40908146100905780637b1039991461008b5780638da5cb5b146100865780639d159568146100815763f2fde38b0361000e576102ac565b61028e565b610265565b610220565b6101db565b610178565b6040366003190112610106576100ae610109565b60243567ffffffffffffffff928382116101065736602383011215610106578160040135938411610106573660248584010111610106576101026100f6856024850186610554565b60405191829182610164565b0390f35b80fd5b600435906001600160a01b038216820361011f57565b600080fd5b919082519283825260005b848110610150575050826000602080949584010152601f8019910116010190565b60208183018101518483018201520161012f565b906020610175928181520190610124565b90565b3461011f57604036600319011261011f57610191610109565b602435906001600160a01b0390818316830361011f571660009081526003602090815260408083206001600160a01b03909416835292815291902060ff9054166040519015158152f35b3461011f57602036600319011261011f5760043563ffffffff60e01b811680910361011f576000526002602052602060018060a01b0360406000205416604051908152f35b3461011f57600036600319011261011f576040517f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03168152602090f35b3461011f57600036600319011261011f576000546040516001600160a01b039091168152602090f35b3461011f57600036600319011261011f576020600154604051908152f35b3461011f57602036600319011261011f576102c5610109565b6001600160a01b03907f000000000000000000000000000000000000000000000000000000000000000082163381036103175750166bffffffffffffffffffffffff60a01b6000541617600055600080f35b6044906040519063124ad49160e11b82526004820152336024820152fd5b634e487b7160e01b600052604160045260246000fd5b6040519190601f01601f1916820167ffffffffffffffff81118382101761037157604052565b610335565b67ffffffffffffffff811161037157601f01601f191660200190565b9190916103a66103a182610376565b61034b565b92818452811161011f57602081600092838387013784010152565b9291926103d06103a183610376565b938285528282011161011f57816000926020928387013784010152565b606090610175939260408252806040830152806000848401376000838284010152601f8019910116810190602083828403019101520190610124565b82610175949360609360408452816040850152848401376000838284010152601f8019910116810190602083828403019101520190610124565b600080356001600160e01b0319168082526002602052604090912091929161049290546001600160a01b031690565b6001600160a01b03811691821561052a57506104b76104b13686610392565b83610673565b927fc4dabe0d7ef7462e2218f2c398c21ef217803e1c46f5cf802d1a5d1d9b503f2f8480976104eb604051928392836103ed565b0390a2156104f7575050565b8151156105075750805190602001fd5b60405163023c045d60e21b81526001600160a01b03919091166004820152602490fd5b60405163300eff3960e21b81523360048201526001600160e01b0319919091166024820152604490fd5b6000549293926001600160a01b03929083163381141580610645575b6106155750813b156105f4577fb24ebe141c5f2a744b103bea65fce6c40e0dc65d7341d092c09b160f404479906105b16105ab3688856103c1565b84610673565b9490936105c8868099604051948594169684610429565b0390a2156105d35750565b8051156105e257602081519101fd5b60405163061a160d60e41b8152600490fd5b604051636d17e5ef60e11b81526001600160a01b0383166004820152602490fd5b6040516355d1750960e01b81526001600160a01b0391821660048201523360248201529216604483015250606490fd5b503360009081526003602090815260408083206001600160a01b038716845290915290205460ff1615610570565b6000805490936001600160a01b039182169390925a9060015482039182116107165791869291839260208351930191f4923d1561070c576106d63d6106ba6103a182610376565b9081523d87602083013e5b95549495946001600160a01b031690565b91821681036106e3575050565b60405163609ca23d60e01b81526001600160a01b03918216600482015291166024820152604490fd5b6106d660606106c5565b634e487b7160e01b87526011600452602487fd";



///@notice deploy the bytecode with the create instruction
address deployedAddress;
assembly {
deployedAddress := create(0, add(bytecode, 0x20), mload(bytecode))
}

// emit log(deployedAddress);


///@notice check that the deployment was successful
require(deployedAddress != address(0), "Could not deploy Yul contract");

///@notice return the address that the contract was deployed to
return deployedAddress;
}

bytes proof;
address syncStepVerifierAddress;

function run() external {
vm.startBroadcast();

syncStepVerifierAddress = address(deployContract("sync_step_k21"));

proof = vm.parseBytes(vm.readFile("test/data/sync_step_21.calldata"));

Spectre spectre = new Spectre(syncStepVerifierAddress);

spectre.postHeader(proof);

vm.stopBroadcast();
}
}
6 changes: 6 additions & 0 deletions contracts/script/deploy_local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh
cd $(git rev-parse --show-toplevel)
source .env
LOCAL_RPC_URL="http://localhost:8545"

forge script script/SpectreDeployLocal.s.sol:SpectreDeployLocal --private-key $ANVIL_PRIVATE_KEY --rpc-url $LOCAL_RPC_URL --broadcast -vvvv
Loading

0 comments on commit 588c5ee

Please sign in to comment.