CircuitOram.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright (C) 2014 by Xiao Shaun Wang <wangxiao@cs.umd.edu>
  2. package com.oblivm.backend.oram;
  3. import java.util.Arrays;
  4. import com.oblivm.backend.flexsc.CompEnv;
  5. import com.oblivm.backend.flexsc.Party;
  6. import com.oblivm.backend.util.Utils;
  7. public class CircuitOram<T> extends TreeBasedOramParty<T> {
  8. public CircuitOramLib<T> lib;
  9. Block<T>[] scQueue;
  10. int cnt = 0;
  11. public PlainBlock[] queue;
  12. public int queueCapacity;
  13. boolean[] nextPath() {
  14. boolean[] res = new boolean[logN];
  15. int temp = cnt;
  16. for (int i = res.length - 1; i >= 0; --i) {
  17. res[i] = (temp & 1) == 1;
  18. temp >>= 1;
  19. }
  20. cnt = (cnt + 1) % N;
  21. return res;
  22. }
  23. public CircuitOram(CompEnv<T> env, int N, int dataSize, int cap, int sp) {
  24. super(env, N, dataSize, cap);
  25. lib = new CircuitOramLib<T>(lengthOfIden, lengthOfPos, lengthOfData, logN, capacity, env);
  26. queueCapacity = 30;
  27. queue = new PlainBlock[queueCapacity];
  28. for (int i = 0; i < queue.length; ++i)
  29. queue[i] = getDummyBlock(p == Party.Alice);
  30. scQueue = prepareBlocks(queue, queue);
  31. }
  32. public CircuitOram(CompEnv<T> env, int N, int dataSize) {
  33. super(env, N, dataSize, 3);
  34. lib = new CircuitOramLib<T>(lengthOfIden, lengthOfPos, lengthOfData, logN, capacity, env);
  35. queueCapacity = 30;
  36. queue = new PlainBlock[queueCapacity];
  37. for (int i = 0; i < queue.length; ++i)
  38. queue[i] = getDummyBlock(p == Party.Alice);
  39. scQueue = prepareBlocks(queue, queue);
  40. }
  41. protected void ControlEviction() {
  42. flushOneTime(nextPath());
  43. flushOneTime(nextPath());
  44. }
  45. public void flushOneTime(boolean[] pos) {
  46. PlainBlock[][] blocks = getPath(pos);
  47. Block<T>[][] scPath = preparePath(blocks, blocks);
  48. lib.flush(scPath, pos, scQueue);
  49. blocks = preparePlainPath(scPath);
  50. putPath(blocks, pos);
  51. }
  52. int initalValue = 0;
  53. public void setInitialValue(int intial) {
  54. initalValue = intial;
  55. }
  56. public T[] readAndRemove(T[] scIden, boolean[] pos, boolean RandomWhenNotFound) {
  57. PlainBlock[][] blocks = getPath(pos);
  58. Block<T>[][] scPath = preparePath(blocks, blocks);
  59. Block<T> res = lib.readAndRemove(scPath, scIden);
  60. Block<T> res2 = lib.readAndRemove(scQueue, scIden);
  61. res = lib.mux(res, res2, res.isDummy);
  62. blocks = preparePlainPath(scPath);
  63. putPath(blocks, pos);
  64. if (RandomWhenNotFound) {
  65. PlainBlock b = randomBlock();
  66. Block<T> scb = inputBlockOfClient(b);
  67. Block<T> finalRes = lib.mux(res, scb, res.isDummy);
  68. return finalRes.data;
  69. } else {
  70. return lib.mux(res.data, lib.toSignals(initalValue, res.data.length), res.isDummy);
  71. }
  72. }
  73. public void putBack(T[] scIden, T[] scNewPos, T[] scData) {
  74. Block<T> b = new Block<T>(scIden, scNewPos, scData, lib.SIGNAL_ZERO);
  75. lib.add(scQueue, b);
  76. env.flush();
  77. ControlEviction();
  78. }
  79. public T[] read(T[] scIden, boolean[] pos, T[] scNewPos) {
  80. scIden = Arrays.copyOf(scIden, lengthOfIden);
  81. T[] r = readAndRemove(scIden, pos, false);
  82. putBack(scIden, scNewPos, r);
  83. return r;
  84. }
  85. public void write(T[] scIden, boolean[] pos, T[] scNewPos, T[] scData) {
  86. scIden = Arrays.copyOf(scIden, lengthOfIden);
  87. readAndRemove(scIden, pos, false);
  88. putBack(scIden, scNewPos, scData);
  89. }
  90. public void write(T[] scIden, T[] pos, T[] scNewPos, T[] scData, T dummy) {
  91. scIden = Arrays.copyOf(scIden, lengthOfIden);
  92. conditionalReadAndRemove(scIden, pos, dummy);
  93. conditionalPutBack(scIden, scNewPos, scData, dummy);
  94. }
  95. public T[] access(T[] scIden, boolean[] pos, T[] scNewPos, T[] scData, T op) {
  96. scIden = Arrays.copyOf(scIden, lengthOfIden);
  97. T[] r = readAndRemove(scIden, pos, false);
  98. T[] toWrite = lib.mux(r, scData, op);
  99. putBack(scIden, scNewPos, toWrite);
  100. return toWrite;
  101. }
  102. public T[] conditionalReadAndRemove(T[] scIden, T[] pos, T condition) {
  103. // Utils.print(env, "rar: iden:", scIden, pos, condition);
  104. scIden = Arrays.copyOf(scIden, lengthOfIden);
  105. T[] scPos = Arrays.copyOf(pos, lengthOfPos);
  106. T[] randbools = lib.randBools(scPos.length);
  107. T[] posToUse = lib.mux(randbools, scPos, condition);
  108. boolean[] path = lib.declassifyToBoth(posToUse);
  109. PlainBlock[][] blocks = getPath(path);
  110. Block<T>[][] scPath = preparePath(blocks, blocks);
  111. Block<T> res = lib.conditionalReadAndRemove(scPath, scIden, condition);
  112. Block<T> res2 = lib.conditionalReadAndRemove(scQueue, scIden, condition);
  113. res = lib.mux(res, res2, res.isDummy);
  114. blocks = preparePlainPath(scPath);
  115. putPath(blocks, path);
  116. env.flush();
  117. return lib.mux(res.data, lib.toSignals(initalValue, res.data.length), res.isDummy);
  118. }
  119. public void conditionalPutBack(T[] scIden, T[] scNewPos, T[] scData, T condition) {
  120. env.flush();
  121. scIden = Arrays.copyOf(scIden, lengthOfIden);
  122. Block<T> b = new Block<T>(scIden, scNewPos, scData, lib.SIGNAL_ZERO);
  123. lib.conditionalAdd(scQueue, b, condition);
  124. env.flush();
  125. ControlEviction();
  126. }
  127. }