Browse Source

add InsLbl impl

Boyoung- 6 years ago
parent
commit
d0889a47c2
7 changed files with 430 additions and 1 deletions
  1. 227 0
      src/pir/InsLbl.java
  2. 2 0
      src/ui/CLI.java
  3. 2 1
      src/util/P.java
  4. 49 0
      src/util/Util.java
  5. 50 0
      test/pir/TestInsLbl_C.java
  6. 50 0
      test/pir/TestInsLbl_D.java
  7. 50 0
      test/pir/TestInsLbl_E.java

+ 227 - 0
src/pir/InsLbl.java

@@ -0,0 +1,227 @@
+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 InsLbl extends Protocol {
+
+	private int pid = P.InsLbl;
+
+	public InsLbl(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	public void runP1(PreData predata, int dN1, byte[] L1, int ttp, Timer timer) {
+		timer.start(pid, M.offline_comp);
+
+		int l = L1.length;
+
+		byte[] p = Util.nextBytes(ttp * l, Crypto.sr);
+		byte[] a = Util.nextBytes(ttp * l, Crypto.sr);
+		byte[] b = Util.nextBytes(ttp * l, Crypto.sr);
+		int v = Crypto.sr.nextInt(ttp);
+		int w = Crypto.sr.nextInt(ttp);
+
+		int alpha1 = Crypto.sr.nextInt(ttp);
+		int u1 = alpha1 ^ v;
+		byte[] pstar = Util.xor(p, Util.xorRotate(a, u1, ttp, l));
+
+		timer.start(pid, M.offline_write);
+		con1.write(p);
+		con1.write(a);
+		con1.write(b);
+		con1.write(v);
+		con1.write(w);
+		con2.write(u1);
+		con2.write(pstar);
+		timer.stop(pid, M.offline_write);
+
+		timer.stop(pid, M.offline_comp);
+
+		// ----------------------------------------- //
+
+		timer.start(pid, M.online_comp);
+
+		int m = dN1 ^ alpha1;
+
+		timer.start(pid, M.online_write);
+		con1.write(pid, m);
+		timer.stop(pid, M.online_write);
+
+		timer.start(pid, M.online_read);
+		m = con1.readInt(pid);
+		timer.stop(pid, M.online_read);
+
+		int beta1 = m ^ dN1;
+
+		int index = beta1 ^ w;
+		for (int i = 0; i < l; i++) {
+			b[index * l + i] = (byte) (b[index * l + i] ^ L1[i]);
+		}
+
+		timer.start(pid, M.online_write);
+		con2.write(pid, b);
+		timer.stop(pid, M.online_write);
+
+		timer.stop(pid, M.online_comp);
+		return;
+	}
+
+	public byte[] runP2(PreData predata, int dN2, byte[] L2, int ttp, Timer timer) {
+		timer.start(pid, M.offline_comp);
+
+		int l = L2.length;
+
+		timer.start(pid, M.offline_read);
+		byte[] p = con1.read();
+		byte[] a = con1.read();
+		byte[] b = con1.read();
+		int v = con1.readInt();
+		int w = con1.readInt();
+		timer.stop(pid, M.offline_read);
+
+		int beta2 = Crypto.sr.nextInt(ttp);
+		int u2 = beta2 ^ w;
+		byte[] z2 = Util.xor(p, Util.xorRotate(b, u2, ttp, l));
+
+		timer.start(pid, M.offline_write);
+		con2.write(u2);
+		timer.stop(pid, M.offline_write);
+
+		timer.stop(pid, M.offline_comp);
+
+		// ----------------------------------------- //
+
+		timer.start(pid, M.online_comp);
+
+		int m = beta2 ^ dN2;
+
+		timer.start(pid, M.online_write);
+		con1.write(pid, m);
+		timer.stop(pid, M.online_write);
+
+		timer.start(pid, M.online_read);
+		m = con1.readInt(pid);
+		timer.stop(pid, M.online_read);
+
+		int alpha2 = m ^ dN2;
+
+		int index = alpha2 ^ v;
+		for (int i = 0; i < l; i++) {
+			a[index * l + i] = (byte) (a[index * l + i] ^ L2[i]);
+		}
+
+		timer.start(pid, M.online_write);
+		con2.write(pid, a);
+		timer.stop(pid, M.online_write);
+
+		timer.stop(pid, M.online_comp);
+		return z2;
+	}
+
+	public byte[] runP3(PreData predata, int ttp, int l, Timer timer) {
+		timer.start(pid, M.offline_comp);
+
+		timer.start(pid, M.offline_read);
+		int u1 = con1.readInt();
+		byte[] pstar = con1.read();
+		int u2 = con2.readInt();
+		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[] s1 = con1.read(pid);
+		byte[] s2 = con2.read(pid);
+		timer.stop(pid, M.online_read);
+
+		s2 = Util.xorRotate(s2, u1, ttp, l);
+		s1 = Util.xorRotate(s1, u2, ttp, l);
+		Util.setXor(pstar, s1);
+		Util.setXor(pstar, s2);
+
+		timer.stop(pid, M.online_comp);
+		return pstar;
+	}
+
+	@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 ttp = (int) Math.pow(2, 8);
+			int l = 10;
+			int dN1 = Crypto.sr.nextInt(ttp);
+			int dN2 = Crypto.sr.nextInt(ttp);
+			byte[] L1 = Util.nextBytes(l, Crypto.sr);
+			byte[] L2 = Util.nextBytes(l, Crypto.sr);
+
+			if (party == Party.Eddie) {
+				this.runP1(predata, dN1, L1, ttp, timer);
+				con1.write(dN1);
+				con1.write(L1);
+
+			} else if (party == Party.Debbie) {
+				byte[] m1 = this.runP2(predata, dN2, L2, ttp, timer);
+				byte[] m2 = con2.read();
+				dN1 = con1.readInt();
+				L1 = con1.read();
+				int dN = dN1 ^ dN2;
+				byte[] L = Util.xor(L1, L2);
+				byte[] M = Util.xor(m1, m2);
+				byte[] expectL = Arrays.copyOfRange(M, dN * l, dN * l + l);
+
+				boolean fail = false;
+				if (!Util.equal(L, expectL)) {
+					System.err.println(j + ": InsLbl test failed on L");
+					fail = true;
+				}
+				for (int i = 0; i < dN * l; i++) {
+					if (M[i] != 0) {
+						System.err.println(j + ": InsLbl test failed 1");
+						fail = true;
+						break;
+					}
+				}
+				for (int i = dN * l + l; i < M.length; i++) {
+					if (M[i] != 0) {
+						System.err.println(j + ": InsLbl test failed 2");
+						fail = true;
+						break;
+					}
+				}
+				if (!fail)
+					System.out.println(j + ": InsLbl test passed");
+
+			} else if (party == Party.Charlie) {
+				byte[] m2 = this.runP3(predata, ttp, l, timer);
+				con2.write(m2);
+
+			} else {
+				throw new NoSuchPartyException(party + "");
+			}
+		}
+	}
+
+	@Override
+	public void run(Party party, Metadata md, Forest forest) {
+	}
+}

