diff --git a/source/crypto/crypto-adv/pom.xml b/source/crypto/crypto-adv/pom.xml
index dc01e85d..ac72b043 100644
--- a/source/crypto/crypto-adv/pom.xml
+++ b/source/crypto/crypto-adv/pom.xml
@@ -27,20 +27,6 @@
${project.version}
-
- org.powermock
- powermock-module-junit4
- 1.6.2
- test
-
-
-
- org.powermock
- powermock-api-mockito
- 1.6.2
- test
-
-
net.i2p.cryptoeddsa
@@ -52,11 +38,6 @@
5.1.0
-
- org.mockito
- mockito-core
- test
- com.jd.blockchaincrypto-framework
@@ -64,5 +45,11 @@
compile
+
+ com.n1analytics
+ javallier_2.10
+ 0.6.0
+
+
\ No newline at end of file
diff --git a/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/mpc/MultiSum.java b/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/mpc/MultiSum.java
index cb0316d3..7fe64ef9 100644
--- a/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/mpc/MultiSum.java
+++ b/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/mpc/MultiSum.java
@@ -2,6 +2,8 @@ package com.jd.blockchain.crypto.mpc;
import java.math.BigInteger;
+import com.n1analytics.paillier.PaillierPrivateKey;
+import com.n1analytics.paillier.PaillierPublicKey;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.agreement.ECDHBasicAgreement;
@@ -12,9 +14,6 @@ import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.util.encoders.Hex;
-import com.jd.blockchain.crypto.paillier.KeyPair;
-import com.jd.blockchain.crypto.paillier.PaillierUtils;
-import com.jd.blockchain.crypto.paillier.PublicKey;
import com.jd.blockchain.crypto.utils.sm.SM2Utils;
import com.jd.blockchain.crypto.utils.sm.SM3Utils;
import com.jd.blockchain.utils.io.BytesUtils;
@@ -32,7 +31,6 @@ public class MultiSum {
this.ePubKey = (ECPublicKeyParameters) eKeyPair.getPublic();
this.curve = SM2Utils.getCurve();
this.domainParams = SM2Utils.getDomainParams();
-
}
public BigInteger calculateAgreement(CipherParameters otherEPubKey){
@@ -47,20 +45,23 @@ public class MultiSum {
return new BigInteger(1,SM3Utils.hash(inputBytes));
}
- public static BigInteger encryptBlindedMsg(PublicKey encKey, BigInteger msg, BigInteger frontShare, BigInteger rearShare){
- return encKey.encrypt(msg.add(frontShare).subtract(rearShare).mod(encKey.getN()));
+ public static BigInteger encryptBlindedMsg(PaillierPublicKey encKey, BigInteger msg, BigInteger frontShare, BigInteger rearShare){
+ BigInteger modulus = encKey.getModulus();
+ BigInteger plaintext = msg.add(frontShare).subtract(rearShare).mod(modulus);
+ return encKey.raw_encrypt(plaintext);
}
- public static BigInteger aggregateCiphertexts(PublicKey encKey, BigInteger... bigIntegersList){
+ public static BigInteger aggregateCiphertexts(PaillierPublicKey encKey, BigInteger... bigIntegers){
BigInteger aggregatedCiphertext = BigInteger.ONE;
- for (BigInteger entry : bigIntegersList) {
- aggregatedCiphertext = aggregatedCiphertext.multiply(entry).mod(encKey.getnSquared());
+ BigInteger modulusSquared = encKey.getModulusSquared();
+ for (BigInteger entry : bigIntegers) {
+ aggregatedCiphertext = aggregatedCiphertext.multiply(entry).mod(modulusSquared);
}
return aggregatedCiphertext;
}
- public static BigInteger decrypt(KeyPair keyPair, BigInteger ciphertext){
- return keyPair.decrypt(ciphertext);
+ public static BigInteger decrypt(PaillierPrivateKey decKey, BigInteger ciphertext){
+ return decKey.raw_decrypt(ciphertext);
}
public ECPublicKeyParameters getEPubKey(){return ePubKey;}
@@ -79,7 +80,7 @@ public class MultiSum {
}
public byte[] getEPrivKeyBytes(){
- return PaillierUtils.BigIntegerToLBytes(ePrivKey.getD(),32);
+ return BigIntegerToLBytes(ePrivKey.getD(),32);
}
public ECPublicKeyParameters resolveEPubKey(byte[] ePubKeyBytes){
@@ -95,4 +96,16 @@ public class MultiSum {
return new ECPrivateKeyParameters(new BigInteger(1,ePrivKeyBytes),domainParams);
}
+ // To convert BigInteger to byte[] whose length is l
+ private static byte[] BigIntegerToLBytes(BigInteger b, int l){
+ byte[] tmp = b.toByteArray();
+ byte[] result = new byte[l];
+ if (tmp.length > result.length) {
+ System.arraycopy(tmp, tmp.length - result.length, result, 0, result.length);
+ }
+ else {
+ System.arraycopy(tmp,0,result,result.length-tmp.length,tmp.length);
+ }
+ return result;
+ }
}
diff --git a/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/paillier/KeyPair.java b/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/paillier/KeyPair.java
deleted file mode 100644
index 68fd6970..00000000
--- a/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/paillier/KeyPair.java
+++ /dev/null
@@ -1,98 +0,0 @@
-package com.jd.blockchain.crypto.paillier;
-
-import java.math.BigInteger;
-import java.util.List;
-
-import com.jd.blockchain.utils.io.BytesUtils;
-
-/**
- * The MIT License (MIT)
- *
- * Copyright (c) 2014 Hendrik Kunert
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-/**
- * A class that holds a pair of associated public and private keys.
- */
-public class KeyPair {
- private final PrivateKey privateKey;
- private final PublicKey publicKey;
- private final BigInteger upperBound;
-
- public KeyPair(PrivateKey privateKey, PublicKey publicKey, BigInteger upperBound) {
- this.privateKey = privateKey;
- this.publicKey = publicKey;
- this.upperBound = upperBound;
- }
-
- public KeyPair(byte[] privKeyBytes,byte[] pubKeyBytes,byte[] upperBoundBytes){
- this.privateKey = new PrivateKey(privKeyBytes);
- this.publicKey = new PublicKey(pubKeyBytes);
- this.upperBound = new BigInteger(upperBoundBytes);
- }
-
- public KeyPair(byte[] keyPairBytes){
- List list = PaillierUtils.split(keyPairBytes, "##KeyPair##".getBytes());
- this.privateKey = new PrivateKey(list.get(0));
- this.publicKey = new PublicKey(list.get(1));
- this.upperBound = new BigInteger(list.get(2));
- }
-
- public PrivateKey getPrivateKey() {
- return privateKey;
- }
-
- public PublicKey getPublicKey() {
- return publicKey;
- }
-
- public BigInteger getUpperBound() { return upperBound; }
-
- /**
- * Decrypts the given ciphertext.
- *
- * @param c The ciphertext that should be decrypted.
- * @return The corresponding plaintext. If an upper bound was given to {@link KeyPairBuilder},
- * the result can also be negative. See {@link KeyPairBuilder#upperBound(BigInteger)} for details.
- */
- public final BigInteger decrypt(BigInteger c) {
-
- BigInteger n = publicKey.getN();
- BigInteger nSquare = publicKey.getnSquared();
- BigInteger lambda = privateKey.getLambda();
-
- BigInteger u = privateKey.getPreCalculatedDenominator();
-
- BigInteger p = c.modPow(lambda, nSquare).subtract(BigInteger.ONE).divide(n).multiply(u).mod(n);
-
- if (upperBound != null && p.compareTo(upperBound) > 0) {
- p = p.subtract(n);
- }
-
- return p;
- }
-
- public byte[] getUpperBoundBytes(){ return upperBound.toByteArray(); }
-
- public byte[] getKeyPairBytes(){
- return BytesUtils.concat(privateKey.getPrivKeyBytes(),"##KeyPair##".getBytes(),publicKey.getPubKeyBytes(),"##KeyPair##".getBytes(),upperBound.toByteArray());
- }
-}
diff --git a/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/paillier/KeyPairBuilder.java b/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/paillier/KeyPairBuilder.java
deleted file mode 100644
index c4234cae..00000000
--- a/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/paillier/KeyPairBuilder.java
+++ /dev/null
@@ -1,165 +0,0 @@
-package com.jd.blockchain.crypto.paillier;
-
-import java.math.BigInteger;
-import java.security.SecureRandom;
-import java.util.Random;
-/**
- * The MIT License (MIT)
- *
- * Copyright (c) 2014 Hendrik Kunert
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-/**
- * A class that is used for generating a pair of associated public and private
- * keys.
- *
- * @see KeyPair
- */
-public class KeyPairBuilder {
-
- private int bits = 1024;
-
- private int certainty = 0;
-
- private Random rng;
-
- private BigInteger upperBound;
-
- /**
- * Sets the size of the key to be created.
- *
- * The default size is 1024 bits.
- *
- * @param bits The size of the key in bits.
- * @return This instance of KeyPairBuilder for method chaining.
- */
- public KeyPairBuilder bits(int bits) {
- this.bits = bits;
- return this;
- }
-
- /**
- * See {@link BigInteger#BigInteger(int, int, Random)} for more details.
- *
- * The default value is 0.
- *
- * @return This instance of KeyPairBuilder for method chaining.
- */
- public KeyPairBuilder certainty(int certainty) {
- this.certainty = certainty;
- return this;
- }
-
- /**
- * Sets the random number generator that is used for the generation of
- * internally needed prime numbers.
- *
- * The default is {@link SecureRandom}.
- *
- * Warning:
- * The change of this value affects the security of the whole cryptographic
- * system.
- *
- * @param rng The random number generator that should be used instead of
- * {@link SecureRandom}.
- * @return This instance of KeyPairBuilder for method chaining.
- */
- public KeyPairBuilder randomNumberGenerator(Random rng) {
- this.rng = rng;
- return this;
- }
-
- /**
- * Sets an upper bound that is used for decrypting ciphertexts representing a negative value.
- *
- * In most cases the upper bound should be the same as of the underlying number system -
- * for example {@link Integer#MAX_VALUE}.
- *
- * @param b The upper bound.
- * @return This instance of KeyPairBuilder for method chaining.
- */
- public KeyPairBuilder upperBound(BigInteger b) {
- this.upperBound = b;
- return this;
- }
-
- /**
- * Creates a pair of associated public and private keys.
- *
- * @return The pair of associated public and private keys.
- */
- public KeyPair generateKeyPair() {
- if (rng == null) {
- rng = new SecureRandom();
- }
-
- BigInteger p, q;
- int length = bits / 2;
- if (certainty > 0) {
- p = new BigInteger(length, certainty, rng);
- q = new BigInteger(length, certainty, rng);
- } else {
- p = BigInteger.probablePrime(length, rng);
- q = BigInteger.probablePrime(length, rng);
- }
-
- BigInteger n = p.multiply(q);
- BigInteger nSquared = n.multiply(n);
-
- BigInteger pMinusOne = p.subtract(BigInteger.ONE);
- BigInteger qMinusOne = q.subtract(BigInteger.ONE);
-
- BigInteger lambda = this.lcm(pMinusOne, qMinusOne);
-
- BigInteger g;
- BigInteger helper;
-
- do {
- g = new BigInteger(bits, rng);
- helper = calculateL(g.modPow(lambda, nSquared), n);
-
- } while (!helper.gcd(n).equals(BigInteger.ONE));
-
- PublicKey publicKey = new PublicKey(n, nSquared, g, bits);
- PrivateKey privateKey = new PrivateKey(lambda, helper.modInverse(n));
-
- return new KeyPair(privateKey, publicKey, upperBound);
-
- }
-
- // TODO separate this somewhere
- private BigInteger calculateL(BigInteger u, BigInteger n) {
- BigInteger result = u.subtract(BigInteger.ONE);
- result = result.divide(n);
- return result;
- }
-
- // TODO add to own BigInteger extended class
- private BigInteger lcm(BigInteger a, BigInteger b) {
- BigInteger result;
- BigInteger gcd = a.gcd(b);
-
- result = a.abs().divide(gcd);
- result = result.multiply(b.abs());
-
- return result;
- }
-}
diff --git a/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/paillier/PaillierUtils.java b/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/paillier/PaillierUtils.java
deleted file mode 100644
index 4c199487..00000000
--- a/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/paillier/PaillierUtils.java
+++ /dev/null
@@ -1,61 +0,0 @@
-package com.jd.blockchain.crypto.paillier;
-
-import java.math.BigInteger;
-import java.util.Arrays;
-import java.util.LinkedList;
-import java.util.List;
-
-public class PaillierUtils {
-
- // To convert BigInteger to byte[] whose length is l
- public static byte[] BigIntegerToLBytes(BigInteger b, int l){
- byte[] tmp = b.toByteArray();
- byte[] result = new byte[l];
- if (tmp.length > result.length) {
- System.arraycopy(tmp, tmp.length - result.length, result, 0, result.length);
- }
- else {
- System.arraycopy(tmp,0,result,result.length-tmp.length,tmp.length);
- }
- return result;
- }
-
- public static byte[] intToBytes(int i){
- byte[] result = new byte[4];
- result[0] = (byte) (i >> 24);
- result[1] = (byte) (i >> 16);
- result[2] = (byte) (i >> 8);
- result[3] = (byte) (i);
- return result;
- }
-
- public static int bytesToInt(byte[] array){
- int result = 0;
- result |= ((array[0] & 0xFF) << 24);
- result |= ((array[1] & 0xFF) << 16);
- result |= ((array[2] & 0xFF) << 8);
- result |= ((array[3] & 0xFF));
- return result;
- }
-
- public static List split(byte[] array, byte[] delimiter) {
- List byteArrays = new LinkedList<>();
- if (delimiter.length == 0) {
- return byteArrays;
- }
- int begin = 0;
-
- outer:
- for (int i = 0; i < array.length - delimiter.length + 1; i++) {
- for (int j = 0; j < delimiter.length; j++) {
- if (array[i + j] != delimiter[j]) {
- continue outer;
- }
- }
- byteArrays.add(Arrays.copyOfRange(array, begin, i));
- begin = i + delimiter.length;
- }
- byteArrays.add(Arrays.copyOfRange(array, begin, array.length));
- return byteArrays;
- }
-}
diff --git a/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/paillier/PrivateKey.java b/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/paillier/PrivateKey.java
deleted file mode 100644
index ac4b656a..00000000
--- a/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/paillier/PrivateKey.java
+++ /dev/null
@@ -1,75 +0,0 @@
-package com.jd.blockchain.crypto.paillier;
-
-import java.math.BigInteger;
-import java.util.List;
-/**
- * The MIT License (MIT)
- *
- * Copyright (c) 2014 Hendrik Kunert
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-import com.jd.blockchain.utils.io.BytesUtils;
-
-/**
- * A class that represents the private part of the Paillier key pair.
- */
-public class PrivateKey {
-
- private final BigInteger lambda;
- private final BigInteger preCalculatedDenominator;
-
- public PrivateKey(BigInteger lambda, BigInteger preCalculatedDenominator) {
- this.lambda = lambda;
-
- this.preCalculatedDenominator = preCalculatedDenominator;
- }
-
- public PrivateKey(byte[] lambdaBytes, byte[] preCalculatedDenominatorBytes){
- this.lambda = new BigInteger(lambdaBytes);
- this.preCalculatedDenominator = new BigInteger(preCalculatedDenominatorBytes);
- }
-
- public PrivateKey(byte[] privKeyBytes){
- List list = PaillierUtils.split(privKeyBytes, "##PrivateKey##".getBytes());
- this.lambda = new BigInteger(list.get(0));
- this.preCalculatedDenominator = new BigInteger(list.get(1));
- }
-
- public BigInteger getLambda() {
- return lambda;
- }
-
- public BigInteger getPreCalculatedDenominator() {
- return preCalculatedDenominator;
- }
-
- public byte[] getLambdaBytes(){
- return lambda.toByteArray();
- }
-
- public byte[] getPreCalculatedDenominatorBytes(){
- return preCalculatedDenominator.toByteArray();
- }
-
- public byte[] getPrivKeyBytes(){
- return BytesUtils.concat(getLambdaBytes(),"##PrivateKey##".getBytes(),getPreCalculatedDenominatorBytes());
- }
-}
diff --git a/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/paillier/PublicKey.java b/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/paillier/PublicKey.java
deleted file mode 100644
index aa1d3f51..00000000
--- a/source/crypto/crypto-adv/src/main/java/com/jd/blockchain/crypto/paillier/PublicKey.java
+++ /dev/null
@@ -1,129 +0,0 @@
-package com.jd.blockchain.crypto.paillier;
-
-import java.math.BigInteger;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Random;
-/**
- * The MIT License (MIT)
- *
- * Copyright (c) 2014 Hendrik Kunert
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-import com.jd.blockchain.utils.io.ByteArray;
-import com.jd.blockchain.utils.io.BytesUtils;
-
-/**
- * A class that represents the public part of the Paillier key pair.
- *
- * As in all asymmetric cryptographic systems it is responsible for the
- * encryption.
- *
- * Additional instructions for the decryption can be found on {@link KeyPair}.
- *
- * @see KeyPair
- */
-public class PublicKey {
- private final int bits;
- private final BigInteger n;
- private final BigInteger nSquared;
- private final BigInteger g;
-
- public PublicKey(BigInteger n, BigInteger nSquared, BigInteger g, int bits) {
- this.n = n;
- this.nSquared = nSquared;
- this.bits = bits;
- this.g = g;
- }
-
- public PublicKey(byte[] nBytes, byte[] nSquaredBytes, byte[] gBytes, byte[] bitsBytes) {
- this.n = new BigInteger(nBytes);
- this.nSquared = new BigInteger(nSquaredBytes);
- this.g = new BigInteger(gBytes);
- this.bits = PaillierUtils.bytesToInt(bitsBytes);
- }
-
- public PublicKey(byte[] pubKeyBytes){
- List list = PaillierUtils.split(pubKeyBytes, "##PublicKey##".getBytes());
- this.n = new BigInteger(list.get(0));
- this.nSquared = new BigInteger(list.get(1));
- this.g = new BigInteger(list.get(2));
- this.bits = PaillierUtils.bytesToInt(list.get(3));
- }
-
-
- public int getBits() {
- return bits;
- }
-
- public BigInteger getN() {
- return n;
- }
-
- public BigInteger getnSquared() {
- return nSquared;
- }
-
- public BigInteger getG() {
- return g;
- }
-
- /**
- * Encrypts the given plaintext.
- *
- * @param m The plaintext that should be encrypted.
- * @return The corresponding ciphertext.
- */
- public final BigInteger encrypt(BigInteger m) {
-
- BigInteger r;
- do {
- r = new BigInteger(bits, new Random());
- } while (r.compareTo(n) >= 0);
-
- BigInteger result = g.modPow(m, nSquared);
- BigInteger x = r.modPow(n, nSquared);
-
- result = result.multiply(x);
- result = result.mod(nSquared);
-
- return result;
- }
-
- public byte[] getBitsBytes(){
- return PaillierUtils.intToBytes(bits);
- }
-
- public byte[] getNBytes(){ return n.toByteArray(); }
-
- public byte[] getNSquaredBytes(){
- return nSquared.toByteArray();
- }
-
- public byte[] getGBytes(){
- return g.toByteArray();
- }
-
- public byte[] getPubKeyBytes(){
- return BytesUtils.concat(getNBytes(),"##PublicKey##".getBytes(),getNSquaredBytes(),"##PublicKey##".getBytes(),getGBytes(),"##PublicKey##".getBytes(),getBitsBytes());
- }
-}
-
diff --git a/source/crypto/crypto-adv/src/test/java/test/com/jd/blockchain/crypto/mpc/MultiSumTest.java b/source/crypto/crypto-adv/src/test/java/test/com/jd/blockchain/crypto/mpc/MultiSumTest.java
index 4dff44cc..2bb53407 100644
--- a/source/crypto/crypto-adv/src/test/java/test/com/jd/blockchain/crypto/mpc/MultiSumTest.java
+++ b/source/crypto/crypto-adv/src/test/java/test/com/jd/blockchain/crypto/mpc/MultiSumTest.java
@@ -1,9 +1,8 @@
package test.com.jd.blockchain.crypto.mpc;
import com.jd.blockchain.crypto.mpc.MultiSum;
-import com.jd.blockchain.crypto.paillier.KeyPair;
-import com.jd.blockchain.crypto.paillier.KeyPairBuilder;
-import com.jd.blockchain.crypto.paillier.PublicKey;
+import com.n1analytics.paillier.PaillierPrivateKey;
+import com.n1analytics.paillier.PaillierPublicKey;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.util.encoders.Hex;
@@ -16,14 +15,13 @@ import static org.junit.Assert.*;
public class MultiSumTest {
- private KeyPair keyPair;
- private PublicKey encKey;
+ private PaillierPrivateKey decKey;
+ private PaillierPublicKey encKey;
@Before
public void init() {
- KeyPairBuilder keygen = new KeyPairBuilder();
- keyPair = keygen.generateKeyPair();
- encKey = keyPair.getPublicKey();
+ decKey = PaillierPrivateKey.create(2048);
+ encKey = decKey.getPublicKey();
}
@Test
@@ -72,7 +70,7 @@ public class MultiSumTest {
BigInteger aggregatedCiphertext = MultiSum.aggregateCiphertexts(encKey,c1,c2,c3);
- BigInteger decryptedValue = MultiSum.decrypt(keyPair,aggregatedCiphertext);
+ BigInteger decryptedValue = MultiSum.decrypt(decKey,aggregatedCiphertext);
assertEquals(expectedSum,decryptedValue);
}
diff --git a/source/crypto/crypto-adv/src/test/java/test/com/jd/blockchain/crypto/paillier/DecryptionTest.java b/source/crypto/crypto-adv/src/test/java/test/com/jd/blockchain/crypto/paillier/DecryptionTest.java
deleted file mode 100644
index 8994d9dc..00000000
--- a/source/crypto/crypto-adv/src/test/java/test/com/jd/blockchain/crypto/paillier/DecryptionTest.java
+++ /dev/null
@@ -1,58 +0,0 @@
-package test.com.jd.blockchain.crypto.paillier;
-
-import com.jd.blockchain.crypto.paillier.KeyPair;
-import com.jd.blockchain.crypto.paillier.KeyPairBuilder;
-import com.jd.blockchain.crypto.paillier.PublicKey;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-
-import java.math.BigInteger;
-import java.util.Arrays;
-import java.util.Collection;
-
-import static org.junit.Assert.assertEquals;
-
-/**
- * Created by kunerd on 22.09.15.
- */
-@RunWith(value = Parameterized.class)
-public class DecryptionTest {
-
- @Parameterized.Parameters
- public static Collection