Skip to content

Commit

Permalink
Merge pull request #87 from okx/feature/ruanpc/non_neg_user_check
Browse files Browse the repository at this point in the history
Feature/ruanpc/non neg user check
  • Loading branch information
RUAN0007 authored Sep 25, 2024
2 parents c14cac9 + 876ae39 commit 3af083e
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 10 deletions.
75 changes: 75 additions & 0 deletions crates/zk-por-cli/src/checker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use super::{constant::DEFAULT_BATCH_SIZE, prover::calculate_per_parse_account_num};
use plonky2_field::types::{Field, PrimeField64};

use std::str::FromStr;
use zk_por_core::{
config::ProverConfig,
error::PoRError,
parser::{AccountParser, FileAccountReader, FileManager, FilesCfg},
types::F,
};
use zk_por_tracing::{init_tracing, TraceConfig};

pub fn check_non_neg_user(cfg: ProverConfig) -> Result<(), PoRError> {
let trace_cfg: TraceConfig = cfg.log.into();
let _g = init_tracing(trace_cfg);

let batch_size = cfg.prover.batch_size.unwrap_or(DEFAULT_BATCH_SIZE);
let file_manager = FileManager {};
let mut account_parser = FileAccountReader::new(
FilesCfg {
dir: std::path::PathBuf::from_str(&cfg.prover.user_data_path).unwrap(),
batch_size: batch_size,
tokens: cfg.prover.tokens.clone(),
},
&file_manager,
);
account_parser.log_state();
let mut offset = 0;
let batch_prove_threads_num = cfg.prover.batch_prove_threads_num;
let per_parse_account_num =
calculate_per_parse_account_num(batch_size, batch_prove_threads_num);

let batch_num = account_parser.total_num_of_users().div_ceil(batch_size);
let token_num = cfg.prover.tokens.len();
let mut parse_num = 0;

tracing::info!(
"start to check {} accounts with {} tokens, {} batch size",
account_parser.total_num_of_users(),
token_num,
batch_size,
);

while offset < account_parser.total_num_of_users() {
let accounts = account_parser.read_n_accounts(offset, per_parse_account_num, &file_manager);
let account_num = accounts.len();

tracing::info!(
"parse {} times, with number of accounts {}, number of batches {}",
parse_num,
account_num,
batch_num,
);
parse_num += 1;
tracing::info!("finish checking {} accounts", offset);
offset += per_parse_account_num;

for account in accounts {
let equity_sum =
account.equity.iter().fold(F::ZERO, |acc, x| acc + *x).to_canonical_u64();
let debt_sum = account.debt.iter().fold(F::ZERO, |acc, x| acc + *x).to_canonical_u64();

if equity_sum < debt_sum {
tracing::error!(
"account {} has negative equity, the equity sum {}, the debt sum {}",
account.id,
equity_sum,
debt_sum
);
return Err(PoRError::InvalidUser);
}
}
}
Ok(())
}
1 change: 1 addition & 0 deletions crates/zk-por-cli/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod checker;
pub mod constant;
pub mod prover;
pub mod verifier;
30 changes: 28 additions & 2 deletions crates/zk-por-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{path::PathBuf, str::FromStr};

