Util.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 void setXor(byte[][] a, byte[][] b) {
  88. assert a.length == b.length;
  89. for (int i = 0; i < a.length; i++) {
  90. setXor(a[i], b[i]);
  91. }
  92. }
  93. public static BigInteger[] xor(BigInteger[] a, BigInteger[] b) {
  94. if (a.length != b.length)
  95. throw new LengthNotMatchException(a.length + " != " + b.length);
  96. BigInteger[] c = new BigInteger[a.length];
  97. for (int i = 0; i < a.length; i++)
  98. c[i] = a[i].xor(b[i]);
  99. return c;
  100. }
  101. public static byte[] intToBytes(int i) {
  102. ByteBuffer bb = ByteBuffer.allocate(4);
  103. bb.putInt(i);
  104. return bb.array();
  105. }
  106. public static int bytesToInt(byte[] b) {
  107. return new BigInteger(b).intValue();
  108. }
  109. public static int[] identityPermutation(int len) {
  110. int[] out = new int[len];
  111. for (int i = 0; i < len; i++)
  112. out[i] = i;
  113. return out;
  114. }
  115. public static int[] randomPermutation(int len, Random rand) {
  116. List<Integer> list = new ArrayList<Integer>(len);
  117. for (int i = 0; i < len; i++)
  118. list.add(i);
  119. Collections.shuffle(list, rand);
  120. int[] array = new int[len];
  121. for (int i = 0; i < len; i++)
  122. array[i] = list.get(i);
  123. return array;
  124. }
  125. public static int[] inversePermutation(int[] p) {
  126. int[] ip = new int[p.length];
  127. for (int i = 0; i < p.length; i++)
  128. ip[p[i]] = i;
  129. return ip;
  130. }
  131. @SuppressWarnings("unchecked")
  132. public static <T> T[] permute(T[] original, int[] p) {
  133. T[] permuted = (T[]) new Object[original.length];
  134. for (int i = 0; i < original.length; i++)
  135. permuted[p[i]] = original[i];
  136. return (T[]) Arrays.copyOf(permuted, permuted.length, original.getClass());
  137. }
  138. public static int[] permute(int[] original, int[] p) {
  139. int[] permuted = new int[original.length];
  140. for (int i = 0; i < original.length; i++)
  141. permuted[p[i]] = original[i];
  142. return permuted;
  143. }
  144. public static byte[] longToBytes(long l, int numBytes) {
  145. byte[] bytes = BigInteger.valueOf(l).toByteArray();
  146. if (bytes.length == numBytes)
  147. return bytes;
  148. else if (bytes.length > numBytes)
  149. return Arrays.copyOfRange(bytes, bytes.length - numBytes, bytes.length);
  150. else {
  151. byte[] out = new byte[numBytes];
  152. System.arraycopy(bytes, 0, out, numBytes - bytes.length, bytes.length);
  153. return out;
  154. }
  155. }
  156. public static int[] getXorPermutation(byte[] b, int bits) {
  157. BigInteger bi = Util.getSubBits(new BigInteger(b), bits, 0);
  158. int len = (int) Math.pow(2, bits);
  159. int[] p = new int[len];
  160. for (int i = 0; i < len; i++) {
  161. p[i] = BigInteger.valueOf(i).xor(bi).intValue();
  162. }
  163. return p;
  164. }
  165. public static String addZeros(String a, int n) {
  166. String out = a;
  167. for (int i = 0; i < n - a.length(); i++)
  168. out = "0" + out;
  169. return out;
  170. }
  171. public static byte[] padArray(byte[] in, int len) {
  172. if (in.length == len)
  173. return in;
  174. else if (in.length < len) {
  175. byte[] out = new byte[len];
  176. System.arraycopy(in, 0, out, len - in.length, in.length);
  177. return out;
  178. } else {
  179. return Arrays.copyOfRange(in, in.length - len, in.length);
  180. }
  181. }
  182. public static byte[] xorSelect(byte[][] array, byte[] ind) {
  183. assert array.length == ind.length;
  184. byte[] out = new byte[array[0].length];
  185. for (int i = 0; i < ind.length; i++) {
  186. if ((ind[i] & 1) == 1) {
  187. setXor(out, array[i]);
  188. }
  189. }
  190. return out;
  191. }
  192. public static byte[] xorRotate(byte[] arr, int r, int ttp, int l) {
  193. assert arr.length == ttp * l;
  194. assert r >= 0 && r < ttp;
  195. byte[] ret = new byte[arr.length];
  196. for (int i = 0; i < ttp; i++) {
  197. for (int j = 0; j < l; j++) {
  198. ret[i * l + j] = arr[(i ^ r) * l + j];
  199. }
  200. }
  201. return ret;
  202. }
  203. public static byte[] createRotate(byte[] arr, int order) {
  204. byte[] ret = arr.clone();
  205. rotate(ret, order);
  206. return ret;
  207. }
  208. public static void rotate(byte[] arr, int order) {
  209. if (arr == null || arr.length == 0 || order < 0) {
  210. throw new IllegalArgumentException("Illegal argument!");
  211. }
  212. if (order > arr.length) {
  213. order = order % arr.length;
  214. }
  215. // length of first part
  216. int a = arr.length - order;
  217. reverse(arr, 0, a - 1);
  218. reverse(arr, a, arr.length - 1);
  219. reverse(arr, 0, arr.length - 1);
  220. }
  221. public static void reverse(byte[] arr, int left, int right) {
  222. if (arr == null || arr.length == 1)
  223. return;
  224. while (left < right) {
  225. byte temp = arr[left];
  226. arr[left] = arr[right];
  227. arr[right] = temp;
  228. left++;
  229. right--;
  230. }
  231. }
  232. public static void debug(String s) {
  233. // only to make Communication.java compile
  234. }
  235. public static void disp(String s) {
  236. // only to make Communication.java compile
  237. }
  238. public static void error(String s) {
  239. // only to make Communication.java compile
  240. }
  241. public static void error(String s, Exception e) {
  242. // only to make Communication.java compile
  243. }
  244. }