Browse Source

continue remove PreData and Timer

Boyoung- 6 years ago
parent
commit
425ce246b8
2 changed files with 225 additions and 135 deletions
  1. 93 65
      src/protocols/PermuteIndex.java
  2. 132 70
      src/protocols/PermuteTarget.java

+ 93 - 65
src/protocols/PermuteIndex.java

@@ -7,116 +7,153 @@ import crypto.Crypto;
 import exceptions.NoSuchPartyException;
 import oram.Forest;
 import oram.Metadata;
-import protocols.precomputation.PrePermuteIndex;
 import protocols.struct.Party;
-import protocols.struct.PreData;
 import util.M;
-import util.P;
-import util.Timer;
 import util.Util;
 
 public class PermuteIndex extends Protocol {
 
-	private int pid = P.PI;
-
 	public PermuteIndex(Communication con1, Communication con2) {
 		super(con1, con2);
 	}
 
-	public void runE() {
+	public void runE(int w, int[] evict_pi) {
+		timer.start(M.offline_comp);
+
+		int d = evict_pi.length;
+		int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
+
+		byte[][] p = new byte[d][(logW + 7) / 8];
+		byte[][] r = new byte[d][(logW + 7) / 8];
+		byte[][] a = new byte[d][];
+		for (int i = 0; i < d; i++) {
+			Crypto.sr_DE.nextBytes(p[i]);
+			Crypto.sr_CE.nextBytes(r[i]);
+			a[i] = Util.xor(p[i], r[i]);
+		}
+		a = Util.permute(a, evict_pi);
+
+		timer.start(M.offline_write);
+		con1.write(offline_band, a);
+		timer.stop(M.offline_write);
+
+		timer.stop(M.offline_comp);
 	}
 
-	public int[] runD(PreData predata, boolean firstTree, byte[][] ti, int w, Timer timer) {
+	public int[] runD(boolean firstTree, byte[][] ti, int w) {
 		if (firstTree)
 			return null;
 
-		timer.start(pid, M.online_comp);
+		timer.start(M.offline_comp);
+
+		int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
+
+		timer.start(M.offline_read);
+		byte[][] a = con1.readDoubleByteArrayAndDec();
+		timer.stop(M.offline_read);
 
-		byte[][] z = Util.xor(ti, predata.pi_p);
+		int d = a.length;
 
-		timer.start(pid, M.online_write);
-		con2.write(pid, z);
-		timer.stop(pid, M.online_write);
+		byte[][] p = new byte[d][(logW + 7) / 8];
+		for (int i = 0; i < d; i++) {
+			Crypto.sr_DE.nextBytes(p[i]);
+		}
 
-		timer.start(pid, M.online_read);
-		byte[][] g = con2.readDoubleByteArray(pid);
-		timer.stop(pid, M.online_read);
+		timer.stop(M.offline_comp);
 
-		ti = Util.xor(predata.pi_a, g);
+		////////////////////////////////////////////////////////////
+
+		timer.start(M.online_comp);
+
+		byte[][] z = Util.xor(ti, p);
+
+		timer.start(M.online_write);
+		con2.write(online_band, z);
+		timer.stop(M.online_write);
+
+		timer.start(M.online_read);
+		byte[][] g = con2.readDoubleByteArrayAndDec();
+		timer.stop(M.online_read);
+
+		ti = Util.xor(a, g);
 
-		int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
 		int[] ti_pp = new int[ti.length];
 		for (int i = 0; i < ti.length; i++)
 			ti_pp[i] = Util.getSubBits(new BigInteger(ti[i]), logW, 0).intValue();
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return ti_pp;
 	}
 
-	public void runC(PreData predata, boolean firstTree, Timer timer) {
+	public void runC(boolean firstTree, int w, int[] evict_pi, byte[][] evict_rho) {
 		if (firstTree)
 			return;
 
-		timer.start(pid, M.online_comp);
+		timer.start(M.offline_comp);
 
-		timer.start(pid, M.online_read);
-		byte[][] z = con2.readDoubleByteArray(pid);
-		timer.stop(pid, M.online_read);
+		int d = evict_pi.length;
+		int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
 
-		z = Util.xor(z, predata.pi_r);
-		z = Util.permute(z, predata.evict_pi);
-		byte[][] g = Util.xor(predata.evict_rho, z);
+		byte[][] r = new byte[d][(logW + 7) / 8];
+		for (int i = 0; i < d; i++) {
+			Crypto.sr_CE.nextBytes(r[i]);
+		}
+
+		timer.stop(M.offline_comp);
+
+		////////////////////////////////////////////////////////////////
+
+		timer.start(M.online_comp);
+
+		timer.start(M.online_read);
+		byte[][] z = con2.readDoubleByteArrayAndDec();
+		timer.stop(M.online_read);
 
-		timer.start(pid, M.online_write);
-		con2.write(pid, g);
-		timer.stop(pid, M.online_write);
+		z = Util.xor(z, r);
+		z = Util.permute(z, evict_pi);
+		byte[][] g = Util.xor(evict_rho, z);
 
-		timer.stop(pid, M.online_comp);
+		timer.start(M.online_write);
+		con2.write(online_band, g);
+		timer.stop(M.online_write);
+
+		timer.stop(M.online_comp);
 	}
 
-	// for testing correctness
 	@Override
-	public void run(Party party, Metadata md, Forest forest) {
-		Timer timer = new Timer();
+	public void run(Party party, Metadata md, Forest[] forest) {
 
 		for (int i = 0; i < 100; i++) {
 
 			System.out.println("i=" + i);
 
-			PreData predata = new PreData();
-			PrePermuteIndex prepermuteindex = new PrePermuteIndex(con1, con2);
-
 			if (party == Party.Eddie) {
 				int d = Crypto.sr.nextInt(15) + 5;
 				int w = Crypto.sr.nextInt(15) + 5;
 				int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
 
 				byte[][] ti = new byte[d][];
-				predata.evict_pi = Util.randomPermutation(d, Crypto.sr);
-				predata.evict_rho = new byte[d][];
+				int[] evict_pi = Util.randomPermutation(d, Crypto.sr);
+				byte[][] evict_rho = new byte[d][];
 				for (int j = 0; j < d; j++) {
 					ti[j] = Util.nextBytes((logW + 7) / 8, Crypto.sr);
-					predata.evict_rho[j] = Util.nextBytes((logW + 7) / 8, Crypto.sr);
+					evict_rho[j] = Util.nextBytes((logW + 7) / 8, Crypto.sr);
 				}
 
-				con1.write(predata.evict_pi);
-				con1.write(predata.evict_rho);
 				con1.write(ti);
 				con1.write(w);
 
-				con2.write(predata.evict_pi);
-				con2.write(predata.evict_rho);
+				con2.write(w);
+				con2.write(evict_pi);
+				con2.write(evict_rho);
 
-				prepermuteindex.runE(predata, d, w, timer);
-
-				runE();
+				runE(w, evict_pi);
 
 				int[] ti_pp = con1.readIntArray();
-				ti = Util.permute(ti, predata.evict_pi);
+				ti = Util.permute(ti, evict_pi);
 				int j = 0;
 				for (; j < d; j++) {
-					int tmp = Util.getSubBits(new BigInteger(Util.xor(predata.evict_rho[j], ti[j])), logW, 0)
-							.intValue();
+					int tmp = Util.getSubBits(new BigInteger(Util.xor(evict_rho[j], ti[j])), logW, 0).intValue();
 					if (tmp != ti_pp[j]) {
 						System.err.println("PermuteIndex test failed");
 						break;
@@ -126,35 +163,26 @@ public class PermuteIndex extends Protocol {
 					System.out.println("PermuteIndex test passed");
 
 			} else if (party == Party.Debbie) {
-				predata.evict_pi = con1.readIntArray();
-				predata.evict_rho = con1.readDoubleByteArray();
 				byte[][] ti = con1.readDoubleByteArray();
 				int w = con1.readInt();
 
-				prepermuteindex.runD(predata, timer);
-
-				int[] ti_pp = runD(predata, false, ti, w, timer);
+				int[] ti_pp = runD(false, ti, w);
 				con1.write(ti_pp);
 
 			} else if (party == Party.Charlie) {
-				predata.evict_pi = con1.readIntArray();
-				predata.evict_rho = con1.readDoubleByteArray();
-
-				prepermuteindex.runC(predata, timer);
+				int w = con1.readInt();
+				int[] evict_pi = con1.readIntArray();
+				byte[][] evict_rho = con1.readDoubleByteArray();
 
-				runC(predata, false, timer);
+				runC(false, w, evict_pi, evict_rho);
 
 			} else {
 				throw new NoSuchPartyException(party + "");
 			}
 		}
-
-		// timer.print();
 	}
 
 	@Override
-	public void run(Party party, Metadata md, Forest[] forest) {
-		// TODO Auto-generated method stub
-
+	public void run(Party party, Metadata md, Forest forest) {
 	}
 }

+ 132 - 70
src/protocols/PermuteTarget.java

@@ -10,140 +10,212 @@ import exceptions.NoSuchPartyException;
 import gc.GCUtil;
 import oram.Forest;
 import oram.Metadata;
-import protocols.precomputation.PrePermuteTarget;
 import protocols.struct.Party;
-import protocols.struct.PreData;
 import util.M;
-import util.P;
-import util.Timer;
 import util.Util;
 
 public class PermuteTarget extends Protocol {
 
-	private int pid = P.PT;
-
 	public PermuteTarget(Communication con1, Communication con2) {
 		super(con1, con2);
 	}
 
-	public void runE() {
+	public void runE(int d, int[] evict_pi, GCSignal[][][] evict_targetOutKeyPairs) {
+		timer.start(M.offline_comp);
+
+		// PermuteTargetI
+		int logD = (int) Math.ceil(Math.log(d) / Math.log(2));
+
+		byte[][][] keyT = new byte[d][d][];
+		byte[][][] targetT = new byte[d][d][];
+		byte[][][] maskT = new byte[d][d][];
+
+		for (int i = 0; i < d; i++) {
+			for (int j = 0; j < d; j++) {
+				GCSignal[] keys = GCUtil.revSelectKeys(evict_targetOutKeyPairs[i], BigInteger.valueOf(j).toByteArray());
+				keyT[i][j] = GCUtil.hashAll(keys);
+
+				maskT[i][j] = Util.nextBytes((logD + 7) / 8, Crypto.sr);
+
+				targetT[i][j] = Util.xor(Util.padArray(BigInteger.valueOf(evict_pi[j]).toByteArray(), (logD + 7) / 8),
+						maskT[i][j]);
+			}
+
+			int[] randPerm = Util.randomPermutation(d, Crypto.sr);
+			keyT[i] = Util.permute(keyT[i], randPerm);
+			maskT[i] = Util.permute(maskT[i], randPerm);
+			targetT[i] = Util.permute(targetT[i], randPerm);
+		}
+
+		timer.start(M.offline_write);
+		con1.write(offline_band, keyT);
+		con1.write(offline_band, targetT);
+		con2.write(offline_band, maskT);
+		timer.stop(M.offline_write);
+
+		// PermuteTargetII
+		byte[][] p = new byte[d][(logD + 7) / 8];
+		byte[][] r = new byte[d][(logD + 7) / 8];
+		byte[][] a = new byte[d][];
+		for (int i = 0; i < d; i++) {
+			Crypto.sr_DE.nextBytes(p[i]);
+			Crypto.sr_CE.nextBytes(r[i]);
+			a[i] = Util.xor(p[i], r[i]);
+		}
+		a = Util.permute(a, evict_pi);
+
+		timer.start(M.offline_write);
+		con1.write(offline_band, a);
+		timer.stop(M.offline_write);
+
+		timer.stop(M.offline_comp);
 	}
 
-	public int[] runD(PreData predata, boolean firstTree, GCSignal[][] targetOutKeys, Timer timer) {
+	public int[] runD(boolean firstTree, GCSignal[][] targetOutKeys) {
 		if (firstTree)
 			return null;
 
-		timer.start(pid, M.online_comp);
+		timer.start(M.offline_comp);
 
-		// PermuteTargetI
 		int d = targetOutKeys.length;
 		int logD = (int) Math.ceil(Math.log(d) / Math.log(2));
+
+		timer.start(M.offline_read);
+		// PermuteTargetI
+		byte[][][] keyT = con1.readTripleByteArrayAndDec();
+		byte[][][] targetT = con1.readTripleByteArrayAndDec();
+
+		// PermuteTargetII
+		byte[][] a = con1.readDoubleByteArrayAndDec();
+		timer.stop(M.offline_read);
+
+		byte[][] p = new byte[d][(logD + 7) / 8];
+		for (int i = 0; i < d; i++) {
+			Crypto.sr_DE.nextBytes(p[i]);
+		}
+
+		timer.stop(M.offline_comp);
+
+		//////////////////////////////////////////////////////////////
+
+		timer.start(M.online_comp);
+
+		// PermuteTargetI
 		int I[] = new int[d];
 		byte[][] target = new byte[d][];
 
 		for (int i = 0; i < d; i++) {
 			byte[] hashKeys = GCUtil.hashAll(targetOutKeys[i]);
 			for (int j = 0; j < d; j++) {
-				if (Util.equal(hashKeys, predata.pt_keyT[i][j])) {
+				if (Util.equal(hashKeys, keyT[i][j])) {
 					I[i] = j;
-					target[i] = predata.pt_targetT[i][j];
+					target[i] = targetT[i][j];
 					break;
 				}
 			}
 		}
 
 		// PermuteTargetII
-		byte[][] z = Util.xor(target, predata.pt_p);
+		byte[][] z = Util.xor(target, p);
 
-		timer.start(pid, M.online_write);
-		con2.write(pid, z);
-		con2.write(pid, I);
-		timer.stop(pid, M.online_write);
+		timer.start(M.online_write);
+		con2.write(online_band, z);
+		con2.write(online_band, I);
+		timer.stop(M.online_write);
 
-		timer.start(pid, M.online_read);
-		byte[][] g = con2.readDoubleByteArray(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		byte[][] g = con2.readDoubleByteArrayAndDec();
+		timer.stop(M.online_read);
 
-		target = Util.xor(predata.pt_a, g);
+		target = Util.xor(a, g);
 
 		int[] target_pp = new int[d];
 		for (int i = 0; i < d; i++)
 			target_pp[i] = Util.getSubBits(new BigInteger(target[i]), logD, 0).intValue();
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return target_pp;
 	}
 
-	public void runC(PreData predata, boolean firstTree, Timer timer) {
+	public void runC(boolean firstTree, int d, int[] evict_pi) {
 		if (firstTree)
 			return;
 
-		timer.start(pid, M.online_comp);
+		timer.start(M.offline_comp);
+
+		int logD = (int) Math.ceil(Math.log(d) / Math.log(2));
+
+		timer.start(M.offline_read);
+		// PermuteTargetI
+		byte[][][] maskT = con1.readTripleByteArrayAndDec();
+		timer.stop(M.offline_read);
+
+		// PermuteTargetII
+		byte[][] r = new byte[d][(logD + 7) / 8];
+		for (int i = 0; i < d; i++) {
+			Crypto.sr_CE.nextBytes(r[i]);
+		}
+
+		timer.stop(M.offline_comp);
+
+		//////////////////////////////////////////////////////////////
+
+		timer.start(M.online_comp);
 
 		// PermuteTargetII
-		timer.start(pid, M.online_read);
-		byte[][] z = con2.readDoubleByteArray(pid);
-		int[] I = con2.readIntArray(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		byte[][] z = con2.readDoubleByteArrayAndDec();
+		int[] I = con2.readIntArrayAndDec();
+		timer.stop(M.online_read);
 
 		byte[][] mk = new byte[z.length][];
 		for (int i = 0; i < mk.length; i++) {
-			mk[i] = Util.xor(predata.pt_maskT[i][I[i]], z[i]);
-			mk[i] = Util.xor(predata.pt_r[i], mk[i]);
+			mk[i] = Util.xor(maskT[i][I[i]], z[i]);
+			mk[i] = Util.xor(r[i], mk[i]);
 		}
-		byte[][] g = Util.permute(mk, predata.evict_pi);
+		byte[][] g = Util.permute(mk, evict_pi);
 
-		timer.start(pid, M.online_write);
-		con2.write(pid, g);
-		timer.stop(pid, M.online_write);
+		timer.start(M.online_write);
+		con2.write(online_band, g);
+		timer.stop(M.online_write);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 	}
 
-	// for testing correctness
 	@Override
-	public void run(Party party, Metadata md, Forest forest) {
-		Timer timer = new Timer();
+	public void run(Party party, Metadata md, Forest[] forest) {
 
 		for (int i = 0; i < 100; i++) {
 
 			System.out.println("i=" + i);
 
-			PreData predata = new PreData();
-			PrePermuteTarget prepermutetarget = new PrePermuteTarget(con1, con2);
-
 			if (party == Party.Eddie) {
 				int d = Crypto.sr.nextInt(20) + 5;
 				int logD = (int) Math.ceil(Math.log(d) / Math.log(2));
 				int[] target = Util.randomPermutation(d, Crypto.sr);
 
-				predata.evict_pi = Util.randomPermutation(d, Crypto.sr);
-				predata.evict_targetOutKeyPairs = new GCSignal[d][][];
+				int[] evict_pi = Util.randomPermutation(d, Crypto.sr);
+				GCSignal[][][] evict_targetOutKeyPairs = new GCSignal[d][][];
 				GCSignal[][] targetOutKeys = new GCSignal[d][];
 				for (int j = 0; j < d; j++) {
-					predata.evict_targetOutKeyPairs[j] = GCUtil.genKeyPairs(logD);
-					targetOutKeys[j] = GCUtil.revSelectKeys(predata.evict_targetOutKeyPairs[j],
+					evict_targetOutKeyPairs[j] = GCUtil.genKeyPairs(logD);
+					targetOutKeys[j] = GCUtil.revSelectKeys(evict_targetOutKeyPairs[j],
 							BigInteger.valueOf(target[j]).toByteArray());
 				}
 
-				con1.write(d);
-				con1.write(predata.evict_pi);
-				con1.write(predata.evict_targetOutKeyPairs);
 				con1.write(targetOutKeys);
+				con2.write(d);
+				con2.write(evict_pi);
 
-				con2.write(predata.evict_pi);
-
-				prepermutetarget.runE(predata, d, timer);
-
-				runE();
+				runE(d, evict_pi, evict_targetOutKeyPairs);
 
 				int[] target_pp = con1.readIntArray();
-				int[] pi_ivs = Util.inversePermutation(predata.evict_pi);
+				int[] pi_ivs = Util.inversePermutation(evict_pi);
 				int[] piTargetPiIvs = new int[d];
 
 				int j = 0;
 				for (; j < d; j++) {
-					piTargetPiIvs[j] = predata.evict_pi[target[pi_ivs[j]]];
+					piTargetPiIvs[j] = evict_pi[target[pi_ivs[j]]];
 					if (piTargetPiIvs[j] != target_pp[j]) {
 						System.err.println("PermuteTarget test failed");
 						break;
@@ -153,34 +225,24 @@ public class PermuteTarget extends Protocol {
 					System.out.println("PermuteTarget test passed");
 
 			} else if (party == Party.Debbie) {
-				int d = con1.readInt();
-				predata.evict_pi = con1.readIntArray();
-				predata.evict_targetOutKeyPairs = con1.readTripleGCSignalArray();
 				GCSignal[][] targetOutKeys = con1.readDoubleGCSignalArray();
 
-				prepermutetarget.runD(predata, d, timer);
-
-				int[] target_pp = runD(predata, false, targetOutKeys, timer);
+				int[] target_pp = runD(false, targetOutKeys);
 				con1.write(target_pp);
 
 			} else if (party == Party.Charlie) {
-				predata.evict_pi = con1.readIntArray();
-
-				prepermutetarget.runC(predata, timer);
+				int d = con1.readInt();
+				int[] evict_pi = con1.readIntArray();
 
-				runC(predata, false, timer);
+				runC(false, d, evict_pi);
 
 			} else {
 				throw new NoSuchPartyException(party + "");
 			}
 		}
-
-		// timer.print();
 	}
 
 	@Override
-	public void run(Party party, Metadata md, Forest[] forest) {
-		// TODO Auto-generated method stub
-
+	public void run(Party party, Metadata md, Forest forest) {
 	}
 }