Bläddra i källkod

continue removing PreData and Timer

Boyoung- 6 år sedan
förälder
incheckning
01a4d7b441
3 ändrade filer med 169 tillägg och 124 borttagningar
  1. 54 44
      src/pir/SSPIR.java
  2. 41 25
      src/pir/ShiftPIR.java
  3. 74 55
      src/pir/ThreeShiftPIR.java

+ 54 - 44
src/pir/SSPIR.java

@@ -1,5 +1,7 @@
 package pir;
 
+import java.security.SecureRandom;
+
 import communication.Communication;
 import crypto.Crypto;
 import exceptions.NoSuchPartyException;
@@ -7,100 +9,102 @@ 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 SSPIR extends Protocol {
 
-	private int pid = P.SSPIR;
+	SecureRandom sr1;
+	SecureRandom sr2;
 
 	public SSPIR(Communication con1, Communication con2) {
 		super(con1, con2);
 	}
 
-	public byte[] runP1(PreData predata, byte[][] x, Timer timer) {
-		timer.start(pid, M.offline_comp);
+	public SSPIR(Communication con1, Communication con2, SecureRandom sr1, SecureRandom sr2) {
+		super(con1, con2);
+		this.sr1 = sr1;
+		this.sr2 = sr2;
+	}
+
+	public void reinit(Communication con1, Communication con2, SecureRandom sr1, SecureRandom sr2) {
+		this.con1 = con1;
+		this.con2 = con2;
+		this.sr1 = sr1;
+		this.sr2 = sr2;
+	}
+
+	public byte[] runP1(byte[][] x) {
+		timer.start(M.offline_comp);
 
 		int l = x.length;
 		int m = x[0].length;
 		byte[] a1 = new byte[l];
 		byte[] r = new byte[m];
-		Crypto.sr.nextBytes(a1);
-		Crypto.sr.nextBytes(r);
-
-		timer.start(pid, M.offline_write);
-		con2.write(a1);
-		con1.write(r);
-		timer.stop(pid, M.offline_write);
+		sr2.nextBytes(a1);
+		sr1.nextBytes(r);
 
-		timer.stop(pid, M.offline_comp);
+		timer.stop(M.offline_comp);
 
 		// ----------------------------------------- //
 
-		timer.start(pid, M.online_comp);
+		timer.start(M.online_comp);
 
 		byte[] z = Util.xorSelect(x, a1);
 		Util.setXor(z, r);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return z;
 	}
 
-	public byte[] runP2(PreData predata, byte[][] x, Timer timer) {
-		timer.start(pid, M.offline_comp);
+	public byte[] runP2(byte[][] x) {
+		timer.start(M.offline_comp);
 
-		timer.start(pid, M.offline_read);
-		byte[] r = con1.read();
-		timer.stop(pid, M.offline_read);
+		int m = x[0].length;
+		byte[] r = new byte[m];
+		sr1.nextBytes(r);
 
-		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[] a2 = con2.read(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		byte[] a2 = con2.readAndDec();
+		timer.stop(M.online_read);
 
 		byte[] z = Util.xorSelect(x, a2);
 		Util.setXor(z, r);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return z;
 	}
 
-	public void runP3(PreData predata, int t, Timer timer) {
-		timer.start(pid, M.offline_comp);
+	public void runP3(int l, int t) {
+		timer.start(M.offline_comp);
 
-		timer.start(pid, M.offline_read);
-		byte[] a = con1.read();
-		timer.stop(pid, M.offline_read);
+		byte[] a = new byte[l];
+		sr1.nextBytes(a);
 
-		timer.stop(pid, M.offline_comp);
+		timer.stop(M.offline_comp);
 
 		// ----------------------------------------- //
 
-		timer.start(pid, M.online_comp);
+		timer.start(M.online_comp);
 
 		a[t] = (byte) (a[t] ^ 1);
 
-		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);
 	}
 
 	@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 l = 100;
 			int m = 50;
@@ -110,19 +114,25 @@ public class SSPIR extends Protocol {
 			}
 
 			if (party == Party.Eddie) {
+				this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CE);
+
 				con1.write(x);
-				byte[] out = this.runP1(predata, x, timer);
+				byte[] out = this.runP1(x);
 				con2.write(out);
 				con2.write(x);
 
 			} else if (party == Party.Debbie) {
+				this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CD);
+
 				x = con1.readDoubleByteArray();
-				byte[] out = this.runP2(predata, x, timer);
+				byte[] out = this.runP2(x);
 				con2.write(out);
 
 			} else if (party == Party.Charlie) {
+				this.reinit(con1, con2, Crypto.sr_CE, Crypto.sr_CD);
+
 				int index = Crypto.sr.nextInt(l);
-				this.runP3(predata, index, timer);
+				this.runP3(l, index);
 				byte[] out1 = con1.read();
 				x = con1.readDoubleByteArray();
 				byte[] out2 = con2.read();

+ 41 - 25
src/pir/ShiftPIR.java

@@ -1,5 +1,7 @@
 package pir;
 
+import java.security.SecureRandom;
+
 import communication.Communication;
 import crypto.Crypto;
 import exceptions.NoSuchPartyException;
@@ -7,22 +9,33 @@ 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 ShiftPIR extends Protocol {
 
-	private int pid = P.SftPIR;
+	SecureRandom sr1;
+	SecureRandom sr2;
 
 	public ShiftPIR(Communication con1, Communication con2) {
 		super(con1, con2);
 	}
 
-	public byte[] runP1(PreData predata, byte[][] x, int s, Timer timer) {
-		timer.start(pid, M.online_comp);
+	public ShiftPIR(Communication con1, Communication con2, SecureRandom sr1, SecureRandom sr2) {
+		super(con1, con2);
+		this.sr1 = sr1;
+		this.sr2 = sr2;
+	}
+
+	public void reinit(Communication con1, Communication con2, SecureRandom sr1, SecureRandom sr2) {
+		this.con1 = con1;
+		this.con2 = con2;
+		this.sr1 = sr1;
+		this.sr2 = sr2;
+	}
+
+	public byte[] runP1(byte[][] x, int s) {
+		timer.start(M.online_comp);
 
 		// TODO: do in place shift
 		byte[][] xp = new byte[x.length][];
@@ -30,43 +43,40 @@ public class ShiftPIR extends Protocol {
 			xp[i] = x[(i + s) % x.length];
 		}
 
-		SSPIR sspir = new SSPIR(con1, con2);
-		byte[] z = sspir.runP1(predata, xp, timer);
+		SSPIR sspir = new SSPIR(con1, con2, sr1, sr2);
+		byte[] z = sspir.runP1(xp);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return z;
 	}
 
-	public byte[] runP2(PreData predata, byte[][] x, int s, Timer timer) {
-		timer.start(pid, M.online_comp);
+	public byte[] runP2(byte[][] x, int s) {
+		timer.start(M.online_comp);
 
 		byte[][] xp = new byte[x.length][];
 		for (int i = 0; i < x.length; i++) {
 			xp[i] = x[(i + s) % x.length];
 		}
 
-		SSPIR sspir = new SSPIR(con1, con2);
-		byte[] z = sspir.runP2(predata, xp, timer);
+		SSPIR sspir = new SSPIR(con1, con2, sr1, sr2);
+		byte[] z = sspir.runP2(xp);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return z;
 	}
 
-	public void runP3(PreData predata, int t, Timer timer) {
-		timer.start(pid, M.online_comp);
+	public void runP3(int l, int t) {
+		timer.start(M.online_comp);
 
-		SSPIR sspir = new SSPIR(con1, con2);
-		sspir.runP3(predata, t, timer);
+		SSPIR sspir = new SSPIR(con1, con2, sr1, sr2);
+		sspir.runP3(l, t);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 	}
 
 	@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 l = 100;
 			int m = 50;
@@ -78,21 +88,27 @@ public class ShiftPIR extends Protocol {
 			int t = Crypto.sr.nextInt(l);
 
 			if (party == Party.Eddie) {
+				this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CE);
+
 				con1.write(x);
 				con1.write(s);
-				byte[] out = this.runP1(predata, x, s, timer);
+				byte[] out = this.runP1(x, s);
 				con2.write(out);
 				con2.write(x);
 				con2.write(s);
 
 			} else if (party == Party.Debbie) {
+				this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CD);
+
 				x = con1.readDoubleByteArray();
 				s = con1.readInt();
-				byte[] out = this.runP2(predata, x, s, timer);
+				byte[] out = this.runP2(x, s);
 				con2.write(out);
 
 			} else if (party == Party.Charlie) {
-				this.runP3(predata, t, timer);
+				this.reinit(con1, con2, Crypto.sr_CE, Crypto.sr_CD);
+
+				this.runP3(l, t);
 				byte[] out1 = con1.read();
 				x = con1.readDoubleByteArray();
 				s = con1.readInt();

+ 74 - 55
src/pir/ThreeShiftPIR.java

@@ -1,5 +1,7 @@
 package pir;
 
+import java.security.SecureRandom;
+
 import communication.Communication;
 import crypto.Crypto;
 import exceptions.NoSuchPartyException;
@@ -8,105 +10,116 @@ import oram.Metadata;
 import protocols.Protocol;
 import protocols.struct.OutPIRCOT;
 import protocols.struct.Party;
-import protocols.struct.PreData;
 import protocols.struct.TwoThreeXorByte;
 import util.M;
-import util.P;
-import util.Timer;
 import util.Util;
 
 public class ThreeShiftPIR extends Protocol {
 
-	private int pid = P.TSPIR;
+	SecureRandom sr1;
+	SecureRandom sr2;
 
 	public ThreeShiftPIR(Communication con1, Communication con2) {
 		super(con1, con2);
 	}
 
-	public TwoThreeXorByte runE(PreData predata, byte[][] x_DE, byte[][] x_CE, OutPIRCOT i, Timer timer) {
-		timer.start(pid, M.online_comp);
+	public ThreeShiftPIR(Communication con1, Communication con2, SecureRandom sr1, SecureRandom sr2) {
+		super(con1, con2);
+		this.sr1 = sr1;
+		this.sr2 = sr2;
+	}
+
+	public void reinit(Communication con1, Communication con2, SecureRandom sr1, SecureRandom sr2) {
+		this.con1 = con1;
+		this.con2 = con2;
+		this.sr1 = sr1;
+		this.sr2 = sr2;
+	}
+
+	public TwoThreeXorByte runE(byte[][] x_DE, byte[][] x_CE, OutPIRCOT i) {
+		timer.start(M.online_comp);
 
-		ShiftPIR sftpir = new ShiftPIR(con1, con2);
-		byte[] e1 = sftpir.runP1(predata, x_DE, i.s_DE, timer);
-		sftpir = new ShiftPIR(con2, con1);
-		byte[] e2 = sftpir.runP2(predata, x_CE, i.s_CE, timer);
-		sftpir = new ShiftPIR(con1, con2);
-		sftpir.runP3(predata, i.t_E, timer);
+		int l = x_DE.length;
+		ShiftPIR sftpir = new ShiftPIR(con1, con2, sr1, sr2);
+		byte[] e1 = sftpir.runP1(x_DE, i.s_DE);
+		sftpir.reinit(con2, con1, sr2, sr1);
+		byte[] e2 = sftpir.runP2(x_CE, i.s_CE);
+		sftpir.reinit(con1, con2, sr1, sr2);
+		sftpir.runP3(l, i.t_E);
 		Util.setXor(e1, e2);
 
 		TwoThreeXorByte X = new TwoThreeXorByte();
 		X.DE = e1;
 
-		timer.start(pid, M.online_write);
-		con1.write(pid, X.DE);
-		timer.stop(pid, M.online_write);
+		timer.start(M.online_write);
+		con1.write(online_band, X.DE);
+		timer.stop(M.online_write);
 
-		timer.start(pid, M.online_read);
-		X.CE = con2.read(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		X.CE = con2.readAndDec();
+		timer.stop(M.online_read);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return X;
 	}
 
-	public TwoThreeXorByte runD(PreData predata, byte[][] x_DE, byte[][] x_CD, OutPIRCOT i, Timer timer) {
-		timer.start(pid, M.online_comp);
+	public TwoThreeXorByte runD(byte[][] x_DE, byte[][] x_CD, OutPIRCOT i) {
+		timer.start(M.online_comp);
 
-		ShiftPIR sftpir = new ShiftPIR(con1, con2);
-		byte[] d1 = sftpir.runP2(predata, x_DE, i.s_DE, timer);
-		sftpir = new ShiftPIR(con2, con1);
-		sftpir.runP3(predata, i.t_D, timer);
-		sftpir = new ShiftPIR(con2, con1);
-		byte[] d2 = sftpir.runP1(predata, x_CD, i.s_CD, timer);
+		int l = x_DE.length;
+		ShiftPIR sftpir = new ShiftPIR(con1, con2, sr1, sr2);
+		byte[] d1 = sftpir.runP2(x_DE, i.s_DE);
+		sftpir.reinit(con2, con1, sr2, sr1);
+		sftpir.runP3(l, i.t_D);
+		sftpir.reinit(con2, con1, sr2, sr1);
+		byte[] d2 = sftpir.runP1(x_CD, i.s_CD);
 		Util.setXor(d1, d2);
 
 		TwoThreeXorByte X = new TwoThreeXorByte();
 		X.CD = d1;
 
-		timer.start(pid, M.online_write);
-		con2.write(pid, X.CD);
-		timer.stop(pid, M.online_write);
+		timer.start(M.online_write);
+		con2.write(online_band, X.CD);
+		timer.stop(M.online_write);
 
-		timer.start(pid, M.online_read);
-		X.DE = con1.read(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		X.DE = con1.readAndDec();
+		timer.stop(M.online_read);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return X;
 	}
 
-	public TwoThreeXorByte runC(PreData predata, byte[][] x_CD, byte[][] x_CE, OutPIRCOT i, Timer timer) {
-		timer.start(pid, M.online_comp);
+	public TwoThreeXorByte runC(byte[][] x_CD, byte[][] x_CE, OutPIRCOT i) {
+		timer.start(M.online_comp);
 
-		ShiftPIR sftpir = new ShiftPIR(con1, con2);
-		sftpir.runP3(predata, i.t_C, timer);
-		sftpir = new ShiftPIR(con1, con2);
-		byte[] c1 = sftpir.runP1(predata, x_CE, i.s_CE, timer);
-		sftpir = new ShiftPIR(con2, con1);
-		byte[] c2 = sftpir.runP2(predata, x_CD, i.s_CD, timer);
+		int l = x_CD.length;
+		ShiftPIR sftpir = new ShiftPIR(con1, con2, sr1, sr2);
+		sftpir.runP3(l, i.t_C);
+		sftpir.reinit(con1, con2, sr1, sr2);
+		byte[] c1 = sftpir.runP1(x_CE, i.s_CE);
+		sftpir.reinit(con2, con1, sr2, sr1);
+		byte[] c2 = sftpir.runP2(x_CD, i.s_CD);
 		Util.setXor(c1, c2);
 
 		TwoThreeXorByte X = new TwoThreeXorByte();
 		X.CE = c1;
 
-		timer.start(pid, M.online_write);
-		con1.write(pid, X.CE);
-		timer.stop(pid, M.online_write);
+		timer.start(M.online_write);
+		con1.write(online_band, X.CE);
+		timer.stop(M.online_write);
 
-		timer.start(pid, M.online_read);
-		X.CD = con2.read(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		X.CD = con2.readAndDec();
+		timer.stop(M.online_read);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return X;
 	}
 
 	@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 l = 500;
 			int m = 50;
@@ -130,6 +143,8 @@ public class ThreeShiftPIR extends Protocol {
 			TwoThreeXorByte X = new TwoThreeXorByte();
 
 			if (party == Party.Eddie) {
+				this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CE);
+
 				con1.write(x_CD);
 				con1.write(x_DE);
 				con2.write(x_CD);
@@ -141,7 +156,7 @@ public class ThreeShiftPIR extends Protocol {
 				con2.write(ks.s_CE);
 				con2.write(ks.s_CD);
 
-				X = this.runE(predata, x_DE, x_CE, ks, timer);
+				X = this.runE(x_DE, x_CE, ks);
 				X.CD = con1.read();
 				byte[] e = X.CE;
 				Util.setXor(e, X.CD);
@@ -156,23 +171,27 @@ public class ThreeShiftPIR extends Protocol {
 					System.out.println(j + ": 3ShiftPIR test passed");
 
 			} else if (party == Party.Debbie) {
+				this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CD);
+
 				x_CD = con1.readDoubleByteArray();
 				x_DE = con1.readDoubleByteArray();
 				ks.t_D = con1.readInt();
 				ks.s_DE = con1.readInt();
 				ks.s_CD = con1.readInt();
 
-				X = this.runD(predata, x_DE, x_CD, ks, timer);
+				X = this.runD(x_DE, x_CD, ks);
 				con1.write(X.CD);
 
 			} else if (party == Party.Charlie) {
+				this.reinit(con1, con2, Crypto.sr_CE, Crypto.sr_CD);
+
 				x_CD = con1.readDoubleByteArray();
 				x_CE = con1.readDoubleByteArray();
 				ks.t_C = con1.readInt();
 				ks.s_CE = con1.readInt();
 				ks.s_CD = con1.readInt();
 
-				this.runC(predata, x_CD, x_CE, ks, timer);
+				this.runC(x_CD, x_CE, ks);
 
 			} else {
 				throw new NoSuchPartyException(party + "");