Skip to content

Commit 53bd7bb

Browse files
committed
Merge remote-tracking branch 'origin/main' into parallel-k2-k3-pows
2 parents 1153404 + b29742e commit 53bd7bb

10 files changed

+416
-147
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ scrypt-jane = { git = "https://github.com/spacemeshos/scrypt-jane-rs", branch =
2626
blake3 = "1.3.3"
2727
bitvec = "1.0.1"
2828
rayon = "1.6.1"
29+
rand = "0.8.5"
2930

3031
[dev-dependencies]
3132
criterion = "0.4"

benches/proving.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::hint::black_box;
22

33
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
4-
use post::{prove::ConstDProver, prove::Prover, prove::ProvingParams};
4+
use post::{prove::Prover, prove::Prover8_56, prove::ProvingParams};
55
use pprof::criterion::{Output, PProfProfiler};
66
use rand::{thread_rng, RngCore};
77
use rayon::prelude::{ParallelBridge, ParallelIterator};
@@ -23,7 +23,7 @@ fn threads_to_str(threads: usize) -> String {
2323
fn prover_bench(c: &mut Criterion) {
2424
let mut group = c.benchmark_group("proving");
2525

26-
let mut data = vec![0; 32 * MIB];
26+
let mut data = vec![0; 64 * MIB];
2727
thread_rng().fill_bytes(&mut data);
2828
group.throughput(criterion::Throughput::Bytes(data.len() as u64));
2929

@@ -36,8 +36,8 @@ fn prover_bench(c: &mut Criterion) {
3636
};
3737

3838
for (nonces, threads) in itertools::iproduct!(
39-
[2, 20, 200],
40-
[0, 1] // 0 == automatic
39+
[16, 32, 64],
40+
[1, 0] // 0 == automatic
4141
) {
4242
group.bench_with_input(
4343
BenchmarkId::new(
@@ -46,7 +46,7 @@ fn prover_bench(c: &mut Criterion) {
4646
),
4747
&(nonces, threads),
4848
|b, &(nonces, threads)| {
49-
let prover = ConstDProver::new(CHALLENGE, 0..nonces, params.clone());
49+
let prover = Prover8_56::new(CHALLENGE, 0..nonces, params.clone()).unwrap();
5050
b.iter(|| {
5151
let f = black_box(|_, _| None);
5252
match threads {

src/cipher.rs

+20
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ impl AesCipher {
3737
k2_pow,
3838
}
3939
}
40+
41+
pub(crate) fn new_lazy(
42+
challenge: &[u8; 32],
43+
nonce: u32,
44+
nonce_group: u32,
45+
k2_pow: u64,
46+
) -> Self {
47+
let mut hasher = blake3::Hasher::new();
48+
hasher.update(challenge);
49+
hasher.update(&nonce_group.to_le_bytes());
50+
hasher.update(&k2_pow.to_le_bytes());
51+
hasher.update(&nonce.to_le_bytes());
52+
Self {
53+
aes: Aes128::new(GenericArray::from_slice(
54+
&hasher.finalize().as_bytes()[..16],
55+
)),
56+
nonce_group,
57+
k2_pow,
58+
}
59+
}
4060
}
4161

4262
#[cfg(test)]

src/compression.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub(crate) fn required_bits(value: u64) -> usize {
2424
}
2525

2626
#[cfg(test)]
27+
#[allow(clippy::unusual_byte_groupings)]
2728
mod tests {
2829
use super::*;
2930
use itertools::max;

src/difficulty.rs

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
1+
use eyre::Context;
2+
13
/// Calculate proving difficulty.
4+
///
5+
/// K1 defines how many good labels are expected to be within all the labels.
26
/// The lower the value of K1 - the more difficult it will be to find a good label.
3-
pub(crate) fn proving_difficulty(num_labels: u64, k1: u32) -> eyre::Result<u64> {
7+
///
8+
/// The difficulty is calculated as:
9+
/// difficulty = 2^64 * K1 / num_labels
10+
pub(crate) fn proving_difficulty(k1: u32, num_labels: u64) -> eyre::Result<u64> {
411
eyre::ensure!(num_labels > 0, "number of label blocks must be > 0");
512
eyre::ensure!(
6-
num_labels >= k1 as u64,
7-
format!("k1 ({k1}) cannot be bigger than the number of labels ({num_labels})")
13+
num_labels > k1 as u64,
14+
format!("number of labels ({num_labels}) must be bigger than k1 ({k1})")
815
);
16+
let difficulty = (1u128 << 64) * k1 as u128 / num_labels as u128;
17+
u64::try_from(difficulty).wrap_err("difficulty doesn't fit in u64")
18+
}
19+
20+
#[test]
21+
fn zero_labels() {
22+
assert!(proving_difficulty(1, 0).is_err());
23+
}
924

10-
let x = u64::MAX / num_labels;
11-
let y = u64::MAX % num_labels;
12-
Ok(x * k1 as u64 + y * k1 as u64 / num_labels)
25+
#[test]
26+
fn too_big_k1() {
27+
assert!(proving_difficulty(2, 1).is_err());
28+
assert!(proving_difficulty(1, 1).is_err());
1329
}
1430

1531
#[test]
16-
fn zero_blocks() {
17-
assert!(proving_difficulty(0, 1).is_err());
32+
fn difficulty_calculation() {
33+
assert_eq!(proving_difficulty(1, 2).unwrap(), 1u64 << 63);
34+
assert_eq!(proving_difficulty(1, 4).unwrap(), 1u64 << (64 - 2));
35+
assert_eq!(proving_difficulty(1, 128).unwrap(), 1u64 << (64 - 7));
1836
}

src/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use serde_with::serde_as;
77
const METADATA_FILE_NAME: &str = "postdata_metadata.json";
88

99
#[serde_as]
10-
#[derive(Debug, Deserialize, Serialize, Clone)]
10+
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
1111
#[serde(rename_all = "PascalCase")]
1212
pub struct PostMetadata {
1313
#[serde_as(as = "Base64")]

src/pow.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ mod tests {
7878
proptest! {
7979
#[test]
8080
fn test_k2_pow(nonce: u32) {
81-
let difficulty = 0x7FFFFFFF_FFFFFFFF;
81+
let difficulty = 0x7FFF_FFFF_FFFF_FFFF;
8282
let k2_pow = find_k2_pow(&[0; 32], nonce, ScryptParams::new(2,0,0), difficulty);
8383
assert!(hash_k2_pow(&[0; 32], nonce, ScryptParams::new(2,0,0), k2_pow) < difficulty);
8484
}
8585

8686
#[test]
8787
fn test_k3_pow(nonce: u32, k2_pow: u64, indexes: [u8; 64]) {
88-
let difficulty = 0x7FFFFFFF_FFFFFFFF;
88+
let difficulty = 0x7FFF_FFFF_FFFF_FFFF;
8989
let k3_pow = find_k3_pow(&[0; 32], nonce, &indexes, ScryptParams::new(2,0,0), difficulty, k2_pow);
9090
assert!(hash_k3_pow(&[0; 32], nonce, &indexes, ScryptParams::new(2,0,0), k2_pow, k3_pow) < difficulty);
9191
}

0 commit comments

Comments
 (0)