GCSignal.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (C) 2013 by Yan Huang <yhuang@cs.umd.edu>
  2. // Improved by Xiao Shaun Wang <wangxiao@cs.umd.edu> and Kartik Nayak <kartik@cs.umd.edu>
  3. package com.oblivm.backend.gc;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.io.Serializable;
  7. import java.security.SecureRandom;
  8. import java.util.Arrays;
  9. import com.oblivm.backend.network.Network;
  10. public class GCSignal implements Serializable {
  11. /**
  12. *
  13. */
  14. private static final long serialVersionUID = 1L;
  15. public static final int len = 10;
  16. public byte[] bytes;
  17. public boolean v;
  18. public static final GCSignal ZERO = new GCSignal(new byte[len]);
  19. public GCSignal(byte[] b) {
  20. bytes = b;
  21. }
  22. public GCSignal(boolean b) {
  23. v = b;
  24. }
  25. public static GCSignal freshLabel(SecureRandom rnd) {
  26. byte[] b = new byte[len];
  27. rnd.nextBytes(b);
  28. return new GCSignal(b);
  29. }
  30. public static GCSignal newInstance(byte[] bs) {
  31. assert (bs.length <= len) : "Losing entropy when constructing signals.";
  32. byte[] b = new byte[len];
  33. Arrays.fill(b, (byte) ((bs[0] < 0) ? 0xff : 0));
  34. int newlen = len < bs.length ? len : bs.length;
  35. System.arraycopy(bs, 0, b, len - newlen, newlen);
  36. return new GCSignal(b);
  37. }
  38. public GCSignal(GCSignal lb) {
  39. v = lb.v;
  40. bytes = (lb.bytes == null) ? null : Arrays.copyOf(lb.bytes, len);
  41. }
  42. public boolean isPublic() {
  43. return bytes == null;
  44. }
  45. public GCSignal xor(GCSignal lb) {
  46. byte[] nb = new byte[len];
  47. for (int i = 0; i < len; i++)
  48. nb[i] = (byte) (bytes[i] ^ lb.bytes[i]);
  49. return new GCSignal(nb);
  50. }
  51. public static void xor(GCSignal l, GCSignal r, GCSignal ret) {
  52. for (int i = 0; i < len; i++)
  53. ret.bytes[i] = (byte) (l.bytes[i] ^ r.bytes[i]);
  54. }
  55. public void setLSB() {
  56. bytes[0] |= 1;
  57. }
  58. public int getLSB() {
  59. return (bytes[0] & 1);
  60. }
  61. // 'send' and 'receive' are supposed to be used only for secret signals
  62. public void send(Network channel) {
  63. if (channel.receiver == null)
  64. channel.writeByte(bytes, len);
  65. else
  66. channel.receiver.write(bytes);
  67. }
  68. // 'send' and 'receive' are supposed to be used only for secret signals
  69. public void send(OutputStream os) {
  70. try {
  71. os.write(bytes);
  72. } catch (IOException e) {
  73. // TODO Auto-generated catch block
  74. e.printStackTrace();
  75. }
  76. }
  77. // 'send' and 'receive' are supposed to be used only for secret signals
  78. public static GCSignal receive(Network channel) {
  79. byte[] b = channel.readBytes(len);
  80. return new GCSignal(b);
  81. }
  82. public static void receive(Network channel, GCSignal s) {
  83. if (channel.sender == null) {
  84. if (s.bytes == null)
  85. s.bytes = new byte[len];
  86. channel.readBytes(s.bytes);
  87. } else
  88. s.bytes = channel.sender.read();
  89. }
  90. @Override
  91. public boolean equals(Object lb) {
  92. if (this == lb)
  93. return true;
  94. else if (lb instanceof GCSignal)
  95. return Arrays.equals(bytes, ((GCSignal) lb).bytes);
  96. else
  97. return false;
  98. }
  99. public String toHexStr() {
  100. StringBuilder str = new StringBuilder();
  101. for (byte b : bytes)
  102. str.append(Integer.toHexString(b & 0xff));
  103. return str.toString();
  104. }
  105. }