浏览代码

add 3ShiftXorPIR impl

Boyoung- 6 年之前
父节点
当前提交
4737bc79d1

+ 215 - 0
src/pir/ThreeShiftXorPIR.java

@@ -0,0 +1,215 @@
+package pir;
+
+import java.util.Arrays;
+
+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 protocols.struct.TwoOneXor;
+import util.M;
+import util.P;
+import util.Timer;
+import util.Util;
+
+public class ThreeShiftXorPIR extends Protocol {
+
+	private int pid = P.TSXPIR;
+
+	public ThreeShiftXorPIR(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	public byte[] runE(PreData predata, byte[][] x_DE, byte[][] x_CE, OutPIRCOT i, TwoOneXor j, int m, Timer timer) {
+		timer.start(pid, M.online_comp);
+
+		ShiftXorPIR sftpir = new ShiftXorPIR(con1, con2);
+		byte[] e1 = sftpir.runP1(predata, x_DE, i.s_DE, j.s_DE, m, timer);
+		sftpir = new ShiftXorPIR(con2, con1);
+		byte[] e2 = sftpir.runP2(predata, x_CE, i.s_CE, j.s_CE, m, timer);
+		sftpir = new ShiftXorPIR(con1, con2);
+		sftpir.runP3(predata, i.t_E, j.t_E, m, timer);
+		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(pid, M.online_read);
+		byte[] d = con1.read(pid);
+		byte[] c = con2.read(pid);
+		timer.stop(pid, M.online_read);
+
+		Util.setXor(e1, d);
+		Util.setXor(e1, c);
+
+		timer.stop(pid, M.online_comp);
+		return e1;
+	}
+
+	public byte[] runD(PreData predata, byte[][] x_DE, byte[][] x_CD, OutPIRCOT i, TwoOneXor j, int m, Timer timer) {
+		timer.start(pid, M.online_comp);
+
+		ShiftXorPIR sftpir = new ShiftXorPIR(con1, con2);
+		byte[] d1 = sftpir.runP2(predata, x_DE, i.s_DE, j.s_DE, m, timer);
+		sftpir = new ShiftXorPIR(con2, con1);
+		sftpir.runP3(predata, i.t_D, j.t_D, m, timer);
+		sftpir = new ShiftXorPIR(con2, con1);
+		byte[] d2 = sftpir.runP1(predata, x_CD, i.s_CD, j.s_CD, m, timer);
+		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(pid, M.online_read);
+		byte[] e = con1.read(pid);
+		byte[] c = con2.read(pid);
+		timer.stop(pid, M.online_read);
+
+		Util.setXor(d1, e);
+		Util.setXor(d1, c);
+
+		timer.stop(pid, M.online_comp);
+		return d1;
+	}
+
+	public byte[] runC(PreData predata, byte[][] x_CD, byte[][] x_CE, OutPIRCOT i, TwoOneXor j, int m, Timer timer) {
+		timer.start(pid, M.online_comp);
+
+		ShiftXorPIR sftpir = new ShiftXorPIR(con1, con2);
+		sftpir.runP3(predata, i.t_C, j.t_C, m, timer);
+		sftpir = new ShiftXorPIR(con1, con2);
+		byte[] c1 = sftpir.runP1(predata, x_CE, i.s_CE, j.s_CE, m, timer);
+		sftpir = new ShiftXorPIR(con2, con1);
+		byte[] c2 = sftpir.runP2(predata, x_CD, i.s_CD, j.s_CD, m, timer);
+		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(pid, M.online_read);
+		byte[] e = con1.read(pid);
+		byte[] d = con2.read(pid);
+		timer.stop(pid, M.online_read);
+
+		Util.setXor(c1, e);
+		Util.setXor(c1, d);
+
+		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 n = 500;
+			int m = 16;
+			int l = 4;
+			byte[][] x_CD = new byte[n][m * l];
+			byte[][] x_CE = new byte[n][m * l];
+			byte[][] x_DE = new byte[n][m * l];
+			for (int i = 0; i < n; i++) {
+				Crypto.sr.nextBytes(x_CD[i]);
+				Crypto.sr.nextBytes(x_DE[i]);
+				Crypto.sr.nextBytes(x_CE[i]);
+			}
+			int i1 = Crypto.sr.nextInt(n);
+			OutPIRCOT ks = new OutPIRCOT();
+			ks.t_C = Crypto.sr.nextInt(n);
+			ks.t_D = Crypto.sr.nextInt(n);
+			ks.t_E = Crypto.sr.nextInt(n);
+			ks.s_DE = (i1 - ks.t_C + n) % n;
+			ks.s_CE = (i1 - ks.t_D + n) % n;
+			ks.s_CD = (i1 - ks.t_E + n) % n;
+
+			int i2 = Crypto.sr.nextInt(m);
+			TwoOneXor tox = new TwoOneXor();
+			tox.t_C = Crypto.sr.nextInt(m);
+			tox.t_D = Crypto.sr.nextInt(m);
+			tox.t_E = Crypto.sr.nextInt(m);
+			tox.s_DE = i2 ^ tox.t_C;
+			tox.s_CE = i2 ^ tox.t_D;
+			tox.s_CD = i2 ^ tox.t_E;
+
+			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);
+				con1.write(tox.t_D);
+				con1.write(tox.s_DE);
+				con1.write(tox.s_CD);
+				con2.write(tox.t_C);
+				con2.write(tox.s_CE);
+				con2.write(tox.s_CD);
+
+				byte[] e = this.runE(predata, x_DE, x_CE, ks, tox, m, timer);
+				byte[] d = con1.read();
+				byte[] c = con2.read();
+
+				byte[] x = x_DE[i1];
+				Util.setXor(x, x_CE[i1]);
+				Util.setXor(x, x_CD[i1]);
+				byte[] expect = Arrays.copyOfRange(x, i2 * l, (i2 + 1) * l);
+
+				if (!Util.equal(expect, e) || !Util.equal(expect, d) || !Util.equal(expect, c))
+					System.err.println(j + ": 3ShiftXorPIR test failed");
+				else
+					System.out.println(j + ": 3ShiftXorPIR 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();
+				tox.t_D = con1.readInt();
+				tox.s_DE = con1.readInt();
+				tox.s_CD = con1.readInt();
+
+				byte[] d = this.runD(predata, x_DE, x_CD, ks, tox, m, 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();
+				tox.t_C = con1.readInt();
+				tox.s_CE = con1.readInt();
+				tox.s_CD = con1.readInt();
+
+				byte[] c = this.runC(predata, x_CD, x_CE, ks, tox, m, timer);
+				con1.write(c);
+
+			} else {
+				throw new NoSuchPartyException(party + "");
+			}
+		}
+	}
+
+	@Override
+	public void run(Party party, Metadata md, Forest forest) {
+	}
+}

+ 10 - 0
src/protocols/struct/TwoOneXor.java

@@ -0,0 +1,10 @@
+package protocols.struct;
+
+public class TwoOneXor {
+	public int t_C;
+	public int t_D;
+	public int t_E;
+	public int s_CE;
+	public int s_DE;
+	public int s_CD;
+}

+ 2 - 0
src/ui/CLI.java

@@ -111,6 +111,8 @@ public class CLI {
 			operation = ThreeShiftPIR.class;
 		} else if (protocol.equals("shiftxorpir")) {
 			operation = ShiftXorPIR.class;
+		} else if (protocol.equals("tsxpir")) {
+			operation = ThreeShiftXorPIR.class;
 
 		} else {
 			System.out.println("Protocol " + protocol + " not supported");

+ 2 - 1
src/util/P.java

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

+ 50 - 0
test/pir/TestThreeShiftXorPIR_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 TestThreeShiftXorPIR_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 tsxpir 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/TestThreeShiftXorPIR_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 TestThreeShiftXorPIR_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 tsxpir 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/TestThreeShiftXorPIR_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 TestThreeShiftXorPIR_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 tsxpir 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();
+		}
+	}
+
+}