SecureArray.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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.gc.BadLabelException;
  6. public class SecureArray<T> {
  7. static final int threshold = 256;
  8. boolean useTrivialOram = false;
  9. public LinearScanOram<T> trivialOram = null;
  10. public RecursiveCircuitOram<T> circuitOram = null;
  11. public int lengthOfIden;
  12. public int length;
  13. public int dataSize;
  14. public SecureArray(CompEnv<T> env, int N, int dataSize) throws Exception {
  15. length = N;
  16. this.dataSize = dataSize;
  17. useTrivialOram = N <= threshold;
  18. if (useTrivialOram) {
  19. trivialOram = new LinearScanOram<T>(env, N, dataSize);
  20. lengthOfIden = trivialOram.lengthOfIden;
  21. } else {
  22. circuitOram = new RecursiveCircuitOram<T>(env, N, dataSize);
  23. lengthOfIden = circuitOram.lengthOfIden;
  24. }
  25. }
  26. public T[] readAndRemove(T[] iden) throws BadLabelException {
  27. return circuitOram.clients.get(0).readAndRemove(iden,
  28. Arrays.copyOfRange(circuitOram.clients.get(0).lib.declassifyToBoth(iden), 0,
  29. circuitOram.clients.get(0).lengthOfPos),
  30. false);
  31. }
  32. public T[] read(T[] iden) throws BadLabelException {
  33. if (useTrivialOram)
  34. return trivialOram.read(iden);
  35. else
  36. return circuitOram.read(iden);
  37. }
  38. public void write(T[] iden, T[] data) throws Exception {
  39. if (useTrivialOram)
  40. trivialOram.write(iden, data);
  41. else
  42. circuitOram.write(iden, data);
  43. }
  44. public void conditionalWrite(T[] iden, T[] data, T condition) throws BadLabelException {
  45. if (useTrivialOram) {
  46. T[] readData = trivialOram.readAndRemove(iden);
  47. T[] toAdd = trivialOram.lib.mux(readData, data, condition);
  48. trivialOram.putBack(iden, toAdd);
  49. } else {
  50. // op == 1 means write, 0 means read
  51. circuitOram.access(iden, data, condition);
  52. }
  53. }
  54. }