From 850a9b7b0573095da26834bdb09cbf716a727854 Mon Sep 17 00:00:00 2001 From: dmpierre Date: Fri, 22 Mar 2024 15:50:35 +0100 Subject: [PATCH] chore: update sh script and number of blocks being proven --- Cargo.lock | 2 +- setup-machine-and-run-proving.sh | 8 ++ setup-machine.sh | 6 - src/main.rs | 190 ++++++++++++++++--------------- 4 files changed, 107 insertions(+), 99 deletions(-) create mode 100755 setup-machine-and-run-proving.sh delete mode 100755 setup-machine.sh diff --git a/Cargo.lock b/Cargo.lock index 312b6aa..d2369fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -229,7 +229,7 @@ dependencies = [ [[package]] name = "ark-light-bitcoin-client" version = "0.1.0" -source = "git+https://github.com/dmpierre/ark-light-bitcoin-client.git#1a089539ccf92e80a3a97824bbbc11f8fe5eb2b7" +source = "git+https://github.com/dmpierre/ark-light-bitcoin-client.git#d43d366f8553c8cf0d8fd3c1a08890128d7b55ce" dependencies = [ "ark-bn254", "ark-crypto-primitives", diff --git a/setup-machine-and-run-proving.sh b/setup-machine-and-run-proving.sh new file mode 100755 index 0000000..5004332 --- /dev/null +++ b/setup-machine-and-run-proving.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# run with `source ./setup-machine-and-run-proving.sh` +sudo apt-get update +sudo apt install build-essential -y +curl https://sh.rustup.rs -sSf | sh -s -- -y +. "$HOME/.cargo/env" +git clone https://github.com/dmpierre/folding-schemes-light-btc.git +cd folding-schemes-light-btc && cargo run -r \ No newline at end of file diff --git a/setup-machine.sh b/setup-machine.sh deleted file mode 100755 index bdaedce..0000000 --- a/setup-machine.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -# script for setting up a new machine to generate light client btc proofs -sudo apt-get update -curl https://sh.rustup.rs -sSf | sh -s -- -y # install rust, defaulting to yes -. "$HOME/.cargo/env" - diff --git a/src/main.rs b/src/main.rs index 2ad7cf7..09505b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ use folding_schemes::{ use folding_schemes::{folding::nova::decider_eth_circuit::DeciderEthCircuit, FoldingScheme}; use num_bigint::BigUint; use num_traits::Num; -use std::marker::PhantomData; +use std::{marker::PhantomData, time::Instant}; use utils::setup; mod utils; @@ -78,99 +78,105 @@ impl FCircuit for BTCBlockCheckerFCircuit { } } -fn main() {} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn run_main() { - // this is done to avoid computing large circuits - let file = include_str!("./data/btc-blocks.json"); - let (mut prev_block_hash, blocks) = read_blocks(20, 1, file); - - let mut blocks_prepared = vec![]; - for batch in blocks.iter() { - let block_hashes = - serde_json::from_value::>(batch.get("blockHashes").unwrap().clone()) - .unwrap(); - let block_headers = - serde_json::from_value::>>(batch.get("blockHeaders").unwrap().clone()) - .unwrap(); - for (i, (block_hash, block_header)) in - block_hashes.iter().zip(block_headers).enumerate() - { - let block = Block { - block_header, - block_hash: block_hash.to_string(), - prev_block_hash, - }; - blocks_prepared.push(block.clone()); - prev_block_hash = block_hash.to_string(); - } +fn main() { + let file = include_str!("./data/btc-blocks.json"); + let (mut prev_block_hash, blocks) = read_blocks(100, 1, file); + + let mut blocks_prepared = vec![]; + for batch in blocks.iter() { + let block_hashes = + serde_json::from_value::>(batch.get("blockHashes").unwrap().clone()) + .unwrap(); + let block_headers = + serde_json::from_value::>>(batch.get("blockHeaders").unwrap().clone()) + .unwrap(); + for (block_hash, block_header) in block_hashes.iter().zip(block_headers) { + let block = Block { + block_header, + block_hash: block_hash.to_string(), + prev_block_hash, + }; + blocks_prepared.push(block.clone()); + prev_block_hash = block_hash.to_string(); } + } - type NOVA = Nova< - Projective, - GVar, - Projective2, - GVar2, - BTCBlockCheckerFCircuit, - KZG<'static, Bn254>, - Pedersen, - >; - type DECIDER = Decider< - Projective, - GVar, - Projective2, - GVar2, - BTCBlockCheckerFCircuit, - KZG<'static, Bn254>, - Pedersen, - Groth16, // here we define the Snark to use in the decider - NOVA, // here we define the FoldingScheme to use - >; - - let n_blocks_checked = blocks_prepared.len(); - let circuit = BTCBlockCheckerFCircuit::::new(blocks_prepared.clone()); - let (prover_params, poseidon_config, kzg_vk) = setup(circuit.clone()); - let z_0 = vec![Fr::from(0)]; - let mut nova = NOVA::init(&prover_params, circuit, z_0.clone()).unwrap(); - - for _ in 0..n_blocks_checked { - nova.prove_step().unwrap(); - let current_state = nova.z_i[0].into_bigint(); - println!("Checked block: {}", current_state); + type NOVA = Nova< + Projective, + GVar, + Projective2, + GVar2, + BTCBlockCheckerFCircuit, + KZG<'static, Bn254>, + Pedersen, + >; + + type DECIDER = Decider< + Projective, + GVar, + Projective2, + GVar2, + BTCBlockCheckerFCircuit, + KZG<'static, Bn254>, + Pedersen, + Groth16, + NOVA, + >; + + let n_blocks_checked = blocks_prepared.len(); + let circuit = BTCBlockCheckerFCircuit::::new(blocks_prepared.clone()); + let (prover_params, poseidon_config, kzg_vk) = setup(circuit.clone()); + let z_0 = vec![Fr::from(0)]; + let mut nova = NOVA::init(&prover_params, circuit, z_0.clone()).unwrap(); + + println!("Computing folds..."); + let now = Instant::now(); + for i in 0..n_blocks_checked { + let current_state = nova.z_i[0].into_bigint(); + if i % 10 == 0 { + println!("--- At block: {}/{} ---", current_state, n_blocks_checked); } - - let circuit = DeciderEthCircuit::< - Projective, - GVar, - Projective2, - GVar2, - KZG, - Pedersen, - >::from_nova::>(nova.clone()) - .unwrap(); - let mut rng = rand::rngs::OsRng; - - println!("Starting setup..."); - let (g16_pk, g16_vk) = - Groth16::::circuit_specific_setup(circuit.clone(), &mut rng).unwrap(); - - // decider proof generation - println!("Generating proof..."); - let decider_pp = (poseidon_config.clone(), g16_pk, prover_params.cs_params); - let proof = DECIDER::prove(decider_pp, rng, nova.clone()).unwrap(); - - // decider proof verification - println!("Verifying proof..."); - let decider_vp = (poseidon_config, g16_vk, kzg_vk); - let verified = DECIDER::verify( - decider_vp, nova.i, nova.z_0, nova.z_i, &nova.U_i, &nova.u_i, proof, - ) - .unwrap(); - assert!(verified); + nova.prove_step().unwrap(); } + let elapsed = now.elapsed(); + println!( + "Done folding. Checked {} blocks in: {:.2?}", + n_blocks_checked, elapsed + ); + + let circuit = DeciderEthCircuit::< + Projective, + GVar, + Projective2, + GVar2, + KZG, + Pedersen, + >::from_nova::>(nova.clone()) + .unwrap(); + let mut rng = rand::rngs::OsRng; + + // decider setup + println!("Starting setup..."); + let now = Instant::now(); + let (g16_pk, g16_vk) = + Groth16::::circuit_specific_setup(circuit.clone(), &mut rng).unwrap(); + let elapsed = now.elapsed(); + println!("Setup done in: {:.2?}", elapsed); + + // decider proof generation + println!("Generating proof..."); + let now = Instant::now(); + let decider_pp = (poseidon_config.clone(), g16_pk, prover_params.cs_params); + let proof = DECIDER::prove(decider_pp, rng, nova.clone()).unwrap(); + let elapsed = now.elapsed(); + println!("Proof generated in: {:.2?}", elapsed); + + // decider proof verification + println!("Verifying proof..."); + let decider_vp = (poseidon_config, g16_vk, kzg_vk); + let verified = DECIDER::verify( + decider_vp, nova.i, nova.z_0, nova.z_i, &nova.U_i, &nova.u_i, proof, + ) + .unwrap(); + assert!(verified); }