Skip to content
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

Use zkevm-sha256 gates for Sha256Wide chip #33

Merged
merged 26 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 42 additions & 21 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[workspace]
members = ["lightclient-circuits", "prover", "preprocessor", "eth-types", "contract-tests", "test-utils", "contracts"]

resolver = "2"

# Definition of benchmarks profile to use.
[profile.bench]
opt-level = 3
Expand All @@ -24,26 +26,23 @@ incremental = true
[workspace.dependencies]
lightclient-circuits = { path = "lightclient-circuits" }
test-utils = { path = "test-utils" }
ethereum-consensus-types = { git = "ssh://[email protected]/sygmaprotocol/Zipline.git", rev = "5ecf28d24690862814a7ebc5385c7e5c2eeb6e3d" }

eth-types = { path = "eth-types" }
contracts = { path = "contracts" }
preprocessor = { path = "preprocessor" }
zipline-test-utils = { package = "test-utils", git = "ssh://[email protected]/sygmaprotocol/Zipline.git", rev = "5ecf28d24690862814a7ebc5385c7e5c2eeb6e3d" }
zipline-cryptography = { package = "cryptography", git = "ssh://[email protected]/sygmaprotocol/Zipline.git", rev = "5ecf28d24690862814a7ebc5385c7e5c2eeb6e3d" }
light-client-verifier = { git = "ssh://[email protected]/sygmaprotocol/Zipline.git", rev = "5ecf28d24690862814a7ebc5385c7e5c2eeb6e3d" }
beacon-api-client = { git = "https://github.com/ralexstokes/ethereum-consensus.git", rev = "f3bff52e9c43866f231ec40c8ab0e34125a8957f" }


# halo2
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v2023_02_02", features = [
"dev-graph",
] }
halo2curves = { git = "https://github.com/privacy-scaling-explorations/halo2curves", tag = "0.3.1" }
halo2-base = { git = "https://github.com/axiom-crypto/halo2-lib", branch = "community-edition", default-features = false, features = [
"halo2-pse",
"display",
"jemallocator"
] }
halo2-ecc = { git = "https://github.com/axiom-crypto/halo2-lib", branch = "community-edition", default-features = false }
poseidon = { git = "https://github.com/axiom-crypto/halo2-lib", branch = "community-edition", default-features = false }
zkevm-hashes = { git = "https://github.com/axiom-crypto/halo2-lib", branch = "community-edition", default-features = false }

halo2curves = { package = "halo2curves-axiom", version = "0.4.2" }


