OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // Copyright 2009 The Go Authors. All rights reserved. | |
6 // Use of this source code is governed by a BSD-style | |
7 // license that can be found in the LICENSE file. | |
8 | |
9 /* | |
10 * Copyright (c) 2003-2005 Tom Wu | |
11 * Copyright (c) 2012 Adam Singer (adam@solvr.io) | |
12 * All Rights Reserved. | |
13 * | |
14 * Permission is hereby granted, free of charge, to any person obtaining | |
15 * a copy of this software and associated documentation files (the | |
16 * "Software"), to deal in the Software without restriction, including | |
17 * without limitation the rights to use, copy, modify, merge, publish, | |
18 * distribute, sublicense, and/or sell copies of the Software, and to | |
19 * permit persons to whom the Software is furnished to do so, subject to | |
20 * the following conditions: | |
21 * | |
22 * The above copyright notice and this permission notice shall be | |
23 * included in all copies or substantial portions of the Software. | |
24 * | |
25 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, | |
26 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY | |
27 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. | |
28 * | |
29 * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL, | |
30 * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER | |
31 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF | |
32 * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT | |
33 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
34 * | |
35 * In addition, the following condition applies: | |
36 * | |
37 * All redistributions must retain an intact copy of this copyright notice | |
38 * and disclaimer. | |
39 */ | |
40 | |
41 class _Bigint extends _IntegerImplementation implements int { | |
42 // Bits per digit. | |
43 static const int DIGIT_BITS = 32; | |
Florian Schneider
2014/09/08 09:10:19
Ideally, this should be platform-specific and depe
regis
2014/09/09 19:18:14
Ideally, yes. But this would require a tremendous
| |
44 static const int DIGIT_BASE = 1 << DIGIT_BITS; | |
45 static const int DIGIT_MASK = (1 << DIGIT_BITS) - 1; | |
46 | |
47 // Bits per half digit. | |
48 static const int DIGIT2_BITS = DIGIT_BITS >> 1; | |
49 static const int DIGIT2_BASE = 1 << DIGIT2_BITS; | |
50 static const int DIGIT2_MASK = (1 << DIGIT2_BITS) - 1; | |
51 | |
52 // Allocate extra digits so the bigint can be reused. | |
53 static const int EXTRA_DIGITS = 4; | |
54 | |
55 // Floating-point unit integer precision. | |
56 static const int FP_BITS = 52; | |
Florian Schneider
2014/09/08 09:10:19
These constants should not be needed anymore since
regis
2014/09/09 19:18:14
These constants are needed to approximate a divisi
| |
57 static const int FP_BASE = 1 << FP_BITS; | |
58 static const int FP_D1 = FP_BITS - DIGIT_BITS; | |
59 static const int FP_D2 = 2 * DIGIT_BITS - FP_BITS; | |
60 | |
61 // Min and max of non bigint values. | |
62 static const int MIN_INT64 = (-1) << 63; | |
63 static const int MAX_INT64 = 0x7fffffffffffffff; | |
64 | |
65 // Bigint constant values. | |
66 static final _Bigint ZERO = new _Bigint(); | |
67 static final _Bigint ONE = new _Bigint()._setInt(1); | |
68 static final _Bigint MINUS_ONE = new _Bigint()._setInt(-1); | |
69 | |
70 // Digit conversion table for parsing. | |
71 static final Map<int, int> DIGIT_TABLE = _createDigitTable(); | |
72 | |
73 // Internal data structure. | |
74 // TODO(regis): Remove RawBigint and native getters/setters/factory or | |
75 // intrinsify these native field accessors. | |
76 /* | |
77 bool _neg; | |
78 int _used; // Number of uint32 digits used, _used <= _digits.length. | |
79 Uint32List _digits; | |
80 */ | |
srdjan
2014/09/08 19:19:49
Remove dead code.
regis
2014/09/09 19:18:14
Done.
| |
81 bool get _neg native "Bigint_getNeg"; | |
82 void set _neg(bool neg) native "Bigint_setNeg"; | |
83 int get _used native "Bigint_getUsed"; | |
84 void set _used(int used) native "Bigint_setUsed"; | |
85 Uint32List get _digits native "Bigint_getDigits"; | |
86 void set _digits(Uint32List digits) native "Bigint_setDigits"; | |
87 | |
88 // Factory returning an instance initialized to value 0. | |
89 factory _Bigint() native "Bigint_allocate"; | |
90 | |
91 // Factory returning an instance initialized to an integer value. | |
92 factory _Bigint._fromInt(int i) { | |
93 return new _Bigint()._setInt(i); | |
94 } | |
95 | |
96 // Factory returning an instance initialized to a hex string. | |
97 factory _Bigint._fromHex(String s) { | |
98 return new _Bigint()._setHex(s); | |
99 } | |
100 | |
101 // Factory returning an instance initialized to a double value given by its | |
102 // components. | |
103 factory _Bigint._fromDouble(int sign, int significand, int exponent) { | |
104 return new _Bigint()._setDouble(sign, significand, exponent); | |
105 } | |
106 | |
107 // Initialize instance to the given value no larger than a Mint. | |
108 _Bigint _setInt(int i) { | |
109 assert(i is! _Bigint); | |
110 _ensureLength(2); | |
111 _used = 2; | |
112 var l, h; | |
113 if (i < 0) { | |
114 _neg = true; | |
115 if (i == MIN_INT64) { | |
116 l = 0; | |
117 h = 0x80000000; | |
118 } else { | |
119 l = (-i) & DIGIT_MASK; | |
120 h = (-i) >> DIGIT_BITS; | |
121 } | |
122 } else { | |
123 _neg = false; | |
124 l = i & DIGIT_MASK; | |
125 h = i >> DIGIT_BITS; | |
126 } | |
127 _digits[0] = l; | |
128 _digits[1] = h; | |
129 _clamp(); | |
130 return this; | |
131 } | |
132 | |
133 // Initialize instance to the given hex string. | |
134 // TODO(regis): Copy Bigint::NewFromHexCString, fewer digit accesses. | |
135 // TODO(regis): Unused. | |
136 _Bigint _setHex(String s) { | |
137 const int HEX_BITS = 4; | |
138 const int HEX_DIGITS_PER_DIGIT = 8; | |
139 var hexDigitIndex = s.length; | |
140 _ensureLength((hexDigitIndex + HEX_DIGITS_PER_DIGIT - 1) ~/ HEX_DIGITS_PER_D IGIT); | |
141 var bitIndex = 0; | |
142 while (--hexDigitIndex >= 0) { | |
143 var digit = DIGIT_TABLE[s.codeUnitAt(hexDigitIndex)]; | |
144 if (digit = null) { | |
145 if (s[hexDigitIndex] == "-") _neg = true; | |
146 continue; // Ignore invalid digits. | |
147 } | |
148 _neg = false; // Ignore "-" if not at index 0. | |
149 if (bitIndex == 0) { | |
150 _digits[_used++] = digit; | |
151 // TODO(regis): What if too many bad digits were ignored and | |
152 // _used becomes larger than _digits.length? error or reallocate? | |
153 } else { | |
154 _digits[_used - 1] |= digit << bitIndex; | |
155 } | |
156 bitIndex = (bitIndex + HEX_BITS) % DIGIT_BITS; | |
157 } | |
158 _clamp(); | |
159 return this; | |
160 } | |
161 | |
162 // Initialize instance to the given double value. | |
163 _Bigint _setDouble(int sign, int significand, int exponent) { | |
164 assert(significand >= 0); | |
165 assert(exponent >= 0); | |
166 _setInt(significand); | |
167 _neg = sign < 0; | |
168 if (exponent > 0) { | |
169 _lShiftTo(exponent, this); | |
170 } | |
171 return this; | |
172 } | |
173 | |
174 // Create digit conversion table for parsing. | |
175 static Map<int, int> _createDigitTable() { | |
176 Map table = new HashMap(); | |
177 int digit, value; | |
178 digit = "0".codeUnitAt(0); | |
179 for(value = 0; value <= 9; ++value) table[digit++] = value; | |
180 digit = "a".codeUnitAt(0); | |
181 for(value = 10; value < 36; ++value) table[digit++] = value; | |
182 digit = "A".codeUnitAt(0); | |
183 for(value = 10; value < 36; ++value) table[digit++] = value; | |
184 return table; | |
185 } | |
186 | |
187 // Return most compact integer (i.e. possibly Smi or Mint). | |
188 // TODO(regis): Intrinsify. | |
189 int _toValidInt() { | |
190 assert(DIGIT_BITS == 32); // Otherwise this code needs to be revised. | |
191 if (_used == 0) return 0; | |
192 if (_used == 1) return _neg ? -_digits[0] : _digits[0]; | |
193 if (_used > 2) return this; | |
194 if (_neg) { | |
195 if (_digits[1] > 0x80000000) return this; | |
196 if (_digits[1] == 0x80000000) { | |
197 if (_digits[0] > 0) return this; | |
198 return MIN_INT64; | |
199 } | |
200 return -((_digits[1] << DIGIT_BITS) | _digits[0]); | |
201 } | |
202 if (_digits[1] >= 0x80000000) return this; | |
203 return (_digits[1] << DIGIT_BITS) | _digits[0]; | |
204 } | |
205 | |
206 // Conversion from int to bigint. | |
207 _Bigint _toBigint() => this; | |
208 | |
209 // Make sure at least 'length' _digits are allocated. | |
210 // Copy existing _digits if reallocation is necessary. | |
211 // TODO(regis): Check that we are not preserving _digits unnecessarily. | |
212 void _ensureLength(int length) { | |
213 if (length > 0 && (_digits == null || length > _digits.length)) { | |
214 var new_digits = new Uint32List(length + EXTRA_DIGITS); | |
215 if (_digits != null) { | |
216 for (var i = _used; --i >= 0; ) { | |
217 new_digits[i] = _digits[i]; | |
218 } | |
219 } | |
220 _digits = new_digits; | |
221 } | |
222 } | |
223 | |
224 // Clamp off excess high _digits. | |
225 void _clamp() { | |
226 while (_used > 0 && _digits[_used - 1] == 0) { | |
227 --_used; | |
228 } | |
229 assert(_used > 0 || !_neg); | |
230 } | |
231 | |
232 // Copy this to r. | |
233 void _copyTo(r) { | |
srdjan
2014/09/09 19:31:44
Please add type to arguments (here and below).
regis
2014/09/09 21:19:37
Done.
| |
234 r._ensureLength(_used); | |
235 for (var i = _used - 1; i >= 0; --i) { | |
236 r._digits[i] = _digits[i]; | |
237 } | |
238 r._used = _used; | |
239 r._neg = _neg; | |
240 } | |
241 | |
242 // Return the bit length of digit x. | |
243 int _nbits(int x) { | |
244 var r = 1, t; | |
245 if ((t = x >> 16) != 0) { x = t; r += 16; } | |
246 if ((t = x >> 8) != 0) { x = t; r += 8; } | |
247 if ((t = x >> 4) != 0) { x = t; r += 4; } | |
248 if ((t = x >> 2) != 0) { x = t; r += 2; } | |
249 if ((x >> 1) != 0) { r += 1; } | |
250 return r; | |
251 } | |
252 | |
253 // r = this << n*DIGIT_BITS. | |
254 void _dlShiftTo(n, r) { | |
255 var r_used = _used + n; | |
256 r._ensureLength(r_used); | |
257 for (var i = _used - 1; i >= 0; --i) { | |
258 r._digits[i + n] = _digits[i]; | |
259 } | |
260 for (var i = n - 1; i >= 0; --i) { | |
261 r._digits[i] = 0; | |
262 } | |
263 r._used = r_used; | |
264 r._neg = _neg; | |
265 } | |
266 | |
267 // r = this >> n*DIGIT_BITS. | |
268 void _drShiftTo(n, r) { | |
269 var r_used = _used - n; | |
270 if (r_used < 0) { | |
271 if (_neg) { | |
272 MINUS_ONE._copyTo(r); | |
273 } else { | |
274 ZERO._copyTo(r); | |
275 } | |
276 return; | |
277 } | |
278 r._ensureLength(r_used); | |
279 for (var i = n; i < _used; ++i) { | |
280 r._digits[i - n] = _digits[i]; | |
281 } | |
282 r._used = r_used; | |
283 r._neg = _neg; | |
284 if (_neg) { | |
285 // Round down if any bit was shifted out. | |
286 for (var i = 0; i < n; i++) { | |
287 if (_digits[i] != 0) { | |
288 r._subTo(ONE, r); | |
289 break; | |
290 } | |
291 } | |
292 } | |
293 } | |
294 | |
295 // r = this << n. | |
296 void _lShiftTo(n, r) { | |
297 var ds = n ~/ DIGIT_BITS; | |
298 var bs = n % DIGIT_BITS; | |
299 if (bs == 0) { | |
300 _dlShiftTo(ds, r); | |
301 return; | |
302 } | |
303 var cbs = DIGIT_BITS - bs; | |
304 var bm = (1 << cbs) - 1; | |
305 var r_used = _used + ds + 1; | |
306 r._ensureLength(r_used); | |
307 var c = 0; | |
308 for (var i = _used - 1; i >= 0; --i) { | |
309 r._digits[i + ds + 1] = (_digits[i] >> cbs) | c; | |
310 c = (_digits[i] & bm) << bs; | |
311 } | |
312 for (var i = ds - 1; i >= 0; --i) { | |
313 r._digits[i] = 0; | |
314 } | |
315 r._digits[ds] = c; | |
316 r._used = r_used; | |
317 r._neg = _neg; | |
318 r._clamp(); | |
319 } | |
320 | |
321 // r = this >> n. | |
322 void _rShiftTo(n, r) { | |
323 var ds = n ~/ DIGIT_BITS; | |
324 var bs = n % DIGIT_BITS; | |
325 if (bs == 0) { | |
326 _drShiftTo(ds, r); | |
327 return; | |
328 } | |
329 var r_used = _used - ds; | |
330 if (r_used <= 0) { | |
331 if (_neg) { | |
332 MINUS_ONE._copyTo(r); | |
333 } else { | |
334 ZERO._copyTo(r); | |
335 } | |
336 return; | |
337 } | |
338 var cbs = DIGIT_BITS - bs; | |
339 var bm = (1 << bs) - 1; | |
340 r._ensureLength(r_used); | |
341 r._digits[0] = _digits[ds] >> bs; | |
342 for (var i = ds + 1; i < _used; ++i) { | |
343 r._digits[i - ds - 1] |= (_digits[i] & bm) << cbs; | |
344 r._digits[i - ds] = _digits[i] >> bs; | |
345 } | |
346 r._neg = _neg; | |
347 r._used = r_used; | |
348 r._clamp(); | |
349 if (_neg) { | |
350 // Round down if any bit was shifted out. | |
351 if ((_digits[ds] & bm) != 0) { | |
352 r._subTo(ONE, r); | |
353 return; | |
354 } | |
355 for (var i = 0; i < ds; i++) { | |
356 if (_digits[i] != 0) { | |
357 r._subTo(ONE, r); | |
358 return; | |
359 } | |
360 } | |
361 } | |
362 } | |
363 | |
364 // Return 0 if abs(this) == abs(a). | |
365 // Return a positive number if abs(this) > abs(a). | |
366 // Return a negative number if abs(this) < abs(a). | |
367 int _absCompareTo(a) { | |
368 var r = _used - a._used; | |
369 if (r == 0) { | |
370 var i = _used; | |
371 while (--i >= 0 && (r = _digits[i] - a._digits[i]) == 0); | |
372 } | |
373 return r; | |
374 } | |
375 | |
376 // Return 0 if this == a. | |
377 // Return a positive number if this > a. | |
378 // Return a negative number if this < a. | |
379 int _compareTo(a) { | |
380 var r; | |
381 if (_neg == a._neg) { | |
382 r = _absCompareTo(a); | |
383 if (_neg) { | |
384 r = -r; | |
385 } | |
386 } else if (_neg) { | |
387 r = -1; | |
388 } else { | |
389 r = 1; | |
390 } | |
391 return r; | |
392 } | |
393 | |
394 // r = abs(this) + abs(a). | |
395 void _absAddTo(a, r) { | |
396 if (_used < a._used) { | |
397 a._absAddTo(this, r); | |
398 return; | |
399 } | |
400 if (_used == 0) { | |
401 ZERO._copyTo(r); | |
402 return; | |
403 } | |
404 if (a._used == 0) { | |
405 _copyTo(r); | |
406 return; | |
407 } | |
408 r._ensureLength(_used + 1); | |
409 var c = 0; | |
410 for (var i = 0; i < a._used; i++) { | |
411 c += _digits[i] + a._digits[i]; | |
412 r._digits[i] = c & DIGIT_MASK; | |
413 c >>= DIGIT_BITS; | |
414 } | |
415 for (var i = a._used; i < _used; i++) { | |
416 c += _digits[i]; | |
417 r._digits[i] = c & DIGIT_MASK; | |
418 c >>= DIGIT_BITS; | |
419 } | |
420 r._digits[_used] = c; | |
421 r._used = _used + 1; | |
422 r._clamp(); | |
423 } | |
424 | |
425 // r = abs(this) - abs(a), with abs(this) >= abs(a). | |
426 void _absSubTo(a, r) { | |
427 assert(_absCompareTo(a) >= 0); | |
428 if (_used == 0) { | |
429 ZERO._copyTo(r); | |
430 return; | |
431 } | |
432 if (a._used == 0) { | |
433 _copyTo(r); | |
434 return; | |
435 } | |
436 r._ensureLength(_used); | |
437 var c = 0; | |
438 for (var i = 0; i < a._used; i++) { | |
439 c += _digits[i] - a._digits[i]; | |
440 r._digits[i] = c & DIGIT_MASK; | |
441 c >>= DIGIT_BITS; | |
442 } | |
443 for (var i = a._used; i < _used; i++) { | |
444 c += _digits[i]; | |
445 r._digits[i] = c & DIGIT_MASK; | |
446 c >>= DIGIT_BITS; | |
447 } | |
448 r._used = _used; | |
449 r._clamp(); | |
450 } | |
451 | |
452 // r = abs(this) & abs(a). | |
453 void _absAndTo(a, r) { | |
454 var r_used = (_used < a._used) ? _used : a._used; | |
455 r._ensureLength(r_used); | |
456 for (var i = 0; i < r_used; i++) { | |
457 r._digits[i] = _digits[i] & a._digits[i]; | |
458 } | |
459 r._used = r_used; | |
460 r._clamp(); | |
461 } | |
462 | |
463 // r = abs(this) &~ abs(a). | |
464 void _absAndNotTo(a, r) { | |
465 var r_used = _used; | |
466 r._ensureLength(r_used); | |
467 var m = (r_used < a._used) ? r_used : a._used; | |
468 for (var i = 0; i < m; i++) { | |
469 r._digits[i] = _digits[i] &~ a._digits[i]; | |
470 } | |
471 for (var i = m; i < r_used; i++) { | |
472 r._digits[i] = _digits[i]; | |
473 } | |
474 r._used = r_used; | |
475 r._clamp(); | |
476 } | |
477 | |
478 // r = abs(this) | abs(a). | |
479 void _absOrTo(a, r) { | |
480 var r_used = (_used > a._used) ? _used : a._used; | |
481 r._ensureLength(r_used); | |
482 var l, m; | |
483 if (_used < a._used) { | |
484 l = a; | |
485 m = _used; | |
486 } else { | |
487 l = this; | |
488 m = a._used; | |
489 } | |
490 for (var i = 0; i < m; i++) { | |
491 r._digits[i] = _digits[i] | a._digits[i]; | |
492 } | |
493 for (var i = m; i < r_used; i++) { | |
494 r._digits[i] = l._digits[i]; | |
495 } | |
496 r._used = r_used; | |
497 r._clamp(); | |
498 } | |
499 | |
500 // r = abs(this) ^ abs(a). | |
501 void _absXorTo(a, r) { | |
502 var r_used = (_used > a._used) ? _used : a._used; | |
503 r._ensureLength(r_used); | |
504 var l, m; | |
505 if (_used < a._used) { | |
506 l = a; | |
507 m = _used; | |
508 } else { | |
509 l = this; | |
510 m = a._used; | |
511 } | |
512 for (var i = 0; i < m; i++) { | |
513 r._digits[i] = _digits[i] ^ a._digits[i]; | |
514 } | |
515 for (var i = m; i < r_used; i++) { | |
516 r._digits[i] = l._digits[i]; | |
517 } | |
518 r._used = r_used; | |
519 r._clamp(); | |
520 } | |
521 | |
522 // Return r = this & a. | |
523 _andTo(a, r) { | |
524 if (_neg == a._neg) { | |
525 if (_neg) { | |
526 // (-this) & (-a) == ~(this-1) & ~(a-1) | |
527 // == ~((this-1) | (a-1)) | |
528 // == -(((this-1) | (a-1)) + 1) | |
529 _Bigint t1 = new _Bigint(); | |
530 _absSubTo(ONE, t1); | |
531 _Bigint a1 = new _Bigint(); | |
532 a._absSubTo(ONE, a1); | |
533 t1._absOrTo(a1, r); | |
534 r._absAddTo(ONE, r); | |
535 r._neg = true; // r cannot be zero if this and a are negative. | |
536 return r; | |
537 } | |
538 _absAndTo(a, r); | |
539 r._neg = false; | |
540 return r; | |
541 } | |
542 // _neg != a._neg | |
543 var p, n; | |
544 if (_neg) { | |
545 p = a; | |
546 n = this; | |
547 } else { // & is symmetric. | |
548 p = this; | |
549 n = a; | |
550 } | |
551 // p & (-n) == p & ~(n-1) == p &~ (n-1) | |
552 _Bigint n1 = new _Bigint(); | |
553 n._absSubTo(ONE, n1); | |
554 p._absAndNotTo(n1, r); | |
555 r._neg = false; | |
556 return r; | |
557 } | |
558 | |
559 // Return r = this &~ a. | |
560 _andNotTo(a, r) { | |
561 if (_neg == a._neg) { | |
562 if (_neg) { | |
563 // (-this) &~ (-a) == ~(this-1) &~ ~(a-1) | |
564 // == ~(this-1) & (a-1) | |
565 // == (a-1) &~ (this-1) | |
566 _Bigint t1 = new _Bigint(); | |
567 _absSubTo(ONE, t1); | |
568 _Bigint a1 = new _Bigint(); | |
569 a._absSubTo(ONE, a1); | |
570 a1._absAndNotTo(t1, r); | |
571 r._neg = false; | |
572 return r; | |
573 } | |
574 _absAndNotTo(a, r); | |
575 r._neg = false; | |
576 return r; | |
577 } | |
578 if (_neg) { | |
579 // (-this) &~ a == ~(this-1) &~ a | |
580 // == ~(this-1) & ~a | |
581 // == ~((this-1) | a) | |
582 // == -(((this-1) | a) + 1) | |
583 _Bigint t1 = new _Bigint(); | |
584 _absSubTo(ONE, t1); | |
585 t1._absOrTo(a, r); | |
586 r._absAddTo(ONE, r); | |
587 r._neg = true; // r cannot be zero if this is negative and a is positive. | |
588 return r; | |
589 } | |
590 // this &~ (-a) == this &~ ~(a-1) == this & (a-1) | |
591 _Bigint a1 = new _Bigint(); | |
592 a._absSubTo(ONE, a1); | |
593 _absAndTo(a1, r); | |
594 r._neg = false; | |
595 return r; | |
596 } | |
597 | |
598 // Return r = this | a. | |
599 _orTo(a, r) { | |
600 if (_neg == a._neg) { | |
601 if (_neg) { | |
602 // (-this) | (-a) == ~(this-1) | ~(a-1) | |
603 // == ~((this-1) & (a-1)) | |
604 // == -(((this-1) & (a-1)) + 1) | |
605 _Bigint t1 = new _Bigint(); | |
606 _absSubTo(ONE, t1); | |
607 _Bigint a1 = new _Bigint(); | |
608 a._absSubTo(ONE, a1); | |
609 t1._absAndTo(a1, r); | |
610 r._absAddTo(ONE, r); | |
611 r._neg = true; // r cannot be zero if this and a are negative. | |
612 return r; | |
613 } | |
614 _absOrTo(a, r); | |
615 r._neg = false; | |
616 return r; | |
617 } | |
618 // _neg != a._neg | |
619 var p, n; | |
620 if (_neg) { | |
621 p = a; | |
622 n = this; | |
623 } else { // | is symmetric. | |
624 p = this; | |
625 n = a; | |
626 } | |
627 // p | (-n) == p | ~(n-1) == ~((n-1) &~ p) == -(~((n-1) &~ p) + 1) | |
628 _Bigint n1 = new _Bigint(); | |
629 n._absSubTo(ONE, n1); | |
630 n1._absAndNotTo(p, r); | |
631 r._absAddTo(ONE, r); | |
632 r._neg = true; // r cannot be zero if only one of this or a is negative. | |
633 return r; | |
634 } | |
635 | |
636 // Return r = this ^ a. | |
637 _xorTo(a, r) { | |
638 if (_neg == a._neg) { | |
639 if (_neg) { | |
640 // (-this) ^ (-a) == ~(this-1) ^ ~(a-1) == (this-1) ^ (a-1) | |
641 _Bigint t1 = new _Bigint(); | |
642 _absSubTo(ONE, t1); | |
643 _Bigint a1 = new _Bigint(); | |
644 a._absSubTo(ONE, a1); | |
645 t1._absXorTo(a1, r); | |
646 r._neg = false; | |
647 return r; | |
648 } | |
649 _absXorTo(a, r); | |
650 r._neg = false; | |
651 return r; | |
652 } | |
653 // _neg != a._neg | |
654 var p, n; | |
655 if (_neg) { | |
656 p = a; | |
657 n = this; | |
658 } else { // ^ is symmetric. | |
659 p = this; | |
660 n = a; | |
661 } | |
662 // p ^ (-n) == p ^ ~(n-1) == ~(p ^ (n-1)) == -((p ^ (n-1)) + 1) | |
663 _Bigint n1 = new _Bigint(); | |
664 n._absSubTo(ONE, n1); | |
665 p._absXorTo(n1, r); | |
666 r._absAddTo(ONE, r); | |
667 r._neg = true; // r cannot be zero if only one of this or a is negative. | |
668 return r; | |
669 } | |
670 | |
671 // Return r = ~this. | |
672 _notTo(r) { | |
673 if (_neg) { | |
674 // ~(-this) == ~(~(this-1)) == this-1 | |
675 _absSubTo(ONE, r); | |
676 r._neg = false; | |
677 return r; | |
678 } | |
679 // ~this == -this-1 == -(this+1) | |
680 _absAddTo(ONE, r); | |
681 r._neg = true; // r cannot be zero if this is positive. | |
682 return r; | |
683 } | |
684 | |
685 // Return r = this + a. | |
686 _addTo(a, r) { | |
687 var r_neg = _neg; | |
688 if (_neg == a._neg) { | |
689 // this + a == this + a | |
690 // (-this) + (-a) == -(this + a) | |
691 _absAddTo(a, r); | |
692 } else { | |
693 // this + (-a) == this - a == -(this - a) | |
694 // (-this) + a == a - this == -(this - a) | |
695 if (_absCompareTo(a) >= 0) { | |
696 _absSubTo(a, r); | |
697 } else { | |
698 r_neg = !r_neg; | |
699 a._absSubTo(this, r); | |
700 } | |
701 } | |
702 r._neg = r_neg; | |
703 return r; | |
704 } | |
705 | |
706 // Return r = this - a. | |
707 _subTo(a, r) { | |
708 var r_neg = _neg; | |
709 if (_neg != a._neg) { | |
710 // this - (-a) == this + a | |
711 // (-this) - a == -(this + a) | |
712 _absAddTo(a, r); | |
713 } else { | |
714 // this - a == this - a == -(this - a) | |
715 // (-this) - (-a) == a - this == -(this - a) | |
716 if (_absCompareTo(a) >= 0) { | |
717 _absSubTo(a, r); | |
718 } else { | |
719 r_neg = !r_neg; | |
720 a._absSubTo(this, r); | |
721 } | |
722 } | |
723 r._neg = r_neg; | |
724 return r; | |
725 } | |
726 | |
727 // Accumulate multiply. | |
728 // this[i..i+n-1]: bigint multiplicand. | |
729 // x: digit multiplier. | |
730 // w[j..j+n-1]: bigint accumulator. | |
731 // c: int carry in. | |
732 // Returns carry out. | |
733 // w[j..j+n-1] += this[i..i+n-1] * x + c. | |
734 // Returns carry out. | |
735 // TODO(regis): _sqrTo is the only caller passing an x possibly larger than | |
736 // a digit (2*digit) and passing a non-zero carry in. Refactor? | |
737 int _am(int i, int x, _Bigint w, int j, int c, int n) { | |
738 if (x == 0 && c == 0) { | |
739 // No-op if both x and c are 0. | |
740 return 0; | |
741 } | |
742 int xl = x & DIGIT2_MASK; | |
743 int xh = x >> DIGIT2_BITS; | |
744 while (--n >= 0) { | |
745 int l = _digits[i] & DIGIT2_MASK; | |
746 int h = _digits[i++] >> DIGIT2_BITS; | |
747 int m = xh*l + h*xl; | |
748 l = xl*l + ((m & DIGIT2_MASK) << DIGIT2_BITS) + w._digits[j] + c; | |
749 c = (l >> DIGIT_BITS) + (m >> DIGIT2_BITS) + xh*h; | |
750 w._digits[j++] = l & DIGIT_MASK; | |
751 } | |
752 return c; | |
753 } | |
754 | |
755 // r = this * a. | |
756 void _mulTo(a, r) { | |
757 // TODO(regis): Use karatsuba multiplication when appropriate. | |
758 var i = _used; | |
759 r._ensureLength(i + a._used); | |
760 r._used = i + a._used; | |
761 while (--i >= 0) { | |
762 r._digits[i] = 0; | |
763 } | |
764 for (i = 0; i < a._used; ++i) { | |
765 // TODO(regis): Replace _am with addMulVVW. | |
766 r._digits[i + _used] = _am(0, a._digits[i], r, i, 0, _used); | |
767 } | |
768 r._clamp(); | |
769 r._neg = r._used > 0 && _neg != a._neg; // Zero cannot be negative. | |
770 } | |
771 | |
772 // r = this^2, r != this. | |
773 void _sqrTo(r) { | |
774 var i = 2 * _used; | |
775 r._ensureLength(i); | |
776 r._used = i; | |
777 while (--i >= 0) { | |
778 r._digits[i] = 0; | |
779 } | |
780 for (i = 0; i < _used - 1; ++i) { | |
781 var c = _am(i, _digits[i], r, 2*i, 0, 1); | |
782 var d = r._digits[i + _used]; | |
783 d += _am(i + 1, _digits[i] << 1, r, 2*i + 1, c, _used - i - 1); | |
784 if (d >= DIGIT_BASE) { | |
785 r._digits[i + _used] = d - DIGIT_BASE; | |
786 r._digits[i + _used + 1] = 1; | |
787 } else { | |
788 r._digits[i + _used] = d; | |
789 } | |
790 } | |
791 if (r._used > 0) { | |
792 r._digits[r._used - 1] += _am(i, _digits[i], r, 2*i, 0, 1); | |
793 } | |
794 r._neg = false; | |
795 r._clamp(); | |
796 } | |
797 | |
798 // Truncating division and remainder. | |
799 // If q != null, q = trunc(this / a). | |
800 // If r != null, r = this - a * trunc(this / a). | |
801 void _divRemTo(a, q, r) { | |
802 if (a._used == 0) return; | |
803 if (_used < a._used) { | |
804 if (q != null) { | |
805 // Set q to 0. | |
806 q._neg = false; | |
807 q._used = 0; | |
808 } | |
809 if (r != null) { | |
810 _copyTo(r); | |
811 } | |
812 return; | |
813 } | |
814 if (r == null) { | |
815 r = new _Bigint(); | |
816 } | |
817 var y = new _Bigint(); | |
818 var nsh = DIGIT_BITS - _nbits(a._digits[a._used - 1]); // normalize modulus | |
819 if (nsh > 0) { | |
820 a._lShiftTo(nsh, y); | |
821 _lShiftTo(nsh, r); | |
822 } | |
823 else { | |
824 a._copyTo(y); | |
825 _copyTo(r); | |
826 } | |
827 // We consider this and a positive. Ignore the copied sign. | |
828 y._neg = false; | |
829 r._neg = false; | |
830 var y_used = y._used; | |
831 var y0 = y._digits[y_used - 1]; | |
832 if (y0 == 0) return; | |
833 var yt = y0*(1 << FP_D1) + ((y_used > 1) ? y._digits[y_used - 2] >> FP_D2 : 0); | |
834 var d1 = FP_BASE/yt; | |
835 var d2 = (1 << FP_D1)/yt; | |
836 var e = 1 << FP_D2; | |
837 var i = r._used; | |
838 var j = i - y_used; | |
839 _Bigint t = (q == null) ? new _Bigint() : q; | |
840 | |
841 y._dlShiftTo(j, t); | |
842 | |
843 if (r._compareTo(t) >= 0) { | |
844 r._digits[r._used++] = 1; | |
845 r._subTo(t, r); | |
846 } | |
847 ONE._dlShiftTo(y_used, t); | |
848 t._subTo(y, y); // "negative" y so we can replace sub with _am later | |
849 while (y._used < y_used) { | |
850 y._digits[y._used++] = 0; | |
851 } | |
852 while (--j >= 0) { | |
853 // Estimate quotient digit | |
854 var qd = (r._digits[--i] == y0) | |
855 ? DIGIT_MASK | |
856 : (r._digits[i]*d1 + (r._digits[i - 1] + e)*d2).floor(); | |
857 if ((r._digits[i] += y._am(0, qd, r, j, 0, y_used)) < qd) { // Try it out | |
858 y._dlShiftTo(j, t); | |
859 r._subTo(t, r); | |
860 while (r._digits[i] < --qd) { | |
861 r._subTo(t, r); | |
862 } | |
863 } | |
864 } | |
865 if (q != null) { | |
866 r._drShiftTo(y_used, q); | |
867 if (_neg != a._neg) { | |
868 ZERO._subTo(q, q); | |
869 } | |
870 } | |
871 r._used = y_used; | |
872 r._clamp(); | |
873 if (nsh > 0) { | |
874 r._rShiftTo(nsh, r); // Denormalize remainder | |
875 } | |
876 if (_neg) { | |
877 ZERO._subTo(r, r); | |
878 } | |
879 } | |
880 | |
881 int get _identityHashCode { | |
882 return this; | |
883 } | |
884 int operator ~() { | |
885 _Bigint result = new _Bigint(); | |
886 _notTo(result); | |
887 return result._toValidInt(); | |
888 } | |
889 | |
890 int get bitLength { | |
891 if (_used == 0) return 0; | |
892 if (_neg) return (~this).bitLength; | |
893 return DIGIT_BITS*(_used - 1) + _nbits(_digits[_used - 1]); | |
894 } | |
895 | |
896 // This method must support smi._toBigint()._shrFromInt(int). | |
897 int _shrFromInt(int other) { | |
898 if (_used == 0) return other; // Shift amount is zero. | |
899 if (_neg) throw "negative shift amount"; // TODO(regis): What exception? | |
900 assert(DIGIT_BITS == 32); // Otherwise this code needs to be revised. | |
901 var shift; | |
902 if (_used > 2 || (_used == 2 && _digits[1] > 0x10000000)) { | |
903 if (other < 0) { | |
904 return -1; | |
905 } else { | |
906 return 0; | |
907 } | |
908 } else { | |
909 shift = ((_used == 2) ? (_digits[1] << DIGIT_BITS) : 0) + _digits[0]; | |
910 } | |
911 _Bigint result = new _Bigint(); | |
912 other._toBigint()._rShiftTo(shift, result); | |
913 return result._toValidInt(); | |
914 } | |
915 | |
916 // This method must support smi._toBigint()._shlFromInt(int). | |
917 // An out of memory exception is thrown if the result cannot be allocated. | |
918 int _shlFromInt(int other) { | |
919 if (_used == 0) return other; // Shift amount is zero. | |
920 if (_neg) throw "negative shift amount"; // TODO(regis): What exception? | |
921 assert(DIGIT_BITS == 32); // Otherwise this code needs to be revised. | |
922 var shift; | |
923 if (_used > 2 || (_used == 2 && _digits[1] > 0x10000000)) { | |
924 throw new OutOfMemoryError(); | |
925 } else { | |
926 shift = ((_used == 2) ? (_digits[1] << DIGIT_BITS) : 0) + _digits[0]; | |
927 } | |
928 _Bigint result = new _Bigint(); | |
929 other._toBigint()._lShiftTo(shift, result); | |
930 return result._toValidInt(); | |
931 } | |
932 | |
933 int pow(int exponent) { | |
934 throw "Bigint.pow not implemented"; | |
935 } | |
936 | |
937 // Overriden operators and methods. | |
938 | |
939 int operator -() { | |
940 if (_used == 0) { | |
941 return this; | |
942 } | |
943 var r = new _Bigint(); | |
944 _copyTo(r); | |
945 r._neg = !_neg; | |
946 return r._toValidInt(); | |
947 } | |
948 | |
949 int get sign { | |
950 return (_used == 0) ? 0 : _neg ? -1 : 1; | |
951 } | |
952 | |
953 bool get isEven => _used == 0 || (_digits[0] & 1) == 0; | |
954 bool get isNegative => _neg; | |
955 | |
956 _leftShiftWithMask32(count, mask) { | |
957 if (_used == 0) return 0; | |
958 if (count is! _Smi) { | |
959 _shlFromInt(count); // Throws out of memory exception. | |
960 } | |
961 assert(DIGIT_BITS == 32); // Otherwise this code needs to be revised. | |
962 if (count > 31) return 0; | |
963 return (_digits[0] << count) & mask; | |
964 } | |
965 | |
966 int _bitAndFromInteger(int other) { | |
967 _Bigint result = new _Bigint(); | |
968 other._toBigint()._andTo(this, result); | |
969 return result._toValidInt(); | |
970 } | |
971 int _bitOrFromInteger(int other) { | |
972 _Bigint result = new _Bigint(); | |
973 other._toBigint()._orTo(this, result); | |
974 return result._toValidInt(); | |
975 } | |
976 int _bitXorFromInteger(int other) { | |
977 _Bigint result = new _Bigint(); | |
978 other._toBigint()._xorTo(this, result); | |
979 return result._toValidInt(); | |
980 } | |
981 int _addFromInteger(int other) { | |
982 _Bigint result = new _Bigint(); | |
983 other._toBigint()._addTo(this, result); | |
984 return result._toValidInt(); | |
985 } | |
986 int _subFromInteger(int other) { | |
987 _Bigint result = new _Bigint(); | |
988 other._toBigint()._subTo(this, result); | |
989 return result._toValidInt(); | |
990 } | |
991 int _mulFromInteger(int other) { | |
992 _Bigint result = new _Bigint(); | |
993 other._toBigint()._mulTo(this, result); | |
994 return result._toValidInt(); | |
995 } | |
996 int _truncDivFromInteger(int other) { | |
997 _Bigint result = new _Bigint(); | |
998 other._toBigint()._divRemTo(this, result, null); | |
999 return result._toValidInt(); | |
1000 } | |
1001 int _moduloFromInteger(int other) { | |
1002 _Bigint result = new _Bigint(); | |
1003 var ob = other._toBigint(); | |
1004 other._toBigint()._divRemTo(this, null, result); | |
1005 if (result._neg) { | |
1006 if (_neg) { | |
1007 result._subTo(this, result); | |
1008 } else { | |
1009 result._addTo(this, result); | |
1010 } | |
1011 } | |
1012 return result._toValidInt(); | |
1013 } | |
1014 int _remainderFromInteger(int other) { | |
1015 _Bigint result = new _Bigint(); | |
1016 other._toBigint()._divRemTo(this, null, result); | |
1017 return result._toValidInt(); | |
1018 } | |
1019 bool _greaterThanFromInteger(int other) { | |
1020 return other._toBigint()._compareTo(this) > 0; | |
1021 } | |
1022 bool _equalToInteger(int other) { | |
1023 return other._toBigint()._compareTo(this) == 0; | |
1024 } | |
1025 | |
1026 // New method to support crypto. | |
1027 | |
1028 // Return this.pow(e) mod m, with 256 <= e < 1<<32. | |
1029 int modPow(int e, int m) { | |
1030 assert(e >= 256 && !m.isEven()); | |
1031 if (e >= (1 << 32)) { | |
1032 throw "Bigint.modPow with exponent larger than 32-bit not implemented"; | |
1033 } | |
1034 _Reduction z = new _Montgomery(m); | |
1035 var r = new _Bigint(); | |
1036 var r2 = new _Bigint(); | |
1037 var g = z.convert(this); | |
1038 int i = _nbits(e) - 1; | |
1039 g._copyTo(r); | |
1040 while (--i >= 0) { | |
1041 z.sqrTo(r, r2); | |
1042 if ((e & (1 << i)) > 0) { | |
1043 z.mulTo(r2, g, r); | |
1044 } else { | |
1045 var t = r; | |
1046 r = r2; | |
1047 r2 = t; | |
1048 } | |
1049 } | |
1050 return z.revert(r)._toValidInt(); | |
1051 } | |
1052 | |
1053 | |
1054 /* | |
1055 // Static helpers _used to intrinsify primitive operations on _digits. | |
1056 static int _mulWW(int x, int y) { | |
1057 x &= DIGIT_MASK; | |
1058 y &= DIGIT_MASK; | |
1059 return x*y; | |
1060 } | |
1061 | |
1062 static int _remainder; | |
1063 | |
1064 static int _divWW(int x, int y) { | |
1065 x &= TWO_DIGIT_MASK; | |
1066 y &= DIGIT_MASK; | |
1067 _remainder = x % y; | |
1068 return x ~/ y; | |
1069 } | |
1070 } | |
1071 */ | |
1072 } | |
1073 | |
1074 // New classes to support crypto (modPow method). | |
1075 | |
1076 class _Reduction { | |
1077 const _Reduction(); | |
1078 _Bigint _convert(_Bigint x) => x; | |
1079 _Bigint _revert(_Bigint x) => x; | |
1080 void _mulTo(_Bigint x, _Bigint y, _Bigint r) { | |
1081 x._mulTo(y, r); | |
1082 } | |
1083 void _sqrTo(_Bigint x, _Bigint r) { | |
1084 x._sqrTo(r); | |
1085 } | |
1086 } | |
1087 | |
1088 // Montgomery reduction on _Bigint. | |
1089 class _Montgomery implements _Reduction { | |
1090 final _Bigint _m; | |
1091 var _mp; | |
1092 var _mpl; | |
1093 var _mph; | |
1094 var _um; | |
1095 var _mused2; | |
1096 | |
1097 _Montgomery(this._m) { | |
1098 _mp = _m._invDigit(); | |
1099 _mpl = _mp & _Bigint.DIGIT2_MASK; | |
1100 _mph = _mp >> _Bigint.DIGIT2_BITS; | |
1101 _um = (1 << (_Bigint.DIGIT_BITS - _Bigint.DIGIT2_BITS)) - 1; | |
1102 _mused2 = 2*_m._used; | |
1103 } | |
1104 | |
1105 // Return x*R mod _m | |
1106 _Bigint _convert(_Bigint x) { | |
1107 var r = new _Bigint(); | |
1108 x.abs()._dlShiftTo(_m._used, r); | |
1109 r._divRemTo(_m, null, r); | |
1110 if (x._neg && r._compareTo(_Bigint.ZERO) > 0) { | |
1111 _m._subTo(r, r); | |
1112 } | |
1113 return r; | |
1114 } | |
1115 | |
1116 // Return x/R mod _m | |
1117 _Bigint _revert(_Bigint x) { | |
1118 var r = new _Bigint(); | |
1119 x._copyTo(r); | |
1120 _reduce(r); | |
1121 return r; | |
1122 } | |
1123 | |
1124 // x = x/R mod _m | |
1125 void _reduce(_Bigint x) { | |
1126 x._ensureLength(_mused2 + 1); | |
1127 while (x._used <= _mused2) { // Pad x so _am has enough room later. | |
1128 x._digits[x._used++] = 0; | |
1129 } | |
1130 for (var i = 0; i < _m._used; ++i) { | |
1131 // Faster way of calculating u0 = x[i]*mp mod DIGIT_BASE. | |
1132 var j = x._digits[i] & _Bigint.DIGIT2_MASK; | |
1133 var u0 = (j*_mpl + (((j*_mph + (x._digits[i] >> _Bigint.DIGIT2_BITS) | |
1134 *_mpl) & _um) << _Bigint.DIGIT2_BITS)) & _Bigint.DIGIT_MASK; | |
1135 // Use _am to combine the multiply-shift-add into one call. | |
1136 j = i + _m._used; | |
1137 var digit = x._digits[j]; | |
1138 digit += _m ._am(0, u0, x, i, 0, _m ._used); | |
1139 // propagate carry | |
1140 while (digit >= _Bigint.DIGIT_BASE) { | |
1141 digit -= _Bigint.DIGIT_BASE; | |
1142 x._digits[j++] = digit; | |
1143 digit = x._digits[j]; | |
1144 digit++; | |
1145 } | |
1146 x._digits[j] = digit; | |
1147 } | |
1148 x._clamp(); | |
1149 x._drShiftTo(_m ._used, x); | |
1150 if (x._compareTo(_m ) >= 0) { | |
1151 x._subTo(_m , x); | |
1152 } | |
1153 } | |
1154 | |
1155 // r = x^2/R mod _m ; x != r | |
1156 void _sqrTo(_Bigint x, _Bigint r) { | |
1157 x._sqrTo(r); | |
1158 _reduce(r); | |
1159 } | |
1160 | |
1161 // r = x*y/R mod _m ; x, y != r | |
1162 void _mulTo(_Bigint x, _Bigint y, _Bigint r) { | |
1163 x._mulTo(y, r); | |
1164 _reduce(r); | |
1165 } | |
1166 } | |
1167 | |
OLD | NEW |