Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Config into ProofConfig and InitConfig #145

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 38 additions & 27 deletions benches/verifying.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,59 @@
use std::sync::atomic::AtomicBool;

use criterion::{criterion_group, criterion_main, Criterion};
use post::{
config::ScryptParams,
config::{InitConfig, ProofConfig, ScryptParams},
initialize::{CpuInitializer, Initialize},
metadata::ProofMetadata,
pow::randomx::{PoW, RandomXFlag},
prove::Proof,
verification::{Verifier, VerifyingParams},
prove::generate_proof,
verification::Verifier,
};
#[cfg(not(windows))]
use pprof::criterion::{Output, PProfProfiler};
use tempfile::tempdir;

fn verifying(c: &mut Criterion) {
let challenge = b"hello world, challenge me!!!!!!!";
let metadata = ProofMetadata {
node_id: [0u8; 32],
commitment_atx_id: [0u8; 32],
challenge: *challenge,
num_units: 1,
labels_per_unit: 1024 * 1024 * 1024,
};
let num_labels = metadata.num_units as u64 * metadata.labels_per_unit;

let verifier = Verifier::new(Box::new(
PoW::new(RandomXFlag::get_recommended_flags()).unwrap(),
));
let datadir = tempdir().unwrap();

let (k2, k3) = (37, 37);
let proof = Proof::new(
0,
(0..k2 as u64).collect::<Vec<u64>>().as_slice(),
num_labels,
0,
);
let params = VerifyingParams {
difficulty: u64::MAX,
k2,
k3,
let cfg = ProofConfig {
k1: 199,
k2: 37,
k3: 37,
pow_difficulty: [0xFF; 32],
};
let init_cfg = InitConfig {
min_num_units: 1,
max_num_units: 1,
labels_per_unit: 200,
scrypt: ScryptParams::new(8192, 1, 1),
};

let metadata = CpuInitializer::new(init_cfg.scrypt)
.initialize(
datadir.path(),
&[0u8; 32],
&[0u8; 32],
init_cfg.labels_per_unit,
1,
init_cfg.labels_per_unit,
None,
)
.unwrap();

let pow_flags = RandomXFlag::get_recommended_flags();
// Generate a proof
let stop = AtomicBool::new(false);
let proof = generate_proof(datadir.path(), challenge, cfg, 32, 1, pow_flags, stop).unwrap();
let metadata = ProofMetadata::new(metadata, *challenge);

// Bench verifying the proof
let verifier = Verifier::new(Box::new(PoW::new(pow_flags).unwrap()));
c.bench_function("verify", |b| {
b.iter(|| {
verifier
.verify(&proof, &metadata, params)
.verify(&proof, &metadata, &cfg, &init_cfg)
.expect("proof should be valid");
});
});
Expand Down
15 changes: 4 additions & 11 deletions ffi/src/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,22 +416,15 @@ mod tests {
fn initialize_and_verify() {
// Initialize some data
let datadir = tempdir().unwrap();
let scrypt = ScryptParams::new(2, 1, 1);

let cfg = post::config::Config {
k1: 23,
k2: 32,
k3: 10,
pow_difficulty: [0xFF; 32],
scrypt: ScryptParams::new(2, 1, 1),
};

CpuInitializer::new(cfg.scrypt)
CpuInitializer::new(scrypt)
.initialize(datadir.path(), &[0u8; 32], &[0u8; 32], 256, 31, 700, None)
.unwrap();

// Verify the data
let datapath = CString::new(datadir.path().to_str().unwrap()).unwrap();
let result = verify_pos(datapath.as_ptr(), null(), null(), 100.0, cfg.scrypt);
let result = verify_pos(datapath.as_ptr(), null(), null(), 100.0, scrypt);
assert_eq!(VerifyResult::Ok, result);

// verify with wrong scrypt params
Expand All @@ -441,7 +434,7 @@ mod tests {

// verify with non-existent path
let path = CString::new("non-existent-path").unwrap();
let result = verify_pos(path.as_ptr(), null(), null(), 100.0, cfg.scrypt);
let result = verify_pos(path.as_ptr(), null(), null(), 100.0, scrypt);
assert_eq!(VerifyResult::Failed, result);
}
}
57 changes: 28 additions & 29 deletions ffi/src/post_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use std::{
};

use post::{
config::Config,
config::{InitConfig, ProofConfig},
metadata::ProofMetadata,
pow::randomx::{PoW, RandomXFlag},
prove,
verification::{Verifier, VerifyingParams},
verification::Verifier,
};

use crate::ArrayU8;
Expand Down Expand Up @@ -72,7 +72,7 @@ pub unsafe extern "C" fn free_proof(proof: *mut Proof) {
pub extern "C" fn generate_proof(
datadir: *const c_char,
challenge: *const c_uchar,
cfg: Config,
cfg: ProofConfig,
nonces: usize,
threads: usize,
pow_flags: RandomXFlag,
Expand All @@ -90,7 +90,7 @@ pub extern "C" fn generate_proof(
fn _generate_proof(
datadir: *const c_char,
challenge: *const c_uchar,
cfg: Config,
cfg: ProofConfig,
nonces: usize,
threads: usize,
pow_flags: RandomXFlag,
Expand Down Expand Up @@ -168,7 +168,8 @@ pub unsafe extern "C" fn verify_proof(
verifier: *const Verifier,
proof: Proof,
metadata: *const ProofMetadata,
cfg: Config,
cfg: ProofConfig,
init_cfg: InitConfig,
) -> VerifyResult {
let verifier = match verifier.as_ref() {
Some(verifier) => verifier,
Expand All @@ -191,12 +192,7 @@ pub unsafe extern "C" fn verify_proof(
None => return VerifyResult::InvalidArgument,
};

let params = match VerifyingParams::new(metadata, &cfg) {
Ok(params) => params,
Err(_) => return VerifyResult::InvalidArgument,
};

match verifier.verify(&proof, metadata, params) {
match verifier.verify(&proof, metadata, &cfg, &init_cfg) {
Ok(_) => VerifyResult::Ok,
Err(err) => {
log::error!("Proof is invalid: {err}");
Expand All @@ -215,12 +211,11 @@ mod tests {
#[test]
fn datadir_must_be_utf8() {
let datadir = std::ffi::CString::new([159, 146, 150]).unwrap();
let cfg = super::Config {
let cfg = super::ProofConfig {
k1: 10,
k2: 20,
k3: 20,
pow_difficulty: [0xFF; 32],
scrypt: ScryptParams::new(2, 1, 1),
};
let result = super::_generate_proof(
datadir.as_ptr(),
Expand Down Expand Up @@ -253,11 +248,16 @@ mod tests {
pow: 0,
},
std::ptr::null(),
super::Config {
super::ProofConfig {
k1: 1,
k2: 2,
k3: 2,
pow_difficulty: [0xFF; 32],
},
super::InitConfig {
min_num_units: 1,
max_num_units: 1,
labels_per_unit: 1,
scrypt: ScryptParams::new(2, 1, 1),
},
)
Expand All @@ -268,10 +268,9 @@ mod tests {
#[test]
fn test_end_to_end() {
// Initialize some data first
let labels_per_unit = 200;
let datadir = tempfile::tempdir().unwrap();

let cfg = post::config::Config {
let cfg = post::config::ProofConfig {
k1: 10,
k2: 10,
k3: 10,
Expand All @@ -280,17 +279,23 @@ mod tests {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
],
};

let init_cfg = post::config::InitConfig {
min_num_units: 1,
max_num_units: 2,
labels_per_unit: 200,
scrypt: ScryptParams::new(2, 1, 1),
};

let meta = post::initialize::CpuInitializer::new(cfg.scrypt)
let meta = post::initialize::CpuInitializer::new(init_cfg.scrypt)
.initialize(
datadir.path(),
&[77; 32],
&[0u8; 32],
labels_per_unit,
init_cfg.labels_per_unit,
2,
labels_per_unit,
100,
None,
)
.unwrap();
Expand All @@ -316,17 +321,11 @@ mod tests {
pow_flags,
);

let proof_metadata = ProofMetadata {
node_id: meta.node_id,
commitment_atx_id: meta.commitment_atx_id,
challenge: *challenge,
num_units: meta.num_units,
labels_per_unit: meta.labels_per_unit,
let proof_metadata = ProofMetadata::new(meta, *challenge);
let result = unsafe {
crate::post_impl::verify_proof(verifier, *cproof, &proof_metadata, cfg, init_cfg)
};

let result =
unsafe { crate::post_impl::verify_proof(verifier, *cproof, &proof_metadata as _, cfg) };

assert_eq!(result, super::VerifyResult::Ok);

// Modify the proof to have different k2pow
Expand All @@ -338,7 +337,7 @@ mod tests {
};

let result = unsafe {
crate::post_impl::verify_proof(verifier, invalid_proof, &proof_metadata as _, cfg)
crate::post_impl::verify_proof(verifier, invalid_proof, &proof_metadata, cfg, init_cfg)
};
assert_eq!(result, super::VerifyResult::Invalid);

Expand Down
33 changes: 24 additions & 9 deletions service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,23 @@ struct Cli {
#[derive(Args, Debug)]
/// POST configuration - network parameters
struct PostConfig {
/// The minimal number of units that must be initialized.
#[arg(long, default_value_t = 4)]
pub min_num_units: u32,
/// The maximal number of units that can be initialized.
#[arg(long, default_value_t = u32::MAX)]
pub max_num_units: u32,
/// The number of labels per unit.
#[arg(long, default_value_t = 4294967296)]
pub labels_per_unit: u64,
/// K1 specifies the difficulty for a label to be a candidate for a proof
#[arg(long, default_value = "26")]
#[arg(long, default_value_t = 26)]
k1: u32,
/// K2 is the number of labels below the required difficulty required for a proof
#[arg(long, default_value = "37")]
#[arg(long, default_value_t = 37)]
k2: u32,
/// K3 is the size of the subset of proof indices that is validated
#[arg(long, default_value = "37")]
#[arg(long, default_value_t = 37)]
k3: u32,
/// difficulty for the nonce proof of work (aka "k2pow")
#[arg(
Expand Down Expand Up @@ -164,18 +173,24 @@ async fn main() -> eyre::Result<()> {
log::info!("POST network parameters: {:?}", args.post_config);
log::info!("POST proving settings: {:?}", args.post_settings);

let scrypt = post::config::ScryptParams::new(
args.post_config.scrypt.n,
args.post_config.scrypt.r,
args.post_config.scrypt.p,
);
let service = post_service::service::PostService::new(
args.dir,
post::config::Config {
post::config::ProofConfig {
k1: args.post_config.k1,
k2: args.post_config.k2,
k3: args.post_config.k3,
pow_difficulty: args.post_config.pow_difficulty,
scrypt: post::config::ScryptParams::new(
args.post_config.scrypt.n,
args.post_config.scrypt.r,
args.post_config.scrypt.p,
),
},
post::config::InitConfig {
min_num_units: args.post_config.min_num_units,
max_num_units: args.post_config.max_num_units,
labels_per_unit: args.post_config.labels_per_unit,
scrypt,
},
args.post_settings.nonces,
args.post_settings.threads,
Expand Down
11 changes: 7 additions & 4 deletions service/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use post::{
metadata::{PostMetadata, ProofMetadata},
pow::randomx::{PoW, RandomXFlag},
prove::Proof,
verification::{Verifier, VerifyingParams},
verification::Verifier,
};

#[derive(Debug)]
Expand All @@ -27,7 +27,8 @@ struct ProofGenProcess {

pub struct PostService {
datadir: PathBuf,
cfg: post::config::Config,
cfg: post::config::ProofConfig,
init_cfg: post::config::InitConfig,
nonces: usize,
threads: usize,
pow_flags: RandomXFlag,
Expand All @@ -40,7 +41,8 @@ pub struct PostService {
impl PostService {
pub fn new(
datadir: PathBuf,
cfg: post::config::Config,
cfg: post::config::ProofConfig,
init_cfg: post::config::InitConfig,
nonces: usize,
threads: usize,
pow_flags: RandomXFlag,
Expand All @@ -49,6 +51,7 @@ impl PostService {
proof_generation: Mutex::new(None),
datadir,
cfg,
init_cfg,
nonces,
threads,
pow_flags,
Expand Down Expand Up @@ -113,7 +116,7 @@ impl crate::client::PostService for PostService {

fn verify_proof(&self, proof: &Proof, metadata: &ProofMetadata) -> eyre::Result<()> {
self.verifier
.verify(proof, metadata, VerifyingParams::new(metadata, &self.cfg)?)
.verify(proof, metadata, &self.cfg, &self.init_cfg)
.wrap_err("verifying proof")
}

Expand Down
Loading
Loading