From 234de686ca6c05ddbcd93ab395816bcde5162302 Mon Sep 17 00:00:00 2001 From: saitima Date: Tue, 14 Jan 2025 19:21:28 +0300 Subject: [PATCH] apply missing pinned allocator to combined monomials --- crates/fflonk/src/allocator/pinned.rs | 1 + crates/fflonk/src/convenience.rs | 44 +++++++++---------- crates/fflonk/src/prover.rs | 4 +- crates/fflonk/src/setup.rs | 2 +- crates/fflonk/src/storage.rs | 5 ++- crates/fflonk/src/test.rs | 15 +++---- .../src/proof_system/fflonk.rs | 17 ++++--- .../src/proof_system/plonk.rs | 3 +- 8 files changed, 44 insertions(+), 47 deletions(-) diff --git a/crates/fflonk/src/allocator/pinned.rs b/crates/fflonk/src/allocator/pinned.rs index d6303b5..9e2a2ce 100644 --- a/crates/fflonk/src/allocator/pinned.rs +++ b/crates/fflonk/src/allocator/pinned.rs @@ -22,6 +22,7 @@ pub(crate) fn init_static_host_alloc(domain_size: usize) { return; } } + assert!(domain_size.is_power_of_two()); // Bitmap allocator with small block size and high number of allocations doesn't make // sense, and doesn't give good runtime performance compared to default allocator. // However it provides satisfying improvement for 3 combined monomials, since prover diff --git a/crates/fflonk/src/convenience.rs b/crates/fflonk/src/convenience.rs index 26f9ff3..1df9b20 100644 --- a/crates/fflonk/src/convenience.rs +++ b/crates/fflonk/src/convenience.rs @@ -13,7 +13,7 @@ use circuit_definitions::circuit_definitions::aux_layer::{ }; use fflonk::{FflonkAssembly, L1_VERIFIER_DOMAIN_SIZE_LOG}; -pub type FflonkSnarkVerifierCircuitDeviceSetup = +pub type FflonkSnarkVerifierCircuitDeviceSetup = FflonkDeviceSetup; use super::*; @@ -107,17 +107,18 @@ pub fn gpu_prove_fflonk_snark_verifier_circuit_single_shot( FflonkSnarkVerifierCircuitProof, FflonkSnarkVerifierCircuitVK, ) { - let mut assembly = FflonkAssembly::::new(); + let mut assembly = FflonkAssembly::::new(); circuit.synthesize(&mut assembly).expect("must work"); assert!(assembly.is_satisfied()); let raw_trace_len = assembly.n(); let domain_size = (raw_trace_len + 1).next_power_of_two(); + DeviceContextWithSingleDevice::init_pinned_memory(domain_size).unwrap(); let _context = DeviceContextWithSingleDevice::init(domain_size) .expect("Couldn't create fflonk GPU Context"); let setup = - FflonkDeviceSetup::<_, FflonkSnarkVerifierCircuit, GlobalStaticHost>::create_setup_from_assembly_on_device( - &assembly + FflonkDeviceSetup::<_, FflonkSnarkVerifierCircuit>::create_setup_from_assembly_on_device( + &assembly, ) .unwrap(); let vk = setup.get_verification_key(); @@ -128,14 +129,11 @@ pub fn gpu_prove_fflonk_snark_verifier_circuit_single_shot( assert!(domain_size <= 1 << L1_VERIFIER_DOMAIN_SIZE_LOG); let start = std::time::Instant::now(); - let proof = create_proof::< - _, - FflonkSnarkVerifierCircuit, - _, - RollingKeccakTranscript<_>, - CombinedMonomialDeviceStorage, - _, - >(&assembly, &setup, raw_trace_len) + let proof = create_proof::<_, FflonkSnarkVerifierCircuit, _, RollingKeccakTranscript<_>, _>( + &assembly, + &setup, + raw_trace_len, + ) .unwrap(); println!("proof generation takes {} ms", start.elapsed().as_millis()); @@ -151,7 +149,8 @@ pub fn gpu_prove_fflonk_snark_verifier_circuit_with_precomputation( vk: &FflonkSnarkVerifierCircuitVK, ) -> FflonkSnarkVerifierCircuitProof { println!("Synthesizing for fflonk proving"); - let mut proving_assembly = FflonkAssembly::::new(); + let mut proving_assembly = + FflonkAssembly::::new(); circuit .synthesize(&mut proving_assembly) .expect("must work"); @@ -163,14 +162,11 @@ pub fn gpu_prove_fflonk_snark_verifier_circuit_with_precomputation( assert!(domain_size <= 1 << L1_VERIFIER_DOMAIN_SIZE_LOG); let start = std::time::Instant::now(); - let proof = create_proof::< - _, - FflonkSnarkVerifierCircuit, - _, - RollingKeccakTranscript<_>, - CombinedMonomialDeviceStorage, - _, - >(&proving_assembly, setup, raw_trace_len) + let proof = create_proof::<_, FflonkSnarkVerifierCircuit, _, RollingKeccakTranscript<_>, _>( + &proving_assembly, + setup, + raw_trace_len, + ) .unwrap(); println!("proof generation takes {} ms", start.elapsed().as_millis()); @@ -189,8 +185,10 @@ pub fn precompute_and_save_setup_and_vk_for_fflonk_snark_circuit( println!("Generating fflonk setup data on the device"); let device_setup = - FflonkSnarkVerifierCircuitDeviceSetup::::create_setup_on_device(&circuit) - .unwrap(); + FflonkSnarkVerifierCircuitDeviceSetup::::create_setup_on_device( + &circuit, + ) + .unwrap(); let setup_file_path = format!("{}/final_snark_device_setup.bin", path); println!("Saving setup into file {setup_file_path}"); let device_setup_file = std::fs::File::create(&setup_file_path).unwrap(); diff --git a/crates/fflonk/src/prover.rs b/crates/fflonk/src/prover.rs index 4bfc35c..98bd204 100644 --- a/crates/fflonk/src/prover.rs +++ b/crates/fflonk/src/prover.rs @@ -12,7 +12,7 @@ use fflonk::{ commit_point_as_xy, compute_generators, horner_evaluation, FflonkAssembly, FflonkProof, }; -pub fn create_proof( +pub fn create_proof( assembly: &FflonkAssembly, setup: &FflonkDeviceSetup, raw_trace_len: usize, @@ -58,7 +58,7 @@ where let common_combined_degree = MAX_COMBINED_DEGREE_FACTOR * domain_size; let device = Device::model(); let mut combined_monomial_storage = - GenericCombinedStorage::::allocate_on(&device, domain_size)?; + GenericCombinedStorage::::allocate_on(&device, domain_size)?; let start = std::time::Instant::now(); let ([c1_commitment, c2_commitment], h_all_evaluations, h_aux_evaluations, challenges) = prove_arguments( diff --git a/crates/fflonk/src/setup.rs b/crates/fflonk/src/setup.rs index fb70564..340e6be 100644 --- a/crates/fflonk/src/setup.rs +++ b/crates/fflonk/src/setup.rs @@ -15,7 +15,7 @@ use super::*; use crate::HostAllocator; -pub struct FflonkDeviceSetup, A: HostAllocator = GlobalStaticHost> { +pub struct FflonkDeviceSetup, A: HostAllocator = std::alloc::Global> { pub main_gate_selector_monomials: [Vec; 5], pub variable_indexes: [Vec; 3], pub c0_commitment: E::G1Affine, diff --git a/crates/fflonk/src/storage.rs b/crates/fflonk/src/storage.rs index 573da02..7efd4ad 100644 --- a/crates/fflonk/src/storage.rs +++ b/crates/fflonk/src/storage.rs @@ -13,7 +13,7 @@ pub trait CombinedMonomialStorage { ) -> CudaResult<()>; } -pub enum GenericCombinedStorage +pub enum GenericCombinedStorage where F: PrimeField, A: HostAllocator, @@ -46,9 +46,10 @@ where } } -impl CombinedMonomialStorage for GenericCombinedStorage +impl CombinedMonomialStorage for GenericCombinedStorage where F: PrimeField, + A: HostAllocator, { type Poly = Poly; diff --git a/crates/fflonk/src/test.rs b/crates/fflonk/src/test.rs index 28eaae8..3590677 100644 --- a/crates/fflonk/src/test.rs +++ b/crates/fflonk/src/test.rs @@ -35,7 +35,7 @@ fn test_simple_circuit_with_naive_main_gate() { type C = FflonkTestCircuit; let circuit = C {}; - let mut assembly = FflonkAssembly::::new(); + let mut assembly = FflonkAssembly::::new(); circuit.synthesize(&mut assembly).expect("must work"); assert!(assembly.is_satisfied()); let raw_trace_len = assembly.n(); @@ -51,14 +51,11 @@ fn test_simple_circuit_with_naive_main_gate() { let vk = setup.get_verification_key(); assert_eq!(vk.n + 1, domain_size); - let proof = create_proof::< - _, - C, - _, - RollingKeccakTranscript, - CombinedMonomialDeviceStorage, - GlobalStaticHost, - >(&assembly, &setup, raw_trace_len) + let proof = create_proof::<_, C, _, RollingKeccakTranscript, std::alloc::Global>( + &assembly, + &setup, + raw_trace_len, + ) .expect("proof"); let valid = fflonk::verify::<_, _, RollingKeccakTranscript>(&vk, &proof, None).unwrap(); diff --git a/crates/proof-compression/src/proof_system/fflonk.rs b/crates/proof-compression/src/proof_system/fflonk.rs index a442b88..f4e1f0c 100644 --- a/crates/proof-compression/src/proof_system/fflonk.rs +++ b/crates/proof-compression/src/proof_system/fflonk.rs @@ -71,7 +71,7 @@ impl SnarkWrapperProofSystem for FflonkSnarkWrapper { >; fn pre_init() { - let domain_size = ::fflonk::fflonk::L1_VERIFIER_DOMAIN_SIZE_LOG; + let domain_size = 1 << ::fflonk::fflonk::L1_VERIFIER_DOMAIN_SIZE_LOG; Self::Context::init_pinned_memory(domain_size).unwrap(); } @@ -115,15 +115,14 @@ impl SnarkWrapperProofSystem for FflonkSnarkWrapper { assert_eq!(domain_size, finalization_hint); let ctx = ctx.wait(); let precomputation = precomputation.wait().into_inner(); - let proof = ::fflonk::create_proof::< - _, - _, - _, - RollingKeccakTranscript<_>, - CombinedMonomialDeviceStorage, - _, - >(&proving_assembly, &precomputation, raw_trace_len) + let start = std::time::Instant::now(); + let proof = ::fflonk::create_proof::<_, _, _, RollingKeccakTranscript<_>, _>( + &proving_assembly, + &precomputation, + raw_trace_len, + ) .unwrap(); + println!("fflonk proving takes {} s", start.elapsed().as_secs()); drop(ctx); proof } diff --git a/crates/proof-compression/src/proof_system/plonk.rs b/crates/proof-compression/src/proof_system/plonk.rs index 69bcc71..6be7fe7 100644 --- a/crates/proof-compression/src/proof_system/plonk.rs +++ b/crates/proof-compression/src/proof_system/plonk.rs @@ -144,6 +144,7 @@ impl SnarkWrapperProofSystem for PlonkSnarkWrapper { let mut ctx = ctx.into_inner(); let mut precomputation = precomputation.wait().into_inner(); let worker = bellman::worker::Worker::new(); + let start = std::time::Instant::now(); let proof = gpu_prover::create_proof::<_, _, Self::Transcript, _>( &proving_assembly, &mut ctx, @@ -152,7 +153,7 @@ impl SnarkWrapperProofSystem for PlonkSnarkWrapper { None, ) .unwrap(); - + println!("plonk proving takes {} s", start.elapsed().as_secs()); ctx.free_all_slots(); proof