Skip to content

Commit

Permalink
apply missing pinned allocator to combined monomials
Browse files Browse the repository at this point in the history
  • Loading branch information
saitima committed Jan 14, 2025
1 parent 5eb4da9 commit 234de68
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 47 deletions.
1 change: 1 addition & 0 deletions crates/fflonk/src/allocator/pinned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 21 additions & 23 deletions crates/fflonk/src/convenience.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use circuit_definitions::circuit_definitions::aux_layer::{
};
use fflonk::{FflonkAssembly, L1_VERIFIER_DOMAIN_SIZE_LOG};

pub type FflonkSnarkVerifierCircuitDeviceSetup<A: HostAllocator = GlobalStaticHost> =
pub type FflonkSnarkVerifierCircuitDeviceSetup<A: HostAllocator = std::alloc::Global> =
FflonkDeviceSetup<Bn256, FflonkSnarkVerifierCircuit, A>;

use super::*;
Expand Down Expand Up @@ -107,17 +107,18 @@ pub fn gpu_prove_fflonk_snark_verifier_circuit_single_shot(
FflonkSnarkVerifierCircuitProof,
FflonkSnarkVerifierCircuitVK,
) {
let mut assembly = FflonkAssembly::<Bn256, SynthesisModeTesting, GlobalStaticHost>::new();
let mut assembly = FflonkAssembly::<Bn256, SynthesisModeTesting>::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();
Expand All @@ -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<Fr>,
_,
>(&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());

Expand All @@ -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::<Bn256, SynthesisModeProve, GlobalStaticHost>::new();
let mut proving_assembly =
FflonkAssembly::<Bn256, SynthesisModeProve, std::alloc::Global>::new();
circuit
.synthesize(&mut proving_assembly)
.expect("must work");
Expand All @@ -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<Fr>,
_,
>(&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());

Expand All @@ -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::<GlobalStaticHost>::create_setup_on_device(&circuit)
.unwrap();
FflonkSnarkVerifierCircuitDeviceSetup::<std::alloc::Global>::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();
Expand Down
4 changes: 2 additions & 2 deletions crates/fflonk/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use fflonk::{
commit_point_as_xy, compute_generators, horner_evaluation, FflonkAssembly, FflonkProof,
};

pub fn create_proof<E, C, S, T, CM, A>(
pub fn create_proof<E, C, S, T, A>(
assembly: &FflonkAssembly<E, S, A>,
setup: &FflonkDeviceSetup<E, C, A>,
raw_trace_len: usize,
Expand Down Expand Up @@ -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::<E::Fr, _>::allocate_on(&device, domain_size)?;
GenericCombinedStorage::<E::Fr, GlobalStaticHost>::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(
Expand Down
2 changes: 1 addition & 1 deletion crates/fflonk/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::*;

use crate::HostAllocator;

pub struct FflonkDeviceSetup<E: Engine, C: Circuit<E>, A: HostAllocator = GlobalStaticHost> {
pub struct FflonkDeviceSetup<E: Engine, C: Circuit<E>, A: HostAllocator = std::alloc::Global> {
pub main_gate_selector_monomials: [Vec<E::Fr, A>; 5],
pub variable_indexes: [Vec<u32, A>; 3],
pub c0_commitment: E::G1Affine,
Expand Down
5 changes: 3 additions & 2 deletions crates/fflonk/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub trait CombinedMonomialStorage {
) -> CudaResult<()>;
}

pub enum GenericCombinedStorage<F, A = std::alloc::Global>
pub enum GenericCombinedStorage<F, A = GlobalStaticHost>
where
F: PrimeField,
A: HostAllocator,
Expand Down Expand Up @@ -46,9 +46,10 @@ where
}
}

impl<F> CombinedMonomialStorage for GenericCombinedStorage<F>
impl<F, A> CombinedMonomialStorage for GenericCombinedStorage<F, A>
where
F: PrimeField,
A: HostAllocator,
{
type Poly = Poly<F, MonomialBasis>;

Expand Down
15 changes: 6 additions & 9 deletions crates/fflonk/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn test_simple_circuit_with_naive_main_gate() {
type C = FflonkTestCircuit;
let circuit = C {};

let mut assembly = FflonkAssembly::<Bn256, SynthesisModeTesting, GlobalStaticHost>::new();
let mut assembly = FflonkAssembly::<Bn256, SynthesisModeTesting>::new();
circuit.synthesize(&mut assembly).expect("must work");
assert!(assembly.is_satisfied());
let raw_trace_len = assembly.n();
Expand All @@ -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<Fr>,
CombinedMonomialDeviceStorage<Fr>,
GlobalStaticHost,
>(&assembly, &setup, raw_trace_len)
let proof = create_proof::<_, C, _, RollingKeccakTranscript<Fr>, std::alloc::Global>(
&assembly,
&setup,
raw_trace_len,
)
.expect("proof");

let valid = fflonk::verify::<_, _, RollingKeccakTranscript<Fr>>(&vk, &proof, None).unwrap();
Expand Down
17 changes: 8 additions & 9 deletions crates/proof-compression/src/proof_system/fflonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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<Fr>,
_,
>(&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
}
Expand Down
3 changes: 2 additions & 1 deletion crates/proof-compression/src/proof_system/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -152,7 +153,7 @@ impl SnarkWrapperProofSystem for PlonkSnarkWrapper {
None,
)
.unwrap();

println!("plonk proving takes {} s", start.elapsed().as_secs());
ctx.free_all_slots();

proof
Expand Down

0 comments on commit 234de68

Please sign in to comment.