# verifier SDK
snark-verifier = { git = "https://github.com/axiom-crypto/snark-verifier.git", branch = "community-edition", default-features = false, features = [
Expand All @@ -60,6 +59,12 @@ snark-verifier-sdk = { git = "https://github.com/axiom-crypto/snark-verifier.git
] }
halo2_solidity_verifier = { git = "https://github.com/privacy-scaling-explorations/halo2-solidity-verifier", branch = "ac/initial-verifier-PR" }

# ethereum types
ethereum-consensus-types = { git = "ssh://[email protected]/sygmaprotocol/Zipline.git", rev = "5ecf28d24690862814a7ebc5385c7e5c2eeb6e3d" }
zipline-test-utils = { package = "test-utils", git = "ssh://[email protected]/sygmaprotocol/Zipline.git", rev = "5ecf28d24690862814a7ebc5385c7e5c2eeb6e3d" }
zipline-cryptography = { package = "cryptography", git = "ssh://[email protected]/sygmaprotocol/Zipline.git", rev = "5ecf28d24690862814a7ebc5385c7e5c2eeb6e3d" }
light-client-verifier = { git = "ssh://[email protected]/sygmaprotocol/Zipline.git", rev = "5ecf28d24690862814a7ebc5385c7e5c2eeb6e3d" }
beacon-api-client = { git = "https://github.com/ralexstokes/ethereum-consensus.git", rev = "f3bff52e9c43866f231ec40c8ab0e34125a8957f" }
ssz_rs = "0.9"

# crypto
Expand All @@ -70,7 +75,6 @@ ff = "0.12"
sha2 = { version = "0.10.6", features = ["compress"] }
uint = "0.9.1"
ark-std = { version = "0.4.0", features = ["print-trace"] }
poseidon_native = { git = "https://github.com/axiom-crypto/halo2.git", branch = "axiom/dev", package = "poseidon" }

# misc
itertools = "0.11.0"
Expand All @@ -79,17 +83,34 @@ serde_json = "1.0.78"
log = "0.4.14"
hex = "0.4"

[patch."https://github.com/privacy-scaling-explorations/halo2curves"]
halo2curves = { git = "https://github.com/sygmaprotocol/halo2curves", branch = "dev/bls12_381" }
[patch.crates-io]
halo2curves = { git = "https://github.com/timoftime/halo2curves", package = "halo2curves-axiom", rev = "f3bb3f5a7d3a8ca806368f185c112283a73a94cb" }
ssz_rs = { git = "https://github.com/ralexstokes/ssz-rs", rev = "5f1ec833718efa07bbbff427ab28a1eeaa706164" }


[patch."https://github.com/axiom-crypto/halo2-lib"]
halo2-base = { git = "https://github.com/timoftime/halo2-lib", rev = "95bf9a5ce6b62a3f28b163748a7494281d814496" }
halo2-ecc = { git = "https://github.com/timoftime/halo2-lib", rev = "95bf9a5ce6b62a3f28b163748a7494281d814496" }
poseidon = { git = "https://github.com/timoftime/halo2-lib", rev = "95bf9a5ce6b62a3f28b163748a7494281d814496" }
halo2-base = { git = "https://github.com/timoftime/halo2-lib", branch = "feat/zkevm-sha256-builder", default-features = false, features = [
"halo2-pse",
"display",
"jemallocator"
] }
halo2-ecc = { git = "https://github.com/timoftime/halo2-lib", branch = "feat/zkevm-sha256-builder", default-features = false, features = [
"halo2-pse",
"jemallocator"
] }
zkevm-hashes = { git = "https://github.com/timoftime/halo2-lib", branch = "feat/zkevm-sha256-builder", default-features = false }
# halo2-base = { path = "../halo2-lib/halo2-base", default-features = false, features = [
# "halo2-pse",
# "display",
# ] }
# halo2-ecc = { path = "../halo2-lib/halo2-ecc", default-features = false, features = [
# "halo2-pse",
# ] }
# zkevm-hashes = { path = "../halo2-lib/hashes/zkevm", 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" }
snark-verifier-sdk = { git = "https://github.com/timoftime/snark-verifier", branch = "timoftime/bump-revm" }

[patch.crates-io]
ssz_rs = { git = "https://github.com/ralexstokes/ssz-rs", rev = "5f1ec833718efa07bbbff427ab28a1eeaa706164" }
[patch."https://github.com/axiom-crypto/snark-verifier.git"]
snark-verifier = { git = "https://github.com/timoftime/snark-verifier", branch = "halo2-pse-fix", default-features = false }
snark-verifier-sdk = { git = "https://github.com/timoftime/snark-verifier", branch = "halo2-pse-fix", default-features = false }
# snark-verifier = { path = "../snark-verifier/snark-verifier" }
# snark-verifier-sdk = { path = "../snark-verifier/snark-verifier-sdk" }
84 changes: 39 additions & 45 deletions contract-tests/tests/rotation_input_encoding.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

use std::path::PathBuf;

use contract_tests::make_client;
use eth_types::Minimal;
use eth_types::LIMB_BITS;
use ethers::contract::abigen;
use halo2curves::bn256::{self, Fr};
use itertools::Itertools;
use lightclient_circuits::committee_update_circuit::CommitteeUpdateCircuit;
use lightclient_circuits::halo2_proofs::halo2curves::bn256;
use lightclient_circuits::poseidon::poseidon_committee_commitment_from_compressed;
use lightclient_circuits::witness::CommitteeRotationArgs;
use rstest::rstest;
Expand All @@ -21,6 +23,40 @@ abigen!(
"../contracts/out/RotateExternal.sol/RotateExternal.json"
);

// CommitteeRotationArgs type produced by abigen macro matches the solidity struct type
impl<Spec: eth_types::Spec> From<CommitteeRotationArgs<Spec>> for RotateInput
where
[(); Spec::SYNC_COMMITTEE_SIZE]:,
{
fn from(args: CommitteeRotationArgs<Spec>) -> Self {
let poseidon_commitment_le = poseidon_committee_commitment_from_compressed(
&args.pubkeys_compressed.iter().cloned().collect_vec(),
)
.unwrap();

let mut pk_vector: Vector<Vector<u8, 48>, { Spec::SYNC_COMMITTEE_SIZE }> = args
.pubkeys_compressed
.iter()
.cloned()
.map(|v| v.try_into().unwrap())
.collect_vec()
.try_into()
.unwrap();

let sync_committee_ssz = pk_vector
.hash_tree_root()
.unwrap()
.deref()
.try_into()
.unwrap();

RotateInput {
sync_committee_ssz,
sync_committee_poseidon: poseidon_commitment_le,
}
}
}

#[rstest]
#[tokio::test]
async fn test_rotate_public_input_evm_equivalence(
Expand All @@ -29,7 +65,7 @@ async fn test_rotate_public_input_evm_equivalence(
path: PathBuf,
) -> anyhow::Result<()> {
let (_, witness) = read_test_files_and_gen_witness(&path);
let instance = CommitteeUpdateCircuit::<Minimal, bn256::Fr>::instance(&witness);
let instance = CommitteeUpdateCircuit::<Minimal, bn256::Fr>::instance(&witness, LIMB_BITS);
let finalized_block_root = witness
.finalized_header
.clone()
Expand All @@ -53,53 +89,11 @@ async fn test_rotate_public_input_evm_equivalence(
.map(|v| {
let mut b = [0_u8; 32];
v.to_little_endian(&mut b);
bn256::Fr::from_bytes(&b).unwrap()
bn256::Fr::from_bytes(&b).expect("bad bn256::Fr encoding")
})
.collect();

assert_eq!(result_decoded.len(), instance[0].len());
assert_eq!(vec![result_decoded], instance);
Ok(())
}

// CommitteeRotationArgs type produced by abigen macro matches the solidity struct type
impl<Spec: eth_types::Spec> From<CommitteeRotationArgs<Spec, Fr>> for RotateInput
where
[(); Spec::SYNC_COMMITTEE_SIZE]:,
{
fn from(args: CommitteeRotationArgs<Spec, Fr>) -> Self {
let poseidon_commitment_le = poseidon_committee_commitment_from_compressed(
&args
.pubkeys_compressed
.iter()
.cloned()
.map(|mut b| {
b.reverse();
b
})
.collect_vec(),
)
.unwrap();

let mut pk_vector: Vector<Vector<u8, 48>, { Spec::SYNC_COMMITTEE_SIZE }> = args
.pubkeys_compressed
.iter()
.cloned()
.map(|v| v.try_into().unwrap())
.collect_vec()
.try_into()
.unwrap();

let sync_committee_ssz = pk_vector
.hash_tree_root()
.unwrap()
.deref()
.try_into()
.unwrap();

RotateInput {
sync_committee_ssz,
sync_committee_poseidon: poseidon_commitment_le,
}
}
}
31 changes: 1 addition & 30 deletions contract-tests/tests/spectre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use std::path::PathBuf;
use std::sync::Arc;

use contract_tests::make_client;
use contracts::{
CommitteeUpdateMockVerifier, CommitteeUpdateVerifier, Spectre, StepMockVerifier, StepVerifier,
};
use contracts::{CommitteeUpdateMockVerifier, Spectre, StepMockVerifier};
use ethers::core::types::U256;
use ethers::providers::Middleware;
use rstest::rstest;
Expand Down Expand Up @@ -75,33 +73,6 @@ async fn test_contract_initialization_and_first_step(

//////////// deployment helpers //////////////////

/// Deploy the Spectre contract using the given ethclient
/// Also deploys the step verifier and the update verifier contracts
/// and passes their addresses along with the other params to the constructor
async fn deploy_spectre<M: Middleware + 'static>(
ethclient: Arc<M>,
initial_sync_period: usize,
initial_sync_committee_poseidon: [u8; 32],
slots_per_period: usize,
) -> anyhow::Result<Spectre<M>> {
let step_verifier = StepVerifier::deploy(ethclient.clone(), ())?.send().await?;
let update_verifier = CommitteeUpdateVerifier::deploy(ethclient.clone(), ())?
.send()
.await?;
Ok(Spectre::deploy(
ethclient,
(
step_verifier.address(),
update_verifier.address(),
U256::from(initial_sync_period),
initial_sync_committee_poseidon,
U256::from(slots_per_period),
),
)?
.send()
.await?)
}

/// Deploy the Spectre contract using the given ethclient
/// Also deploys the step verifier and the update verifier contracts
/// and passes their addresses along with the other params to the constructor
Expand Down
58 changes: 29 additions & 29 deletions contract-tests/tests/step_input_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use std::ops::Deref;
use std::path::PathBuf;

use contract_tests::make_client;
use eth_types::Minimal;
use eth_types::{Minimal, LIMB_BITS};
use ethers::contract::abigen;
use halo2curves::bn256;
use lightclient_circuits::halo2_proofs::halo2curves::bn256;
use lightclient_circuits::poseidon::poseidon_committee_commitment_from_uncompressed;
use lightclient_circuits::sync_step_circuit::SyncStepCircuit;
use lightclient_circuits::sync_step_circuit::StepCircuit;
use lightclient_circuits::witness::SyncStepArgs;
use rstest::rstest;
use ssz_rs::Merkleized;
Expand All @@ -17,32 +17,6 @@ abigen!(
"../contracts/out/SyncStepExternal.sol/SyncStepExternal.json"
);

#[rstest]
#[tokio::test]
async fn test_step_instance_commitment_evm_equivalence(
#[files("../consensus-spec-tests/tests/minimal/capella/light_client/sync/pyspec_tests/**")]
#[exclude("deneb*")]
path: PathBuf,
) -> anyhow::Result<()> {
let (witness, _) = read_test_files_and_gen_witness(&path);
let instance = SyncStepCircuit::<Minimal, bn256::Fr>::instance_commitment(&witness);
let poseidon_commitment_le =
poseidon_committee_commitment_from_uncompressed(&witness.pubkeys_uncompressed)?;

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

let result = contract
.to_input_commitment(SyncStepInput::from(witness), poseidon_commitment_le)
.call()
.await?;
let mut result_bytes = [0_u8; 32];
result.to_little_endian(&mut result_bytes);

assert_eq!(bn256::Fr::from_bytes(&result_bytes).unwrap(), instance);
Ok(())
}

// SyncStepInput type produced by abigen macro matches the solidity struct type
impl<Spec: eth_types::Spec> From<SyncStepArgs<Spec>> for SyncStepInput {
fn from(args: SyncStepArgs<Spec>) -> Self {
Expand Down Expand Up @@ -72,3 +46,29 @@ impl<Spec: eth_types::Spec> From<SyncStepArgs<Spec>> for SyncStepInput {
}
}
}

#[rstest]
#[tokio::test]
async fn test_step_instance_commitment_evm_equivalence(
#[files("../consensus-spec-tests/tests/minimal/capella/light_client/sync/pyspec_tests/**")]
#[exclude("deneb*")]
path: PathBuf,
) -> anyhow::Result<()> {
let (witness, _) = read_test_files_and_gen_witness(&path);
let instance = StepCircuit::<Minimal, bn256::Fr>::instance_commitment(&witness, LIMB_BITS);
let poseidon_commitment_le =
poseidon_committee_commitment_from_uncompressed(&witness.pubkeys_uncompressed)?;

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

let result = contract
.to_input_commitment(SyncStepInput::from(witness), poseidon_commitment_le)
.call()
.await?;
let mut result_bytes = [0_u8; 32];
result.to_little_endian(&mut result_bytes);

assert_eq!(bn256::Fr::from_bytes(&result_bytes).unwrap(), instance);
Ok(())
}
Loading