Browse Source

Added online timing for ACC, COT, IOT

Boyoung- 9 years ago
parent
commit
938a12c1bf

+ 16 - 0
src/exceptions/TimingException.java

@@ -0,0 +1,16 @@
+package exceptions;
+
+public class TimingException extends RuntimeException {
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+
+	public TimingException() {
+		super();
+	}
+
+	public TimingException(String message) {
+		super(message);
+	}
+}

+ 15 - 0
src/measure/M.java

@@ -0,0 +1,15 @@
+package measure;
+
+public class M {
+	public static final int size = 6;
+
+	public static final int online_comp = 0;
+	public static final int online_write = 1;
+	public static final int online_read = 2;
+	public static final int offline_comp = 3;
+	public static final int offline_write = 4;
+	public static final int offline_read = 5;
+
+	public static final String[] names = { "online_comp", "online_write", "online_read", "offline_comp",
+			"offline_write", "offline_read" };
+}

+ 11 - 0
src/measure/P.java

@@ -0,0 +1,11 @@
+package measure;
+
+public class P {
+	public static final int size = 3;
+
+	public static final int ACC = 0;
+	public static final int COT = 1;
+	public static final int IOT = 2;
+
+	public static final String[] names = { "ACC", "COT", "IOT" };
+}

+ 44 - 0
src/measure/Timing.java

@@ -0,0 +1,44 @@
+package measure;
+
+import java.util.Stack;
+
+import exceptions.TimingException;
+import util.StopWatch;
+
+public class Timing {
+	StopWatch[][] watches;
+	Stack<StopWatch> stack;
+
+	public Timing() {
+		watches = new StopWatch[P.size][M.size];
+		for (int i = 0; i < P.size; i++)
+			for (int j = 0; j < M.size; j++)
+				watches[i][j] = new StopWatch(P.names[i] + "_" + M.names[j]);
+		stack = new Stack<StopWatch>();
+	}
+
+	public void start(int p, int m) {
+		if (!stack.empty()) {
+			if (stack.peek() == watches[p][m])
+				throw new TimingException("Stopwatch already added to stack");
+			stack.peek().stop();
+		}
+		stack.push(watches[p][m]).start();
+	}
+
+	public void stop(int p, int m) {
+		if (stack.empty())
+			throw new TimingException("No stopwatch found");
+		stack.pop().stop();
+		if (!stack.empty())
+			stack.peek().start();
+	}
+
+	public void print() {
+		if (!stack.empty())
+			throw new TimingException("Stack not empty");
+		for (int i = 0; i < watches.length; i++)
+			for (int j = 0; j < watches[i].length; j++)
+				System.out.println(watches[i][j].toMS());
+	}
+}

+ 39 - 72
src/protocols/Access.java

@@ -9,43 +9,32 @@ import communication.Communication;
 import crypto.Crypto;
 import exceptions.AccessException;
 import exceptions.NoSuchPartyException;
+import measure.M;
+import measure.P;
+import measure.Timing;
 import oram.Bucket;
 import oram.Forest;
 import oram.Metadata;
 import oram.Tree;
 import oram.Tuple;
