Mutex guard the witness calculator

This commit is contained in:
Remco Bloemen 2022-03-18 15:28:30 -07:00
parent 0e5779729d
commit f89a4e399f
2 changed files with 7 additions and 4 deletions

View File

@ -6,6 +6,7 @@ use core::include_bytes;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::io::{Cursor, Write}; use std::io::{Cursor, Write};
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
use std::sync::Mutex;
const ZKEY_BYTES: &[u8] = include_bytes!("../semaphore/build/snark/semaphore_final.zkey"); const ZKEY_BYTES: &[u8] = include_bytes!("../semaphore/build/snark/semaphore_final.zkey");
const WASM: &[u8] = include_bytes!("../semaphore/build/snark/semaphore.wasm"); const WASM: &[u8] = include_bytes!("../semaphore/build/snark/semaphore.wasm");
@ -15,7 +16,7 @@ pub static ZKEY: Lazy<(ProvingKey<Bn254>, ConstraintMatrices<Fr>)> = Lazy::new(|
read_zkey(&mut reader).expect("zkey should be valid") read_zkey(&mut reader).expect("zkey should be valid")
}); });
pub static WITNESS_CALCULATOR: Lazy<WitnessCalculator> = Lazy::new(|| { pub static WITNESS_CALCULATOR: Lazy<Mutex<WitnessCalculator>> = Lazy::new(|| {
// HACK: ark-circom requires a file, so we make one! // HACK: ark-circom requires a file, so we make one!
let mut tmpfile = NamedTempFile::new().expect("Failed to create temp file"); let mut tmpfile = NamedTempFile::new().expect("Failed to create temp file");
let written = tmpfile.write(WASM).expect("Failed to write to temp file"); let written = tmpfile.write(WASM).expect("Failed to write to temp file");
@ -23,5 +24,5 @@ pub static WITNESS_CALCULATOR: Lazy<WitnessCalculator> = Lazy::new(|| {
let path = tmpfile.into_temp_path(); let path = tmpfile.into_temp_path();
let result = WitnessCalculator::new(&path).expect("Failed to create witness calculator"); let result = WitnessCalculator::new(&path).expect("Failed to create witness calculator");
path.close().expect("Could not remove tempfile"); path.close().expect("Could not remove tempfile");
result Mutex::new(result)
}); });

View File

@ -116,8 +116,10 @@ pub fn generate_proof(
let now = Instant::now(); let now = Instant::now();
let full_assignment = WITNESS_CALCULATOR let full_assignment =
.clone() WITNESS_CALCULATOR
.lock()
.expect("witness_calculator mutex should not get poisoned")
.calculate_witness_element::<Bn254, _>(inputs, false) .calculate_witness_element::<Bn254, _>(inputs, false)
.map_err(ProofError::WitnessError)?; .map_err(ProofError::WitnessError)?;