/** * @return true if a < b, where the first [size] bytes are used as the numbers to check. */ public static boolean lt(byte[] a, byte[] b, int size) { for (int i = 0; i < size; i++) { if (a[i] != b[i]) { return lt(a[i], b[i]); } } return false; }
/** * @return true if a < b, where the first [size] bytes are used as the numbers to check. */ public static boolean lt(byte[] a, byte[] b, int size) { for (int i = 0; i < size; i++) { if (a[i] != b[i]) { return lt(a[i], b[i]); } } return false; }
/** * @return true if a < b. */ public static boolean lt(byte[] a, byte[] b) { byte[] max = (a.length > b.length ? a : b); byte[] min = (max == a ? b : a); int diff = max.length - min.length; for (int i = 0; i < max.length - min.length; i++) { if (max[i] != 0) return a != max; } for (int i = diff; i < max.length; i++) { if (max[i] != min[i - diff]) { return lt(max[i], min[i - diff]) == (a == max); } } return false; }
/** * @return true if a < b. */ public static boolean lt(byte[] a, byte[] b) { byte[] max = (a.length > b.length ? a : b); byte[] min = (max == a ? b : a); int diff = max.length - min.length; for (int i = 0; i < max.length - min.length; i++) { if (max[i] != 0) return a != max; } for (int i = diff; i < max.length; i++) { if (max[i] != min[i - diff]) { return lt(max[i], min[i - diff]) == (a == max); } } return false; }
@Override public byte[] call() throws Exception { do { inc(nonce, numberOfCores); mda.update(nonce); mda.update(initialHash); if (!Bytes.lt(target, mda.digest(mda.digest()), 8)) { return nonce; } } while (!Thread.interrupted()); return null; } }
@Override public byte[] calculateNonce(byte[] initialHash, byte[] target) { byte[] nonce = new byte[8]; MessageDigest mda; try { mda = MessageDigest.getInstance("SHA-512"); } catch (Exception e) { throw new RuntimeException(e); } do { inc(nonce); mda.update(nonce); mda.update(initialHash); } while (Bytes.lt(target, mda.digest(mda.digest()), 8)); return nonce; } }
@Override public void run() { do { inc(nonce, numberOfCores); mda.update(nonce); mda.update(initialHash); if (!Bytes.lt(target, mda.digest(mda.digest()), 8)) { successful = true; for (Worker w : workers) { w.interrupt(); } return; } } while (!Thread.interrupted()); } }
@Override public void calculateNonce(byte[] initialHash, byte[] target, Callback callback) { try { MessageDigest mda = MessageDigest.getInstance("SHA-512"); byte[] nonce = new byte[8]; do { inc(nonce); mda.update(nonce); mda.update(initialHash); } while (Bytes.lt(target, mda.digest(mda.digest()), 8)); callback.onNonceCalculated(initialHash, nonce); } catch (NoSuchAlgorithmException e) { throw new ApplicationException(e); } } }
public void checkProofOfWork(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) throws IOException { byte[] target = getProofOfWorkTarget(object, nonceTrialsPerByte, extraBytes); byte[] value = doubleSha512(object.getNonce(), getInitialHash(object)); if (Bytes.lt(target, value, 8)) { throw new InsufficientProofOfWorkException(target, value); } }
/** * @param object to be checked * @param nonceTrialsPerByte difficulty * @param extraBytes bytes to add to the object size * @throws InsufficientProofOfWorkException if proof of work doesn't check out (makes it more difficult to send small messages) */ public static void checkProofOfWork(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) throws IOException { byte[] target = getProofOfWorkTarget(object, nonceTrialsPerByte, extraBytes); byte[] value = Security.doubleSha512(object.getNonce(), getInitialHash(object)); if (Bytes.lt(target, value, 8)) { throw new InsufficientProofOfWorkException(target, value); } }