Skip to content

Commit

Permalink
Merge branch 'main' into timoftime/docs_and_license
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltea committed Dec 14, 2023
2 parents 2bd19b6 + 7fb8fb5 commit 684dbed
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 149 deletions.
92 changes: 9 additions & 83 deletions prover/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,23 @@ use lightclient_circuits::{
util::{gen_srs, AppCircuit},
};
use preprocessor::{rotation_args_from_update, step_args_from_finality_update};
use snark_verifier_sdk::{evm::evm_verify, halo2::aggregation::AggregationCircuit, Snark};
use std::path::{Path, PathBuf};
use snark_verifier_sdk::{halo2::aggregation::AggregationCircuit, Snark};
use std::path::PathBuf;
use std::sync::Arc;

pub type JsonRpcServerState = Arc<JsonRpcServer<JsonRpcMapRouter>>;

use crate::rpc_api::{
CommitteeUpdateEvmProofResult, GenProofRotationWithWitnessParams,
GenProofStepWithWitnessParams, SyncStepCompressedEvmProofResult, SyncStepEvmProofResult,
RPC_EVM_PROOF_ROTATION_CIRCUIT, RPC_EVM_PROOF_STEP_CIRCUIT,
RPC_EVM_PROOF_STEP_CIRCUIT_COMPRESSED,
CommitteeUpdateEvmProofResult, SyncStepCompressedEvmProofResult,
RPC_EVM_PROOF_COMMITTEE_UPDATE_CIRCUIT_COMPRESSED, RPC_EVM_PROOF_STEP_CIRCUIT_COMPRESSED, GenProofCommitteeUpdateParams, GenProofStepParams,
};

