Source file
src/math/big/int.go
1
2
3
4
5
6
7 package big
8
9 import (
10 "fmt"
11 "io"
12 "math/rand"
13 "strings"
14 )
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 type Int struct {
34 neg bool
35 abs nat
36 }
37
38 var intOne = &Int{false, natOne}
39
40
41
42
43
44
45 func (x *Int) Sign() int {
46
47
48
49 if len(x.abs) == 0 {
50 return 0
51 }
52 if x.neg {
53 return -1
54 }
55 return 1
56 }
57
58
59 func (z *Int) SetInt64(x int64) *Int {
60 neg := false
61 if x < 0 {
62 neg = true
63 x = -x
64 }
65 z.abs = z.abs.setUint64(uint64(x))
66 z.neg = neg
67 return z
68 }
69
70
71 func (z *Int) SetUint64(x uint64) *Int {
72 z.abs = z.abs.setUint64(x)
73 z.neg = false
74 return z
75 }
76
77
78 func NewInt(x int64) *Int {
79
80
81 u := uint64(x)
82 if x < 0 {
83 u = -u
84 }
85 var abs []Word
86 if x == 0 {
87 } else if _W == 32 && u>>32 != 0 {
88 abs = []Word{Word(u), Word(u >> 32)}
89 } else {
90 abs = []Word{Word(u)}
91 }
92 return &Int{neg: x < 0, abs: abs}
93 }
94
95
96 func (z *Int) Set(x *Int) *Int {
97 if z != x {
98 z.abs = z.abs.set(x.abs)
99 z.neg = x.neg
100 }
101 return z
102 }
103
104
105
106
107
108
109 func (x *Int) Bits() []Word {
110
111
112
113 return x.abs
114 }
115
116
117
118
119
120
121 func (z *Int) SetBits(abs []Word) *Int {
122 z.abs = nat(abs).norm()
123 z.neg = false
124 return z
125 }
126
127
128 func (z *Int) Abs(x *Int) *Int {
129 z.Set(x)
130 z.neg = false
131 return z
132 }
133
134
135 func (z *Int) Neg(x *Int) *Int {
136 z.Set(x)
137 z.neg = len(z.abs) > 0 && !z.neg
138 return z
139 }
140
141
142 func (z *Int) Add(x, y *Int) *Int {
143 neg := x.neg
144 if x.neg == y.neg {
145
146
147 z.abs = z.abs.add(x.abs, y.abs)
148 } else {
149
150
151 if x.abs.cmp(y.abs) >= 0 {
152 z.abs = z.abs.sub(x.abs, y.abs)
153 } else {
154 neg = !neg
155 z.abs = z.abs.sub(y.abs, x.abs)
156 }
157 }
158 z.neg = len(z.abs) > 0 && neg
159 return z
160 }
161
162
163 func (z *Int) Sub(x, y *Int) *Int {
164 neg := x.neg
165 if x.neg != y.neg {
166
167
168 z.abs = z.abs.add(x.abs, y.abs)
169 } else {
170
171
172 if x.abs.cmp(y.abs) >= 0 {
173 z.abs = z.abs.sub(x.abs, y.abs)
174 } else {
175 neg = !neg
176 z.abs = z.abs.sub(y.abs, x.abs)
177 }
178 }
179 z.neg = len(z.abs) > 0 && neg
180 return z
181 }
182
183
184 func (z *Int) Mul(x, y *Int) *Int {
185
186
187
188
189 if x == y {
190 z.abs = z.abs.sqr(x.abs)
191 z.neg = false
192 return z
193 }
194 z.abs = z.abs.mul(x.abs, y.abs)
195 z.neg = len(z.abs) > 0 && x.neg != y.neg
196 return z
197 }
198
199
200
201
202 func (z *Int) MulRange(a, b int64) *Int {
203 switch {
204 case a > b:
205 return z.SetInt64(1)
206 case a <= 0 && b >= 0:
207 return z.SetInt64(0)
208 }
209
210
211 neg := false
212 if a < 0 {
213 neg = (b-a)&1 == 0
214 a, b = -b, -a
215 }
216
217 z.abs = z.abs.mulRange(uint64(a), uint64(b))
218 z.neg = neg
219 return z
220 }
221
222
223 func (z *Int) Binomial(n, k int64) *Int {
224 if k > n {
225 return z.SetInt64(0)
226 }
227
228 if k > n-k {
229 k = n - k
230 }
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252 var N, K, i, t Int
253 N.SetInt64(n)
254 K.SetInt64(k)
255 z.Set(intOne)
256 for i.Cmp(&K) < 0 {
257 z.Mul(z, t.Sub(&N, &i))
258 i.Add(&i, intOne)
259 z.Quo(z, &i)
260 }
261 return z
262 }
263
264
265
266
267 func (z *Int) Quo(x, y *Int) *Int {
268 z.abs, _ = z.abs.div(nil, x.abs, y.abs)
269 z.neg = len(z.abs) > 0 && x.neg != y.neg
270 return z
271 }
272
273
274
275
276 func (z *Int) Rem(x, y *Int) *Int {
277 _, z.abs = nat(nil).div(z.abs, x.abs, y.abs)
278 z.neg = len(z.abs) > 0 && x.neg
279 return z
280 }
281
282
283
284
285
286
287
288
289
290
291
292
293 func (z *Int) QuoRem(x, y, r *Int) (*Int, *Int) {
294 z.abs, r.abs = z.abs.div(r.abs, x.abs, y.abs)
295 z.neg, r.neg = len(z.abs) > 0 && x.neg != y.neg, len(r.abs) > 0 && x.neg
296 return z, r
297 }
298
299
300
301
302 func (z *Int) Div(x, y *Int) *Int {
303 y_neg := y.neg
304 var r Int
305 z.QuoRem(x, y, &r)
306 if r.neg {
307 if y_neg {
308 z.Add(z, intOne)
309 } else {
310 z.Sub(z, intOne)
311 }
312 }
313 return z
314 }
315
316
317
318
319 func (z *Int) Mod(x, y *Int) *Int {
320 y0 := y
321 if z == y || alias(z.abs, y.abs) {
322 y0 = new(Int).Set(y)
323 }
324 var q Int
325 q.QuoRem(x, y, z)
326 if z.neg {
327 if y0.neg {
328 z.Sub(z, y0)
329 } else {
330 z.Add(z, y0)
331 }
332 }
333 return z
334 }
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350 func (z *Int) DivMod(x, y, m *Int) (*Int, *Int) {
351 y0 := y
352 if z == y || alias(z.abs, y.abs) {
353 y0 = new(Int).Set(y)
354 }
355 z.QuoRem(x, y, m)
356 if m.neg {
357 if y0.neg {
358 z.Add(z, intOne)
359 m.Sub(m, y0)
360 } else {
361 z.Sub(z, intOne)
362 m.Add(m, y0)
363 }
364 }
365 return z, m
366 }
367
368
369
370
371
372
373 func (x *Int) Cmp(y *Int) (r int) {
374
375
376
377
378 switch {
379 case x == y:
380
381 case x.neg == y.neg:
382 r = x.abs.cmp(y.abs)
383 if x.neg {
384 r = -r
385 }
386 case x.neg:
387 r = -1
388 default:
389 r = 1
390 }
391 return
392 }
393
394
395
396
397
398
399 func (x *Int) CmpAbs(y *Int) int {
400 return x.abs.cmp(y.abs)
401 }
402
403
404 func low32(x nat) uint32 {
405 if len(x) == 0 {
406 return 0
407 }
408 return uint32(x[0])
409 }
410
411
412 func low64(x nat) uint64 {
413 if len(x) == 0 {
414 return 0
415 }
416 v := uint64(x[0])
417 if _W == 32 && len(x) > 1 {
418 return uint64(x[1])<<32 | v
419 }
420 return v
421 }
422
423
424
425 func (x *Int) Int64() int64 {
426 v := int64(low64(x.abs))
427 if x.neg {
428 v = -v
429 }
430 return v
431 }
432
433
434
435 func (x *Int) Uint64() uint64 {
436 return low64(x.abs)
437 }
438
439
440 func (x *Int) IsInt64() bool {
441 if len(x.abs) <= 64/_W {
442 w := int64(low64(x.abs))
443 return w >= 0 || x.neg && w == -w
444 }
445 return false
446 }
447
448
449 func (x *Int) IsUint64() bool {
450 return !x.neg && len(x.abs) <= 64/_W
451 }
452
453
454
455 func (x *Int) Float64() (float64, Accuracy) {
456 n := x.abs.bitLen()
457 if n == 0 {
458 return 0.0, Exact
459 }
460
461
462 if n <= 53 || n < 64 && n-int(x.abs.trailingZeroBits()) <= 53 {
463 f := float64(low64(x.abs))
464 if x.neg {
465 f = -f
466 }
467 return f, Exact
468 }
469
470 return new(Float).SetInt(x).Float64()
471 }
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495 func (z *Int) SetString(s string, base int) (*Int, bool) {
496 return z.setFromScanner(strings.NewReader(s), base)
497 }
498
499
500
501 func (z *Int) setFromScanner(r io.ByteScanner, base int) (*Int, bool) {
502 if _, _, err := z.scan(r, base); err != nil {
503 return nil, false
504 }
505
506 if _, err := r.ReadByte(); err != io.EOF {
507 return nil, false
508 }
509 return z, true
510 }
511
512
513
514 func (z *Int) SetBytes(buf []byte) *Int {
515 z.abs = z.abs.setBytes(buf)
516 z.neg = false
517 return z
518 }
519
520
521
522
523 func (x *Int) Bytes() []byte {
524
525
526
527 buf := make([]byte, len(x.abs)*_S)
528 return buf[x.abs.bytes(buf):]
529 }
530
531
532
533
534
535 func (x *Int) FillBytes(buf []byte) []byte {
536
537 for i := range buf {
538 buf[i] = 0
539 }
540 x.abs.bytes(buf)
541 return buf
542 }
543
544
545
546 func (x *Int) BitLen() int {
547
548
549
550 return x.abs.bitLen()
551 }
552
553
554
555 func (x *Int) TrailingZeroBits() uint {
556 return x.abs.trailingZeroBits()
557 }
558
559
560
561
562
563
564
565 func (z *Int) Exp(x, y, m *Int) *Int {
566 return z.exp(x, y, m, false)
567 }
568
569 func (z *Int) expSlow(x, y, m *Int) *Int {
570 return z.exp(x, y, m, true)
571 }
572
573 func (z *Int) exp(x, y, m *Int, slow bool) *Int {
574
575 xWords := x.abs
576 if y.neg {
577 if m == nil || len(m.abs) == 0 {
578 return z.SetInt64(1)
579 }
580
581 inverse := new(Int).ModInverse(x, m)
582 if inverse == nil {
583 return nil
584 }
585 xWords = inverse.abs
586 }
587 yWords := y.abs
588
589 var mWords nat
590 if m != nil {
591 if z == m || alias(z.abs, m.abs) {
592 m = new(Int).Set(m)
593 }
594 mWords = m.abs
595 }
596
597 z.abs = z.abs.expNN(xWords, yWords, mWords, slow)
598 z.neg = len(z.abs) > 0 && x.neg && len(yWords) > 0 && yWords[0]&1 == 1
599 if z.neg && len(mWords) > 0 {
600
601 z.abs = z.abs.sub(mWords, z.abs)
602 z.neg = false
603 }
604
605 return z
606 }
607
608
609
610
611
612
613
614
615
616
617
618
619 func (z *Int) GCD(x, y, a, b *Int) *Int {
620 if len(a.abs) == 0 || len(b.abs) == 0 {
621 lenA, lenB, negA, negB := len(a.abs), len(b.abs), a.neg, b.neg
622 if lenA == 0 {
623 z.Set(b)
624 } else {
625 z.Set(a)
626 }
627 z.neg = false
628 if x != nil {
629 if lenA == 0 {
630 x.SetUint64(0)
631 } else {
632 x.SetUint64(1)
633 x.neg = negA
634 }
635 }
636 if y != nil {
637 if lenB == 0 {
638 y.SetUint64(0)
639 } else {
640 y.SetUint64(1)
641 y.neg = negB
642 }
643 }
644 return z
645 }
646
647 return z.lehmerGCD(x, y, a, b)
648 }
649
650
651
652
653
654
655
656
657
658
659
660
661
662 func lehmerSimulate(A, B *Int) (u0, u1, v0, v1 Word, even bool) {
663
664 var a1, a2, u2, v2 Word
665
666 m := len(B.abs)
667 n := len(A.abs)
668
669
670 h := nlz(A.abs[n-1])
671 a1 = A.abs[n-1]<<h | A.abs[n-2]>>(_W-h)
672
673 switch {
674 case n == m:
675 a2 = B.abs[n-1]<<h | B.abs[n-2]>>(_W-h)
676 case n == m+1:
677 a2 = B.abs[n-2] >> (_W - h)
678 default:
679 a2 = 0
680 }
681
682
683
684
685
686
687 even = false
688
689 u0, u1, u2 = 0, 1, 0
690 v0, v1, v2 = 0, 0, 1
691
692
693
694
695
696 for a2 >= v2 && a1-a2 >= v1+v2 {
697 q, r := a1/a2, a1%a2
698 a1, a2 = a2, r
699 u0, u1, u2 = u1, u2, u1+q*u2
700 v0, v1, v2 = v1, v2, v1+q*v2
701 even = !even
702 }
703 return
704 }
705
706
707
708
709
710
711
712
713
714
715 func lehmerUpdate(A, B, q, r, s, t *Int, u0, u1, v0, v1 Word, even bool) {
716
717 t.abs = t.abs.setWord(u0)
718 s.abs = s.abs.setWord(v0)
719 t.neg = !even
720 s.neg = even
721
722 t.Mul(A, t)
723 s.Mul(B, s)
724
725 r.abs = r.abs.setWord(u1)
726 q.abs = q.abs.setWord(v1)
727 r.neg = even
728 q.neg = !even
729
730 r.Mul(A, r)
731 q.Mul(B, q)
732
733 A.Add(t, s)
734 B.Add(r, q)
735 }
736
737
738
739 func euclidUpdate(A, B, Ua, Ub, q, r, s, t *Int, extended bool) {
740 q, r = q.QuoRem(A, B, r)
741
742 *A, *B, *r = *B, *r, *A
743
744 if extended {
745
746 t.Set(Ub)
747 s.Mul(Ub, q)
748 Ub.Sub(Ua, s)
749 Ua.Set(t)
750 }
751 }
752
753
754
755
756
757
758
759
760
761
762
763 func (z *Int) lehmerGCD(x, y, a, b *Int) *Int {
764 var A, B, Ua, Ub *Int
765
766 A = new(Int).Abs(a)
767 B = new(Int).Abs(b)
768
769 extended := x != nil || y != nil
770
771 if extended {
772
773 Ua = new(Int).SetInt64(1)
774 Ub = new(Int)
775 }
776
777
778 q := new(Int)
779 r := new(Int)
780 s := new(Int)
781 t := new(Int)
782
783
784 if A.abs.cmp(B.abs) < 0 {
785 A, B = B, A
786 Ub, Ua = Ua, Ub
787 }
788
789
790 for len(B.abs) > 1 {
791
792 u0, u1, v0, v1, even := lehmerSimulate(A, B)
793
794
795 if v0 != 0 {
796
797
798
799 lehmerUpdate(A, B, q, r, s, t, u0, u1, v0, v1, even)
800
801 if extended {
802
803
804 lehmerUpdate(Ua, Ub, q, r, s, t, u0, u1, v0, v1, even)
805 }
806
807 } else {
808
809
810 euclidUpdate(A, B, Ua, Ub, q, r, s, t, extended)
811 }
812 }
813
814 if len(B.abs) > 0 {
815
816 if len(A.abs) > 1 {
817
818 euclidUpdate(A, B, Ua, Ub, q, r, s, t, extended)
819 }
820 if len(B.abs) > 0 {
821
822 aWord, bWord := A.abs[0], B.abs[0]
823 if extended {
824 var ua, ub, va, vb Word
825 ua, ub = 1, 0
826 va, vb = 0, 1
827 even := true
828 for bWord != 0 {
829 q, r := aWord/bWord, aWord%bWord
830 aWord, bWord = bWord, r
831 ua, ub = ub, ua+q*ub
832 va, vb = vb, va+q*vb
833 even = !even
834 }
835
836 t.abs = t.abs.setWord(ua)
837 s.abs = s.abs.setWord(va)
838 t.neg = !even
839 s.neg = even
840
841 t.Mul(Ua, t)
842 s.Mul(Ub, s)
843
844 Ua.Add(t, s)
845 } else {
846 for bWord != 0 {
847 aWord, bWord = bWord, aWord%bWord
848 }
849 }
850 A.abs[0] = aWord
851 }
852 }
853 negA := a.neg
854 if y != nil {
855
856 if y == b {
857 B.Set(b)
858 } else {
859 B = b
860 }
861
862 y.Mul(a, Ua)
863 if negA {
864 y.neg = !y.neg
865 }
866 y.Sub(A, y)
867 y.Div(y, B)
868 }
869
870 if x != nil {
871 *x = *Ua
872 if negA {
873 x.neg = !x.neg
874 }
875 }
876
877 *z = *A
878
879 return z
880 }
881
882
883
884
885
886 func (z *Int) Rand(rnd *rand.Rand, n *Int) *Int {
887
888 if n.neg || len(n.abs) == 0 {
889 z.neg = false
890 z.abs = nil
891 return z
892 }
893 z.neg = false
894 z.abs = z.abs.random(rnd, n.abs, n.abs.bitLen())
895 return z
896 }
897
898
899
900
901
902 func (z *Int) ModInverse(g, n *Int) *Int {
903
904 if n.neg {
905 var n2 Int
906 n = n2.Neg(n)
907 }
908 if g.neg {
909 var g2 Int
910 g = g2.Mod(g, n)
911 }
912 var d, x Int
913 d.GCD(&x, nil, g, n)
914
915
916 if d.Cmp(intOne) != 0 {
917 return nil
918 }
919
920
921
922 if x.neg {
923 z.Add(&x, n)
924 } else {
925 z.Set(&x)
926 }
927 return z
928 }
929
930 func (z nat) modInverse(g, n nat) nat {
931
932 return (&Int{abs: z}).ModInverse(&Int{abs: g}, &Int{abs: n}).abs
933 }
934
935
936
937 func Jacobi(x, y *Int) int {
938 if len(y.abs) == 0 || y.abs[0]&1 == 0 {
939 panic(fmt.Sprintf("big: invalid 2nd argument to Int.Jacobi: need odd integer but got %s", y.String()))
940 }
941
942
943
944
945
946 var a, b, c Int
947 a.Set(x)
948 b.Set(y)
949 j := 1
950
951 if b.neg {
952 if a.neg {
953 j = -1
954 }
955 b.neg = false
956 }
957
958 for {
959 if b.Cmp(intOne) == 0 {
960 return j
961 }
962 if len(a.abs) == 0 {
963 return 0
964 }
965 a.Mod(&a, &b)
966 if len(a.abs) == 0 {
967 return 0
968 }
969
970
971
972 s := a.abs.trailingZeroBits()
973 if s&1 != 0 {
974 bmod8 := b.abs[0] & 7
975 if bmod8 == 3 || bmod8 == 5 {
976 j = -j
977 }
978 }
979 c.Rsh(&a, s)
980
981
982 if b.abs[0]&3 == 3 && c.abs[0]&3 == 3 {
983 j = -j
984 }
985 a.Set(&b)
986 b.Set(&c)
987 }
988 }
989
990
991
992
993
994
995
996
997
998 func (z *Int) modSqrt3Mod4Prime(x, p *Int) *Int {
999 e := new(Int).Add(p, intOne)
1000 e.Rsh(e, 2)
1001 z.Exp(x, e, p)
1002 return z
1003 }
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013 func (z *Int) modSqrt5Mod8Prime(x, p *Int) *Int {
1014
1015
1016 e := new(Int).Rsh(p, 3)
1017 tx := new(Int).Lsh(x, 1)
1018 alpha := new(Int).Exp(tx, e, p)
1019 beta := new(Int).Mul(alpha, alpha)
1020 beta.Mod(beta, p)
1021 beta.Mul(beta, tx)
1022 beta.Mod(beta, p)
1023 beta.Sub(beta, intOne)
1024 beta.Mul(beta, x)
1025 beta.Mod(beta, p)
1026 beta.Mul(beta, alpha)
1027 z.Mod(beta, p)
1028 return z
1029 }
1030
1031
1032
1033 func (z *Int) modSqrtTonelliShanks(x, p *Int) *Int {
1034
1035 var s Int
1036 s.Sub(p, intOne)
1037 e := s.abs.trailingZeroBits()
1038 s.Rsh(&s, e)
1039
1040
1041 var n Int
1042 n.SetInt64(2)
1043 for Jacobi(&n, p) != -1 {
1044 n.Add(&n, intOne)
1045 }
1046
1047
1048
1049
1050
1051 var y, b, g, t Int
1052 y.Add(&s, intOne)
1053 y.Rsh(&y, 1)
1054 y.Exp(x, &y, p)
1055 b.Exp(x, &s, p)
1056 g.Exp(&n, &s, p)
1057 r := e
1058 for {
1059
1060 var m uint
1061 t.Set(&b)
1062 for t.Cmp(intOne) != 0 {
1063 t.Mul(&t, &t).Mod(&t, p)
1064 m++
1065 }
1066
1067 if m == 0 {
1068 return z.Set(&y)
1069 }
1070
1071 t.SetInt64(0).SetBit(&t, int(r-m-1), 1).Exp(&g, &t, p)
1072
1073 g.Mul(&t, &t).Mod(&g, p)
1074 y.Mul(&y, &t).Mod(&y, p)
1075 b.Mul(&b, &g).Mod(&b, p)
1076 r = m
1077 }
1078 }
1079
1080
1081
1082
1083
1084 func (z *Int) ModSqrt(x, p *Int) *Int {
1085 switch Jacobi(x, p) {
1086 case -1:
1087 return nil
1088 case 0:
1089 return z.SetInt64(0)
1090 case 1:
1091 break
1092 }
1093 if x.neg || x.Cmp(p) >= 0 {
1094 x = new(Int).Mod(x, p)
1095 }
1096
1097 switch {
1098 case p.abs[0]%4 == 3:
1099
1100 return z.modSqrt3Mod4Prime(x, p)
1101 case p.abs[0]%8 == 5:
1102
1103 return z.modSqrt5Mod8Prime(x, p)
1104 default:
1105
1106 return z.modSqrtTonelliShanks(x, p)
1107 }
1108 }
1109
1110
1111 func (z *Int) Lsh(x *Int, n uint) *Int {
1112 z.abs = z.abs.shl(x.abs, n)
1113 z.neg = x.neg
1114 return z
1115 }
1116
1117
1118 func (z *Int) Rsh(x *Int, n uint) *Int {
1119 if x.neg {
1120
1121 t := z.abs.sub(x.abs, natOne)
1122 t = t.shr(t, n)
1123 z.abs = t.add(t, natOne)
1124 z.neg = true
1125 return z
1126 }
1127
1128 z.abs = z.abs.shr(x.abs, n)
1129 z.neg = false
1130 return z
1131 }
1132
1133
1134
1135 func (x *Int) Bit(i int) uint {
1136 if i == 0 {
1137
1138 if len(x.abs) > 0 {
1139 return uint(x.abs[0] & 1)
1140 }
1141 return 0
1142 }
1143 if i < 0 {
1144 panic("negative bit index")
1145 }
1146 if x.neg {
1147 t := nat(nil).sub(x.abs, natOne)
1148 return t.bit(uint(i)) ^ 1
1149 }
1150
1151 return x.abs.bit(uint(i))
1152 }
1153
1154
1155
1156
1157
1158 func (z *Int) SetBit(x *Int, i int, b uint) *Int {
1159 if i < 0 {
1160 panic("negative bit index")
1161 }
1162 if x.neg {
1163 t := z.abs.sub(x.abs, natOne)
1164 t = t.setBit(t, uint(i), b^1)
1165 z.abs = t.add(t, natOne)
1166 z.neg = len(z.abs) > 0
1167 return z
1168 }
1169 z.abs = z.abs.setBit(x.abs, uint(i), b)
1170 z.neg = false
1171 return z
1172 }
1173
1174
1175 func (z *Int) And(x, y *Int) *Int {
1176 if x.neg == y.neg {
1177 if x.neg {
1178
1179 x1 := nat(nil).sub(x.abs, natOne)
1180 y1 := nat(nil).sub(y.abs, natOne)
1181 z.abs = z.abs.add(z.abs.or(x1, y1), natOne)
1182 z.neg = true
1183 return z
1184 }
1185
1186
1187 z.abs = z.abs.and(x.abs, y.abs)
1188 z.neg = false
1189 return z
1190 }
1191
1192
1193 if x.neg {
1194 x, y = y, x
1195 }
1196
1197
1198 y1 := nat(nil).sub(y.abs, natOne)
1199 z.abs = z.abs.andNot(x.abs, y1)
1200 z.neg = false
1201 return z
1202 }
1203
1204
1205 func (z *Int) AndNot(x, y *Int) *Int {
1206 if x.neg == y.neg {
1207 if x.neg {
1208
1209 x1 := nat(nil).sub(x.abs, natOne)
1210 y1 := nat(nil).sub(y.abs, natOne)
1211 z.abs = z.abs.andNot(y1, x1)
1212 z.neg = false
1213 return z
1214 }
1215
1216
1217 z.abs = z.abs.andNot(x.abs, y.abs)
1218 z.neg = false
1219 return z
1220 }
1221
1222 if x.neg {
1223
1224 x1 := nat(nil).sub(x.abs, natOne)
1225 z.abs = z.abs.add(z.abs.or(x1, y.abs), natOne)
1226 z.neg = true
1227 return z
1228 }
1229
1230
1231 y1 := nat(nil).sub(y.abs, natOne)
1232 z.abs = z.abs.and(x.abs, y1)
1233 z.neg = false
1234 return z
1235 }
1236
1237
1238 func (z *Int) Or(x, y *Int) *Int {
1239 if x.neg == y.neg {
1240 if x.neg {
1241
1242 x1 := nat(nil).sub(x.abs, natOne)
1243 y1 := nat(nil).sub(y.abs, natOne)
1244 z.abs = z.abs.add(z.abs.and(x1, y1), natOne)
1245 z.neg = true
1246 return z
1247 }
1248
1249
1250 z.abs = z.abs.or(x.abs, y.abs)
1251 z.neg = false
1252 return z
1253 }
1254
1255
1256 if x.neg {
1257 x, y = y, x
1258 }
1259
1260
1261 y1 := nat(nil).sub(y.abs, natOne)
1262 z.abs = z.abs.add(z.abs.andNot(y1, x.abs), natOne)
1263 z.neg = true
1264 return z
1265 }
1266
1267
1268 func (z *Int) Xor(x, y *Int) *Int {
1269 if x.neg == y.neg {
1270 if x.neg {
1271
1272 x1 := nat(nil).sub(x.abs, natOne)
1273 y1 := nat(nil).sub(y.abs, natOne)
1274 z.abs = z.abs.xor(x1, y1)
1275 z.neg = false
1276 return z
1277 }
1278
1279
1280 z.abs = z.abs.xor(x.abs, y.abs)
1281 z.neg = false
1282 return z
1283 }
1284
1285
1286 if x.neg {
1287 x, y = y, x
1288 }
1289
1290
1291 y1 := nat(nil).sub(y.abs, natOne)
1292 z.abs = z.abs.add(z.abs.xor(x.abs, y1), natOne)
1293 z.neg = true
1294 return z
1295 }
1296
1297
1298 func (z *Int) Not(x *Int) *Int {
1299 if x.neg {
1300
1301 z.abs = z.abs.sub(x.abs, natOne)
1302 z.neg = false
1303 return z
1304 }
1305
1306
1307 z.abs = z.abs.add(x.abs, natOne)
1308 z.neg = true
1309 return z
1310 }
1311
1312
1313
1314 func (z *Int) Sqrt(x *Int) *Int {
1315 if x.neg {
1316 panic("square root of negative number")
1317 }
1318 z.neg = false
1319 z.abs = z.abs.sqrt(x.abs)
1320 return z
1321 }
1322
View as plain text