Skip to content

Commit

Permalink
fix: updated by PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
sifnoc committed Mar 8, 2024
1 parent 04fb8d3 commit 7f8ed3c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 59 deletions.
2 changes: 1 addition & 1 deletion backend/examples/summa_solvency_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use summa_solvency::{

const K: u32 = 17;
const N_CURRENCIES: usize = 2;
const N_POINTS: usize = 3;
const N_POINTS: usize = N_CURRENCIES + 1;
const N_USERS: usize = 16;
const USER_INDEX: usize = 0;

Expand Down
2 changes: 1 addition & 1 deletion backend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ mod test {

const K: u32 = 17;
const N_CURRENCIES: usize = 2;
const N_POINTS: usize = 3;
const N_POINTS: usize = N_CURRENCIES + 1;
const N_USERS: usize = 16;
const PARAMS_PATH: &str = "../backend/ptau/hermez-raw-17";

Expand Down
18 changes: 8 additions & 10 deletions kzg_prover/benches/kzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ fn bench_kzg<
let verifying_grand_sum_bench_name = format!("<{}> verifying grand sum", name);
let verifying_user_bench_name = format!("<{}> verifying user inclusion", name);

let mut entries: Vec<Entry<N_CURRENCIES>> = vec![Entry::init_empty(); N_USERS];
let mut cryptos = vec![Cryptocurrency::init_empty(); N_CURRENCIES];
let _ = generate_dummy_entries(&mut entries, &mut cryptos);
let entries = generate_dummy_entries::<N_USERS, N_CURRENCIES>().unwrap();

// Calculate total for all entry columns
let mut csv_total: Vec<BigUint> = vec![BigUint::from(0u32); N_CURRENCIES];
Expand Down Expand Up @@ -261,13 +259,13 @@ fn bench_kzg<

fn criterion_benchmark(_c: &mut Criterion) {
const N_CURRENCIES: usize = 1;
const N_POINTS: usize = 2;
const N_POINTS: usize = N_CURRENCIES + 1;

// Demonstrating that a higher value of K has a more significant impact on benchmark performance than the number of users
#[cfg(not(feature = "no_range_check"))]
{
const K: u32 = 18;
const N_USERS: usize = (1 << 17) + (1 << 16) - 6;
const N_USERS: usize = 2usize.pow(K - 1) + 2usize.pow(K - 2) - 6; // Which is equal to (1 << 17) + (1 << 16) - 6
bench_kzg::<
K,
N_USERS,
Expand All @@ -279,7 +277,7 @@ fn criterion_benchmark(_c: &mut Criterion) {
#[cfg(not(feature = "no_range_check"))]
{
const K: u32 = 17;
const N_USERS: usize = (1 << 16) - 6;
const N_USERS: usize = 2usize.pow(K - 1) - 6; // Which is equal to (1 << 16) - 6
bench_kzg::<
K,
N_USERS,
Expand All @@ -292,31 +290,31 @@ fn criterion_benchmark(_c: &mut Criterion) {
#[cfg(feature = "no_range_check")]
{
const K: u32 = 9;
const N_USERS: usize = (1 << 9) - 6;
const N_USERS: usize = 2usize.pow(K - 1) - 6; // Which is equal to (1 << 8) - 6
bench_kzg::<K, N_USERS, N_CURRENCIES, N_POINTS, NoRangeCheckConfig<N_CURRENCIES, N_USERS>>(
format!("K = {K}, N_USERS = {N_USERS}, N_CURRENCIES = {N_CURRENCIES}").as_str(),
);
}
#[cfg(feature = "no_range_check")]
{
const K: u32 = 10;
const N_USERS: usize = (1 << 10) - 6;
const N_USERS: usize = 2usize.pow(K - 1) - 6; // Which is equal to (1 << 9) - 6
bench_kzg::<K, N_USERS, N_CURRENCIES, N_POINTS, NoRangeCheckConfig<N_CURRENCIES, N_USERS>>(
format!("K = {K}, N_USERS = {N_USERS}, N_CURRENCIES = {N_CURRENCIES}").as_str(),
);
}
#[cfg(feature = "no_range_check")]
{
const K: u32 = 11;
const N_USERS: usize = (1 << 11) - 6;
const N_USERS: usize = 2usize.pow(K - 1) - 6; // Which is equal to (1 << 10) - 6
bench_kzg::<K, N_USERS, N_CURRENCIES, N_POINTS, NoRangeCheckConfig<N_CURRENCIES, N_USERS>>(
format!("K = {K}, N_USERS = {N_USERS}, N_CURRENCIES = {N_CURRENCIES}").as_str(),
);
}
#[cfg(feature = "no_range_check")]
{
const K: u32 = 12;
const N_USERS: usize = (1 << 12) - 6;
const N_USERS: usize = 2usize.pow(K - 1) - 6; // Which is equal to (1 << 11) - 6
bench_kzg::<K, N_USERS, N_CURRENCIES, N_POINTS, NoRangeCheckConfig<N_CURRENCIES, N_USERS>>(
format!("K = {K}, N_USERS = {N_USERS}, N_CURRENCIES = {N_CURRENCIES}").as_str(),
);
Expand Down
2 changes: 1 addition & 1 deletion kzg_prover/src/circuits/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod test {

const K: u32 = 17;
const N_CURRENCIES: usize = 2;
const N_POINTS: usize = 3;
const N_POINTS: usize = N_CURRENCIES + 1;
const N_USERS: usize = 16;

#[test]
Expand Down
53 changes: 7 additions & 46 deletions kzg_prover/src/utils/dummy_entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,18 @@ use rand::{distributions::Alphanumeric, Rng};
use rayon::prelude::*;
use std::error::Error;

use crate::cryptocurrency::Cryptocurrency;
use crate::entry::Entry;

// This is for testing purposes with a large dataset instead of using a CSV file
pub fn generate_dummy_entries<const N_CURRENCIES: usize>(
entries: &mut [Entry<N_CURRENCIES>],
cryptocurrencies: &mut [Cryptocurrency],
) -> Result<(), Box<dyn Error>> {
pub fn generate_dummy_entries<const N_USERS: usize, const N_CURRENCIES: usize>(// entries: &mut [Entry<N_CURRENCIES>],
// cryptocurrencies: &mut [Cryptocurrency],
) -> Result<Vec<Entry<N_CURRENCIES>>, Box<dyn Error>> {
// Ensure N_CURRENCIES is greater than 0.
if N_CURRENCIES == 0 {
return Err("N_CURRENCIES must be greater than 0".into());
}

// Ensure the length of `cryptocurrencies` matches `N_CURRENCIES`.
if cryptocurrencies.len() != N_CURRENCIES {
return Err("cryptocurrencies length must be equal to N_CURRENCIES".into());
}

for (i, cryptocurrency) in cryptocurrencies.iter_mut().enumerate() {
*cryptocurrency = Cryptocurrency {
name: format!("ETH{i}"),
chain: "ETH".to_string(),
};
}
let mut entries: Vec<Entry<N_CURRENCIES>> = vec![Entry::init_empty(); N_USERS];

entries.par_iter_mut().for_each(|entry| {
let mut rng = rand::thread_rng();
Expand All @@ -39,7 +27,7 @@ pub fn generate_dummy_entries<const N_CURRENCIES: usize>(
*entry = Entry::new(username, balances).expect("Failed to create entry");
});

Ok(())
Ok(entries)
}

#[cfg(test)]
Expand All @@ -53,14 +41,8 @@ mod tests {
const N_USERS: usize = 1 << 17;
const N_CURRENCIES: usize = 2;

// Setup a buffer for entries and cryptocurrencies
let mut entries = vec![Entry::<N_CURRENCIES>::init_empty(); N_USERS];
let mut cryptocurrencies = vec![Cryptocurrency::init_empty(); N_CURRENCIES];

// Attempt to generate random entries
assert!(
generate_dummy_entries::<N_CURRENCIES>(&mut entries, &mut cryptocurrencies).is_ok()
);
let entries = generate_dummy_entries::<N_USERS, N_CURRENCIES>().unwrap();

// Verify that entries are populated
assert_eq!(entries.len(), N_USERS);
Expand All @@ -75,28 +57,7 @@ mod tests {
const N_USERS: usize = 1 << 17;
const N_CURRENCIES: usize = 0;

// Setup a buffer for entries and cryptocurrencies
let mut entries = vec![Entry::<N_CURRENCIES>::init_empty(); N_USERS];
let mut cryptocurrencies = vec![Cryptocurrency::init_empty(); N_CURRENCIES];

// `N_CURRENCIES` is zero, so this should fail
assert!(
generate_dummy_entries::<N_CURRENCIES>(&mut entries, &mut cryptocurrencies).is_err()
);
}

#[test]
fn test_wrong_cryptocurrencies() {
const N_USERS: usize = 1 << 17;
const N_CURRENCIES: usize = 2;

// Setup a buffer for entries and cryptocurrencies
let mut entries = vec![Entry::<N_CURRENCIES>::init_empty(); N_USERS];
let mut cryptocurrencies = vec![Cryptocurrency::init_empty(); N_CURRENCIES + 1];

// `cryptocurrencies` length is not equal to `N_CURRENCIES`, so this should fail
assert!(
generate_dummy_entries::<N_CURRENCIES>(&mut entries, &mut cryptocurrencies).is_err()
);
assert!(generate_dummy_entries::<N_USERS, N_CURRENCIES>().is_err());
}
}

0 comments on commit 7f8ed3c

Please sign in to comment.