Sort.java 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package com.oblivm.backend.example;
  2. import java.util.Arrays;
  3. import com.oblivm.backend.circuits.BitonicSortLib;
  4. import com.oblivm.backend.flexsc.CompEnv;
  5. import com.oblivm.backend.util.EvaRunnable;
  6. import com.oblivm.backend.util.GenRunnable;
  7. import com.oblivm.backend.util.Utils;
  8. public class Sort {
  9. static public <T> void compute(CompEnv<T> gen, T[][] inputB) {
  10. BitonicSortLib<T> lib = new BitonicSortLib<T>(gen);
  11. lib.sort(inputB, lib.SIGNAL_ONE);
  12. }
  13. public static class Generator<T> extends GenRunnable<T> {
  14. T[][] inputB;
  15. @Override
  16. public void prepareInput(CompEnv<T> gen) {
  17. inputB = gen.newTArray(4000, 0);
  18. T[] scTemp = gen.inputOfBob(new boolean[4000 * 16]);
  19. for (int i = 0; i < inputB.length; ++i)
  20. inputB[i] = Arrays.copyOfRange(scTemp, i * 16, i * 16 + 16);
  21. }
  22. @Override
  23. public void secureCompute(CompEnv<T> gen) {
  24. compute(gen, inputB);
  25. }
  26. @Override
  27. public void prepareOutput(CompEnv<T> gen) {
  28. }
  29. }
  30. public static class Evaluator<T> extends EvaRunnable<T> {
  31. T[][] inputB;
  32. T[] scResult;
  33. @Override
  34. public void prepareInput(CompEnv<T> gen) {
  35. inputB = gen.newTArray(4000, 0);
  36. boolean[] temp = new boolean[4000 * 16];
  37. for (int i = 0; i < 4000; ++i)
  38. System.arraycopy(Utils.fromInt(CompEnv.rnd.nextInt(), 16), 0, temp, 16 * i, 16);
  39. T[] scTemp = gen.inputOfBob(temp);
  40. for (int i = 0; i < inputB.length; ++i)
  41. inputB[i] = Arrays.copyOfRange(scTemp, i * 16, i * 16 + 16);
  42. }
  43. @Override
  44. public void secureCompute(CompEnv<T> gen) {
  45. compute(gen, inputB);
  46. }
  47. @Override
  48. public void prepareOutput(CompEnv<T> gen) {
  49. }
  50. }
  51. }