From 5c091dfc4a8cc67c03230c83a16aa5df8274eefe Mon Sep 17 00:00:00 2001 From: arnaucube Date: Thu, 2 May 2024 09:53:37 +0200 Subject: [PATCH] half-migrate to last Sonobe version whith 'external_inputs' at each step --- .gitignore | 3 +++ Cargo.toml | 11 ++++++----- src/main.rs | 47 +++++++++++++++++++++++++++++------------------ 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 96ef6c0..18a0763 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ /target Cargo.lock +generated +*.calldata +*.inputs diff --git a/Cargo.toml b/Cargo.toml index 3eb7891..7a40f3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,10 +6,11 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -ark-r1cs-std = { git = "https://github.com/arnaucube/ark-r1cs-std-cherry-picked/" } -folding-schemes = { git = "https://github.com/privacy-scaling-explorations/sonobe.git", package="folding-schemes", branch="feature/solidity-decider-verifier", features=["light-test"] } -solidity-verifiers = { git = "https://github.com/privacy-scaling-explorations/sonobe.git", package="solidity-verifiers", branch="feature/solidity-decider-verifier" } -ark-light-bitcoin-client = { git = "https://github.com/dmpierre/ark-light-bitcoin-client.git" } +ark-r1cs-std = { git = "https://github.com/winderica/r1cs-std", branch="cherry-pick" } +folding-schemes = { git = "https://github.com/privacy-scaling-explorations/sonobe.git", package="folding-schemes", branch="circom-external-inputs", features=["light-test"] } +solidity-verifiers = { git = "https://github.com/privacy-scaling-explorations/sonobe.git", package="solidity-verifiers", branch="circom-external-inputs" } +# ark-light-bitcoin-client = { git = "https://github.com/dmpierre/ark-light-bitcoin-client.git" } +ark-light-bitcoin-client = { path = "../ark-light-bitcoin-client" } # WIP ark-ff = "0.4.0" ark-relations = "0.4.0" serde_json = "1.0.114" @@ -26,6 +27,6 @@ ark-poly-commit = "0.4.0" ark-groth16 = "0.4.0" [patch.crates-io] -ark-r1cs-std = { git = "https://github.com/arnaucube/ark-r1cs-std-cherry-picked/" } +ark-r1cs-std = { git = "https://github.com/winderica/r1cs-std", branch="cherry-pick" } ark-bn254 = { git = "https://github.com/arnaucube/ark-curves-cherry-picked", branch="cherry-pick"} ark-grumpkin = { git = "https://github.com/arnaucube/ark-curves-cherry-picked", branch="cherry-pick"} diff --git a/src/main.rs b/src/main.rs index 6b35dd5..6071473 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ use folding_schemes::{ commitment::{kzg::KZG, pedersen::Pedersen}, folding::nova::{decider_eth::Decider, Nova}, frontend::FCircuit, + Error, }; use folding_schemes::{folding::nova::decider_eth::prepare_calldata, Decider as DeciderTrait}; use folding_schemes::{folding::nova::decider_eth_circuit::DeciderEthCircuit, FoldingScheme}; @@ -25,7 +26,7 @@ use solidity_verifiers::{ get_decider_template_for_cyclefold_decider, utils::get_function_selector_for_nova_cyclefold_verifier, }; -use solidity_verifiers::{Groth16Data, KzgData, NovaCycleFoldData}; +use solidity_verifiers::{utils::get_formatted_calldata, NovaCycleFoldVerifierKey}; use std::{fs, marker::PhantomData, time::Instant}; use utils::setup; mod utils; @@ -33,26 +34,35 @@ mod utils; #[derive(Clone, Debug)] pub struct BTCBlockCheckerFCircuit { _f: PhantomData, - block: Vec, } impl FCircuit for BTCBlockCheckerFCircuit { - type Params = Vec; + type Params = (); - fn new(params: Self::Params) -> Self { - Self { - _f: PhantomData, - block: params, - } + fn new(params: Self::Params) -> Result { + Ok(Self { _f: PhantomData }) } fn state_len(&self) -> usize { 1 } + fn external_inputs_len(&self) -> usize { + 1 // TODO set the number of the external_inputs + } - fn step_native(&self, i: usize, z_i: Vec) -> Result, folding_schemes::Error> { + fn step_native( + &self, + i: usize, + z_i: Vec, + external_inputs: Vec, + ) -> Result, folding_schemes::Error> { let new_z_i = vec![z_i[0] + F::one()]; + // TODO update internal logic to instead of using `self.block[i]` to use `external_inputs` + // ie.: + // let block_header = external_inputs[0]; + // let block_hash = external_inputs[1]; + // Check block hash let computed_block_hash = get_block_hash(&self.block[i].block_header); assert_eq!(computed_block_hash, self.block[i].block_hash); @@ -75,8 +85,11 @@ impl FCircuit for BTCBlockCheckerFCircuit { &self, cs: ConstraintSystemRef, i: usize, - z_i: Vec>, - ) -> Result>, SynthesisError> { + z_i: Vec>, + external_inputs: Vec>, + ) -> Result>, SynthesisError> { + // TODO update internal logic to instead of using `self.block[i]` to use `external_inputs` + let new_z_i = vec![z_i[0].clone() + FpVar::new_constant(cs.clone(), F::one())?]; let block_var = BlockVar::new_witness(cs.clone(), || Ok(self.block[i].clone()))?; let _ = BTCBlockCheckerGadget::check_block(cs.clone(), block_var.clone())?; @@ -130,7 +143,7 @@ fn main() { >; let n_blocks_checked = blocks_prepared.len() / 20; // TMP rm '/20' - let circuit = BTCBlockCheckerFCircuit::::new(blocks_prepared.clone()); + let circuit = BTCBlockCheckerFCircuit::::new(()).unwrap(); let (fs_prover_params, kzg_vk) = setup(circuit.clone()); let z_0 = vec![Fr::from(0)]; let mut nova = NOVA::init(&fs_prover_params, circuit, z_0.clone()).unwrap(); @@ -142,6 +155,7 @@ fn main() { if i % 10 == 0 { println!("--- At block: {}/{} ---", current_state, n_blocks_checked); } + // TODO feed the external_inputs (from blocks_prepared) into prove_step nova.prove_step().unwrap(); } println!( @@ -189,12 +203,7 @@ fn main() { .unwrap(); assert!(verified); - let g16_data = Groth16Data::from(g16_vk); - let kzg_data = KzgData::from(( - kzg_vk, - fs_prover_params.cs_params.powers_of_g[0..3].to_vec(), - )); - let nova_cyclefold_data = NovaCycleFoldData::from((g16_data, kzg_data, nova.z_0.len())); + let nova_cyclefold_data = NovaCycleFoldVerifierKey::from((g16_vk, kzg_vk, nova.z_0.len())); let function_selector = get_function_selector_for_nova_cyclefold_verifier(nova.z_0.len() * 2 + 1); @@ -215,6 +224,8 @@ fn main() { save_solidity("./NovaLightBTCClientDecider.sol", &decider_template.clone()); // save calldata fs::write("./solidity-calldata.calldata", calldata.clone()).unwrap(); + let s = get_formatted_calldata(calldata.clone()); + fs::write("./solidity-calldata.inputs", s.join(",\n")).expect(""); let nova_cyclefold_verifier_bytecode = compile_solidity(decider_template, "NovaDecider");