Boyoung- 9 år sedan
förälder
incheckning
a2d3ce4653

+ 3 - 2
src/measure/P.java

@@ -1,11 +1,12 @@
 package measure;
 
 public class P {
-	public static final int size = 3;
+	public static final int size = 4;
 
 	public static final int ACC = 0;
 	public static final int COT = 1;
 	public static final int IOT = 2;
+	public static final int XOT = 3;
 
-	public static final String[] names = { "ACC", "COT", "IOT" };
+	public static final String[] names = { "ACC", "COT", "IOT", "XOT" };
 }

+ 1 - 0
src/protocols/Access.java

@@ -200,6 +200,7 @@ public class Access extends Protocol {
 		return outaccess;
 	}
 
+	// for testing correctness
 	@Override
 	public void run(Party party, Metadata md, Forest forest) {
 		int records = 5;

+ 8 - 0
src/protocols/PreData.java

@@ -18,4 +18,12 @@ public class PreData {
 
 	public int[] access_sigma;
 	public Tuple[] access_p;
+
+	public Tuple[] ssxot_delta;
+	public int[] ssxot_E_pi;
+	public int[] ssxot_C_pi;
+	public int[] ssxot_E_pi_ivs;
+	public int[] ssxot_C_pi_ivs;
+	public Tuple[] ssxot_E_r;
+	public Tuple[] ssxot_C_r;
 }

+ 64 - 0
src/protocols/PreSSXOT.java

@@ -0,0 +1,64 @@
+package protocols;
+
+import communication.Communication;
+import crypto.Crypto;
+import measure.M;
+import measure.P;
+import measure.Timer;
+import oram.Forest;
+import oram.Metadata;
+import oram.Tuple;
+import util.Util;
+
+public class PreSSXOT extends Protocol {
+	public PreSSXOT(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	public void runE(PreData predata, Timer timer) {
+		timer.start(P.XOT, M.offline_read);
+		predata.ssxot_E_pi = con1.readObject();
+		predata.ssxot_E_r = con1.readObject();
+		timer.stop(P.XOT, M.offline_read);
+	}
+
+	public void runD(PreData predata, int n, int k, int[] tupleParam, Timer timer) {
+		timer.start(P.XOT, M.offline_comp);
+
+		predata.ssxot_delta = new Tuple[k];
+		for (int i = 0; i < k; i++)
+			predata.ssxot_delta[i] = new Tuple(tupleParam[0], tupleParam[1], tupleParam[2], tupleParam[3], Crypto.sr);
+
+		predata.ssxot_E_pi = Util.randomPermutation(n, Crypto.sr);
+		predata.ssxot_C_pi = Util.randomPermutation(n, Crypto.sr);
+		predata.ssxot_E_pi_ivs = Util.inversePermutation(predata.ssxot_E_pi);
+		predata.ssxot_C_pi_ivs = Util.inversePermutation(predata.ssxot_C_pi);
+
+		predata.ssxot_E_r = new Tuple[n];
+		predata.ssxot_C_r = new Tuple[n];
+		for (int i = 0; i < n; i++) {
+			predata.ssxot_E_r[i] = new Tuple(tupleParam[0], tupleParam[1], tupleParam[2], tupleParam[3], Crypto.sr);
+			predata.ssxot_C_r[i] = new Tuple(tupleParam[0], tupleParam[1], tupleParam[2], tupleParam[3], Crypto.sr);
+		}
+
+		timer.start(P.XOT, M.offline_write);
+		con1.write(predata.ssxot_E_pi);
+		con1.write(predata.ssxot_E_r);
+		con2.write(predata.ssxot_C_pi);
+		con2.write(predata.ssxot_C_r);
+		timer.stop(P.XOT, M.offline_write);
+
+		timer.stop(P.XOT, M.offline_comp);
+	}
+
+	public void runC(PreData predata, Timer timer) {
+		timer.start(P.XOT, M.offline_read);
+		predata.ssxot_C_pi = con2.readObject();
+		predata.ssxot_C_r = con2.readObject();
+		timer.stop(P.XOT, M.offline_read);
+	}
+
+	@Override
+	public void run(Party party, Metadata md, Forest forest) {
+	}
+}

+ 3 - 0
src/protocols/SSCOT.java

@@ -104,6 +104,7 @@ public class SSCOT extends Protocol {
 		return output;
 	}
 
+	// for testing correctness
 	@Override
 	public void run(Party party, Metadata md, Forest forest) {
 		Timer timer = new Timer();
@@ -153,5 +154,7 @@ public class SSCOT extends Protocol {
 				throw new NoSuchPartyException(party + "");
 			}
 		}
+
+		// timer.print();
 	}
 }

