Browse Source

add local access test

Boyoung- 8 years ago
parent
commit
393fced869
2 changed files with 77 additions and 6 deletions
  1. 6 0
      src/oram/Forest.java
  2. 71 6
      test/oram/TestForest.java

+ 6 - 0
src/oram/Forest.java

@@ -20,6 +20,12 @@ public class Forest {
 		insertRecords(md);
 	}
 
+	public Forest(Metadata md) {
+		numBytes = md.getForestBytes();
+		initTrees(md, null);
+		insertRecords(md);
+	}
+
 	// build empty/random content forest
 	public Forest(Random rand) {
 		Metadata md = new Metadata();

+ 71 - 6
test/oram/TestForest.java

@@ -1,14 +1,79 @@
 package oram;
 
+import java.math.BigInteger;
+
+import crypto.OramCrypto;
+import util.Util;
+
 public class TestForest {
 
 	public static void main(String[] args) {
-		Forest forest1 = new Forest();
-		Forest forest2 = new Forest();
-		Forest forest3 = forest1.xor(forest2);
-		forest1.print();
-		forest2.print();
-		forest3.print();
+		Metadata md = new Metadata();
+		Forest forest = new Forest(md);
+		int tau = md.getTau();
+		int addrBits = md.getAddrBits();
+		long numRecords = md.getNumInsertRecords();
+		int numTrees = forest.getNumTrees();
+
+		int numTests = 100;
+		for (int n = 0; n < numTests; n++) {
+			// address of record we want to test
+			long testAddr = Util.nextLong(OramCrypto.sr, numRecords);
+			long L = 0;
+			long outRecord = 0;
+
+			for (int i = 0; i < numTrees; i++) {
+				// set address of tuple and index of label for searching
+				long N;
+				int indexN;
+				if (i == 0) {
+					N = 0;
+					indexN = Util.getSubBits(BigInteger.valueOf(testAddr), addrBits, addrBits - tau).intValue();
+				} else if (i < numTrees - 1) {
+					N = Util.getSubBits(testAddr, addrBits, addrBits - i * tau);
+					indexN = Util.getSubBits(BigInteger.valueOf(testAddr), addrBits - i * tau,
+							Math.max(addrBits - (i + 1) * tau, 0)).intValue();
+				} else {
+					N = testAddr;
+					indexN = 0;
+				}
+
+				// get the path buckets and search for the tuple
+				Tree tree = forest.getTree(i);
+				Bucket[] pathBuckets = tree.getBucketsOnPath(L);
+				Tuple targetTuple = null;
+				if (i == 0) {
+					targetTuple = pathBuckets[0].getTuple(0);
+				} else {
+					for (int j = 0; j < pathBuckets.length; j++) {
+						for (int k = 0; k < pathBuckets[j].getNumTuples(); k++) {
+							Tuple tuple = pathBuckets[j].getTuple(k);
+							if (tuple.getF()[0] == 1 && new BigInteger(1, tuple.getL()).longValue() == L
+									&& new BigInteger(1, tuple.getN()).longValue() == N) {
+								targetTuple = tuple;
+								break;
+							}
+						}
+						if (targetTuple != null)
+							break;
+					}
+				}
+
+				// retrieve the next label or record from the tuple
+				if (i < numTrees - 1)
+					L = new BigInteger(1,
+							targetTuple.getSubA(indexN * tree.getAlBytes(), (indexN + 1) * tree.getAlBytes()))
+									.longValue();
+				else
+					outRecord = new BigInteger(1, targetTuple.getA()).longValue();
+			}
+
+			// verify correctness
+			if (testAddr == outRecord)
+				System.out.println("Success on address " + BigInteger.valueOf(testAddr).toString(2));
+			else
+				System.err.println("Error on address " + BigInteger.valueOf(testAddr).toString(2));
+		}
 	}
 
 }