RecursiveCircuitOram.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (C) 2014 by Xiao Shaun Wang <wangxiao@cs.umd.edu>
  2. package com.oblivm.backend.oram;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import com.oblivm.backend.flexsc.CompEnv;
  6. import com.oblivm.backend.flexsc.Party;
  7. public class RecursiveCircuitOram<T> {
  8. public LinearScanOram<T> baseOram;
  9. public ArrayList<CircuitOram<T>> clients = new ArrayList<>();
  10. public int lengthOfIden;
  11. int recurFactor;
  12. int cutoff;
  13. int capacity;
  14. Party p;
  15. public RecursiveCircuitOram(CompEnv<T> env, int N, int dataSize, int cutoff, int recurFactor, int capacity,
  16. int sp) {
  17. init(env, N, dataSize, cutoff, recurFactor, capacity, sp);
  18. }
  19. public RecursiveCircuitOram(CompEnv<T> env, int N, int dataSize, int cutoff, int recurFactor) {
  20. init(env, N, dataSize, cutoff, recurFactor, 3, 80);
  21. }
  22. // with default params
  23. public RecursiveCircuitOram(CompEnv<T> env, int N, int dataSize) {
  24. init(env, N, dataSize, 1 << 6, 8, 3, 80);
  25. }
  26. public void setInitialValue(int initial) {
  27. clients.get(0).setInitialValue(initial);
  28. }
  29. void init(CompEnv<T> env, int N, int dataSize, int cutoff, int recurFactor, int capacity, int sp) {
  30. this.p = env.party;
  31. this.cutoff = cutoff;
  32. this.recurFactor = recurFactor;
  33. this.capacity = capacity;
  34. CircuitOram<T> oram = new CircuitOram<T>(env, N, dataSize, capacity, sp);
  35. lengthOfIden = oram.lengthOfIden;
  36. clients.add(oram);
  37. int newDataSize = oram.lengthOfPos * recurFactor, newN = (1 << oram.lengthOfIden) / recurFactor;
  38. while (newN > cutoff) {
  39. oram = new CircuitOram<T>(env, newN, newDataSize, capacity, sp);
  40. clients.add(oram);
  41. newDataSize = oram.lengthOfPos * recurFactor;
  42. newN = (1 << oram.lengthOfIden) / recurFactor;
  43. }
  44. CircuitOram<T> last = clients.get(clients.size() - 1);
  45. baseOram = new LinearScanOram<T>(env, (1 << last.lengthOfIden), last.lengthOfPos);
  46. }
  47. public T[] read(T[] iden) {
  48. T[][] poses = travelToDeep(iden, 1);
  49. CircuitOram<T> currentOram = clients.get(0);
  50. boolean[] oldPos = baseOram.lib.declassifyToBoth(poses[0]);
  51. T[] res = currentOram.read(iden, oldPos, poses[1]);
  52. return res;
  53. }
  54. public void write(T[] iden, T[] data) {
  55. T[][] poses = travelToDeep(iden, 1);
  56. CircuitOram<T> currentOram = clients.get(0);
  57. boolean[] oldPos = baseOram.lib.declassifyToBoth(poses[0]);
  58. currentOram.write(iden, oldPos, poses[1], data);
  59. }
  60. public void write(T[] iden, T[] data, T dummy) {
  61. T[][] poses = travelToDeep(iden, 1);
  62. CircuitOram<T> currentOram = clients.get(0);
  63. currentOram.write(iden, poses[0], poses[1], data, dummy);
  64. }
  65. public T[] access(T[] iden, T[] data, T op) {
  66. T[][] poses = travelToDeep(iden, 1);
  67. CircuitOram<T> currentOram = clients.get(0);
  68. boolean[] oldPos = baseOram.lib.declassifyToBoth(poses[0]);
  69. return currentOram.access(iden, oldPos, poses[1], data, op);
  70. }
  71. public T[][] travelToDeep(T[] iden, int level) {
  72. if (level == clients.size()) {
  73. T[] baseMap = baseOram.readAndRemove(baseOram.lib.padSignal(iden, baseOram.lengthOfIden));
  74. T[] ithPos = baseOram.lib.rightPublicShift(iden, baseOram.lengthOfIden);// iden>>baseOram.lengthOfIden;
  75. T[] pos = extract(baseMap, ithPos, clients.get(level - 1).lengthOfPos);
  76. T[] newPos = baseOram.lib.randBools(clients.get(level - 1).lengthOfPos);
  77. put(baseMap, ithPos, newPos);
  78. baseOram.putBack(baseOram.lib.padSignal(iden, baseOram.lengthOfIden), baseMap);
  79. T[][] result = baseOram.env.newTArray(2, 0);
  80. result[0] = pos;
  81. result[1] = newPos;
  82. return result;
  83. } else {
  84. CircuitOram<T> currentOram = clients.get(level);
  85. T[][] poses = travelToDeep(subIdentifier(iden, currentOram), level + 1);
  86. boolean[] oldPos = baseOram.lib.declassifyToBoth(poses[0]);
  87. T[] data = currentOram.readAndRemove(subIdentifier(iden, currentOram), oldPos, true);
  88. T[] ithPos = currentOram.lib.rightPublicShift(iden, currentOram.lengthOfIden);// iden>>currentOram.lengthOfIden;//iden/(1<<currentOram.lengthOfIden);
  89. T[] pos = extract(data, ithPos, clients.get(level - 1).lengthOfPos);
  90. T[] tmpNewPos = baseOram.lib.randBools(clients.get(level - 1).lengthOfPos);
  91. put(data, ithPos, tmpNewPos);
  92. currentOram.putBack(subIdentifier(iden, currentOram), poses[1], data);
  93. T[][] result = currentOram.env.newTArray(2, 0);
  94. result[0] = pos;
  95. result[1] = tmpNewPos;
  96. return result;
  97. }
  98. }
  99. public T[] subIdentifier(T[] iden, OramParty<T> o) {
  100. // int a = iden & ((1<<o.lengthOfIden)-1);//(iden % (1<<o.lengthOfIden))
  101. return o.lib.padSignal(iden, o.lengthOfIden);
  102. }
  103. public T[] extract(T[] array, T[] ithPos, int length) {
  104. int numberOfEntry = array.length / length;
  105. T[] result = Arrays.copyOfRange(array, 0, length);
  106. for (int i = 1; i < numberOfEntry; ++i) {
  107. T hit = baseOram.lib.eq(baseOram.lib.toSignals(i, ithPos.length), ithPos);
  108. result = baseOram.lib.mux(result, Arrays.copyOfRange(array, i * length, (i + 1) * length), hit);
  109. }
  110. return result;
  111. }
  112. public void put(T[] array, T[] ithPos, T[] content) {
  113. int numberOfEntry = array.length / content.length;
  114. for (int i = 0; i < numberOfEntry; ++i) {
  115. T hit = baseOram.lib.eq(baseOram.lib.toSignals(i, ithPos.length), ithPos);
  116. T[] tmp = baseOram.lib.mux(Arrays.copyOfRange(array, i * content.length, (i + 1) * content.length), content,
  117. hit);
  118. System.arraycopy(tmp, 0, array, i * content.length, content.length);
  119. }
  120. }
  121. }