Browse Source

add SSPIR implementation

Boyang Wei 7 years ago
parent
commit
b4f70aef6f
7 changed files with 311 additions and 1 deletions
  1. 145 0
      src/pir/SSPIR.java
  2. 2 0
      src/ui/CLI.java
  3. 3 1
      src/util/P.java
  4. 11 0
      src/util/Util.java
  5. 50 0
      test/pir/TestSSPIR_C.java
  6. 50 0
      test/pir/TestSSPIR_D.java
  7. 50 0
      test/pir/TestSSPIR_E.java

+ 145 - 0
src/pir/SSPIR.java

@@ -0,0 +1,145 @@
+package pir;
+
+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 SSPIR extends Protocol {
+
+	private int pid = P.SSPIR;
+
+	public SSPIR(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	public byte[] runP1(PreData predata, byte[][] x, Timer timer) {
+		timer.start(pid, M.offline_comp);
+
+		int l = x.length;
+		int m = x[0].length;
+		byte[] a1 = new byte[l];
+		byte[] r = new byte[m];
+		Crypto.sr.nextBytes(a1);
+		Crypto.sr.nextBytes(r);
+
+		timer.start(pid, M.offline_write);
+		con2.write(a1);
+		con1.write(r);
+		timer.stop(pid, M.offline_write);
+
+		timer.stop(pid, M.offline_comp);
+
+		// ----------------------------------------- //
+
+		timer.start(pid, M.online_comp);
+
+		byte[] z = Util.xorSelect(x, a1);
+		Util.setXor(z, r);
+
+		timer.stop(pid, M.online_comp);
+		return z;
+	}
+
+	public byte[] runP2(PreData predata, byte[][] x, Timer timer) {
+		timer.start(pid, M.offline_comp);
+
+		timer.start(pid, M.offline_read);
+		byte[] r = con1.read();
+		timer.stop(pid, M.offline_read);
+
+		timer.stop(pid, M.offline_comp);
+
+		// ----------------------------------------- //
+
+		timer.start(pid, M.online_comp);
+
+		timer.start(pid, M.online_read);
+		byte[] a2 = con2.read(pid);
+		timer.stop(pid, M.online_read);
+
+		byte[] z = Util.xorSelect(x, a2);
+		Util.setXor(z, r);
+
+		timer.stop(pid, M.online_comp);
+		return z;
+	}
+
+	public void runP3(PreData predata, int t, Timer timer) {
+		timer.start(pid, M.offline_comp);
+
+		timer.start(pid, M.offline_read);
+		byte[] a = con1.read();
+		timer.stop(pid, M.offline_read);
+
+		timer.stop(pid, M.offline_comp);
+
+		// ----------------------------------------- //
+
+		timer.start(pid, M.online_comp);
+
+		a[t] = (byte) (a[t] ^ 1);
+
+		timer.start(pid, M.online_write);
+		con2.write(pid, a);
+		timer.stop(pid, M.online_write);
+
+		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 l = 100;
+			int m = 50;
+			byte[][] x = new byte[l][m];
+			for (int i = 0; i < l; i++) {
+				Crypto.sr.nextBytes(x[i]);
+			}
+
+			if (party == Party.Eddie) {
+				con1.write(x);
+				byte[] out = this.runP1(predata, x, timer);
+				con2.write(out);
+				con2.write(x);
+
+			} else if (party == Party.Debbie) {
+				x = con1.readDoubleByteArray();
+				byte[] out = this.runP2(predata, x, timer);
+				con2.write(out);
+
+			} else if (party == Party.Charlie) {
+				int index = Crypto.sr.nextInt(l);
+				this.runP3(predata, index, timer);
+				byte[] out1 = con1.read();
+				x = con1.readDoubleByteArray();
+				byte[] out2 = con2.read();
+				Util.setXor(out1, out2);
+
+				if (!Util.equal(out1, x[index]))
+					System.err.println(j + ": SSPIR test failed");
+				else
+					System.out.println(j + ": SSPIR test passed");
+
+			} else {
+				throw new NoSuchPartyException(party + "");
+			}
+		}
+	}
+
+	@Override
+	public void run(Party party, Metadata md, Forest forest) {
+	}
+}

+ 2 - 0
src/ui/CLI.java

@@ -103,6 +103,8 @@ public class CLI {
 			operation = PIRRetrieve.class;
 		} else if (protocol.equals("pirrsf")) {
 			operation = PIRReshuffle.class;
+		} else if (protocol.equals("sspir")) {
+			operation = SSPIR.class;
 
 		} else {
 			System.out.println("Protocol " + protocol + " not supported");

+ 3 - 1
src/util/P.java

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

+ 11 - 0
src/util/Util.java

@@ -199,6 +199,17 @@ public class Util {
 		}
 	}
 
+	public static byte[] xorSelect(byte[][] array, byte[] ind) {
+		assert array.length == ind.length;
+		byte[] out = new byte[array[0].length];
+		for (int i = 0; i < ind.length; i++) {
+			if ((ind[i] & 1) == 1) {
+				setXor(out, array[i]);
+			}
+		}
+		return out;
+	}
+
 	public static void debug(String s) {
 		// only to make Communication.java compile
 	}

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