Skip to content

Commit

Permalink
Switch to snark-verifier Solidity contracts (#61)
Browse files Browse the repository at this point in the history
* switch to deneb

* gen step contract

* switch to latest halo2-lib changes

* assert_satisfied_par -> assert_satisfied

* expose finalized_header_root as hi/lo

* remove halo2-solidity-verified dependency

* update halo2-lib dep

* remove contracts dir

* add contracts add submodule

* contract tests (wip)

* update submodule

* chore

* fix tests

* remove unused dep on contracts

* init submodules in gh actions

* fix step circuit test

* removed commeted code

* fix rotate circuti tests
  • Loading branch information
nulltea authored Feb 21, 2024
1 parent 864e966 commit 9642044
Show file tree
Hide file tree
Showing 51 changed files with 171 additions and 1,003 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ jobs:
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1

- name: init submodules
uses: snickerbockers/submodules-init@v4

- name: Build contracts
run: just build-contracts

# skip this in favour of compiling in the `Test` action.
# - name: Check
# run: cargo check --all

- name: Test
run: RUST_LOG="lightclient-circuits=debug" PARAMS_DIR="./test_data" cargo test --release test_eth2_spec_mock_1 -- --nocapture
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "contracts/lib/forge-std"]
path = contracts/lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "contracts"]
path = contracts
url = https://github.com/ChainSafe/spectre-contracts
22 changes: 9 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,18 @@ zkevm-hashes = { git = "https://github.com/axiom-crypto/halo2-lib", tag = "v0.4.
halo2curves = { package = "halo2curves-axiom", version = "=0.5.2" }

# verifier SDK
snark-verifier = { git = "https://github.com/axiom-crypto/snark-verifier.git", tag = "v0.1.6", default-features = false, features = [
snark-verifier = { git = "https://github.com/axiom-crypto/snark-verifier.git", tag = "v0.1.7-git", default-features = false, features = [
"display",
"loader_halo2",
"loader_evm",
"halo2-pse",
] }
snark-verifier-sdk = { git = "https://github.com/axiom-crypto/snark-verifier.git", tag = "v0.1.6", default-features = false, features = [
snark-verifier-sdk = { git = "https://github.com/axiom-crypto/snark-verifier.git", tag = "v0.1.7-git", default-features = false, features = [
"display",
"loader_halo2",
"loader_evm",
"halo2-pse",
"revm",
] }

# ethereum types
Expand Down Expand Up @@ -90,16 +91,11 @@ ark-std = { version = "0.4.0", features = ["print-trace"] }

[patch.crates-io]
ssz_rs = { git = "https://github.com/ralexstokes/ssz-rs", rev = "5f1ec833718efa07bbbff427ab28a1eeaa706164" }
halo2-base = { git = "https://github.com/nulltea/halo2-lib", branch = "feat/zkevm-sha256-builder" }
halo2-ecc = { git = "https://github.com/nulltea/halo2-lib", branch = "feat/zkevm-sha256-builder" }
zkevm-hashes = { git = "https://github.com/nulltea/halo2-lib", branch = "feat/zkevm-sha256-builder" }
halo2-base = { git = "https://github.com/nulltea/halo2-lib", branch = "feat/bls12-381-hash2curve" }
halo2-ecc = { git = "https://github.com/nulltea/halo2-lib", branch = "feat/bls12-381-hash2curve" }
zkevm-hashes = { git = "https://github.com/nulltea/halo2-lib", branch = "feat/bls12-381-hash2curve" }

[patch."https://github.com/axiom-crypto/halo2-lib"]
halo2-base = { git = "https://github.com/nulltea/halo2-lib", branch = "feat/zkevm-sha256-builder" }
halo2-ecc = { git = "https://github.com/nulltea/halo2-lib", branch = "feat/zkevm-sha256-builder" }
zkevm-hashes = { git = "https://github.com/nulltea/halo2-lib", branch = "feat/zkevm-sha256-builder" }


[patch."https://github.com/axiom-crypto/snark-verifier.git"]
snark-verifier = { git = "https://github.com/nulltea/snark-verifier", branch = "yul-codegen" }
snark-verifier-sdk = { git = "https://github.com/nulltea/snark-verifier", branch = "yul-codegen" }
halo2-base = { git = "https://github.com/nulltea/halo2-lib", branch = "feat/bls12-381-hash2curve" }
halo2-ecc = { git = "https://github.com/nulltea/halo2-lib", branch = "feat/bls12-381-hash2curve" }
zkevm-hashes = { git = "https://github.com/nulltea/halo2-lib", branch = "feat/bls12-381-hash2curve" }
109 changes: 0 additions & 109 deletions contract-tests/tests/rotation_input_encoding.rs

This file was deleted.

26 changes: 15 additions & 11 deletions contract-tests/tests/spectre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ use std::path::PathBuf;
use std::sync::Arc;

use contract_tests::make_client;
use contracts::{
CommitteeUpdateMockVerifier, Spectre, SyncStepCompressedMockVerifier, SyncStepInput,
};
use contracts::{MockVerifier, Spectre};
use eth_types::{Minimal, LIMB_BITS};
use ethers::core::types::U256;
use ethers::providers::Middleware;
use halo2_base::halo2_proofs::halo2curves::bn256::Fr;
use lightclient_circuits::sync_step_circuit::StepCircuit;
use rstest::rstest;
use test_utils::{get_initial_sync_committee_poseidon, read_test_files_and_gen_witness};

const SLOTS_PER_EPOCH: usize = 8;
const EPOCHS_PER_SYNC_COMMITTEE_PERIOD: usize = 8;
const SLOTS_PER_SYNC_COMMITTEE_PERIOD: usize = EPOCHS_PER_SYNC_COMMITTEE_PERIOD * SLOTS_PER_EPOCH;
const FINALITY_THRESHOLD: usize = 20; // ~ 2/3 of 32

#[tokio::test]
async fn test_deploy_spectre() -> anyhow::Result<()> {
Expand Down Expand Up @@ -54,9 +56,14 @@ async fn test_contract_initialization_and_first_step(
// pre conditions
assert_eq!(contract.head().call().await?, U256::from(0));

let instances = StepCircuit::<Minimal, Fr>::get_instances(&witness, LIMB_BITS);

// call step with the input and proof
let step_input: SyncStepInput = witness.into();
let step_call = contract.step(step_input.clone(), Vec::new().into());
let step_input: contracts::StepInput = witness.into();
let mut proof = vec![0; 384];
proof.extend(instances[0][0].to_bytes().into_iter().rev());
proof.extend(instances[0][1].to_bytes().into_iter().rev());
let step_call = contract.step(step_input.clone(), proof.into());
let _receipt = step_call.send().await?.confirmations(1).await?;

// post conditions
Expand Down Expand Up @@ -85,12 +92,8 @@ async fn deploy_spectre_mock_verifiers<M: Middleware + 'static>(
initial_sync_committee_poseidon: U256,
slots_per_period: usize,
) -> anyhow::Result<Spectre<M>> {
let step_verifier = SyncStepCompressedMockVerifier::deploy(ethclient.clone(), ())?
.send()
.await?;
let update_verifier = CommitteeUpdateMockVerifier::deploy(ethclient.clone(), ())?
.send()
.await?;
let step_verifier = MockVerifier::deploy(ethclient.clone(), ())?.send().await?;
let update_verifier = MockVerifier::deploy(ethclient.clone(), ())?.send().await?;
Ok(Spectre::deploy(
ethclient,
(
Expand All @@ -99,6 +102,7 @@ async fn deploy_spectre_mock_verifiers<M: Middleware + 'static>(
U256::from(initial_sync_period),
initial_sync_committee_poseidon,
U256::from(slots_per_period),
U256::from(FINALITY_THRESHOLD)
),
)?
.send()
Expand Down
28 changes: 12 additions & 16 deletions contract-tests/tests/step_input_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ use contract_tests::make_client;
use eth_types::{Minimal, LIMB_BITS};
use ethers::contract::abigen;
use lightclient_circuits::halo2_proofs::halo2curves::bn256;
use lightclient_circuits::sync_step_circuit::StepCircuit;
use lightclient_circuits::witness::SyncStepArgs;
use rstest::rstest;
use ssz_rs::Merkleized;
use test_utils::read_test_files_and_gen_witness;

abigen!(
SyncStepExternal,
"../contracts/out/SyncStepExternal.sol/SyncStepExternal.json"
StepExternal,
"../contracts/out/StepExternal.sol/StepExternal.json";
);

// SyncStepInput type produced by abigen macro matches the solidity struct type
impl<Spec: eth_types::Spec> From<SyncStepArgs<Spec>> for SyncStepInput {
impl<Spec: eth_types::Spec> From<SyncStepArgs<Spec>> for StepInput {
fn from(args: SyncStepArgs<Spec>) -> Self {
let participation = args
.pariticipation_bits
Expand All @@ -40,13 +39,12 @@ impl<Spec: eth_types::Spec> From<SyncStepArgs<Spec>> for SyncStepInput {

let execution_payload_root: [u8; 32] = args.execution_payload_root.try_into().unwrap();

SyncStepInput {
StepInput {
attested_slot: args.attested_header.slot,
finalized_slot: args.finalized_header.slot,
participation,
finalized_header_root,
execution_payload_root,
accumulator: Default::default(),
}
}
}
Expand All @@ -59,25 +57,23 @@ async fn test_step_instance_commitment_evm_equivalence(
path: PathBuf,
) -> anyhow::Result<()> {
use contract_tests::decode_solidity_u256_array;
use ethers::types::U256;

let (witness, _) = read_test_files_and_gen_witness(&path);
let instance = StepCircuit::<Minimal, bn256::Fr>::get_instances(&witness, LIMB_BITS);
let instance =
lightclient_circuits::sync_step_circuit::StepCircuit::<Minimal, bn256::Fr>::get_instances(
&witness, LIMB_BITS,
);

let (_anvil_instance, ethclient) = make_client();
let contract = SyncStepExternal::deploy(ethclient, ())?.send().await?;
let contract = StepExternal::deploy(ethclient, ())?.send().await?;

let result = contract
.to_public_inputs(
SyncStepInput::from(witness),
U256::from_little_endian(&instance[0][1].to_bytes()),
)
.to_public_inputs_commitment(StepInput::from(witness))
.call()
.await?;
let result_decoded = decode_solidity_u256_array(&result);
let result_decoded = decode_solidity_u256_array(&[result]);

assert_eq!(result_decoded[12], instance[0][0]); // public input commitment
assert_eq!(result_decoded[13], instance[0][1]); // committee poseidon
assert_eq!(result_decoded[0], instance[0][0]); // public input commitment

Ok(())
}
1 change: 1 addition & 0 deletions contracts
Submodule contracts added at 4418b0
12 changes: 0 additions & 12 deletions contracts/.gitignore

This file was deleted.

16 changes: 0 additions & 16 deletions contracts/Cargo.toml

This file was deleted.

Loading

0 comments on commit 9642044

Please sign in to comment.