use clap::{Parser, Subcommand};
use zk_por_cli::{
checker::check_non_neg_user,
constant::{DEFAULT_USER_PROOF_FILE_PATTERN, GLOBAL_PROOF_FILENAME},
prover::prove,
verifier::{verify_global, verify_user},
Expand All @@ -27,6 +28,11 @@ pub enum ZkPorCommitCommands {
#[arg(short, long)]
output_path: String, // path to output file
},
CheckNonNegUser {
#[arg(short, long)]
cfg_path: String, // path to config file
},

VerifyGlobal {
#[arg(short, long)]
proof_path: String,
Expand All @@ -51,6 +57,13 @@ impl Execute for Option<ZkPorCommitCommands> {
prove(prover_cfg, output_path)
}

Some(ZkPorCommitCommands::CheckNonNegUser { cfg_path }) => {
let cfg = zk_por_core::config::ProverConfig::load(&cfg_path)
.map_err(|e| PoRError::ConfigError(e))?;
let prover_cfg = cfg.try_deserialize().unwrap();
check_non_neg_user(prover_cfg)
}

Some(ZkPorCommitCommands::VerifyGlobal { proof_path: global_proof_path }) => {
let global_proof_path = PathBuf::from_str(&global_proof_path).unwrap();
verify_global(global_proof_path, true)
Expand All @@ -66,8 +79,21 @@ impl Execute for Option<ZkPorCommitCommands> {

None => {
println!("============Validation started============");
let global_proof_path = PathBuf::from_str(GLOBAL_PROOF_FILENAME).unwrap();
let user_proof_path_pattern = DEFAULT_USER_PROOF_FILE_PATTERN.to_owned();
let exec_parent_path = std::env::current_exe()
.expect("fail to get current exe path")
.parent()
.unwrap()
.to_path_buf();

// join the dir path and GLOBAL_PROOF_FILENAME
let global_proof_path = exec_parent_path.join(GLOBAL_PROOF_FILENAME);

let user_proof_path_pattern = exec_parent_path
.join(DEFAULT_USER_PROOF_FILE_PATTERN)
.to_str()
.unwrap()
.to_string();

if verify_global(global_proof_path.clone(), false).is_ok() {
println!("Total sum and non-negative constraint validation passed")
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/zk-por-cli/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use zk_por_core::{
use zk_por_tracing::{init_tracing, TraceConfig};

// as we use one thread to prove each batch, we load num_cpus batches to increase the parallelism.
fn calculate_per_parse_account_num(batch_size: usize, threads_num: usize) -> usize {
pub fn calculate_per_parse_account_num(batch_size: usize, threads_num: usize) -> usize {
let num_cpus = num_cpus::get();
let num_cpus = if threads_num < num_cpus { threads_num } else { num_cpus };
num_cpus * batch_size
Expand Down
7 changes: 0 additions & 7 deletions crates/zk-por-core/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,6 @@ impl GlobalMst {
/// `recursive_level` count from bottom to top; recursive_level = 1 means the bottom layer; increase whilve moving to the top.
pub fn set_recursive_hash(&mut self, recursive_level: usize, index: usize, hash: HashOut<F>) {
let idx = GlobalMst::get_recursive_global_index(&self.cfg, recursive_level, index);
tracing::debug!(
"set_recursive_hash, recursive_level: {:?}, index: {:?}, hash: {:?}, idx: {:?}",
recursive_level,
index,
hash,
idx,
);
self.inner[idx] = hash;
}

Expand Down
7 changes: 7 additions & 0 deletions docs/release.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Config
Edit `config/default.toml` such that `user_data_path` is the directory containing the user files only. Currently, it is set to `./sample_data` for demo.

# Check
check all user accounst are non-negative
```
cfg_dir_path="config"
./zk-por-checker check-non-neg-user --cfg-path "${cfg_dir_path}" --output-path "${output_proof_dir_path}"
```

# Prove
```
cfg_dir_path="config"
Expand Down
2 changes: 2 additions & 0 deletions scripts/release_linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ mkdir -p release/{config,sample_data}

RUSTFLAGS="-C target-feature=+crt-static" cargo build --release --target x86_64-unknown-linux-gnu
mv target/x86_64-unknown-linux-gnu/release/zk-por-cli release/zk-por-prover
cp release/zk-por-prover release/zk-por-checker

RUSTFLAGS="-C target-feature=+crt-static" cargo build --features zk-por-core/verifier --release --target x86_64-unknown-linux-gnu
mv target/x86_64-unknown-linux-gnu/release/zk-por-cli release/zk-por-verifier

Expand Down
18 changes: 18 additions & 0 deletions scripts/release_mac_arm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
rm -rf release
mkdir -p release/{config,sample_data}

cargo build --release
mv target/release/zk-por-cli release/zk-por-prover
cp release/zk-por-prover release/zk-por-checker

cargo build --features zk-por-core/verifier --release
mv target/release/zk-por-cli release/zk-por-verifier

mkdir -p release/config
sed 's|/opt/data/zkpor/users/|./sample_data/|g' config/default.toml > release/config/default.toml

mkdir -p release/sample_data
cp -r test-data/batch0.json release/sample_data
cp docs/release.md release/README.md

tar -cvf zk-por-mac-arm.tar ./release/

0 comments on commit 3af083e

Please sign in to comment.