TrivialPrivateOram.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 TrivialPrivateOram<T> extends OramParty<T> {
  7. public PlainBlock[] bucket;
  8. Block<T>[] result;
  9. int capacity;
  10. public TrivialPrivateOram(CompEnv<T> env, int N, int dataSize) {
  11. super(env, N, dataSize, 1);
  12. this.capacity = N;
  13. bucket = new PlainBlock[capacity];
  14. for (int i = 0; i < bucket.length; ++i) {
  15. bucket[i] = getDummyBlock(p == Party.Alice);
  16. }
  17. result = prepareBlocks(bucket, bucket);
  18. }
  19. int InitialValue = 0;
  20. public void setInitialValue(int initial) {
  21. InitialValue = initial;
  22. }
  23. public void add(T[] iden, T[] data) {
  24. T[] pos = env.newTArray(1);
  25. pos[0] = lib.SIGNAL_ONE;
  26. Block<T> scNewBlock = new Block<T>(iden, pos, data, lib.SIGNAL_ZERO);
  27. lib.add(result, scNewBlock);
  28. }
  29. public T[] readAndRemove(T[] scIden) {
  30. return readAndRemove(scIden, true);
  31. }
  32. public T[] readAndRemove(T[] scIden, boolean randomWhennotFound) {
  33. scIden = lib.padSignal(scIden, lengthOfIden);
  34. Block<T> res = lib.readAndRemove(result, scIden);
  35. T[] finalRes;
  36. if (randomWhennotFound) {
  37. PlainBlock b1 = randomBlock();
  38. Block<T> scb1 = inputBlockOfClient(b1);
  39. finalRes = lib.mux(res.data, scb1.data, res.isDummy);
  40. } else {
  41. finalRes = lib.mux(res.data, lib.toSignals(InitialValue, res.data.length), res.isDummy);
  42. }
  43. return finalRes;
  44. }
  45. public T[] read(int index) {
  46. return result[index].data;
  47. }
  48. public void write(int index, T[] d) {
  49. result[index].data = d;
  50. }
  51. public T[] read(T[] scIden) {
  52. scIden = Arrays.copyOf(scIden, lengthOfIden);
  53. T[] r = readAndRemove(scIden, false);
  54. putBack(scIden, r);
  55. return r;
  56. }
  57. public void write(T[] scIden, T[] b) {
  58. scIden = Arrays.copyOf(scIden, lengthOfIden);
  59. readAndRemove(scIden);
  60. putBack(scIden, b);
  61. }
  62. public void putBack(T[] scIden, T[] scData) {
  63. scIden = Arrays.copyOf(scIden, lengthOfIden);
  64. add(scIden, scData);
  65. }
  66. public T[] conditionalReadAndRemove(T[] iden, T condition) {
  67. return lib.conditionalReadAndRemove(result, iden, condition).data;
  68. }
  69. public void conditionalPutBack(T[] iden, T[] data, T condition) {
  70. T[] pos = env.newTArray(1);
  71. pos[0] = lib.SIGNAL_ONE;
  72. Block<T> scNewBlock = new Block<T>(iden, pos, data, lib.SIGNAL_ZERO);
  73. lib.conditionalAdd(result, scNewBlock, condition);
  74. }
  75. }