Skip to content

Commit d1393a0

Browse files
authored
Merge pull request #40 from spacemeshos/parallel-k2-k3-pows
Find k2 and k3 PoWs in parallel for better speed
2 parents b29742e + 53bd7bb commit d1393a0

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

benches/pow.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,22 @@ fn bench_k2_pow(c: &mut Criterion) {
1212
b"hello world, CHALLENGE me!!!!!!!",
1313
0,
1414
ScryptParams::new(6, 0, 0),
15-
black_box(0x0FFF_FFFF_FFFF_FFFF),
15+
black_box(0x000F_FFFF_FFFF_FFFF),
16+
)
17+
})
18+
});
19+
}
20+
21+
fn bench_k3_pow(c: &mut Criterion) {
22+
c.bench_function("k3_pow", |b| {
23+
b.iter(|| {
24+
post::pow::find_k3_pow(
25+
b"hello world, CHALLENGE me!!!!!!!",
26+
0,
27+
&[0; 300],
28+
ScryptParams::new(6, 0, 0),
29+
black_box(0x000F_FFFF_FFFF_FFFF),
30+
77,
1631
)
1732
})
1833
});
@@ -21,7 +36,7 @@ fn bench_k2_pow(c: &mut Criterion) {
2136
criterion_group!(
2237
name = benches;
2338
config = Criterion::default().with_profiler(PProfProfiler::new(1000, Output::Flamegraph(None)));
24-
targets=bench_k2_pow
39+
targets=bench_k2_pow,bench_k3_pow
2540
);
2641

2742
criterion_main!(benches);

src/pow.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
//! without actually holding the whole POST data.
88
//!
99
//! TODO: explain the need for "K3 PoW".
10+
use rayon::prelude::{ParallelBridge, ParallelIterator};
1011
use scrypt_jane::scrypt::{scrypt, ScryptParams};
1112

1213
pub fn find_k2_pow(challenge: &[u8; 32], nonce: u32, params: ScryptParams, difficulty: u64) -> u64 {
13-
for k2_pow in 0u64.. {
14-
if hash_k2_pow(challenge, nonce, params, k2_pow) < difficulty {
15-
return k2_pow;
16-
}
17-
}
18-
unreachable!()
14+
(0u64..)
15+
.par_bridge()
16+
.find_any(|&k2_pow| hash_k2_pow(challenge, nonce, params, k2_pow) < difficulty)
17+
.expect("looking for k2pow")
1918
}
2019

2120
#[inline(always)]
@@ -33,20 +32,20 @@ pub(crate) fn hash_k2_pow(
3332
u64::from_le_bytes(output)
3433
}
3534

36-
pub(crate) fn find_k3_pow(
35+
pub fn find_k3_pow(
3736
challenge: &[u8; 32],
3837
nonce: u32,
3938
indexes: &[u8],
4039
params: ScryptParams,
4140
difficulty: u64,
4241
k2_pow: u64,
4342
) -> u64 {
44-
for k3_pow in 0u64.. {
45-
if hash_k3_pow(challenge, nonce, indexes, params, k2_pow, k3_pow) < difficulty {
46-
return k3_pow;
47-
}
48-
}
49-
unreachable!()
43+
(0u64..)
44+
.par_bridge()
45+
.find_any(|&k3_pow| {
46+
hash_k3_pow(challenge, nonce, indexes, params, k2_pow, k3_pow) < difficulty
47+
})
48+
.expect("looking for k3pow")
5049
}
5150

5251
#[inline(always)]

0 commit comments

Comments
 (0)