diff --git a/src/utils/crypto_box.ml b/src/utils/crypto_box.ml index 7620a6aad..fb06f3fed 100644 --- a/src/utils/crypto_box.ml +++ b/src/utils/crypto_box.ml @@ -15,6 +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 let random_keypair = Sodium.Box.random_keypair let random_nonce = Sodium.Box.random_nonce @@ -24,6 +25,13 @@ let box_open sk pk msg nonce = try Some (Sodium.Box.Bigbytes.box_open sk pk msg nonce) with | Sodium.Verification_failure -> None +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 ()) + let public_key_encoding = let open Data_encoding in conv @@ -44,4 +52,3 @@ let nonce_encoding = Sodium.Box.Bigbytes.of_nonce Sodium.Box.Bigbytes.to_nonce (Fixed.bytes Sodium.Box.nonce_size) - diff --git a/src/utils/crypto_box.mli b/src/utils/crypto_box.mli index e286ff0aa..a31fb6380 100644 --- a/src/utils/crypto_box.mli +++ b/src/utils/crypto_box.mli @@ -10,6 +10,7 @@ (** Tezos - X25519/XSalsa20-Poly1305 cryptography *) type nonce +type difficulty val random_nonce : unit -> nonce val increment_nonce : ?step:int -> nonce -> nonce @@ -28,3 +29,5 @@ val box : secret_key -> public_key -> MBytes.t -> nonce -> MBytes.t val box_open : secret_key -> public_key -> MBytes.t -> nonce -> MBytes.t option +val check_proof_of_work : public_key -> nonce -> difficulty -> bool +val generate_proof_of_work : public_key -> difficulty -> nonce