Skip to content

Commit

Permalink
Cleanup Some Obvious Stuff (#54)
Browse files Browse the repository at this point in the history
* Update toolchain to latest nightly

* clippy

* remove some easy to remove clones

* Shouldnt re read params

* unnecessary clone

* remove cli batteries

* Remove mut

* fix halo2curves dependency

* remove unused rpc structs

* Remove unnecessary mut

* remove mock_circuit method

* Update README.md

---------

Co-authored-by: Timofey Luin <[email protected]>
  • Loading branch information
ec2 and nulltea authored Jan 11, 2024
1 parent 1697d63 commit ea1cb85
Show file tree
Hide file tree
Showing 20 changed files with 110 additions and 110 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ halo2-base = { git = "https://github.com/axiom-crypto/halo2-lib", branch = "comm
halo2-ecc = { 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" }
halo2curves = { package = "halo2curves-axiom", version = "0.5" }

# verifier SDK
snark-verifier = { git = "https://github.com/axiom-crypto/snark-verifier.git", branch = "community-edition", default-features = false, features = [
Expand Down Expand Up @@ -89,7 +89,7 @@ ark-std = { version = "0.4.0", features = ["print-trace"] }


[patch.crates-io]
halo2curves = { git = "https://github.com/timoftime/halo2curves", package = "halo2curves-axiom", rev = "1bd39b8" }
halo2curves = { git = "https://github.com/timoftime/halo2curves", package = "halo2curves-axiom", branch = "support_bls12-381" }
ssz_rs = { git = "https://github.com/ralexstokes/ssz-rs", rev = "5f1ec833718efa07bbbff427ab28a1eeaa706164" }


Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,6 @@ where `<NETWORK>` is one of `["GOERLI", "SEPOLIA", "MAINNET"]`.
Prover is accessible via JSON RPC interface. To start it, run:

```shell
cargo run -r -- rpc --port 3000
cargo run -r -- rpc --port 3000 --spec testnet
```
where `--spec` is one of `["testnet", "mainnet"]`.
37 changes: 17 additions & 20 deletions lightclient-circuits/src/committee_update_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ impl<S: Spec, F: Field> CommitteeUpdateCircuit<S, F> {

let mut pk_vector: Vector<Vector<u8, 48>, { S::SYNC_COMMITTEE_SIZE }> = args
.pubkeys_compressed
.as_slice()
.iter()
.cloned()
.map(|v| v.try_into().unwrap())
.map(|v| v.as_slice().try_into().unwrap())
.collect_vec()
.try_into()
.unwrap();
Expand Down Expand Up @@ -329,12 +329,13 @@ mod tests {
fn test_committee_update_circuit() {
const K: u32 = 18;
let witness = load_circuit_args();
let params: ParamsKZG<Bn256> = gen_srs(K);

let circuit = CommitteeUpdateCircuit::<Testnet, Fr>::mock_circuit(
let circuit = CommitteeUpdateCircuit::<Testnet, Fr>::create_circuit(
CircuitBuilderStage::Mock,
None,
&witness,
K,
&params,
)
.unwrap();

Expand Down Expand Up @@ -392,25 +393,25 @@ mod tests {
);

let witness = load_circuit_args();
let snark = gen_application_snark(&params_app, &pk_app, &witness, APP_PINNING_PATH);
let snark = vec![gen_application_snark(
&params_app,
&pk_app,
&witness,
APP_PINNING_PATH,
)];

let agg_params = gen_srs(AGG_K);
println!("agg_params k: {:?}", agg_params.k());

let pk = AggregationCircuit::create_pk(
&agg_params,
AGG_PK_PATH,
AGG_CONFIG_PATH,
&vec![snark.clone()],
None,
);
let pk =
AggregationCircuit::create_pk(&agg_params, AGG_PK_PATH, AGG_CONFIG_PATH, &snark, None);

let agg_config = AggregationConfigPinning::from_path(AGG_CONFIG_PATH);

let agg_circuit = AggregationCircuit::create_circuit(
CircuitBuilderStage::Prover,
Some(agg_config),
&vec![snark.clone()],
&snark,
&agg_params,
)
.unwrap();
Expand All @@ -423,13 +424,9 @@ mod tests {

let proof = gen_evm_proof_shplonk(&agg_params, &pk, agg_circuit, instances.clone());
println!("proof size: {}", proof.len());
let deployment_code = AggregationCircuit::gen_evm_verifier_shplonk(
&agg_params,
&pk,
None::<String>,
&vec![snark],
)
.unwrap();
let deployment_code =
AggregationCircuit::gen_evm_verifier_shplonk(&agg_params, &pk, None::<String>, &snark)
.unwrap();
println!("deployment_code size: {}", deployment_code.len());
evm_verify(deployment_code, instances, proof);
}
Expand Down
1 change: 0 additions & 1 deletion lightclient-circuits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#![feature(stmt_expr_attributes)]
#![feature(trait_alias)]
#![feature(generic_arg_infer)]
#![feature(return_position_impl_trait_in_trait)]
#![allow(clippy::needless_range_loop)]

pub mod gadget;
Expand Down
33 changes: 17 additions & 16 deletions lightclient-circuits/src/sync_step_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ impl<S: Spec, F: Field> StepCircuit<S, F> {

let pubkey_affines = args
.pubkeys_uncompressed
.as_slice()
.iter()
.cloned()
.map(|bytes| {
G1Affine::from_uncompressed_unchecked(&bytes.as_slice().try_into().unwrap())
.unwrap()
Expand Down Expand Up @@ -264,8 +264,8 @@ impl<S: Spec, F: Field> StepCircuit<S, F> {

let pubkey_affines = args
.pubkeys_uncompressed
.as_slice()
.iter()
.cloned()
.map(|bytes| {
G1Affine::from_uncompressed_unchecked(&bytes.as_slice().try_into().unwrap())
.unwrap()
Expand Down Expand Up @@ -468,10 +468,15 @@ mod tests {
fn test_step_circuit() {
const K: u32 = 20;
let witness = load_circuit_args();
let params: ParamsKZG<Bn256> = gen_srs(K);

let circuit =
StepCircuit::<Testnet, Fr>::mock_circuit(CircuitBuilderStage::Mock, None, &witness, K)
.unwrap();
let circuit = StepCircuit::<Testnet, Fr>::create_circuit(
CircuitBuilderStage::Mock,
None,
&witness,
&params,
)
.unwrap();

let instance = StepCircuit::<Testnet, Fr>::get_instances(&witness, LIMB_BITS);

Expand Down Expand Up @@ -562,22 +567,22 @@ mod tests {
);

let witness = load_circuit_args();
let snark = StepCircuit::<Testnet, Fr>::gen_snark_shplonk(
let snark = vec![StepCircuit::<Testnet, Fr>::gen_snark_shplonk(
&params_app,
&pk_app,
APP_PINNING_PATH,
None::<String>,
&witness,
)
.unwrap();
.unwrap()];

let agg_params = gen_srs(AGG_K);

let pk = AggregationCircuit::create_pk(
&agg_params,
AGG_PK_PATH,
AGG_CONFIG_PATH,
&vec![snark.clone()],
&snark.clone(),
Some(AggregationConfigPinning::new(AGG_K, 19)),
);

Expand All @@ -586,7 +591,7 @@ mod tests {
let agg_circuit = AggregationCircuit::create_circuit(
CircuitBuilderStage::Prover,
Some(agg_config),
&vec![snark.clone()],
&snark,
&agg_params,
)
.unwrap();
Expand All @@ -599,13 +604,9 @@ mod tests {

let proof = gen_evm_proof_shplonk(&agg_params, &pk, agg_circuit, instances.clone());
println!("proof size: {}", proof.len());
let deployment_code = AggregationCircuit::gen_evm_verifier_shplonk(
&agg_params,
&pk,
None::<String>,
&vec![snark],
)
.unwrap();
let deployment_code =
AggregationCircuit::gen_evm_verifier_shplonk(&agg_params, &pk, None::<String>, &snark)
.unwrap();
println!("deployment_code size: {}", deployment_code.len());
evm_verify(deployment_code, instances, proof);
}
Expand Down
12 changes: 0 additions & 12 deletions lightclient-circuits/src/util/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use halo2_base::halo2_proofs::{
plonk::{Circuit, Error, VerifyingKey},
poly::kzg::commitment::ParamsKZG,
};
use halo2_base::utils::fs::gen_srs;
use serde::{Deserialize, Serialize};
use snark_verifier_sdk::evm::{
encode_calldata, evm_verify, gen_evm_proof_shplonk, gen_evm_verifier_shplonk,
Expand Down Expand Up @@ -249,17 +248,6 @@ pub trait AppCircuit {

Ok(calldata)
}

/// Same as [`AppCircuit::create_circuit`] but with a mock circuit.
fn mock_circuit(
stage: CircuitBuilderStage,
pinning: Option<Self::Pinning>,
witness: &Self::Witness,
k: u32,
) -> Result<impl crate::util::PinnableCircuit<Fr>, Error> {
let params = gen_srs(k);
Self::create_circuit(stage, pinning, witness, &params)
}
}

pub fn custom_gen_evm_verifier_shplonk<C: CircuitExt<Fr>>(
Expand Down
14 changes: 10 additions & 4 deletions lightclient-circuits/src/witness/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,27 @@ mod tests {
use crate::{committee_update_circuit::CommitteeUpdateCircuit, util::AppCircuit};
use eth_types::Testnet;
use halo2_base::{
gates::circuit::CircuitBuilderStage, halo2_proofs::dev::MockProver,
halo2_proofs::halo2curves::bn256::Fr,
gates::circuit::CircuitBuilderStage,
halo2_proofs::dev::MockProver,
halo2_proofs::{
halo2curves::bn256::{Bn256, Fr},
poly::kzg::commitment::ParamsKZG,
},
utils::fs::gen_srs,
};
use snark_verifier_sdk::CircuitExt;

#[test]
fn test_committee_update_default_witness() {
const K: u32 = 18;
let witness = CommitteeUpdateArgs::<Testnet>::default();
let params: ParamsKZG<Bn256> = gen_srs(K);

let circuit = CommitteeUpdateCircuit::<Testnet, Fr>::mock_circuit(
let circuit = CommitteeUpdateCircuit::<Testnet, Fr>::create_circuit(
CircuitBuilderStage::Mock,
None,
&witness,
K,
&params,
)
.unwrap();

Expand Down
17 changes: 13 additions & 4 deletions lightclient-circuits/src/witness/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,28 @@ mod tests {
use crate::{sync_step_circuit::StepCircuit, util::AppCircuit};
use eth_types::Testnet;
use halo2_base::{
gates::circuit::CircuitBuilderStage, halo2_proofs::dev::MockProver,
gates::circuit::CircuitBuilderStage,
halo2_proofs::halo2curves::bn256::Fr,
halo2_proofs::{
dev::MockProver, halo2curves::bn256::Bn256, poly::kzg::commitment::ParamsKZG,
},
utils::fs::gen_srs,
};
use snark_verifier_sdk::CircuitExt;

#[test]
fn test_step_default_witness() {
const K: u32 = 20;
let witness = SyncStepArgs::<Testnet>::default();
let params: ParamsKZG<Bn256> = gen_srs(K);

let circuit =
StepCircuit::<Testnet, Fr>::mock_circuit(CircuitBuilderStage::Mock, None, &witness, K)
.unwrap();
let circuit = StepCircuit::<Testnet, Fr>::create_circuit(
CircuitBuilderStage::Mock,
None,
&witness,
&params,
)
.unwrap();

let prover = MockProver::<Fr>::run(K, &circuit, circuit.instances()).unwrap();
prover.assert_satisfied_par();
Expand Down
4 changes: 2 additions & 2 deletions lightclient-circuits/tests/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ fn test_eth2_spec_evm_verify(

let pinning = Eth2ConfigPinning::from_path("./config/sync_step_21.json");

let circuit = StepCircuit::<Minimal, bn256::Fr>::mock_circuit(
let circuit = StepCircuit::<Minimal, bn256::Fr>::create_circuit(
CircuitBuilderStage::Prover,
Some(pinning),
&witness,
K,
&params,
)
.unwrap();

Expand Down
27 changes: 18 additions & 9 deletions preprocessor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ where
[(); S::SYNC_COMMITTEE_DEPTH]:,
[(); S::FINALIZED_HEADER_INDEX]:,
{
let route = format!("eth/v1/beacon/light_client/updates");
let route = "eth/v1/beacon/light_client/updates";
let mut updates: Vec<VersionedValue<_>> = client
.http
.get(client.endpoint.join(&route)?)
.get(client.endpoint.join(route)?)
.query(&[("start_period", period), ("count", 1)])
.send()
.await?
Expand Down Expand Up @@ -176,6 +176,9 @@ mod tests {
use eth_types::Testnet;
use ethereum_consensus_types::signing::{compute_domain, DomainType};
use ethereum_consensus_types::ForkData;
use halo2_base::halo2_proofs::halo2curves::bn256::Bn256;
use halo2_base::halo2_proofs::poly::kzg::commitment::ParamsKZG;
use halo2_base::utils::fs::gen_srs;
use lightclient_circuits::committee_update_circuit::CommitteeUpdateCircuit;
use lightclient_circuits::halo2_proofs::{dev::MockProver, halo2curves::bn256::Fr};
use lightclient_circuits::util::{Eth2ConfigPinning, Halo2ConfigPinning};
Expand Down Expand Up @@ -206,7 +209,7 @@ mod tests {

// Fetch light client update and create circuit arguments
let (s, mut c) = {
let mut update = get_light_client_update_at_period(&client, period)
let update = get_light_client_update_at_period(&client, period)
.await
.unwrap();

Expand Down Expand Up @@ -236,7 +239,7 @@ mod tests {
fork_version,
};
let domain = compute_domain(DomainType::SyncCommittee, &fork_data).unwrap();
light_client_update_to_args::<Testnet>(&mut update, pubkeys_compressed, domain)
light_client_update_to_args::<Testnet>(&update, pubkeys_compressed, domain)
.await
.unwrap()
};
Expand All @@ -263,21 +266,27 @@ mod tests {
// Replaces the attested header with step circuits finalized header
c.finalized_header = s.finalized_header.clone();

let circuit =
StepCircuit::<Testnet, Fr>::mock_circuit(CircuitBuilderStage::Mock, None, &s, K)
.unwrap();
let params: ParamsKZG<Bn256> = gen_srs(K);

let circuit = StepCircuit::<Testnet, Fr>::create_circuit(
CircuitBuilderStage::Mock,
None,
&s,
&params,
)
.unwrap();

let prover = MockProver::<Fr>::run(K, &circuit, circuit.instances()).unwrap();
prover.assert_satisfied_par();

const CONFIG_PATH: &str = "../lightclient-circuits/config/committee_update_testnet.json";

let pinning = Eth2ConfigPinning::from_path(CONFIG_PATH);
let circuit = CommitteeUpdateCircuit::<Testnet, Fr>::mock_circuit(
let circuit = CommitteeUpdateCircuit::<Testnet, Fr>::create_circuit(
CircuitBuilderStage::Mock,
Some(pinning),
&c,
K,
&params,
)
.unwrap();

Expand Down
Loading

0 comments on commit ea1cb85

Please sign in to comment.