2016-11-03 22:15:31 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2018-02-06 00:17:03 +04:00
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
2016-11-03 22:15:31 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
|
|
|
(** Tezos - X25519/XSalsa20-Poly1305 cryptography *)
|
|
|
|
|
2018-02-04 21:39:34 +04:00
|
|
|
open Tweetnacl
|
|
|
|
|
|
|
|
type secret_key = Box.secret Box.key
|
|
|
|
type public_key = Box.public Box.key
|
|
|
|
type channel_key = Box.combined Box.key
|
|
|
|
type nonce = Nonce.t
|
2017-01-23 14:09:51 +04:00
|
|
|
type target = Z.t
|
2016-11-03 22:15:31 +04:00
|
|
|
|
2018-02-06 22:16:26 +04:00
|
|
|
module Secretbox = struct
|
|
|
|
include Secretbox
|
|
|
|
|
|
|
|
let of_bytes bytes =
|
|
|
|
of_cstruct (Cstruct.of_bigarray bytes)
|
|
|
|
|
|
|
|
let of_bytes_exn bytes =
|
|
|
|
of_cstruct_exn (Cstruct.of_bigarray bytes)
|
|
|
|
|
|
|
|
let box key msg nonce =
|
|
|
|
let msg = Cstruct.of_bigarray msg in
|
|
|
|
Cstruct.to_bigarray (box ~key ~msg ~nonce)
|
|
|
|
|
|
|
|
let box_open key cmsg nonce =
|
|
|
|
let cmsg = Cstruct.of_bigarray cmsg in
|
|
|
|
Option.map ~f:Cstruct.to_bigarray (box_open ~key ~cmsg ~nonce)
|
|
|
|
|
|
|
|
let box_noalloc key nonce msg =
|
|
|
|
let msg = Cstruct.of_bigarray msg in
|
|
|
|
box_noalloc ~key ~nonce ~msg
|
|
|
|
|
|
|
|
let box_open_noalloc key nonce cmsg =
|
|
|
|
let cmsg = Cstruct.of_bigarray cmsg in
|
|
|
|
box_open_noalloc ~key ~nonce ~cmsg
|
|
|
|
end
|
|
|
|
|
2017-11-27 09:13:12 +04:00
|
|
|
module Public_key_hash = Blake2B.Make (Base58) (struct
|
2017-01-14 16:13:49 +04:00
|
|
|
let name = "Crypto_box.Public_key_hash"
|
|
|
|
let title = "A Cryptobox public key ID"
|
2017-02-19 21:22:32 +04:00
|
|
|
let b58check_prefix = Base58.Prefix.cryptobox_public_key_hash
|
2017-01-14 16:13:49 +04:00
|
|
|
let size = Some 16
|
|
|
|
end)
|
|
|
|
|
2017-02-19 21:22:32 +04:00
|
|
|
let () =
|
|
|
|
Base58.check_encoded_prefix Public_key_hash.b58check_encoding "id" 30
|
|
|
|
|
2017-01-14 16:13:49 +04:00
|
|
|
let hash pk =
|
2018-02-04 21:39:34 +04:00
|
|
|
Public_key_hash.hash_bytes [Cstruct.to_bigarray (Box.to_cstruct pk)]
|
|
|
|
|
|
|
|
let zerobytes = Box.zerobytes
|
|
|
|
let boxzerobytes = Box.boxzerobytes
|
2017-01-14 16:13:49 +04:00
|
|
|
|
|
|
|
let random_keypair () =
|
2018-02-04 21:39:34 +04:00
|
|
|
let pk, sk = Box.keypair () in
|
2017-01-14 16:13:49 +04:00
|
|
|
sk, pk, hash pk
|
2018-02-06 22:16:26 +04:00
|
|
|
|
|
|
|
let zero_nonce = Tweetnacl.Nonce.(of_cstruct_exn (Cstruct.create bytes))
|
2018-02-04 21:39:34 +04:00
|
|
|
let random_nonce = Nonce.gen
|
|
|
|
let increment_nonce = Nonce.increment
|
|
|
|
|
|
|
|
let box sk pk msg nonce =
|
|
|
|
let msg = Cstruct.of_bigarray msg in
|
|
|
|
Cstruct.to_bigarray (Box.box ~sk ~pk ~msg ~nonce)
|
|
|
|
|
|
|
|
let box_open sk pk cmsg nonce =
|
|
|
|
let cmsg = Cstruct.of_bigarray cmsg in
|
|
|
|
Option.map ~f:Cstruct.to_bigarray (Box.box_open ~sk ~pk ~cmsg ~nonce)
|
|
|
|
|
|
|
|
let box_noalloc sk pk nonce msg =
|
|
|
|
let msg = Cstruct.of_bigarray msg in
|
|
|
|
Box.box_noalloc ~sk ~pk ~nonce ~msg
|
|
|
|
|
|
|
|
let box_open_noalloc sk pk nonce cmsg =
|
|
|
|
let cmsg = Cstruct.of_bigarray cmsg in
|
|
|
|
Box.box_open_noalloc ~sk ~pk ~nonce ~cmsg
|
|
|
|
|
|
|
|
let precompute sk pk = Box.combine pk sk
|
|
|
|
|
|
|
|
let fast_box k msg nonce =
|
|
|
|
let msg = Cstruct.of_bigarray msg in
|
|
|
|
Cstruct.to_bigarray (Box.box_combined ~k ~msg ~nonce)
|
|
|
|
|
|
|
|
let fast_box_open k cmsg nonce =
|
|
|
|
let cmsg = Cstruct.of_bigarray cmsg in
|
|
|
|
Option.map ~f:Cstruct.to_bigarray (Box.box_open_combined ~k ~cmsg ~nonce)
|
|
|
|
|
|
|
|
let fast_box_noalloc k nonce msg =
|
|
|
|
let msg = Cstruct.of_bigarray msg in
|
|
|
|
Box.box_combined_noalloc ~k ~nonce ~msg
|
|
|
|
|
|
|
|
let fast_box_open_noalloc k nonce cmsg =
|
|
|
|
let cmsg = Cstruct.of_bigarray cmsg in
|
|
|
|
Box.box_open_combined_noalloc ~k ~nonce ~cmsg
|
2017-01-14 16:13:49 +04:00
|
|
|
|
2016-11-25 21:03:57 +04:00
|
|
|
let compare_target hash target =
|
2017-11-27 09:13:12 +04:00
|
|
|
let hash = Z.of_bits (Blake2B.to_string hash) in
|
2017-01-23 14:09:51 +04:00
|
|
|
Z.compare hash target <= 0
|
|
|
|
|
|
|
|
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)
|
2016-11-19 02:07:27 +04:00
|
|
|
|
2017-01-23 14:09:51 +04:00
|
|
|
let default_target = make_target 24.
|
2016-11-18 00:02:32 +04:00
|
|
|
|
|
|
|
let check_proof_of_work pk nonce target =
|
2016-11-25 21:03:57 +04:00
|
|
|
let hash =
|
2017-11-27 09:13:12 +04:00
|
|
|
Blake2B.hash_bytes [
|
2018-02-04 21:39:34 +04:00
|
|
|
Cstruct.to_bigarray (Box.to_cstruct pk) ;
|
|
|
|
Cstruct.to_bigarray (Nonce.to_cstruct nonce) ;
|
2016-11-25 22:46:50 +04:00
|
|
|
] in
|
2016-11-25 21:03:57 +04:00
|
|
|
compare_target hash target
|
2016-11-18 00:02:32 +04:00
|
|
|
|
2017-01-23 14:09:48 +04:00
|
|
|
let generate_proof_of_work ?max pk target =
|
|
|
|
let may_interupt =
|
|
|
|
match max with
|
|
|
|
| None -> (fun _ -> ())
|
|
|
|
| Some max -> (fun cpt -> if max < cpt then raise Not_found) in
|
|
|
|
let rec loop nonce cpt =
|
|
|
|
may_interupt cpt ;
|
|
|
|
if check_proof_of_work pk nonce target then
|
|
|
|
nonce
|
|
|
|
else
|
2018-02-04 21:39:34 +04:00
|
|
|
loop (Nonce.increment nonce) (cpt + 1) in
|
2017-01-23 14:09:48 +04:00
|
|
|
loop (random_nonce ()) 0
|
2016-11-09 06:18:09 +04:00
|
|
|
|
2018-02-08 13:51:01 +04:00
|
|
|
let public_key_to_bigarray x = Cstruct.to_bigarray (Box.to_cstruct x)
|
|
|
|
let public_key_of_bigarray x = Box.pk_of_cstruct_exn (Cstruct.of_bigarray x)
|
|
|
|
let public_key_size = Box.pkbytes
|
|
|
|
|
|
|
|
let secret_key_to_bigarray x = Cstruct.to_bigarray (Box.to_cstruct x)
|
|
|
|
let secret_key_of_bigarray x = Box.sk_of_cstruct_exn (Cstruct.of_bigarray x)
|
|
|
|
let secret_key_size = Box.skbytes
|
|
|
|
|
|
|
|
let nonce_to_bigarray x = Cstruct.to_bigarray (Nonce.to_cstruct x)
|
|
|
|
let nonce_of_bigarray x = Nonce.of_cstruct_exn (Cstruct.of_bigarray x)
|
|
|
|
let nonce_size = Nonce.bytes
|
2018-04-03 13:44:11 +04:00
|
|
|
|
|
|
|
let public_key_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
conv
|
|
|
|
public_key_to_bigarray
|
|
|
|
public_key_of_bigarray
|
|
|
|
(Fixed.bytes public_key_size)
|
|
|
|
|
|
|
|
let secret_key_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
conv
|
|
|
|
secret_key_to_bigarray
|
|
|
|
secret_key_of_bigarray
|
|
|
|
(Fixed.bytes secret_key_size)
|
|
|
|
|
|
|
|
let nonce_encoding =
|
|
|
|
let open Data_encoding in
|
|
|
|
conv
|
|
|
|
nonce_to_bigarray
|
|
|
|
nonce_of_bigarray
|
|
|
|
(Fixed.bytes nonce_size)
|
|
|
|
|