LinearScanOram.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (C) 2014 by Xiao Shaun Wang <wangxiao@cs.umd.edu>
  2. package com.oblivm.backend.oram;
  3. import com.oblivm.backend.circuits.arithmetic.IntegerLib;
  4. import com.oblivm.backend.flexsc.CompEnv;
  5. import com.oblivm.backend.util.Utils;
  6. public class LinearScanOram<T> {
  7. public T[][] content;
  8. public CompEnv<T> env;
  9. public int lengthOfIden;
  10. public IntegerLib<T> lib;
  11. public int dataSize;
  12. public LinearScanOram(CompEnv<T> env, int N, int dataSize) {
  13. this.env = env;
  14. this.dataSize = dataSize;
  15. lib = new IntegerLib<T>(env);
  16. content = env.newTArray(N, 0);
  17. lengthOfIden = Utils.log2Ceil(N);
  18. for (int i = 0; i < N; ++i)
  19. content[i] = lib.zeros(dataSize);
  20. }
  21. public void add(T[] iden, T[] data, T dummy) {
  22. T[] iden1 = lib.padSignal(iden, lengthOfIden);
  23. for (int i = 0; i < content.length; ++i) {
  24. T eq = lib.eq(iden1, lib.toSignals(i, lengthOfIden));
  25. eq = lib.and(eq, dummy);
  26. content[i] = lib.mux(content[i], data, eq);
  27. }
  28. }
  29. public void add(T[] iden, T[] data) {
  30. T[] iden1 = lib.padSignal(iden, lengthOfIden);
  31. for (int i = 0; i < content.length; ++i) {
  32. T eq = lib.eq(iden1, lib.toSignals(i, lengthOfIden));
  33. content[i] = lib.mux(content[i], data, eq);
  34. }
  35. }
  36. public T[] readAndRemove(T[] iden) {
  37. return readAndRemove(iden, false);
  38. }
  39. public T[] readAndRemove(T[] iden, boolean randomWhennotFound) {
  40. T[] iden1 = lib.padSignal(iden, lengthOfIden);
  41. T[] res = lib.zeros(content[0].length);
  42. for (int i = 0; i < content.length; ++i) {
  43. T eq = lib.eq(iden1, lib.toSignals(i, lengthOfIden));
  44. res = lib.mux(res, content[i], eq);
  45. }
  46. return res;
  47. }
  48. public T[] read(T[] iden) {
  49. return readAndRemove(iden, false);
  50. }
  51. public void write(T[] iden, T[] data) {
  52. add(iden, data);
  53. }
  54. public void write(T[] iden, T[] data, T dummy) {
  55. add(iden, data, dummy);
  56. }
  57. public void putBack(T[] scIden, T[] scData) {
  58. add(scIden, scData);
  59. }
  60. }