Proof of work
This page describes Bitmessage's Proof of work ("POW") mechanism as it currently exists. In this document, hash() means SHA512(). SHA512 was chosen as it is widely supported and so that Bitcoin POW hardware cannot trivially be used for Bitmessage POWs. The author acknowledges that they are essentially the same algorithm with a different key size.
payloadLengthExtraBytes is a constant which equals 320
averageProofOfWorkNonceTrialsPerByte is a constant which equals 14000. The purpose of this value is to add some extra weight to small messages.
Do a POW
Let us use a msg message as an example.
payload = embeddedTime + encodedStreamNumber + encrypted
target = 2^64 / ((length of the payload in bytes + payloadLengthExtraBytes + 8) * averageProofOfWorkNonceTrialsPerByte)
initialHash = hash(payload)
start with trialValue = 99999999999999999999
also start with nonce = 0 where nonce is 8 bytes in length and can be hashed as if it is a string.
while trialValue > target: nonce = nonce + 1 resultHash = hash(hash( nonce + initialHash )) trialValue = the last 8 bytes of resultHash, converted to an integer
When this loop finishes, you will have your 8 byte nonce value which you can prepend onto the front of the payload. The message is then ready to send.
Check a POW
Let us assume that 'payload' contains the payload for a msg message (the nonce down through the encrypted message data).
nonce = the first 8 bytes of payload
dataToCheck = the ninth byte of payload on down (thus it is everything except the nonce)
initialHash = hash(dataToCheck)
resultHash = hash(hash( nonce + initialHash ))
POWValue = the first eight bytes of resultHash converted to an integer
target = 2^64 / ((length of payload + payloadLengthExtraBytes) * averageProofOfWorkNonceTrialsPerByte)
If POWValue is less than or equal to target, then the POW check passes.