Tuple.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package oram;
  2. import java.io.Serializable;
  3. import java.math.BigInteger;
  4. import java.util.Arrays;
  5. import java.util.Random;
  6. import exceptions.LengthNotMatchException;
  7. import util.Util;
  8. public class Tuple implements Serializable {
  9. /**
  10. *
  11. */
  12. private static final long serialVersionUID = 1L;
  13. private byte[] F;
  14. private byte[] N;
  15. private byte[] L;
  16. private byte[] A;
  17. public Tuple(int fs, int ns, int ls, int as, Random rand) {
  18. F = new byte[fs];
  19. N = new byte[ns];
  20. L = new byte[ls];
  21. A = new byte[as];
  22. if (rand != null) {
  23. rand.nextBytes(F);
  24. rand.nextBytes(N);
  25. rand.nextBytes(L);
  26. rand.nextBytes(A);
  27. }
  28. }
  29. public Tuple(byte[] f, byte[] n, byte[] l, byte[] a) {
  30. F = f;
  31. N = n;
  32. L = l;
  33. A = a;
  34. }
  35. // deep copy
  36. public Tuple(Tuple t) {
  37. F = t.getF().clone();
  38. N = t.getN().clone();
  39. L = t.getL().clone();
  40. A = t.getA().clone();
  41. }
  42. public int getNumBytes() {
  43. return F.length + N.length + L.length + A.length;
  44. }
  45. public byte[] getF() {
  46. return F;
  47. }
  48. public void setF(byte[] f) {
  49. if (F.length < f.length)
  50. throw new LengthNotMatchException(F.length + " < " + f.length);
  51. else if (F.length > f.length) {
  52. for (int i = 0; i < F.length - f.length; i++)
  53. F[i] = 0;
  54. System.arraycopy(f, 0, F, F.length - f.length, f.length);
  55. } else
  56. F = f;
  57. }
  58. public byte[] getN() {
  59. return N;
  60. }
  61. public void setN(byte[] n) {
  62. if (N.length < n.length)
  63. throw new LengthNotMatchException(N.length + " < " + n.length);
  64. else if (N.length > n.length) {
  65. for (int i = 0; i < N.length - n.length; i++)
  66. N[i] = 0;
  67. System.arraycopy(n, 0, N, N.length - n.length, n.length);
  68. } else
  69. N = n;
  70. }
  71. public byte[] getL() {
  72. return L;
  73. }
  74. public void setL(byte[] l) {
  75. if (L.length < l.length)
  76. throw new LengthNotMatchException(L.length + " < " + l.length);
  77. else if (L.length > l.length) {
  78. for (int i = 0; i < L.length - l.length; i++)
  79. L[i] = 0;
  80. System.arraycopy(l, 0, L, L.length - l.length, l.length);
  81. } else
  82. L = l;
  83. }
  84. public byte[] getA() {
  85. return A;
  86. }
  87. public void setA(byte[] a) {
  88. if (A.length < a.length)
  89. throw new LengthNotMatchException(A.length + " < " + a.length);
  90. else if (A.length > a.length) {
  91. for (int i = 0; i < A.length - a.length; i++)
  92. A[i] = 0;
  93. System.arraycopy(a, 0, A, A.length - a.length, a.length);
  94. } else
  95. A = a;
  96. }
  97. public byte[] getSubA(int start, int end) {
  98. return Arrays.copyOfRange(A, start, end);
  99. }
  100. public void setSubA(int start, int end, byte[] label) {
  101. if (start < 0)
  102. throw new IllegalArgumentException(start + " < 0");
  103. if (start > end)
  104. throw new IllegalArgumentException(start + " > " + end);
  105. int len = end - start;
  106. if (len < label.length)
  107. throw new LengthNotMatchException(len + " < " + label.length);
  108. else if (len > label.length) {
  109. for (int i = 0; i < len - label.length; i++)
  110. A[start + i] = 0;
  111. }
  112. System.arraycopy(label, 0, A, end - label.length, label.length);
  113. }
  114. public Tuple xor(Tuple t) {
  115. if (!this.sameLength(t))
  116. throw new LengthNotMatchException(this.getNumBytes() + " != " + t.getNumBytes());
  117. byte[] newF = Util.xor(F, t.getF());
  118. byte[] newN = Util.xor(N, t.getN());
  119. byte[] newL = Util.xor(L, t.getL());
  120. byte[] newA = Util.xor(A, t.getA());
  121. return new Tuple(newF, newN, newL, newA);
  122. }
  123. public void setXor(Tuple t) {
  124. if (!this.sameLength(t))
  125. throw new LengthNotMatchException(this.getNumBytes() + " != " + t.getNumBytes());
  126. Util.setXor(F, t.getF());
  127. Util.setXor(N, t.getN());
  128. Util.setXor(L, t.getL());
  129. Util.setXor(A, t.getA());
  130. }
  131. public boolean sameLength(Tuple t) {
  132. return getNumBytes() == t.getNumBytes();
  133. }
  134. public boolean equals(Tuple t) {
  135. if (!sameLength(t))
  136. return false;
  137. return Util.equal(toByteArray(), t.toByteArray());
  138. }
  139. public byte[] toByteArray() {
  140. byte[] tuple = new byte[getNumBytes()];
  141. int offset = 0;
  142. System.arraycopy(F, 0, tuple, offset, F.length);
  143. offset += F.length;
  144. System.arraycopy(N, 0, tuple, offset, N.length);
  145. offset += N.length;
  146. System.arraycopy(L, 0, tuple, offset, L.length);
  147. offset += L.length;
  148. System.arraycopy(A, 0, tuple, offset, A.length);
  149. return tuple;
  150. }
  151. @Override
  152. public String toString() {
  153. String str = "Tuple: ";
  154. str += ("F=" + new BigInteger(1, getF()).toString(2) + ", ");
  155. str += ("N=" + new BigInteger(1, getN()).toString(2) + ", ");
  156. str += ("L=" + new BigInteger(1, getL()).toString(2) + ", ");
  157. str += ("A=" + new BigInteger(1, getA()).toString(2));
  158. return str;
  159. }
  160. }