Browse Source

add pir iot

Boyoung- 8 years ago
parent
commit
67e4e8ab7b

+ 139 - 0
src/pir/PIRIOT.java

@@ -0,0 +1,139 @@
+package pir;
+
+import communication.Communication;
+import crypto.Crypto;
+import exceptions.NoSuchPartyException;
+import exceptions.SSIOTException;
+import oram.Forest;
+import oram.Metadata;
+import precomputation.PrePIRIOT;
+import protocols.Protocol;
+import protocols.struct.OutSSIOT;
+import protocols.struct.Party;
+import protocols.struct.PreData;
+import util.M;
+import util.P;
+import util.Timer;
+import util.Util;
+
+public class PIRIOT extends Protocol {
+
+	private int pid = P.IOT;
+
+	public PIRIOT(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	public void runE(PreData predata, int n, byte[] Nip1_pr, Timer timer) {
+		timer.start(pid, M.online_comp);
+
+		// step 1
+		byte[][] x = new byte[n][];
+		byte[][] v = new byte[n][];
+
+		for (int i = 0; i < n; i++) {
+			byte[] i_bytes = Util.intToBytes(i);
+			x[i] = predata.ssiot_r.clone();
+			for (int j = 0; j < Nip1_pr.length; j++)
+				x[i][x[i].length - 1 - j] ^= Nip1_pr[Nip1_pr.length - 1 - j] ^ i_bytes[i_bytes.length - 1 - j];
+
+			v[i] = predata.ssiot_F_kprime.compute(x[i]);
+		}
+
+		timer.start(pid, M.online_write);
+		con2.write(pid, v);
+		timer.stop(pid, M.online_write);
+
+		timer.stop(pid, M.online_comp);
+	}
+
+	public void runD(PreData predata, byte[] Nip1_pr, Timer timer) {
+		timer.start(pid, M.online_comp);
+
+		// step 2
+		byte[] y = predata.ssiot_r;
+		for (int i = 0; i < Nip1_pr.length; i++)
+			y[y.length - 1 - i] ^= Nip1_pr[Nip1_pr.length - 1 - i];
+		byte[] w = predata.ssiot_F_kprime.compute(y);
+
+		timer.start(pid, M.online_write);
+		con2.write(pid, w);
+		timer.stop(pid, M.online_write);
+
+		timer.stop(pid, M.online_comp);
+	}
+
+	public OutSSIOT runC(Timer timer) {
+		timer.start(pid, M.online_comp);
+
+		// step 1
+		timer.start(pid, M.online_read);
+		byte[][] v = con1.readDoubleByteArray(pid);
+
+		// step 2
+		byte[] w = con2.read(pid);
+		timer.stop(pid, M.online_read);
+
+		// step 3
+		int n = v.length;
+		OutSSIOT output = null;
+		int invariant = 0;
+
+		for (int i = 0; i < n; i++) {
+			if (Util.equal(v[i], w)) {
+				output = new OutSSIOT(i, null);
+				invariant++;
+			}
+		}
+
+		if (invariant != 1)
+			throw new SSIOTException("Invariant error: " + invariant);
+
+		timer.stop(pid, M.online_comp);
+		return output;
+	}
+
+	// for testing correctness
+	@Override
+	public void run(Party party, Metadata md, Forest forest) {
+		Timer timer = new Timer();
+
+		for (int j = 0; j < 100; j++) {
+			int twoTauPow = 64;
+			byte[] sE_Nip1_pr = new byte[1];
+			byte[] sD_Nip1_pr = new byte[1];
+			int index = Crypto.sr.nextInt(twoTauPow);
+			Crypto.sr.nextBytes(sE_Nip1_pr);
+			sD_Nip1_pr[0] = (byte) (Util.intToBytes(index)[3] ^ sE_Nip1_pr[0]);
+
+			PreData predata = new PreData();
+			PrePIRIOT pressiot = new PrePIRIOT(con1, con2);
+
+			if (party == Party.Eddie) {
+				con1.write(sD_Nip1_pr);
+				con2.write(index);
+				pressiot.runE(predata, twoTauPow, timer);
+				runE(predata, twoTauPow, sE_Nip1_pr, timer);
+
+			} else if (party == Party.Debbie) {
+				sD_Nip1_pr = con1.read();
+				pressiot.runD(predata, timer);
+				runD(predata, sD_Nip1_pr, timer);
+
+			} else if (party == Party.Charlie) {
+				index = con1.readInt();
+				pressiot.runC();
+				OutSSIOT output = runC(timer);
+				if (output.t == index)
+					System.out.println("PIRIOT test passed");
+				else
+					System.err.println("PIRIOT test failed");
+
+			} else {
+				throw new NoSuchPartyException(party + "");
+			}
+		}
+
+		// timer.print();
+	}
+}

+ 61 - 0
src/precomputation/PrePIRIOT.java

@@ -0,0 +1,61 @@
+package precomputation;
+
+import communication.Communication;
+import crypto.Crypto;
+import crypto.PRF;
+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 PrePIRIOT extends Protocol {
+
+	private int pid = P.IOT;
+
+	public PrePIRIOT(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	public void runE(PreData predata, int n, Timer timer) {
+		timer.start(pid, M.offline_comp);
+
+		predata.ssiot_kprime = PRF.generateKey(Crypto.sr);
+		predata.ssiot_r = Util.nextBytes(Crypto.secParamBytes, Crypto.sr);
+
+		timer.start(pid, M.offline_write);
+		con1.write(predata.ssiot_kprime);
+		con1.write(predata.ssiot_r);
+		timer.stop(pid, M.offline_write);
+
+		predata.ssiot_F_kprime = new PRF(Crypto.secParam);
+		predata.ssiot_F_kprime.init(predata.ssiot_kprime);
+
+		timer.stop(pid, M.offline_comp);
+	}
+
+	public void runD(PreData predata, Timer timer) {
+		timer.start(pid, M.offline_comp);
+
+		timer.start(pid, M.offline_read);
+		predata.ssiot_kprime = con1.read();
+		predata.ssiot_r = con1.read();
+		timer.stop(pid, M.offline_read);
+
+		predata.ssiot_F_kprime = new PRF(Crypto.secParam);
+		predata.ssiot_F_kprime.init(predata.ssiot_kprime);
+
+		timer.stop(pid, M.offline_comp);
+	}
+
+	public void runC() {
+	}
+
+	@Override
+	public void run(Party party, Metadata md, Forest forest) {
+	}
+}

+ 2 - 0
src/ui/CLI.java

@@ -95,6 +95,8 @@ public class CLI {
 
 		} else if (protocol.equals("pircot")) {
 			operation = PIRCOT.class;
+		} else if (protocol.equals("piriot")) {
+			operation = PIRIOT.class;
 
 		} else {
 			System.out.println("Protocol " + protocol + " not supported");

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