Skip to content

Commit

Permalink
Merge pull request #30 from spacemeshos/scale-pow-thresholds-by-numunits
Browse files Browse the repository at this point in the history
Scale K2 and K3 proofs of work thresholds by num units
  • Loading branch information
poszu committed Apr 4, 2023
2 parents dfb6355 + c88dd71 commit f6902f1
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 33 deletions.
2 changes: 1 addition & 1 deletion benches/proving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn prover_bench(c: &mut Criterion) {

let chunk_size = 64 * KIB;
let params = ProvingParams {
scrypt: ScryptParams::new(6, 0, 0),
pow_scrypt: ScryptParams::new(6, 0, 0),
difficulty: 0, // impossible to find a proof
k2_pow_difficulty: u64::MAX, // extremely easy to find k2_pow
k3_pow_difficulty: u64::MAX,
Expand Down
3 changes: 1 addition & 2 deletions ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ pub unsafe extern "C" fn verify_proof(
None => return VerifyResult::InvalidArgument,
};

let num_labels = metadata.num_units as u64 * metadata.labels_per_unit;
let params = match VerifyingParams::new(num_labels, cfg) {
let params = match VerifyingParams::new(metadata, &cfg) {
Ok(params) => params,
Err(_) => return VerifyResult::InvalidArgument,
};
Expand Down
72 changes: 54 additions & 18 deletions src/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
compression::{compress_indices, required_bits},
config::Config,
difficulty::proving_difficulty,
metadata,
metadata::{self, PostMetadata},
reader::read_data,
};

Expand Down Expand Up @@ -55,7 +55,19 @@ pub struct ProvingParams {
pub difficulty: u64,
pub k2_pow_difficulty: u64,
pub k3_pow_difficulty: u64,
pub scrypt: ScryptParams,
pub pow_scrypt: ScryptParams,
}

impl ProvingParams {
pub fn new(metadata: &PostMetadata, cfg: &Config) -> eyre::Result<Self> {
let num_labels = metadata.num_units as u64 * metadata.labels_per_unit;
Ok(Self {
difficulty: proving_difficulty(num_labels, cfg.k1)?,
k2_pow_difficulty: cfg.k2_pow_difficulty / metadata.num_units as u64,
k3_pow_difficulty: cfg.k3_pow_difficulty / metadata.num_units as u64,
pow_scrypt: cfg.pow_scrypt,
})
}
}

pub trait Prover {
Expand All @@ -77,7 +89,7 @@ impl ConstDProver {
let end = 1.max(nonces.end / 2);
ConstDProver {
ciphers: (start..end)
.map(|n| AesCipher::new(challenge, n, params.scrypt, params.k2_pow_difficulty))
.map(|n| AesCipher::new(challenge, n, params.pow_scrypt, params.k2_pow_difficulty))
.collect(),
difficulty: params.difficulty,
}
Expand Down Expand Up @@ -132,20 +144,11 @@ pub fn generate_proof(
threads: usize,
) -> eyre::Result<Proof> {
let metadata = metadata::load(datadir).wrap_err("loading metadata")?;

let num_labels = metadata.num_units as u64 * metadata.labels_per_unit;
let difficulty = proving_difficulty(num_labels, cfg.k1)?;
let params = ProvingParams::new(&metadata, &cfg)?;

let mut start_nonce = 0;
let mut end_nonce = start_nonce + nonces as u32;

let params = ProvingParams {
scrypt: cfg.pow_scrypt,
difficulty,
k2_pow_difficulty: cfg.k2_pow_difficulty,
k3_pow_difficulty: cfg.k3_pow_difficulty,
};

loop {
let indexes = Mutex::new(HashMap::<u32, Vec<u64>>::new());
let prover = ConstDProver::new(challenge, start_nonce..end_nonce, params.clone());
Expand Down Expand Up @@ -183,7 +186,7 @@ pub fn generate_proof(
challenge,
nonce,
&compressed_indices,
params.scrypt,
params.pow_scrypt,
params.k3_pow_difficulty,
prover.cipher(nonce).unwrap().k2_pow,
);
Expand All @@ -206,12 +209,45 @@ mod tests {
use rand::{thread_rng, RngCore};
use std::{collections::HashMap, iter::repeat};

/// Test that PoW thresholds are scaled with num_units.
#[test]
fn scaling_pows_thresholds() {
let cfg = Config {
k1: 32,
k2: 32,
k3: 10,
k2_pow_difficulty: u64::MAX / 100,
k3_pow_difficulty: u64::MAX / 8,
pow_scrypt: ScryptParams::new(1, 0, 0),
scrypt: ScryptParams::new(2, 0, 0),
};
let metadata = PostMetadata {
num_units: 10,
labels_per_unit: 100,
max_file_size: 1,
node_id: [0u8; 32],
commitment_atx_id: [0u8; 32],
nonce: None,
last_position: None,
};

let params = ProvingParams::new(&metadata, &cfg).unwrap();
assert_eq!(
cfg.k2_pow_difficulty / metadata.num_units as u64,
params.k2_pow_difficulty
);
assert_eq!(
cfg.k3_pow_difficulty / metadata.num_units as u64,
params.k3_pow_difficulty
);
}

#[test]
fn sanity() {
let (tx, rx) = std::sync::mpsc::channel();
let challenge = b"hello world, challenge me!!!!!!!";
let params = ProvingParams {
scrypt: ScryptParams::new(1, 0, 0),
pow_scrypt: ScryptParams::new(1, 0, 0),
difficulty: u64::MAX,
k2_pow_difficulty: u64::MAX,
k3_pow_difficulty: u64::MAX,
Expand Down Expand Up @@ -262,7 +298,7 @@ mod tests {
let mut start_nonce = 0;
let mut end_nonce = start_nonce + 20;
let params = ProvingParams {
scrypt: ScryptParams::new(1, 0, 0),
pow_scrypt: ScryptParams::new(1, 0, 0),
difficulty: proving_difficulty(NUM_LABELS as u64, K1).unwrap(),
k2_pow_difficulty: u64::MAX,
k3_pow_difficulty: u64::MAX,
Expand Down Expand Up @@ -319,7 +355,7 @@ mod tests {
let k1 = 1000;
let k2 = 1000;
let params = ProvingParams {
scrypt: ScryptParams::new(1, 0, 0),
pow_scrypt: ScryptParams::new(1, 0, 0),
difficulty: proving_difficulty(num_labels as u64, k1).unwrap(),
k2_pow_difficulty: u64::MAX,
k3_pow_difficulty: u64::MAX,
Expand Down Expand Up @@ -368,7 +404,7 @@ mod tests {
let k1 = 4;
let k2 = 32;
let params = ProvingParams {
scrypt: ScryptParams::new(8, 0, 0),
pow_scrypt: ScryptParams::new(8, 0, 0),
difficulty: proving_difficulty(num_labels as u64, k1).unwrap(),
k2_pow_difficulty: u64::MAX,
k3_pow_difficulty: u64::MAX,
Expand Down
8 changes: 5 additions & 3 deletions src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use scrypt_jane::scrypt::ScryptParams;
use crate::{
cipher::AesCipher,
compression::{decompress_indexes, required_bits},
config::Config,
difficulty::proving_difficulty,
initialize::{calc_commitment, generate_label},
metadata::ProofMetadata,
Expand All @@ -64,13 +65,14 @@ pub struct VerifyingParams {
}

impl VerifyingParams {
pub fn new(num_labels: u64, cfg: crate::config::Config) -> eyre::Result<Self> {
pub fn new(metadata: &ProofMetadata, cfg: &Config) -> eyre::Result<Self> {
let num_labels = metadata.num_units as u64 * metadata.labels_per_unit;
Ok(Self {
difficulty: proving_difficulty(num_labels, cfg.k1)?,
k2: cfg.k2,
k3: cfg.k3,
k2_pow_difficulty: cfg.k2_pow_difficulty,
k3_pow_difficulty: cfg.k3_pow_difficulty,
k2_pow_difficulty: cfg.k2_pow_difficulty / metadata.num_units as u64,
k3_pow_difficulty: cfg.k3_pow_difficulty / metadata.num_units as u64,
pow_scrypt: cfg.pow_scrypt,
scrypt: cfg.scrypt,
})
Expand Down
19 changes: 10 additions & 9 deletions tests/generate_and_verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,20 @@ fn test_generate_and_verify() {
.unwrap();

// Generate a proof
let proof = generate_proof(datadir.path(), challenge, cfg, 10, 0).unwrap();
let proof = generate_proof(datadir.path(), challenge, cfg, 10, 2).unwrap();

// Verify the proof
let metadata = ProofMetadata {
node_id: metadata.node_id,
commitment_atx_id: metadata.commitment_atx_id,
challenge: *challenge,
num_units: metadata.num_units,
labels_per_unit: metadata.labels_per_unit,
};
let valid = verify(
&proof,
&ProofMetadata {
node_id: metadata.node_id,
commitment_atx_id: metadata.commitment_atx_id,
challenge: *challenge,
num_units: metadata.num_units,
labels_per_unit: metadata.labels_per_unit,
},
VerifyingParams::new(labels_per_unit, cfg).unwrap(),
&metadata,
VerifyingParams::new(&metadata, &cfg).unwrap(),
0,
);

Expand Down

0 comments on commit f6902f1

Please sign in to comment.