Selaa lähdekoodia

PermuteIndex tested

Boyoung- 8 vuotta sitten
vanhempi
commit
e3543d21c2

+ 128 - 0
src/protocols/PermuteIndex.java

@@ -0,0 +1,128 @@
+package protocols;
+
+import java.math.BigInteger;
+
+import communication.Communication;
+import crypto.Crypto;
+import exceptions.NoSuchPartyException;
+import measure.Timer;
+import oram.Forest;
+import oram.Metadata;
+import util.Util;
+
+public class PermuteIndex extends Protocol {
+	public PermuteIndex(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	public void runE() {
+	}
+
+	public int[] runD(PreData predata, boolean firstTree, int[] ti, Timer timer) {
+		if (firstTree)
+			return null;
+		
+		BigInteger[] ti_p = new BigInteger[ti.length];
+		for (int i=0; i<ti.length; i++)
+			ti_p[i] = BigInteger.valueOf(ti[i]);
+
+		BigInteger[] z = Util.xor(ti_p, predata.pi_p);
+
+		con2.write(z);
+
+		BigInteger[] g = con2.readObject();
+
+		ti_p = Util.xor(predata.pi_a, g);
+
+		int[] ti_pp = new int[ti.length];
+		for (int i=0; i<ti.length; i++)
+			ti_pp[i] = ti_p[i].intValue();
+		
+		return ti_pp;
+	}
+
+	public void runC(PreData predata, boolean firstTree, Timer timer) {
+		if (firstTree)
+			return;
+
+		BigInteger[] z = con2.readObject();
+
+		z = Util.xor(z, predata.pi_r);
+		z = Util.permute(z, predata.evict_pi);
+		BigInteger[] g = Util.xor(predata.evict_rho, z);
+
+		con2.write(g);
+	}
+
+	// for testing correctness
+	@Override
+	public void run(Party party, Metadata md, Forest forest) {
+		Timer timer = new Timer();
+
+		for (int i = 0; i < 10; i++) {
+
+			System.out.println("i=" + i);
+
+			PreData predata = new PreData();
+			PrePermuteIndex prepermuteindex = new PrePermuteIndex(con1, con2);
+
+			if (party == Party.Eddie) {
+				int d = Crypto.sr.nextInt(5) + 5;
+				int w = Crypto.sr.nextInt(5) + 5;
+				int logW = (int) Math.ceil(Math.log(w+1) / Math.log(2));
+				
+				int[] ti = new int[d];
+				predata.evict_pi = Util.randomPermutation(d, Crypto.sr);
+				predata.evict_rho = new BigInteger[d];
+				for (int j=0; j<d; j++) {
+					ti[j] = Crypto.sr.nextInt(w+1);
+					predata.evict_rho[j] = new BigInteger(logW, Crypto.sr);
+				}
+
+				con1.write(predata.evict_pi);
+				con1.write(predata.evict_rho);
+				con1.write(ti);
+
+				con2.write(predata.evict_pi);
+				con2.write(predata.evict_rho);
+
+				prepermuteindex.runE(predata, d, w, timer);
+
+				runE();
+
+				ti = Util.permute(ti, predata.evict_pi);
+				for (int j = 0; j < d; j++) {
+					ti[j] = predata.evict_rho[j].intValue() ^ ti[j];
+					System.out.print(ti[j] + " ");
+				}
+				System.out.println();
+
+			} else if (party == Party.Debbie) {
+				predata.evict_pi = con1.readObject();
+				predata.evict_rho = con1.readObject();
+				int[] ti = con1.readObject();
+
+				prepermuteindex.runD(predata, timer);
+
+				int[] ti_pp = runD(predata, false, ti, timer);
+				for (int j = 0; j < ti.length; j++) {
+					System.out.print(ti_pp[j] + " ");
+				}
+				System.out.println();
+
+			} else if (party == Party.Charlie) {
+				predata.evict_pi = con1.readObject();
+				predata.evict_rho = con1.readObject();
+
+				prepermuteindex.runC(predata, timer);
+
+				runC(predata, false, timer);
+
+			} else {
+				throw new NoSuchPartyException(party + "");
+			}
+		}
+
+		// timer.print();
+	}
+}

+ 5 - 0
src/protocols/PreData.java

@@ -76,4 +76,9 @@ public class PreData {
 	public BigInteger[] pt_p;
 	public BigInteger[] pt_r;
 	public BigInteger[] pt_a;
+	
+	// PermuteIndex
+	public BigInteger[] pi_p;
+	public BigInteger[] pi_r;
+	public BigInteger[] pi_a;
 }

+ 48 - 0
src/protocols/PrePermuteIndex.java

@@ -0,0 +1,48 @@
+package protocols;
+
+import java.math.BigInteger;
+
+import communication.Communication;
+import crypto.Crypto;
+import measure.Timer;
+import oram.Forest;
+import oram.Metadata;
+import util.Util;
+
+public class PrePermuteIndex extends Protocol {
+	public PrePermuteIndex(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	public void runE(PreData predata, int d, int w, Timer timer) {
+		int logW = (int) Math.ceil(Math.log(w+1) / Math.log(2));
+
+		predata.pi_p = new BigInteger[d];
+		predata.pi_r = new BigInteger[d];
+		predata.pi_a = new BigInteger[d];
+		for (int i = 0; i < d; i++) {
+			predata.pi_p[i] = new BigInteger(logW, Crypto.sr);
+			predata.pi_r[i] = new BigInteger(logW, Crypto.sr);
+			predata.pi_a[i] = predata.pi_p[i].xor(predata.pi_r[i]);
+		}
+		predata.pi_a = Util.permute(predata.pi_a, predata.evict_pi);
+
+		con1.write(predata.pi_p);
+		con1.write(predata.pi_a);
+
+		con2.write(predata.pi_r);
+	}
+
+	public void runD(PreData predata, Timer timer) {
+		predata.pi_p = con1.readObject();
+		predata.pi_a = con1.readObject();
+	}
+
+	public void runC(PreData predata, Timer timer) {
+		predata.pi_r = con1.readObject();
+	}
+
+	@Override
+	public void run(Party party, Metadata md, Forest forest) {
+	}
+}

+ 2 - 0
src/protocols/PrePermuteTarget.java

@@ -18,6 +18,7 @@ public class PrePermuteTarget extends Protocol {
 	}
 
 	public void runE(PreData predata, int d, Timer timer) {
+		// PermuteTargetI
 		int logD = (int) Math.ceil(Math.log(d) / Math.log(2));
 
 		predata.pt_keyT = new BigInteger[d][d];
@@ -46,6 +47,7 @@ public class PrePermuteTarget extends Protocol {
 
 		con2.write(predata.pt_maskT);
 
+		// PermuteTargetII
 		predata.pt_p = new BigInteger[d];
 		predata.pt_r = new BigInteger[d];
 		predata.pt_a = new BigInteger[d];

+ 2 - 0
src/ui/CLI.java

@@ -87,6 +87,8 @@ public class CLI {
 			operation = PrepareTarget.class;
 		} else if (protocol.equals("permt")) {
 			operation = PermuteTarget.class;
+		} else if (protocol.equals("permi")) {
+			operation = PermuteIndex.class;
 		} else if (protocol.equals("mc")) {
 			operation = MakeCycle.class;
 		} else if (protocol.equals("update")) {

+ 7 - 0
src/util/Util.java

@@ -134,6 +134,13 @@ public class Util {
 		return (T[]) Arrays.copyOf(permuted, permuted.length, original.getClass());
 	}
 
+	public static int[] permute(int[] original, int[] p) {
+		int[] permuted = new int[original.length];
+		for (int i = 0; i < original.length; i++)
+			permuted[p[i]] = original[i];
+		return permuted;
+	}
+
 	public static byte[] longToBytes(long l, int numBytes) {
 		byte[] bytes = BigInteger.valueOf(l).toByteArray();
 		if (bytes.length == numBytes)

+ 50 - 0
test/protocols/TestPermuteIndex_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 TestPermuteIndex_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 permi 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/TestPermuteIndex_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 TestPermuteIndex_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 permi 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/TestPermuteIndex_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 TestPermuteIndex_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 permi 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();
+		}
+	}
+
+}