+ 2 - 0
src/ui/CLI.java

@@ -117,6 +117,8 @@ public class CLI {
 			operation = Shift.class;
 		} else if (protocol.equals("ff")) {
 			operation = FlipFlag.class;
+		} else if (protocol.equals("inslbl")) {
+			operation = InsLbl.class;
 
 		} else {
 			System.out.println("Protocol " + protocol + " not supported");

+ 2 - 1
src/util/P.java

@@ -20,8 +20,9 @@ public class P {
 	public static final int TSXPIR = 15;
 	public static final int Shift = 16;
 	public static final int FF = 17;
+	public static final int InsLbl = 18;
 
 	public static final String[] names = { "ACC", "COT", "IOT", "PPT", "RSF", "UR", "URXOT", "EVI", "PT", "PI", "XOT",
-			"SSPIR", "SftPIR", "TSPIR", "SftXorPIR", "TSXPIR", "Shift", "FF" };
+			"SSPIR", "SftPIR", "TSPIR", "SftXorPIR", "TSXPIR", "Shift", "FF", "InsLbl" };
 	public static final int size = names.length;
 }

+ 49 - 0
src/util/Util.java

@@ -217,6 +217,55 @@ public class Util {
 		return out;
 	}
 
+	public static byte[] xorRotate(byte[] arr, int r, int ttp, int l) {
+		assert arr.length == ttp * l;
+		assert r >= 0 && r < ttp;
+		byte[] ret = new byte[arr.length];
+		for (int i = 0; i < ttp; i++) {
+			for (int j = 0; j < l; j++) {
+				ret[i * l + j] = arr[(i ^ r) * l + j];
+			}
+		}
+		return ret;
+	}
+
+	public static byte[] createRotate(byte[] arr, int order) {
+		byte[] ret = arr.clone();
+		rotate(ret, order);
+		return ret;
+	}
+
+	public static void rotate(byte[] arr, int order) {
+		if (arr == null || arr.length == 0 || order < 0) {
+			throw new IllegalArgumentException("Illegal argument!");
+		}
+
+		if (order > arr.length) {
+			order = order % arr.length;
+		}
+
+		// length of first part
+		int a = arr.length - order;
+
+		reverse(arr, 0, a - 1);
+		reverse(arr, a, arr.length - 1);
+		reverse(arr, 0, arr.length - 1);
+
+	}
+
+	public static void reverse(byte[] arr, int left, int right) {
+		if (arr == null || arr.length == 1)
+			return;
+
+		while (left < right) {
+			byte temp = arr[left];
+			arr[left] = arr[right];
+			arr[right] = temp;
+			left++;
+			right--;
+		}
+	}
+
 	public static void debug(String s) {
 		// only to make Communication.java compile
 	}

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