Browse Source

routing in eviction tested

Boyoung- 8 years ago
parent
commit
108dc4f41f

+ 18 - 0
src/oram/Bucket.java

@@ -1,6 +1,7 @@
 package oram;
 
 import java.io.Serializable;
+import java.util.Arrays;
 import java.util.Random;
 
 import exceptions.LengthNotMatchException;
@@ -44,6 +45,10 @@ public class Bucket implements Serializable {
 		return tuples.length;
 	}
 
+	public Tuple[] getTuples() {
+		return tuples;
+	}
+
 	public Tuple getTuple(int i) {
 		return tuples[i];
 	}
@@ -107,4 +112,17 @@ public class Bucket implements Serializable {
 
 		return tuples;
 	}
+
+	public static Bucket[] tuplesToBuckets(Tuple[] tuples, int d, int sw, int w) {
+		if (tuples.length != sw + (d - 1) * w)
+			throw new LengthNotMatchException(tuples.length + " != " + (sw + (d - 1) * w));
+
+		Bucket[] buckets = new Bucket[d];
+		for (int i = 0; i < d; i++) {
+			int start = i == 0 ? 0 : sw + (i - 1) * w;
+			int end = i == 0 ? sw : start + w;
+			buckets[i] = new Bucket(Arrays.copyOfRange(tuples, start, end));
+		}
+		return buckets;
+	}
 }

+ 173 - 0
src/protocols/Eviction.java

