From a218a30b24bb2f6831dac312d3b67b2b586d5f44 Mon Sep 17 00:00:00 2001 From: teddav Date: Mon, 29 Apr 2024 11:36:37 +0200 Subject: [PATCH] teddav changes --- contracts/src/GrandSumVerifier.sol | 20 ++++++++++++++------ contracts/src/InclusionVerifier.sol | 2 +- prover/src/circuits/univariate_grand_sum.rs | 5 ++--- prover/src/entry.rs | 6 +++--- prover/src/utils/csv_parser.rs | 6 ++++-- prover/src/utils/dummy_entries.rs | 2 +- 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/contracts/src/GrandSumVerifier.sol b/contracts/src/GrandSumVerifier.sol index cfd2f3df..4815db87 100644 --- a/contracts/src/GrandSumVerifier.sol +++ b/contracts/src/GrandSumVerifier.sol @@ -102,8 +102,12 @@ contract GrandSumVerifier { // Ensure the proof length is divisible by `0x80`, accommodating the structured data layout. success := and(success, eq(0, mod(proof_length, 0x80))) if iszero(success) { - mstore(0, "Invalid proof length") - revert(0, 0x20) + mstore(0, "Error(string)") + mstore(0, keccak256(0, 13)) + mstore(4, 0x20) + mstore(0x24, 20) + mstore(0x44, "Invalid proof length") + revert(0, 0x64) } // Load the length of evaluation values, positioned after the proof data. @@ -113,8 +117,12 @@ contract GrandSumVerifier { // The proof length should match 4 times the length of the evaluation values. success := and(success, eq(4, div(proof_length, mul(evaluation_values_length, 0x20)))) if iszero(success) { - mstore(0, "Number of evaluation mismatch") - revert(0, 0x20) + mstore(0, "Error(string)") + mstore(0, keccak256(0, 13)) + mstore(4, 0x20) + mstore(0x24, 29) + mstore(0x44, "Number of evaluation mismatch") + revert(0, 0x64) } for { let i := 0 } lt(i, evaluation_values_length) { i := add(i, 1) } { @@ -130,7 +138,7 @@ contract GrandSumVerifier { // Assign values on memory for multiplication mstore(0x80, mload(G1_X_MPTR)) mstore(0xa0, mload(G1_Y_MPTR)) - success := and(success, ec_mul_tmp(success, minus_z)) + success := ec_mul_tmp(success, minus_z) // Performaing `c_g_to_minus_z := c + g_to_minus_z` // `c` is equivalent to `commitment` as input on the `open_grand_sums` function. @@ -152,7 +160,7 @@ contract GrandSumVerifier { let rhs_x := calldataload(proof_pos) // PI_X let rhs_y := calldataload(add(proof_pos, 0x20)) // PI_Y - success := and(success, ec_pairing(success, mload(LHS_X_MPTR), mload(LHS_Y_MPTR), rhs_x, rhs_y)) + success := ec_pairing(success, mload(LHS_X_MPTR), mload(LHS_Y_MPTR), rhs_x, rhs_y) } // Return 1 as result if everything succeeds diff --git a/contracts/src/InclusionVerifier.sol b/contracts/src/InclusionVerifier.sol index e84b41cc..6aeb76ae 100644 --- a/contracts/src/InclusionVerifier.sol +++ b/contracts/src/InclusionVerifier.sol @@ -154,7 +154,7 @@ contract InclusionVerifier { let rhs_x := calldataload(proof_pos) // PI_X let rhs_y := calldataload(add(proof_pos, 0x20)) // PI_Y - success := and(success, ec_pairing(success, mload(LHS_X_MPTR), mload(LHS_Y_MPTR), rhs_x, rhs_y)) + success := ec_pairing(success, mload(LHS_X_MPTR), mload(LHS_Y_MPTR), rhs_x, rhs_y) } // Return 1 as result if everything succeeds diff --git a/prover/src/circuits/univariate_grand_sum.rs b/prover/src/circuits/univariate_grand_sum.rs index 75f60b41..4290fd54 100644 --- a/prover/src/circuits/univariate_grand_sum.rs +++ b/prover/src/circuits/univariate_grand_sum.rs @@ -88,8 +88,7 @@ where let instance = meta.instance_column(); meta.enable_equality(instance); - for item in balances.iter().take(N_CURRENCIES) { - let z = *item; + for item in balances.into_iter() { // Create 4 advice columns for each range check chip let zs = [(); 4].map(|_| meta.advice_column()); @@ -97,7 +96,7 @@ where meta.enable_equality(*column); } - let range_check_config = RangeCheckU64Chip::configure(meta, z, zs, range_u16); + let range_check_config = RangeCheckU64Chip::configure(meta, item, zs, range_u16); range_check_configs.push(range_check_config); } diff --git a/prover/src/entry.rs b/prover/src/entry.rs index daaef76c..14ebf277 100644 --- a/prover/src/entry.rs +++ b/prover/src/entry.rs @@ -12,12 +12,12 @@ pub struct Entry { } impl Entry { - pub fn new(username: String, balances: [BigUint; N_CURRENCIES]) -> Result { - Ok(Entry { + pub fn new(username: String, balances: [BigUint; N_CURRENCIES]) -> Self { + Entry { username_as_big_uint: big_intify_username(&username), balances, username, - }) + } } pub fn init_empty() -> Self { diff --git a/prover/src/utils/csv_parser.rs b/prover/src/utils/csv_parser.rs index 43b34895..aa3045e8 100644 --- a/prover/src/utils/csv_parser.rs +++ b/prover/src/utils/csv_parser.rs @@ -24,7 +24,7 @@ pub fn parse_csv_to_entries, const N_CURRENCIES: usize>( "Too many columns in the CSV file, expected {}, skipping the rest", cryptocurrencies.len() ); - break; + break; // this should panic!, not just break } // Skipping 'username' column let parts: Vec<&str> = header.split('_').collect(); @@ -39,6 +39,7 @@ pub fn parse_csv_to_entries, const N_CURRENCIES: usize>( } } + // what's the point of this variable? it's computing the sum of all user balances, but we don't return it let mut balances_acc: Vec = vec![BigUint::from(0_usize); N_CURRENCIES]; for (i, result) in rdr.deserialize().enumerate() { @@ -66,7 +67,8 @@ pub fn parse_csv_to_entries, const N_CURRENCIES: usize>( .map(|(x, y)| x + y) .collect(); - let entry = Entry::new(username, balances_big_int.try_into().unwrap())?; + let entry = Entry::new(username, balances_big_int.try_into().unwrap()); + // if there are more users in the CSV than expected, this will crash at the end of the process, which is not convenient entries[i] = entry; } diff --git a/prover/src/utils/dummy_entries.rs b/prover/src/utils/dummy_entries.rs index f566b9c0..807380e1 100644 --- a/prover/src/utils/dummy_entries.rs +++ b/prover/src/utils/dummy_entries.rs @@ -23,7 +23,7 @@ pub fn generate_dummy_entries( let balances: [BigUint; N_CURRENCIES] = std::array::from_fn(|_| BigUint::from(rng.gen_range(1000..90000) as u32)); - *entry = Entry::new(username, balances).expect("Failed to create entry"); + *entry = Entry::new(username, balances); }); Ok(entries)