Browse Source

fix bug in Access; add PPT

Boyoung- 8 years ago
parent
commit
42e878519a

+ 4 - 3
src/measure/P.java

@@ -1,12 +1,13 @@
 package measure;
 
 public class P {
-	public static final int size = 4;
+	public static final int size = 5;
 
 	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 int PPT = 3;
+	public static final int XOT = 4;
 
-	public static final String[] names = { "ACC", "COT", "IOT", "XOT" };
+	public static final String[] names = { "ACC", "COT", "IOT", "PPT", "XOT" };
 }

+ 3 - 3
src/protocols/Access.java

@@ -83,9 +83,9 @@ public class Access extends Protocol {
 		if (OTi.getTreeIndex() == 0)
 			Ti = pathTuples[0];
 		else
-			Ti = new Tuple(new byte[0], Ni, Li, y);
+			Ti = new Tuple(new byte[1], Ni, Li, y);
 
-		OutAccess outaccess = new OutAccess(null, null, null, Ti, pathTuples);
+		OutAccess outaccess = new OutAccess(null, null, null, null, Ti, pathTuples);
 
 		timer.stop(P.ACC, M.online_comp);
 		return outaccess;
@@ -194,7 +194,7 @@ public class Access extends Protocol {
 			Crypto.sr.nextBytes(pathTuples[j1].getA());
 		}
 
-		OutAccess outaccess = new OutAccess(Lip1, Ti, pathTuples, null, null);
+		OutAccess outaccess = new OutAccess(Lip1, Ti, pathTuples, j2, null, null);
 
 		timer.stop(P.ACC, M.online_comp);
 		return outaccess;

+ 3 - 1
src/protocols/OutAccess.java

@@ -8,12 +8,14 @@ public class OutAccess {
 	public Tuple C_Ti;
 	public Tuple[] E_P;
 	public Tuple[] C_P;
+	public Integer C_j2;
 
-	public OutAccess(byte[] C_Lip1, Tuple C_Ti, Tuple[] C_P, Tuple E_Ti, Tuple[] E_P) {
+	public OutAccess(byte[] C_Lip1, Tuple C_Ti, Tuple[] C_P, Integer C_j2, Tuple E_Ti, Tuple[] E_P) {
 		this.C_Lip1 = C_Lip1;
 		this.E_Ti = E_Ti;
 		this.C_Ti = C_Ti;
 		this.E_P = E_P;
 		this.C_P = C_P;
+		this.C_j2 = C_j2;
 	}
 }

+ 226 - 0
src/protocols/PostProcessT.java

@@ -0,0 +1,226 @@
+package protocols;
+
+import java.math.BigInteger;
+
+import communication.Communication;
+import crypto.Crypto;
+import exceptions.AccessException;
+import exceptions.NoSuchPartyException;
+import measure.M;
+import measure.P;
+import measure.Timer;
+import oram.Forest;
+import oram.Metadata;
+import oram.Tree;
+import oram.Tuple;
+import util.Util;
+
+public class PostProcessT extends Protocol {
+
+	public PostProcessT(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	public Tuple runE(PreData predata, Tuple Ti, boolean lastTree, Timer timer) {
+		timer.start(P.PPT, M.online_comp);
+
+		if (lastTree) {
+			Tuple out = new Tuple(Ti);
+			Util.setXor(out.getL(), predata.ppt_Li);
+
+			timer.stop(P.PPT, M.online_comp);
+			return out;
+		}
+
+		// step 1
+		int delta = con2.readObject();
+
+		// step 3
+		int twoTauPow = predata.ppt_s.length;
+		byte[][] e = new byte[twoTauPow][];
+		for (int i = 0; i < twoTauPow; i++)
+			e[i] = predata.ppt_s[(i + delta) % twoTauPow];
+		byte[] e_all = new byte[twoTauPow * e[0].length];
+		for (int i = 0; i < twoTauPow; i++)
+			System.arraycopy(e[i], 0, e_all, i * e[0].length, e[0].length);
+
+		Tuple out = new Tuple(Ti);
+		Util.setXor(out.getL(), predata.ppt_Li);
+		Util.setXor(out.getA(), e_all);
+
+		timer.stop(P.PPT, M.online_comp);
+		return out;
+	}
+
+	public void runD() {
+	}
+
+	public Tuple runC(PreData predata, Tuple Ti, byte[] Li, byte[] Lip1, int j2, boolean lastTree, Timer timer) {
+		timer.start(P.PPT, M.online_comp);
+
+		if (lastTree) {
+			Tuple out = new Tuple(Ti);
+			Util.setXor(out.getL(), Util.xor(Li, predata.ppt_Li));
+
+			timer.stop(P.PPT, M.online_comp);
+			return out;
+		}
+
+		// step 1
+		int twoTauPow = predata.ppt_r.length;
+		int delta = (predata.ppt_alpha - j2 + twoTauPow) % twoTauPow;
+
+		con1.write(delta);
+
+		// step 2
+		byte[][] c = new byte[twoTauPow][];
+		for (int i = 0; i < twoTauPow; i++)
+			c[i] = predata.ppt_r[(i + delta) % twoTauPow];
+		c[j2] = Util.xor(Util.xor(c[j2], Lip1), predata.ppt_Lip1);
+		byte[] c_all = new byte[twoTauPow * Lip1.length];
+		for (int i = 0; i < twoTauPow; i++)
+			System.arraycopy(c[i], 0, c_all, i * Lip1.length, Lip1.length);
+
+		Tuple out = new Tuple(Ti);
+		Util.setXor(out.getL(), Util.xor(Li, predata.ppt_Li));
+		Util.setXor(out.getA(), c_all);
+
+		timer.stop(P.PPT, M.online_comp);
+		return out;
+	}
+
+	// for testing correctness
+	@Override
+	public void run(Party party, Metadata md, Forest forest) {
+		int records = 5;
+		int repeat = 5;
+
+		int tau = md.getTau();
+		int numTrees = md.getNumTrees();
+		long numInsert = md.getNumInsertRecords();
+		int addrBits = md.getAddrBits();
+
+		Timer timer = new Timer();
+
+		sanityCheck();
+
+		System.out.println();
+
+		for (int i = 0; i < records; i++) {
+			long N = Util.nextLong(numInsert, Crypto.sr);
+
+			for (int j = 0; j < repeat; j++) {
+				System.out.println("Test: " + i + " " + j);
+				System.out.println("N=" + BigInteger.valueOf(N).toString(2));
+
+				byte[] Li = new byte[0];
+
+				PreData prev = null;
+
+				for (int ti = 0; ti < numTrees; ti++) {
+					long Ni_value = Util.getSubBits(N, addrBits, addrBits - md.getNBitsOfTree(ti));
+					long Nip1_pr_value = Util.getSubBits(N, addrBits - md.getNBitsOfTree(ti),
+							Math.max(addrBits - md.getNBitsOfTree(ti) - tau, 0));
+					byte[] Ni = Util.longToBytes(Ni_value, md.getNBytesOfTree(ti));
+					byte[] Nip1_pr = Util.longToBytes(Nip1_pr_value, (tau + 7) / 8);
+
+					PreData predata = new PreData();
+					PreAccess preaccess = new PreAccess(con1, con2);
+					Access access = new Access(con1, con2);
+					PrePostProcessT prepostprocesst = new PrePostProcessT(con1, con2);
+
+					if (party == Party.Eddie) {
+						Tree OTi = forest.getTree(ti);
+						int numTuples = (OTi.getD() - 1) * OTi.getW() + OTi.getStashSize();
+						preaccess.runE(predata, OTi, numTuples, timer);
+
+						byte[] sE_Ni = Util.nextBytes(Ni.length, Crypto.sr);
+						byte[] sD_Ni = Util.xor(Ni, sE_Ni);
+						con1.write(sD_Ni);
+
+						byte[] sE_Nip1_pr = Util.nextBytes(Nip1_pr.length, Crypto.sr);
+						byte[] sD_Nip1_pr = Util.xor(Nip1_pr, sE_Nip1_pr);
+						con1.write(sD_Nip1_pr);
+
+						OutAccess outaccess = access.runE(predata, OTi, sE_Ni, sE_Nip1_pr, timer);
+
+						if (ti == numTrees - 1)
+							con2.write(N);
+
+						prepostprocesst.runE(predata, timer);
+						Tuple Ti_prime = runE(predata, outaccess.E_Ti, ti == numTrees - 1, timer);
+
+						Ti_prime.setXor(con2.readObject());
+						byte[] Li_prime = Util.xor(predata.ppt_Li, con2.read());
+						byte[] Lip1_prime = Util.xor(predata.ppt_Lip1, con2.read());
+						int j2 = con2.readObject();
+						Tuple Ti = outaccess.E_Ti.xor(con2.readObject());
+
+						if (!Util.equal(Ti.getF(), Ti_prime.getF()))
+							System.err.println("PPT test failed");
+						else if (!Util.equal(Ti.getN(), Ti_prime.getN()))
+							System.err.println("PPT test failed");
+						else if (!Util.equal(Li_prime, Ti_prime.getL()))
+							System.err.println("PPT test failed");
+						else if (!Util.equal(Lip1_prime,
+								Ti_prime.getSubA(j2 * Lip1_prime.length, (j2 + 1) * Lip1_prime.length)))
+							System.err.println("PPT test failed");
+						else
+							System.out.println("PPT test passed");
+
+					} else if (party == Party.Debbie) {
+						Tree OTi = forest.getTree(ti);
+						preaccess.runD(predata, timer);
+
+						byte[] sD_Ni = con1.read();
+
+						byte[] sD_Nip1_pr = con1.read();
+
+						access.runD(predata, OTi, sD_Ni, sD_Nip1_pr, timer);
+
+						prepostprocesst.runD(predata, prev, md.getLBytesOfTree(ti), md.getAlBytesOfTree(ti), tau,
+								timer);
+						runD();
+
+					} else if (party == Party.Charlie) {
+						preaccess.runC(timer);
+
+						System.out.println("L" + ti + "=" + new BigInteger(1, Li).toString(2));
+
+						OutAccess outaccess = access.runC(md, ti, Li, timer);
+
+						prepostprocesst.runC(predata, prev, md.getLBytesOfTree(ti), md.getAlBytesOfTree(ti), timer);
+						Tuple Ti_prime = runC(predata, outaccess.C_Ti, Li, outaccess.C_Lip1, outaccess.C_j2,
+								ti == numTrees - 1, timer);
+
+						Li = outaccess.C_Lip1;
+
+						if (ti == numTrees - 1) {
+							N = con1.readObject();
+							long data = new BigInteger(1, outaccess.C_Ti.getA()).longValue();
+							if (N == data) {
+								System.out.println("Access passed");
+								System.out.println();
+							} else {
+								throw new AccessException("Access failed");
+							}
+						}
+
+						con1.write(Ti_prime);
+						con1.write(predata.ppt_Li);
+						con1.write(predata.ppt_Lip1);
+						con1.write(outaccess.C_j2);
+						con1.write(outaccess.C_Ti);
+
+					} else {
+						throw new NoSuchPartyException(party + "");
+					}
+
+					prev = predata;
+				}
+			}
+		}
+
+		// timer.print();
+	}
+}

+ 6 - 0
src/protocols/PreData.java

@@ -26,4 +26,10 @@ public class PreData {
 	public int[] ssxot_C_pi_ivs;
 	public Tuple[] ssxot_E_r;
 	public Tuple[] ssxot_C_r;
+
+	public byte[] ppt_Li;
+	public byte[] ppt_Lip1;
+	public int ppt_alpha;
+	public byte[][] ppt_r;
+	public byte[][] ppt_s;
 }

+ 75 - 0
src/protocols/PrePostProcessT.java

@@ -0,0 +1,75 @@
+package protocols;
+
+import communication.Communication;
+import crypto.Crypto;
+import measure.M;
+import measure.P;
+import measure.Timer;
+import oram.Forest;
+import oram.Metadata;
+import util.Util;
+
+public class PrePostProcessT extends Protocol {
+	public PrePostProcessT(Communication con1, Communication con2) {
+		super(con1, con2);
+	}
+
+	public void runE(PreData predata, Timer timer) {
+		timer.start(P.PPT, M.offline_comp);
+
+		predata.ppt_Li = con1.read();
+		predata.ppt_Lip1 = con1.read();
+
+		predata.ppt_s = con1.readObject();
+
+		timer.stop(P.PPT, M.offline_comp);
+	}
+
+	public void runD(PreData predata, PreData prev, int LiBytes, int Lip1Bytes, int tau, Timer timer) {
+		timer.start(P.PPT, M.offline_comp);
+
+		if (prev != null)
+			predata.ppt_Li = prev.ppt_Lip1;
+		else
+			predata.ppt_Li = Util.nextBytes(LiBytes, Crypto.sr);
+		predata.ppt_Lip1 = Util.nextBytes(Lip1Bytes, Crypto.sr);
+
+		int twoTauPow = (int) Math.pow(2, tau);
+		predata.ppt_alpha = Crypto.sr.nextInt(tau);
+		predata.ppt_r = new byte[twoTauPow][];
+		predata.ppt_s = new byte[twoTauPow][];
+		for (int i = 0; i < twoTauPow; i++) {
+			predata.ppt_r[i] = Util.nextBytes(Lip1Bytes, Crypto.sr);
+			predata.ppt_s[i] = predata.ppt_r[i];
+		}
+		predata.ppt_s[predata.ppt_alpha] = Util.xor(predata.ppt_r[predata.ppt_alpha], predata.ppt_Lip1);
+
+		con1.write(predata.ppt_Li);
+		con1.write(predata.ppt_Lip1);
+
+		con2.write(predata.ppt_alpha);
+		con2.write(predata.ppt_r);
+		con1.write(predata.ppt_s);
+
+		timer.stop(P.PPT, M.offline_comp);
+	}
+
+	public void runC(PreData predata, PreData prev, int LiBytes, int Lip1Bytes, Timer timer) {
+		timer.start(P.PPT, M.offline_comp);
+
+		if (prev != null)
+			predata.ppt_Li = prev.ppt_Lip1;
+		else
+			predata.ppt_Li = Util.nextBytes(LiBytes, Crypto.sr);
+		predata.ppt_Lip1 = Util.nextBytes(Lip1Bytes, Crypto.sr);
+
+		predata.ppt_alpha = con2.readObject();
+		predata.ppt_r = con2.readObject();
+
+		timer.stop(P.PPT, M.offline_comp);
+	}
+
+	@Override
+	public void run(Party party, Metadata md, Forest forest) {
+	}
+}

+ 3 - 0
src/ui/CLI.java

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

+ 2 - 0
src/util/Util.java

@@ -12,6 +12,8 @@ import exceptions.LengthNotMatchException;
 
 public class Util {
 	public static boolean equal(byte[] a, byte[] b) {
+		if (a.length == 0 && b.length == 0)
+			return true;
 		if (a.length != b.length)
 			return false;
 		return new BigInteger(a).compareTo(new BigInteger(b)) == 0;

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