Skip to content

Commit

Permalink
no rayon on verification code path (#46)
Browse files Browse the repository at this point in the history
verification will execute all workload on the same thread that made an ffi call
  • Loading branch information
dshulyak authored May 8, 2023
1 parent f7d93cb commit 8b05e71
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 65 deletions.
21 changes: 6 additions & 15 deletions benches/verifying.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ use pprof::criterion::{Output, PProfProfiler};

use scrypt_jane::scrypt::ScryptParams;

fn threads_to_str(threads: usize) -> String {
if threads == 0 {
"auto".into()
} else {
threads.to_string()
}
}

fn verifying(c: &mut Criterion) {
let challenge = b"hello world, challenge me!!!!!!!";
let metadata = ProofMetadata {
Expand All @@ -27,18 +19,17 @@ fn verifying(c: &mut Criterion) {
};
let num_labels = metadata.num_units as u64 * metadata.labels_per_unit;

for (k2, k3, threads) in itertools::iproduct!(
for (k2, k3) in itertools::iproduct!(
[200, 300],
[50, 100],
[0, 1] // 0 == automatic
[50, 100]
) {
c.bench_with_input(
BenchmarkId::new(
"verify",
format!("k2={k2}/k3={k3}/threads={}", threads_to_str(threads)),
format!("k2={k2}/k3={k3}"),
),
&(k2, k3, threads),
|b, &(k2, k3, threads)| {
&(k2, k3),
|b, &(k2, k3)| {
let proof = Proof::new(
0,
(0..k2 as u64).collect::<Vec<u64>>().as_slice(),
Expand All @@ -57,7 +48,7 @@ fn verifying(c: &mut Criterion) {
};

b.iter(|| {
let result = verify(&proof, &metadata, params, threads);
let result = verify(&proof, &metadata, params);
assert_eq!(Ok(()), result, "proof is not valid");
});
},
Expand Down
3 changes: 1 addition & 2 deletions ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ pub unsafe extern "C" fn verify_proof(
proof: Proof,
metadata: *const ProofMetadata,
cfg: Config,
threads: usize,
) -> VerifyResult {
let proof = {
let indices =
Expand All @@ -127,7 +126,7 @@ pub unsafe extern "C" fn verify_proof(
Err(_) => return VerifyResult::InvalidArgument,
};

let result = match verify(&proof, metadata, params, threads) {
let result = match verify(&proof, metadata, params) {
Ok(_) => VerifyResult::Ok,
Err(err) => {
eprintln!("Proof is invalid: {err}");
Expand Down
79 changes: 35 additions & 44 deletions src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ use std::cmp::Ordering;

use cipher::BlockEncrypt;
use itertools::Itertools;
use rayon::prelude::{ParallelBridge, ParallelIterator};
use scrypt_jane::scrypt::ScryptParams;

use crate::{
Expand Down Expand Up @@ -94,8 +93,7 @@ impl VerifyingParams {
pub fn verify(
proof: &Proof,
metadata: &ProofMetadata,
params: VerifyingParams,
threads: usize,
params: VerifyingParams
) -> Result<(), String> {
let challenge = metadata.challenge;

Expand Down Expand Up @@ -156,48 +154,41 @@ pub fn verify(

let k3_indices = RandomValuesIterator::new(indices_unpacked, seed).take(params.k3 as usize);

let pool = rayon::ThreadPoolBuilder::new()
.num_threads(threads)
.build()
.unwrap();

pool.install(|| {
k3_indices.par_bridge().try_for_each(|index| {
let mut output = [0u8; 16];
let label = generate_label(&commitment, params.scrypt, index);
cipher.aes.encrypt_block_b2b(
&label.into(),
(&mut output).into(),
);
k3_indices.into_iter().try_for_each(|index| {
let mut output = [0u8; 16];
let label = generate_label(&commitment, params.scrypt, index);
cipher.aes.encrypt_block_b2b(
&label.into(),
(&mut output).into(),
);

let msb = output[output_index];
match msb.cmp(&difficulty_msb) {
Ordering::Less => {
// valid
},
Ordering::Greater => {
// invalid
let msb = output[output_index];
match msb.cmp(&difficulty_msb) {
Ordering::Less => {
// valid
},
Ordering::Greater => {
// invalid
return Err(format!(
"MSB value for index: {index} doesn't satisfy difficulty: {msb} > {difficulty_msb} (label: {label:?})",
));
},
Ordering::Equal => {
// Need to check LSB
let mut output = [0u64; 2];
lazy_cipher.aes.encrypt_block_b2b(
&label.into(),
bytemuck::cast_slice_mut(&mut output).into(),
);
let lsb = output[0].to_le() & 0x00ff_ffff_ffff_ffff;
if lsb >= difficulty_lsb {
return Err(format!(
"MSB value for index: {index} doesn't satisfy difficulty: {msb} > {difficulty_msb} (label: {label:?})",
"LSB value for index: {index} doesn't satisfy difficulty: {lsb} >= {difficulty_lsb} (label: {label:?})",
));
},
Ordering::Equal => {
// Need to check LSB
let mut output = [0u64; 2];
lazy_cipher.aes.encrypt_block_b2b(
&label.into(),
bytemuck::cast_slice_mut(&mut output).into(),
);
let lsb = output[0].to_le() & 0x00ff_ffff_ffff_ffff;
if lsb >= difficulty_lsb {
return Err(format!(
"LSB value for index: {index} doesn't satisfy difficulty: {lsb} >= {difficulty_lsb} (label: {label:?})",
));
}
}
}
Ok(())
})
}
Ok(())
})
}

Expand Down Expand Up @@ -278,7 +269,7 @@ mod tests {
k2_pow,
k3_pow,
};
assert!(verify(&empty_proof, &fake_metadata, params, 1).is_err());
assert!(verify(&empty_proof, &fake_metadata, params).is_err());
}
{
let proof_with_not_enough_indices = Proof {
Expand All @@ -287,7 +278,7 @@ mod tests {
k2_pow,
k3_pow,
};
assert!(verify(&proof_with_not_enough_indices, &fake_metadata, params, 1).is_err());
assert!(verify(&proof_with_not_enough_indices, &fake_metadata, params).is_err());
}
{
let proof_with_invalid_k2_pow = Proof {
Expand All @@ -296,7 +287,7 @@ mod tests {
k2_pow: params.k2_pow_difficulty,
k3_pow,
};
assert!(verify(&proof_with_invalid_k2_pow, &fake_metadata, params, 1).is_err());
assert!(verify(&proof_with_invalid_k2_pow, &fake_metadata, params).is_err());
}
{
let proof_with_invalid_k3_pow = Proof {
Expand All @@ -305,7 +296,7 @@ mod tests {
k2_pow,
k3_pow: params.k3_pow_difficulty,
};
assert!(verify(&proof_with_invalid_k3_pow, &fake_metadata, params, 1).is_err());
assert!(verify(&proof_with_invalid_k3_pow, &fake_metadata, params).is_err());
}
}
}
4 changes: 0 additions & 4 deletions tests/generate_and_verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ fn test_generate_and_verify() {
&proof,
&metadata,
VerifyingParams::new(&metadata, &cfg).unwrap(),
0,
);
assert_eq!(Ok(()), valid, "proof is not valid");

Expand All @@ -61,7 +60,6 @@ fn test_generate_and_verify() {
&invalid_proof,
&metadata,
VerifyingParams::new(&metadata, &cfg).unwrap(),
0,
);
assert!(valid.is_err(), "proof should be invalid");
}
Expand Down Expand Up @@ -111,7 +109,6 @@ fn test_generate_and_verify_difficulty_msb_not_zero() {
&proof,
&metadata,
VerifyingParams::new(&metadata, &cfg).unwrap(),
0,
);
assert_eq!(Ok(()), valid, "proof is not valid");

Expand All @@ -122,7 +119,6 @@ fn test_generate_and_verify_difficulty_msb_not_zero() {
&invalid_proof,
&metadata,
VerifyingParams::new(&metadata, &cfg).unwrap(),
0,
);
assert!(valid.is_err(), "proof should be invalid");
}

0 comments on commit 8b05e71

Please sign in to comment.