Skip to content

Commit

Permalink
Merge pull request #143 from spacemeshos/change-scrypt-params-in-config
Browse files Browse the repository at this point in the history
Use N,R,P scrypt params instead of factors in config
  • Loading branch information
poszu authored Nov 2, 2023
2 parents cd9b2a7 + 3cf5917 commit 3016155
Show file tree
Hide file tree
Showing 16 changed files with 95 additions and 75 deletions.
5 changes: 2 additions & 3 deletions benches/verifying.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use criterion::{criterion_group, criterion_main, Criterion};
use post::{
config::ScryptParams,
metadata::ProofMetadata,
pow::randomx::{PoW, RandomXFlag},
prove::Proof,
Expand All @@ -8,8 +9,6 @@ use post::{
#[cfg(not(windows))]
use pprof::criterion::{Output, PProfProfiler};

use scrypt_jane::scrypt::ScryptParams;

fn verifying(c: &mut Criterion) {
let challenge = b"hello world, challenge me!!!!!!!";
let metadata = ProofMetadata {
Expand Down Expand Up @@ -37,7 +36,7 @@ fn verifying(c: &mut Criterion) {
k2,
k3,
pow_difficulty: [0xFF; 32],
scrypt: ScryptParams::new(12, 0, 0),
scrypt: ScryptParams::new(8192, 1, 1),
};

c.bench_function("verify", |b| {
Expand Down
16 changes: 6 additions & 10 deletions ffi/src/initialization.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{error::Error, ffi::c_char, fmt::Debug};

use post::{
config::ScryptParams,
initialize::{CpuInitializer, Initialize},
pos_verification::VerificationError,
ScryptParams,
};
use scrypt_ocl::{ocl::DeviceType, OpenClInitializer, ProviderId};

Expand Down Expand Up @@ -186,11 +186,7 @@ fn _new_initializer(
};

let instance: Box<dyn Initialize> = match provider_id {
CPU_PROVIDER_ID => Box::new(CpuInitializer::new(ScryptParams::new(
n.ilog2() as u8 - 1,
0,
0,
))),
CPU_PROVIDER_ID => Box::new(CpuInitializer::new(ScryptParams::new(n, 1, 1))),
id => Box::new(OpenClInitializer::new(
Some(ProviderId(id)),
n,
Expand Down Expand Up @@ -260,8 +256,8 @@ mod tests {
};

use post::{
config::ScryptParams,
initialize::{CpuInitializer, Initialize, MockInitialize},
ScryptParams,
};
use tempfile::tempdir;

Expand Down Expand Up @@ -299,7 +295,7 @@ mod tests {

let mut expected = Vec::<u8>::with_capacity(indices.clone().count());

CpuInitializer::new(ScryptParams::new(4, 0, 0))
CpuInitializer::new(ScryptParams::new(32, 1, 1))
.initialize_to(
&mut expected,
&[0u8; 32],
Expand Down Expand Up @@ -426,7 +422,7 @@ mod tests {
k2: 32,
k3: 10,
pow_difficulty: [0xFF; 32],
scrypt: ScryptParams::new(0, 0, 0),
scrypt: ScryptParams::new(2, 1, 1),
};

CpuInitializer::new(cfg.scrypt)
Expand All @@ -439,7 +435,7 @@ mod tests {
assert_eq!(VerifyResult::Ok, result);

// verify with wrong scrypt params
let wrong_scrypt = ScryptParams::new(2, 0, 0);
let wrong_scrypt = ScryptParams::new(4, 1, 1);
let result = verify_pos(datapath.as_ptr(), null(), null(), 100.0, wrong_scrypt);
assert_eq!(VerifyResult::Invalid, result);

Expand Down
16 changes: 9 additions & 7 deletions ffi/src/post_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ use std::{
sync::atomic::AtomicBool,
};

pub use post::config::Config;
pub use post::metadata::ProofMetadata;
pub use post::ScryptParams;
use post::{
config::Config,
metadata::ProofMetadata,
pow::randomx::{PoW, RandomXFlag},
prove,
verification::{Verifier, VerifyingParams},
Expand Down Expand Up @@ -208,7 +207,10 @@ pub unsafe extern "C" fn verify_proof(

#[cfg(test)]
mod tests {
use post::{initialize::Initialize, metadata::ProofMetadata, pow::randomx::RandomXFlag};
use post::{
config::ScryptParams, initialize::Initialize, metadata::ProofMetadata,
pow::randomx::RandomXFlag,
};

#[test]
fn datadir_must_be_utf8() {
Expand All @@ -218,7 +220,7 @@ mod tests {
k2: 20,
k3: 20,
pow_difficulty: [0xFF; 32],
scrypt: super::ScryptParams::new(1, 1, 1),
scrypt: ScryptParams::new(2, 1, 1),
};
let result = super::_generate_proof(
datadir.as_ptr(),
Expand Down Expand Up @@ -256,7 +258,7 @@ mod tests {
k2: 2,
k3: 2,
pow_difficulty: [0xFF; 32],
scrypt: super::ScryptParams::new(1, 0, 0),
scrypt: ScryptParams::new(2, 1, 1),
},
)
};
Expand All @@ -278,7 +280,7 @@ mod tests {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
],
scrypt: post::ScryptParams::new(0, 0, 0),
scrypt: ScryptParams::new(2, 1, 1),
};

let meta = post::initialize::CpuInitializer::new(cfg.scrypt)
Expand Down
10 changes: 3 additions & 7 deletions initializer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use base64::{engine::general_purpose, Engine};
use clap::{Args, Parser, Subcommand, ValueEnum};
use eyre::Context;
use post::{
config::ScryptParams,
initialize::{CpuInitializer, Initialize, LABEL_SIZE},
ScryptParams,
};
use rand::seq::IteratorRandom;
use rayon::prelude::{ParallelBridge, ParallelIterator};
Expand Down Expand Up @@ -128,7 +128,7 @@ fn verify_data(args: VerifyData) -> eyre::Result<()> {
let input_file_size = input_file.metadata()?.len();
let labels_in_file = input_file_size / 16;
let labels_to_verify = (labels_in_file as f64 * (args.fraction / 100.0)) as usize;
let scrypt_params = ScryptParams::new(args.n.ilog2() as u8 - 1, 0, 0);
let scrypt_params = ScryptParams::new(args.n, 1, 1);

let mut rng = rand::thread_rng();
(0..labels_in_file)
Expand Down Expand Up @@ -171,11 +171,7 @@ fn initialize(args: InitializeArgs) -> eyre::Result<()> {
eyre::ensure!(args.n.is_power_of_two(), "scrypt N must be a power of two");

let mut initializer: Box<dyn Initialize> = match args.method {
InitializationMethod::Cpu => Box::new(CpuInitializer::new(ScryptParams::new(
args.n.ilog2() as u8 - 1,
0,
0,
))),
InitializationMethod::Cpu => Box::new(CpuInitializer::new(ScryptParams::new(args.n, 1, 1))),
InitializationMethod::Gpu => Box::new(OpenClInitializer::new(
args.provider.map(ProviderId),
args.n,
Expand Down
14 changes: 7 additions & 7 deletions scrypt-ocl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ impl Initialize for OpenClInitializer {
#[cfg(test)]
mod tests {
use post::{
config::ScryptParams,
initialize::{CpuInitializer, Initialize},
ScryptParams,
};
use rstest::rstest;

Expand Down Expand Up @@ -386,7 +386,7 @@ mod tests {
.unwrap();

let mut expected = Vec::with_capacity(1);
CpuInitializer::new(ScryptParams::new(12, 0, 0))
CpuInitializer::new(ScryptParams::new(8192, 1, 1))
.initialize_to(&mut expected, &[0u8; 32], 0..1, None)
.unwrap();

Expand All @@ -411,7 +411,7 @@ mod tests {
let mut expected =
Vec::<u8>::with_capacity(usize::try_from(indices.end - indices.start).unwrap());

CpuInitializer::new(ScryptParams::new(n.ilog2() as u8 - 1, 0, 0))
CpuInitializer::new(ScryptParams::new(n, 1, 1))
.initialize_to(&mut expected, &[0u8; 32], indices, None)
.unwrap();

Expand All @@ -436,7 +436,7 @@ mod tests {
let mut expected =
Vec::<u8>::with_capacity(usize::try_from(indices.end - indices.start).unwrap());

CpuInitializer::new(ScryptParams::new(n.ilog2() as u8 - 1, 0, 0))
CpuInitializer::new(ScryptParams::new(n, 1, 1))
.initialize_to(&mut expected, &[0u8; 32], indices, None)
.unwrap();

Expand All @@ -457,7 +457,7 @@ mod tests {
let mut expected =
Vec::<u8>::with_capacity(usize::try_from(indices.end - indices.start).unwrap());

CpuInitializer::new(ScryptParams::new(12, 0, 0))
CpuInitializer::new(ScryptParams::new(8192, 1, 1))
.initialize_to(&mut expected, commitment, indices, None)
.unwrap();

Expand Down Expand Up @@ -485,7 +485,7 @@ mod tests {
let nonce = opencl_nonce.expect("vrf nonce not found");

let mut label = Vec::<u8>::with_capacity(LABEL_SIZE);
let mut cpu_initializer = CpuInitializer::new(ScryptParams::new(n.ilog2() as u8 - 1, 0, 0));
let mut cpu_initializer = CpuInitializer::new(ScryptParams::new(n, 1, 1));
cpu_initializer
.initialize_to(&mut label, commitment, nonce.index..nonce.index + 1, None)
.unwrap();
Expand Down Expand Up @@ -525,7 +525,7 @@ mod tests {
let mut expected =
Vec::<u8>::with_capacity(usize::try_from(indices.end - indices.start).unwrap());

CpuInitializer::new(ScryptParams::new(N.ilog2() as u8 - 1, 0, 0))
CpuInitializer::new(ScryptParams::new(N, 1, 1))
.initialize_to(&mut expected, &[0u8; 32], indices, None)
.unwrap();

Expand Down
8 changes: 4 additions & 4 deletions service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ async fn main() -> eyre::Result<()> {
k2: args.post_config.k2,
k3: args.post_config.k3,
pow_difficulty: args.post_config.pow_difficulty,
scrypt: post::ScryptParams::new(
args.post_config.scrypt.n.ilog2() as u8 - 1,
args.post_config.scrypt.r.ilog2() as u8,
args.post_config.scrypt.p.ilog2() as u8,
scrypt: post::config::ScryptParams::new(
args.post_config.scrypt.n,
args.post_config.scrypt.r,
args.post_config.scrypt.p,
),
},
args.post_settings.nonces,
Expand Down
2 changes: 1 addition & 1 deletion service/tests/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ async fn test_get_metadata(#[case] vrf_difficulty: Option<[u8; 32]>) {
k2: 32,
k3: 10,
pow_difficulty: [0xFF; 32],
scrypt: post::ScryptParams::new(0, 0, 0),
scrypt: post::config::ScryptParams::new(2, 1, 1),
};

let metadata = CpuInitializer::new(cfg.scrypt)
Expand Down
8 changes: 4 additions & 4 deletions service/tests/test_service.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{thread::sleep, time::Duration};

use post::{
config::ScryptParams,
initialize::{CpuInitializer, Initialize},
metadata::ProofMetadata,
pow::randomx::RandomXFlag,
ScryptParams,
};
use post_service::{client::PostService, service::ProofGenState};

Expand All @@ -19,7 +19,7 @@ fn test_generate_and_verify() {
k2: 4,
k3: 4,
pow_difficulty: [0xFF; 32],
scrypt: ScryptParams::new(0, 0, 0),
scrypt: ScryptParams::new(2, 1, 1),
};

let metadata = CpuInitializer::new(cfg.scrypt)
Expand Down Expand Up @@ -65,7 +65,7 @@ fn reject_invalid_challenge() {
k2: 4,
k3: 4,
pow_difficulty: [0xFF; 32],
scrypt: ScryptParams::new(0, 0, 0),
scrypt: ScryptParams::new(2, 1, 1),
};

CpuInitializer::new(cfg.scrypt)
Expand Down Expand Up @@ -103,7 +103,7 @@ fn cannot_run_parallel_proof_gens() {
k2: 4,
k3: 4,
pow_difficulty: [0xFF; 32],
scrypt: ScryptParams::new(0, 0, 0),
scrypt: ScryptParams::new(2, 1, 1),
};

CpuInitializer::new(cfg.scrypt)
Expand Down
30 changes: 29 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,33 @@ pub struct Config {
/// `pow` for [Proof][crate::prove::Proof].
pub pow_difficulty: [u8; 32],
/// Scrypt paramters for initilizing labels
pub scrypt: scrypt_jane::scrypt::ScryptParams,
pub scrypt: ScryptParams,
}

#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct ScryptParams {
pub n: usize,
pub r: usize,
pub p: usize,
}

impl ScryptParams {
pub fn new(n: usize, r: usize, p: usize) -> Self {
assert!(n >= 2);
assert!(n.is_power_of_two());
assert!(r.is_power_of_two());
assert!(p.is_power_of_two());
Self { n, r, p }
}
}

impl From<ScryptParams> for scrypt_jane::scrypt::ScryptParams {
fn from(params: ScryptParams) -> Self {
Self::new(
params.n.ilog2() as u8 - 1,
params.r.ilog2() as u8,
params.p.ilog2() as u8,
)
}
}
16 changes: 8 additions & 8 deletions src/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use std::{

use mockall::automock;
use rayon::prelude::{IntoParallelIterator, ParallelIterator};
use scrypt_jane::scrypt::{scrypt, ScryptParams};
use scrypt_jane::scrypt::scrypt;

use crate::metadata::PostMetadata;
use crate::{config::ScryptParams, metadata::PostMetadata};

pub const LABEL_SIZE: usize = 16;
pub const ENTIRE_LABEL_SIZE: usize = 32;
Expand Down Expand Up @@ -115,7 +115,7 @@ impl Initialize for CpuInitializer {
let mut scrypt_data = [0u8; 72];
scrypt_data[0..32].copy_from_slice(commitment);
scrypt_data[32..40].copy_from_slice(&index.to_le_bytes());
scrypt(&scrypt_data, &[], self.scrypt_params, &mut label);
scrypt(&scrypt_data, &[], self.scrypt_params.into(), &mut label);
label
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -168,7 +168,7 @@ mod tests {

let mut pos_file = tempfile::tempfile().unwrap();
let commitment = [0u8; 32];
let scrypt_params = ScryptParams::new(1, 0, 0);
let scrypt_params = ScryptParams::new(4, 1, 1);
CpuInitializer::new(scrypt_params)
.initialize_to(&mut pos_file, &commitment, labels, None)
.unwrap();
Expand All @@ -178,7 +178,7 @@ mod tests {

#[test]
fn test_initialize_fits_in_single_file() {
let scrypt_params = ScryptParams::new(1, 0, 0);
let scrypt_params = ScryptParams::new(4, 1, 1);
let data_dir = tempfile::tempdir().unwrap();
let data_path = data_dir.path();
CpuInitializer::new(scrypt_params)
Expand All @@ -201,7 +201,7 @@ mod tests {

#[test]
fn test_initialize_returns_metadata() {
let scrypt_params = ScryptParams::new(1, 0, 0);
let scrypt_params = ScryptParams::new(4, 1, 1);
let data_dir = tempfile::tempdir().unwrap();
let node_id = rand::random::<[u8; 32]>();
let commitment_atx_id = rand::random::<[u8; 32]>();
Expand All @@ -228,7 +228,7 @@ mod tests {

#[test]
fn test_initialize_split_many_files() {
let scrypt_params = ScryptParams::new(1, 0, 0);
let scrypt_params = ScryptParams::new(4, 1, 1);
let data_dir = tempfile::tempdir().unwrap();
let data_path = data_dir.path();
CpuInitializer::new(scrypt_params)
Expand Down Expand Up @@ -262,7 +262,7 @@ mod tests {

#[test]
fn initialization_to_many_files_gives_same_result_as_single_file() {
let scrypt_params = ScryptParams::new(1, 0, 0);
let scrypt_params = ScryptParams::new(4, 1, 1);
let data_dir = tempfile::tempdir().unwrap();
let data_path = data_dir.path();

Expand Down
Loading

0 comments on commit 3016155

Please sign in to comment.