Skip to content

Commit 82c5c13

Browse files
committed
Support flat directories for groth16 proofs
1 parent d36b40c commit 82c5c13

File tree

1 file changed

+87
-51
lines changed

1 file changed

+87
-51
lines changed

crates/task-sender/src/commands.rs

Lines changed: 87 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -450,64 +450,100 @@ pub async fn send_infinite_proofs(args: SendInfiniteProofsArgs) {
450450
).await;
451451
}
452452

453+
fn load_groth16_proof_files(dir_path: &std::path::Path, base_name: &str) -> Option<VerificationData> {
454+
let proof_path = dir_path.join(format!("{}.proof", base_name));
455+
let public_input_path = dir_path.join(format!("{}.pub", base_name));
456+
let vk_path = dir_path.join(format!("{}.vk", base_name));
457+
458+
let proof = std::fs::read(&proof_path).ok()?;
459+
let public_input = std::fs::read(&public_input_path).ok()?;
460+
let vk = std::fs::read(&vk_path).ok()?;
461+
462+
Some(VerificationData {
463+
proving_system: ProvingSystemId::GnarkGroth16Bn254,
464+
proof,
465+
pub_input: Some(public_input),
466+
verification_key: Some(vk),
467+
vm_program_code: None,
468+
proof_generator_addr: Address::zero(), // Will be set later
469+
})
470+
}
471+
472+
fn load_from_subdirectories(dir_path: &str) -> Vec<VerificationData> {
473+
let mut verifications_data = vec![];
474+
let dir = std::fs::read_dir(dir_path).expect("Directory does not exist");
475+
476+
for entry in dir.flatten() {
477+
let proof_folder_dir = entry.path();
478+
if proof_folder_dir.is_dir() && proof_folder_dir.to_str().unwrap().contains("groth16") {
479+
// Get the first file to determine the base name
480+
if let Some(first_file) = fs::read_dir(&proof_folder_dir)
481+
.ok()
482+
.and_then(|dir| dir.flatten().map(|e| e.path()).find(|path| path.is_file()))
483+
{
484+
if let Some(base_name) = first_file.file_stem().and_then(|s| s.to_str()) {
485+
if let Some(verification_data) = load_groth16_proof_files(&proof_folder_dir, base_name) {
486+
verifications_data.push(verification_data);
487+
}
488+
}
489+
}
490+
}
491+
}
492+
493+
verifications_data
494+
}
495+
496+
fn load_from_flat_directory(dir_path: &str) -> Vec<VerificationData> {
497+
let mut verifications_data = vec![];
498+
let mut base_names = std::collections::HashSet::new();
499+
500+
// Collect all unique base names from .proof files
501+
if let Ok(dir) = std::fs::read_dir(dir_path) {
502+
for entry in dir.flatten() {
503+
let path = entry.path();
504+
if path.is_file() && path.extension().and_then(|s| s.to_str()) == Some("proof") {
505+
if let Some(base_name) = path.file_stem().and_then(|s| s.to_str()) {
506+
base_names.insert(base_name.to_string());
507+
}
508+
}
509+
}
510+
}
511+
512+
// Load verification data for each base name
513+
let dir_path = std::path::Path::new(dir_path);
514+
for base_name in base_names {
515+
if let Some(verification_data) = load_groth16_proof_files(dir_path, &base_name) {
516+
verifications_data.push(verification_data);
517+
}
518+
}
519+
520+
verifications_data
521+
}
522+
453523
/// Returns the corresponding verification data for the generated proofs directory
454524
fn get_verification_data_from_proofs_folder(
455525
dir_path: String,
456526
default_addr: Address,
457527
) -> Vec<VerificationData> {
458-
let mut verifications_data = vec![];
459-
460528
info!("Reading proofs from {:?}", dir_path);
461529

462-
let dir = std::fs::read_dir(dir_path).expect("Directory does not exists");
463-
464-
for proof_folder in dir {
465-
// each proof_folder is a dir called groth16_n
466-
let proof_folder_dir = proof_folder.unwrap().path();
467-
if proof_folder_dir.is_dir() {
468-
// todo(marcos): this should be improved if we want to support more proofs
469-
// currently we stored the proofs on subdirs with a prefix for the proof type
470-
// and here we check the subdir name and based on build the verification data accordingly
471-
if proof_folder_dir.to_str().unwrap().contains("groth16") {
472-
// Get the first file from the folder
473-
let first_file = fs::read_dir(proof_folder_dir.clone())
474-
.expect("Can't read proofs directory")
475-
.filter_map(|entry| entry.ok().map(|e| e.path()))
476-
.find(|path| path.is_file()) // Find any valid file
477-
.expect("No valid proof files found");
478-
479-
// Extract the base name (file stem) without extension
480-
let base_name = first_file
481-
.file_stem()
482-
.and_then(|s| s.to_str())
483-
.expect("Failed to extract base name");
484-
485-
// Generate the paths for the other files
486-
let proof_path = proof_folder_dir.join(format!("{}.proof", base_name));
487-
let public_input_path = proof_folder_dir.join(format!("{}.pub", base_name));
488-
let vk_path = proof_folder_dir.join(format!("{}.vk", base_name));
489-
490-
let Ok(proof) = std::fs::read(&proof_path) else {
491-
continue;
492-
};
493-
let Ok(public_input) = std::fs::read(&public_input_path) else {
494-
continue;
495-
};
496-
let Ok(vk) = std::fs::read(&vk_path) else {
497-
continue;
498-
};
499-
500-
let verification_data = VerificationData {
501-
proving_system: ProvingSystemId::GnarkGroth16Bn254,
502-
proof,
503-
pub_input: Some(public_input),
504-
verification_key: Some(vk),
505-
vm_program_code: None,
506-
proof_generator_addr: default_addr,
507-
};
508-
verifications_data.push(verification_data);
509-
}
510-
}
530+
// Check if we have subdirectories with groth16 in the name
531+
let has_groth16_subdirs = std::fs::read_dir(&dir_path)
532+
.map(|dir| {
533+
dir.flatten()
534+
.any(|entry| entry.path().is_dir() && entry.path().to_str().unwrap().contains("groth16"))
535+
})
536+
.unwrap_or(false);
537+
538+
let mut verifications_data = if has_groth16_subdirs {
539+
load_from_subdirectories(&dir_path)
540+
} else {
541+
load_from_flat_directory(&dir_path)
542+
};
543+
544+
// Set the default address for all verification data
545+
for data in &mut verifications_data {
546+
data.proof_generator_addr = default_addr;
511547
}
512548

513549
verifications_data

0 commit comments

Comments
 (0)