CircuitOram.java 4.6 KB

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