diff --git a/runtime/lib/bigint.dart b/runtime/lib/bigint.dart index 60b42454946a..27c27fa7157c 100644 --- a/runtime/lib/bigint.dart +++ b/runtime/lib/bigint.dart @@ -1542,6 +1542,7 @@ class _Bigint extends _IntegerImplementation implements int { int modInverse(int m) { if (m is! int) throw new ArgumentError(m); if (m <= 0) throw new RangeError(m); + if (_used == 0) return 0; m = m._toBigint(); // TODO(regis): Implement modInverse for an even modulus. if (m.isEven) throw new UnimplementedError(); diff --git a/runtime/lib/integers.dart b/runtime/lib/integers.dart index 5bee4047dbd1..7302e19fc0c7 100644 --- a/runtime/lib/integers.dart +++ b/runtime/lib/integers.dart @@ -297,6 +297,7 @@ class _IntegerImplementation extends _Num { if (m is _Bigint) { return _toBigint().modInverse(m); } + if (this == 0) return 0; bool ac = m.isEven; int u = m; int v = this; diff --git a/sdk/lib/_internal/compiler/js_lib/js_number.dart b/sdk/lib/_internal/compiler/js_lib/js_number.dart index 0648aad12d66..03c68bb00022 100644 --- a/sdk/lib/_internal/compiler/js_lib/js_number.dart +++ b/sdk/lib/_internal/compiler/js_lib/js_number.dart @@ -415,6 +415,7 @@ class JSInt extends JSNumber implements int, double { int modInverse(int m) { if (m is! int) throw new ArgumentError(m); if (m <= 0) throw new RangeError(m); + if (this == 0) return 0; bool ac = m.isEven; int u = m; int v = this; diff --git a/sdk/lib/core/int.dart b/sdk/lib/core/int.dart index f12c97c5edb1..745ff07a9478 100644 --- a/sdk/lib/core/int.dart +++ b/sdk/lib/core/int.dart @@ -116,6 +116,7 @@ abstract class int extends num { * modulo [modulus]. * * The [modulus] must be positive. + * Returns 0 if no modular inverse exists. */ int modInverse(int modulus); diff --git a/tests/corelib/big_integer_arith_vm_test.dart b/tests/corelib/big_integer_arith_vm_test.dart index a2d8ad23fd89..7492d0834e34 100644 --- a/tests/corelib/big_integer_arith_vm_test.dart +++ b/tests/corelib/big_integer_arith_vm_test.dart @@ -235,6 +235,9 @@ UnimplementedEvenModulusModInverse(x, m) { testBigintModInverse() { var x, m; + x = 0; + m = 1000000001; + Expect.equals(0, x.modInverse(m)); // Not coprime. x = 1234567890; m = 19; Expect.equals(11, x.modInverse(m));