Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Neg prop test for felts #721

Merged
merged 3 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions felt/proptest-regressions/lib.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc 1bf12114a6bfc70e8f949318158595883c33bc6fb35862852bc5144547ac3557 # shrinks to ref x = "0"
12 changes: 10 additions & 2 deletions felt/src/bigint_felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,22 @@ impl Sum for FeltBigInt {
impl Neg for FeltBigInt {
type Output = FeltBigInt;
fn neg(self) -> Self::Output {
FeltBigInt(&*CAIRO_PRIME - self.0)
if self.is_zero() {
self
} else {
FeltBigInt(&*CAIRO_PRIME - self.0)
}
}
}

impl<'a> Neg for &'a FeltBigInt {
type Output = FeltBigInt;
fn neg(self) -> Self::Output {
FeltBigInt(&*CAIRO_PRIME - &self.0)
if self.is_zero() {
self.clone()
} else {
FeltBigInt(&*CAIRO_PRIME - &self.0)
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions felt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,16 @@ mod test {
let p = &BigUint::parse_bytes(PRIME_STR[2..].as_bytes(), 16).unwrap();
prop_assert!(&x.to_biguint() < p);
}
#[test]
// Property-based test that ensures, for 100 felt values that are randomly generated each time tests are run, that the negative of a felt doesn't fall outside the range [0, p].
fn neg_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();
let neg = -x;
let as_uint = &neg.to_biguint();
println!("x: {}, neg: {}", x, neg);
prop_assert!(as_uint < p);
}

#[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.
Expand Down