Boyoung- 8 лет назад
Родитель
Сommit
e8cdd11470

+ 149 - 0
src/pir/PIRCOT.java

@@ -0,0 +1,149 @@
+package pir;
+
+import communication.Communication;
+import crypto.Crypto;
+import exceptions.NoSuchPartyException;
+import exceptions.SSCOTException;
+import oram.Forest;
+import oram.Metadata;
+import precomputation.PrePIRCOT;
+import protocols.Protocol;
+import protocols.struct.OutSSCOT;
+import protocols.struct.Party;
+import protocols.struct.PreData;
+import util.M;
+import util.P;
+import util.Timer;
+import util.Util;
+
+public class PIRCOT extends Protocol {
+
+	private int pid = P.COT;
+
+	public PIRCOT(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	public void runE(PreData predata, byte[][] a, Timer timer) {
+		timer.start(pid, M.online_comp);
+
+		// step 1
+		int n = a.length;
+		byte[][] x = predata.sscot_r;
+		byte[][] v = new byte[n][];
+
+		for (int i = 0; i < n; i++) {
+			for (int j = 0; j < a[i].length; j++)
+				x[i][j] = (byte) (predata.sscot_r[i][j] ^ a[i][j]);
+
+			v[i] = predata.sscot_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[][] b, Timer timer) {
+		timer.start(pid, M.online_comp);
+
+		// step 2
+		int n = b.length;
+		byte[][] y = predata.sscot_r;
+		byte[][] w = new byte[n][];
+
+		for (int i = 0; i < n; i++) {
+			for (int j = 0; j < b[i].length; j++)
+				y[i][j] = (byte) (predata.sscot_r[i][j] ^ b[i][j]);
+
+			w[i] = predata.sscot_F_kprime.compute(y[i]);
+		}
+
+		timer.start(pid, M.online_write);
+		con2.write(pid, w);
+		timer.stop(pid, M.online_write);
+
+		timer.stop(pid, M.online_comp);
+	}
+
+	public OutSSCOT 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.readDoubleByteArray(pid);
+		timer.stop(pid, M.online_read);
+
+		// step 3
+		int n = v.length;
+		OutSSCOT output = null;
+		int invariant = 0;
+
+		for (int i = 0; i < n; i++) {
+			if (Util.equal(v[i], w[i])) {
+				output = new OutSSCOT(i, null);
+				invariant++;
+			}
+		}
+
+		if (invariant != 1)
+			throw new SSCOTException("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 n = 100;
+			int FN = 5;
+			byte[][] a = new byte[n][FN];
+			byte[][] b = new byte[n][FN];
+			for (int i = 0; i < n; i++) {
+				Crypto.sr.nextBytes(a[i]);
+				Crypto.sr.nextBytes(b[i]);
+				while (Util.equal(a[i], b[i]))
+					Crypto.sr.nextBytes(b[i]);
+			}
+			int index = Crypto.sr.nextInt(n);
+			b[index] = a[index].clone();
+
+			PreData predata = new PreData();
+			PrePIRCOT presscot = new PrePIRCOT(con1, con2);
+			if (party == Party.Eddie) {
+				con1.write(b);
+				con2.write(index);
+				presscot.runE(predata, n, timer);
+				runE(predata, a, timer);
+
+			} else if (party == Party.Debbie) {
+				b = con1.readDoubleByteArray();
+				presscot.runD(predata, timer);
+				runD(predata, b, timer);
+
+			} else if (party == Party.Charlie) {
+				index = con1.readInt();
+				presscot.runC();
+				OutSSCOT output = runC(timer);
+				if (output.t == index)
+					System.out.println("PIRCOT test passed");
+				else
+					System.err.println("PIRCOT test failed");
+
+			} else {
+				throw new NoSuchPartyException(party + "");
+			}
+		}
+
+		// timer.print();
+	}
+}

+ 64 - 0
src/precomputation/PrePIRCOT.java

@@ -0,0 +1,64 @@
+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;
+
+public class PrePIRCOT extends Protocol {
+
+	private int pid = P.COT;
+
+	public PrePIRCOT(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	public void runE(PreData predata, int n, Timer timer) {
+		timer.start(pid, M.offline_comp);
+
+		predata.sscot_kprime = PRF.generateKey(Crypto.sr);
+		predata.sscot_r = new byte[n][];
+		for (int i = 0; i < n; i++) {
+			predata.sscot_r[i] = new byte[Crypto.secParamBytes];
+			Crypto.sr.nextBytes(predata.sscot_r[i]);
+		}
+
+		timer.start(pid, M.offline_write);
+		con1.write(predata.sscot_kprime);
+		con1.write(predata.sscot_r);
+		timer.stop(pid, M.offline_write);
+
+		predata.sscot_F_kprime = new PRF(Crypto.secParam);
+		predata.sscot_F_kprime.init(predata.sscot_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.sscot_kprime = con1.read();
+		predata.sscot_r = con1.readDoubleByteArray();
+		timer.stop(pid, M.offline_read);
+
+		predata.sscot_F_kprime = new PRF(Crypto.secParam);
+		predata.sscot_F_kprime.init(predata.sscot_kprime);
+
+		timer.stop(pid, M.offline_comp);
+	}
+
+	public void runC() {
+	}
+
+	@Override
+	public void run(Party party, Metadata md, Forest forest) {
+	}
+}

+ 5 - 0
src/ui/CLI.java

@@ -15,6 +15,7 @@ import exceptions.NoSuchPartyException;
 import oram.Global;
 import oram.Metadata;
 import protocols.*;
+import pir.*;
 import protocols.struct.Party;
 
 public class CLI {
@@ -91,6 +92,10 @@ public class CLI {
 			operation = SSXOT.class;
 		} else if (protocol.equals("rtv")) {
 			operation = Retrieve.class;
+
+		} else if (protocol.equals("pircot")) {
+			operation = PIRCOT.class;
+
 		} else {
 			System.out.println("Protocol " + protocol + " not supported");
 			System.exit(-1);

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