From 8c43b54308fa8cf457c86da7cc2e2a72b33fa1f6 Mon Sep 17 00:00:00 2001 From: Sebastian T F Date: Tue, 20 Feb 2024 10:49:31 +0530 Subject: [PATCH 1/2] test: negative values in csv_parser, mst, range_check --- csv/entry_16_negative.csv | 17 +++++++++++++++++ zk_prover/src/merkle_sum_tree/tests.rs | 19 +++++++++++++++++++ .../src/merkle_sum_tree/utils/csv_parser.rs | 12 ++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 csv/entry_16_negative.csv diff --git a/csv/entry_16_negative.csv b/csv/entry_16_negative.csv new file mode 100644 index 0000000..bd6f16b --- /dev/null +++ b/csv/entry_16_negative.csv @@ -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 diff --git a/zk_prover/src/merkle_sum_tree/tests.rs b/zk_prover/src/merkle_sum_tree/tests.rs index b79fb22..087415d 100644 --- a/zk_prover/src/merkle_sum_tree/tests.rs +++ b/zk_prover/src/merkle_sum_tree/tests.rs @@ -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::::from_csv("../csv/entry_16_negative.csv"); + assert!(merkle_tree.is_err()); + } + #[test] fn get_middle_node_hash_preimage() { let merkle_tree = diff --git a/zk_prover/src/merkle_sum_tree/utils/csv_parser.rs b/zk_prover/src/merkle_sum_tree/utils/csv_parser.rs index 5df9c44..c5bd8ea 100644 --- a/zk_prover/src/merkle_sum_tree/utils/csv_parser.rs +++ b/zk_prover/src/merkle_sum_tree/utils/csv_parser.rs @@ -63,3 +63,15 @@ pub fn parse_csv_to_entries, 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()); + } +} From 6be12777a41f31f6bab1bdcf56119a05b540912a Mon Sep 17 00:00:00 2001 From: Sebastian T F Date: Tue, 20 Feb 2024 11:02:34 +0530 Subject: [PATCH 2/2] test: try Fp::from negative values --- zk_prover/src/chips/range/tests.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/zk_prover/src/chips/range/tests.rs b/zk_prover/src/chips/range/tests.rs index 07cc576..95da899 100644 --- a/zk_prover/src/chips/range/tests.rs +++ b/zk_prover/src/chips/range/tests.rs @@ -272,6 +272,18 @@ mod testing { ); } + /* #[test] + // test converting negative value to Fp for use in range_check chip + fn test_negative_value() { + let a = Fp::from(-1i8); + /* + the trait bound `halo2_proofs::halo2curves::bn256::Fr: From` is not satisfied + the following other types implement trait `From`: + > + > + */ + } */ + #[cfg(feature = "dev-graph")] #[test] fn print_range_check_test() {