Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Shamir's secret sharing problem! HELP Please!!

843810Dec 22 2004 — edited Jan 26 2005
Hi,
I've implemented Shamir's secret sharing scheme, which takes a private key (proxy.getX()), number of shares to be generated ( nShares), number of shares required to reconstruct the key ( nRebuild), list of points to evaluate the polynomial ( []sID), list of coefficients to generate the shares ([]sCoeff), and the prime number ( prime), which = proxy.getParams().getQ(). The code works fine when I test it using small numbers, e.g. proxy = 13, prime = 17, etc. But it doesn't when I use the real values!! (the original key is not equal to the reconstructed key)!! I spent hours and hours trying to figure out the problem but couldn't!! Any help would be very appreciated. Here is the code:

//The class with the main method
BigInteger[] sCoef = new BigInteger[K-1];
SecureRandom sec = new SecureRandom();
// generating the coefficients
for(int i=0; i<(K-1); i++)
{
do {
s = new BigInteger(proxy.getParams().getQ().bitLength(), sec);
}
while (s.compareTo(proxy.getParams().getQ()) >= 0);
sCoef[i] = s;
}
//generating the evaluation points
try{
BigInteger t[];
t = new BigInteger[Y+1];
t[0] = new BigInteger(1,IdAgent);
for (int i=1; i<=Y; i++)
{
t[i] = new BigInteger(1, IdTTP[i - 1]);
}
SecretShare S = new SecretShare(proxy.getX(), Y+1, K, t, sCoef, proxy.getParams().getQ());
Share []keyShares = new Share[Y+1];
keyShares = S.GenerateShares();
Share []selected = new Share[3];
selected[0] = keyShares[0];
selected[1] = keyShares[2];
selected[2] = keyShares[4];

BigInteger RKey = S.reconstructKey(selected, K);
if (proxy.getX().compareTo(RKey) == 0)
System.out.println("Both keys are equal :-)");
else
System.out.println("Both keys are not equals :-(");

//The Secret Sharing class
public SecretShare(BigInteger sKey, int nShares, int nRebuild, BigInteger []sID,
BigInteger []sCoeff, BigInteger prime)
{
key = sKey;
primeNum = prime;
if (key.compareTo(primeNum)>=0)
System.out.println("Proxy Key is larger than Q");
else
System.out.println("Proxy Key is smaller than Q");
numShares = nShares;
numRebuild = nRebuild;
IDs = new BigInteger[numShares];
for (int i=0; i<numShares; i++)
IDs[i] = sID;
// compute the prime number or use the one provided.
primeNum = prime;
// build the shamir polynomial by generating coefficients as random BigInteger's
Coeff = new BigInteger[numRebuild];
Coeff[0] = key;
for (int i = 1; i < numRebuild; i++)
Coeff[i] = sCoeff[i-1];

for (int i=0; i<numRebuild; i++)
System.out.println("Coef "+ i+ " = "+Coeff[i]);
}

public Share[] GenerateShares()
{//compute the values of each of the shares
//positions = new BigInteger[numShares];
KeyShares = new Share[numShares];

for (int share = 1; share <= numShares; share++)
{
BigInteger value = Coeff[0];
BigInteger powx = IDs[share-1];
for (int n=1; n<numRebuild; n++)
{
BigInteger term = powx;
term = term.multiply(Coeff[n]);
value = value.add(term);
value = value.mod(primeNum);
powx = powx.multiply(powx);
}
KeyShares[share-1] = new Share(value, new BigInteger(Integer.toString(share)));
System.out.println("The value of share "+KeyShares[share-1].position.toString()+ " is = "+KeyShares[share-1].share.toString());
}
return KeyShares;
}
public BigInteger reconstructKey(Share s[], int sharesReq)
{ //int sharesReq = numRebuild;
BigInteger RecKey;
for (int i=0; i<sharesReq; i++)
System.out.println("The selected share: "+s[i].share.toString()+ "and its position is : "+s[i].position.toString());
System.out.println("Q = "+primeNum);
// compute the key
RecKey = new BigInteger("0");
for(int k=0; k<sharesReq; k++)
{
BigInteger temp1 = new BigInteger("1");
for(int j=0; j<sharesReq; j++)
{
if (j==k) continue;
BigInteger temp2 = s[k].position.subtract(s[j].position).mod(primeNum);
temp2 = temp2.modInverse(primeNum);
temp2 = s[j].position.negate().multiply(temp2);
temp1 = temp1.multiply(temp2);
}
BigInteger temp3 = s[k].share.multiply(temp1).mod(primeNum);
RecKey = RecKey.add(temp3).mod(primeNum);
}
RecKey = RecKey.mod(primeNum);
System.out.println("The reconstructed key = "+RecKey.toString());
return RecKey;
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 23 2005
Added on Dec 22 2004
2 comments
140 views