Forráskód Böngészése

fixed problem of too many writes and much overhead!

Boyoung- 8 éve
szülő
commit
f4e61e1ddd

+ 2 - 89
src/communication/Communication.java

@@ -5,20 +5,17 @@ import java.io.DataOutputStream;
 import java.io.IOException;
 import java.io.Serializable;
 import java.io.StreamCorruptedException;
-import java.math.BigInteger;
 import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.SocketException;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
 
 import org.apache.commons.lang3.SerializationUtils;
 
-import oram.Tuple;
 import util.Util;
 
 /**
@@ -332,44 +329,9 @@ public class Communication {
 		write(out);
 	}
 
-	public void write(int out) {
-		write(BigInteger.valueOf(out).toByteArray());
-	}
-
-	public void write(byte[][] out) {
-		write(out.length);
-		for (int i = 0; i < out.length; i++)
-			write(out[i]);
-	}
-
-	public void write(int[] out) {
-		write(out.length);
-		for (int i = 0; i < out.length; i++)
-			write(out[i]);
-	}
-
-	private <T> void write(T out) {
+	public <T> void write(T out) {
 		write(SerializationUtils.serialize((Serializable) out));
 	}
-	
-	/*
-	public <T> void write(T[] out) {
-		write(out.length);
-		for (int i = 0; i < out.length; i++)
-			write(out[i]);
-	}
-	*/
-	
-	public void write(Tuple[] tuples) {
-		write(tuples.length);
-		for (int i=0; i<tuples.length; i++) {
-			write(tuples[i].getF().length);
-			write(tuples[i].getN().length);
-			write(tuples[i].getL().length);
-			write(tuples[i].getA().length);
-			write(tuples[i].toByteArray());
-		}
-	}
 
 	public static final Charset defaultCharset = Charset.forName("ASCII");
 
@@ -451,60 +413,11 @@ public class Communication {
 		return total;
 	}
 
