Newer
Older
import java.math.BigInteger;
import java.util.*;
public class d_he {
private static Random rand = new Random();
public static Pk_Sk KeyGen(){
var ri = new BigInteger(60, rand);
var twoR = ri.multiply(BigInteger.TWO);
var yi = (p.multiply(qi)).add(twoR);
pk_list.add(yi);
}
return new Pk_Sk(p, pk_list);
}
public static boolean isOdd(BigInteger val) {
if(!(val.mod(BigInteger.TWO)).equals(BigInteger.ZERO))
return true;
return false;
}
public static BigInteger Encryption(BigInteger m, List<BigInteger> pk_list){
List<BigInteger> pk_list_copy = new ArrayList<>(pk_list);
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
Collections.shuffle(pk_list_copy);
List<BigInteger> S = pk_list_copy.subList(0, num_of_elements);
var sum = BigInteger.ZERO;
for (int i = 0; i < S.size(); i++) {
sum = sum.add(S.get(i));
}
return m.add(sum);
}
public static BigInteger Decryption(BigInteger c, BigInteger sk){
return (c.mod(sk)).mod(BigInteger.TWO);
}
public static BigInteger Eval(BigInteger c1, BigInteger c2, BigInteger sk, List<BigInteger> pk_list, String gate){
var lh = Decryption(c1, sk);
var rh = Decryption(c2, sk);
BigInteger temp = BigInteger.ZERO;
BigInteger c3 = BigInteger.ZERO;
if (gate.equals("xor")){
temp = (lh.add(rh));
if(temp.compareTo(BigInteger.TWO) == 0){
temp = BigInteger.ZERO;
}
} else if(gate.equals("and")){
temp = (lh.multiply(rh));
}
if(temp.compareTo(BigInteger.ZERO) == 0){
c3 = Encryption(BigInteger.ONE, pk_list);
} else {
c3 = Encryption(BigInteger.ZERO, pk_list);
}
return c3;
}
}