Browse Source

continue removing PreData and Timer

Boyoung- 7 years ago
parent
commit
161f97f19d
2 changed files with 90 additions and 118 deletions
  1. 46 56
      src/pir/FlipFlag.java
  2. 44 62
      src/pir/Shift.java

+ 46 - 56
src/pir/FlipFlag.java

@@ -8,142 +8,132 @@ import oram.Metadata;
 import protocols.Protocol;
 import protocols.struct.OutFF;
 import protocols.struct.Party;
-import protocols.struct.PreData;
 import util.M;
-import util.P;
-import util.Timer;
 import util.Util;
 
 public class FlipFlag extends Protocol {
 
-	private int pid = P.FF;
-
 	public FlipFlag(Communication con1, Communication con2) {
 		super(con1, con2);
 	}
 
 	// TODO: remove loop around setXor: use Util.setXor(byte[][], byte[][])
 
-	public OutFF runE(PreData predata, byte[][] fb_DE, byte[][] fb_CE, int i2, Timer timer) {
-		timer.start(pid, M.offline_comp);
+	public OutFF runE(byte[][] fb_DE, byte[][] fb_CE, int i2) {
+		timer.start(M.offline_comp);
 
+		int n = fb_DE.length;
 		OutFF outff = new OutFF();
 
-		timer.start(pid, M.offline_read);
-		outff.fb_DE = con1.readDoubleByteArray();
-		timer.stop(pid, M.offline_read);
+		outff.fb_DE = new byte[n][1];
+		for (int i = 0; i < n; i++) {
+			Crypto.sr_DE.nextBytes(outff.fb_DE[i]);
+		}
 
-		timer.stop(pid, M.offline_comp);
+		timer.stop(M.offline_comp);
 
 		// ----------------------------------------- //
 
-		timer.start(pid, M.online_comp);
+		timer.start(M.online_comp);
 
-		int n = fb_DE.length;
 		byte[][] a2 = new byte[n][1];
 
 		Shift shift = new Shift(con1, con2);
-		byte[][] m2 = shift.runE(predata, a2, n - i2, timer);
+		byte[][] m2 = shift.runE(a2, n - i2);
 
 		for (int i = 0; i < n; i++)
 			Util.setXor(m2[i], outff.fb_DE[i]);
 
-		timer.start(pid, M.online_write);
-		con2.write(pid, m2);
-		timer.stop(pid, M.online_write);
+		timer.start(M.online_write);
+		con2.write(online_band, m2);
+		timer.stop(M.online_write);
 
-		timer.start(pid, M.online_read);
-		byte[][] m1 = con2.readDoubleByteArray(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		byte[][] m1 = con2.readDoubleByteArrayAndDec();
+		timer.stop(M.online_read);
 
 		outff.fb_CE = Util.xor(m1, m2);
 		Util.setXor(outff.fb_CE, fb_CE);
 		Util.setXor(outff.fb_DE, fb_DE);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return outff;
 	}
 
-	public OutFF runD(PreData predata, byte[][] fb_DE, byte[][] fb_CD, int i2, Timer timer) {
-		timer.start(pid, M.offline_comp);
+	public OutFF runD(byte[][] fb_DE, byte[][] fb_CD, int i2) {
+		timer.start(M.offline_comp);
 
 		int n = fb_DE.length;
 		OutFF outff = new OutFF();
+
 		outff.fb_CD = new byte[n][1];
 		outff.fb_DE = new byte[n][1];
 		for (int i = 0; i < n; i++) {
-			Crypto.sr.nextBytes(outff.fb_CD[i]);
-			Crypto.sr.nextBytes(outff.fb_DE[i]);
+			Crypto.sr_CD.nextBytes(outff.fb_CD[i]);
+			Crypto.sr_DE.nextBytes(outff.fb_DE[i]);
 		}
 
-		timer.start(pid, M.offline_write);
-		con1.write(outff.fb_DE);
-		con2.write(outff.fb_CD);
-		timer.stop(pid, M.offline_write);
-
-		timer.stop(pid, M.offline_comp);
+		timer.stop(M.offline_comp);
 
 		// ----------------------------------------- //
 
-		timer.start(pid, M.online_comp);
+		timer.start(M.online_comp);
 
 		Shift shift = new Shift(con1, con2);
-		shift.runD(predata, n - i2, n, 1, timer);
+		shift.runD(n - i2, n, 1);
 
 		Util.setXor(outff.fb_CD, fb_CD);
 		Util.setXor(outff.fb_DE, fb_DE);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return outff;
 	}
 
-	public OutFF runC(PreData predata, byte[][] fb_CD, byte[][] fb_CE, int i1, Timer timer) {
-		timer.start(pid, M.offline_comp);
+	public OutFF runC(byte[][] fb_CD, byte[][] fb_CE, int i1) {
+		timer.start(M.offline_comp);
 
+		int n = fb_CD.length;
 		OutFF outff = new OutFF();
 
-		timer.start(pid, M.offline_read);
-		outff.fb_CD = con2.readDoubleByteArray();
-		timer.stop(pid, M.offline_read);
+		outff.fb_CD = new byte[n][1];
+		for (int i = 0; i < n; i++) {
+			Crypto.sr_CD.nextBytes(outff.fb_CD[i]);
+		}
 
-		timer.stop(pid, M.offline_comp);
+		timer.stop(M.offline_comp);
 
 		// ----------------------------------------- //
 
-		timer.start(pid, M.online_comp);
+		timer.start(M.online_comp);
 
-		int n = fb_CD.length;
 		byte[][] a1 = new byte[n][1];
 		a1[i1][0] = 1;
 
 		Shift shift = new Shift(con1, con2);
-		byte[][] m1 = shift.runC(predata, a1, timer);
+		byte[][] m1 = shift.runC(a1);
 
 		for (int i = 0; i < n; i++)
 			Util.setXor(m1[i], outff.fb_CD[i]);
 
-		timer.start(pid, M.online_write);
-		con1.write(pid, m1);
-		timer.stop(pid, M.online_write);
+		timer.start(M.online_write);
+		con1.write(online_band, m1);
+		timer.stop(M.online_write);
 
-		timer.start(pid, M.online_read);
-		byte[][] m2 = con1.readDoubleByteArray(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		byte[][] m2 = con1.readDoubleByteArrayAndDec();
+		timer.stop(M.online_read);
 
 		outff.fb_CE = Util.xor(m1, m2);
 		Util.setXor(outff.fb_CE, fb_CE);
 		Util.setXor(outff.fb_CD, fb_CD);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return outff;
 	}
 
 	@Override
 	public void run(Party party, Metadata md, Forest[] forest) {
 
-		Timer timer = new Timer();
-		PreData predata = new PreData();
-
 		for (int j = 0; j < 100; j++) {
 			int n = 100;
 			int l = 1;
@@ -170,7 +160,7 @@ public class FlipFlag extends Protocol {
 				con2.write(fb_CD);
 				con2.write(i1);
 
-				outff = this.runE(predata, fb_DE, fb_CE, i2, timer);
+				outff = this.runE(fb_DE, fb_CE, i2);
 				outff.fb_CD = con1.readDoubleByteArray();
 
 				byte[][] fbp = Util.xor(Util.xor(outff.fb_CD, outff.fb_CE), outff.fb_DE);
@@ -192,7 +182,7 @@ public class FlipFlag extends Protocol {
 				fb_DE = con1.readDoubleByteArray();
 				i2 = con1.readInt();
 
-				outff = this.runD(predata, fb_DE, fb_CD, i2, timer);
+				outff = this.runD(fb_DE, fb_CD, i2);
 				con1.write(outff.fb_CD);
 
 			} else if (party == Party.Charlie) {
@@ -200,7 +190,7 @@ public class FlipFlag extends Protocol {
 				fb_CD = con1.readDoubleByteArray();
 				i1 = con1.readInt();
 
-				outff = this.runC(predata, fb_CD, fb_CE, i1, timer);
+				outff = this.runC(fb_CD, fb_CE, i1);
 
 			} else {
 				throw new NoSuchPartyException(party + "");

+ 44 - 62
src/pir/Shift.java

@@ -7,46 +7,38 @@ import oram.Forest;
 import oram.Metadata;
 import protocols.Protocol;
 import protocols.struct.Party;
-import protocols.struct.PreData;
 import util.M;
-import util.P;
-import util.Timer;
 import util.Util;
 
 public class Shift extends Protocol {
 
-	private int pid = P.Shift;
-
 	public Shift(Communication con1, Communication con2) {
 		super(con1, con2);
 	}
 
-	public byte[][] runE(PreData predata, byte[][] x, int s, Timer timer) {
-		timer.start(pid, M.offline_comp);
+	public byte[][] runE(byte[][] x, int s) {
+		timer.start(M.offline_comp);
 
 		int n = x.length;
 		int l = x[0].length;
+
 		byte[][] q = new byte[n][];
 		for (int i = 0; i < n; i++)
-			q[i] = Util.nextBytes(l, Crypto.sr);
+			q[i] = Util.nextBytes(l, Crypto.sr_CE);
 
-		timer.start(pid, M.offline_write);
-		con2.write(q);
-		timer.stop(pid, M.offline_write);
-
-		timer.start(pid, M.offline_read);
-		byte[][] r = con1.readDoubleByteArray();
-		timer.stop(pid, M.offline_read);
+		byte[][] r = new byte[n][];
+		for (int i = 0; i < n; i++)
+			r[i] = Util.nextBytes(l, Crypto.sr_DE);
 
-		timer.stop(pid, M.offline_comp);
+		timer.stop(M.offline_comp);
 
 		// ----------------------------------------- //
 
-		timer.start(pid, M.online_comp);
+		timer.start(M.online_comp);
 
-		timer.start(pid, M.online_read);
-		byte[][] z = con2.readDoubleByteArray(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		byte[][] z = con2.readDoubleByteArrayAndDec();
+		timer.stop(M.online_read);
 
 		byte[][] b = new byte[n][];
 		for (int i = 0; i < n; i++) {
@@ -58,93 +50,83 @@ public class Shift extends Protocol {
 			Util.setXor(b[i], q[i]);
 		}
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return b;
 	}
 
-	public void runD(PreData predata, int s, int n, int l, Timer timer) {
-		timer.start(pid, M.offline_comp);
+	public void runD(int s, int n, int l) {
+		timer.start(M.offline_comp);
 
 		byte[][] r = new byte[n][];
 		for (int i = 0; i < n; i++)
-			r[i] = Util.nextBytes(l, Crypto.sr);
-
-		timer.start(pid, M.offline_write);
-		con1.write(r);
-		timer.stop(pid, M.offline_write);
+			r[i] = Util.nextBytes(l, Crypto.sr_DE);
 
-		timer.start(pid, M.offline_read);
-		byte[][] p = con2.readDoubleByteArray();
-		timer.stop(pid, M.offline_read);
+		byte[][] p = new byte[n][];
+		for (int i = 0; i < n; i++)
+			p[i] = Util.nextBytes(l, Crypto.sr_CD);
 
 		for (int i = 0; i < n; i++)
 			Util.setXor(p[i], r[i]);
 
-		timer.stop(pid, M.offline_comp);
+		timer.stop(M.offline_comp);
 
 		// ----------------------------------------- //
 
-		timer.start(pid, M.online_comp);
+		timer.start(M.online_comp);
 
 		byte[][] a = new byte[n][];
 		for (int i = 0; i < n; i++)
 			a[i] = p[(i + s) % n];
 
-		timer.start(pid, M.online_write);
-		con2.write(pid, a);
-		timer.stop(pid, M.online_write);
+		timer.start(M.online_write);
+		con2.write(online_band, a);
+		timer.stop(M.online_write);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return;
 	}
 
-	public byte[][] runC(PreData predata, byte[][] x, Timer timer) {
-		timer.start(pid, M.offline_comp);
+	public byte[][] runC(byte[][] x) {
+		timer.start(M.offline_comp);
 
 		int n = x.length;
 		int l = x[0].length;
+
 		byte[][] p = new byte[n][];
 		for (int i = 0; i < n; i++)
-			p[i] = Util.nextBytes(l, Crypto.sr);
-
-		timer.start(pid, M.offline_write);
-		con2.write(p);
-		timer.stop(pid, M.offline_write);
+			p[i] = Util.nextBytes(l, Crypto.sr_CD);
 
-		timer.start(pid, M.offline_read);
-		byte[][] q = con1.readDoubleByteArray();
-		timer.stop(pid, M.offline_read);
+		byte[][] q = new byte[n][];
+		for (int i = 0; i < n; i++)
+			q[i] = Util.nextBytes(l, Crypto.sr_CE);
 
-		timer.stop(pid, M.offline_comp);
+		timer.stop(M.offline_comp);
 
 		// ----------------------------------------- //
 
-		timer.start(pid, M.online_comp);
+		timer.start(M.online_comp);
 
 		for (int i = 0; i < n; i++)
 			Util.setXor(p[i], x[i]);
 
-		timer.start(pid, M.online_write);
-		con1.write(pid, p);
-		timer.stop(pid, M.online_write);
+		timer.start(M.online_write);
+		con1.write(online_band, p);
+		timer.stop(M.online_write);
 
-		timer.start(pid, M.online_read);
-		byte[][] a = con2.readDoubleByteArray(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		byte[][] a = con2.readDoubleByteArrayAndDec();
+		timer.stop(M.online_read);
 
 		for (int i = 0; i < n; i++)
 			Util.setXor(a[i], q[i]);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return a;
 	}
 
 	@Override
 	public void run(Party party, Metadata md, Forest[] forest) {
 
-		Timer timer = new Timer();
-		PreData predata = new PreData();
-
 		for (int j = 0; j < 100; j++) {
 			int n = 500;
 			int l = 50;
@@ -156,7 +138,7 @@ public class Shift extends Protocol {
 
 			if (party == Party.Eddie) {
 				con1.write(s);
-				byte[][] y1 = this.runE(predata, x, s, timer);
+				byte[][] y1 = this.runE(x, s);
 
 				byte[][] x2 = con2.readDoubleByteArray();
 				byte[][] y2 = con2.readDoubleByteArray();
@@ -177,10 +159,10 @@ public class Shift extends Protocol {
 
 			} else if (party == Party.Debbie) {
 				s = con1.readInt();
-				this.runD(predata, s, n, l, timer);
+				this.runD(s, n, l);
 
 			} else if (party == Party.Charlie) {
-				byte[][] y2 = this.runC(predata, x, timer);
+				byte[][] y2 = this.runC(x);
 
 				con1.write(x);
 				con1.write(y2);