Utils.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package com.oblivm.backend.util;
  2. import java.io.BufferedReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.math.BigDecimal;
  7. import java.math.BigInteger;
  8. import java.util.Arrays;
  9. import com.oblivm.backend.flexsc.CompEnv;
  10. import com.oblivm.backend.flexsc.Party;
  11. public class Utils {
  12. public static int logFloor(int n) {
  13. int w = 0;
  14. n--;
  15. while (n > 0) {
  16. w++;
  17. n >>= 1;
  18. }
  19. return w == 0 ? 1 : w;
  20. }
  21. public static <T> void print(CompEnv<T> env, String name, T[] data, T[] data2, T con) throws Exception {
  22. int a = toInt(env.outputToAlice(data));
  23. int ab = toInt(env.outputToAlice(data2));
  24. boolean cc = env.outputToAlice(con);
  25. if (cc)
  26. if (env.getParty() == Party.Alice && a != 0)
  27. System.out.println(name + " " + a + " " + ab);
  28. }
  29. public static <T> void print(CompEnv<T> env, String name, T[] data) throws Exception {
  30. long a = toSignedInt(env.outputToAlice(data));
  31. if (env.getParty() == Party.Alice && a != 0)
  32. System.out.println(name + " " + a);
  33. }
  34. public static <T> void print(CompEnv<T> env, String name, T data) throws Exception {
  35. boolean a = env.outputToAlice(data);
  36. if (env.getParty() == Party.Alice)
  37. System.out.println(name + " " + a);
  38. }
  39. public static Boolean[] toBooleanArray(boolean[] a) {
  40. Boolean[] res = new Boolean[a.length];
  41. for (int i = 0; i < a.length; i++)
  42. res[i] = a[i];
  43. return res;
  44. }
  45. public static boolean[] tobooleanArray(Boolean[] a) {
  46. boolean[] res = new boolean[a.length];
  47. for (int i = 0; i < a.length; i++)
  48. res[i] = a[i];
  49. return res;
  50. }
  51. public static boolean[] fromInt(int value, int width) {
  52. boolean[] res = new boolean[width];
  53. for (int i = 0; i < width; i++)
  54. res[i] = (((value >> i) & 1) == 0) ? false : true;
  55. return res;
  56. }
  57. public static int toInt(boolean[] value) {
  58. int res = 0;
  59. for (int i = 0; i < value.length; i++)
  60. res = (value[i]) ? (res | (1 << i)) : res;
  61. return res;
  62. }
  63. public static long toUnSignedInt(boolean[] v) {
  64. long result = 0;
  65. for (int i = 0; i < v.length; ++i) {
  66. if (v[i])
  67. result += ((long) 1 << i);
  68. }
  69. return result;
  70. }
  71. public static long toSignedInt(boolean[] v) {
  72. int i = 0;
  73. if (v[v.length - 1] == false)
  74. return toUnSignedInt(v);
  75. boolean[] c2 = new boolean[v.length];
  76. while (v[i] != true) {
  77. c2[i] = v[i];
  78. ++i;
  79. }
  80. c2[i] = v[i];
  81. ++i;
  82. for (; i < v.length; ++i)
  83. c2[i] = !v[i];
  84. return toUnSignedInt(c2) * -(long) (1);
  85. }
  86. public static boolean[] fromLong(long value, int width) {
  87. boolean[] res = new boolean[width];
  88. for (int i = 0; i < width; i++)
  89. res[i] = (((value >> i) & 1) == 0) ? false : true;
  90. return res;
  91. }
  92. public static long toLong(boolean[] value) {
  93. long res = 0;
  94. for (int i = 0; i < value.length; i++)
  95. res = (value[i]) ? (res | (1L << i)) : res;// 1L!! not 1!!
  96. return res;
  97. }
  98. public static double toFloat(boolean[] value, int widthV, int widthP) {
  99. boolean[] v = Arrays.copyOfRange(value, 1, 1 + widthV);
  100. boolean[] p = Arrays.copyOfRange(value, 1 + widthV, value.length);
  101. double result = value[0] ? -1 : 1;
  102. long value_v = Utils.toUnSignedInt(v);
  103. long value_p = Utils.toSignedInt(p);
  104. result = result * value_v;
  105. result = result * Math.pow(2, value_p);
  106. BigDecimal b = new BigDecimal(result);
  107. return b.setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue(); // 6 is
  108. // should
  109. // not
  110. // be
  111. // fixed.
  112. }
  113. public static boolean[] fromFloat(double d, int widthV, int widthP) {
  114. boolean s;
  115. boolean[] v, p;
  116. v = new boolean[widthV];
  117. p = new boolean[widthP];
  118. s = d < 0;
  119. if (d == 0) {
  120. for (int i = 0; i < widthV; ++i)
  121. v[i] = false;
  122. for (int i = 0; i < widthP; ++i)
  123. p[i] = false;
  124. p[widthP - 1] = true;
  125. } else {
  126. d = s ? -1 * d : d;
  127. int pInt = 0;
  128. double lower_bound = Math.pow(2, widthV - 1);
  129. double upper_bound = Math.pow(2, widthV);
  130. while (d < lower_bound) {
  131. d *= 2;
  132. pInt--;
  133. }
  134. while (d >= upper_bound) {
  135. d /= 2;
  136. pInt++;
  137. }
  138. p = Utils.fromInt(pInt, widthP);
  139. long tmp = (long) (d + 0.000001);// a hack...
  140. v = Utils.fromLong(tmp, widthV);
  141. }
  142. boolean[] result = new boolean[1 + widthV + widthP];
  143. result[0] = s;
  144. System.arraycopy(v, 0, result, 1, v.length);
  145. System.arraycopy(p, 0, result, 1 + v.length, p.length);
  146. return result;
  147. }
  148. final static int[] mask = { 0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000, 0b00100000, 0b01000000,
  149. 0b10000000 };
  150. public static boolean[] fromBigInteger(BigInteger bd, int length) {
  151. byte[] b = bd.toByteArray();
  152. boolean[] result = new boolean[length];
  153. for (int i = 0; i < b.length; ++i) {
  154. for (int j = 0; j < 8 && i * 8 + j < length; ++j)
  155. result[i * 8 + j] = (((b[b.length - i - 1] & mask[j]) >> j) == 1);
  156. }
  157. return result;
  158. }
  159. public static BigInteger toBigInteger(boolean[] b) {
  160. BigInteger res = new BigInteger("0");
  161. BigInteger c = new BigInteger("1");
  162. for (int i = 0; i < b.length; i++) {
  163. if (b[i])
  164. res = res.add(c);
  165. c = c.multiply(new BigInteger("2"));
  166. }
  167. return res;
  168. }
  169. public static boolean[] fromFixPoint(double a, int width, int offset) {
  170. a *= Math.pow(2, offset);
  171. return Utils.fromLong((long) a, width);
  172. }
  173. public static double toFixPoint(boolean[] b, int offset) {
  174. double a = toSignedInt(b);
  175. a /= Math.pow(2, offset);
  176. return a;
  177. }
  178. public static boolean[] flatten(boolean[][] data) {
  179. int length = 0;
  180. for (int i = 0; i < data.length; i++) {
  181. length += data[i].length;
  182. }
  183. boolean[] ret = new boolean[length];
  184. int pos = 0;
  185. for (int i = 0; i < data.length; i++) {
  186. System.arraycopy(data[i], 0, ret, pos, data[i].length);
  187. pos += data[i].length;
  188. }
  189. return ret;
  190. }
  191. public static boolean[] flatten(boolean[][][] data) {
  192. int length = 0;
  193. for (int i = 0; i < data.length; i++) {
  194. for (int j = 0; j < data[0].length; j++) {
  195. length += data[i][j].length;
  196. }
  197. }
  198. boolean[] ret = new boolean[length];
  199. int pos = 0;
  200. for (int i = 0; i < data.length; i++) {
  201. for (int j = 0; j < data[0].length; j++) {
  202. System.arraycopy(data[i][j], 0, ret, pos, data[i][j].length);
  203. pos += data[i][j].length;
  204. }
  205. }
  206. return ret;
  207. }
  208. public static <T> T[] flatten(CompEnv<T> env, T[]... data) {
  209. int length = 0;
  210. for (int i = 0; i < data.length; i++) {
  211. length += data[i].length;
  212. }
  213. T[] ret = env.newTArray(length);
  214. int pos = 0;
  215. for (int i = 0; i < data.length; i++) {
  216. System.arraycopy(data[i], 0, ret, pos, data[i].length);
  217. pos += data[i].length;
  218. }
  219. return ret;
  220. }
  221. public static <T> T[][] flatten(CompEnv<T> env, T[][]... data) {
  222. int length = 0;
  223. for (int i = 0; i < data.length; i++) {
  224. length += data[i][0].length;
  225. }
  226. T[][] ret = env.newTArray(data[0].length, length);
  227. int pos = 0;
  228. for (int i = 0; i < data.length; i++) {
  229. for (int j = 0; j < data[0].length; j++) {
  230. System.arraycopy(data[i][j], 0, ret[j], pos, data[i][j].length);
  231. }
  232. pos += data[i][0].length;
  233. }
  234. return ret;
  235. }
  236. public static <T> void unflatten(T[][] flat, T[][]... x) {
  237. int pos = 0;
  238. for (int i = 0; i < x.length; i++) {
  239. for (int j = 0; j < x[i].length; j++) {
  240. System.arraycopy(flat[j], pos, x[i][j], 0, x[i][j].length);
  241. }
  242. pos += x[i][0].length;
  243. }
  244. }
  245. public static <T> void unflatten(T[] flat, T[]... x) {
  246. int pos = 0;
  247. for (int i = 0; i < x.length; i++) {
  248. System.arraycopy(flat, pos, x[i], 0, x[i].length);
  249. pos += x[i].length;
  250. }
  251. }
  252. public static <T> void unflatten(T[] flat, T[][][] x) {
  253. int pos = 0;
  254. for (int i = 0; i < x.length; i++) {
  255. for (int j = 0; j < x[0].length; j++) {
  256. System.arraycopy(flat, pos, x[i][j], 0, x[i][j].length);
  257. pos += x[i][j].length;
  258. }
  259. }
  260. }
  261. private static double getMega(double bytes) {
  262. return bytes / (1024.0 * 1024);
  263. }
  264. public static int log2(int n) {
  265. if (n <= 0) {
  266. throw new IllegalArgumentException();
  267. }
  268. return 31 - Integer.numberOfLeadingZeros(n);
  269. }
  270. public static int log2Ceil(int n) {
  271. int m = log2(n);
  272. if ((1 << m) < n)
  273. m++;
  274. return m;
  275. }
  276. public static double getRandom() {
  277. double ret = Utils.RAND[Utils.RAND_CNT];
  278. Utils.RAND_CNT = (Utils.RAND_CNT + 1) % Utils.RAND_LIM;
  279. return ret;
  280. }
  281. public static void generateRandomNumbers() throws FileNotFoundException, IOException {
  282. Utils.RAND = new double[Utils.RAND_LIM];
  283. BufferedReader reader = new BufferedReader(new FileReader("in/rand.out"));
  284. for (int i = 0; i < Utils.RAND_LIM; i++) {
  285. Utils.RAND[i] = Double.parseDouble(reader.readLine());
  286. }
  287. reader.close();
  288. }
  289. public static double RAND[];
  290. public static int RAND_CNT = 0;
  291. public static int RAND_LIM = 10000000;
  292. public static byte[] toByte(int value) {
  293. byte[] b = new byte[4];
  294. for (int i = 0; i < 4; ++i) {
  295. b[i] = (byte) (value & ((1 << 8) - 1));
  296. value >>= 8;
  297. }
  298. return b;
  299. }
  300. public static int fromByte(byte[] b) {
  301. int value = 0;
  302. for (int i = 3; i >= 0; --i) {
  303. int t = b[i];
  304. if (t < 0) {
  305. t += 1 << 8;
  306. }
  307. value = (value << 8) | t;
  308. }
  309. return value;
  310. }
  311. }