-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 18 commits
1445c20
b07b04e
2c3315b
3540cc5
fa4680d
ce15ebe
96acebf
3eaadd7
c8b5edb
d95ff4e
90a97c1
61a7d8c
36dcefc
79c925b
749a6e9
38624bf
6e8eb0f
6a949d9
20cdb33
8187a6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
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 | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
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