Skip to content

Commit eadf2be

Browse files
author
gefeili
committed
Create APIs: SecretShare, SplitSecret and SecretSplitter. Change split package to threshold
1 parent b8f9492 commit eadf2be

File tree

75 files changed

+289
-226
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+289
-226
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.bouncycastle.crypto.threshold;
2+
3+
public interface SecretShare
4+
{
5+
}

core/src/main/java/org/bouncycastle/crypto/split/SecretSharing.java renamed to core/src/main/java/org/bouncycastle/crypto/threshold/SecretSplitter.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
package org.bouncycastle.crypto.split;
2-
3-
import java.security.SecureRandom;
1+
package org.bouncycastle.crypto.threshold;
42

53
/**
64
* Secret sharing (also called secret splitting) refers to methods for distributing a secret among a group.
75
* In this process, no individual holds any intelligible information about the secret.
86
* However, when a sufficient number of individuals combine their 'shares', the secret can be reconstructed.
97
*/
10-
public interface SecretSharing
8+
public interface SecretSplitter
119
{
1210
/**
1311
* Creates secret shares from a given secret. The secret will be divided into shares, where the secret has a length of L bytes.
1412
*
15-
* @param random the source of secure random
1613
* @return An array of {@code byte[][]} representing the generated secret shares for m users with l bytes each.
1714
*/
18-
byte[][] createShares(SecureRandom random);
15+
SplitSecret split();
1916

2017
/**
2118
* Recombines secret shares to reconstruct the original secret.

core/src/main/java/org/bouncycastle/crypto/split/PolynomialNative.java renamed to core/src/main/java/org/bouncycastle/crypto/threshold/ShamirNativeSecretSplitter.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
package org.bouncycastle.crypto.split;
1+
package org.bouncycastle.crypto.threshold;
22

3-
public class PolynomialNative
4-
extends Polynomial
3+
import java.security.SecureRandom;
4+
5+
public class ShamirNativeSecretSplitter
6+
extends ShamirSecretSplitter
57
{
68
private final int IRREDUCIBLE;
79

8-
public PolynomialNative(int algorithm, int l, int m, int n)
10+
public ShamirNativeSecretSplitter(int algorithm, int l, int m, int n, SecureRandom random)
911
{
10-
super(l, m, n);
12+
super(l, m, n, random);
1113
switch (algorithm)
1214
{
1315
case AES:

core/src/main/java/org/bouncycastle/crypto/split/Polynomial.java renamed to core/src/main/java/org/bouncycastle/crypto/threshold/ShamirSecretSplitter.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package org.bouncycastle.crypto.split;
1+
package org.bouncycastle.crypto.threshold;
22

33
import java.security.SecureRandom;
44

5-
public abstract class Polynomial
6-
implements SecretSharing
5+
public abstract class ShamirSecretSplitter
6+
implements SecretSplitter
77
{
88
public static final int AES = 0;
99
public static final int RSA = 1;
@@ -21,8 +21,9 @@ public abstract class Polynomial
2121
*/
2222
protected int n;
2323
protected byte[][] p;
24+
protected SecureRandom random;
2425

25-
protected Polynomial(int l, int m, int n)
26+
protected ShamirSecretSplitter(int l, int m, int n, SecureRandom random)
2627
{
2728
if (l < 0 || l > 65534)
2829
{
@@ -39,6 +40,7 @@ protected Polynomial(int l, int m, int n)
3940
this.l = l;
4041
this.m = m;
4142
this.n = n;
43+
this.random = random;
4244
}
4345

4446
protected void init()
@@ -53,20 +55,20 @@ protected void init()
5355
}
5456
}
5557

56-
public byte[][] createShares(SecureRandom random)
58+
public ShamirSplitSecret split()
5759
{
5860
byte[][] sr = new byte[m][l];
59-
byte[][] result = new byte[p.length][l];
61+
ShamirSplitSecretShare[] secretShares = new ShamirSplitSecretShare[l];
6062
int i;
6163
for (i = 0; i < m; ++i)
6264
{
6365
random.nextBytes(sr[i]);
6466
}
6567
for (i = 0; i < p.length; i++)
6668
{
67-
result[i] = gfVecMul(p[i], sr);
69+
secretShares[i] = new ShamirSplitSecretShare(gfVecMul(p[i], sr), i + 1);
6870
}
69-
return result;
71+
return new ShamirSplitSecret(secretShares);
7072
}
7173

7274
public byte[] recombineShares(int[] rr, byte[]... splits)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.bouncycastle.crypto.threshold;
2+
3+
public class ShamirSplitSecret
4+
implements SplitSecret
5+
{
6+
private ShamirSplitSecretShare[] secretShares;
7+
8+
public ShamirSplitSecret(ShamirSplitSecretShare[] secretShares)
9+
{
10+
this.secretShares = secretShares;
11+
}
12+
13+
public ShamirSplitSecretShare[] getSecretShare()
14+
{
15+
return secretShares;
16+
}
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.bouncycastle.crypto.threshold;
2+
3+
import org.bouncycastle.util.Arrays;
4+
5+
public class ShamirSplitSecretShare
6+
implements SecretShare
7+
{
8+
private final byte[] secretShare;
9+
private final int r; // index of secretShare
10+
11+
public ShamirSplitSecretShare(byte[] secretShare, int r)
12+
{
13+
this.secretShare = Arrays.clone(secretShare);
14+
this.r = r;
15+
}
16+
17+
public byte[] getSecretShare()
18+
{
19+
return secretShare;
20+
}
21+
}

core/src/main/java/org/bouncycastle/crypto/split/PolynomialTable.java renamed to core/src/main/java/org/bouncycastle/crypto/threshold/ShamirTableSecretSplitter.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
package org.bouncycastle.crypto.split;
1+
package org.bouncycastle.crypto.threshold;
22

3-
public class PolynomialTable
4-
extends Polynomial
3+
import java.security.SecureRandom;
4+
5+
public class ShamirTableSecretSplitter
6+
extends ShamirSecretSplitter
57
{
68
private final byte[] LOG;
79
private final byte[] EXP;
@@ -146,9 +148,9 @@ public class PolynomialTable
146148
(byte)0x1b, (byte)0x36, (byte)0x6c, (byte)0xd8, (byte)0xad, (byte)0x47, (byte)0x8e, (byte)0x01
147149
};
148150

149-
public PolynomialTable(int algorithm, int l, int m, int n)
151+
public ShamirTableSecretSplitter(int algorithm, int l, int m, int n, SecureRandom random)
150152
{
151-
super(l, m, n);
153+
super(l, m, n, random);
152154
switch (algorithm)
153155
{
154156
case AES:
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.bouncycastle.crypto.threshold;
2+
3+
public interface SplitSecret
4+
{
5+
SecretShare[] getSecretShare();
6+
}

core/src/test/java/org/bouncycastle/crypto/split/test/AllTests.java renamed to core/src/test/java/org/bouncycastle/crypto/threshold/test/AllTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.bouncycastle.crypto.split.test;
1+
package org.bouncycastle.crypto.threshold.test;
22

33
import junit.extensions.TestSetup;
44
import junit.framework.Test;
@@ -18,7 +18,7 @@ public static void main(String[] args)
1818
public static Test suite()
1919
{
2020
TestSuite suite = new TestSuite("Secret Sharing Tests");
21-
suite.addTestSuite(PolynomialTest.class);
21+
suite.addTestSuite(ShamirSecretSplitterTest.class);
2222
return new AllTests.BCTestSetup(suite);
2323
}
2424

0 commit comments

Comments
 (0)