Browse Source

still editing alg1's code

Boyoung- 9 years ago
parent
commit
e7f5ff543e

+ 15 - 8
ObliVMGC/com/oblivm/backend/circuits/arithmetic/IntegerLib.java

@@ -57,6 +57,10 @@ public class IntegerLib<T> extends CircuitLib<T> implements ArithmeticLib<T> {
 		return res;
 	}
 
+	public T[] addFull(T[] x, T[] y) {
+		return addFull(x, y, false);
+	}
+
 	public T[] add(T[] x, T[] y, boolean cin) {
 		return Arrays.copyOf(addFull(x, y, cin), x.length);
 	}
@@ -106,23 +110,26 @@ public class IntegerLib<T> extends CircuitLib<T> implements ArithmeticLib<T> {
 		return sub(x, one);
 	}
 
-	public T geq(T[] x, T[] y) {
+	public T less(T[] x, T[] y) {
 		assert (x.length == y.length) : "bad input";
 
 		T[] result = sub(x, y);
-		return not(result[result.length - 1]);
+		return result[result.length - 1];
 	}
 
-	public T leq(T[] x, T[] y) {
-		return geq(y, x);
+	public T greater(T[] x, T[] y) {
+		return less(y, x);
 	}
 
-	public T greater(T[] x, T[] y) {
-		return not(leq(x, y));
+	public T geq(T[] x, T[] y) {
+		assert (x.length == y.length) : "bad input";
+
+		T[] result = sub(x, y);
+		return not(result[result.length - 1]);
 	}
 
-	public T less(T[] x, T[] y) {
-		return not(geq(x, y));
+	public T leq(T[] x, T[] y) {
+		return geq(y, x);
 	}
 
 	public T[] multiply(T[] x, T[] y) {

+ 21 - 8
src/gc/GarbledCircuitLib.java

@@ -1,5 +1,8 @@
 package gc;
 
+import java.math.BigInteger;
+import java.util.Arrays;
+
 import com.oblivm.backend.circuits.arithmetic.IntegerLib;
 import com.oblivm.backend.flexsc.CompEnv;
 
@@ -27,27 +30,37 @@ public class GarbledCircuitLib<T> extends IntegerLib<T> {
 		}
 	}
 
-	public T[][] deepestAndEmptyTuples(long i, T[] pathLabel, T[] feBits, T[][] tupleLabels) {
-		T[] l = toSignals(1L << (d - 1 - i) - 1L, d - 1);
-		T[] j1 = toSignals(Util.nextLong(w, Crypto.sr), logW);
+	public T[][] deepestAndEmptyTuples(int i, byte[] Li, T[] E_feBits, T[] C_feBits, T[][] E_tupleLabels,
+			T[][] C_tupleLabels) {
+		T[] pathLabel = toSignals(new BigInteger(1, Li).longValue(), d - 1); // no
+																				// sign
+																				// bit
+		T[] feBits = xor(E_feBits, C_feBits);
+		T[][] tupleLabels = env.newTArray(E_tupleLabels.length, 0);
+		for (int j = 0; j < tupleLabels.length; j++)
+			tupleLabels[j] = xor(E_tupleLabels[j], C_tupleLabels[j]);
+
+		T[] l = padSignal(ones(d - 1 - i), d); // has sign bit 0
+		T[] j1 = toSignals(Util.nextLong(w, Crypto.sr), logW); // no sign bit
 		T[] j2 = toSignals(Util.nextLong(w, Crypto.sr), logW);
-		T[] et = zeros(1);
+		T[] et = zeros(1); // no sign bit
 		for (int j = 0; j < w; j++) {
 			T[] tupleIndex = toSignals(j, logW);
-			T[] lz = xor(pathLabel, tupleLabels[j]);
+			T[] lz = xor(pathLabel, tupleLabels[j]); // no sign bit
 			zerosFollowedByOnes(lz);
+			lz = padSignal(lz, d); // has 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]);
 		}
-		T[] l_p = numberOfOnes(not(l)); // TODO: set length to logD?
+		T[] l_p = numberOfOnes(not(Arrays.copyOf(l, d - 1))); // has sign bit
 
 		T[][] output = env.newTArray(4, 0);
 		output[0] = l_p;
-		output[1] = j1;
-		output[2] = j2;
+		output[1] = padSignal(j1, logW + 1);
+		output[2] = padSignal(j2, logW + 1);
 		output[3] = et;
 		return output;
 	}

+ 0 - 5
src/protocols/GarbledCircuit.java

@@ -1,7 +1,5 @@
 package protocols;
 
-import java.math.BigInteger;
-
 import com.oblivm.backend.circuits.arithmetic.IntegerLib;
 import com.oblivm.backend.flexsc.CompEnv;
 import com.oblivm.backend.gc.GCGenComp;
@@ -12,12 +10,9 @@ import com.oblivm.backend.network.Network;
 
 import communication.Communication;
 import crypto.Crypto;
-import exceptions.AccessException;
 import exceptions.NoSuchPartyException;
 import oram.Forest;
 import oram.Metadata;
-import oram.Tree;
-import util.Util;
 
 public class GarbledCircuit extends Protocol {
 

+ 133 - 0
src/protocols/GarbledCircuitTest.java

@@ -0,0 +1,133 @@
+package protocols;
+
+import com.oblivm.backend.circuits.arithmetic.IntegerLib;
+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 oram.Forest;
+import oram.Metadata;
+
+public class GarbledCircuitTest extends Protocol {
+
+	private int totalLength = 2;
+
+	public GarbledCircuitTest(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	private int countOnes(boolean[] in) {
+		int cnt = 0;
+		for (int i = 0; i < in.length; i++)
+			if (in[i])
+				cnt++;
+		return cnt;
+	}
+
+	private int booleansToInt(boolean[] arr) {
+		int n = 0;
+		for (int i = arr.length - 1; i >= 0; i--)
+			n = (n << 1) | (arr[i] ? 1 : 0);
+		return n;
+	}
+
+	public void runE() {
+		Network w = new Network(null, con1);
+		CompEnv<GCSignal> gen = new GCGen(w);
+
+		boolean[] input1 = new boolean[totalLength];
+		boolean[] input2 = new boolean[totalLength];
+		boolean[] input = new boolean[totalLength];
+		for (int i = 0; i < input1.length; ++i) {
+			input1[i] = false;
+			input2[i] = true;
+			input[i] = input1[i] ^ input2[i];
+		}
+
+		GCSignal[][] inputKeyPairs1 = new GCSignal[input1.length][];
+		GCSignal[] localInputKeys1 = new GCSignal[input1.length];
+		GCSignal[][] inputKeyPairs2 = new GCSignal[input1.length][];
+		GCSignal[] localInputKeys2 = new GCSignal[input1.length];
+		GCSignal[] inputE = new GCSignal[input1.length];
+		GCSignal[] inputD = new GCSignal[input1.length];
+		for (int i = 0; i < input1.length; i++) {
+			inputKeyPairs1[i] = GCGenComp.genPair();
+			localInputKeys1[i] = inputKeyPairs1[i][0];
+			inputKeyPairs2[i] = GCGenComp.genPair();
+			localInputKeys2[i] = inputKeyPairs2[i][0];
+
+			inputE[i] = input1[i] ? inputKeyPairs1[i][1] : inputKeyPairs1[i][0];
+			inputD[i] = input2[i] ? inputKeyPairs2[i][1] : inputKeyPairs2[i][0];
+		}
+
+		GCSignal[] outputE = new GCSignal[] { new IntegerLib<GCSignal>(gen).less(localInputKeys1, localInputKeys2) };
+
+		con1.write(inputE);
+		con1.write(inputD);
+
+		GCSignal[] outputD = con1.readObject();
+
+		boolean[] output = new boolean[totalLength];
+		for (int i = 0; i < outputE.length; i++) {
+			if (outputE[i].isPublic())
+				output[i] = outputE[i].v;
+			else if (outputE[i].equals(outputD[i]))
+				output[i] = false;
+			else if (outputD[i].equals(GCGenComp.R.xor(outputE[i])))
+				output[i] = true;
+			else
+				System.err.println("ERROR on GC output!");
+		}
+
+		// int inCnt = countOnes(input);
+		// int outCnt = booleansToInt(output);
+		// System.out.println((inCnt == outCnt) + " " + inCnt + " " + outCnt);
+		System.out.println(output[0]);
+	}
+
+	public void runD() {
+		Network w = new Network(con1, null);
+		CompEnv<GCSignal> gen = new GCEva(w);
+
+		GCSignal[] randomInput = new GCSignal[totalLength];
+		for (int i = 0; i < randomInput.length; i++)
+			randomInput[i] = GCSignal.freshLabel(Crypto.sr);
+		IntegerLib<GCSignal> il = new IntegerLib<GCSignal>(gen);
+		il.less(randomInput, randomInput);
+
+		GCSignal[] inputE = con1.readObject();
+		GCSignal[] inputD = con1.readObject();
+
+		gen.setEvaluate();
+		GCSignal[] outputD = new GCSignal[] { il.less(inputE, inputD) };
+
+		con1.write(outputD);
+	}
+
+	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 + "");
+		}
+	}
+}

+ 3 - 0
src/ui/CLI.java

@@ -21,6 +21,7 @@ import protocols.PostProcessT;
 import protocols.SSXOT;
 import protocols.Access;
 import protocols.GarbledCircuit;
+import protocols.GarbledCircuitTest;
 
 public class CLI {
 	public static final int DEFAULT_PORT = 8000;
@@ -85,6 +86,8 @@ public class CLI {
 			operation = Access.class;
 		} else if (protocol.equals("gc")) {
 			operation = GarbledCircuit.class;
+		} else if (protocol.equals("gctest")) {
+			operation = GarbledCircuitTest.class;
 		} else {
 			System.out.println("Protocol " + protocol + " not supported");
 			System.exit(-1);