Skip to content

Commit

Permalink
Proving key pre-loading (#51)
Browse files Browse the repository at this point in the history
* switch to per spec RPC server

* fix verifier circuits keys loading
  • Loading branch information
nulltea authored Jan 8, 2024
1 parent d354dd0 commit 2102a86
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 143 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,16 @@ pasta_curves = "0.5.1"
ff = "0.13"
sha2 = { version = "0.9", features = ["compress"] }
uint = "0.9.1"
ark-std = { version = "0.4.0", features = ["print-trace"] }

# misc
itertools = "0.12.0"
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.78"
getset = "0.1.2"
log = "0.4.14"
hex = "0.4"
ark-std = { version = "0.4.0", features = ["print-trace"] }


[patch.crates-io]
halo2curves = { git = "https://github.com/timoftime/halo2curves", package = "halo2curves-axiom", rev = "1bd39b8" }
Expand Down
4 changes: 4 additions & 0 deletions eth-types/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use core::fmt::Debug;

/// Beacon chain specification.
pub trait Spec: 'static + Sized + Copy + Default + Debug {
const NAME: &'static str;
const SYNC_COMMITTEE_SIZE: usize;
const SYNC_COMMITTEE_ROOT_INDEX: usize;
const SYNC_COMMITTEE_DEPTH: usize;
Expand All @@ -25,6 +26,7 @@ pub trait Spec: 'static + Sized + Copy + Default + Debug {
pub struct Minimal;

impl Spec for Minimal {
const NAME: &'static str = "minimal";
const SYNC_COMMITTEE_SIZE: usize = 32;
const SYNC_COMMITTEE_DEPTH: usize = 5;
const SYNC_COMMITTEE_ROOT_INDEX: usize = 55;
Expand All @@ -45,6 +47,7 @@ impl Spec for Minimal {
pub struct Testnet;

impl Spec for Testnet {
const NAME: &'static str = "testnet";
const SYNC_COMMITTEE_SIZE: usize = 512;
const SYNC_COMMITTEE_DEPTH: usize = 5;
const SYNC_COMMITTEE_ROOT_INDEX: usize = 55;
Expand All @@ -64,6 +67,7 @@ impl Spec for Testnet {
pub struct Mainnet;

impl Spec for Mainnet {
const NAME: &'static str = "mainnet";
const SYNC_COMMITTEE_SIZE: usize = 512;
const SYNC_COMMITTEE_DEPTH: usize = 5;
const SYNC_COMMITTEE_ROOT_INDEX: usize = 55;
Expand Down
10 changes: 4 additions & 6 deletions lightclient-circuits/tests/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,11 @@ fn run_test_eth2_spec_mock<const K_ROTATION: u32, const K_SYNC: u32>(path: PathB

let rotation_circuit = mock_committee_update_circuit(&rotation_witness, K_ROTATION, None);

let rotation_instance =
CommitteeUpdateCircuit::<Minimal, bn256::Fr>::get_instances(&rotation_witness, LIMB_BITS);
let timer = start_timer!(|| "committee_update mock prover run");
let prover = MockProver::<bn256::Fr>::run(
K_ROTATION,
&rotation_circuit,
CommitteeUpdateCircuit::<Minimal, bn256::Fr>::get_instances(&rotation_witness, LIMB_BITS),
)
.unwrap();
let prover =
MockProver::<bn256::Fr>::run(K_ROTATION, &rotation_circuit, rotation_instance).unwrap();
prover.assert_satisfied_par();
end_timer!(timer);

Expand Down
1 change: 1 addition & 0 deletions prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ ark-std.workspace = true
itertools.workspace = true
serde.workspace = true
serde_json.workspace = true
getset.workspace = true
log.workspace = true
url = "2"

Expand Down
8 changes: 8 additions & 0 deletions prover/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ pub enum BaseCmd {
/// Port for RPC server to listen on
#[clap(long, short, default_value = "3000")]
port: String,

/// Network specification [mainnet, testnet]
#[clap(long, short, default_value = "mainnet")]
spec: Spec,

/// Path to directory with circuit artifacts
#[clap(long, short, default_value = "./build")]
build_dir: PathBuf,
},
/// Circuit related commands.
Circuit {
Expand Down
2 changes: 2 additions & 0 deletions prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
pub mod prover;

pub mod rpc_api;
pub mod rpc_client;

Expand Down
26 changes: 24 additions & 2 deletions prover/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,30 @@ mod args;

async fn app(options: Cli) -> eyre::Result<()> {
match options.subcommand {
args::BaseCmd::Rpc { port } => {
run_rpc(port.parse().unwrap()).await?;
args::BaseCmd::Rpc {
port,
spec,
build_dir,
} => {
match spec {
args::Spec::Testnet => {
run_rpc::<eth_types::Testnet>(
port.parse().unwrap(),
options.args.config_dir,
build_dir,
)
.await
}
args::Spec::Mainnet => {
run_rpc::<eth_types::Mainnet>(
port.parse().unwrap(),
options.args.config_dir,
build_dir,
)
.await
}
args::Spec::Minimal => Err(eyre::eyre!("Minimal spec is not supported for RPC")),
}?;

log::info!("Stopped accepting RPC connections");

Expand Down
114 changes: 114 additions & 0 deletions prover/src/prover.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// The Licensed Work is (c) 2023 ChainSafe
// Code: https://github.com/ChainSafe/Spectre
// SPDX-License-Identifier: LGPL-3.0-only

use eth_types::Spec;
use getset::Getters;
use lightclient_circuits::committee_update_circuit::CommitteeUpdateCircuit;
use lightclient_circuits::halo2_base::utils::fs::gen_srs;
use lightclient_circuits::halo2_proofs::{
halo2curves::bn256::{Bn256, Fr, G1Affine},
plonk::ProvingKey,
poly::kzg::commitment::ParamsKZG,
};
use lightclient_circuits::sync_step_circuit::StepCircuit;
use lightclient_circuits::util::AppCircuit;
use snark_verifier_sdk::halo2::aggregation::AggregationCircuit;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

#[derive(Clone, Debug, Getters)]
pub struct CircuitContext {
#[getset(get = "pub")]
pub config_path: PathBuf,
#[getset(get = "pub")]
pub degree: u32,
#[getset(get = "pub")]
pub pk: ProvingKey<G1Affine>,
}

#[derive(Clone, Debug)]
pub struct ProverState {
// degree -> params (use BTreeMap to find proper degree for params downsize)
pub params: BTreeMap<u32, ParamsKZG<Bn256>>,
pub step: CircuitContext,
pub step_verifier: CircuitContext,
pub committee_update: CircuitContext,
pub committee_update_verifier: CircuitContext,
}

impl ProverState {
pub fn new<S: Spec>(config_dir: &Path, build_dir: &Path) -> Self {
let mut params_map = BTreeMap::new();

fn load_ctx<Circuit: AppCircuit>(
config_path: PathBuf,
pk_path: PathBuf,
params_map: &mut BTreeMap<u32, ParamsKZG<Bn256>>,
default_witness: Circuit::Witness,
) -> CircuitContext {
let degree = Circuit::get_degree(&config_path);
let params = gen_srs(degree);

let pk = Circuit::read_pk(&params, pk_path, &config_path, &default_witness);

params_map.insert(degree, params);

CircuitContext {
config_path,
degree,
pk,
}
}

let step = load_ctx::<StepCircuit<S, Fr>>(
config_dir.join(format!("sync_step_{}.json", S::NAME)),
build_dir.join(format!("sync_step_{}.pkey", S::NAME)),
&mut params_map,
Default::default(),
);

let step_snark = StepCircuit::<S, Fr>::gen_snark_shplonk(
params_map.get(step.degree()).unwrap(),
step.pk(),
step.config_path(),
None::<String>,
&Default::default(),
)
.unwrap();

let committee_update = load_ctx::<CommitteeUpdateCircuit<S, Fr>>(
config_dir.join(format!("committee_update_{}.json", S::NAME)),
build_dir.join(format!("committee_update_{}.pkey", S::NAME)),
&mut params_map,
Default::default(),
);

let committee_update_snark = CommitteeUpdateCircuit::<S, Fr>::gen_snark_shplonk(
params_map.get(committee_update.degree()).unwrap(),
committee_update.pk(),
committee_update.config_path(),
None::<String>,
&Default::default(),
)
.unwrap();

Self {
step,
step_verifier: load_ctx::<AggregationCircuit>(
config_dir.join(format!("sync_step_verifier_{}.json", S::NAME)),
build_dir.join(format!("sync_step_verifier_{}.pkey", S::NAME)),
&mut params_map,
vec![step_snark],
),
committee_update,
committee_update_verifier: load_ctx::<AggregationCircuit>(
config_dir.join(format!("committee_update_verifier_{}.json", S::NAME)),
build_dir.join(format!("committee_update_verifier_{}.pkey", S::NAME)),
&mut params_map,
vec![committee_update_snark],
),
params: params_map,
}
}
}
Loading

0 comments on commit 2102a86

Please sign in to comment.