+ 3 - 0
src/protocols/SSIOT.java

@@ -99,6 +99,7 @@ public class SSIOT extends Protocol {
 		return output;
 	}
 
+	// for testing correctness
 	@Override
 	public void run(Party party, Metadata md, Forest forest) {
 		Timer timer = new Timer();
@@ -144,5 +145,7 @@ public class SSIOT extends Protocol {
 				throw new NoSuchPartyException(party + "");
 			}
 		}
+
+		// timer.print();
 	}
 }

+ 166 - 0
src/protocols/SSXOT.java

@@ -0,0 +1,166 @@
+package protocols;
+
+import java.math.BigInteger;
+
+import communication.Communication;
+import crypto.Crypto;
+import exceptions.NoSuchPartyException;
+import measure.M;
+import measure.P;
+import measure.Timer;
+import oram.Metadata;
+import oram.Tuple;
+import util.Util;
+
+public class SSXOT extends Protocol {
+
+	public SSXOT(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	public Tuple[] runE(PreData predata, Tuple[] m, Timer timer) {
+		timer.start(P.XOT, M.online_comp);
+
+		// step 1
+		Tuple[] a = new Tuple[m.length];
+		for (int i = 0; i < m.length; i++)
+			a[i] = m[predata.ssxot_E_pi[i]].xor(predata.ssxot_E_r[i]);
+
+		timer.start(P.XOT, M.online_write);
+		con2.write(a);
+		timer.stop(P.XOT, M.online_write);
+
+		timer.start(P.XOT, M.online_read);
+		a = con2.readObject();
+
+		// step 2
+		int[] j = con1.readObject();
+		Tuple[] p = con1.readObject();
+		timer.stop(P.XOT, M.online_read);
+
+		// step 3
+		Tuple[] z = new Tuple[j.length];
+		for (int i = 0; i < j.length; i++)
+			z[i] = a[j[i]].xor(p[i]);
+
+		timer.stop(P.XOT, M.online_comp);
+		return z;
+	}
+
+	public void runD(PreData predata, int[] index, Timer timer) {
+		timer.start(P.XOT, M.online_comp);
+
+		// step 2
+		int k = index.length;
+		int[] E_j = new int[k];
+		int[] C_j = new int[k];
+		Tuple[] E_p = new Tuple[k];
+		Tuple[] C_p = new Tuple[k];
+		for (int i = 0; i < k; i++) {
+			E_j[i] = predata.ssxot_E_pi_ivs[index[i]];
+			C_j[i] = predata.ssxot_C_pi_ivs[index[i]];
+			E_p[i] = predata.ssxot_E_r[E_j[i]].xor(predata.ssxot_delta[i]);
+			C_p[i] = predata.ssxot_C_r[C_j[i]].xor(predata.ssxot_delta[i]);
+		}
+
+		timer.start(P.XOT, M.online_write);
+		con2.write(E_j);
+		con2.write(E_p);
+		con1.write(C_j);
+		con1.write(C_p);
+		timer.stop(P.XOT, M.online_write);
+
+		timer.stop(P.XOT, M.online_comp);
+	}
+
+	public Tuple[] runC(PreData predata, Tuple[] m, Timer timer) {
+		timer.start(P.XOT, M.online_comp);
+
+		// step 1
+		Tuple[] a = new Tuple[m.length];
+		for (int i = 0; i < m.length; i++)
+			a[i] = m[predata.ssxot_C_pi[i]].xor(predata.ssxot_C_r[i]);
+
+		timer.start(P.XOT, M.online_write);
+		con1.write(a);
+		timer.stop(P.XOT, M.online_write);
+
+		timer.start(P.XOT, M.online_read);
+		a = con1.readObject();
+
+		// step 2
+		int[] j = con2.readObject();
+		Tuple[] p = con2.readObject();
+		timer.stop(P.XOT, M.online_read);
+
+		// step 3
+		Tuple[] z = new Tuple[j.length];
+		for (int i = 0; i < j.length; i++)
+			z[i] = a[j[i]].xor(p[i]);
+
+		timer.stop(P.XOT, M.online_comp);
+		return z;
+	}
+
+	// for testing correctness
+	@Override
+	public void run(protocols.Party party, Metadata md, oram.Forest forest) {
+		Timer timer = new Timer();
+
+		for (int j = 0; j < 100; j++) {
+			int n = 100;
+			int k = Crypto.sr.nextInt(50) + 50;
+			int[] index = Util.randomPermutation(k, Crypto.sr);
+			int[] tupleParam = new int[] { 1, 2, 3, 4 };
+			Tuple[] E_m = new Tuple[n];
+			Tuple[] C_m = new Tuple[n];
+			for (int i = 0; i < n; i++) {
+				E_m[i] = new Tuple(tupleParam[0], tupleParam[1], tupleParam[2], tupleParam[3], Crypto.sr);
+				C_m[i] = new Tuple(tupleParam[0], tupleParam[1], tupleParam[2], tupleParam[3], null);
+			}
+
+			PreData predata = new PreData();
+			PreSSXOT pressxot = new PreSSXOT(con1, con2);
+
+			if (party == Party.Eddie) {
+				pressxot.runE(predata, timer);
+				Tuple[] E_out_m = runE(predata, E_m, timer);
+
+				con2.write(E_m);
+				con2.write(E_out_m);
+
+			} else if (party == Party.Debbie) {
+				pressxot.runD(predata, n, k, tupleParam, timer);
+				runD(predata, index, timer);
+
+				con2.write(index);
+
+			} else if (party == Party.Charlie) {
+				pressxot.runC(predata, timer);
+				Tuple[] C_out_m = runC(predata, C_m, timer);
+
+				index = con2.readObject();
+				E_m = con1.readObject();
+				Tuple[] E_out_m = con1.readObject();
+
+				boolean pass = true;
+				for (int i = 0; i < index.length; i++) {
+					int input = new BigInteger(1, E_m[index[i]].getA()).intValue();
+					int output = new BigInteger(1, Util.xor(E_out_m[i].getA(), C_out_m[i].getA())).intValue();
+					if (input != output) {
+						System.err.println("SSXOT test failed");
+						pass = false;
+						break;
+					}
+				}
+				if (pass)
+					System.out.println("SSXOT test passed");
+
+			} else {
+				throw new NoSuchPartyException(party + "");
+			}
+		}
+
+		// timer.print();
+	}
+}

+ 3 - 0
src/ui/CLI.java

@@ -16,6 +16,7 @@ import protocols.Party;
 import protocols.Protocol;
 import protocols.SSCOT;
 import protocols.SSIOT;
+import protocols.SSXOT;
 import protocols.Access;
 
 public class CLI {
@@ -71,6 +72,8 @@ public class CLI {
 			operation = SSCOT.class;
 		} else if (protocol.equals("ssiot")) {
 			operation = SSIOT.class;
+		} else if (protocol.equals("ssxot")) {
+			operation = SSXOT.class;
 		} else if (protocol.equals("access")) {
 			operation = Access.class;
 		} else {

+ 7 - 0
src/util/Util.java

@@ -108,6 +108,13 @@ public class Util {
 		return array;
 	}
 
+	public static int[] inversePermutation(int[] p) {
+		int[] ip = new int[p.length];
+		for (int i = 0; i < p.length; i++)
+			ip[p[i]] = i;
+		return ip;
+	}
+
 	public static <T> T[] permute(T[] original, int[] p) {
 		@SuppressWarnings("unchecked")
 		T[] permuted = (T[]) new Object[original.length];

+ 1 - 1
test/misc/HelloWorld.java → test/misc/MiscTests.java

@@ -10,7 +10,7 @@ import oram.Metadata;
 import util.StopWatch;
 import util.Util;
 
-public class HelloWorld {
+public class MiscTests {
 
 	public static void main(String[] args) {
 		/*

+ 50 - 0
test/protocols/TestSSXOT_C.java

@@ -0,0 +1,50 @@
+package protocols;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Arrays;
+
+public class TestSSXOT_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 ssxot 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/protocols/TestSSXOT_D.java

@@ -0,0 +1,50 @@
+package protocols;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Arrays;
+
+public class TestSSXOT_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 ssxot 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/protocols/TestSSXOT_E.java

@@ -0,0 +1,50 @@
+package protocols;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Arrays;
+
+public class TestSSXOT_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 ssxot 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();
+		}
+	}
+
+}