GCSignal.java 2.7 KB

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