Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into 139-basic-service-ope…
Browse files Browse the repository at this point in the history
…rator-api
  • Loading branch information
poszu committed Nov 10, 2023
2 parents b30c5aa + 7d5b235 commit 378836a
Show file tree
Hide file tree
Showing 26 changed files with 579 additions and 352 deletions.
37 changes: 31 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = [".", "ffi", "scrypt-ocl", "initializer", "profiler", "service"]

[package]
name = "post-rs"
version = "0.5.1"
version = "0.5.2"
edition = "2021"

[lib]
Expand Down
68 changes: 39 additions & 29 deletions benches/verifying.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,59 @@
use std::sync::atomic::AtomicBool;

use criterion::{criterion_group, criterion_main, Criterion};
use post::{
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 scrypt_jane::scrypt::ScryptParams;
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],
scrypt: ScryptParams::new(12, 0, 0),
};
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
2 changes: 1 addition & 1 deletion ffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "post-cbindings"
version = "0.5.1"
version = "0.5.2"
edition = "2021"


Expand Down
29 changes: 9 additions & 20 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 @@ -420,32 +416,25 @@ 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(0, 0, 0),
};

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
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);

// 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);
}
}
Loading

0 comments on commit 378836a

Please sign in to comment.