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

feature:add-merkle-proof-circuit-and-tests #14

Merged
merged 20 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 18 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
8 changes: 4 additions & 4 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ once_cell = "1.14"
static_assertions = { version = "1.1.0", default-features = false }
unroll = { version = "0.1.5", default-features = false }
# zkp
plonky2 = { git = "https://github.com/okx/plonky2"}
plonky2_field = { git = "https://github.com/okx/plonky2"}
plonky2 = { git = "https://github.com/okx/plonky2", branch="clone-circuit"}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it has already been merged to main; i moved

plonky2_field = { git = "https://github.com/okx/plonky2", branch="clone-circuit"}
# computing
rayon = "1.8"
# data
Expand Down
22 changes: 10 additions & 12 deletions crates/zk-por-core/benches/batch_circuit.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
#![feature(test)]

use plonky2::plonk::circuit_builder::CircuitBuilder;
use zk_por_core::{
account::gen_accounts_with_random_data,
merkle_sum_prover::{
circuits::merkle_sum_circuit::build_merkle_sum_tree_circuit, prover::MerkleSumTreeProver,
},
account::gen_accounts_with_random_data, circuit_config::STANDARD_CONFIG, merkle_sum_prover::{circuits::account_circuit::AccountTargets, prover::MerkleSumTreeProver}, types::{C, D, F}
};

extern crate test;
use test::Bencher;

fn bench(b: &mut Bencher, batch_size: usize) {
let num_assets = 4;
let (circuit_data, account_targets) = build_merkle_sum_tree_circuit(batch_size, num_assets);
let accounts = gen_accounts_with_random_data(batch_size, num_assets).0;
b.iter(|| {
let prover = MerkleSumTreeProver { accounts: accounts.clone() };
_ = prover.prove_with_circuit(&circuit_data, account_targets.clone());
});
let mut builder = CircuitBuilder::<F, D>::new(STANDARD_CONFIG);
let num_assets = 50;
let accounts = gen_accounts_with_random_data(batch_size, num_assets);
let prover = MerkleSumTreeProver { accounts };
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the idea of prove_with_circuit is to avoid build multiple times. copy is lighter compared to build circuit again. can have a comparison between copy and build

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah will add a version for this

let account_targets: Vec<AccountTargets> = prover.build_merkle_tree_targets(&mut builder);
let data = &builder.build::<C>();

b.iter(|| _ = prover.get_proof_with_circuit_data(&account_targets, data));
}

#[bench]
Expand Down
37 changes: 12 additions & 25 deletions crates/zk-por-core/benches/recursive_circuit.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,31 @@
#![feature(test)]

use zk_por_core::merkle_sum_prover::{
circuits::merkle_sum_circuit::build_merkle_sum_tree_circuit, prover::MerkleSumTreeProver,
};
use plonky2::plonk::proof::ProofWithPublicInputs;
use zk_por_core::merkle_sum_prover::prover::MerkleSumTreeProver;

use zk_por_core::{
account::gen_accounts_with_random_data,
recursive::{circuit::build_recursive_n_circuit, prove::prove_n_subproofs},
types::C,
recursive_prover::prover::RecursiveProver,
types::{C, D, F},
};

extern crate test;
use test::Bencher;

fn bench<const SUBPROOF_NUM: usize>(b: &mut Bencher, batch_size: usize) {
let asset_num = 4;
let (merkle_sum_circuit, account_targets) =
build_merkle_sum_tree_circuit(batch_size, asset_num);

let accounts = gen_accounts_with_random_data(batch_size, asset_num).0;
let asset_num = 50;
let accounts = gen_accounts_with_random_data(batch_size, asset_num);
let prover = MerkleSumTreeProver { accounts };

let merkle_sum_proof = prover.prove_with_circuit(&merkle_sum_circuit, account_targets).unwrap();
let (merkle_sum_proof, merkle_sum_cd) = prover.get_proof_and_circuit_data();

let (recursive_circuit, recursive_account_targets) = build_recursive_n_circuit::<C, SUBPROOF_NUM>(
&merkle_sum_circuit.common,
&merkle_sum_circuit.verifier_only,
);
let mut subproofs = Vec::new();
(0..SUBPROOF_NUM).for_each(|_| {
subproofs.push(merkle_sum_proof.clone());
});
let sub_proofs: [ProofWithPublicInputs<F, C, D>; SUBPROOF_NUM] =
std::array::from_fn(|_| merkle_sum_proof.clone());

let recursive_prover = RecursiveProver { sub_proofs, merkle_sum_circuit: merkle_sum_cd };

b.iter(|| {
_ = prove_n_subproofs(
subproofs.clone(),
&merkle_sum_circuit.verifier_only,
&recursive_circuit,
recursive_account_targets.clone(),
);
recursive_prover.get_proof();
});
}
#[bench]
Expand Down
61 changes: 47 additions & 14 deletions crates/zk-por-core/src/account.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,71 @@
use std::i32;
use plonky2::{
hash::{hash_types::HashOut, poseidon::PoseidonHash},
plonk::config::Hasher,
};
use plonky2_field::types::Field;

