1
2
3
4
5
6
7 package civitas.util;
8
9 import java.math.BigInteger;
10
11 @Boilerplate
12 public class CivitasBigInteger extends CivitasBigintegerBase {
13
14 CivitasBigInteger(final BigInteger integer) {
15 super(integer);
16 }
17
18 public CivitasBigInteger add(final CivitasBigInteger x) {
19 if (x == ZERO) {
20 return this;
21 }
22 return new CivitasBigInteger(i.add(x.i));
23 }
24
25 public CivitasBigInteger mod(final CivitasBigInteger q) {
26 BigInteger m = this.i.mod(q.i);
27 if (this.i.equals(m)) {
28 return this;
29 }
30 return new CivitasBigInteger(m);
31 }
32
33 public CivitasBigInteger modAdd(final CivitasBigInteger x, final CivitasBigInteger p) {
34 if (x == ZERO) {
35 return this.mod(p);
36 }
37 return new CivitasBigInteger(i.add(x.i).mod(p.i));
38 }
39
40 public CivitasBigInteger modPow(final CivitasBigInteger x, final CivitasBigInteger p) {
41 return new CivitasBigInteger(this.i.modPow(x.i, p.i));
42 }
43
44 public CivitasBigInteger multiply(final CivitasBigInteger x) {
45 if (x == ONE) {
46 return this;
47 }
48 if (x == ZERO) {
49 return ZERO;
50 }
51 return new CivitasBigInteger(this.i.multiply(x.i));
52 }
53
54 public CivitasBigInteger modMultiply(final CivitasBigInteger x, final CivitasBigInteger p) {
55 if (x == ZERO) {
56 return ZERO;
57 }
58 if (x == ONE) {
59 return this.mod(p);
60 }
61 return new CivitasBigInteger(this.i.multiply(x.i).mod(p.i));
62 }
63
64 public CivitasBigInteger modDivide(final CivitasBigInteger x, final CivitasBigInteger p) {
65 if (x == ONE) {
66 return this.mod(p);
67 }
68 return new CivitasBigInteger(this.i.multiply(x.i.modInverse(p.i)).mod(p.i));
69 }
70
71 public CivitasBigInteger subtract(final CivitasBigInteger x) {
72 if (x == ZERO) {
73 return this;
74 }
75 return new CivitasBigInteger(this.i.subtract(x.i));
76 }
77
78 public CivitasBigInteger modSubtract(final CivitasBigInteger x, final CivitasBigInteger p) {
79 if (x == ZERO) {
80 return this.mod(p);
81 }
82 return new CivitasBigInteger(this.i.subtract(x.i).mod(p.i));
83 }
84 }