Procházet zdrojové kódy

Add ShiftXorPIR impl

Boyoung- před 6 roky
rodič
revize
93bf8fdb01

+ 138 - 0
src/pir/ShiftXorPIR.java

@@ -0,0 +1,138 @@
+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.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;
+
+	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);
+
+		int n = x.length;
+		int l = x[0].length / m;
+
+		byte[][] xp = new byte[n * m][];
+		for (int i = 0; i < n; i++) {
+			for (int j = 0; j < m; j++) {
+				xp[i * m + j] = Arrays.copyOfRange(x[(i + s1) % n], (j ^ s2) * l, ((j ^ s2) + 1) * l);
+			}
+		}
+
+		SSPIR sspir = new SSPIR(con1, con2);
+		byte[] z = sspir.runP1(predata, xp, timer);
+
+		timer.stop(pid, 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);
+
+		int n = x.length;
+		int l = x[0].length / m;
+
+		byte[][] xp = new byte[n * m][];
+		for (int i = 0; i < n; i++) {
+			for (int j = 0; j < m; j++) {
+				xp[i * m + j] = Arrays.copyOfRange(x[(i + s1) % n], (j ^ s2) * l, ((j ^ s2) + 1) * l);
+			}
+		}
+
+		SSPIR sspir = new SSPIR(con1, con2);
+		byte[] z = sspir.runP2(predata, xp, timer);
+
+		timer.stop(pid, M.online_comp);
+		return z;
+	}
+
+	public void runP3(PreData predata, int t1, int t2, int m, Timer timer) {
+		timer.start(pid, M.online_comp);
+
+		int t = t1 * m + t2;
+
+		SSPIR sspir = new SSPIR(con1, con2);
+		sspir.runP3(predata, t, timer);
+
+		timer.stop(pid, 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;
+			int l = 4;
+			byte[][] x = new byte[n][m * l];
+			for (int i = 0; i < n; i++) {
+				Crypto.sr.nextBytes(x[i]);
+			}
+			int s1 = Crypto.sr.nextInt(n);
+			int t1 = Crypto.sr.nextInt(n);
+			int s2 = Crypto.sr.nextInt(m);
+			int t2 = Crypto.sr.nextInt(m);
+
+			if (party == Party.Eddie) {
+				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[] d = con1.read();
+				Util.setXor(e, d);
+
+				int i1 = (s1 + t1) % n;
+				int i2 = s2 ^ t2;
+				byte[] expect = Arrays.copyOfRange(x[i1], i2 * l, (i2 + 1) * l);
+
+				if (!Util.equal(e, expect))
+					System.err.println(j + ": ShiftXorPIR test failed");
+				else
+					System.out.println(j + ": ShiftXorPIR test passed");
+
+			} else if (party == Party.Debbie) {
+				x = con1.readDoubleByteArray();
+				s1 = con1.readInt();
+				s2 = con1.readInt();
+				byte[] d = this.runP2(predata, x, s1, s2, m, timer);
+
+				con1.write(d);
+
+			} else if (party == Party.Charlie) {
+				t1 = con1.readInt();
+				t2 = con1.readInt();
+				this.runP3(predata, t1, t2, m, timer);
+
+			} else {
+				throw new NoSuchPartyException(party + "");
+			}
+		}
+	}
+
+	@Override
+	public void run(Party party, Metadata md, Forest forest) {
+	}
+}

+ 2 - 0
src/ui/CLI.java

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

+ 2 - 1
src/util/P.java

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

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