-	public int readInt() {
-		return new BigInteger(read()).intValue();
-	}
-
-	public byte[][] readDoubleByteArray() {
-		int len = readInt();
-		byte[][] data = new byte[len][];
-		for (int i = 0; i < len; i++)
-			data[i] = read();
-		return data;
-	}
-
-	public int[] readIntArray() {
-		int len = readInt();
-		int[] data = new int[len];
-		for (int i = 0; i < len; i++)
-			data[i] = readInt();
-		return data;
-	}
-
-	private <T> T readObject() {
+	public <T> T readObject() {
 		T object = SerializationUtils.deserialize(read());
 		return object;
 	}
 
-	/*
-	public <T> T[] readObjectArray() {
-		int len = readInt();
-		@SuppressWarnings("unchecked")
-		T[] data = (T[]) new Object[len];
-		for (int i = 0; i < len; i++)
-			data[i] = readObject();
-		return data;
-	}
-	*/
-	
-	public Tuple[] readTupleArray() {
-		int len = readInt();
-		Tuple[] tuples = new Tuple[len];
-		for (int i=0; i<len; i++) {
-			int f = readInt();
-			int n = readInt();
-			int l = readInt();
-			int a = readInt();
-			tuples[i] = new Tuple(new byte[f], new byte[n], new byte[l], new byte[a]);
-			byte[] data = read();
-			tuples[i].setF(Arrays.copyOfRange(data, 0, f));
-			tuples[i].setN(Arrays.copyOfRange(data, f, f+n));
-			tuples[i].setL(Arrays.copyOfRange(data, f+n, f+n+l));
-			tuples[i].setA(Arrays.copyOfRange(data, f+n+l, data.length));
-		}
-		return tuples;
-	}
-
 	/**
 	 * This thread runs while listening for incoming connections. It behaves
 	 * like a server-side client. It runs until a connection is accepted (or

+ 8 - 33
src/protocols/Access.java

@@ -18,7 +18,7 @@ import util.StopWatch;
 import util.Util;
 
 public class Access extends Protocol {
-	
+
 	private StopWatch step0;
 	private StopWatch step1;
 	private StopWatch step2;
@@ -28,7 +28,7 @@ public class Access extends Protocol {
 
 	public Access(Communication con1, Communication con2) {
 		super(con1, con2);
-		
+
 		step0 = new StopWatch();
 		step1 = new StopWatch();
 		step2 = new StopWatch();
@@ -124,17 +124,10 @@ public class Access extends Protocol {
 		pathTuples = Arrays.copyOf(objArray, objArray.length, Tuple[].class);
 		step1.stop();
 
-		byte[] test = Util.nextBytes(pathTuples.length*pathTuples[0].getNumBytes(), Crypto.sr);
-		
 		step2.start();
 		// step 2
-		//con2.write(pathTuples);
-		con2.write(pathTuples.length);
-		for (int i=0; i<pathTuples.length; i++)
-			con2.write(pathTuples[i].toByteArray());
+		con2.write(pathTuples);
 		con2.write(Ni);
-		
-		//con2.write(test);
 		step2.stop();
 
 		step3.start();
@@ -173,26 +166,8 @@ public class Access extends Protocol {
 
 		step2.start();
 		// step 2
-		//Object[] objArray = con2.readObjectArray();
-		//Tuple[] pathTuples = Arrays.copyOf(objArray, objArray.length, Tuple[].class);
-		//Tuple[] pathTuples = con2.readTupleArray();
-		int numTuples = con2.readInt();
-		Tuple[] pathTuples = new Tuple[numTuples];
-		for (int i=0; i<numTuples; i++) {
-			byte[] data = con2.read();
-			int f = treeIndex==0?0:1;
-			int n = md.getNBytesOfTree(treeIndex);
-			int l = md.getLBytesOfTree(treeIndex);
-			int a = md.getABytesOfTree(treeIndex);
-			pathTuples[i] = new Tuple(new byte[f], new byte[n], new byte[l], new byte[a]);
-			pathTuples[i].setF(Arrays.copyOfRange(data, 0, f));
-			pathTuples[i].setN(Arrays.copyOfRange(data, f, f+n));
-			pathTuples[i].setL(Arrays.copyOfRange(data, f+n, f+n+l));
-			pathTuples[i].setA(Arrays.copyOfRange(data, f+n+l, data.length));
-		}
+		Tuple[] pathTuples = con2.readObject();
 		byte[] Ni = con2.read();
-		
-		//byte[] test = con2.read();
 		step2.stop();
 
 		step3.start();
@@ -209,7 +184,7 @@ public class Access extends Protocol {
 			z = Util.xor(je.m_t, d);
 		}
 		step3.stop();
-		
+
 		step4.start();
 		// step 4
 		int j2 = 0;
@@ -297,7 +272,7 @@ public class Access extends Protocol {
 						stopwatch.stop();
 
 						if (ti == numTrees - 1)
-							con2.write(BigInteger.valueOf(N).toByteArray());
+							con2.write(N);
 
 					} else if (party == Party.Debbie) {
 						Tree OTi = forest.getTree(ti);
@@ -323,7 +298,7 @@ public class Access extends Protocol {
 						Li = outaccess.C_Lip1;
 
 						if (ti == numTrees - 1) {
-							N = new BigInteger(con1.read()).longValue();
+							N = con1.readObject();
 							long data = new BigInteger(1, outaccess.C_Ti.getA()).longValue();
 							if (N == data) {
 								System.out.println("Access passed");
@@ -341,7 +316,7 @@ public class Access extends Protocol {
 		}
 
 		System.out.println(stopwatch.toMS());
-		
+
 		System.out.println("step0\n" + step0.toMS());
 		System.out.println("step1\n" + step1.toMS());
 		System.out.println("step2\n" + step2.toMS());

+ 2 - 6
src/protocols/PreAccess.java

@@ -1,7 +1,5 @@
 package protocols;
 
-import java.util.Arrays;
-
 import communication.Communication;
 import crypto.Crypto;
 import oram.Forest;
@@ -44,10 +42,8 @@ public class PreAccess extends Protocol {
 		pressiot.runD(predata);
 
 		// Access
-		predata.access_sigma = con1.readIntArray();
-		//Object[] objArray = con1.readObjectArray();
-		//predata.access_p = Arrays.copyOf(objArray, objArray.length, Tuple[].class);
-		predata.access_p = con1.readTupleArray();
+		predata.access_sigma = con1.readObject();
+		predata.access_p = con1.readObject();
 	}
 
 	public void runC() {

+ 1 - 1
src/protocols/PreSSCOT.java

@@ -27,7 +27,7 @@ public class PreSSCOT extends Protocol {
 	public void runD(PreData predata) {
 		predata.sscot_k = con1.read();
 		predata.sscot_kprime = con1.read();
-		predata.sscot_r = con1.readDoubleByteArray();
+		predata.sscot_r = con1.readObject();
 	}
 
 	public void runC() {

+ 7 - 7
src/protocols/SSCOT.java

@@ -65,12 +65,12 @@ public class SSCOT extends Protocol {
 
 	public OutSSCOT runC() {
 		// step 1
-		byte[][] e = con1.readDoubleByteArray();
-		byte[][] v = con1.readDoubleByteArray();
+		byte[][] e = con1.readObject();
+		byte[][] v = con1.readObject();
 
 		// step 2
-		byte[][] p = con2.readDoubleByteArray();
-		byte[][] w = con2.readDoubleByteArray();
+		byte[][] p = con2.readObject();
+		byte[][] w = con2.readObject();
 
 		// step 3
 		int n = e.length;
@@ -121,13 +121,13 @@ public class SSCOT extends Protocol {
 				runE(predata, m, a);
 
 			} else if (party == Party.Debbie) {
-				b = con1.readDoubleByteArray();
+				b = con1.readObject();
 				presscot.runD(predata);
 				runD(predata, b);
 
 			} else if (party == Party.Charlie) {
-				m = con1.readDoubleByteArray();
-				index = con1.readInt();
+				m = con1.readObject();
+				index = con1.readObject();
 				presscot.runC();
 				OutSSCOT output = runC();
 				if (output.t == index && Util.equal(output.m_t, m[index]))

+ 4 - 4
src/protocols/SSIOT.java

@@ -61,8 +61,8 @@ public class SSIOT extends Protocol {
 
 	public OutSSIOT runC() {
 		// step 1
-		byte[][] e = con1.readDoubleByteArray();
-		byte[][] v = con1.readDoubleByteArray();
+		byte[][] e = con1.readObject();
+		byte[][] v = con1.readObject();
 
 		// step 2
 		byte[] p = con2.read();
@@ -118,8 +118,8 @@ public class SSIOT extends Protocol {
 				runD(predata, sD_Nip1_pr);
 
 			} else if (party == Party.Charlie) {
-				y = con1.readDoubleByteArray();
-				index = con1.readInt();
+				y = con1.readObject();
+				index = con1.readObject();
 				pressiot.runC();
 				OutSSIOT output = runC();
 				if (output.t == index && Util.equal(output.m_t, y[index]))

+ 8 - 7
test/misc/HelloWorld.java

@@ -35,23 +35,24 @@ public class HelloWorld {
 		 * byte aa = 1; aa ^= 1; System.out.println(aa);
 		 */
 
-		/*Metadata md = new Metadata();
-		Forest forest = Forest.readFromFile(md.getDefaultForestFileName());
-		forest.print();*/
-		
+		/*
+		 * Metadata md = new Metadata(); Forest forest =
+		 * Forest.readFromFile(md.getDefaultForestFileName()); forest.print();
+		 */
+
 		StopWatch sw1 = new StopWatch();
 		StopWatch sw2 = new StopWatch();
 		byte[] arr1 = Util.nextBytes((int) Math.pow(2, 20), Crypto.sr);
 		byte[] arr2 = Util.nextBytes((int) Math.pow(2, 20), Crypto.sr);
-		
+
 		sw1.start();
 		Util.xor(arr1, arr2);
 		sw1.stop();
-		
+
 		sw2.start();
 		new BigInteger(1, arr1).xor(new BigInteger(1, arr2)).toByteArray();
 		sw2.stop();
-		
+
 		System.out.println(sw1.toMS());
 		System.out.println(sw2.toMS());
 	}

+ 1 - 1
test/util/TestStopWatch.java

@@ -12,7 +12,7 @@ public class TestStopWatch {
 		}
 		sw.stop();
 		System.out.println(sw.toMS());
-		
+
 		sw.start();
 		try {
 			Thread.sleep(1000);