1 8 package org.lsmp.djep.groupJep.groups; 9 import org.lsmp.djep.groupJep.interfaces.*; 10 import java.math.*; 11 12 34 public class Zn extends Group implements FieldI, 35 OrderedSetI,HasModI,HasPowerI { 36 BigInteger modulus; 37 40 private Zn() {} 41 42 public Zn(BigInteger modulus) { 43 this.modulus = modulus; 44 } 45 46 public Number getZERO() { 47 return BigInteger.ZERO; 48 } 49 50 public Number getONE() { 51 return BigInteger.ONE; 52 } 53 54 public Number getInverse(Number num) { 55 BigInteger a = (BigInteger) num; 56 return a.negate().mod(modulus); 57 } 58 59 public Number getMulInverse(Number num) { 60 BigInteger a = (BigInteger) num; 61 return a.modInverse(modulus); 62 } 63 64 public Number add(Number num1, Number num2) { 65 BigInteger a = (BigInteger) num1; 66 BigInteger b = (BigInteger) num2; 67 return a.add(b).mod(modulus); 68 } 69 70 public Number sub(Number num1, Number num2) { 71 BigInteger a = (BigInteger) num1; 72 BigInteger b = (BigInteger) num2; 73 return a.subtract(b).mod(modulus); 74 } 75 76 public Number mul(Number num1, Number num2) { 77 BigInteger a = (BigInteger) num1; 78 BigInteger b = (BigInteger) num2; 79 return a.multiply(b).mod(modulus); 80 } 81 82 public Number div(Number num1, Number num2) { 83 BigInteger a = (BigInteger) num1; 84 BigInteger b = (BigInteger) num2; 85 return a.multiply(b.modInverse(modulus)).mod(modulus); 86 } 87 88 public Number mod(Number num1, Number num2) { 89 BigInteger a = (BigInteger) num1; 90 BigInteger b = (BigInteger) num2; 91 return a.mod(b).mod(modulus); 92 } 93 94 public Number pow(Number num1, Number num2) { 95 BigInteger a = (BigInteger) num1; 96 BigInteger b = (BigInteger) num2; 97 return a.modPow(b,modulus); 98 } 99 public boolean equals(Number a,Number b) { 100 return ((Integer ) a).compareTo((Integer ) b) == 0; 101 } 102 103 public int compare(Number a,Number b) { 104 return ((Integer ) a).compareTo((Integer ) b); 105 } 106 107 108 public Number valueOf(String str) { 109 BigInteger in = new BigInteger(str); 110 return in.mod(modulus); 111 } 112 113 public String toString() { return "Integers mod "+this.modulus; } 114 } 115 | Popular Tags |