updated mixer code

mixer
tzlil.gon 2015-12-08 00:15:16 +02:00
parent a834194d50
commit b502dc82d3
2 changed files with 25 additions and 24 deletions

View File

@ -10,7 +10,7 @@ class Graph
private Node[] nodes; private Node[] nodes;
protected Graph(int[] permutation){ protected Graph(int[] permutation){
n = permutation.length; // n = 2^k n = permutation.length; // n = 2^k
nDiv2 = n /2; nDiv2 = n >> 1;
createNodes(); createNodes();
createEdges(permutation); createEdges(permutation);
setSwitches(); setSwitches();

View File

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