Skip to content

Commit

Permalink
test: negative values in csv_parser, mst, range_check
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastiantf committed Feb 20, 2024
1 parent 95d63fe commit 8c43b54
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
17 changes: 17 additions & 0 deletions csv/entry_16_negative.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
username,balance_ETH_ETH,balance_USDT_ETH
dxGaEAii,-11888,41163
MBlfbBGI,67823,18651
lAhWlEWZ,-18651,2087
nuZweYtO,22073,-55683
gbdSwiuY,34897,83296
RZNneNuP,-83296,16881
YsscHXkp,31699,35479
RkLzkDun,-2087,-79731
HlQlnEYI,30605,11888
RqkZOFYe,16881,-14874
NjCSRAfD,-41163,67823
pHniJMQY,14874,22073
dOGIMzKR,10032,10032
HfMDmNLp,55683,34897
xPLKzCBl,79731,30605
AtwIxZHo,35479,31699
19 changes: 19 additions & 0 deletions zk_prover/src/merkle_sum_tree/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,25 @@ mod test {
assert_eq!(fp_3, 18446744073709551613.into());
}

#[test]
fn test_negative_big_uint_conversion() {
// test that the conversion from a negative big uint to an Fp returns None & panics
let big_uint = (-3).to_biguint();
assert!(big_uint.is_none());
assert!(std::panic::catch_unwind(|| big_uint.unwrap()).is_err());
// cannot convert negative number to big uint
// big uint is being converted to field element using merkle_sum_tree::utils::big_uint_to_fp
// plus field elements are non-negative values so it doesn't seem possible to get a negative value into mst or range_check
}

#[test]
fn test_mst_negative_value_in_csv() {
// create new merkle tree with csv containing negative values
let merkle_tree =
MerkleSumTree::<N_CURRENCIES, N_BYTES>::from_csv("../csv/entry_16_negative.csv");
assert!(merkle_tree.is_err());
}

#[test]
fn get_middle_node_hash_preimage() {
let merkle_tree =
Expand Down
12 changes: 12 additions & 0 deletions zk_prover/src/merkle_sum_tree/utils/csv_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,15 @@ pub fn parse_csv_to_entries<P: AsRef<Path>, const N_CURRENCIES: usize, const N_B

Ok((cryptocurrencies, entries))
}

#[cfg(test)]
mod test {
use crate::merkle_sum_tree::utils::parse_csv_to_entries;

#[test]
fn test_parse_csv_to_entries() {
// test negative value in csv
let result = parse_csv_to_entries::<&str, 2, 8>("../csv/entry_16_negative.csv");
assert!(result.is_err());
}
}

0 comments on commit 8c43b54

Please sign in to comment.