|
| 1 | +//! Blake2s hash benchmark |
| 2 | +
|
| 3 | +use std::env; |
| 4 | + |
| 5 | +use binius_examples::{ |
| 6 | + ExampleCircuit, |
| 7 | + circuits::blake2s::{Blake2sExample, Instance, Params}, |
| 8 | + setup, |
| 9 | +}; |
| 10 | +use binius_frontend::compiler::CircuitBuilder; |
| 11 | +use binius_utils::platform_diagnostics::PlatformDiagnostics; |
| 12 | +use binius_verifier::{ |
| 13 | + config::StdChallenger, |
| 14 | + transcript::{ProverTranscript, VerifierTranscript}, |
| 15 | +}; |
| 16 | +use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; |
| 17 | + |
| 18 | +const DEFAULT_MAX_BYTES: usize = 2048 * 64; // 2048 blocks × 64 bytes/block = 131,072 bytes |
| 19 | + |
| 20 | +fn get_feature_suffix(_diagnostics: &PlatformDiagnostics) -> String { |
| 21 | + let mut suffix_parts = vec![]; |
| 22 | + |
| 23 | + // Add architecture |
| 24 | + #[cfg(target_arch = "x86_64")] |
| 25 | + { |
| 26 | + suffix_parts.push("x86_64"); |
| 27 | + // Add key features based on compile-time features |
| 28 | + #[cfg(target_feature = "gfni")] |
| 29 | + suffix_parts.push("gfni"); |
| 30 | + #[cfg(target_feature = "avx512f")] |
| 31 | + suffix_parts.push("avx512"); |
| 32 | + #[cfg(all(not(target_feature = "avx512f"), target_feature = "avx2"))] |
| 33 | + suffix_parts.push("avx2"); |
| 34 | + } |
| 35 | + |
| 36 | + #[cfg(target_arch = "aarch64")] |
| 37 | + { |
| 38 | + suffix_parts.push("arm64"); |
| 39 | + // Check for NEON and AES |
| 40 | + #[cfg(all(target_feature = "neon", target_feature = "aes"))] |
| 41 | + suffix_parts.push("neon_aes"); |
| 42 | + #[cfg(all(target_feature = "neon", not(target_feature = "aes")))] |
| 43 | + suffix_parts.push("neon"); |
| 44 | + } |
| 45 | + |
| 46 | + suffix_parts.join("_") |
| 47 | +} |
| 48 | + |
| 49 | +/// Benchmark Blake2s hash circuit |
| 50 | +fn bench_blake2s_hash(c: &mut Criterion) { |
| 51 | + // Get maximum message size from environment or use default |
| 52 | + let max_bytes = env::var("BLAKE2S_MAX_BYTES") |
| 53 | + .ok() |
| 54 | + .and_then(|s| s.parse::<usize>().ok()) |
| 55 | + .unwrap_or(DEFAULT_MAX_BYTES); |
| 56 | + |
| 57 | + // Gather and print comprehensive platform diagnostics |
| 58 | + let diagnostics = PlatformDiagnostics::gather(); |
| 59 | + diagnostics.print(); |
| 60 | + |
| 61 | + // Print benchmark-specific parameters |
| 62 | + let blocks = max_bytes.div_ceil(64); |
| 63 | + println!("\nBlake2s Benchmark Parameters:"); |
| 64 | + println!(" Circuit capacity: {} bytes ({} blocks × 64 bytes/block)", max_bytes, blocks); |
| 65 | + println!(" Message length: {} bytes (using full capacity)", max_bytes); |
| 66 | + println!(" Note: Circuit size is dynamic based on max_bytes parameter"); |
| 67 | + println!("=======================================\n"); |
| 68 | + |
| 69 | + let params = Params { max_bytes }; |
| 70 | + let instance = Instance {}; |
| 71 | + |
| 72 | + // Setup phase - do this once outside the benchmark loop |
| 73 | + let mut builder = CircuitBuilder::new(); |
| 74 | + let example = Blake2sExample::build(params.clone(), &mut builder).unwrap(); |
| 75 | + let circuit = builder.build(); |
| 76 | + let cs = circuit.constraint_system().clone(); |
| 77 | + let (verifier, prover) = setup(cs, 1).unwrap(); |
| 78 | + |
| 79 | + // Create a witness once for proof size measurement |
| 80 | + let mut filler = circuit.new_witness_filler(); |
| 81 | + example |
| 82 | + .populate_witness(instance.clone(), &mut filler) |
| 83 | + .unwrap(); |
| 84 | + circuit.populate_wire_witness(&mut filler).unwrap(); |
| 85 | + let witness = filler.into_value_vec(); |
| 86 | + |
| 87 | + let feature_suffix = get_feature_suffix(&diagnostics); |
| 88 | + |
| 89 | + // Benchmark 1: Witness generation |
| 90 | + { |
| 91 | + let mut group = c.benchmark_group("blake2s_witness_generation"); |
| 92 | + group.throughput(Throughput::Bytes(max_bytes as u64)); |
| 93 | + |
| 94 | + let bench_name = format!("bytes_{}_{}", max_bytes, feature_suffix); |
| 95 | + group.bench_with_input(BenchmarkId::from_parameter(&bench_name), &max_bytes, |b, _| { |
| 96 | + b.iter(|| { |
| 97 | + let mut filler = circuit.new_witness_filler(); |
| 98 | + example |
| 99 | + .populate_witness(instance.clone(), &mut filler) |
| 100 | + .unwrap(); |
| 101 | + circuit.populate_wire_witness(&mut filler).unwrap(); |
| 102 | + filler.into_value_vec() |
| 103 | + }) |
| 104 | + }); |
| 105 | + |
| 106 | + group.finish(); |
| 107 | + } |
| 108 | + |
| 109 | + // Benchmark 2: Proof generation |
| 110 | + { |
| 111 | + let mut group = c.benchmark_group("blake2s_proof_generation"); |
| 112 | + group.throughput(Throughput::Bytes(max_bytes as u64)); |
| 113 | + group.measurement_time(std::time::Duration::from_secs(120)); // 120 seconds measurement time |
| 114 | + group.sample_size(100); // Keep 100 samples |
| 115 | + |
| 116 | + let bench_name = format!("bytes_{}_{}", max_bytes, feature_suffix); |
| 117 | + group.bench_with_input(BenchmarkId::from_parameter(&bench_name), &max_bytes, |b, _| { |
| 118 | + b.iter(|| { |
| 119 | + let mut prover_transcript = ProverTranscript::new(StdChallenger::default()); |
| 120 | + prover |
| 121 | + .prove(witness.clone(), &mut prover_transcript) |
| 122 | + .unwrap() |
| 123 | + }) |
| 124 | + }); |
| 125 | + |
| 126 | + group.finish(); |
| 127 | + } |
| 128 | + |
| 129 | + // Generate one proof for verification benchmark and size reporting |
| 130 | + let mut prover_transcript = ProverTranscript::new(StdChallenger::default()); |
| 131 | + prover |
| 132 | + .prove(witness.clone(), &mut prover_transcript) |
| 133 | + .unwrap(); |
| 134 | + let proof_bytes = prover_transcript.finalize(); |
| 135 | + |
| 136 | + // Benchmark 3: Proof verification |
| 137 | + { |
| 138 | + let mut group = c.benchmark_group("blake2s_proof_verification"); |
| 139 | + group.throughput(Throughput::Bytes(max_bytes as u64)); |
| 140 | + |
| 141 | + let bench_name = format!("bytes_{}_{}", max_bytes, feature_suffix); |
| 142 | + group.bench_with_input(BenchmarkId::from_parameter(&bench_name), &max_bytes, |b, _| { |
| 143 | + b.iter(|| { |
| 144 | + let mut verifier_transcript = |
| 145 | + VerifierTranscript::new(StdChallenger::default(), proof_bytes.clone()); |
| 146 | + verifier |
| 147 | + .verify(witness.public(), &mut verifier_transcript) |
| 148 | + .expect("Proof verification failed") |
| 149 | + }) |
| 150 | + }); |
| 151 | + |
| 152 | + group.finish(); |
| 153 | + } |
| 154 | + |
| 155 | + // Print proof size |
| 156 | + println!("\n\nBlake2s proof size for {} bytes message: {} bytes", max_bytes, proof_bytes.len()); |
| 157 | +} |
| 158 | + |
| 159 | +criterion_group!(benches, bench_blake2s_hash); |
| 160 | +criterion_main!(benches); |
0 commit comments