@@ -0,0 +1,173 @@
+package protocols;
+
+import java.math.BigInteger;
+
+import com.oblivm.backend.gc.GCSignal;
+
+import communication.Communication;
+import crypto.Crypto;
+import exceptions.NoSuchPartyException;
+import gc.GCUtil;
+import measure.Timer;
+import oram.Bucket;
+import oram.Forest;
+import oram.Metadata;
+import oram.Tuple;
+import util.Util;
+
+public class Eviction extends Protocol {
+	public Eviction(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	public Tuple[] runE(PreData predata, boolean firstTree, byte[] Li, Tuple[] pathTuples, int d, int w, Timer timer) {
+		if (firstTree)
+			return pathTuples;
+
+		Bucket[] pathBuckets = Bucket.tuplesToBuckets(pathTuples, d, w, w);
+
+		GCSignal[] LiInputKeys = GCUtil.revSelectKeys(predata.evict_LiKeyPairs, Li);
+		GCSignal[][] E_feInputKeys = new GCSignal[d][];
+		GCSignal[][][] E_labelInputKeys = new GCSignal[d][][];
+		GCSignal[][] deltaInputKeys = new GCSignal[d][];
+		for (int i = 0; i < d; i++) {
+			E_feInputKeys[i] = GCUtil.selectFeKeys(predata.evict_E_feKeyPairs[i], pathBuckets[i].getTuples());
+			E_labelInputKeys[i] = GCUtil.selectLabelKeys(predata.evict_E_labelKeyPairs[i], pathBuckets[i].getTuples());
+			deltaInputKeys[i] = GCUtil.revSelectKeys(predata.evict_deltaKeyPairs[i],
+					predata.evict_delta[i].toByteArray());
+		}
+
+		con1.write(LiInputKeys);
+		con1.write(E_feInputKeys);
+		con1.write(E_labelInputKeys);
+		con1.write(deltaInputKeys);
+
+		return pathTuples;
+	}
+
+	public void runD(PreData predata, boolean firstTree, byte[] Li, int w, Timer timer) {
+		if (firstTree)
+			return;
+
+		GCSignal[] LiInputKeys = con1.readObject();
+		GCSignal[][] E_feInputKeys = con1.readObject();
+		GCSignal[][][] E_labelInputKeys = con1.readObject();
+		GCSignal[][] deltaInputKeys = con1.readObject();
+
+		GCSignal[][] C_feInputKeys = con2.readObject();
+		GCSignal[][][] C_labelInputKeys = con2.readObject();
+
+		GCSignal[][][] outKeys = predata.evict_gc.routing(LiInputKeys, E_feInputKeys, C_feInputKeys, E_labelInputKeys,
+				C_labelInputKeys, deltaInputKeys);
+
+		int[] ti_p = new int[deltaInputKeys.length];
+		// int[] target = new int[deltaInputKeys.length];
+		for (int i = 0; i < ti_p.length; i++) {
+			ti_p[i] = GCUtil.evaOutKeys(outKeys[1][i], predata.evict_tiOutKeyHashes[i]).intValue();
+			// target[i] = GCUtil.evaOutKeys(outKeys[0][i],
+			// predata.tmpKeyHashes[i]).intValue();
+		}
+
+		System.out.println("ti:");
+		for (int i = 0; i < ti_p.length; i++)
+			System.out.print(ti_p[i] + " ");
+		System.out.println();
+		// System.out.println("target:");
+		// for (int i=0; i<ti_p.length; i++)
+		// System.out.print(target[i] + " ");
+		// System.out.println();
+	}
+
+	public Tuple[] runC(PreData predata, boolean firstTree, Tuple[] pathTuples, int d, int w, Timer timer) {
+		if (firstTree)
+			return pathTuples;
+
+		Bucket[] pathBuckets = Bucket.tuplesToBuckets(pathTuples, d, w, w);
+
+		GCSignal[][] C_feInputKeys = new GCSignal[d][];
+		GCSignal[][][] C_labelInputKeys = new GCSignal[d][][];
+		for (int i = 0; i < d; i++) {
+			C_feInputKeys[i] = GCUtil.selectFeKeys(predata.evict_C_feKeyPairs[i], pathBuckets[i].getTuples());
+			C_labelInputKeys[i] = GCUtil.selectLabelKeys(predata.evict_C_labelKeyPairs[i], pathBuckets[i].getTuples());
+		}
+
+		con2.write(C_feInputKeys);
+		con2.write(C_labelInputKeys);
+
+		return pathTuples;
+	}
+
+	// for testing correctness
+	@Override
+	public void run(Party party, Metadata md, Forest forest) {
+		Timer timer = new Timer();
+
+		for (int i = 0; i < 2; i++) {
+
+			System.out.println("i=" + i);
+
+			PreData predata = new PreData();
+			PreEviction preeviction = new PreEviction(con1, con2);
+
+			if (party == Party.Eddie) {
+				int w = Crypto.sr.nextInt(1) + 4;
+				int d = Crypto.sr.nextInt(1) + 5;
+				int lBits = d - 1;
+				int lBytes = (lBits + 7) / 8;
+				byte[] Li = Util.nextBytes(lBytes, Crypto.sr);
+				Tuple[] path = new Tuple[d * w];
+				for (int j = 0; j < d * w; j++)
+					path[j] = new Tuple(1, 2, lBytes, 3, Crypto.sr);
+
+				System.out.println("d, w: " + d + " " + w);
+
+				con1.write(d);
+				con1.write(w);
+				con1.write(Li);
+				con2.write(d);
+				con2.write(w);
+
+				preeviction.runE(predata, d, w, timer);
+				runE(predata, false, Li, path, d, w, timer);
+
+				int emptyIndex = 0;
+				for (int j = 0; j < d * w; j++) {
+					if (new BigInteger(path[j].getF()).testBit(0)) {
+						String l = Util.addZeros(
+								Util.getSubBits(new BigInteger(1, Util.xor(path[j].getL(), Li)), lBits, 0).toString(2),
+								lBits);
+						System.out.println(j + ":\t" + l);
+					} else {
+						emptyIndex = j;
+					}
+				}
+				System.out.println("last empty: " + emptyIndex);
+
+			} else if (party == Party.Debbie) {
+				int d = con1.readObject();
+				int w = con1.readObject();
+				byte[] Li = con1.read();
+				int[] tupleParam = new int[] { 1, 2, (d - 1 + 7) / 8, 3 };
+
+				preeviction.runD(predata, d, w, tupleParam, timer);
+				runD(predata, false, Li, w, timer);
+
+			} else if (party == Party.Charlie) {
+				int d = con1.readObject();
+				int w = con1.readObject();
+				int lBytes = (d - 1 + 7) / 8;
+				Tuple[] path = new Tuple[d * w];
+				for (int j = 0; j < d * w; j++)
+					path[j] = new Tuple(1, 2, lBytes, 3, null);
+
+				preeviction.runC(predata, timer);
+				runC(predata, false, path, d, w, timer);
+
+			} else {
+				throw new NoSuchPartyException(party + "");
+			}
+		}
+
+		// timer.print();
+	}
+}

+ 10 - 0
src/protocols/PreData.java

@@ -57,4 +57,14 @@ public class PreData {
 	public GCSignal[][][] evict_C_feKeyPairs;
 	public GCSignal[][][][] evict_E_labelKeyPairs;
 	public GCSignal[][][][] evict_C_labelKeyPairs;
+	public GCSignal[][][] evict_deltaKeyPairs;
+	public BigInteger[][] evict_tiOutKeyHashes;
+	public GCLib<GCSignal> evict_gc;
+	public int[] evict_pi;
+	public BigInteger[] evict_delta;
+	public BigInteger[] evict_rho;
+	public int[][] evict_delta_p;
+	public int[][] evict_rho_p;
+
+	// public BigInteger[][] tmpKeyHashes;
 }

+ 159 - 0
src/protocols/PreEviction.java

@@ -0,0 +1,159 @@
+package protocols;
+
+import java.math.BigInteger;
+
+import com.oblivm.backend.flexsc.CompEnv;
+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 gc.GCLib;
+import gc.GCUtil;
+import measure.Timer;
+import oram.Forest;
+import oram.Metadata;
+import util.Util;
+
+public class PreEviction extends Protocol {
+	public PreEviction(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	public void runE(PreData predata, int d, int w, Timer timer) {
+		// GC
+		int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
+
+		predata.evict_LiKeyPairs = GCUtil.genKeyPairs(d - 1);
+		GCSignal[] LiZeroKeys = GCUtil.getZeroKeys(predata.evict_LiKeyPairs);
+
+		predata.evict_E_feKeyPairs = new GCSignal[d][][];
+		predata.evict_C_feKeyPairs = new GCSignal[d][][];
+		GCSignal[][] E_feZeroKeys = new GCSignal[d][];
+		GCSignal[][] C_feZeroKeys = new GCSignal[d][];
+
+		predata.evict_E_labelKeyPairs = new GCSignal[d][w][][];
+		predata.evict_C_labelKeyPairs = new GCSignal[d][w][][];
+		GCSignal[][][] E_labelZeroKeys = new GCSignal[d][w][];
+		GCSignal[][][] C_labelZeroKeys = new GCSignal[d][w][];
+
+		predata.evict_deltaKeyPairs = new GCSignal[d][][];
+		GCSignal[][] deltaZeroKeys = new GCSignal[d][];
+
+		for (int i = 0; i < d; i++) {
+			predata.evict_E_feKeyPairs[i] = GCUtil.genKeyPairs(w);
+			predata.evict_C_feKeyPairs[i] = GCUtil.genKeyPairs(w);
+			E_feZeroKeys[i] = GCUtil.getZeroKeys(predata.evict_E_feKeyPairs[i]);
+			C_feZeroKeys[i] = GCUtil.getZeroKeys(predata.evict_C_feKeyPairs[i]);
+
+			predata.evict_deltaKeyPairs[i] = GCUtil.genKeyPairs(logW);
+			deltaZeroKeys[i] = GCUtil.getZeroKeys(predata.evict_deltaKeyPairs[i]);
+
+			for (int j = 0; j < w; j++) {
+				predata.evict_E_labelKeyPairs[i][j] = GCUtil.genKeyPairs(d - 1);
+				predata.evict_C_labelKeyPairs[i][j] = GCUtil.genKeyPairs(d - 1);
+				E_labelZeroKeys[i][j] = GCUtil.getZeroKeys(predata.evict_E_labelKeyPairs[i][j]);
+				C_labelZeroKeys[i][j] = GCUtil.getZeroKeys(predata.evict_C_labelKeyPairs[i][j]);
+			}
+		}
+
+		Network channel = new Network(null, con1);
+		CompEnv<GCSignal> gen = new GCGen(channel);
+		GCSignal[][][] outZeroKeys = new GCLib<GCSignal>(gen, d, w).routing(LiZeroKeys, E_feZeroKeys, C_feZeroKeys,
+				E_labelZeroKeys, C_labelZeroKeys, deltaZeroKeys);
+
+		predata.evict_tiOutKeyHashes = new BigInteger[d][];
+		// predata.tmpKeyHashes = new BigInteger[d][];
+		for (int i = 0; i < d; i++) {
+			predata.evict_tiOutKeyHashes[i] = GCUtil.genOutKeyHashes(outZeroKeys[1][i]);
+			// predata.tmpKeyHashes[i] =
+			// GCUtil.genOutKeyHashes(outZeroKeys[0][i]);
+		}
+
+		con2.write(predata.evict_C_feKeyPairs);
+		con2.write(predata.evict_C_labelKeyPairs);
+		con1.write(predata.evict_tiOutKeyHashes);
+		// con1.write(predata.tmpKeyHashes);
+
+		// Permutation
+		predata.evict_pi = Util.randomPermutation(d, Crypto.sr);
+		predata.evict_delta = new BigInteger[d];
+		predata.evict_rho = new BigInteger[d];
+		predata.evict_delta_p = new int[d][];
+		predata.evict_rho_p = new int[d][];
+
+		for (int i = 0; i < d; i++) {
+			predata.evict_delta[i] = new BigInteger(logW, Crypto.sr);
+			predata.evict_rho[i] = new BigInteger(logW, Crypto.sr);
+			predata.evict_delta_p[i] = Util.getXorPermutation(predata.evict_delta[i], logW);
+			predata.evict_rho_p[i] = Util.getXorPermutation(predata.evict_rho[i], logW);
+		}
+
+		con2.write(predata.evict_pi);
+		con2.write(predata.evict_delta);
+		con2.write(predata.evict_rho);
+		con2.write(predata.evict_delta_p);
+		con2.write(predata.evict_rho_p);
+
+		// PreSSXOT pressxot = new PreSSXOT(con1, con2, 0);
+		// pressxot.runE(predata, timer);
+	}
+
+	public void runD(PreData predata, int d, int w, int[] tupleParam, Timer timer) {
+		// GC
+		int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
+
+		GCSignal[] LiZeroKeys = GCUtil.genEmptyKeys(d - 1);
+		GCSignal[][] E_feZeroKeys = new GCSignal[d][];
+		GCSignal[][] C_feZeroKeys = new GCSignal[d][];
+		GCSignal[][][] E_labelZeroKeys = new GCSignal[d][w][];
+		GCSignal[][][] C_labelZeroKeys = new GCSignal[d][w][];
+		GCSignal[][] deltaZeroKeys = new GCSignal[d][];
+
+		for (int i = 0; i < d; i++) {
+			E_feZeroKeys[i] = GCUtil.genEmptyKeys(w);
+			C_feZeroKeys[i] = GCUtil.genEmptyKeys(w);
+			deltaZeroKeys[i] = GCUtil.genEmptyKeys(logW);
+
+			for (int j = 0; j < w; j++) {
+				E_labelZeroKeys[i][j] = GCUtil.genEmptyKeys(d - 1);
+				C_labelZeroKeys[i][j] = GCUtil.genEmptyKeys(d - 1);
+			}
+		}
+
+		Network channel = new Network(con1, null);
+		CompEnv<GCSignal> eva = new GCEva(channel);
+		predata.evict_gc = new GCLib<GCSignal>(eva, d, w);
+		predata.evict_gc.routing(LiZeroKeys, E_feZeroKeys, C_feZeroKeys, E_labelZeroKeys, C_labelZeroKeys,
+				deltaZeroKeys);
+		eva.setEvaluate();
+
+		predata.evict_tiOutKeyHashes = con1.readObject();
+		// predata.tmpKeyHashes = con1.readObject();
+
+		// PreSSXOT pressxot = new PreSSXOT(con1, con2, 0);
+		// pressxot.runD(predata, sw + 1, sw, tupleParam, timer);
+	}
+
+	public void runC(PreData predata, Timer timer) {
+		// GC
+		predata.evict_C_feKeyPairs = con1.readObject();
+		predata.evict_C_labelKeyPairs = con1.readObject();
+
+		// Permutation
+		predata.evict_pi = con1.readObject();
+		predata.evict_delta = con1.readObject();
+		predata.evict_rho = con1.readObject();
+		predata.evict_delta_p = con1.readObject();
+		predata.evict_rho_p = con1.readObject();
+
+		// PreSSXOT pressxot = new PreSSXOT(con1, con2, 0);
+		// pressxot.runC(predata, timer);
+	}
+
+	@Override
+	public void run(Party party, Metadata md, Forest forest) {
+	}
+}

+ 2 - 0
src/ui/CLI.java

@@ -89,6 +89,8 @@ public class CLI {
 			operation = MakeCycle.class;
 		} else if (protocol.equals("update")) {
 			operation = UpdateRoot.class;
+		} else if (protocol.equals("evict")) {
+			operation = Eviction.class;
 		} else if (protocol.equals("retrieve")) {
 			operation = Retrieve.class;
 		} else {

+ 9 - 0
src/util/Util.java

@@ -138,6 +138,15 @@ public class Util {
 		}
 	}
 
+	public static int[] getXorPermutation(BigInteger b, int bits) {
+		int len = (int) Math.pow(2, bits);
+		int[] p = new int[len];
+		for (int i = 0; i < len; i++) {
+			p[i] = BigInteger.valueOf(i).xor(b).intValue();
+		}
+		return p;
+	}
+
 	public static String addZeros(String a, int n) {
 		String out = a;
 		for (int i = 0; i < n - a.length(); i++)

+ 6 - 0
test/misc/MiscTests.java

@@ -67,6 +67,12 @@ public class MiscTests {
 		for (int i = 0; i < n; i++) {
 			System.out.println(oldArr[i] + " " + newArr[i]);
 		}
+
+		BigInteger b = new BigInteger("101", 2);
+		int[] p = Util.getXorPermutation(b, 3);
+		for (int i = 0; i < p.length; i++)
+			System.out.print(p[i] + " ");
+		System.out.println();
 	}
 
 }