TestForest.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package oram;
  2. import java.math.BigInteger;
  3. import crypto.OramCrypto;
  4. import util.Util;
  5. public class TestForest {
  6. public static void main(String[] args) {
  7. Metadata md = new Metadata();
  8. // Forest forest = new Forest(md);
  9. Forest forest = Forest.readFromFile(md.getDefaultForestFileName());
  10. int tau = md.getTau();
  11. int addrBits = md.getAddrBits();
  12. long numRecords = md.getNumInsertRecords();
  13. int numTrees = forest.getNumTrees();
  14. int numTests = 100;
  15. for (int n = 0; n < numTests; n++) {
  16. // address of record we want to test
  17. long testAddr = Util.nextLong(OramCrypto.sr, numRecords);
  18. long L = 0;
  19. long outRecord = 0;
  20. for (int i = 0; i < numTrees; i++) {
  21. // set address of tuple and index of label for searching
  22. long N;
  23. int indexN;
  24. if (i == 0) {
  25. N = 0;
  26. indexN = Util.getSubBits(BigInteger.valueOf(testAddr), addrBits, addrBits - tau).intValue();
  27. } else if (i < numTrees - 1) {
  28. N = Util.getSubBits(testAddr, addrBits, addrBits - i * tau);
  29. indexN = Util.getSubBits(BigInteger.valueOf(testAddr), addrBits - i * tau,
  30. Math.max(addrBits - (i + 1) * tau, 0)).intValue();
  31. } else {
  32. N = testAddr;
  33. indexN = 0;
  34. }
  35. // get the path buckets and search for the tuple
  36. Tree tree = forest.getTree(i);
  37. Bucket[] pathBuckets = tree.getBucketsOnPath(L);
  38. Tuple targetTuple = null;
  39. if (i == 0) {
  40. targetTuple = pathBuckets[0].getTuple(0);
  41. } else {
  42. for (int j = 0; j < pathBuckets.length; j++) {
  43. for (int k = 0; k < pathBuckets[j].getNumTuples(); k++) {
  44. Tuple tuple = pathBuckets[j].getTuple(k);
  45. if (tuple.getF()[0] == 1 && new BigInteger(1, tuple.getL()).longValue() == L
  46. && new BigInteger(1, tuple.getN()).longValue() == N) {
  47. targetTuple = tuple;
  48. break;
  49. }
  50. }
  51. if (targetTuple != null)
  52. break;
  53. }
  54. }
  55. // retrieve the next label or record from the tuple
  56. if (i < numTrees - 1)
  57. L = new BigInteger(1,
  58. targetTuple.getSubA(indexN * tree.getAlBytes(), (indexN + 1) * tree.getAlBytes()))
  59. .longValue();
  60. else
  61. outRecord = new BigInteger(1, targetTuple.getA()).longValue();
  62. }
  63. // verify correctness
  64. if (testAddr == outRecord)
  65. System.out.println("Success on address " + BigInteger.valueOf(testAddr).toString(2));
  66. else
  67. System.err.println("Error on address " + BigInteger.valueOf(testAddr).toString(2));
  68. }
  69. }
  70. }