-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSS.java
More file actions
123 lines (115 loc) · 4 KB
/
Copy pathSSS.java
File metadata and controls
123 lines (115 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class SSS {
BigInteger n; // Number of Shares
BigInteger k; // Threshold Shares
BigInteger prime; // Big Prime used for Modulus
ArrayList<BigInteger> a; // Polynomial co-efficients
final Integer bitLength = 512; // Default bit-length of big prime number
/**
* Construct class and generate a random big prime number.
*
* @param n BigInteger - Number of Shares
* @param k BigInteger - Threshold Number
*/
public SSS(BigInteger n, BigInteger k) {
this.n = n;
this.k = k;
this.prime = this.genPrime(this.bitLength);
this.a = new ArrayList<>();
}
/**
* Generate a big prime number
*
* @param bitLength Integer - Bit length of generated prime number
* @return BigInteger - Prime number.
*/
public BigInteger genPrime(Integer bitLength) {
SecureRandom s_rand = new SecureRandom();
return BigInteger.probablePrime(bitLength, s_rand);
}
/**
* Set Prime Number
*
* @param prime BigInteger - Prime number
*/
public void setPrime(BigInteger prime) {
this.prime = prime;
}
/**
* Ser Secret
*
* @param secret BigInteger - Secret to be divided into different share
*/
public void setSecret(BigInteger secret) {
this.a.add(secret);
SecureRandom s_rand = new SecureRandom();
for (int i = 1; i < Integer.valueOf(k.toString()); i++) {
this.a.add(new BigInteger(this.bitLength, s_rand).mod(this.prime));
}
}
/**
* Compute share based on the given values
*
* @param x BigInteger - Proddice xth share
* @return BigInteger - xth share
* @throws Exception
*/
public BigInteger computeShare(BigInteger x) throws Exception {
BigInteger result = this.a.get(0);
BigInteger temp = x;
for (int i = 1; i < Integer.valueOf(k.toString()); i++) {
result = result.add(this.a.get(i).multiply(temp));
temp = temp.multiply(x);
}
return result.mod(this.prime);
}
/**
* Compute and return all shares at onces.
*
* @return HashMap - map of all shares (1 <= x <= k)
* @throws Exception
*/
public HashMap<Integer, BigInteger> computeAllShare() throws Exception {
HashMap<Integer, BigInteger> result = new HashMap<>();
for (int x = 1; x < Integer.valueOf(n.toString()); x++) {
BigInteger holdVal = this.a.get(0);
BigInteger temp = BigInteger.valueOf(x);
for (int i = 1; i < Integer.valueOf(k.toString()); i++) {
holdVal = holdVal.add(this.a.get(i).multiply(temp));
temp = temp.multiply(BigInteger.valueOf(x));
}
result.put(x, holdVal.mod(this.prime));
}
return result;
}
/**
* Computes the secret using the provided share if enough
*
* @param shares Map of (shareId, distributedSecret)
* @return BigInteger - Reconstructed secret
* @throws Exception
*/
public BigInteger reconstructSecret(HashMap<Integer, BigInteger> shares) throws Exception {
if (shares.size() < Integer.valueOf(this.k.toString())) {
throw new Exception("Number of shares must be equal or greater to threshold");
}
BigInteger result = BigInteger.ZERO;
BigInteger xi, xj, yi;
for (Map.Entry<Integer, BigInteger> e : shares.entrySet()) {
xi = BigInteger.valueOf(e.getKey());
yi = e.getValue();
for (Map.Entry<Integer, BigInteger> e2 : shares.entrySet()) {
xj = BigInteger.valueOf(e2.getKey());
if (!xi.equals(xj)) {
yi = yi.multiply(xj).multiply(xj.subtract(xi).modInverse(prime)).mod(prime);
}
}
result = result.add(yi).mod(prime);
}
return result;
}
}