Chromium Code Reviews| Index: runtime/lib/integers.dart |
| =================================================================== |
| --- runtime/lib/integers.dart (revision 40320) |
| +++ runtime/lib/integers.dart (working copy) |
| @@ -263,6 +263,31 @@ |
| } |
| _leftShiftWithMask32(count, mask) native "Integer_leftShiftWithMask32"; |
| + |
| + // TODO(regis): Make this method private once the plumbing to invoke it from |
| + // dart:math is in place. Move the argument checking to dart:math. |
| + // Return pow(this, e) % m. |
| + int modPow(int e, int m) { |
| + if (e is! int) throw new ArgumentError(e); |
| + if (m is! int) throw new ArgumentError(m); |
|
srdjan
2014/09/16 18:08:36
Maybe instead:
if ((e is! int) || (m is! int) {
regis
2014/09/16 18:18:54
This can be done when we add modPow to dart:math.
|
| + if (e is _Bigint || m is _Bigint) { |
| + return _toBigint().modPow(e, m); |
| + } |
| + if (e < 1) return 1; |
| + int b = this; |
| + if (b < 0 || b > m) { |
| + b = b % m; |
| + } |
| + int r = 1; |
| + while (e > 0) { |
| + if ((e & 1) != 0) { |
| + r = (r * b) % m; |
| + } |
| + e >>= 1; |
| + b = (b * b) % m; |
| + } |
| + return r; |
| + } |
| } |
| class _Smi extends _IntegerImplementation implements int { |