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 static void generateBallots(File ballotDir, File candidateDir, String baseName, Random rand, int ballotSize, ECElGamalEncryptor encryptor, ECDomainParameters ecParams, SecureRandom pointRandom) throws IOException { File ballotFile = new File(ballotDir, baseName + ".blt"); File candidateFile = new File(candidateDir, baseName + "." + "candidates.cid"); int numberOfCandidates = 4 + rand.nextInt(10); List<ECPoint> candidateNumbers = new ArrayList<>(numberOfCandidates); for (int candidateNo = 0; candidateNo != numberOfCandidates; candidateNo++) { candidateNumbers.add(generatePoint(ecParams, pointRandom)); } int numberOfBallots = ballotSize + rand.nextInt(ballotSize / 10); ECPair[][] ballots = new ECPair[numberOfBallots][]; for (int ballotNo = 0; ballotNo != numberOfBallots; ballotNo++) { Collections.shuffle(candidateNumbers, rand); ECPair[] ballot = new ECPair[numberOfCandidates]; for (int i = 0; i != ballot.length; i++) { ballot[i] = encryptor.encrypt(candidateNumbers.get(i)); } ballots[ballotNo] = ballot; } OutputStream fOut = new BufferedOutputStream(new FileOutputStream(ballotFile)); for (int j = 0; j != ballots.length; j++) { fOut.write(new PairSequence(ballots[j]).getEncoded()); } fOut.close(); createCandidateList(baseName, candidateFile, "ATL", candidateNumbers); }
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"); } }