diff --git a/src/node/net/p2p.ml b/src/node/net/p2p.ml index 9452db98e..6d8e0de68 100644 --- a/src/node/net/p2p.ml +++ b/src/node/net/p2p.ml @@ -174,6 +174,7 @@ type net = { and peer = { gid : gid ; public_key : Crypto_box.public_key ; + (*proof_of_work_nonce : Crypto_box.nonce ;*) current_nonce : unit -> Crypto_box.nonce ; point : point ; listening_port : port option ; diff --git a/src/utils/crypto_box.ml b/src/utils/crypto_box.ml index 8931436c9..c0cc4248e 100644 --- a/src/utils/crypto_box.ml +++ b/src/utils/crypto_box.ml @@ -13,6 +13,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 let random_keypair = Sodium.Box.random_keypair let random_nonce = Sodium.Box.random_nonce @@ -27,3 +28,9 @@ let to_public_key = Sodium.Box.Bigbytes.to_public_key let of_public_key = Sodium.Box.Bigbytes.of_public_key let to_nonce = Sodium.Box.Bigbytes.to_nonce let of_nonce = Sodium.Box.Bigbytes.of_nonce +let check_proof_of_work pk nonce difficulty = assert false +let generate_proof_of_work pk difficulty = + let rec loop nonce = + if check_proof_of_work pk nonce difficulty then nonce + else loop (increment_nonce nonce) in + loop (random_nonce ()) diff --git a/src/utils/crypto_box.mli b/src/utils/crypto_box.mli index c1d1b9e47..1bb45c606 100644 --- a/src/utils/crypto_box.mli +++ b/src/utils/crypto_box.mli @@ -12,6 +12,7 @@ type secret_key type public_key type nonce +type difficulty val random_keypair : unit -> secret_key * public_key val random_nonce : unit -> nonce @@ -24,3 +25,5 @@ val to_public_key : MBytes.t -> public_key val of_public_key : public_key -> MBytes.t val to_nonce : MBytes.t -> nonce val of_nonce : nonce -> MBytes.t +val check_proof_of_work : public_key -> nonce -> difficulty -> bool +val generate_proof_of_work : public_key -> difficulty -> nonce