diff --git a/mixer/src/main/java/mixer/Graph.java b/mixer/src/main/java/mixer/Graph.java index d57a044..115d38d 100644 --- a/mixer/src/main/java/mixer/Graph.java +++ b/mixer/src/main/java/mixer/Graph.java @@ -10,7 +10,7 @@ class Graph private Node[] nodes; protected Graph(int[] permutation){ n = permutation.length; // n = 2^k - nDiv2 = n /2; + nDiv2 = n >> 1; createNodes(); createEdges(permutation); setSwitches(); diff --git a/mixer/src/main/java/mixer/Mixer.java b/mixer/src/main/java/mixer/Mixer.java index e8fee4b..4179e6a 100644 --- a/mixer/src/main/java/mixer/Mixer.java +++ b/mixer/src/main/java/mixer/Mixer.java @@ -6,6 +6,8 @@ import java.util.Queue; import java.util.concurrent.ArrayBlockingQueue; import javafx.util.Pair; import java.util.Random; +import java.util.regex.Matcher; + import com.google.protobuf.InvalidProtocolBufferException; import meerkat.protobuf.Crypto.*; @@ -30,41 +32,38 @@ public class Mixer implements meerkat.crypto.mixnet.Mixer{ public Pair mix(List ciphertexts) throws InvalidProtocolBufferException{ int n = ciphertexts.size(); + int nDiv2 = n >> 1; // assert n = 2^k and n > 1 if( n <= 1 || ((n & (n-1)) != 0)) return null; //initialization - int layers = 0; - for (int i = n; i > 1; i>>=1) { - layers++; - } - layers<<=1; - layers--; + int layers = (int)(2 * Math.log(n)/Math.log(2)) - 1; // layers = 2logn -1 RerandomizableEncryptedMessage[][] encryptionTable = new RerandomizableEncryptedMessage[layers][n]; - ZeroKnowledgeProof[][] proofsTable= new ZeroKnowledgeProof[layers][n/2]; + ZeroKnowledgeProof[][] proofsTable= new ZeroKnowledgeProof[layers][nDiv2]; boolean[][] mixnet = createMixNet(n,layers); int index1, index2, switchIndex = 0; EncryptionRandomness r1 ,r2; RerandomizableEncryptedMessage e1, e2; boolean half = true; - //set first level of encryption + // set first level of encryption for (int j = 0; j < n; j++) { encryptionTable[0][j] = ciphertexts.get(j); } // main loop - int i = n; + int i = n,iDiv2; for (int layer = 0; layer < layers; layer++) // i == permutation size { + iDiv2 = i >> 1; for (int j = 0; j < n; j += i) // j == permutation index { - for (int k = 0; k < i / 2; k++) // k == elements index in permutation j + for (int k = 0; k < iDiv2; k++) // k == elements index in permutation j { index1 = k + j; - index2 = k + j + i / 2; + index2 = k + j + iDiv2; e1 = encryptionTable[layer][index1]; e2 = encryptionTable[layer][index2]; r1 = encryptor.generateRandomness(random); @@ -85,12 +84,12 @@ public class Mixer implements meerkat.crypto.mixnet.Mixer{ encryptionTable[layer + 1][index2], mixnet[layer][switchIndex], r1,r2); - switchIndex = (switchIndex + 1) % (n / 2); + switchIndex = (switchIndex + 1) % nDiv2; } } if (half) { - i >>= 1; + i = iDiv2; if (i == 1) { half = false; @@ -127,32 +126,34 @@ public class Mixer implements meerkat.crypto.mixnet.Mixer{ int[] pi, piL, piR; Queue permutationsQueue = new ArrayBlockingQueue(n); Graph graph; - boolean[][] mixnet = new boolean[layers][n>>1]; + int iDiv2; + int nDiv2 = n >> 1; + boolean[][] mixnet = new boolean[layers][nDiv2]; permutationsQueue.add(permutation); - for (int i = n, layer = 0; i > 1; i >>= 1, layer++) // i == permutation size { - for (int j = 0; j < n / 2; j += i / 2) // j == permutation index + iDiv2 = i >> 1; + for (int j = 0; j < nDiv2; j += iDiv2) // j == permutation index { pi = permutationsQueue.remove(); graph = new Graph(pi); - piL = new int[i / 2]; - piR = new int[i / 2]; - for (int k = 0; k < i / 2; k++) // k == switch index in permutation j + piL = new int[iDiv2]; + piR = new int[iDiv2]; + for (int k = 0; k < iDiv2; k++) // k == switch index in permutation j { mixnet[layers - layer - 1][k + j] = graph.getSwitchValue(k, true); mixnet[layer][k + j] = graph.getSwitchValue(k, false); if (!mixnet[layers - layer - 1][k + j]) { - piL[k] = pi[k] % (i / 2); - piR[k] = pi[k + i / 2] % (i / 2); + piL[k] = pi[k] % iDiv2; + piR[k] = pi[k + iDiv2] % iDiv2; } else { - piL[k] = pi[k + i / 2] % (i / 2); - piR[k] = pi[k] % (i / 2); + piL[k] = pi[k + iDiv2] % iDiv2; + piR[k] = pi[k] % iDiv2; } } permutationsQueue.add(piL);