Tuple.java 4.3 KB

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