/** * This method accepts an array of MPIs, and returns a byte array containing a reversible * serialization. * * @throws OTRException */ public static byte[] serializeMPIArray(MPI[] mpis) throws OTRException { int totalsize = 0; for (int i = 0; i < mpis.length; i++) { totalsize += 4 + mpis[i].getLength(); } totalsize += 4; byte[] buf = new byte[totalsize]; OutBuf obuf = new OutBuf(buf); obuf.writeUInt(mpis.length); for (int i = 0; i < mpis.length; i++) { mpis[i].write(obuf); } return obuf.getBytes(); }
/** Hash one or two MPIs. To hash only one MPI, b may be set to NULL. */ public static MPI hash(int version, MPI a, MPI b, Provider prov) throws OTRException { int totalsize = 1 + 4 + a.getLength(); if (b != null) { totalsize += 4 + b.getLength(); } byte[] buf = new byte[totalsize]; OutBuf obuf = new OutBuf(buf); obuf.writeByte((byte) version); obuf.writeUInt(a.getLength()); a.writeRaw(obuf); if (b != null) { obuf.writeUInt(b.getLength()); b.writeRaw(obuf); } byte[] out = obuf.getBytes(); SHA256 sha = prov.getSHA256(); byte[] digest = sha.hash(out); return new MPI(digest); }
/** * Transfers the control dependencies from a given {@link Exit} to the done {@link Exit}, creating * one if necessary. The given {@link Exit} and its peer are then removed. */ private void mergeBreakExit(Exit breakExit) { Exit doneExit = getExit(Exit.DONE); if (doneExit == null) { doneExit = makeExit(0, Exit.DONE); } OutBuf breakOutBuf = breakExit.getPeer(); OutBuf doneOutBuf = doneExit.getPeer(); Port donePort = doneOutBuf.getGoPort(); for (Entry breakEntry : breakOutBuf.getEntries()) { Exit drivingExit = breakEntry.getDrivingExit(); Entry doneEntry = doneOutBuf.makeEntry(drivingExit); doneEntry.addDependency(donePort, new ControlDependency(drivingExit.getDoneBus())); } breakOutBuf.disconnect(); breakExit.disconnect(); removeExit(breakExit); }