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