(a: BigInt, n: &BigInt)
| 414 | if a.is_negative() { a + n } else { a } |
| 415 | } |
| 416 | fn inverse(a: BigInt, n: &BigInt) -> Option<BigInt> { |
| 417 | let ExtendedGcd { gcd, x: c, .. } = a.extended_gcd(n); |
| 418 | if gcd.is_one() { |
| 419 | Some(normalize(c, n)) |
| 420 | } else { |
| 421 | None |
| 422 | } |
| 423 | } |
| 424 | let a = inverse(a % modulus, modulus).ok_or_else(|| { |
| 425 | vm.new_value_error("base is not invertible for the given modulus") |
| 426 | })?; |