diff --git a/felt/src/lib.rs b/felt/src/lib.rs index 87701408ee..d86cc749c0 100644 --- a/felt/src/lib.rs +++ b/felt/src/lib.rs @@ -159,6 +159,8 @@ mod test { proptest! { #[test] + // Property-based test that ensures, for 100 felt values that are randomly generated each time tests are run, that a new felt doesn't fall outside the range [0, p]. + // In this and some of the following tests, The value of {x} can be either [0] or a very large number, in order to try to overflow the value of {p} and thus ensure the modular arithmetic is working correctly. fn new_in_range(ref x in "(0|[1-9][0-9]*)") { let x = &Felt::parse_bytes(x.as_bytes(), 10).unwrap(); let p = &BigUint::parse_bytes(PRIME_STR[2..].as_bytes(), 16).unwrap(); @@ -166,6 +168,7 @@ mod test { } #[test] + // Property-based test that ensures, for 100 {x} and {y} values that are randomly generated each time tests are run, that a multiplication between two felts {x} and {y} and doesn't fall outside the range [0, p]. The values of {x} and {y} can be either [0] or a very large number. fn mul_in_range(ref x in "(0|[1-9][0-9]*)", ref y in "(0|[1-9][0-9]*)") { let x = &Felt::parse_bytes(x.as_bytes(), 10).unwrap(); let y = &Felt::parse_bytes(y.as_bytes(), 10).unwrap(); @@ -177,6 +180,7 @@ mod test { } #[test] + // Property-based test that ensures, for 100 {x} and {y} values that are randomly generated each time tests are run, that the result of the division of {x} by {y} is the inverse multiplicative of {x} --that is, multiplying the result by {y} returns the original number {x}. The values of {x} and {y} can be either [0] or a very large number. fn div_is_mul_inv(ref x in "(0|[1-9][0-9]*)", ref y in "[1-9][0-9]*") { prop_assume!("0" != y); @@ -191,15 +195,23 @@ mod test { } #[test] - // Property-based test that ensures, for 100 values {x} that are randomly generated each time tests are run, that raising {x} to the {y}th power returns a result that is inside of the range [0, p]. - fn pow_in_range(ref x in "(0|[1-9][0-9]*)", ref y in "[0-9]{1,2}"){ - let base = &Felt::parse_bytes(x.as_bytes(), 10).unwrap(); - let exponent:u32 = y.parse()?; + // Property-based test that ensures, for 100 {value}s that are randomly generated each time tests are run, that performing a bit shift to the left by {shift_amount} of bits (between 0 and 999) returns a result that is inside of the range [0, p]. + fn shift_left_in_range(ref value in "(0|[1-9][0-9]*)", ref shift_amount in "[0-9]{1,3}"){ + let value = Felt::parse_bytes(value.as_bytes(), 10).unwrap(); let p = &BigUint::parse_bytes(PRIME_STR[2..].as_bytes(), 16).unwrap(); + let shift_amount:u32 = shift_amount.parse::().unwrap(); + let result = (value << shift_amount).to_biguint(); + prop_assert!(&result < p); + } - let result = Pow::pow(base, exponent); - let as_uint = &result.to_biguint(); - prop_assert!(as_uint < p, "{}", as_uint); + #[test] + // Property-based test that ensures, for 100 {value}s that are randomly generated each time tests are run, that performing a bit shift to the right by {shift_amount} of bits (between 0 and 999) returns a result that is inside of the range [0, p]. + fn shift_right_in_range(ref value in "(0|[1-9][0-9]*)", ref shift_amount in "[0-9]{1,3}"){ + let value = Felt::parse_bytes(value.as_bytes(), 10).unwrap(); + let p = &BigUint::parse_bytes(PRIME_STR[2..].as_bytes(), 16).unwrap(); + let shift_amount:u32 = shift_amount.parse::().unwrap(); + let result = (value >> shift_amount).to_biguint(); + prop_assert!(&result < p); } #[test] @@ -213,5 +225,17 @@ mod test { value.to_biguint(); prop_assert!(value < p); } + + #[test] + // Property-based test that ensures, for 100 values {x} that are randomly generated each time tests are run, that raising {x} to the {y}th power returns a result that is inside of the range [0, p]. + fn pow_in_range(ref x in "(0|[1-9][0-9]*)", ref y in "[0-9]{1,2}"){ + let base = &Felt::parse_bytes(x.as_bytes(), 10).unwrap(); + let exponent:u32 = y.parse()?; + let p = &BigUint::parse_bytes(PRIME_STR[2..].as_bytes(), 16).unwrap(); + + let result = Pow::pow(base, exponent); + let as_uint = &result.to_biguint(); + prop_assert!(as_uint < p, "{}", as_uint); + } } }