Skip to content

Commit d78fcfa

Browse files
authored
Refactor QM31 test (#2114)
* Use proptest for QM31 test * Fix attributes conditions * Add missing cfg constraint * Add missing cfg constraint * Remove unnecessary cgf constraints * Filter special case * Add .no_shrink() * Add test with random input * Remove enum Configuration
1 parent 7c1b9d9 commit d78fcfa

File tree

1 file changed

+31
-52
lines changed

1 file changed

+31
-52
lines changed

vm/src/math_utils/mod.rs

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -613,13 +613,12 @@ mod tests {
613613
use assert_matches::assert_matches;
614614

615615
use num_traits::Num;
616-
use rand::Rng;
617616

618617
#[cfg(feature = "std")]
619618
use num_prime::RandPrime;
620619

621620
#[cfg(feature = "std")]
622-
use proptest::prelude::*;
621+
use proptest::{array::uniform4, prelude::*};
623622

624623
// Only used in proptest for now
625624
#[cfg(feature = "std")]
@@ -1286,56 +1285,6 @@ mod tests {
12861285
assert_eq!(qm31_packed_reduced_mul(x, res), Ok(Felt252::from(1)));
12871286
}
12881287

1289-
// TODO: Refactor using proptest and separating particular cases
1290-
#[test]
1291-
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
1292-
fn test_qm31_packed_reduced_inv_extensive() {
1293-
let mut rng = SmallRng::seed_from_u64(11480028852697973135);
1294-
#[derive(Clone, Copy)]
1295-
enum Configuration {
1296-
Zero,
1297-
One,
1298-
MinusOne,
1299-
Random,
1300-
}
1301-
let configurations = [
1302-
Configuration::Zero,
1303-
Configuration::One,
1304-
Configuration::MinusOne,
1305-
Configuration::Random,
1306-
];
1307-
let mut cartesian_product = vec![];
1308-
for &a in &configurations {
1309-
for &b in &configurations {
1310-
for &c in &configurations {
1311-
for &d in &configurations {
1312-
cartesian_product.push([a, b, c, d]);
1313-
}
1314-
}
1315-
}
1316-
}
1317-
1318-
for test_case in cartesian_product {
1319-
let x_coordinates: [u64; 4] = test_case
1320-
.iter()
1321-
.map(|&x| match x {
1322-
Configuration::Zero => 0,
1323-
Configuration::One => 1,
1324-
Configuration::MinusOne => STWO_PRIME - 1,
1325-
Configuration::Random => rng.gen_range(0..STWO_PRIME),
1326-
})
1327-
.collect::<Vec<u64>>()
1328-
.try_into()
1329-
.unwrap();
1330-
if x_coordinates == [0, 0, 0, 0] {
1331-
continue;
1332-
}
1333-
let x = qm31_coordinates_to_packed_reduced(x_coordinates);
1334-
let res = qm31_packed_reduced_inv(x).unwrap();
1335-
assert_eq!(qm31_packed_reduced_mul(x, res), Ok(Felt252::from(1)));
1336-
}
1337-
}
1338-
13391288
#[test]
13401289
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
13411290
fn test_qm31_packed_reduced_div() {
@@ -1353,8 +1302,38 @@ mod tests {
13531302
assert_eq!(res, y);
13541303
}
13551304

1305+
/// Necessary strat to use proptest on the QM31 test
1306+
#[cfg(feature = "std")]
1307+
fn configuration_strat() -> BoxedStrategy<u64> {
1308+
prop_oneof![Just(0), Just(1), Just(STWO_PRIME - 1), 0..STWO_PRIME].boxed()
1309+
}
1310+
13561311
#[cfg(feature = "std")]
13571312
proptest! {
1313+
1314+
#[test]
1315+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
1316+
fn qm31_packed_reduced_inv_random(x_coordinates in uniform4(0u64..STWO_PRIME)
1317+
.prop_filter("All configs cant be 0",
1318+
|arr| !arr.iter().all(|x| *x == 0))
1319+
) {
1320+
let x = qm31_coordinates_to_packed_reduced(x_coordinates);
1321+
let res = qm31_packed_reduced_inv(x).unwrap();
1322+
assert_eq!(qm31_packed_reduced_mul(x, res), Ok(Felt252::from(1)));
1323+
}
1324+
1325+
#[test]
1326+
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
1327+
fn qm31_packed_reduced_inv_extensive(x_coordinates in uniform4(configuration_strat())
1328+
.prop_filter("All configs cant be 0",
1329+
|arr| !arr.iter().all(|x| *x == 0))
1330+
.no_shrink()
1331+
) {
1332+
let x = qm31_coordinates_to_packed_reduced(x_coordinates);
1333+
let res = qm31_packed_reduced_inv(x).unwrap();
1334+
assert_eq!(qm31_packed_reduced_mul(x, res), Ok(Felt252::from(1)));
1335+
}
1336+
13581337
#[test]
13591338
fn pow2_const_in_range_returns_power_of_2(x in 0..=251u32) {
13601339
prop_assert_eq!(pow2_const(x), Felt252::TWO.pow(x));

0 commit comments

Comments
 (0)