Shell: Add Cryptobox.target_of_float
[target_of_float f] is `2 ^ (256 - f)`.
This commit is contained in:
parent
3a70d88fe6
commit
197ac28f0b
@ -224,7 +224,7 @@ end
|
|||||||
|
|
||||||
module Fake = struct
|
module Fake = struct
|
||||||
|
|
||||||
let id = Identity.generate Crypto_box.default_target
|
let id = Identity.generate (Crypto_box.make_target 0.)
|
||||||
let empty_stat = {
|
let empty_stat = {
|
||||||
Stat.total_sent = 0 ;
|
Stat.total_sent = 0 ;
|
||||||
total_recv = 0 ;
|
total_recv = 0 ;
|
||||||
|
@ -15,7 +15,7 @@ type secret_key = Sodium.Box.secret_key
|
|||||||
type public_key = Sodium.Box.public_key
|
type public_key = Sodium.Box.public_key
|
||||||
type channel_key = Sodium.Box.channel_key
|
type channel_key = Sodium.Box.channel_key
|
||||||
type nonce = Sodium.Box.nonce
|
type nonce = Sodium.Box.nonce
|
||||||
type target = int64 list (* used as unsigned intergers... *)
|
type target = Z.t
|
||||||
exception TargetNot256Bit
|
exception TargetNot256Bit
|
||||||
|
|
||||||
module Public_key_hash = Hash.Make_Blake2B (Base48) (struct
|
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
|
try Some (Sodium.Box.Bigbytes.fast_box_open ck msg nonce) with
|
||||||
| Sodium.Verification_failure -> None
|
| 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 compare_target hash target =
|
||||||
let hash = Hash.Generic_hash.to_string hash in
|
let hash = Z.of_bits (Hash.Generic_hash.to_string hash) in
|
||||||
let rec check offset = function
|
Z.compare hash target <= 0
|
||||||
| [] -> true
|
|
||||||
| x :: xs ->
|
|
||||||
Compare.Uint64.(EndianString.BigEndian.get_int64 hash offset <= x)
|
|
||||||
&& check (offset + 8) xs in
|
|
||||||
check 0 target
|
|
||||||
|
|
||||||
let default_target =
|
let make_target f =
|
||||||
(* FIXME we use an easy target until we allow custom configuration. *)
|
if f < 0. || 256. < f then invalid_arg "Cryptobox.target_of_float" ;
|
||||||
[ Int64.shift_left 1L 48 ]
|
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 check_proof_of_work pk nonce target =
|
||||||
let hash =
|
let hash =
|
||||||
|
@ -16,8 +16,8 @@ val increment_nonce : ?step:int -> nonce -> nonce
|
|||||||
val nonce_encoding : nonce Data_encoding.t
|
val nonce_encoding : nonce Data_encoding.t
|
||||||
|
|
||||||
type target
|
type target
|
||||||
val make_target : (* unsigned *) Int64.t list -> target
|
|
||||||
val default_target : target
|
val default_target : target
|
||||||
|
val make_target : float -> target
|
||||||
|
|
||||||
type secret_key
|
type secret_key
|
||||||
type public_key
|
type public_key
|
||||||
|
Loading…
Reference in New Issue
Block a user