Bläddra i källkod

prepareTarget tested

Boyoung- 9 år sedan
förälder
incheckning
a1b9ebee34
3 ändrade filer med 341 tillägg och 7 borttagningar
  1. 85 7
      src/gc/GCLib.java
  2. 254 0
      src/protocols/PrepareTarget.java
  3. 2 0
      src/ui/CLI.java

+ 85 - 7
src/gc/GCLib.java

@@ -8,6 +8,8 @@ import com.oblivm.backend.flexsc.CompEnv;
 
 public class GCLib<T> extends IntegerLib<T> {
 
+	// TODO: take root bucket width into account
+
 	private int d;
 	private int w;
 	private int logD;
@@ -18,7 +20,8 @@ public class GCLib<T> extends IntegerLib<T> {
 		this.d = d;
 		this.w = w;
 		logD = (int) Math.ceil(Math.log(d) / Math.log(2));
-		logW = (int) Math.ceil(Math.log(w) / Math.log(2));
+		logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2)); // includes fake
+																// empty tuple
 	}
 
 	private void zerosFollowedByOnes(T[] input) {
@@ -47,9 +50,11 @@ public class GCLib<T> extends IntegerLib<T> {
 			T[] lz = xor(pathLabel, tupleLabels[j]);
 			zerosFollowedByOnes(lz);
 			lz = padSignal(lz, d); // add sign bit
+
 			T firstIf = and(feBits[j], less(lz, l));
 			l = mux(l, lz, firstIf);
 			j1 = mux(j1, tupleIndex, firstIf);
+
 			et = mux(ones(1), et, feBits[j]);
 			j2 = mux(tupleIndex, j2, feBits[j]);
 		}
@@ -68,13 +73,14 @@ public class GCLib<T> extends IntegerLib<T> {
 
 	public T[][][] prepareDeepest(byte[] Li, T[][] E_feBits, T[][] C_feBits, T[][][] E_tupleLabels,
 			T[][][] C_tupleLabels) {
+		T[] perpD = ones(logD + 1);
 		T[][][] output = env.newTArray(4, d, 0);
 
 		T[][] deepest = env.newTArray(d, 0);
 		for (int j = 0; j < d; j++)
-			deepest[j] = ones(logD + 1);
-		T[] src = ones(logD + 1);
-		T[] goal = ones(logD + 1); // -1 in 2's complement form
+			deepest[j] = perpD;
+		T[] src = perpD;
+		T[] goal = perpD; // \perp = -1 in 2's complement form
 
 		for (int i = 0; i < d; i++) {
 			T[] index = toSignals(i, logD + 1);
@@ -86,13 +92,85 @@ public class GCLib<T> extends IntegerLib<T> {
 			output[2][i] = dae[2];
 			output[3][i] = dae[3];
 
-			T secondIf = greater(l, goal);
-			goal = mux(goal, l, secondIf);
-			src = mux(src, index, secondIf);
+			T lGreaterGoal = greater(l, goal);
+			goal = mux(goal, l, lGreaterGoal);
+			src = mux(src, index, lGreaterGoal);
 		}
 
 		output[0] = deepest;
 		return output;
 	}
 
+	public T[][][] prepareTarget(T[][] deepest, T[][] j1, T[][] j2, T[][] et) {
+		// TODO: root bucket ft?
+		T[] ft = toSignals(w, logW + 1);
+		T[] perpD = ones(logD + 1);
+		T[] perpW = ones(logW + 1);
+		T[][][] output = env.newTArray(3, 4, 0);
+
+		T[] nTop = perpD;
+		T[] nBot = perpD;
+		T[] eTop = perpD;
+		T[] eBot = perpD;
+		T[] src = perpD;
+		T[] dest = perpD;
+		T[][] target = env.newTArray(d, 0);
+		T[][] f = env.newTArray(d, 0);
+		for (int i = 0; i < d; i++) {
+			target[i] = perpD;
+			f[i] = perpW;
+		}
+
+		for (int i = d - 1; i >= 0; i--) {
+			T[] index = toSignals(i, logD + 1);
+
+			T iEqSrc = eq(index, src);
+			target[i] = mux(target[i], dest, iEqSrc);
+			f[i] = mux(f[i], j1[i], iEqSrc);
+			src = mux(src, perpD, iEqSrc);
+
+			T deepestEqPerp = eq(deepest[i], perpD);
+			dest = mux(dest, index, and(iEqSrc, deepestEqPerp));
+			dest = mux(dest, perpD, and(iEqSrc, not(deepestEqPerp)));
+
+			T destEqPerp = eq(dest, perpD);
+			T srcEqPerp = eq(src, perpD);
+			T fourthIf = and(not(deepestEqPerp), and(not(destEqPerp), srcEqPerp));
+			target[i] = mux(target[i], dest, fourthIf);
+			f[i] = mux(f[i], j2[i], fourthIf);
+
+			T targetEqPerp = eq(target[i], perpD);
+			T fifthIf = and(not(deepestEqPerp), or(and(destEqPerp, et[i][0]), not(targetEqPerp)));
+			src = mux(src, deepest[i], fifthIf);
+			dest = mux(dest, index, fifthIf);
+			eTop = mux(eTop, src, fifthIf);
+
+			T eBotEqPerp = eq(eBot, perpD);
+			T sixthIf = and(fifthIf, eBotEqPerp);
+			eBot = mux(eBot, dest, sixthIf);
+			f[i] = mux(f[i], j2[i], sixthIf);
+
+			T fEqPerp = eq(f[i], perpW);
+			f[i] = mux(f[i], ft, fEqPerp);
+			nTop = mux(nTop, index, fEqPerp);
+
+			T nBotEqPerp = eq(nBot, perpD);
+			nBot = mux(nBot, index, and(fEqPerp, nBotEqPerp));
+		}
+
+		output[0] = target;
+		output[1] = f;
+		output[2][0] = nTop;
+		output[2][1] = nBot;
+		output[2][2] = eTop;
+		output[2][3] = eBot;
+		return output;
+	}
+
+	public T[][][] combineDeepestAndTarget(byte[] Li, T[][] E_feBits, T[][] C_feBits, T[][][] E_tupleLabels,
+			T[][][] C_tupleLabels) {
+		T[][][] out = prepareDeepest(Li, E_feBits, C_feBits, E_tupleLabels, C_tupleLabels);
+		return prepareTarget(out[0], out[1], out[2], out[3]);
+	}
+
 }

+ 254 - 0
src/protocols/PrepareTarget.java

@@ -0,0 +1,254 @@
+package protocols;
+
+import java.math.BigInteger;
+
+import com.oblivm.backend.flexsc.CompEnv;
+import com.oblivm.backend.gc.GCGenComp;
+import com.oblivm.backend.gc.GCSignal;
+import com.oblivm.backend.gc.regular.GCEva;
+import com.oblivm.backend.gc.regular.GCGen;
+import com.oblivm.backend.network.Network;
+
+import communication.Communication;
+import crypto.Crypto;
+import exceptions.NoSuchPartyException;
+import gc.GCLib;
+import oram.Forest;
+import oram.Metadata;
+import util.Util;
+
+public class PrepareTarget extends Protocol {
+
+	private int d = 10;
+	private int w = 8;
+	private int logD = (int) Math.ceil(Math.log(d) / Math.log(2));
+
+	public PrepareTarget(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	private GCSignal[] revSelectKeys(GCSignal[][] pairs, byte[] input) {
+		BigInteger in = new BigInteger(1, input);
+		GCSignal[] out = new GCSignal[pairs.length];
+		for (int i = 0; i < pairs.length; i++)
+			out[i] = pairs[i][in.testBit(i) ? 1 : 0];
+		return out;
+	}
+
+	private GCSignal[] selectKeys(GCSignal[][] pairs, byte[] input) {
+		BigInteger in = new BigInteger(1, input);
+		GCSignal[] out = new GCSignal[pairs.length];
+		for (int i = 0; i < pairs.length; i++)
+			out[i] = pairs[i][in.testBit(pairs.length - 1 - i) ? 1 : 0];
+		return out;
+	}
+
+	private GCSignal[][] genKeyPairs(int n) {
+		GCSignal[][] pairs = new GCSignal[n][];
+		for (int i = 0; i < n; i++)
+			pairs[i] = GCGenComp.genPair();
+		return pairs;
+	}
+
+	private GCSignal[] getZeroKeys(GCSignal[][] pairs) {
+		GCSignal[] keys = new GCSignal[pairs.length];
+		for (int i = 0; i < keys.length; i++)
+			keys[i] = pairs[i][0];
+		return keys;
+	}
+
+	private BigInteger decodeOutput(GCSignal[] out1, GCSignal[] out2) {
+		BigInteger output = BigInteger.ZERO;
+		for (int i = 0; i < out1.length; i++) {
+			if (out2[i].isPublic()) {
+				if (out2[i].v)
+					output = output.setBit(i);
+			} else if (out1[i].equals(out2[i])) {
+				;
+			} else if (out1[i].equals(GCGenComp.R.xor(out2[i]))) {
+				output = output.setBit(i);
+			} else {
+				System.err.println("ERROR on GC output!");
+				return null;
+			}
+		}
+		return output;
+	}
+
+	private String addZeros(String a, int n) {
+		String out = a;
+		for (int i = 0; i < n - a.length(); i++)
+			out = "0" + out;
+		return out;
+	}
+
+	public void runE() {
+		byte[] Li = Util.nextBytes((d - 1 + 7) / 8, Crypto.sr);
+
+		byte[][] feBits = new byte[d][];
+		byte[][][] tupleLabels = new byte[d][w][];
+
+		GCSignal[][][] E_feKeyPairs = new GCSignal[d][][];
+		GCSignal[][][] C_feKeyPairs = new GCSignal[d][][];
+		GCSignal[][] E_feZeroKeys = new GCSignal[d][];
+		GCSignal[][] C_feZeroKeys = new GCSignal[d][];
+		GCSignal[][] E_feKeyInput = new GCSignal[d][];
+
+		GCSignal[][][][] E_labelKeyPairs = new GCSignal[d][w][][];
+		GCSignal[][][][] C_labelKeyPairs = new GCSignal[d][w][][];
+		GCSignal[][][] E_labelZeroKeys = new GCSignal[d][w][];
+		GCSignal[][][] C_labelZeroKeys = new GCSignal[d][w][];
+		GCSignal[][][] E_labelKeyInput = new GCSignal[d][w][];
+
+		for (int i = 0; i < d; i++) {
+			feBits[i] = Util.nextBytes((w + 7) / 8, Crypto.sr);
+			for (int j = 0; j < w; j++)
+				tupleLabels[i][j] = Util.nextBytes((d - 1 + 7) / 8, Crypto.sr);
+
+			E_feKeyPairs[i] = genKeyPairs(w);
+			C_feKeyPairs[i] = genKeyPairs(w);
+			E_feZeroKeys[i] = getZeroKeys(E_feKeyPairs[i]);
+			C_feZeroKeys[i] = getZeroKeys(C_feKeyPairs[i]);
+			E_feKeyInput[i] = selectKeys(E_feKeyPairs[i], feBits[i]);
+
+			for (int j = 0; j < w; j++) {
+				E_labelKeyPairs[i][j] = genKeyPairs(d - 1);
+				C_labelKeyPairs[i][j] = genKeyPairs(d - 1);
+				E_labelZeroKeys[i][j] = getZeroKeys(E_labelKeyPairs[i][j]);
+				C_labelZeroKeys[i][j] = getZeroKeys(C_labelKeyPairs[i][j]);
+				E_labelKeyInput[i][j] = revSelectKeys(E_labelKeyPairs[i][j], tupleLabels[i][j]);
+			}
+		}
+
+		con1.write(Li);
+		con1.write(E_feKeyInput);
+		con1.write(C_feZeroKeys);
+		con1.write(E_labelKeyInput);
+		con1.write(C_labelZeroKeys);
+
+		Network channel = new Network(null, con1);
+
+		CompEnv<GCSignal> gen = new GCGen(channel);
+		GCSignal[][][] E_out = new GCLib<GCSignal>(gen, d, w).prepareDeepest(Li, E_feZeroKeys, C_feZeroKeys,
+				E_labelZeroKeys, C_labelZeroKeys);
+
+		GCSignal[][][] D_out = con1.readObject();
+
+		CompEnv<GCSignal> gen1 = new GCGen(channel);
+		GCSignal[][][] E_out1 = new GCLib<GCSignal>(gen1, d, w).combineDeepestAndTarget(Li, E_feZeroKeys, C_feZeroKeys,
+				E_labelZeroKeys, C_labelZeroKeys);
+
+		GCSignal[][][] D_out1 = con1.readObject();
+
+		int[] deepest = new int[d];
+		int[] j1 = new int[d];
+		int[] j2 = new int[d];
+		int[] et = new int[d];
+
+		int[] target = new int[d];
+		int[] f = new int[d];
+
+		int nTop = decodeOutput(E_out1[2][0], D_out1[2][0]).intValue();
+		int nBot = decodeOutput(E_out1[2][1], D_out1[2][1]).intValue();
+		int eTop = decodeOutput(E_out1[2][2], D_out1[2][2]).intValue();
+		int eBot = decodeOutput(E_out1[2][3], D_out1[2][3]).intValue();
+
+		for (int i = 0; i < d; i++) {
+			deepest[i] = decodeOutput(E_out[0][i], D_out[0][i]).intValue();
+			j1[i] = decodeOutput(E_out[1][i], D_out[1][i]).intValue();
+			j2[i] = decodeOutput(E_out[2][i], D_out[2][i]).intValue();
+			et[i] = decodeOutput(E_out[3][i], D_out[3][i]).intValue();
+
+			target[i] = decodeOutput(E_out1[0][i], D_out1[0][i]).intValue();
+			f[i] = decodeOutput(E_out1[1][i], D_out1[1][i]).intValue();
+
+			System.out.println("i=" + i);
+
+			System.out.print("full tuples: ");
+			BigInteger fe = new BigInteger(1, feBits[i]);
+			for (int j = 0; j < w; j++)
+				if (fe.testBit(w - 1 - j))
+					System.out.print(j + " ");
+			System.out.println();
+			System.out.print("empty tuples: ");
+			for (int j = 0; j < w; j++)
+				if (!fe.testBit(w - 1 - j))
+					System.out.print(j + " ");
+			System.out.println();
+
+			System.out.println("tuple labels xor path label:");
+			BigInteger pathLabel = new BigInteger(1, Li);
+			for (int j = 0; j < w; j++) {
+				BigInteger xor = Util.getSubBits(new BigInteger(1, tupleLabels[i][j]).xor(pathLabel), d - 1, 0);
+				System.out.println(j + ": " + addZeros(xor.toString(2), d - 1));
+			}
+
+			int perp = (int) (Math.pow(2, logD + 1)) - 1;
+
+			System.out.println();
+			System.out.println("deepest[i]: " + ((deepest[i] == perp) ? "\\perp" : "" + deepest[i]));
+			System.out.println("j1[i]: " + j1[i]);
+			System.out.println("j2[i]: " + j2[i]);
+			System.out.println("et[i]: " + et[i]);
+			System.out.println("target[i]: " + ((target[i] == perp) ? "\\perp" : "" + target[i]));
+			System.out.println("f[i]: " + f[i]);
+
+			System.out.println();
+		}
+		System.out.println("nTop: " + nTop);
+		System.out.println("nBot: " + nBot);
+		System.out.println("eTop: " + eTop);
+		System.out.println("eBot: " + eBot);
+	}
+
+	public void runD() {
+		byte[] Li = con1.read();
+		GCSignal[][] E_feKeyInput = con1.readObject();
+		GCSignal[][] C_feZeroKeys = con1.readObject();
+		GCSignal[][][] E_labelKeyInput = con1.readObject();
+		GCSignal[][][] C_labelZeroKeys = con1.readObject();
+
+		Network channel = new Network(con1, null);
+
+		CompEnv<GCSignal> gen = new GCEva(channel);
+		GCLib<GCSignal> dae = new GCLib<GCSignal>(gen, d, w);
+		dae.prepareDeepest(Li, E_feKeyInput, C_feZeroKeys, E_labelKeyInput, C_labelZeroKeys);
+
+		gen.setEvaluate();
+		GCSignal[][][] D_out = dae.prepareDeepest(Li, E_feKeyInput, C_feZeroKeys, E_labelKeyInput, C_labelZeroKeys);
+
+		con1.write(D_out);
+
+		CompEnv<GCSignal> gen1 = new GCEva(channel);
+		GCLib<GCSignal> dae1 = new GCLib<GCSignal>(gen1, d, w);
+		dae1.combineDeepestAndTarget(Li, E_feKeyInput, C_feZeroKeys, E_labelKeyInput, C_labelZeroKeys);
+
+		gen1.setEvaluate();
+		GCSignal[][][] D_out1 = dae1.combineDeepestAndTarget(Li, E_feKeyInput, C_feZeroKeys, E_labelKeyInput,
+				C_labelZeroKeys);
+
+		con1.write(D_out1);
+	}
+
+	public void runC() {
+
+	}
+
+	// for testing correctness
+	@Override
+	public void run(Party party, Metadata md, Forest forest) {
+
+		if (party == Party.Eddie) {
+			runE();
+
+		} else if (party == Party.Debbie) {
+			runD();
+
+		} else if (party == Party.Charlie) {
+			runC();
+
+		} else {
+			throw new NoSuchPartyException(party + "");
+		}
+	}
+}

+ 2 - 0
src/ui/CLI.java

@@ -83,6 +83,8 @@ public class CLI {
 			operation = DeepestAndEmpty.class;
 		} else if (protocol.equals("pd")) {
 			operation = PrepareDeepest.class;
+		} else if (protocol.equals("pt")) {
+			operation = PrepareTarget.class;
 		} else {
 			System.out.println("Protocol " + protocol + " not supported");
 			System.exit(-1);