Explorar el Código

Add 3ShiftPIR implementation

Boyoung- hace 6 años
padre
commit
4849f9a64b

+ 1 - 1
src/pir/ShiftPIR.java

@@ -15,7 +15,7 @@ import util.Util;
 
 public class ShiftPIR extends Protocol {
 
-	private int pid = P.ShiftPIR;
+	private int pid = P.SftPIR;
 
 	public ShiftPIR(Communication con1, Communication con2) {
 		super(con1, con2);

+ 151 - 0
src/pir/ThreeShiftPIR.java

@@ -0,0 +1,151 @@
+package pir;
+
+import communication.Communication;
+import crypto.Crypto;
+import exceptions.NoSuchPartyException;
+import oram.Forest;
+import oram.Metadata;
+import protocols.Protocol;
+import protocols.struct.OutPIRCOT;
+import protocols.struct.Party;
+import protocols.struct.PreData;
+import util.M;
+import util.P;
+import util.Timer;
+import util.Util;
+
+public class ThreeShiftPIR extends Protocol {
+
+	private int pid = P.TSPIR;
+
+	public ThreeShiftPIR(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	public byte[] runE(PreData predata, byte[][] x_DE, byte[][] x_CE, OutPIRCOT i, Timer timer) {
+		timer.start(pid, 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);
+		Util.setXor(e1, e2);
+
+		timer.stop(pid, M.online_comp);
+		return e1;
+	}
+
+	public byte[] runD(PreData predata, byte[][] x_DE, byte[][] x_CD, OutPIRCOT i, Timer timer) {
+		timer.start(pid, 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);
+		Util.setXor(d1, d2);
+
+		timer.stop(pid, M.online_comp);
+		return d1;
+	}
+
+	public byte[] runC(PreData predata, byte[][] x_CD, byte[][] x_CE, OutPIRCOT i, Timer timer) {
+		timer.start(pid, 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);
+		Util.setXor(c1, c2);
+
+		timer.stop(pid, M.online_comp);
+		return c1;
+	}
+
+	@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;
+			byte[][] x_CD = new byte[l][m];
+			byte[][] x_CE = new byte[l][m];
+			byte[][] x_DE = new byte[l][m];
+			for (int i = 0; i < l; i++) {
+				Crypto.sr.nextBytes(x_CD[i]);
+				Crypto.sr.nextBytes(x_DE[i]);
+				Crypto.sr.nextBytes(x_CE[i]);
+			}
+			int index = Crypto.sr.nextInt(l);
+			OutPIRCOT ks = new OutPIRCOT();
+			ks.t_C = Crypto.sr.nextInt(l);
+			ks.t_D = Crypto.sr.nextInt(l);
+			ks.t_E = Crypto.sr.nextInt(l);
+			ks.s_DE = (index - ks.t_C + l) % l;
+			ks.s_CE = (index - ks.t_D + l) % l;
+			ks.s_CD = (index - ks.t_E + l) % l;
+
+			if (party == Party.Eddie) {
+				con1.write(x_CD);
+				con1.write(x_DE);
+				con2.write(x_CD);
+				con2.write(x_CE);
+				con1.write(ks.t_D);
+				con1.write(ks.s_DE);
+				con1.write(ks.s_CD);
+				con2.write(ks.t_C);
+				con2.write(ks.s_CE);
+				con2.write(ks.s_CD);
+
+				byte[] e = this.runE(predata, x_DE, x_CE, ks, timer);
+				byte[] d = con1.read();
+				byte[] c = con2.read();
+				Util.setXor(e, d);
+				Util.setXor(e, c);
+				byte[] x = x_DE[index];
+				Util.setXor(x, x_CE[index]);
+				Util.setXor(x, x_CD[index]);
+
+				if (!Util.equal(x, e))
+					System.err.println(j + ": 3ShiftPIR test failed");
+				else
+					System.out.println(j + ": 3ShiftPIR test passed");
+
+			} else if (party == Party.Debbie) {
+				x_CD = con1.readDoubleByteArray();
+				x_DE = con1.readDoubleByteArray();
+				ks.t_D = con1.readInt();
+				ks.s_DE = con1.readInt();
+				ks.s_CD = con1.readInt();
+
+				byte[] d = this.runD(predata, x_DE, x_CD, ks, timer);
+				con1.write(d);
+
+			} else if (party == Party.Charlie) {
+				x_CD = con1.readDoubleByteArray();
+				x_CE = con1.readDoubleByteArray();
+				ks.t_C = con1.readInt();
+				ks.s_CE = con1.readInt();
+				ks.s_CD = con1.readInt();
+
+				byte[] c = this.runC(predata, x_CD, x_CE, ks, timer);
+				con1.write(c);
+
+			} else {
+				throw new NoSuchPartyException(party + "");
+			}
+		}
+	}
+
+	@Override
+	public void run(Party party, Metadata md, Forest forest) {
+	}
+}

+ 2 - 0
src/ui/CLI.java

@@ -107,6 +107,8 @@ public class CLI {
 			operation = SSPIR.class;
 		} else if (protocol.equals("shiftpir")) {
 			operation = ShiftPIR.class;
+		} else if (protocol.equals("tspir")) {
+			operation = ThreeShiftPIR.class;
 
 		} else {
 			System.out.println("Protocol " + protocol + " not supported");

+ 3 - 2
src/util/P.java

@@ -14,9 +14,10 @@ public class P {
 	public static final int PI = 9;
 	public static final int XOT = 10;
 	public static final int SSPIR = 11;
-	public static final int ShiftPIR = 12;
+	public static final int SftPIR = 12;
+	public static final int TSPIR = 13;
 
 	public static final String[] names = { "ACC", "COT", "IOT", "PPT", "RSF", "UR", "URXOT", "EVI", "PT", "PI", "XOT",
-			"SSPIR", "ShiftPIR" };
+			"SSPIR", "SftPIR", "TSPIR" };
 	public static final int size = names.length;
 }

+ 50 - 0
test/pir/TestThreeShiftPIR_C.java

@@ -0,0 +1,50 @@
+package pir;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Arrays;
+
+public class TestThreeShiftPIR_C {
+
+	public static void main(String[] args) {
+		Runtime runTime = Runtime.getRuntime();
+		Process process = null;
+		String dir = System.getProperty("user.dir");
+		String binDir = dir + "\\bin";
+		String libs = dir + "\\lib\\*";
+		try {
+			process = runTime.exec("java -classpath " + binDir + ";" + libs + " ui.CLI -protocol tspir charlie");
+
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		InputStream inputStream = process.getInputStream();
+		InputStreamReader isr = new InputStreamReader(inputStream);
+		InputStream errorStream = process.getErrorStream();
+		InputStreamReader esr = new InputStreamReader(errorStream);
+
+		System.out.println("STANDARD OUTPUT:");
+		int n1;
+		char[] c1 = new char[1024];
+		try {
+			while ((n1 = isr.read(c1)) > 0) {
+				System.out.print(new String(Arrays.copyOfRange(c1, 0, n1)));
+			}
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+
+		System.out.println("STANDARD ERROR:");
+		int n2;
+		char[] c2 = new char[1024];
+		try {
+			while ((n2 = esr.read(c2)) > 0) {
+				System.err.print(new String(Arrays.copyOfRange(c2, 0, n2)));
+			}
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+}

+ 50 - 0
test/pir/TestThreeShiftPIR_D.java

@@ -0,0 +1,50 @@
+package pir;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Arrays;
+
+public class TestThreeShiftPIR_D {
+
+	public static void main(String[] args) {
+		Runtime runTime = Runtime.getRuntime();
+		Process process = null;
+		String dir = System.getProperty("user.dir");
+		String binDir = dir + "\\bin";
+		String libs = dir + "\\lib\\*";
+		try {
+			process = runTime.exec("java -classpath " + binDir + ";" + libs + " ui.CLI -protocol tspir debbie");
+
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		InputStream inputStream = process.getInputStream();
+		InputStreamReader isr = new InputStreamReader(inputStream);
+		InputStream errorStream = process.getErrorStream();
+		InputStreamReader esr = new InputStreamReader(errorStream);
+
+		System.out.println("STANDARD OUTPUT:");
+		int n1;
+		char[] c1 = new char[1024];
+		try {
+			while ((n1 = isr.read(c1)) > 0) {
+				System.out.print(new String(Arrays.copyOfRange(c1, 0, n1)));
+			}
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+
+		System.out.println("STANDARD ERROR:");
+		int n2;
+		char[] c2 = new char[1024];
+		try {
+			while ((n2 = esr.read(c2)) > 0) {
+				System.err.print(new String(Arrays.copyOfRange(c2, 0, n2)));
+			}
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+}

+ 50 - 0
test/pir/TestThreeShiftPIR_E.java

@@ -0,0 +1,50 @@
+package pir;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Arrays;
+
+public class TestThreeShiftPIR_E {
+
+	public static void main(String[] args) {
+		Runtime runTime = Runtime.getRuntime();
+		Process process = null;
+		String dir = System.getProperty("user.dir");
+		String binDir = dir + "\\bin";
+		String libs = dir + "\\lib\\*";
+		try {
+			process = runTime.exec("java -classpath " + binDir + ";" + libs + " ui.CLI -protocol tspir eddie");
+
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		InputStream inputStream = process.getInputStream();
+		InputStreamReader isr = new InputStreamReader(inputStream);
+		InputStream errorStream = process.getErrorStream();
+		InputStreamReader esr = new InputStreamReader(errorStream);
+
+		System.out.println("STANDARD OUTPUT:");
+		int n1;
+		char[] c1 = new char[1024];
+		try {
+			while ((n1 = isr.read(c1)) > 0) {
+				System.out.print(new String(Arrays.copyOfRange(c1, 0, n1)));
+			}
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+
+		System.out.println("STANDARD ERROR:");
+		int n2;
+		char[] c2 = new char[1024];
+		try {
+			while ((n2 = esr.read(c2)) > 0) {
+				System.err.print(new String(Arrays.copyOfRange(c2, 0, n2)));
+			}
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+}