MainRunnable.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.oblivm.backend.lang.inter;
  2. import java.io.IOException;
  3. import java.lang.reflect.Constructor;
  4. import com.oblivm.backend.flexsc.CompEnv;
  5. /***
  6. * Copyright (C) 2015 by Chang Liu <liuchang@cs.umd.edu>
  7. */
  8. import com.oblivm.backend.lang.inter.input.BitFileInput;
  9. import com.oblivm.backend.util.EvaRunnable;
  10. import com.oblivm.backend.util.GenRunnable;
  11. import com.oblivm.backend.util.Utils;
  12. public class MainRunnable {
  13. public static class Generator<T> extends GenRunnable<T> {
  14. public Input inputAlice;
  15. public String className;
  16. public int lenA, lenB;
  17. public Generator(String className, String aliceInputFile) {
  18. this.className = className;
  19. try {
  20. inputAlice = BitFileInput.open(aliceInputFile);
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. ISecureRunnable<T> runnable;
  26. T[] inputA;
  27. T[] inputB;
  28. T[] scResult;
  29. @SuppressWarnings("unchecked")
  30. @Override
  31. public void prepareInput(CompEnv<T> env) {
  32. try {
  33. Class<?> cl = Class.forName(className);
  34. Constructor<?> ctor = cl.getConstructors()[0];
  35. runnable = (ISecureRunnable<T>) ctor.newInstance(env);
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. boolean[] in = inputAlice.readAll();
  40. lenA = in.length;
  41. lenB = 0;
  42. try {
  43. os.write(Utils.toByte(lenA));
  44. os.flush();
  45. lenB = Utils.fromByte(this.readBytes(4));
  46. this.flush();
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. }
  50. inputA = env.inputOfAlice(in);
  51. env.flush();
  52. inputB = env.inputOfBob(new boolean[lenB]);
  53. // System.out.println("LenA="+lenA+"\tLenB="+lenB);
  54. }
  55. @Override
  56. public void secureCompute(CompEnv<T> env) throws Exception {
  57. scResult = runnable.main(lenA, lenB, inputA, inputB);
  58. }
  59. @Override
  60. public void prepareOutput(CompEnv<T> gen) throws Exception {
  61. System.out.println(Utils.toInt(gen.outputToAlice(scResult)));
  62. gen.channel.os.write(new byte[] { 0 });
  63. gen.flush();
  64. }
  65. }
  66. public static class Evaluator<T> extends EvaRunnable<T> {
  67. public Input inputBob;
  68. public String className;
  69. public int lenA, lenB;
  70. public Evaluator(String className, String bobInputFile) {
  71. this.className = className;
  72. try {
  73. inputBob = BitFileInput.open(bobInputFile);
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. ISecureRunnable<T> runnable;
  79. T[] inputA;
  80. T[] inputB;
  81. T[] scResult;
  82. @SuppressWarnings("unchecked")
  83. @Override
  84. public void prepareInput(CompEnv<T> env) {
  85. boolean[] in = inputBob.readAll();
  86. lenA = 0;
  87. lenB = in.length;
  88. try {
  89. lenA = Utils.fromByte(this.readBytes(4));
  90. os.write(Utils.toByte(lenB));
  91. os.flush();
  92. } catch (IOException e) {
  93. e.printStackTrace();
  94. }
  95. try {
  96. Class<?> cl = Class.forName(className);
  97. Constructor<?> ctor = cl.getConstructors()[0];
  98. runnable = (ISecureRunnable<T>) ctor.newInstance(env);
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. }
  102. inputA = env.inputOfAlice(new boolean[lenA]);
  103. env.flush();
  104. inputB = env.inputOfBob(in);
  105. }
  106. @Override
  107. public void secureCompute(CompEnv<T> env) throws Exception {
  108. scResult = runnable.main(lenA, lenB, inputA, inputB);
  109. }
  110. @Override
  111. public void prepareOutput(CompEnv<T> env) throws Exception {
  112. env.outputToAlice(scResult);
  113. env.channel.is.read(new byte[] { 0 });
  114. env.flush();
  115. }
  116. }
  117. }