GCEva.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package com.oblivm.backend.gc.regular;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import com.oblivm.backend.flexsc.Flag;
  6. import com.oblivm.backend.flexsc.Mode;
  7. import com.oblivm.backend.gc.GCEvaComp;
  8. import com.oblivm.backend.gc.GCSignal;
  9. import com.oblivm.backend.network.Network;
  10. import util.Timer;
  11. public class GCEva extends GCEvaComp {
  12. Garbler gb;
  13. GCSignal[][] gtt = new GCSignal[2][2];
  14. boolean evaluate = false;
  15. GCEva next = null;
  16. GCEva curr = null;
  17. Timer timer = null;
  18. int p;
  19. int m;
  20. List<byte[]> msg = new ArrayList<byte[]>(threshold);
  21. public GCEva(Network channel) {
  22. super(channel, Mode.REAL);
  23. gb = new Garbler();
  24. gtt[0][0] = GCSignal.ZERO;
  25. gtt[0][1] = GCSignal.newInstance(new byte[10]);
  26. gtt[1][0] = GCSignal.newInstance(new byte[10]);
  27. gtt[1][1] = GCSignal.newInstance(new byte[10]);
  28. }
  29. public GCEva(Network channel, Timer timer, int p, int m) {
  30. super(channel, Mode.REAL);
  31. gb = new Garbler();
  32. gtt[0][0] = GCSignal.ZERO;
  33. gtt[0][1] = GCSignal.newInstance(new byte[10]);
  34. gtt[1][0] = GCSignal.newInstance(new byte[10]);
  35. gtt[1][1] = GCSignal.newInstance(new byte[10]);
  36. this.timer = timer;
  37. this.p = p;
  38. this.m = m;
  39. }
  40. public void setEvaluate() {
  41. evaluate = true;
  42. curr = this;
  43. }
  44. public void receiveLastSetGTT() {
  45. int remainder = (int) (numOfAnds % threshold);
  46. if (remainder > 0) {
  47. msg = channel.sender.readObject();
  48. for (int i = 0; i < remainder; i++) {
  49. if (curr == null) {
  50. curr = this;
  51. } else {
  52. curr.next = new GCEva(channel, timer, p, m);
  53. curr = curr.next;
  54. }
  55. byte[] rows = msg.get(i);
  56. curr.gtt[0][1].bytes = Arrays.copyOfRange(rows, 0, GCSignal.len);
  57. curr.gtt[1][0].bytes = Arrays.copyOfRange(rows, GCSignal.len, GCSignal.len * 2);
  58. curr.gtt[1][1].bytes = Arrays.copyOfRange(rows, GCSignal.len * 2, rows.length);
  59. }
  60. msg.clear();
  61. }
  62. }
  63. private void receiveGTT() {
  64. if (timer == null) {
  65. try {
  66. Flag.sw.startGCIO();
  67. GCSignal.receive(channel, gtt[0][1]);
  68. GCSignal.receive(channel, gtt[1][0]);
  69. GCSignal.receive(channel, gtt[1][1]);
  70. // gtt[1][0] = GCSignal.receive(channel);
  71. // gtt[1][1] = GCSignal.receive(channel);
  72. Flag.sw.stopGCIO();
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. System.exit(1);
  76. }
  77. } else {
  78. if (numOfAnds % threshold == 0) {
  79. msg = channel.sender.readObject();
  80. for (int i = 0; i < threshold; i++) {
  81. if (curr == null) {
  82. curr = this;
  83. } else {
  84. curr.next = new GCEva(channel, timer, p, m);
  85. curr = curr.next;
  86. }
  87. byte[] rows = msg.get(i);
  88. curr.gtt[0][1].bytes = Arrays.copyOfRange(rows, 0, GCSignal.len);
  89. curr.gtt[1][0].bytes = Arrays.copyOfRange(rows, GCSignal.len, GCSignal.len * 2);
  90. curr.gtt[1][1].bytes = Arrays.copyOfRange(rows, GCSignal.len * 2, rows.length);
  91. }
  92. msg.clear();
  93. }
  94. }
  95. }
  96. public GCSignal and(GCSignal a, GCSignal b) {
  97. Flag.sw.startGC();
  98. GCSignal res;
  99. if (a.isPublic() && b.isPublic())
  100. res = new GCSignal(a.v && b.v);
  101. else if (a.isPublic())
  102. res = a.v ? b : new GCSignal(false);
  103. else if (b.isPublic())
  104. res = b.v ? a : new GCSignal(false);
  105. else {
  106. res = new GCSignal(new byte[10]);
  107. if (!evaluate) {
  108. ++numOfAnds;
  109. receiveGTT();
  110. } else {
  111. int i0 = a.getLSB();
  112. int i1 = b.getLSB();
  113. gb.dec(a, b, gid, curr.gtt[i0][i1], res);
  114. curr = curr.next;
  115. gid++;
  116. }
  117. }
  118. Flag.sw.stopGC();
  119. return res;
  120. }
  121. }