Skip to content

Commit

Permalink
Remove the unused N_POINTS generic constant (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
alxkzmn authored Mar 14, 2024
1 parent 69505f2 commit 067431b
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 49 deletions.
5 changes: 2 additions & 3 deletions backend/examples/summa_solvency_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use summa_solvency::{

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

Expand Down Expand Up @@ -96,7 +95,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

// Using the `round` instance, the commitment is dispatched to the Summa contract with the `dispatch_commitment` method.
let timestamp = 1u64;
let mut round = Round::<N_CURRENCIES, N_POINTS, N_USERS>::new(
let mut round = Round::<N_CURRENCIES, N_USERS>::new(
&signer,
zk_snark_proof,
advice_polys,
Expand Down Expand Up @@ -143,7 +142,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let commitment = summa_contract.commitments(snapshot_time).call().await?;

// Ensure the length of the commitment matches the expected size for the number of points.
assert_eq!(commitment.to_vec().len(), 0x40 * N_POINTS);
assert_eq!(commitment.to_vec().len(), 0x40 * (N_CURRENCIES + 1));

// Validate the inclusion proof using the contract verifier.
let mut verification_result = false;
Expand Down
17 changes: 7 additions & 10 deletions backend/src/apis/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ impl KZGProof {
/// # Type Parameters
///
/// * `N_CURRENCIES`: The number of currencies for which solvency is verified in this round.
/// * `N_POINTS`: The number of points in the `UnivariateGrandSum` circuit, which is `N_CURRENCIES + 1`.
/// * `N_USERS`: The number of users involved in this round of the protocol.
///
/// These parameters are used for initializing the `UnivariateGrandSum` circuit within the `Snapshot` struct.
Expand All @@ -57,14 +56,13 @@ impl KZGProof {
/// * `snapshot`: A `Snapshot` struct capturing the round's state, including user identities and balances.
/// * `signer`: A reference to a `SummaSigner`, the entity responsible for signing transactions with the Summa contract in this round.
///
pub struct Round<'a, const N_CURRENCIES: usize, const N_POINTS: usize, const N_USERS: usize> {
pub struct Round<'a, const N_CURRENCIES: usize, const N_USERS: usize> {
timestamp: u64,
snapshot: Snapshot<N_CURRENCIES, N_POINTS, N_USERS>,
snapshot: Snapshot<N_CURRENCIES, N_USERS>,
signer: &'a SummaSigner,
}

impl<const N_CURRENCIES: usize, const N_POINTS: usize, const N_USERS: usize>
Round<'_, N_CURRENCIES, N_POINTS, N_USERS>
impl<const N_CURRENCIES: usize, const N_USERS: usize> Round<'_, N_CURRENCIES, N_USERS>
where
[usize; N_CURRENCIES + 1]: Sized,
{
Expand All @@ -75,10 +73,10 @@ where
params: ParamsKZG<Bn256>,
verifying_key: VerifyingKey<G1Affine>,
timestamp: u64,
) -> Round<'_, N_CURRENCIES, N_POINTS, N_USERS> {
) -> Round<'_, N_CURRENCIES, N_USERS> {
Round {
timestamp,
snapshot: Snapshot::<N_CURRENCIES, N_POINTS, N_USERS>::new(
snapshot: Snapshot::<N_CURRENCIES, N_USERS>::new(
zk_snark_proof,
advice_polys,
params,
Expand Down Expand Up @@ -118,15 +116,14 @@ where
/// * `params`: The parameters for the KZG commitment scheme.
/// * `verifying_key`: The verifying key for getting domains, which is used for generating inclusion proofs.
///
pub struct Snapshot<const N_CURRENCIES: usize, const N_POINTS: usize, const N_USERS: usize> {
pub struct Snapshot<const N_CURRENCIES: usize, const N_USERS: usize> {
zk_snark_proof: Vec<u8>,
advice_polys: AdviceSingle<G1Affine, Coeff>,
params: ParamsKZG<Bn256>,
verifying_key: VerifyingKey<G1Affine>,
}

impl<const N_CURRENCIES: usize, const N_POINTS: usize, const N_USERS: usize>
Snapshot<N_CURRENCIES, N_POINTS, N_USERS>
impl<const N_CURRENCIES: usize, const N_USERS: usize> Snapshot<N_CURRENCIES, N_USERS>
where
[usize; N_CURRENCIES + 1]: Sized,
{
Expand Down
7 changes: 3 additions & 4 deletions backend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ mod test {

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

Expand Down Expand Up @@ -210,15 +209,15 @@ mod test {
&[instances.clone()],
);

let mut round_one = Round::<N_CURRENCIES, N_POINTS, N_USERS>::new(
let mut round_one = Round::<N_CURRENCIES, N_USERS>::new(
&signer,
zk_snark_proof.clone(),
advice_polys.clone(),
params.clone(),
vk.clone(),
1,
);
let mut round_two = Round::<N_CURRENCIES, N_POINTS, N_USERS>::new(
let mut round_two = Round::<N_CURRENCIES, N_USERS>::new(
&signer,
zk_snark_proof,
advice_polys,
Expand Down Expand Up @@ -331,7 +330,7 @@ mod test {
));

let snapshot_time = 1u64;
let mut round = Round::<N_CURRENCIES, N_POINTS, N_USERS>::new(
let mut round = Round::<N_CURRENCIES, N_USERS>::new(
&signer,
zk_snark_proof,
advice_polys,
Expand Down
34 changes: 11 additions & 23 deletions kzg_prover/benches/kzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ use summa_solvency::{
verify_user_inclusion,
},
},
cryptocurrency::Cryptocurrency,
entry::Entry,
utils::{big_uint_to_fp, generate_dummy_entries},
};

fn bench_kzg<
const K: u32,
const N_USERS: usize,
const N_CURRENCIES: usize,
const N_POINTS: usize,
CONFIG: CircuitConfig<N_CURRENCIES, N_USERS>,
>(
name: &str,
Expand Down Expand Up @@ -243,7 +240,7 @@ fn bench_kzg<
b.iter_batched(
|| (column_range.clone(), omega, user_index),
|(column_range, omega, user_index)| {
verify_user_inclusion::<N_POINTS>(
verify_user_inclusion(
&params,
&zk_snark_proof,
&openings_batch_proof,
Expand All @@ -259,63 +256,54 @@ fn bench_kzg<

fn criterion_benchmark(_c: &mut Criterion) {
const N_CURRENCIES: usize = 1;
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 = 17;
const N_USERS: usize = 2usize.pow(K) + 2usize.pow(16) - 6; // Subtracting 2^16 (reserved for range checks) and 6 (reserved rows) from 2^K.
bench_kzg::<
K,
N_USERS,
N_CURRENCIES,
N_POINTS,
UnivariateGrandSumConfig<N_CURRENCIES, N_USERS>,
>(format!("K = {K}, N_USERS = {N_USERS}, N_CURRENCIES = {N_CURRENCIES}").as_str());
bench_kzg::<K, N_USERS, N_CURRENCIES, UnivariateGrandSumConfig<N_CURRENCIES, N_USERS>>(
format!("K = {K}, N_USERS = {N_USERS}, N_CURRENCIES = {N_CURRENCIES}").as_str(),
);
}
#[cfg(not(feature = "no_range_check"))]
{
const K: u32 = 18;
const N_USERS: usize = 2usize.pow(K) - 2usize.pow(16) - 6; // Subtracting 2^16 (reserved for range checks) and 6 (reserved rows) from 2^K.
bench_kzg::<
K,
N_USERS,
N_CURRENCIES,
N_POINTS,
UnivariateGrandSumConfig<N_CURRENCIES, N_USERS>,
>(format!("K = {K}, N_USERS = {N_USERS}, N_CURRENCIES = {N_CURRENCIES}").as_str());
bench_kzg::<K, N_USERS, N_CURRENCIES, UnivariateGrandSumConfig<N_CURRENCIES, N_USERS>>(
format!("K = {K}, N_USERS = {N_USERS}, N_CURRENCIES = {N_CURRENCIES}").as_str(),
);
}
//Use the following benchmarks for quick evaluation/prototyping (no range check)
#[cfg(feature = "no_range_check")]
{
const K: u32 = 9;
const N_USERS: usize = 2usize.pow(K) - 6;
bench_kzg::<K, N_USERS, N_CURRENCIES, N_POINTS, NoRangeCheckConfig<N_CURRENCIES, N_USERS>>(
bench_kzg::<K, N_USERS, N_CURRENCIES, 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 = 2usize.pow(K) - 6;
bench_kzg::<K, N_USERS, N_CURRENCIES, N_POINTS, NoRangeCheckConfig<N_CURRENCIES, N_USERS>>(
bench_kzg::<K, N_USERS, N_CURRENCIES, 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 = 2usize.pow(K) - 6;
bench_kzg::<K, N_USERS, N_CURRENCIES, N_POINTS, NoRangeCheckConfig<N_CURRENCIES, N_USERS>>(
bench_kzg::<K, N_USERS, N_CURRENCIES, 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 = 2usize.pow(K) - 6;
bench_kzg::<K, N_USERS, N_CURRENCIES, N_POINTS, NoRangeCheckConfig<N_CURRENCIES, N_USERS>>(
bench_kzg::<K, N_USERS, N_CURRENCIES, NoRangeCheckConfig<N_CURRENCIES, N_USERS>>(
format!("K = {K}, N_USERS = {N_USERS}, N_CURRENCIES = {N_CURRENCIES}").as_str(),
);
}
Expand Down
6 changes: 3 additions & 3 deletions kzg_prover/bin/gen_commit_and_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn main() {
let poly_length = 1 << u64::from(K);
let total_balances = csv_total
.iter()
.map(|x| big_uint_to_fp(&(x)) * Fp::from(poly_length).invert().unwrap())
.map(|x| big_uint_to_fp(x) * Fp::from(poly_length).invert().unwrap())
.collect::<Vec<Fp>>();

let mut grand_sums_kzg_proof = Vec::new();
Expand Down Expand Up @@ -165,8 +165,8 @@ fn main() {
let user_values = &entries
.get(user_index as usize)
.map(|entry| {
std::iter::once(big_uint_to_fp(&(entry.username_as_big_uint())))
.chain(entry.balances().iter().map(|x| big_uint_to_fp(x)))
std::iter::once(big_uint_to_fp(entry.username_as_big_uint()))
.chain(entry.balances().iter().map(big_uint_to_fp))
.collect::<Vec<Fp>>()
})
.unwrap();
Expand Down
5 changes: 2 additions & 3 deletions kzg_prover/src/circuits/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ mod test {

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

#[test]
Expand Down Expand Up @@ -239,7 +238,7 @@ mod test {

let column_range = 0..N_CURRENCIES + 1;
// The Verifier verifies the inclusion of the 4th user entry
let (inclusion_verified, id_and_balance_values) = verify_user_inclusion::<N_POINTS>(
let (inclusion_verified, id_and_balance_values) = verify_user_inclusion(
&params,
&zk_snark_proof,
&openings_batch_proof,
Expand Down Expand Up @@ -314,7 +313,7 @@ mod test {
// Test failure case with the wrong group generator
// Slightly modify the generator
let bad_omega = omega.sub(&Fp::one());
let (balances_verified, _) = verify_user_inclusion::<N_CURRENCIES>(
let (balances_verified, _) = verify_user_inclusion(
&params,
&zk_snark_proof,
&openings_batch_proof,
Expand Down
4 changes: 1 addition & 3 deletions kzg_prover/src/circuits/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,6 @@ pub fn verify_grand_sum_openings<const N_CURRENCIES: usize>(
/// Verifies the KZG batch proof of the polynomial openings being the evaluations
/// of the advice polynomials at the point corresponding to the user index
///
/// * `N_POINTS` - the size of the user entry being verified (e.g., 1 ID value + 4 balance values = 5)
///
/// # Arguments
/// * `params` - the KZG parameters
/// * `zk_snark_proof` - the ZK-SNARK proof of the circuit whose advice columns contain the user entry polynomials
Expand All @@ -378,7 +376,7 @@ pub fn verify_grand_sum_openings<const N_CURRENCIES: usize>(
/// # Returns
/// * `bool` - whether the user entry openings are verified correctly
/// * `Vec<BigUint>` - the evaluations of the advice polynomials at the point corresponding to the user index
pub fn verify_user_inclusion<const N_POINTS: usize>(
pub fn verify_user_inclusion(
params: &ParamsKZG<Bn256>,
zk_snark_proof: &[u8],
balance_opening_batch_proof: &[u8],
Expand Down

0 comments on commit 067431b

Please sign in to comment.