From 0edfae4621d286750b3a0bd65f03acbd60ad9818 Mon Sep 17 00:00:00 2001 From: Eitan Chatav Date: Wed, 16 Nov 2016 17:09:01 -0800 Subject: [PATCH] bitmask use bit mask to check proof of work --- src/utils/crypto_box.ml | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/utils/crypto_box.ml b/src/utils/crypto_box.ml index 97db6c028..bf4586799 100644 --- a/src/utils/crypto_box.ml +++ b/src/utils/crypto_box.ml @@ -15,7 +15,7 @@ type secret_key = Sodium.Box.secret_key type public_key = Sodium.Box.public_key type channel_key = Sodium.Box.channel_key type nonce = Sodium.Box.nonce -type difficulty = int +type difficulty = int64 let random_keypair = Sodium.Box.random_keypair let random_nonce = Sodium.Box.random_nonce @@ -35,13 +35,9 @@ let check_proof_of_work pk nonce difficulty = [ 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 last_int64 = + EndianBigstring.BigEndian.get_int64 bytes (MBytes.length bytes - 8) in + Int64.logand last_int64 (Int64.of_int 1) < difficulty let generate_proof_of_work pk difficulty = let rec loop nonce = if check_proof_of_work pk nonce difficulty then nonce