Shell: reduce difficulty for P2P identity...
... until we properly store the nonce in the configuration file, together with the expected difficulty.
This commit is contained in:
parent
710e3e755a
commit
daf3343dc1
@ -15,10 +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
|
||||||
(* target ought to be an unsigned 256 bit integer
|
type target = int64 list (* used as unsigned intergers... *)
|
||||||
but this representation works better with ocplib-endian; make
|
|
||||||
sure target has length 16! *)
|
|
||||||
type target = int list
|
|
||||||
exception TargetNot256Bit
|
exception TargetNot256Bit
|
||||||
|
|
||||||
let random_keypair = Sodium.Box.random_keypair
|
let random_keypair = Sodium.Box.random_keypair
|
||||||
@ -29,33 +26,33 @@ 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 validate_target target =
|
let make_target target =
|
||||||
if List.length target <> 16 then raise TargetNot256Bit;
|
if List.length target > 8 then raise TargetNot256Bit ;
|
||||||
if List.for_all (fun t -> t < 0 || t >= 1 lsl 16) target
|
target
|
||||||
then raise TargetNot256Bit
|
|
||||||
|
|
||||||
(* compare a SHA256 hash to a 256 bit target *)
|
(* Compare a SHA256 hash to a 256bits-target prefix.
|
||||||
let compare_target xs target =
|
The prefix is a list of "unsigned" int64. *)
|
||||||
let hash =
|
let compare_target hash target =
|
||||||
let hash = Cryptokit.Hash.sha256 () in
|
let rec check offset = function
|
||||||
List.iter (fun b -> hash#add_string (MBytes.to_string b)) xs;
|
| [] -> true
|
||||||
let r = hash#result in hash#wipe; r in
|
| x :: xs ->
|
||||||
let bytes = MBytes.of_string hash in
|
Compare.Uint64.(EndianString.BigEndian.get_int64 hash offset < x)
|
||||||
let get_16 = EndianBigstring.BigEndian.get_uint16 bytes in
|
&& check (offset + 8) xs in
|
||||||
let offsets = [0;2;4;6;8;10;12;14;16;18;20;22;24;26;28;30] in
|
check 0 target
|
||||||
List.for_all2 (fun o t -> get_16 o <= t) offsets target
|
|
||||||
|
|
||||||
let default_target =
|
let default_target =
|
||||||
let x = 65535 in [0;256;x;x;x;x;x;x;x;x;x;x;x;x;x;x]
|
(* FIXME we use an easy target until we allow custom configuration. *)
|
||||||
|
[ Int64.shift_left 1L 48 ]
|
||||||
|
|
||||||
let check_proof_of_work pk nonce target =
|
let check_proof_of_work pk nonce target =
|
||||||
let what_to_hash =
|
let hash =
|
||||||
[ Sodium.Box.Bigbytes.of_public_key pk
|
let hash = Cryptokit.Hash.sha256 () in
|
||||||
; Sodium.Box.Bigbytes.of_nonce nonce ] in
|
hash#add_string (Bytes.to_string @@ Sodium.Box.Bytes.of_public_key pk) ;
|
||||||
compare_target what_to_hash target
|
hash#add_string (Bytes.to_string @@ Sodium.Box.Bytes.of_nonce nonce) ;
|
||||||
|
let r = hash#result in hash#wipe ; r in
|
||||||
|
compare_target hash target
|
||||||
|
|
||||||
let generate_proof_of_work pk target =
|
let generate_proof_of_work pk target =
|
||||||
validate_target target;
|
|
||||||
let rec loop nonce =
|
let rec loop nonce =
|
||||||
if check_proof_of_work pk nonce target then nonce
|
if check_proof_of_work pk nonce target then nonce
|
||||||
else loop (increment_nonce nonce) in
|
else loop (increment_nonce nonce) in
|
||||||
|
@ -16,8 +16,7 @@ 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 compare_target : MBytes.t list -> target -> bool
|
val make_target : (* unsigned *) Int64.t list -> target
|
||||||
val validate_target : target -> unit
|
|
||||||
val default_target : target
|
val default_target : target
|
||||||
|
|
||||||
type secret_key
|
type secret_key
|
||||||
|
Loading…
Reference in New Issue
Block a user