Преглед изворни кода

continue removing PreData and Timer

Boyoung- пре 6 година
родитељ
комит
0a02da2334
2 измењених фајлова са 119 додато и 89 уклоњено
  1. 40 25
      src/pir/ShiftXorPIR.java
  2. 79 64
      src/pir/ThreeShiftXorPIR.java

+ 40 - 25
src/pir/ShiftXorPIR.java

@@ -1,5 +1,6 @@
 package pir;
 
+import java.security.SecureRandom;
 import java.util.Arrays;
 
 import communication.Communication;
@@ -9,22 +10,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 ShiftXorPIR extends Protocol {
 
-	private int pid = P.SftXorPIR;
+	SecureRandom sr1;
+	SecureRandom sr2;
 
 	public ShiftXorPIR(Communication con1, Communication con2) {
 		super(con1, con2);
 	}
 
-	public byte[] runP1(PreData predata, byte[][] x, int s1, int s2, int m, Timer timer) {
-		timer.start(pid, M.online_comp);
+	public ShiftXorPIR(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 s1, int s2, int m) {
+		timer.start(M.online_comp);
 
 		int n = x.length;
 		int l = x[0].length / m;
@@ -36,15 +48,15 @@ public class ShiftXorPIR extends Protocol {
 			}
 		}
 
-		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 s1, int s2, int m, Timer timer) {
-		timer.start(pid, M.online_comp);
+	public byte[] runP2(byte[][] x, int s1, int s2, int m) {
+		timer.start(M.online_comp);
 
 		int n = x.length;
 		int l = x[0].length / m;
@@ -56,30 +68,27 @@ public class ShiftXorPIR extends Protocol {
 			}
 		}
 
-		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 t1, int t2, int m, Timer timer) {
-		timer.start(pid, M.online_comp);
+	public void runP3(int t1, int t2, int n, int m) {
+		timer.start(M.online_comp);
 
 		int t = t1 * m + t2;
 
-		SSPIR sspir = new SSPIR(con1, con2);
-		sspir.runP3(predata, t, timer);
+		SSPIR sspir = new SSPIR(con1, con2, sr1, sr2);
+		sspir.runP3(n * m, 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 n = 500;
 			int m = 16;
@@ -94,12 +103,14 @@ public class ShiftXorPIR extends Protocol {
 			int t2 = Crypto.sr.nextInt(m);
 
 			if (party == Party.Eddie) {
+				this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CE);
+
 				con1.write(x);
 				con1.write(s1);
 				con1.write(s2);
 				con2.write(t1);
 				con2.write(t2);
-				byte[] e = this.runP1(predata, x, s1, s2, m, timer);
+				byte[] e = this.runP1(x, s1, s2, m);
 
 				byte[] d = con1.read();
 				Util.setXor(e, d);
@@ -114,17 +125,21 @@ public class ShiftXorPIR extends Protocol {
 					System.out.println(j + ": ShiftXorPIR test passed");
 
 			} else if (party == Party.Debbie) {
+				this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CD);
+
 				x = con1.readDoubleByteArray();
 				s1 = con1.readInt();
 				s2 = con1.readInt();
-				byte[] d = this.runP2(predata, x, s1, s2, m, timer);
+				byte[] d = this.runP2(x, s1, s2, m);
 
 				con1.write(d);
 
 			} else if (party == Party.Charlie) {
+				this.reinit(con1, con2, Crypto.sr_CE, Crypto.sr_CD);
+
 				t1 = con1.readInt();
 				t2 = con1.readInt();
-				this.runP3(predata, t1, t2, m, timer);
+				this.runP3(t1, t2, n, m);
 
 			} else {
 				throw new NoSuchPartyException(party + "");

+ 79 - 64
src/pir/ThreeShiftXorPIR.java

@@ -1,5 +1,6 @@
 package pir;
 
+import java.security.SecureRandom;
 import java.util.Arrays;
 
 import communication.Communication;
@@ -10,121 +11,129 @@ import oram.Metadata;
 import protocols.Protocol;
 import protocols.struct.OutPIRCOT;
 import protocols.struct.Party;
-import protocols.struct.PreData;
 import protocols.struct.TwoOneXor;
 import protocols.struct.TwoThreeXorByte;
 import util.M;
-import util.P;
-import util.Timer;
 import util.Util;
 
 public class ThreeShiftXorPIR extends Protocol {
 
-	private int pid = P.TSXPIR;
+	SecureRandom sr1;
+	SecureRandom sr2;
 
 	public ThreeShiftXorPIR(Communication con1, Communication con2) {
 		super(con1, con2);
 	}
 
-	public TwoThreeXorByte runE(PreData predata, byte[][] x_DE, byte[][] x_CE, OutPIRCOT i, TwoOneXor dN, int ttp,
-			Timer timer) {
-		timer.start(pid, M.online_comp);
+	public ThreeShiftXorPIR(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, TwoOneXor dN, int ttp) {
+		timer.start(M.online_comp);
 
-		ShiftXorPIR sftpir = new ShiftXorPIR(con1, con2);
-		byte[] e1 = sftpir.runP1(predata, x_DE, i.s_DE, dN.s_DE, ttp, timer);
-		sftpir = new ShiftXorPIR(con2, con1);
-		byte[] e2 = sftpir.runP2(predata, x_CE, i.s_CE, dN.s_CE, ttp, timer);
-		sftpir = new ShiftXorPIR(con1, con2);
-		sftpir.runP3(predata, i.t_E, dN.t_E, ttp, timer);
+		int n = x_DE.length;
+		ShiftXorPIR sftpir = new ShiftXorPIR(con1, con2, sr1, sr2);
+		byte[] e1 = sftpir.runP1(x_DE, i.s_DE, dN.s_DE, ttp);
+		sftpir.reinit(con2, con1, sr2, sr1);
+		byte[] e2 = sftpir.runP2(x_CE, i.s_CE, dN.s_CE, ttp);
+		sftpir.reinit(con1, con2, sr1, sr2);
+		sftpir.runP3(i.t_E, dN.t_E, n, ttp);
 		Util.setXor(e1, e2);
 
-		timer.start(pid, M.online_write);
-		con1.write(pid, e1);
-		con2.write(pid, e1);
-		timer.stop(pid, M.online_write);
+		timer.start(M.online_write);
+		con1.write(online_band, e1);
+		con2.write(online_band, e1);
+		timer.stop(M.online_write);
 
-		timer.start(pid, M.online_read);
-		byte[] d = con1.read(pid);
-		byte[] c = con2.read(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		byte[] d = con1.readAndDec();
+		byte[] c = con2.readAndDec();
+		timer.stop(M.online_read);
 
 		TwoThreeXorByte nextL = new TwoThreeXorByte();
 		nextL.DE = e1;
 		nextL.CD = d;
 		nextL.CE = c;
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return nextL;
 	}
 
-	public TwoThreeXorByte runD(PreData predata, byte[][] x_DE, byte[][] x_CD, OutPIRCOT i, TwoOneXor dN, int ttp,
-			Timer timer) {
-		timer.start(pid, M.online_comp);
+	public TwoThreeXorByte runD(byte[][] x_DE, byte[][] x_CD, OutPIRCOT i, TwoOneXor dN, int ttp) {
+		timer.start(M.online_comp);
 
-		ShiftXorPIR sftpir = new ShiftXorPIR(con1, con2);
-		byte[] d1 = sftpir.runP2(predata, x_DE, i.s_DE, dN.s_DE, ttp, timer);
-		sftpir = new ShiftXorPIR(con2, con1);
-		sftpir.runP3(predata, i.t_D, dN.t_D, ttp, timer);
-		sftpir = new ShiftXorPIR(con2, con1);
-		byte[] d2 = sftpir.runP1(predata, x_CD, i.s_CD, dN.s_CD, ttp, timer);
+		int n = x_DE.length;
+		ShiftXorPIR sftpir = new ShiftXorPIR(con1, con2, sr1, sr2);
+		byte[] d1 = sftpir.runP2(x_DE, i.s_DE, dN.s_DE, ttp);
+		sftpir.reinit(con2, con1, sr2, sr1);
+		sftpir.runP3(i.t_D, dN.t_D, n, ttp);
+		sftpir.reinit(con2, con1, sr2, sr1);
+		byte[] d2 = sftpir.runP1(x_CD, i.s_CD, dN.s_CD, ttp);
 		Util.setXor(d1, d2);
 
-		timer.start(pid, M.online_write);
-		con1.write(pid, d1);
-		con2.write(pid, d1);
-		timer.stop(pid, M.online_write);
+		timer.start(M.online_write);
+		con1.write(online_band, d1);
+		con2.write(online_band, d1);
+		timer.stop(M.online_write);
 
-		timer.start(pid, M.online_read);
-		byte[] e = con1.read(pid);
-		byte[] c = con2.read(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		byte[] e = con1.readAndDec();
+		byte[] c = con2.readAndDec();
+		timer.stop(M.online_read);
 
 		TwoThreeXorByte nextL = new TwoThreeXorByte();
 		nextL.DE = e;
 		nextL.CD = d1;
 		nextL.CE = c;
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return nextL;
 	}
 
-	public TwoThreeXorByte runC(PreData predata, byte[][] x_CD, byte[][] x_CE, OutPIRCOT i, TwoOneXor dN, int ttp,
-			Timer timer) {
-		timer.start(pid, M.online_comp);
+	public TwoThreeXorByte runC(byte[][] x_CD, byte[][] x_CE, OutPIRCOT i, TwoOneXor dN, int ttp) {
+		timer.start(M.online_comp);
 
-		ShiftXorPIR sftpir = new ShiftXorPIR(con1, con2);
-		sftpir.runP3(predata, i.t_C, dN.t_C, ttp, timer);
-		sftpir = new ShiftXorPIR(con1, con2);
-		byte[] c1 = sftpir.runP1(predata, x_CE, i.s_CE, dN.s_CE, ttp, timer);
-		sftpir = new ShiftXorPIR(con2, con1);
-		byte[] c2 = sftpir.runP2(predata, x_CD, i.s_CD, dN.s_CD, ttp, timer);
+		int n = x_CD.length;
+		ShiftXorPIR sftpir = new ShiftXorPIR(con1, con2, sr1, sr2);
+		sftpir.runP3(i.t_C, dN.t_C, n, ttp);
+		sftpir.reinit(con1, con2, sr1, sr2);
+		byte[] c1 = sftpir.runP1(x_CE, i.s_CE, dN.s_CE, ttp);
+		sftpir.reinit(con2, con1, sr2, sr1);
+		byte[] c2 = sftpir.runP2(x_CD, i.s_CD, dN.s_CD, ttp);
 		Util.setXor(c1, c2);
 
-		timer.start(pid, M.online_write);
-		con1.write(pid, c1);
-		con2.write(pid, c1);
-		timer.stop(pid, M.online_write);
+		timer.start(M.online_write);
+		con1.write(online_band, c1);
+		con2.write(online_band, c1);
+		timer.stop(M.online_write);
 
-		timer.start(pid, M.online_read);
-		byte[] e = con1.read(pid);
-		byte[] d = con2.read(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		byte[] e = con1.readAndDec();
+		byte[] d = con2.readAndDec();
+		timer.stop(M.online_read);
 
 		TwoThreeXorByte nextL = new TwoThreeXorByte();
 		nextL.DE = e;
 		nextL.CD = d;
 		nextL.CE = c1;
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return nextL;
 	}
 
 	@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 m = 16;
@@ -156,6 +165,8 @@ public class ThreeShiftXorPIR extends Protocol {
 			tox.s_CD = i2 ^ tox.t_E;
 
 			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);
@@ -173,7 +184,7 @@ public class ThreeShiftXorPIR extends Protocol {
 				con2.write(tox.s_CE);
 				con2.write(tox.s_CD);
 
-				TwoThreeXorByte nextL = this.runE(predata, x_DE, x_CE, ks, tox, m, timer);
+				TwoThreeXorByte nextL = this.runE(x_DE, x_CE, ks, tox, m);
 				byte[] e = Util.xor(Util.xor(nextL.DE, nextL.CE), nextL.CD);
 				byte[] d = con1.read();
 				byte[] c = con2.read();
@@ -189,6 +200,8 @@ public class ThreeShiftXorPIR extends Protocol {
 					System.out.println(j + ": 3ShiftXorPIR 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();
@@ -198,11 +211,13 @@ public class ThreeShiftXorPIR extends Protocol {
 				tox.s_DE = con1.readInt();
 				tox.s_CD = con1.readInt();
 
-				TwoThreeXorByte nextL = this.runD(predata, x_DE, x_CD, ks, tox, m, timer);
+				TwoThreeXorByte nextL = this.runD(x_DE, x_CD, ks, tox, m);
 				byte[] d = Util.xor(Util.xor(nextL.DE, nextL.CE), nextL.CD);
 				con1.write(d);
 
 			} 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();
@@ -212,7 +227,7 @@ public class ThreeShiftXorPIR extends Protocol {
 				tox.s_CE = con1.readInt();
 				tox.s_CD = con1.readInt();
 
-				TwoThreeXorByte nextL = this.runC(predata, x_CD, x_CE, ks, tox, m, timer);
+				TwoThreeXorByte nextL = this.runC(x_CD, x_CE, ks, tox, m);
 				byte[] c = Util.xor(Util.xor(nextL.DE, nextL.CE), nextL.CD);
 				con1.write(c);