private void doTest(ECPrivateKeyParameters priKey, ParametersWithRandom pRandom, BigInteger value) { ECPoint data = priKey.getParameters().getG().multiply(value); ECEncryptor encryptor = new ECElGamalEncryptor(); encryptor.init(pRandom); ECPair pair = encryptor.encrypt(data); ECDecryptor decryptor = new ECElGamalDecryptor(); decryptor.init(priKey); ECPoint result = decryptor.decrypt(pair); if (!data.equals(result)) { fail("point pair failed to decrypt back to original"); } }
private void doTest(ECDomainParameters domainParams, AsymmetricCipherKeyPair[] kps, int threshold, boolean shouldPass, int... missing) { int numberOfPeers = kps.length; // create the splitter for the peers/threshold over the order of the curve. ShamirSecretSplitter secretSplitter = new ShamirSecretSplitter(numberOfPeers, threshold, domainParams.getN(), new SecureRandom()); // Having created a private key the server creates shares of that // private key. It would keep one share for itself and sends the others // shares to the other servers. BigInteger[][] privateKeyShares = new BigInteger[numberOfPeers][]; BigInteger[] finalPrivateKeyShares = new BigInteger[numberOfPeers]; for (int i = 0; i < numberOfPeers; i++) { privateKeyShares[i] = secretSplitter.split(((ECPrivateKeyParameters)kps[i].getPrivate()).getD()).getShares(); } // Simulates distributing shares and combining them for (int i = 0; i < numberOfPeers; i++) { finalPrivateKeyShares[i] = privateKeyShares[0][i]; for (int j = 1; j < numberOfPeers; j++) { finalPrivateKeyShares[i] = finalPrivateKeyShares[i].add(privateKeyShares[j][i]); } } ECPoint pubPoint = ((ECPublicKeyParameters)kps[0].getPublic()).getQ(); for (int i = 1; i < numberOfPeers; i++) { pubPoint = pubPoint.add(((ECPublicKeyParameters)kps[i].getPublic()).getQ()); } ECPublicKeyParameters jointPub = new ECPublicKeyParameters(pubPoint, domainParams); // Create a random plaintext ECPoint plaintext = generatePoint(domainParams, new SecureRandom()); // Encrypt it using the joint public key ECEncryptor enc = new ECElGamalEncryptor(); enc.init(new ParametersWithRandom(jointPub, new SecureRandom())); ECPair cipherText = enc.encrypt(plaintext); // do partial decrypts ECPoint[] partialDecs = new ECPoint[numberOfPeers]; for (int i = 0; i < numberOfPeers; i++) { partialDecs[i] = cipherText.getX().multiply(finalPrivateKeyShares[i]); } // simulate missing peers for (int i = 0; i != missing.length; i++) { partialDecs[missing[i]] = null; } // decryption step LagrangeWeightCalculator lagrangeWeightCalculator = new LagrangeWeightCalculator(numberOfPeers, domainParams.getN()); BigInteger[] weights = lagrangeWeightCalculator.computeWeights(partialDecs); // weighting ECPoint weightedDecryption = partialDecs[0].multiply(weights[0]); for (int i = 1; i < weights.length; i++) { if (partialDecs[i] != null) { weightedDecryption = weightedDecryption.add(partialDecs[i].multiply(weights[i])); } } // Do final decryption to recover plaintext ECPoint ECPoint decrypted = cipherText.getY().add(weightedDecryption.negate()); Assert.assertEquals(shouldPass, plaintext.equals(decrypted)); }
private void doSameKeyTest(ECPrivateKeyParameters priKey, ParametersWithRandom pRandom, BigInteger value) { ECPoint data = priKey.getParameters().getG().multiply(value); ECEncryptor encryptor = new ECElGamalEncryptor(); encryptor.init(pRandom); ECPair pair = encryptor.encrypt(data); ECPairTransform ecr = new ECNewRandomnessTransform(); ecr.init(pRandom); ECPair srcPair = pair; // re-encrypt the message portion pair = ecr.transform(srcPair); ECDecryptor decryptor = new ECElGamalDecryptor(); decryptor.init(priKey); // decrypt the fully transformed point. ECPoint result = decryptor.decrypt(pair); if (!data.equals(result)) { fail("point pair failed to decrypt back to original"); } }
private void doTest(ECPrivateKeyParameters priKey, ParametersWithRandom pRandom, BigInteger value) { ECPoint data = priKey.getParameters().getG().multiply(value); ECEncryptor encryptor = new ECElGamalEncryptor(); encryptor.init(pRandom); ECPair pair = encryptor.encrypt(data); ECKeyPairGenerator ecGen = new ECKeyPairGenerator(); ecGen.init(new ECKeyGenerationParameters(priKey.getParameters(), new SecureRandom())); AsymmetricCipherKeyPair reEncKP = ecGen.generateKeyPair(); ECPairTransform ecr = new ECNewPublicKeyTransform(); ecr.init(reEncKP.getPublic()); ECPair srcPair = pair; // re-encrypt the message portion pair = ecr.transform(srcPair); ECDecryptor decryptor = new ECElGamalDecryptor(); decryptor.init(priKey); // decrypt out the original private key ECPoint p = decryptor.decrypt(new ECPair(srcPair.getX(), pair.getY())); decryptor.init(reEncKP.getPrivate()); // decrypt the fully transformed point. ECPoint result = decryptor.decrypt(new ECPair(pair.getX(), p)); if (!data.equals(result)) { fail("point pair failed to decrypt back to original"); } }