Skip to content

Commit

Permalink
fix overflow sum (#900)
Browse files Browse the repository at this point in the history
Co-authored-by: Mauro Toscano <12560266+MauroToscano@users.noreply.github.com>
  • Loading branch information
diegokingston and MauroToscano authored Aug 30, 2024
1 parent 9f7b369 commit 5bad6b6
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions math/src/field/fields/montgomery_backed_prime_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,17 @@ where
if v <= u {
u = u - v;
if b < c {
b = b + modulus;
b = modulus - c + b;
} else {
b = b - c;
}
b = b - c;
} else {
v = v - u;
if c < b {
c = c + modulus;
c = modulus - b + c;
} else {
c = c - b;
}
c = c - b;
}
}

Expand Down Expand Up @@ -1243,6 +1245,14 @@ mod tests_u256_prime_fields {
assert_eq!(minus_3_pow_2, nine);
}

#[test]
fn secp256k1_inv_works() {
let a = SecpMontElement::from_hex_unchecked("0x456");
let a_inv = a.inv().unwrap();

assert_eq!(a * a_inv, SecpMontElement::one());
}

#[test]
fn test_cios_overflow_case() {
let a = GoldilocksElement::from(732582227915286439);
Expand Down

0 comments on commit 5bad6b6

Please sign in to comment.