Added some POW code, it probably doesn't work yet

(takes very long and uses lots of battery, but I didn't get a result yet)
This commit is contained in:
2015-04-11 16:16:41 +02:00
parent 9b383e3bcd
commit daa9a9911b
10 changed files with 371 additions and 35 deletions

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.utils;
/**
* A helper class for working with byte arrays interpreted as unsigned big endian integers.
*/
public class Bytes {
public static void inc(byte[] nonce) {
for (int i = nonce.length - 1; i >= 0; i--) {
nonce[i]++;
if (nonce[i] != 0) break;
}
}
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;
}
private static boolean lt(byte a, byte b) {
if (a < 0) return b < 0 && a < b;
if (b < 0) return a >= 0 || a < b;
return a < b;
}
}

View File

@@ -16,9 +16,13 @@
package ch.dissem.bitmessage.utils;
import ch.dissem.bitmessage.entity.Streamable;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
/**
* This class handles encoding simple types from byte stream, according to
@@ -76,4 +80,27 @@ public class Encode {
varInt(bytes.length, stream);
stream.write(bytes);
}
/**
* Returns an array of bytes representing the given streamable object.
*/
public static byte[] bytes(Streamable streamable) throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
streamable.write(stream);
return stream.toByteArray();
}
/**
* Returns the bytes of the given streamable object, 0-padded such that the final
* length is x*padding.
*/
public static byte[] bytes(Streamable streamable, int padding) throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
streamable.write(stream);
int offset = padding - stream.size() % padding;
int length = stream.size() + offset;
byte[] result = new byte[length];
stream.write(result, offset, stream.size());
return result;
}
}

View File

@@ -16,37 +16,128 @@
package ch.dissem.bitmessage.utils;
import ch.dissem.bitmessage.entity.ObjectMessage;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.io.IOException;
import java.math.BigInteger;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import static ch.dissem.bitmessage.utils.Bytes.inc;
/**
* Provides some methods to help with hashing and encryption.
*/
public class Security {
public static byte[] sha512(byte[] data) {
private static final SecureRandom RANDOM = new SecureRandom();
private static final BigInteger TWO = BigInteger.valueOf(2);
static {
java.security.Security.addProvider(new BouncyCastleProvider());
}
public static byte[] sha512(byte[]... data) {
return hash("SHA-512", data);
}
public static byte[] doubleSha512(byte[]... data) {
MessageDigest mda = md("SHA-512");
for (byte[] d : data) {
mda.update(d);
}
return mda.digest(mda.digest());
}
public static byte[] ripemd160(byte[]... data) {
return hash("RIPEMD160", data);
}
public static byte[] sha1(byte[]... data) {
return hash("SHA-1", data);
}
public static byte[] randomBytes(int length) {
byte[] result = new byte[length];
RANDOM.nextBytes(result);
return result;
}
public static void doProofOfWork(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) throws IOException {
// payload = embeddedTime + encodedObjectVersion + encodedStreamNumber + encrypted
byte[] payload = object.getPayloadBytes();
// payloadLength = the length of payload, in bytes, + 8 (to account for the nonce which we will append later)
// TTL = the number of seconds in between now and the object expiresTime.
// initialHash = hash(payload)
byte[] initialHash = getInitialHash(object);
byte[] target = getProofOfWorkTarget(object, nonceTrialsPerByte, extraBytes);
// start with trialValue = 99999999999999999999
byte[] trialValue;
// also start with nonce = 0 where nonce is 8 bytes in length and can be hashed as if it is a string.
byte[] nonce = new byte[8];
MessageDigest mda = md("SHA-512");
do {
inc(nonce);
mda.update(nonce);
mda.update(initialHash);
trialValue = bytes(mda.digest(mda.digest()), 8);
} while (Bytes.lt(target, trialValue));
object.setNonce(nonce);
}
/**
* @param object
* @param nonceTrialsPerByte
* @param extraBytes
* @throws IOException if proof of work doesn't check out
*/
public static void checkProofOfWork(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) throws IOException {
// nonce = the first 8 bytes of payload
byte[] nonce = object.getNonce();
byte[] initialHash = getInitialHash(object);
// resultHash = hash(hash( nonce || initialHash ))
byte[] resultHash = Security.doubleSha512(nonce, initialHash);
// POWValue = the first eight bytes of resultHash converted to an integer
byte[] powValue = bytes(resultHash, 8);
if (Bytes.lt(getProofOfWorkTarget(object, nonceTrialsPerByte, extraBytes), powValue)) {
throw new IOException("Insufficient proof of work");
}
}
private static byte[] getInitialHash(ObjectMessage object) throws IOException {
return Security.sha512(object.getPayloadBytes());
}
private static byte[] getProofOfWorkTarget(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) throws IOException {
BigInteger TTL = BigInteger.valueOf(object.getExpiresTime() - (System.currentTimeMillis() / 1000));
BigInteger numerator = TWO.pow(64);
BigInteger powLength = BigInteger.valueOf(object.getPayloadBytes().length + extraBytes);
BigInteger denominator = BigInteger.valueOf(nonceTrialsPerByte).multiply(powLength.add(powLength.multiply(TTL).divide(BigInteger.valueOf(2).pow(16))));
return numerator.divide(denominator).toByteArray();
}
private static byte[] hash(String algorithm, byte[]... data) {
MessageDigest mda = md(algorithm);
for (byte[] d : data) {
mda.update(d);
}
return mda.digest();
}
private static MessageDigest md(String algorithm) {
try {
MessageDigest mda = MessageDigest.getInstance("SHA-512");
return mda.digest(data);
} catch (NoSuchAlgorithmException e) {
return MessageDigest.getInstance(algorithm, "BC");
} catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
public static byte[] doubleSha512(byte[] data) {
try {
MessageDigest mda = MessageDigest.getInstance("SHA-512");
return mda.digest(mda.digest(data));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static byte[] ripemd160(byte[] data) {
try {
MessageDigest mda = MessageDigest.getInstance("RIPEMD-160");
return mda.digest(data);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
private static byte[] bytes(byte[] data, int count) {
byte[] result = new byte[count];
System.arraycopy(data, 0, result, 0, count);
return result;
}
}