Bladeren bron

continue removing PreData and Timer

Boyoung- 6 jaren geleden
bovenliggende
commit
9c172dcf1e
2 gewijzigde bestanden met toevoegingen van 154 en 140 verwijderingen
  1. 84 76
      src/pir/InsLbl.java
  2. 70 64
      src/pir/ULiT.java

+ 84 - 76
src/pir/InsLbl.java

@@ -1,5 +1,6 @@
 package pir;
 
+import java.security.SecureRandom;
 import java.util.Arrays;
 
 import communication.Communication;
@@ -9,60 +10,66 @@ 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 InsLbl extends Protocol {
 
-	private int pid = P.InsLbl;
+	SecureRandom sr1;
+	SecureRandom sr2;
 
 	public InsLbl(Communication con1, Communication con2) {
 		super(con1, con2);
 	}
 
-	public void runP1(PreData predata, int dN1, byte[] L1, int ttp, Timer timer) {
-		timer.start(pid, M.offline_comp);
+	public InsLbl(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 void runP1(int dN1, byte[] L1, int ttp) {
+		timer.start(M.offline_comp);
 
 		int l = L1.length;
 
-		byte[] p = Util.nextBytes(ttp * l, Crypto.sr);
-		byte[] a = Util.nextBytes(ttp * l, Crypto.sr);
-		byte[] b = Util.nextBytes(ttp * l, Crypto.sr);
-		int v = Crypto.sr.nextInt(ttp);
-		int w = Crypto.sr.nextInt(ttp);
+		byte[] p = Util.nextBytes(ttp * l, sr1);
+		byte[] a = Util.nextBytes(ttp * l, sr1);
+		byte[] b = Util.nextBytes(ttp * l, sr1);
+		int v = sr1.nextInt(ttp);
+		int w = sr1.nextInt(ttp);
 
 		int alpha1 = Crypto.sr.nextInt(ttp);
 		int u1 = alpha1 ^ v;
 		byte[] pstar = Util.xor(p, Util.xorRotate(a, u1, ttp, l));
 
-		timer.start(pid, M.offline_write);
-		con1.write(p);
-		con1.write(a);
-		con1.write(b);
-		con1.write(v);
-		con1.write(w);
-		con2.write(u1);
-		con2.write(pstar);
-		timer.stop(pid, M.offline_write);
+		timer.start(M.offline_write);
+		con2.write(offline_band, u1);
+		con2.write(offline_band, pstar);
+		timer.stop(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);
 
 		int m = dN1 ^ alpha1;
 
-		timer.start(pid, M.online_write);
-		con1.write(pid, m);
-		timer.stop(pid, M.online_write);
+		timer.start(M.online_write);
+		con1.write(online_band, m);
+		timer.stop(M.online_write);
 
-		timer.start(pid, M.online_read);
-		m = con1.readInt(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		m = con1.readIntAndDec();
+		timer.stop(M.online_read);
 
 		int beta1 = m ^ dN1;
 
@@ -71,50 +78,48 @@ public class InsLbl extends Protocol {
 			b[index * l + i] = (byte) (b[index * l + i] ^ L1[i]);
 		}
 
-		timer.start(pid, M.online_write);
-		con2.write(pid, b);
-		timer.stop(pid, M.online_write);
+		timer.start(M.online_write);
+		con2.write(online_band, b);
+		timer.stop(M.online_write);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return;
 	}
 
-	public byte[] runP2(PreData predata, int dN2, byte[] L2, int ttp, Timer timer) {
-		timer.start(pid, M.offline_comp);
+	public byte[] runP2(int dN2, byte[] L2, int ttp) {
+		timer.start(M.offline_comp);
 
 		int l = L2.length;
 
-		timer.start(pid, M.offline_read);
-		byte[] p = con1.read();
-		byte[] a = con1.read();
-		byte[] b = con1.read();
-		int v = con1.readInt();
-		int w = con1.readInt();
-		timer.stop(pid, M.offline_read);
+		byte[] p = Util.nextBytes(ttp * l, sr1);
+		byte[] a = Util.nextBytes(ttp * l, sr1);
+		byte[] b = Util.nextBytes(ttp * l, sr1);
+		int v = sr1.nextInt(ttp);
+		int w = sr1.nextInt(ttp);
 
 		int beta2 = Crypto.sr.nextInt(ttp);
 		int u2 = beta2 ^ w;
 		byte[] z2 = Util.xor(p, Util.xorRotate(b, u2, ttp, l));
 
-		timer.start(pid, M.offline_write);
-		con2.write(u2);
-		timer.stop(pid, M.offline_write);
+		timer.start(M.offline_write);
+		con2.write(offline_band, u2);
+		timer.stop(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);
 
 		int m = beta2 ^ dN2;
 
-		timer.start(pid, M.online_write);
-		con1.write(pid, m);
-		timer.stop(pid, M.online_write);
+		timer.start(M.online_write);
+		con1.write(online_band, m);
+		timer.stop(M.online_write);
 
-		timer.start(pid, M.online_read);
-		m = con1.readInt(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		m = con1.readIntAndDec();
+		timer.stop(M.online_read);
 
 		int alpha2 = m ^ dN2;
 
@@ -123,49 +128,46 @@ public class InsLbl extends Protocol {
 			a[index * l + i] = (byte) (a[index * l + i] ^ L2[i]);
 		}
 
-		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 z2;
 	}
 
-	public byte[] runP3(PreData predata, int ttp, int l, Timer timer) {
-		timer.start(pid, M.offline_comp);
+	public byte[] runP3(int ttp, int l) {
+		timer.start(M.offline_comp);
 
-		timer.start(pid, M.offline_read);
-		int u1 = con1.readInt();
-		byte[] pstar = con1.read();
-		int u2 = con2.readInt();
-		timer.stop(pid, M.offline_read);
+		timer.start(M.offline_read);
+		int u1 = con1.readIntAndDec();
+		byte[] pstar = con1.readAndDec();
+		int u2 = con2.readIntAndDec();
+		timer.stop(M.offline_read);
 
-		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[] s1 = con1.read(pid);
-		byte[] s2 = con2.read(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		byte[] s1 = con1.readAndDec();
+		byte[] s2 = con2.readAndDec();
+		timer.stop(M.online_read);
 
 		s2 = Util.xorRotate(s2, u1, ttp, l);
 		s1 = Util.xorRotate(s1, u2, ttp, l);
 		Util.setXor(pstar, s1);
 		Util.setXor(pstar, s2);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return pstar;
 	}
 
 	@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 ttp = (int) Math.pow(2, 8);
 			int l = 10;
@@ -175,12 +177,16 @@ public class InsLbl extends Protocol {
 			byte[] L2 = Util.nextBytes(l, Crypto.sr);
 
 			if (party == Party.Eddie) {
-				this.runP1(predata, dN1, L1, ttp, timer);
+				this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CE);
+
+				this.runP1(dN1, L1, ttp);
 				con1.write(dN1);
 				con1.write(L1);
 
 			} else if (party == Party.Debbie) {
-				byte[] m1 = this.runP2(predata, dN2, L2, ttp, timer);
+				this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CD);
+
+				byte[] m1 = this.runP2(dN2, L2, ttp);
 				byte[] m2 = con2.read();
 				dN1 = con1.readInt();
 				L1 = con1.read();
@@ -212,7 +218,9 @@ public class InsLbl extends Protocol {
 					System.out.println(j + ": InsLbl test passed");
 
 			} else if (party == Party.Charlie) {
-				byte[] m2 = this.runP3(predata, ttp, l, timer);
+				this.reinit(con1, con2, Crypto.sr_CE, Crypto.sr_CD);
+
+				byte[] m2 = this.runP3(ttp, l);
 				con2.write(m2);
 
 			} else {

+ 70 - 64
src/pir/ULiT.java

@@ -1,5 +1,6 @@
 package pir;
 
+import java.security.SecureRandom;
 import java.util.Arrays;
 
 import communication.Communication;
@@ -11,50 +12,59 @@ import oram.Tuple;
 import protocols.Protocol;
 import protocols.struct.OutULiT;
 import protocols.struct.Party;
-import protocols.struct.PreData;
 import protocols.struct.TwoThreeXorByte;
 import protocols.struct.TwoThreeXorInt;
 import util.M;
-import util.P;
-import util.Timer;
 import util.Util;
 
 public class ULiT extends Protocol {
 
-	private int pid = P.ULiT;
+	SecureRandom sr1;
+	SecureRandom sr2;
 
 	public ULiT(Communication con1, Communication con2) {
 		super(con1, con2);
 	}
 
-	public OutULiT runE(PreData predata, TwoThreeXorByte X, TwoThreeXorByte N, TwoThreeXorInt dN, TwoThreeXorByte Lp,
-			TwoThreeXorByte Lpi, TwoThreeXorByte Li, int ttp, Timer timer) {
-		timer.start(pid, M.offline_comp);
+	public ULiT(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 OutULiT runE(TwoThreeXorByte X, TwoThreeXorByte N, TwoThreeXorInt dN, TwoThreeXorByte Lp,
+			TwoThreeXorByte Lpi, TwoThreeXorByte Li, int ttp) {
+		timer.start(M.offline_comp);
 
 		int l = Li.CE.length;
 
-		timer.start(pid, M.offline_read);
-		byte[] x2 = con1.read();
-		timer.stop(pid, M.offline_read);
+		byte[] x2 = Util.nextBytes(X.CD.length, sr1);
 
-		timer.stop(pid, M.offline_comp);
+		timer.stop(M.offline_comp);
 
 		// ----------------------------------------- //
 
-		timer.start(pid, M.online_comp);
+		timer.start(M.online_comp);
 
 		int dN_E = dN.CE;
 		byte[] xorLi_E = Util.xor(Lpi.CE, Li.CE);
 
-		InsLbl inslbl = new InsLbl(con1, con2);
-		inslbl.runP1(predata, dN_E, xorLi_E, ttp, timer);
+		InsLbl inslbl = new InsLbl(con1, con2, sr1, sr2);
+		inslbl.runP1(dN_E, xorLi_E, ttp);
 
-		inslbl = new InsLbl(con2, con1);
-		byte[] b1 = inslbl.runP3(predata, ttp, l, timer);
+		inslbl.reinit(con2, con1, sr2, sr1);
+		byte[] b1 = inslbl.runP3(ttp, l);
 
-		timer.start(pid, M.online_read);
-		byte[] me = con1.read(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		byte[] me = con1.readAndDec();
+		timer.stop(M.online_read);
 
 		byte[] x3 = Util.xor(me, b1);
 
@@ -65,46 +75,41 @@ public class ULiT extends Protocol {
 		out.CE = new Tuple(new byte[] { 1 }, N.CE, Lp.CE, X.CE);
 		out.DE = new Tuple(new byte[] { 1 }, N.DE, Lp.DE, X.DE);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return out;
 	}
 
-	public OutULiT runD(PreData predata, TwoThreeXorByte X, TwoThreeXorByte N, TwoThreeXorInt dN, TwoThreeXorByte Lp,
-			TwoThreeXorByte Lpi, TwoThreeXorByte Li, int ttp, Timer timer) {
-		timer.start(pid, M.offline_comp);
-
-		byte[] x1 = Util.nextBytes(X.CD.length, Crypto.sr);
-		byte[] x2 = Util.nextBytes(X.CD.length, Crypto.sr);
+	public OutULiT runD(TwoThreeXorByte X, TwoThreeXorByte N, TwoThreeXorInt dN, TwoThreeXorByte Lp,
+			TwoThreeXorByte Lpi, TwoThreeXorByte Li, int ttp) {
+		timer.start(M.offline_comp);
 
-		timer.start(pid, M.offline_write);
-		con2.write(x1);
-		con1.write(x2);
-		timer.stop(pid, M.offline_write);
+		byte[] x1 = Util.nextBytes(X.CD.length, sr2);
+		byte[] x2 = Util.nextBytes(X.CD.length, sr1);
 
-		timer.stop(pid, M.offline_comp);
+		timer.stop(M.offline_comp);
 
 		// ----------------------------------------- //
 
-		timer.start(pid, M.online_comp);
+		timer.start(M.online_comp);
 
 		int dN_D = dN.CD ^ dN.DE;
 		byte[] xorLi_D = Util.xor(Util.xor(Lpi.CD, Li.CD), Util.xor(Lpi.DE, Li.DE));
 
-		InsLbl inslbl = new InsLbl(con1, con2);
-		byte[] a2 = inslbl.runP2(predata, dN_D, xorLi_D, ttp, timer);
+		InsLbl inslbl = new InsLbl(con1, con2, sr1, sr2);
+		byte[] a2 = inslbl.runP2(dN_D, xorLi_D, ttp);
 
-		inslbl = new InsLbl(con2, con1);
-		byte[] a1 = inslbl.runP2(predata, dN_D, xorLi_D, ttp, timer);
+		inslbl.reinit(con2, con1, sr2, sr1);
+		byte[] a1 = inslbl.runP2(dN_D, xorLi_D, ttp);
 
 		Util.setXor(a1, x1);
 		Util.setXor(a1, x2);
 		Util.setXor(a2, x1);
 		Util.setXor(a2, x2);
 
-		timer.start(pid, M.online_write);
-		con1.write(pid, a1);
-		con2.write(pid, a2);
-		timer.stop(pid, M.online_write);
+		timer.start(M.online_write);
+		con1.write(online_band, a1);
+		con2.write(online_band, a2);
+		timer.stop(M.online_write);
 
 		Util.setXor(X.CD, x1);
 		Util.setXor(X.DE, x2);
@@ -113,38 +118,36 @@ public class ULiT extends Protocol {
 		out.CD = new Tuple(new byte[] { 1 }, N.CD, Lp.CD, X.CD);
 		out.DE = new Tuple(new byte[] { 1 }, N.DE, Lp.DE, X.DE);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return out;
 	}
 
-	public OutULiT runC(PreData predata, TwoThreeXorByte X, TwoThreeXorByte N, TwoThreeXorInt dN, TwoThreeXorByte Lp,
-			TwoThreeXorByte Lpi, TwoThreeXorByte Li, int ttp, Timer timer) {
-		timer.start(pid, M.offline_comp);
+	public OutULiT runC(TwoThreeXorByte X, TwoThreeXorByte N, TwoThreeXorInt dN, TwoThreeXorByte Lp,
+			TwoThreeXorByte Lpi, TwoThreeXorByte Li, int ttp) {
+		timer.start(M.offline_comp);
 
 		int l = Li.CE.length;
 
-		timer.start(pid, M.offline_read);
-		byte[] x1 = con2.read();
-		timer.stop(pid, M.offline_read);
+		byte[] x1 = Util.nextBytes(X.CD.length, sr2);
 
-		timer.stop(pid, M.offline_comp);
+		timer.stop(M.offline_comp);
 
 		// ----------------------------------------- //
 
-		timer.start(pid, M.online_comp);
-
-		InsLbl inslbl = new InsLbl(con1, con2);
-		byte[] b2 = inslbl.runP3(predata, ttp, l, timer);
+		timer.start(M.online_comp);
 
 		int dN_C = dN.CE;
 		byte[] xorLi_C = Util.xor(Lpi.CE, Li.CE);
 
-		inslbl = new InsLbl(con2, con1);
-		inslbl.runP1(predata, dN_C, xorLi_C, ttp, timer);
+		InsLbl inslbl = new InsLbl(con1, con2, sr1, sr2);
+		byte[] b2 = inslbl.runP3(ttp, l);
+
+		inslbl.reinit(con2, con1, sr2, sr1);
+		inslbl.runP1(dN_C, xorLi_C, ttp);
 
-		timer.start(pid, M.online_read);
-		byte[] mc = con2.read(pid);
-		timer.stop(pid, M.online_read);
+		timer.start(M.online_read);
+		byte[] mc = con2.readAndDec();
+		timer.stop(M.online_read);
 
 		byte[] x3 = Util.xor(mc, b2);
 
@@ -155,16 +158,13 @@ public class ULiT extends Protocol {
 		out.CD = new Tuple(new byte[] { 1 }, N.CD, Lp.CD, X.CD);
 		out.CE = new Tuple(new byte[] { 1 }, N.CE, Lp.CE, X.CE);
 
-		timer.stop(pid, M.online_comp);
+		timer.stop(M.online_comp);
 		return out;
 	}
 
 	@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 ttp = (int) Math.pow(2, 8);
 			int l = 10;
@@ -206,6 +206,8 @@ public class ULiT extends Protocol {
 			Util.setXor(Li.CE, Li.DE);
 
 			if (party == Party.Eddie) {
+				this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CE);
+
 				con1.write(X.CD);
 				con1.write(X.DE);
 				con1.write(N.CD);
@@ -232,7 +234,7 @@ public class ULiT extends Protocol {
 				con2.write(dN.CD);
 				con2.write(dN.CE);
 
-				OutULiT out = this.runE(predata, X, N, dN, Lp, Lpi, Li, ttp, timer);
+				OutULiT out = this.runE(X, N, dN, Lp, Lpi, Li, ttp);
 				out.CD = con1.readTuple();
 				Tuple T = out.CD.xor(out.CE);
 				T.setXor(out.DE);
@@ -280,6 +282,8 @@ public class ULiT extends Protocol {
 					System.out.println(j + ": ULiT test passed");
 
 			} else if (party == Party.Debbie) {
+				this.reinit(con1, con2, Crypto.sr_DE, Crypto.sr_CD);
+
 				X.CD = con1.read();
 				X.DE = con1.read();
 				N.CD = con1.read();
@@ -293,10 +297,12 @@ public class ULiT extends Protocol {
 				dN.CD = con1.readInt();
 				dN.DE = con1.readInt();
 
-				OutULiT out = this.runD(predata, X, N, dN, Lp, Lpi, Li, ttp, timer);
+				OutULiT out = this.runD(X, N, dN, Lp, Lpi, Li, ttp);
 				con1.write(out.CD);
 
 			} else if (party == Party.Charlie) {
+				this.reinit(con1, con2, Crypto.sr_CE, Crypto.sr_CD);
+
 				X.CD = con1.read();
 				X.CE = con1.read();
 				N.CD = con1.read();
@@ -310,7 +316,7 @@ public class ULiT extends Protocol {
 				dN.CD = con1.readInt();
 				dN.CE = con1.readInt();
 
-				this.runC(predata, X, N, dN, Lp, Lpi, Li, ttp, timer);
+				this.runC(X, N, dN, Lp, Lpi, Li, ttp);
 
 			} else {
 				throw new NoSuchPartyException(party + "");