1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This file implements signed multi-precision integers.
17 // An Int represents a signed multi-precision integer.
18 // The zero value for an Int represents the value 0.
21 abs nat
// absolute value of the integer
24 var intOne
= &Int
{false, natOne
}
32 func (x
*Int
) Sign() int {
42 // SetInt64 sets z to x and returns z.
43 func (z
*Int
) SetInt64(x
int64) *Int
{
49 z
.abs
= z
.abs
.setUint64(uint64(x
))
54 // SetUint64 sets z to x and returns z.
55 func (z
*Int
) SetUint64(x
uint64) *Int
{
56 z
.abs
= z
.abs
.setUint64(x
)
61 // NewInt allocates and returns a new Int set to x.
62 func NewInt(x
int64) *Int
{
63 return new(Int
).SetInt64(x
)
66 // Set sets z to x and returns z.
67 func (z
*Int
) Set(x
*Int
) *Int
{
69 z
.abs
= z
.abs
.set(x
.abs
)
75 // Bits provides raw (unchecked but fast) access to x by returning its
76 // absolute value as a little-endian Word slice. The result and x share
77 // the same underlying array.
78 // Bits is intended to support implementation of missing low-level Int
79 // functionality outside this package; it should be avoided otherwise.
80 func (x
*Int
) Bits() []Word
{
84 // SetBits provides raw (unchecked but fast) access to z by setting its
85 // value to abs, interpreted as a little-endian Word slice, and returning
86 // z. The result and abs share the same underlying array.
87 // SetBits is intended to support implementation of missing low-level Int
88 // functionality outside this package; it should be avoided otherwise.
89 func (z
*Int
) SetBits(abs
[]Word
) *Int
{
90 z
.abs
= nat(abs
).norm()
95 // Abs sets z to |x| (the absolute value of x) and returns z.
96 func (z
*Int
) Abs(x
*Int
) *Int
{
102 // Neg sets z to -x and returns z.
103 func (z
*Int
) Neg(x
*Int
) *Int
{
105 z
.neg
= len(z
.abs
) > 0 && !z
.neg
// 0 has no sign
109 // Add sets z to the sum x+y and returns z.
110 func (z
*Int
) Add(x
, y
*Int
) *Int
{
114 // (-x) + (-y) == -(x + y)
115 z
.abs
= z
.abs
.add(x
.abs
, y
.abs
)
117 // x + (-y) == x - y == -(y - x)
118 // (-x) + y == y - x == -(x - y)
119 if x
.abs
.cmp(y
.abs
) >= 0 {
120 z
.abs
= z
.abs
.sub(x
.abs
, y
.abs
)
123 z
.abs
= z
.abs
.sub(y
.abs
, x
.abs
)
126 z
.neg
= len(z
.abs
) > 0 && neg
// 0 has no sign
130 // Sub sets z to the difference x-y and returns z.
131 func (z
*Int
) Sub(x
, y
*Int
) *Int
{
135 // (-x) - y == -(x + y)
136 z
.abs
= z
.abs
.add(x
.abs
, y
.abs
)
138 // x - y == x - y == -(y - x)
139 // (-x) - (-y) == y - x == -(x - y)
140 if x
.abs
.cmp(y
.abs
) >= 0 {
141 z
.abs
= z
.abs
.sub(x
.abs
, y
.abs
)
144 z
.abs
= z
.abs
.sub(y
.abs
, x
.abs
)
147 z
.neg
= len(z
.abs
) > 0 && neg
// 0 has no sign
151 // Mul sets z to the product x*y and returns z.
152 func (z
*Int
) Mul(x
, y
*Int
) *Int
{
154 // x * (-y) == -(x * y)
155 // (-x) * y == -(x * y)
156 // (-x) * (-y) == x * y
157 z
.abs
= z
.abs
.mul(x
.abs
, y
.abs
)
158 z
.neg
= len(z
.abs
) > 0 && x
.neg
!= y
.neg
// 0 has no sign
162 // MulRange sets z to the product of all integers
163 // in the range [a, b] inclusively and returns z.
164 // If a > b (empty range), the result is 1.
165 func (z
*Int
) MulRange(a
, b
int64) *Int
{
168 return z
.SetInt64(1) // empty range
169 case a
<= 0 && b
>= 0:
170 return z
.SetInt64(0) // range includes 0
172 // a <= b && (b < 0 || a > 0)
180 z
.abs
= z
.abs
.mulRange(uint64(a
), uint64(b
))
185 // Binomial sets z to the binomial coefficient of (n, k) and returns z.
186 func (z
*Int
) Binomial(n
, k
int64) *Int
{
193 // Quo sets z to the quotient x/y for y != 0 and returns z.
194 // If y == 0, a division-by-zero run-time panic occurs.
195 // Quo implements truncated division (like Go); see QuoRem for more details.
196 func (z
*Int
) Quo(x
, y
*Int
) *Int
{
197 z
.abs
, _
= z
.abs
.div(nil, x
.abs
, y
.abs
)
198 z
.neg
= len(z
.abs
) > 0 && x
.neg
!= y
.neg
// 0 has no sign
202 // Rem sets z to the remainder x%y for y != 0 and returns z.
203 // If y == 0, a division-by-zero run-time panic occurs.
204 // Rem implements truncated modulus (like Go); see QuoRem for more details.
205 func (z
*Int
) Rem(x
, y
*Int
) *Int
{
206 _
, z
.abs
= nat(nil).div(z
.abs
, x
.abs
, y
.abs
)
207 z
.neg
= len(z
.abs
) > 0 && x
.neg
// 0 has no sign
211 // QuoRem sets z to the quotient x/y and r to the remainder x%y
212 // and returns the pair (z, r) for y != 0.
213 // If y == 0, a division-by-zero run-time panic occurs.
215 // QuoRem implements T-division and modulus (like Go):
217 // q = x/y with the result truncated to zero
220 // (See Daan Leijen, ``Division and Modulus for Computer Scientists''.)
221 // See DivMod for Euclidean division and modulus (unlike Go).
223 func (z
*Int
) QuoRem(x
, y
, r
*Int
) (*Int
, *Int
) {
224 z
.abs
, r
.abs
= z
.abs
.div(r
.abs
, x
.abs
, y
.abs
)
225 z
.neg
, r
.neg
= len(z
.abs
) > 0 && x
.neg
!= y
.neg
, len(r
.abs
) > 0 && x
.neg
// 0 has no sign
229 // Div sets z to the quotient x/y for y != 0 and returns z.
230 // If y == 0, a division-by-zero run-time panic occurs.
231 // Div implements Euclidean division (unlike Go); see DivMod for more details.
232 func (z
*Int
) Div(x
, y
*Int
) *Int
{
233 y_neg
:= y
.neg
// z may be an alias for y
246 // Mod sets z to the modulus x%y for y != 0 and returns z.
247 // If y == 0, a division-by-zero run-time panic occurs.
248 // Mod implements Euclidean modulus (unlike Go); see DivMod for more details.
249 func (z
*Int
) Mod(x
, y
*Int
) *Int
{
251 if z
== y ||
alias(z
.abs
, y
.abs
) {
266 // DivMod sets z to the quotient x div y and m to the modulus x mod y
267 // and returns the pair (z, m) for y != 0.
268 // If y == 0, a division-by-zero run-time panic occurs.
270 // DivMod implements Euclidean division and modulus (unlike Go):
272 // q = x div y such that
273 // m = x - y*q with 0 <= m < |q|
275 // (See Raymond T. Boute, ``The Euclidean definition of the functions
276 // div and mod''. ACM Transactions on Programming Languages and
277 // Systems (TOPLAS), 14(2):127-144, New York, NY, USA, 4/1992.
279 // See QuoRem for T-division and modulus (like Go).
281 func (z
*Int
) DivMod(x
, y
, m
*Int
) (*Int
, *Int
) {
283 if z
== y ||
alias(z
.abs
, y
.abs
) {
299 // Cmp compares x and y and returns:
305 func (x
*Int
) Cmp(y
*Int
) (r
int) {
306 // x cmp y == x cmp y
309 // (-x) cmp (-y) == -(x cmp y)
324 func (x
*Int
) String() string {
329 return "-" + x
.abs
.decimalString()
331 return x
.abs
.decimalString()
334 func charset(ch rune
) string {
337 return lowercaseDigits
[0:2]
339 return lowercaseDigits
[0:8]
341 return lowercaseDigits
[0:10]
343 return lowercaseDigits
[0:16]
345 return uppercaseDigits
[0:16]
347 return "" // unknown format
350 // write count copies of text to s
351 func writeMultiple(s fmt
.State
, text
string, count
int) {
354 for ; count
> 0; count
-- {
360 // Format is a support routine for fmt.Formatter. It accepts
361 // the formats 'b' (binary), 'o' (octal), 'd' (decimal), 'x'
362 // (lowercase hexadecimal), and 'X' (uppercase hexadecimal).
363 // Also supported are the full suite of package fmt's format
364 // verbs for integral types, including '+', '-', and ' '
365 // for sign control, '#' for leading zero in octal and for
366 // hexadecimal, a leading "0x" or "0X" for "%#x" and "%#X"
367 // respectively, specification of minimum digits precision,
368 // output field width, space or zero padding, and left or
369 // right justification.
371 func (x
*Int
) Format(s fmt
.State
, ch rune
) {
378 fmt
.Fprintf(s
, "%%!%c(big.Int=%s)", ch
, x
.String())
381 fmt
.Fprint(s
, "<nil>")
385 // determine sign character
390 case s
.Flag('+'): // supersedes ' ' when both specified
396 // determine prefix characters for indicating output base
402 case 'x': // hexadecimal
409 // determine digits with base set by len(cs) and digit characters from cs
410 digits
:= x
.abs
.string(cs
)
412 // number of characters for the three classes of number padding
413 var left
int // space characters to left of digits for right justification ("%8d")
414 var zeroes
int // zero characters (actually cs[0]) as left-most digits ("%.8d")
415 var right
int // space characters to right of digits for left justification ("%-8d")
417 // determine number padding from precision: the least number of digits to output
418 precision
, precisionSet
:= s
.Precision()
421 case len(digits
) < precision
:
422 zeroes
= precision
- len(digits
) // count of zero padding
423 case digits
== "0" && precision
== 0:
424 return // print nothing if zero value (x == 0) and zero precision ("." or ".0")
428 // determine field pad from width: the least number of characters to output
429 length
:= len(sign
) + len(prefix
) + zeroes
+ len(digits
)
430 if width
, widthSet
:= s
.Width(); widthSet
&& length
< width
{ // pad as specified
431 switch d
:= width
- length
; {
433 // pad on the right with spaces; supersedes '0' when both specified
435 case s
.Flag('0') && !precisionSet
:
436 // pad with zeroes unless precision also specified
439 // pad on the left with spaces
444 // print number as [left pad][sign][prefix][zero pad][digits][right pad]
445 writeMultiple(s
, " ", left
)
446 writeMultiple(s
, sign
, 1)
447 writeMultiple(s
, prefix
, 1)
448 writeMultiple(s
, "0", zeroes
)
449 writeMultiple(s
, digits
, 1)
450 writeMultiple(s
, " ", right
)
453 // scan sets z to the integer value corresponding to the longest possible prefix
454 // read from r representing a signed integer number in a given conversion base.
455 // It returns z, the actual conversion base used, and an error, if any. In the
456 // error case, the value of z is undefined but the returned value is nil. The
457 // syntax follows the syntax of integer literals in Go.
459 // The base argument must be 0 or a value from 2 through MaxBase. If the base
460 // is 0, the string prefix determines the actual conversion base. A prefix of
461 // ``0x'' or ``0X'' selects base 16; the ``0'' prefix selects base 8, and a
462 // ``0b'' or ``0B'' prefix selects base 2. Otherwise the selected base is 10.
464 func (z
*Int
) scan(r io
.RuneScanner
, base
int) (*Int
, int, error
) {
466 ch
, _
, err
:= r
.ReadRune()
474 case '+': // nothing to do
479 // determine mantissa
480 z
.abs
, base
, err
= z
.abs
.scan(r
, base
)
482 return nil, base
, err
484 z
.neg
= len(z
.abs
) > 0 && neg
// 0 has no sign
489 // Scan is a support routine for fmt.Scanner; it sets z to the value of
490 // the scanned number. It accepts the formats 'b' (binary), 'o' (octal),
491 // 'd' (decimal), 'x' (lowercase hexadecimal), and 'X' (uppercase hexadecimal).
492 func (z
*Int
) Scan(s fmt
.ScanState
, ch rune
) error
{
493 s
.SkipSpace() // skip leading space characters
505 // let scan determine the base
507 return errors
.New("Int.Scan: invalid verb")
509 _
, _
, err
:= z
.scan(s
, base
)
513 // Int64 returns the int64 representation of x.
514 // If x cannot be represented in an int64, the result is undefined.
515 func (x
*Int
) Int64() int64 {
516 v
:= int64(x
.Uint64())
523 // Uint64 returns the uint64 representation of x.
524 // If x cannot be represented in a uint64, the result is undefined.
525 func (x
*Int
) Uint64() uint64 {
529 v
:= uint64(x
.abs
[0])
530 if _W
== 32 && len(x
.abs
) > 1 {
531 v |
= uint64(x
.abs
[1]) << 32
536 // SetString sets z to the value of s, interpreted in the given base,
537 // and returns z and a boolean indicating success. If SetString fails,
538 // the value of z is undefined but the returned value is nil.
540 // The base argument must be 0 or a value from 2 through MaxBase. If the base
541 // is 0, the string prefix determines the actual conversion base. A prefix of
542 // ``0x'' or ``0X'' selects base 16; the ``0'' prefix selects base 8, and a
543 // ``0b'' or ``0B'' prefix selects base 2. Otherwise the selected base is 10.
545 func (z
*Int
) SetString(s
string, base
int) (*Int
, bool) {
546 r
:= strings
.NewReader(s
)
547 _
, _
, err
:= z
.scan(r
, base
)
551 _
, _
, err
= r
.ReadRune()
555 return z
, true // err == io.EOF => scan consumed all of s
558 // SetBytes interprets buf as the bytes of a big-endian unsigned
559 // integer, sets z to that value, and returns z.
560 func (z
*Int
) SetBytes(buf
[]byte) *Int
{
561 z
.abs
= z
.abs
.setBytes(buf
)
566 // Bytes returns the absolute value of x as a big-endian byte slice.
567 func (x
*Int
) Bytes() []byte {
568 buf
:= make([]byte, len(x
.abs
)*_S
)
569 return buf
[x
.abs
.bytes(buf
):]
572 // BitLen returns the length of the absolute value of x in bits.
573 // The bit length of 0 is 0.
574 func (x
*Int
) BitLen() int {
575 return x
.abs
.bitLen()
578 // Exp sets z = x**y mod |m| (i.e. the sign of m is ignored), and returns z.
579 // If y <= 0, the result is 1; if m == nil or m == 0, z = x**y.
580 // See Knuth, volume 2, section 4.6.3.
581 func (z
*Int
) Exp(x
, y
, m
*Int
) *Int
{
582 if y
.neg ||
len(y
.abs
) == 0 {
589 mWords
= m
.abs
// m.abs may be nil for m == 0
592 z
.abs
= z
.abs
.expNN(x
.abs
, y
.abs
, mWords
)
593 z
.neg
= len(z
.abs
) > 0 && x
.neg
&& y
.abs
[0]&1 == 1 // 0 has no sign
597 // GCD sets z to the greatest common divisor of a and b, which both must
598 // be > 0, and returns z.
599 // If x and y are not nil, GCD sets x and y such that z = a*x + b*y.
600 // If either a or b is <= 0, GCD sets z = x = y = 0.
601 func (z
*Int
) GCD(x
, y
, a
, b
*Int
) *Int
{
602 if a
.Sign() <= 0 || b
.Sign() <= 0 {
612 if x
== nil && y
== nil {
613 return z
.binaryGCD(a
, b
)
620 Y
:= new(Int
).SetInt64(1)
622 lastX
:= new(Int
).SetInt64(1)
630 q
, r
= q
.QuoRem(A
, B
, r
)
659 // binaryGCD sets z to the greatest common divisor of a and b, which both must
660 // be > 0, and returns z.
661 // See Knuth, The Art of Computer Programming, Vol. 2, Section 4.5.2, Algorithm B.
662 func (z
*Int
) binaryGCD(a
, b
*Int
) *Int
{
666 // use one Euclidean iteration to ensure that u and v are approx. the same size
668 case len(a
.abs
) > len(b
.abs
):
671 case len(a
.abs
) < len(b
.abs
):
685 // determine largest k such that u = u' << k, v = v' << k
686 k
:= u
.abs
.trailingZeroBits()
687 if vk
:= v
.abs
.trailingZeroBits(); vk
< k
{
693 // determine t (we know that u > 0)
704 t
.Rsh(t
, t
.abs
.trailingZeroBits())
707 v
.neg
= len(v
.abs
) > 0 && !v
.neg
// 0 has no sign
717 // ProbablyPrime performs n Miller-Rabin tests to check whether x is prime.
718 // If it returns true, x is prime with probability 1 - 1/4^n.
719 // If it returns false, x is not prime.
720 func (x
*Int
) ProbablyPrime(n
int) bool {
721 return !x
.neg
&& x
.abs
.probablyPrime(n
)
724 // Rand sets z to a pseudo-random number in [0, n) and returns z.
725 func (z
*Int
) Rand(rnd
*rand
.Rand
, n
*Int
) *Int
{
727 if n
.neg
== true ||
len(n
.abs
) == 0 {
731 z
.abs
= z
.abs
.random(rnd
, n
.abs
, n
.abs
.bitLen())
735 // ModInverse sets z to the multiplicative inverse of g in the group ℤ/pℤ (where
736 // p is a prime) and returns z.
737 func (z
*Int
) ModInverse(g
, p
*Int
) *Int
{
740 // x and y are such that g*x + p*y = d. Since p is prime, d = 1. Taking
741 // that modulo p results in g*x = 1, therefore x is the inverse element.
748 // Lsh sets z = x << n and returns z.
749 func (z
*Int
) Lsh(x
*Int
, n
uint) *Int
{
750 z
.abs
= z
.abs
.shl(x
.abs
, n
)
755 // Rsh sets z = x >> n and returns z.
756 func (z
*Int
) Rsh(x
*Int
, n
uint) *Int
{
758 // (-x) >> s == ^(x-1) >> s == ^((x-1) >> s) == -(((x-1) >> s) + 1)
759 t
:= z
.abs
.sub(x
.abs
, natOne
) // no underflow because |x| > 0
761 z
.abs
= t
.add(t
, natOne
)
762 z
.neg
= true // z cannot be zero if x is negative
766 z
.abs
= z
.abs
.shr(x
.abs
, n
)
771 // Bit returns the value of the i'th bit of x. That is, it
772 // returns (x>>i)&1. The bit index i must be >= 0.
773 func (x
*Int
) Bit(i
int) uint {
775 // optimization for common case: odd/even test of x
777 return uint(x
.abs
[0] & 1) // bit 0 is same for -x
782 panic("negative bit index")
785 t
:= nat(nil).sub(x
.abs
, natOne
)
786 return t
.bit(uint(i
)) ^ 1
789 return x
.abs
.bit(uint(i
))
792 // SetBit sets z to x, with x's i'th bit set to b (0 or 1).
793 // That is, if b is 1 SetBit sets z = x | (1 << i);
794 // if b is 0 SetBit sets z = x &^ (1 << i). If b is not 0 or 1,
795 // SetBit will panic.
796 func (z
*Int
) SetBit(x
*Int
, i
int, b
uint) *Int
{
798 panic("negative bit index")
801 t
:= z
.abs
.sub(x
.abs
, natOne
)
802 t
= t
.setBit(t
, uint(i
), b
^1)
803 z
.abs
= t
.add(t
, natOne
)
804 z
.neg
= len(z
.abs
) > 0
807 z
.abs
= z
.abs
.setBit(x
.abs
, uint(i
), b
)
812 // And sets z = x & y and returns z.
813 func (z
*Int
) And(x
, y
*Int
) *Int
{
816 // (-x) & (-y) == ^(x-1) & ^(y-1) == ^((x-1) | (y-1)) == -(((x-1) | (y-1)) + 1)
817 x1
:= nat(nil).sub(x
.abs
, natOne
)
818 y1
:= nat(nil).sub(y
.abs
, natOne
)
819 z
.abs
= z
.abs
.add(z
.abs
.or(x1
, y1
), natOne
)
820 z
.neg
= true // z cannot be zero if x and y are negative
825 z
.abs
= z
.abs
.and(x
.abs
, y
.abs
)
832 x
, y
= y
, x
// & is symmetric
835 // x & (-y) == x & ^(y-1) == x &^ (y-1)
836 y1
:= nat(nil).sub(y
.abs
, natOne
)
837 z
.abs
= z
.abs
.andNot(x
.abs
, y1
)
842 // AndNot sets z = x &^ y and returns z.
843 func (z
*Int
) AndNot(x
, y
*Int
) *Int
{
846 // (-x) &^ (-y) == ^(x-1) &^ ^(y-1) == ^(x-1) & (y-1) == (y-1) &^ (x-1)
847 x1
:= nat(nil).sub(x
.abs
, natOne
)
848 y1
:= nat(nil).sub(y
.abs
, natOne
)
849 z
.abs
= z
.abs
.andNot(y1
, x1
)
855 z
.abs
= z
.abs
.andNot(x
.abs
, y
.abs
)
861 // (-x) &^ y == ^(x-1) &^ y == ^(x-1) & ^y == ^((x-1) | y) == -(((x-1) | y) + 1)
862 x1
:= nat(nil).sub(x
.abs
, natOne
)
863 z
.abs
= z
.abs
.add(z
.abs
.or(x1
, y
.abs
), natOne
)
864 z
.neg
= true // z cannot be zero if x is negative and y is positive
868 // x &^ (-y) == x &^ ^(y-1) == x & (y-1)
869 y1
:= nat(nil).add(y
.abs
, natOne
)
870 z
.abs
= z
.abs
.and(x
.abs
, y1
)
875 // Or sets z = x | y and returns z.
876 func (z
*Int
) Or(x
, y
*Int
) *Int
{
879 // (-x) | (-y) == ^(x-1) | ^(y-1) == ^((x-1) & (y-1)) == -(((x-1) & (y-1)) + 1)
880 x1
:= nat(nil).sub(x
.abs
, natOne
)
881 y1
:= nat(nil).sub(y
.abs
, natOne
)
882 z
.abs
= z
.abs
.add(z
.abs
.and(x1
, y1
), natOne
)
883 z
.neg
= true // z cannot be zero if x and y are negative
888 z
.abs
= z
.abs
.or(x
.abs
, y
.abs
)
895 x
, y
= y
, x
// | is symmetric
898 // x | (-y) == x | ^(y-1) == ^((y-1) &^ x) == -(^((y-1) &^ x) + 1)
899 y1
:= nat(nil).sub(y
.abs
, natOne
)
900 z
.abs
= z
.abs
.add(z
.abs
.andNot(y1
, x
.abs
), natOne
)
901 z
.neg
= true // z cannot be zero if one of x or y is negative
905 // Xor sets z = x ^ y and returns z.
906 func (z
*Int
) Xor(x
, y
*Int
) *Int
{
909 // (-x) ^ (-y) == ^(x-1) ^ ^(y-1) == (x-1) ^ (y-1)
910 x1
:= nat(nil).sub(x
.abs
, natOne
)
911 y1
:= nat(nil).sub(y
.abs
, natOne
)
912 z
.abs
= z
.abs
.xor(x1
, y1
)
918 z
.abs
= z
.abs
.xor(x
.abs
, y
.abs
)
925 x
, y
= y
, x
// ^ is symmetric
928 // x ^ (-y) == x ^ ^(y-1) == ^(x ^ (y-1)) == -((x ^ (y-1)) + 1)
929 y1
:= nat(nil).sub(y
.abs
, natOne
)
930 z
.abs
= z
.abs
.add(z
.abs
.xor(x
.abs
, y1
), natOne
)
931 z
.neg
= true // z cannot be zero if only one of x or y is negative
935 // Not sets z = ^x and returns z.
936 func (z
*Int
) Not(x
*Int
) *Int
{
938 // ^(-x) == ^(^(x-1)) == x-1
939 z
.abs
= z
.abs
.sub(x
.abs
, natOne
)
944 // ^x == -x-1 == -(x+1)
945 z
.abs
= z
.abs
.add(x
.abs
, natOne
)
946 z
.neg
= true // z cannot be zero if x is positive
950 // Gob codec version. Permits backward-compatible changes to the encoding.
951 const intGobVersion
byte = 1
953 // GobEncode implements the gob.GobEncoder interface.
954 func (x
*Int
) GobEncode() ([]byte, error
) {
958 buf
:= make([]byte, 1+len(x
.abs
)*_S
) // extra byte for version and sign bit
959 i
:= x
.abs
.bytes(buf
) - 1 // i >= 0
960 b
:= intGobVersion
<< 1 // make space for sign bit
968 // GobDecode implements the gob.GobDecoder interface.
969 func (z
*Int
) GobDecode(buf
[]byte) error
{
971 // Other side sent a nil or default value.
976 if b
>>1 != intGobVersion
{
977 return errors
.New(fmt
.Sprintf("Int.GobDecode: encoding version %d not supported", b
>>1))
980 z
.abs
= z
.abs
.setBytes(buf
[1:])
984 // MarshalJSON implements the json.Marshaler interface.
985 func (x
*Int
) MarshalJSON() ([]byte, error
) {
986 // TODO(gri): get rid of the []byte/string conversions
987 return []byte(x
.String()), nil
990 // UnmarshalJSON implements the json.Unmarshaler interface.
991 func (z
*Int
) UnmarshalJSON(x
[]byte) error
{
992 // TODO(gri): get rid of the []byte/string conversions
993 _
, ok
:= z
.SetString(string(x
), 0)
995 return fmt
.Errorf("math/big: cannot unmarshal %s into a *big.Int", x
)