Util.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package util;
  2. import java.math.BigInteger;
  3. import java.nio.ByteBuffer;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Collections;
  7. import java.util.List;
  8. import java.util.Random;
  9. import exceptions.LengthNotMatchException;
  10. public class Util {
  11. public static boolean equal(byte[] a, byte[] b) {
  12. if (a.length == 0 && b.length == 0)
  13. return true;
  14. if (a.length != b.length)
  15. return false;
  16. return new BigInteger(a).compareTo(new BigInteger(b)) == 0;
  17. }
  18. public static byte[] nextBytes(int len, Random r) {
  19. byte[] data = new byte[len];
  20. r.nextBytes(data);
  21. return data;
  22. }
  23. public static long nextLong(long range, Random r) {
  24. long bits, val;
  25. do {
  26. bits = (r.nextLong() << 1) >>> 1;
  27. val = bits % range;
  28. } while (bits - val + (range - 1) < 0L);
  29. return val;
  30. }
  31. public static long getSubBits(long l, int end, int start) {
  32. if (start < 0)
  33. throw new IllegalArgumentException(start + " < 0");
  34. if (start > end)
  35. throw new IllegalArgumentException(start + " > " + end);
  36. long mask = (1L << (end - start)) - 1L;
  37. return (l >>> start) & mask;
  38. }
  39. public static BigInteger getSubBits(BigInteger bi, int end, int start) {
  40. if (start < 0)
  41. throw new IllegalArgumentException(start + " < 0");
  42. if (start > end)
  43. throw new IllegalArgumentException(start + " > " + end);
  44. BigInteger mask = BigInteger.ONE.shiftLeft(end - start).subtract(BigInteger.ONE);
  45. return bi.shiftRight(start).and(mask);
  46. }
  47. public static long setSubBits(long target, long input, int end, int start) {
  48. input = getSubBits(input, end - start, 0);
  49. long trash = getSubBits(target, end, start);
  50. return ((trash ^ input) << start) ^ target;
  51. }
  52. public static BigInteger setSubBits(BigInteger target, BigInteger input, int end, int start) {
  53. if (input.bitLength() > end - start)
  54. input = getSubBits(input, end - start, 0);
  55. BigInteger trash = getSubBits(target, end, start);
  56. return trash.xor(input).shiftLeft(start).xor(target);
  57. }
  58. public static byte[] rmSignBit(byte[] arr) {
  59. if (arr[0] == 0)
  60. return Arrays.copyOfRange(arr, 1, arr.length);
  61. return arr;
  62. }
  63. public static byte[][] xor(byte[][] a, byte[][] b) {
  64. if (a.length != b.length)
  65. throw new LengthNotMatchException(a.length + " != " + b.length);
  66. byte[][] c = new byte[a.length][];
  67. for (int i = 0; i < a.length; i++)
  68. c[i] = xor(a[i], b[i]);
  69. return c;
  70. }
  71. // c = a ^ b
  72. public static byte[] xor(byte[] a, byte[] b) {
  73. if (a.length != b.length)
  74. throw new LengthNotMatchException(a.length + " != " + b.length);
  75. byte[] c = new byte[a.length];
  76. for (int i = 0; i < a.length; i++)
  77. c[i] = (byte) (a[i] ^ b[i]);
  78. return c;
  79. }
  80. // a = a ^ b to save memory
  81. public static void setXor(byte[] a, byte[] b) {
  82. if (a.length != b.length)
  83. throw new LengthNotMatchException(a.length + " != " + b.length);
  84. for (int i = 0; i < a.length; i++)
  85. a[i] = (byte) (a[i] ^ b[i]);
  86. }
  87. public static BigInteger[] xor(BigInteger[] a, BigInteger[] b) {
  88. if (a.length != b.length)
  89. throw new LengthNotMatchException(a.length + " != " + b.length);
  90. BigInteger[] c = new BigInteger[a.length];
  91. for (int i = 0; i < a.length; i++)
  92. c[i] = a[i].xor(b[i]);
  93. return c;
  94. }
  95. public static byte[] intToBytes(int i) {
  96. ByteBuffer bb = ByteBuffer.allocate(4);
  97. bb.putInt(i);
  98. return bb.array();
  99. }
  100. public static int bytesToInt(byte[] b) {
  101. return new BigInteger(b).intValue();
  102. }
  103. public static int[] identityPermutation(int len) {
  104. int[] out = new int[len];
  105. for (int i = 0; i < len; i++)
  106. out[i] = i;
  107. return out;
  108. }
  109. public static int[] randomPermutation(int len, Random rand) {
  110. List<Integer> list = new ArrayList<Integer>(len);
  111. for (int i = 0; i < len; i++)
  112. list.add(i);
  113. Collections.shuffle(list, rand);
  114. int[] array = new int[len];
  115. for (int i = 0; i < len; i++)
  116. array[i] = list.get(i);
  117. return array;
  118. }
  119. public static int[] inversePermutation(int[] p) {
  120. int[] ip = new int[p.length];
  121. for (int i = 0; i < p.length; i++)
  122. ip[p[i]] = i;
  123. return ip;
  124. }
  125. @SuppressWarnings("unchecked")
  126. public static <T> T[] permute(T[] original, int[] p) {
  127. T[] permuted = (T[]) new Object[original.length];
  128. for (int i = 0; i < original.length; i++)
  129. permuted[p[i]] = original[i];
  130. return (T[]) Arrays.copyOf(permuted, permuted.length, original.getClass());
  131. }
  132. public static int[] permute(int[] original, int[] p) {
  133. int[] permuted = new int[original.length];
  134. for (int i = 0; i < original.length; i++)
  135. permuted[p[i]] = original[i];
  136. return permuted;
  137. }
  138. public static byte[] longToBytes(long l, int numBytes) {
  139. byte[] bytes = BigInteger.valueOf(l).toByteArray();
  140. if (bytes.length == numBytes)
  141. return bytes;
  142. else if (bytes.length > numBytes)
  143. return Arrays.copyOfRange(bytes, bytes.length - numBytes, bytes.length);
  144. else {
  145. byte[] out = new byte[numBytes];
  146. System.arraycopy(bytes, 0, out, numBytes - bytes.length, bytes.length);
  147. return out;
  148. }
  149. }
  150. public static int[] getXorPermutation(byte[] b, int bits) {
  151. BigInteger bi = Util.getSubBits(new BigInteger(b), bits, 0);
  152. int len = (int) Math.pow(2, bits);
  153. int[] p = new int[len];
  154. for (int i = 0; i < len; i++) {
  155. p[i] = BigInteger.valueOf(i).xor(bi).intValue();
  156. }
  157. return p;
  158. }
  159. public static String addZeros(String a, int n) {
  160. String out = a;
  161. for (int i = 0; i < n - a.length(); i++)
  162. out = "0" + out;
  163. return out;
  164. }
  165. public static byte[] padArray(byte[] in, int len) {
  166. if (in.length == len)
  167. return in;
  168. else if (in.length < len) {
  169. byte[] out = new byte[len];
  170. System.arraycopy(in, 0, out, len - in.length, in.length);
  171. return out;
  172. } else {
  173. return Arrays.copyOfRange(in, in.length - len, in.length);
  174. }
  175. }
  176. public static void debug(String s) {
  177. // only to make Communication.java compile
  178. }
  179. public static void disp(String s) {
  180. // only to make Communication.java compile
  181. }
  182. public static void error(String s) {
  183. // only to make Communication.java compile
  184. }
  185. public static void error(String s, Exception e) {
  186. // only to make Communication.java compile
  187. }
  188. }