use crate::types::F;
use plonky2_field::types::Field;
use rand::Rng;

/// A struct representing a users account. It represents their equity and debt as a Vector of goldilocks field elements.
#[derive(Debug, Clone)]
pub struct Account {
pub id: String,
pub id: String, // 256 bit hex string
pub equity: Vec<F>,
pub debt: Vec<F>,
}

pub fn gen_accounts_with_random_data(
num_accounts: usize,
num_assets: usize,
) -> (Vec<Account>, u32, u32) {
impl Account {
/// Gets the account hash for a given account.
pub fn get_hash(&self) -> HashOut<F> {
let sum_equity = self.equity.iter().fold(F::ZERO, |acc, x| acc + *x);

let sum_debt = self.debt.iter().fold(F::ZERO, |acc, x| acc + *x);

let id = self.get_user_id_in_field();

let hash =
PoseidonHash::hash_no_pad(vec![id, vec![sum_equity, sum_debt]].concat().as_slice());

hash
}

/// Gets a user id as a vec of 5 GF elements.
pub fn get_user_id_in_field(&self) -> Vec<F> {
assert!(self.id.len() == 64);
let segments = vec![
self.id[0..14].to_string(), // First 56 bits (14 hex chars)
self.id[14..28].to_string(), // Second 56 bits
self.id[28..42].to_string(), // Third 56 bits
self.id[42..56].to_string(), // Fourth 56 bits
self.id[56..64].to_string(), // Remaining 32 bits (8 hex chars, fits in 56 bits)
];

segments
.iter()
.map(|seg| F::from_canonical_u64(u64::from_str_radix(seg, 16).unwrap()))
.collect::<Vec<F>>()
}
}

/// Generates num_accounts number of accounts with num_assets of assets (with equity and debt being seperate vecs)
pub fn gen_accounts_with_random_data(num_accounts: usize, num_assets: usize) -> Vec<Account> {
let mut accounts: Vec<Account> = Vec::new();
let mut rng = rand::thread_rng(); // Create a random number generator
let mut equity_sum = 0;
let mut debt_sum = 0;
for _ in 0..num_accounts {
let mut equities = Vec::new();
let mut debts = Vec::new();
for _ in 0..num_assets {
let equity = rng.gen_range(1..10);
let equity = rng.gen_range(1..1000);
let debt = equity - 1; // such that debt is always less than equity
equity_sum += equity;
debt_sum += debt;
equities.push(F::from_canonical_u32(equity));
debts.push(F::from_canonical_u32(debt));
}
let account_id = rng.gen_range(0..i32::MAX).to_string();

let mut bytes = [0u8; 32]; // 32 bytes * 2 hex chars per byte = 64 hex chars
rng.fill(&mut bytes);
let account_id = bytes.iter().map(|byte| format!("{:02x}", byte)).collect::<String>();
accounts.push(Account { id: account_id, equity: equities, debt: debts });
}
(accounts, equity_sum, debt_sum)
accounts
}
57 changes: 0 additions & 57 deletions crates/zk-por-core/src/bin/batch_proof.rs

This file was deleted.

20 changes: 0 additions & 20 deletions crates/zk-por-core/src/bin/bench_batch.rs

This file was deleted.

44 changes: 0 additions & 44 deletions crates/zk-por-core/src/bin/bench_recursion.rs

This file was deleted.

Loading
Loading