check proof of work

This commit is contained in:
Eitan Chatav 2016-11-09 18:29:57 -08:00 committed by Grégoire Henry
parent 0398a66423
commit ee5aec87ad

View File

@ -25,7 +25,23 @@ let box_open sk pk msg nonce =
try Some (Sodium.Box.Bigbytes.box_open sk pk msg nonce) with try Some (Sodium.Box.Bigbytes.box_open sk pk msg nonce) with
| Sodium.Verification_failure -> None | Sodium.Verification_failure -> None
let check_proof_of_work pk nonce difficulty = assert false let check_proof_of_work pk nonce difficulty =
let hash_bytes l =
let hash = Cryptokit.Hash.sha256 () in
List.iter (fun b -> hash#add_string (MBytes.to_string b)) l;
let r = hash#result in hash#wipe; r in
let hash =
hash_bytes
[ Sodium.Box.Bigbytes.of_public_key pk ;
Sodium.Box.Bigbytes.of_nonce nonce ] in
let bytes = MBytes.of_string hash in
let len = MBytes.length bytes * 8 in
try
for i = len - 1 downto (len - difficulty) do
if MBytes.get_bool bytes i then raise Exit
done;
true
with Exit -> false
let generate_proof_of_work pk difficulty = let generate_proof_of_work pk difficulty =
let rec loop nonce = let rec loop nonce =
if check_proof_of_work pk nonce difficulty then nonce if check_proof_of_work pk nonce difficulty then nonce