pub(crate) fn jsonrpc_server() -> JsonRpcServer<JsonRpcMapRouter> {
JsonRpcServer::new()
.with_method(
RPC_EVM_PROOF_ROTATION_CIRCUIT,
RPC_EVM_PROOF_COMMITTEE_UPDATE_CIRCUIT_COMPRESSED,
gen_evm_proof_committee_update_handler,
)
.with_method(RPC_EVM_PROOF_STEP_CIRCUIT, gen_evm_proof_sync_step_handler)
.with_method(
RPC_EVM_PROOF_STEP_CIRCUIT_COMPRESSED,
gen_evm_proof_sync_step_compressed_handler,
Expand All @@ -40,9 +37,9 @@ pub(crate) fn jsonrpc_server() -> JsonRpcServer<JsonRpcMapRouter> {
}

pub(crate) async fn gen_evm_proof_committee_update_handler(
Params(params): Params<GenProofRotationWithWitnessParams>,
Params(params): Params<GenProofCommitteeUpdateParams>,
) -> Result<CommitteeUpdateEvmProofResult, JsonRpcError> {
let GenProofRotationWithWitnessParams {
let GenProofCommitteeUpdateParams {
spec,
light_client_update,
} = params;
Expand Down Expand Up @@ -117,59 +114,10 @@ pub(crate) async fn gen_evm_proof_committee_update_handler(
})
}

pub(crate) async fn gen_evm_proof_sync_step_handler(
Params(params): Params<GenProofStepWithWitnessParams>,
) -> Result<SyncStepEvmProofResult, JsonRpcError> {
let GenProofStepWithWitnessParams {
spec,
light_client_finality_update,
domain,
pubkeys,
} = params;

let (proof, instances) = match spec {
Spec::Testnet => {
let update = ssz_rs::deserialize(&light_client_finality_update)?;
let pubkeys = ssz_rs::deserialize(&pubkeys)?;
let witness = step_args_from_finality_update(update, pubkeys, domain).await?;

gen_evm_proof::<StepCircuit<eth_types::Testnet, Fr>>(
PathBuf::from("./build/sync_step_testnet.pkey"),
PathBuf::from("./lightclient-circuits/config/sync_step_testnet.json"),
witness,
None::<PathBuf>,
)?
}
Spec::Mainnet => {
let update = ssz_rs::deserialize(&light_client_finality_update)?;
let pubkeys = ssz_rs::deserialize(&pubkeys)?;
let witness = step_args_from_finality_update(update, pubkeys, domain).await?;

gen_evm_proof::<StepCircuit<eth_types::Mainnet, Fr>>(
PathBuf::from("./build/sync_step_mainnet.pkey"),
PathBuf::from("./lightclient-circuits/config/sync_step_mainnet.json"),
witness,
None::<PathBuf>,
)?
}
Spec::Minimal => return Err(JsonRpcError::internal("Minimal spec not supported in RPC")),
};

let public_inputs = instances[0]
.iter()
.map(|pi| U256::from_little_endian(&pi.to_bytes()))
.collect();

Ok(SyncStepEvmProofResult {
proof,
public_inputs,
})
}

pub(crate) async fn gen_evm_proof_sync_step_compressed_handler(
Params(params): Params<GenProofStepWithWitnessParams>,
Params(params): Params<GenProofStepParams>,
) -> Result<SyncStepCompressedEvmProofResult, JsonRpcError> {
let GenProofStepWithWitnessParams {
let GenProofStepParams {
spec,
light_client_finality_update,
domain,
Expand Down Expand Up @@ -262,28 +210,6 @@ where
)?)
}

fn gen_evm_proof<C: AppCircuit>(
pk_path: impl AsRef<Path>,
config_path: PathBuf,
witness: C::Witness,
yul_path_if_verify: Option<impl AsRef<Path>>,
) -> eyre::Result<(Vec<u8>, Vec<Vec<Fr>>)> {
let k = C::get_degree(&config_path);
let params = gen_srs(k);

let pk = C::read_pk(&params, pk_path, &witness);

let (proof, instances) = C::gen_evm_proof_shplonk(&params, &pk, &config_path, None, &witness)
.map_err(|e| eyre::eyre!("Failed to generate calldata: {}", e))?;

if let Some(deployment_code_path) = yul_path_if_verify {
let deployment_code =
C::gen_evm_verifier_shplonk(&params, &pk, Some(deployment_code_path), &witness)?;
evm_verify(deployment_code, instances.clone(), proof.clone());
}
Ok((proof, instances))
}

pub async fn run_rpc(port: usize) -> Result<(), eyre::Error> {
let tcp_listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port)).await?;
let rpc_server = Arc::new(jsonrpc_server());
Expand Down
29 changes: 5 additions & 24 deletions prover/src/rpc_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,14 @@ use crate::args;
use primitive_types::U256;
use serde::{Deserialize, Serialize};

pub const RPC_EVM_PROOF_STEP_CIRCUIT: &str = "genEvmProof_SyncStep";
pub const RPC_EVM_PROOF_STEP_CIRCUIT_COMPRESSED: &str = "genEvmProof_SyncStepCompressed";
pub const RPC_EVM_PROOF_ROTATION_CIRCUIT: &str = "genEvmProof_CommitteeUpdate";

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenProofRotationParams {
pub spec: args::Spec,

#[serde(default = "default_beacon_api")]
pub beacon_api: String,
}
pub const RPC_EVM_PROOF_COMMITTEE_UPDATE_CIRCUIT_COMPRESSED: &str =
"genEvmProof_CommitteeUpdateCompressed";

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenProofStepParams {
pub spec: args::Spec,

#[serde(default = "default_beacon_api")]
pub beacon_api: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenProofStepWithWitnessParams {
pub spec: args::Spec,

// Serializing as Vec<u8> so that we can differentiate between Mainnet, Testnet, Minimal at runtime
pub light_client_finality_update: Vec<u8>,
pub pubkeys: Vec<u8>,
Expand All @@ -34,16 +18,17 @@ pub struct GenProofStepWithWitnessParams {
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenProofRotationWithWitnessParams {
pub struct GenProofCommitteeUpdateParams {
pub spec: args::Spec,

// Serializing as Vec<u8> so that we can differentiate between Mainnet, Testnet, Minimal at runtime
pub light_client_update: Vec<u8>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncStepEvmProofResult {
pub struct SyncStepCompressedEvmProofResult {
pub proof: Vec<u8>,
pub accumulator: [U256; 12],
pub public_inputs: Vec<U256>,
}

Expand Down Expand Up @@ -71,7 +56,3 @@ pub struct SyncCommitteePoseidonParams {
pub struct SyncCommitteePoseidonResult {
pub commitment: [u8; 32],
}

fn default_beacon_api() -> String {
String::from("http://127.0.0.1:5052")
}
50 changes: 8 additions & 42 deletions prover/src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
use url::Url;

use crate::rpc_api::{
CommitteeUpdateEvmProofResult, GenProofRotationParams, GenProofStepParams,
SyncStepEvmProofResult, RPC_EVM_PROOF_ROTATION_CIRCUIT, RPC_EVM_PROOF_STEP_CIRCUIT,
CommitteeUpdateEvmProofResult, GenProofStepParams, SyncStepCompressedEvmProofResult,
RPC_EVM_PROOF_COMMITTEE_UPDATE_CIRCUIT_COMPRESSED, RPC_EVM_PROOF_STEP_CIRCUIT_COMPRESSED, GenProofCommitteeUpdateParams,
};

/// Error object in a response
Expand Down Expand Up @@ -47,17 +47,19 @@ impl Client {
/// Generates a proof along with instance values for committee Rotation circuit
pub async fn gen_evm_proof_committee_update(
&self,
params: GenProofRotationParams,
params: GenProofCommitteeUpdateParams,
) -> Result<CommitteeUpdateEvmProofResult, Error> {
self.call(RPC_EVM_PROOF_ROTATION_CIRCUIT, params).await
self.call(RPC_EVM_PROOF_COMMITTEE_UPDATE_CIRCUIT_COMPRESSED, params)
.await
}

/// Generates a proof along with instance values for Step circuit
pub async fn gen_evm_proof_step(
&self,
params: GenProofStepParams,
) -> Result<SyncStepEvmProofResult, Error> {
self.call(RPC_EVM_PROOF_STEP_CIRCUIT, params).await
) -> Result<SyncStepCompressedEvmProofResult, Error> {
self.call(RPC_EVM_PROOF_STEP_CIRCUIT_COMPRESSED, params)
.await
}

/// Utility method for sending RPC requests over HTTP
Expand Down Expand Up @@ -85,39 +87,3 @@ impl Client {
}
}
}

#[cfg(test)]
mod test {
use super::*;
use crate::args;
use jsonrpc_v2::Error;

#[tokio::test]
#[ignore = "requires a running prover"]
async fn test_rpc_client() {
let client = Client::new("http://localhost:3000/rpc");

let p = GenProofStepParams {
spec: args::Spec::Testnet,
beacon_api: String::from("https://lodestar-sepolia.chainsafe.io"),
};

let r = client.gen_evm_proof_step(p).await;

match r {
Ok(r) => {
println!("res: {:?}", r);
}
Err(Error::Full {
data: _,
code,
message,
}) => {
println!("Error: {}, Code: {}", message, code);
}
Err(Error::Provided { code, message }) => {
println!("Error: {}, Code: {}", message, code);
}
}
}
}

0 comments on commit 684dbed

Please sign in to comment.