MainRunnable.java 3.3 KB

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