-import util.StopWatch;
 import util.Util;
 
 public class Access extends Protocol {
 
-	private StopWatch step0;
-	private StopWatch step1;
-	private StopWatch step2;
-	private StopWatch step3;
-	private StopWatch step4;
-	private StopWatch step5;
-
 	public Access(Communication con1, Communication con2) {
 		super(con1, con2);
-
-		step0 = new StopWatch();
-		step1 = new StopWatch();
-		step2 = new StopWatch();
-		step3 = new StopWatch();
-		step4 = new StopWatch();
-		step5 = new StopWatch();
 	}
 
-	public OutAccess runE(PreData predata, Tree OTi, byte[] Ni, byte[] Nip1_pr) {
-		step0.start();
+	public OutAccess runE(PreData predata, Tree OTi, byte[] Ni, byte[] Nip1_pr, Timing time) {
+		time.start(P.ACC, M.online_comp);
+
 		// step 0: get Li from C
+		time.start(P.ACC, M.online_read);
 		byte[] Li = new byte[0];
 		if (OTi.getTreeIndex() > 0)
 			Li = con2.read();
-		step0.stop();
+		time.stop(P.ACC, M.online_read);
 
-		step1.start();
 		// step 1
 		Bucket[] pathBuckets = OTi.getBucketsOnPath(new BigInteger(1, Li).longValue());
 		Tuple[] pathTuples = Bucket.bucketsToTuples(pathBuckets);
@@ -53,9 +42,7 @@ public class Access extends Protocol {
 			pathTuples[i].setXor(predata.access_p[i]);
 		Object[] objArray = Util.permute(pathTuples, predata.access_sigma);
 		pathTuples = Arrays.copyOf(objArray, objArray.length, Tuple[].class);
-		step1.stop();
 
-		step3.start();
 		// step 3
 		byte[] y = null;
 		if (OTi.getTreeIndex() == 0)
@@ -76,11 +63,9 @@ public class Access extends Protocol {
 			}
 
 			SSCOT sscot = new SSCOT(con1, con2);
-			sscot.runE(predata, m, a);
+			sscot.runE(predata, m, a, time);
 		}
-		step3.stop();
 
-		step4.start();
 		// step 4
 		if (OTi.getTreeIndex() < OTi.getH() - 1) {
 			int ySegBytes = y.length / OTi.getTwoTauPow();
@@ -89,32 +74,32 @@ public class Access extends Protocol {
 				y_array[i] = Arrays.copyOfRange(y, i * ySegBytes, (i + 1) * ySegBytes);
 
 			SSIOT ssiot = new SSIOT(con1, con2);
-			ssiot.runE(predata, y_array, Nip1_pr);
+			ssiot.runE(predata, y_array, Nip1_pr, time);
 		}
-		step4.stop();
 
-		step5.start();
 		// step 5
 		Tuple Ti = null;
 		if (OTi.getTreeIndex() == 0)
 			Ti = pathTuples[0];
 		else
 			Ti = new Tuple(new byte[0], Ni, Li, y);
-		step5.stop();
 
 		OutAccess outaccess = new OutAccess(null, null, null, Ti, pathTuples);
+
+		time.stop(P.ACC, M.online_comp);
 		return outaccess;
 	}
 
-	public void runD(PreData predata, Tree OTi, byte[] Ni, byte[] Nip1_pr) {
-		step0.start();
+	public void runD(PreData predata, Tree OTi, byte[] Ni, byte[] Nip1_pr, Timing time) {
+		time.start(P.ACC, M.online_comp);
+
 		// step 0: get Li from C
+		time.start(P.ACC, M.online_read);
 		byte[] Li = new byte[0];
 		if (OTi.getTreeIndex() > 0)
 			Li = con2.read();
-		step0.stop();
+		time.stop(P.ACC, M.online_read);
 
-		step1.start();
 		// step 1
 		Bucket[] pathBuckets = OTi.getBucketsOnPath(new BigInteger(1, Li).longValue());
 		Tuple[] pathTuples = Bucket.bucketsToTuples(pathBuckets);
@@ -122,15 +107,13 @@ public class Access extends Protocol {
 			pathTuples[i].setXor(predata.access_p[i]);
 		Object[] objArray = Util.permute(pathTuples, predata.access_sigma);
 		pathTuples = Arrays.copyOf(objArray, objArray.length, Tuple[].class);
-		step1.stop();
 
-		step2.start();
 		// step 2
+		time.start(P.ACC, M.online_write);
 		con2.write(pathTuples);
 		con2.write(Ni);
-		step2.stop();
+		time.stop(P.ACC, M.online_write);
 
-		step3.start();
 		// step 3
 		if (OTi.getTreeIndex() > 0) {
 			byte[][] b = new byte[pathTuples.length][];
@@ -142,35 +125,35 @@ public class Access extends Protocol {
 			}
 
 			SSCOT sscot = new SSCOT(con1, con2);
-			sscot.runD(predata, b);
+			sscot.runD(predata, b, time);
 		}
-		step3.stop();
 
-		step4.start();
 		// step 4
 		if (OTi.getTreeIndex() < OTi.getH() - 1) {
 			SSIOT ssiot = new SSIOT(con1, con2);
-			ssiot.runD(predata, Nip1_pr);
+			ssiot.runD(predata, Nip1_pr, time);
 		}
-		step4.stop();
+
+		time.stop(P.ACC, M.online_comp);
 	}
 
-	public OutAccess runC(Metadata md, int treeIndex, byte[] Li) {
-		step0.start();
+	public OutAccess runC(Metadata md, int treeIndex, byte[] Li, Timing time) {
+		time.start(P.ACC, M.online_comp);
+
 		// step 0: send Li to E and D
+		time.start(P.ACC, M.online_write);
 		if (treeIndex > 0) {
 			con1.write(Li);
 			con2.write(Li);
 		}
-		step0.stop();
+		time.stop(P.ACC, M.online_write);
 
-		step2.start();
 		// step 2
+		time.start(P.ACC, M.online_read);
 		Tuple[] pathTuples = con2.readObject();
 		byte[] Ni = con2.read();
-		step2.stop();
+		time.stop(P.ACC, M.online_read);
 
-		step3.start();
 		// step 3
 		int j1 = 0;
 		byte[] z = null;
@@ -178,20 +161,18 @@ public class Access extends Protocol {
 			z = pathTuples[0].getA();
 		} else {
 			SSCOT sscot = new SSCOT(con1, con2);
-			OutSSCOT je = sscot.runC();
+			OutSSCOT je = sscot.runC(time);
 			j1 = je.t;
 			byte[] d = pathTuples[j1].getA();
 			z = Util.xor(je.m_t, d);
 		}
-		step3.stop();
 
-		step4.start();
 		// step 4
 		int j2 = 0;
 		byte[] Lip1 = null;
 		if (treeIndex < md.getNumTrees() - 1) {
 			SSIOT ssiot = new SSIOT(con1, con2);
-			OutSSIOT jy = ssiot.runC();
+			OutSSIOT jy = ssiot.runC(time);
 
 			// step 5
 			j2 = jy.t;
@@ -199,9 +180,7 @@ public class Access extends Protocol {
 			byte[] z_j2 = Arrays.copyOfRange(z, j2 * lSegBytes, (j2 + 1) * lSegBytes);
 			Lip1 = Util.xor(jy.m_t, z_j2);
 		}
-		step4.stop();
 
-		step5.start();
 		Tuple Ti = null;
 		if (treeIndex == 0) {
 			Ti = pathTuples[0];
@@ -213,9 +192,10 @@ public class Access extends Protocol {
 			Crypto.sr.nextBytes(pathTuples[j1].getL());
 			Crypto.sr.nextBytes(pathTuples[j1].getA());
 		}
-		step5.stop();
 
 		OutAccess outaccess = new OutAccess(Lip1, Ti, pathTuples, null, null);
+
+		time.stop(P.ACC, M.online_comp);
 		return outaccess;
 	}
 
@@ -229,7 +209,7 @@ public class Access extends Protocol {
 		long numInsert = md.getNumInsertRecords();
 		int addrBits = md.getAddrBits();
 
-		StopWatch stopwatch = new StopWatch();
+		Timing time = new Timing();
 
 		sanityCheck();
 
@@ -267,9 +247,7 @@ public class Access extends Protocol {
 						byte[] sD_Nip1_pr = Util.xor(Nip1_pr, sE_Nip1_pr);
 						con1.write(sD_Nip1_pr);
 
-						stopwatch.start();
-						runE(predata, OTi, sE_Ni, sE_Nip1_pr);
-						stopwatch.stop();
+						runE(predata, OTi, sE_Ni, sE_Nip1_pr, time);
 
 						if (ti == numTrees - 1)
 							con2.write(N);
@@ -282,18 +260,14 @@ public class Access extends Protocol {
 
 						byte[] sD_Nip1_pr = con1.read();
 
-						stopwatch.start();
-						runD(predata, OTi, sD_Ni, sD_Nip1_pr);
-						stopwatch.stop();
+						runD(predata, OTi, sD_Ni, sD_Nip1_pr, time);
 
 					} else if (party == Party.Charlie) {
 						preaccess.runC();
 
 						System.out.println("L" + ti + "=" + new BigInteger(1, Li).toString(2));
 
-						stopwatch.start();
-						OutAccess outaccess = runC(md, ti, Li);
-						stopwatch.stop();
+						OutAccess outaccess = runC(md, ti, Li, time);
 
 						Li = outaccess.C_Lip1;
 
@@ -315,13 +289,6 @@ public class Access extends Protocol {
 			}
 		}
 
-		System.out.println(stopwatch.toMS());
-
-		System.out.println("step0\n" + step0.toMS());
-		System.out.println("step1\n" + step1.toMS());
-		System.out.println("step2\n" + step2.toMS());
-		System.out.println("step3\n" + step3.toMS());
-		System.out.println("step4\n" + step4.toMS());
-		System.out.println("step5\n" + step5.toMS());
+		time.print();
 	}
 }

+ 29 - 6
src/protocols/SSCOT.java

@@ -6,6 +6,9 @@ import crypto.PRF;
 import crypto.PRG;
 import exceptions.NoSuchPartyException;
 import exceptions.SSCOTException;
+import measure.M;
+import measure.P;
+import measure.Timing;
 import oram.Forest;
 import oram.Metadata;
 import util.Util;
@@ -15,7 +18,9 @@ public class SSCOT extends Protocol {
 		super(con1, con2);
 	}
 
-	public void runE(PreData predata, byte[][] m, byte[][] a) {
+	public void runE(PreData predata, byte[][] m, byte[][] a, Timing time) {
+		time.start(P.COT, M.online_comp);
+
 		// step 1
 		int n = m.length;
 		int l = m[0].length * 8;
@@ -36,11 +41,17 @@ public class SSCOT extends Protocol {
 			v[i] = F_kprime.compute(x[i]);
 		}
 
+		time.start(P.COT, M.online_write);
 		con2.write(e);
 		con2.write(v);
+		time.stop(P.COT, M.online_write);
+
+		time.stop(P.COT, M.online_comp);
 	}
 
-	public void runD(PreData predata, byte[][] b) {
+	public void runD(PreData predata, byte[][] b, Timing time) {
+		time.start(P.COT, M.online_comp);
+
 		// step 2
 		int n = b.length;
 		byte[][] y = predata.sscot_r;
@@ -59,18 +70,26 @@ public class SSCOT extends Protocol {
 			w[i] = F_kprime.compute(y[i]);
 		}
 
+		time.start(P.COT, M.online_write);
 		con2.write(p);
 		con2.write(w);
+		time.stop(P.COT, M.online_write);
+
+		time.stop(P.COT, M.online_comp);
 	}
 
-	public OutSSCOT runC() {
+	public OutSSCOT runC(Timing time) {
+		time.start(P.COT, M.online_comp);
+
 		// step 1
+		time.start(P.COT, M.online_read);
 		byte[][] e = con1.readObject();
 		byte[][] v = con1.readObject();
 
 		// step 2
 		byte[][] p = con2.readObject();
 		byte[][] w = con2.readObject();
+		time.stop(P.COT, M.online_read);
 
 		// step 3
 		int n = e.length;
@@ -89,11 +108,15 @@ public class SSCOT extends Protocol {
 
 		if (invariant != 1)
 			throw new SSCOTException("Invariant error: " + invariant);
+
+		time.stop(P.COT, M.online_comp);
 		return output;
 	}
 
 	@Override
 	public void run(Party party, Metadata md, Forest forest) {
+		Timing time = new Timing();
+
 		for (int j = 0; j < 100; j++) {
 			int n = 100;
 			int A = 32;
@@ -118,18 +141,18 @@ public class SSCOT extends Protocol {
 				con2.write(m);
 				con2.write(index);
 				presscot.runE(predata, n);
-				runE(predata, m, a);
+				runE(predata, m, a, time);
 
 			} else if (party == Party.Debbie) {
 				b = con1.readObject();
 				presscot.runD(predata);
-				runD(predata, b);
+				runD(predata, b, time);
 
 			} else if (party == Party.Charlie) {
 				m = con1.readObject();
 				index = con1.readObject();
 				presscot.runC();
-				OutSSCOT output = runC();
+				OutSSCOT output = runC(time);
 				if (output.t == index && Util.equal(output.m_t, m[index]))
 					System.out.println("SSCOT test passed");
 				else

+ 29 - 6
src/protocols/SSIOT.java

@@ -6,6 +6,9 @@ import crypto.PRF;
 import crypto.PRG;
 import exceptions.NoSuchPartyException;
 import exceptions.SSIOTException;
+import measure.M;
+import measure.P;
+import measure.Timing;
 import oram.Forest;
 import oram.Metadata;
 import util.Util;
@@ -15,7 +18,9 @@ public class SSIOT extends Protocol {
 		super(con1, con2);
 	}
 
-	public void runE(PreData predata, byte[][] y, byte[] Nip1_pr) {
+	public void runE(PreData predata, byte[][] y, byte[] Nip1_pr, Timing time) {
+		time.start(P.IOT, M.online_comp);
+
 		// step 1
 		int n = y.length;
 		int l = y[0].length * 8;
@@ -38,11 +43,17 @@ public class SSIOT extends Protocol {
 			v[i] = F_kprime.compute(x[i]);
 		}
 
+		time.start(P.IOT, M.online_write);
 		con2.write(e);
 		con2.write(v);
+		time.stop(P.IOT, M.online_write);
+
+		time.stop(P.IOT, M.online_comp);
 	}
 
-	public void runD(PreData predata, byte[] Nip1_pr) {
+	public void runD(PreData predata, byte[] Nip1_pr, Timing time) {
+		time.start(P.IOT, M.online_comp);
+
 		// step 2
 		PRF F_k = new PRF(Crypto.secParam);
 		F_k.init(predata.ssiot_k);
@@ -55,18 +66,26 @@ public class SSIOT extends Protocol {
 		byte[] p = F_k.compute(y);
 		byte[] w = F_kprime.compute(y);
 
+		time.start(P.IOT, M.online_write);
 		con2.write(p);
 		con2.write(w);
+		time.stop(P.IOT, M.online_write);
+
+		time.stop(P.IOT, M.online_comp);
 	}
 
-	public OutSSIOT runC() {
+	public OutSSIOT runC(Timing time) {
+		time.start(P.IOT, M.online_comp);
+
 		// step 1
+		time.start(P.IOT, M.online_read);
 		byte[][] e = con1.readObject();
 		byte[][] v = con1.readObject();
 
 		// step 2
 		byte[] p = con2.read();
 		byte[] w = con2.read();
+		time.stop(P.IOT, M.online_read);
 
 		// step 3
 		int n = e.length;
@@ -85,11 +104,15 @@ public class SSIOT extends Protocol {
 
 		if (invariant != 1)
 			throw new SSIOTException("Invariant error: " + invariant);
+
+		time.stop(P.IOT, M.online_comp);
 		return output;
 	}
 
 	@Override
 	public void run(Party party, Metadata md, Forest forest) {
+		Timing time = new Timing();
+
 		for (int j = 0; j < 100; j++) {
 			int twoTauPow = 64;
 			int label = 4;
@@ -110,18 +133,18 @@ public class SSIOT extends Protocol {
 				con2.write(y);
 				con2.write(index);
 				pressiot.runE(predata, twoTauPow);
-				runE(predata, y, sE_Nip1_pr);
+				runE(predata, y, sE_Nip1_pr, time);
 
 			} else if (party == Party.Debbie) {
 				sD_Nip1_pr = con1.read();
 				pressiot.runD(predata);
-				runD(predata, sD_Nip1_pr);
+				runD(predata, sD_Nip1_pr, time);
 
 			} else if (party == Party.Charlie) {
 				y = con1.readObject();
 				index = con1.readObject();
 				pressiot.runC();
-				OutSSIOT output = runC();
+				OutSSIOT output = runC(time);
 				if (output.t == index && Util.equal(output.m_t, y[index]))
 					System.out.println("SSIOT test passed");
 				else

+ 17 - 3
src/util/StopWatch.java

@@ -7,6 +7,7 @@ import exceptions.StopWatchException;
 
 public class StopWatch {
 
+	private String task;
 	private long startWC;
 	private long startCPU;
 	public long elapsedWC;
@@ -14,6 +15,16 @@ public class StopWatch {
 	private boolean isOn;
 
 	public StopWatch() {
+		task = "";
+		startWC = 0;
+		startCPU = 0;
+		elapsedWC = 0;
+		elapsedCPU = 0;
+		isOn = false;
+	}
+
+	public StopWatch(String t) {
+		task = t;
 		startWC = 0;
 		startCPU = 0;
 		elapsedWC = 0;
@@ -70,8 +81,11 @@ public class StopWatch {
 	}
 
 	public String toMS() {
-		String out = "WallClock(ms): " + elapsedWC / 1000000;
-		out += "\nCPUClock(ms): " + elapsedCPU / 1000000;
-		return out;
+		String out = " WallClock(ms): " + elapsedWC / 1000000;
+		out += "\n CPUClock(ms): " + elapsedCPU / 1000000;
+		if (task.length() == 0)
+			return out;
+		else
+			return task + "\n" + out;
 	}
 }