Millionaire.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package com.oblivm.backend.example;
  2. import com.oblivm.backend.circuits.arithmetic.IntegerLib;
  3. import com.oblivm.backend.flexsc.CompEnv;
  4. import com.oblivm.backend.gc.BadLabelException;
  5. import com.oblivm.backend.util.EvaRunnable;
  6. import com.oblivm.backend.util.GenRunnable;
  7. import com.oblivm.backend.util.Utils;
  8. public class Millionaire {
  9. static public <T> T compute(CompEnv<T> gen, T[] inputA, T[] inputB) {
  10. return new IntegerLib<T>(gen).geq(inputA, inputB);
  11. }
  12. public static class Generator<T> extends GenRunnable<T> {
  13. T[] inputA;
  14. T[] inputB;
  15. T scResult;
  16. @Override
  17. public void prepareInput(CompEnv<T> gen) {
  18. inputA = gen.inputOfAlice(Utils.fromInt(new Integer(args[0]), 32));
  19. gen.flush();
  20. inputB = gen.inputOfBob(new boolean[32]);
  21. }
  22. @Override
  23. public void secureCompute(CompEnv<T> gen) {
  24. scResult = compute(gen, inputA, inputB);
  25. }
  26. @Override
  27. public void prepareOutput(CompEnv<T> gen) throws BadLabelException {
  28. System.out.println(gen.outputToAlice(scResult));
  29. }
  30. }
  31. public static class Evaluator<T> extends EvaRunnable<T> {
  32. T[] inputA;
  33. T[] inputB;
  34. T scResult;
  35. @Override
  36. public void prepareInput(CompEnv<T> gen) {
  37. inputA = gen.inputOfAlice(new boolean[32]);
  38. gen.flush();
  39. inputB = gen.inputOfBob(Utils.fromInt(new Integer(args[0]), 32));
  40. }
  41. @Override
  42. public void secureCompute(CompEnv<T> gen) {
  43. scResult = compute(gen, inputA, inputB);
  44. }
  45. @Override
  46. public void prepareOutput(CompEnv<T> gen) throws BadLabelException {
  47. gen.outputToAlice(scResult);
  48. }
  49. }
  50. }