From b7e543e5e8b81da6f72ea7b76c3356f6e183aff2 Mon Sep 17 00:00:00 2001 From: "tzlil.gon" Date: Sun, 20 Mar 2016 16:15:43 +0200 Subject: [PATCH] simple rerandomize test --- gradle/wrapper/gradle-wrapper.properties | 2 +- .../src/test/java/mixer/CreateTestVector.java | 4 +- .../src/test/java/mixer/RerandomizeTest.java | 16 +++- .../java/mixer/SimpleRerandomizeTest.java | 89 +++++++++++++++++++ 4 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 mixer/src/test/java/mixer/SimpleRerandomizeTest.java diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index b99caa4..98af882 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,4 +1,4 @@ -#Sun Dec 27 09:28:01 IST 2015 +#Sun Mar 20 15:13:00 IST 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME diff --git a/mixer/src/test/java/mixer/CreateTestVector.java b/mixer/src/test/java/mixer/CreateTestVector.java index fcb273b..52f07fa 100644 --- a/mixer/src/test/java/mixer/CreateTestVector.java +++ b/mixer/src/test/java/mixer/CreateTestVector.java @@ -68,7 +68,7 @@ public class CreateTestVector { } return result; } - //@Test + //@SimpleRerandomizeTest public void createValidTest() throws IOException { List mixerInput = generateMixerInput(); @@ -81,7 +81,7 @@ public class CreateTestVector { System.out.println("all done"); } - //@Test + //@SimpleRerandomizeTest public void createInvalidTest() throws IOException { //Mix2ZeroKnowledgeVerifier corruptedVerifier = new Verifier(encryptor,randomOracle,true); diff --git a/mixer/src/test/java/mixer/RerandomizeTest.java b/mixer/src/test/java/mixer/RerandomizeTest.java index c607f61..c2f0bb7 100644 --- a/mixer/src/test/java/mixer/RerandomizeTest.java +++ b/mixer/src/test/java/mixer/RerandomizeTest.java @@ -62,10 +62,18 @@ public class RerandomizeTest { ConcreteCrypto.ElGamalCiphertext eElGamal = ECElGamalEncryption.RerandomizableEncryptedMessage2ElGamalCiphertext(e); ConcreteCrypto.ElGamalCiphertext eNewElGamal = ECElGamalEncryption.RerandomizableEncryptedMessage2ElGamalCiphertext(eNew); - assert (g.multiply(new BigInteger(r.getData().toByteArray())).equals( - group.add(convert2ECPoint(eNewElGamal.getC1()),group.negate(convert2ECPoint(eElGamal.getC1()))))); - assert(h.multiply(new BigInteger(r.getData().toByteArray())).equals( - group.add(convert2ECPoint(eNewElGamal.getC2()), group.negate(convert2ECPoint(eElGamal.getC2()))))); + ECPoint expected1 = g.multiply(new BigInteger(r.getData().toByteArray())); + ECPoint result1 = group.add(convert2ECPoint(eNewElGamal.getC1()),group.negate(convert2ECPoint(eElGamal.getC1()))); + expected1.normalize(); + result1.normalize(); + assert (expected1.equals(result1)); + + ECPoint expected2 = h.multiply(new BigInteger(r.getData().toByteArray())); + ECPoint result2 = group.add(convert2ECPoint(eNewElGamal.getC2()), group.negate(convert2ECPoint(eElGamal.getC2()))); + expected2.normalize(); + result2.normalize(); + + assert (expected2.equals(result2)); } @Test diff --git a/mixer/src/test/java/mixer/SimpleRerandomizeTest.java b/mixer/src/test/java/mixer/SimpleRerandomizeTest.java new file mode 100644 index 0000000..b907e86 --- /dev/null +++ b/mixer/src/test/java/mixer/SimpleRerandomizeTest.java @@ -0,0 +1,89 @@ +package mixer; + +import org.bouncycastle.math.ec.ECPoint; +import org.junit.Before; +import qilin.primitives.concrete.ECGroup; + +import java.math.BigInteger; +import java.util.Random; + +/** + * Created by Tzlil on 3/20/2016. + */ +public class SimpleRerandomizeTest { + + Random rand; + ECGroup group; + ECPoint g ; + ECPoint h ; + BigInteger sk; + @Before + public void setup() throws Exception { + rand = new Random(); + group = new ECGroup("secp256k1"); + g = group.getGenerator(); + sk = random(); + h = g.multiply(sk); + } + + public BigInteger random(){ + return new BigInteger(256,rand).mod(group.orderUpperBound()); + } + + public ECPoint randomMessage(){ + return group.sample(rand); + } + + public Encryption encrypt(ECPoint message,BigInteger randomness){ + return new Encryption(g.multiply(randomness), message.add(h.multiply(randomness))); + } + + public Encryption rerandomize(Encryption encryption,BigInteger randomness){ + return new Encryption(encryption.a.add(g.multiply(randomness)),encryption.b.add(h.multiply(randomness))); + } + + public ECPoint decrypt(Encryption encryption){ + return encryption.a.multiply(sk).negate().add(encryption.b); + } + + public class Encryption{ + ECPoint a,b; + Encryption(ECPoint a, ECPoint b){ + this.a = a; + this.b = b; + } + } + + public void oneRerandomizeTest(){ + ECPoint message = randomMessage(); + Encryption e = encrypt(message,random()); + BigInteger r = random(); + Encryption eNew = rerandomize(e,r); + assert (decrypt(e).equals(message)); + assert (decrypt(eNew).equals(message)); + + ECPoint expected1 = g.multiply(r); + ECPoint result1 = group.add(eNew.a,e.a.negate()); + expected1.normalize(); + result1.normalize(); + assert (expected1.equals(result1)); + + ECPoint expected2 = h.multiply(r); + ECPoint result2 = group.add(eNew.b,e.b.negate()); + expected2.normalize(); + result2.normalize(); + assert (expected2.equals(result2)); + } + + + @org.junit.Test + public void rerandomizeTest(){ + + int tests = 1000; + + for (int i = 0; i < tests; i ++){ + System.out.println("rerandomize test #" + i); + oneRerandomizeTest(); + } + } +}