Skip to content

Commit

Permalink
teddav changes
Browse files Browse the repository at this point in the history
  • Loading branch information
teddav committed Apr 29, 2024
1 parent fec83a7 commit a218a30
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 16 deletions.
20 changes: 14 additions & 6 deletions contracts/src/GrandSumVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) } {
Expand All @@ -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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion contracts/src/InclusionVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions prover/src/circuits/univariate_grand_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,15 @@ 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());

for column in &zs {
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);
}
Expand Down
6 changes: 3 additions & 3 deletions prover/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ pub struct Entry<const N_CURRENCIES: usize> {
}

impl<const N_CURRENCIES: usize> Entry<N_CURRENCIES> {
pub fn new(username: String, balances: [BigUint; N_CURRENCIES]) -> Result<Self, &'static str> {
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 {
Expand Down
6 changes: 4 additions & 2 deletions prover/src/utils/csv_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn parse_csv_to_entries<P: AsRef<Path>, 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();
Expand All @@ -39,6 +39,7 @@ pub fn parse_csv_to_entries<P: AsRef<Path>, 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<BigUint> = vec![BigUint::from(0_usize); N_CURRENCIES];

for (i, result) in rdr.deserialize().enumerate() {
Expand Down Expand Up @@ -66,7 +67,8 @@ pub fn parse_csv_to_entries<P: AsRef<Path>, 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;
}

Expand Down
2 changes: 1 addition & 1 deletion prover/src/utils/dummy_entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn generate_dummy_entries<const N_USERS: usize, const N_CURRENCIES: usize>(
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)
Expand Down

0 comments on commit a218a30

Please sign in to comment.