diff --git a/src/node/net/p2p.ml b/src/node/net/p2p.ml index 87d9352b3..b5713b0a1 100644 --- a/src/node/net/p2p.ml +++ b/src/node/net/p2p.ml @@ -224,7 +224,7 @@ end module Fake = struct - let id = Identity.generate Crypto_box.default_target + let id = Identity.generate (Crypto_box.make_target 0.) let empty_stat = { Stat.total_sent = 0 ; total_recv = 0 ; diff --git a/src/utils/crypto_box.ml b/src/utils/crypto_box.ml index 8e9cefe63..d3d1d4a0a 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 target = int64 list (* used as unsigned intergers... *) +type target = Z.t exception TargetNot256Bit module Public_key_hash = Hash.Make_Blake2B (Base48) (struct @@ -44,24 +44,29 @@ let fast_box_open ck msg nonce = try Some (Sodium.Box.Bigbytes.fast_box_open ck msg nonce) with | Sodium.Verification_failure -> None -let make_target target = - if List.length target > 8 then raise TargetNot256Bit ; - target - -(* Compare a SHA256 hash to a 256bits-target prefix. - The prefix is a list of "unsigned" int64. *) let compare_target hash target = - let hash = Hash.Generic_hash.to_string hash in - let rec check offset = function - | [] -> true - | x :: xs -> - Compare.Uint64.(EndianString.BigEndian.get_int64 hash offset <= x) - && check (offset + 8) xs in - check 0 target + let hash = Z.of_bits (Hash.Generic_hash.to_string hash) in + Z.compare hash target <= 0 -let default_target = - (* FIXME we use an easy target until we allow custom configuration. *) - [ Int64.shift_left 1L 48 ] +let make_target f = + if f < 0. || 256. < f then invalid_arg "Cryptobox.target_of_float" ; + let frac, shift = modf f in + let shift = int_of_float shift in + let m = + Z.of_int64 @@ + if frac = 0. then + Int64.(pred (shift_left 1L 54)) + else + Int64.of_float (2. ** (54. -. frac)) + in + if shift < 202 then + Z.logor + (Z.shift_left m (202 - shift)) + (Z.pred @@ Z.shift_left Z.one (202 - shift)) + else + Z.shift_right m (shift - 202) + +let default_target = make_target 24. let check_proof_of_work pk nonce target = let hash = diff --git a/src/utils/crypto_box.mli b/src/utils/crypto_box.mli index cbeedd440..487f3f56f 100644 --- a/src/utils/crypto_box.mli +++ b/src/utils/crypto_box.mli @@ -16,8 +16,8 @@ val increment_nonce : ?step:int -> nonce -> nonce val nonce_encoding : nonce Data_encoding.t type target -val make_target : (* unsigned *) Int64.t list -> target val default_target : target +val make_target : float -> target type secret_key type public_key