From f22b3576d2d679cd57a53a096e0131dbec2e2603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Thu, 8 Feb 2018 10:51:01 +0100 Subject: [PATCH] Client refactor: simpler dependencies in `tezos-crypto` --- src/lib_base/blake2B.ml | 187 +++++++++++++ src/lib_base/blake2B.mli | 75 ++++++ src/lib_base/block_hash.ml | 6 +- src/lib_base/block_hash.mli | 2 +- src/lib_base/context_hash.ml | 6 +- src/lib_base/context_hash.mli | 2 +- src/lib_base/crypto_box.ml | 32 +++ src/lib_base/crypto_box.mli | 14 + src/lib_base/ed25519.ml | 111 ++++++++ src/lib_base/ed25519.mli | 44 +++ src/lib_base/net_id.ml | 250 +++++++----------- src/lib_base/net_id.mli | 3 +- src/lib_base/operation_hash.ml | 6 +- src/lib_base/operation_hash.mli | 2 +- src/lib_base/operation_list_hash.ml | 6 +- src/lib_base/operation_list_hash.mli | 2 +- src/lib_base/operation_list_list_hash.ml | 6 +- src/lib_base/operation_list_list_hash.mli | 2 +- src/lib_base/p2p_identity.mli | 1 + src/lib_base/p2p_peer_id.ml | 2 +- src/lib_base/p2p_peer_id.mli | 2 +- src/lib_base/protocol_environment.ml | 3 +- src/lib_base/protocol_hash.ml | 6 +- src/lib_base/protocol_hash.mli | 2 +- src/lib_base/s.ml | 121 +++++++++ src/lib_base/tzPervasives.ml | 6 +- src/lib_base/tzPervasives.mli | 7 +- src/lib_crypto/blake2B.ml | 109 +------- src/lib_crypto/blake2B.mli | 21 +- src/lib_crypto/crypto_box.ml | 32 +-- src/lib_crypto/crypto_box.mli | 16 +- src/lib_crypto/ed25519.ml | 106 +------- src/lib_crypto/ed25519.mli | 35 +-- src/lib_crypto/jbuild | 13 +- src/lib_crypto/rand.mli | 4 +- src/lib_crypto/s.ml | 76 ++---- src/lib_crypto/test/jbuild | 2 - src/lib_crypto/test/test_merkle.ml | 3 - src/lib_crypto/tezos-crypto.opam | 6 +- .../jbuild_protocol_template | 3 +- src/lib_protocol_environment_sigs/v1/s.mli | 15 +- src/lib_shell/distributed_db_functors.ml | 2 +- src/lib_storage/context.ml | 2 +- 43 files changed, 802 insertions(+), 549 deletions(-) create mode 100644 src/lib_base/blake2B.ml create mode 100644 src/lib_base/blake2B.mli create mode 100644 src/lib_base/crypto_box.ml create mode 100644 src/lib_base/crypto_box.mli create mode 100644 src/lib_base/ed25519.ml create mode 100644 src/lib_base/ed25519.mli diff --git a/src/lib_base/blake2B.ml b/src/lib_base/blake2B.ml new file mode 100644 index 000000000..9ca48ff36 --- /dev/null +++ b/src/lib_base/blake2B.ml @@ -0,0 +1,187 @@ +(**************************************************************************) +(* *) +(* Copyright (c) 2014 - 2018. *) +(* Dynamic Ledger Solutions, Inc. *) +(* *) +(* All rights reserved. No warranty, explicit or implicit, provided. *) +(* *) +(**************************************************************************) + +open Tezos_crypto +open Error_monad + +module Extend(H : Tezos_crypto.S.HASH) = struct + + include H + + let encoding = + let open Data_encoding in + splitted + ~binary: + (conv H.to_bytes H.of_bytes_exn (Fixed.bytes H.size)) + ~json: + (describe ~title: (H.title ^ " (Base58Check-encoded Blake2B hash)") @@ + conv H.to_b58check (Data_encoding.Json.wrap_error H.of_b58check_exn) string) + + let of_b58check s = + match H.of_b58check_opt s with + | Some x -> Ok x + | None -> + generic_error "Failed to read a base58-encoded hash (%s)" H.name + + let of_bytes s = + match H.of_bytes_opt s with + | Some x -> Ok x + | None -> + generic_error "Failed to deserialize a hash (%s)" H.name + + let rpc_arg = + RPC_arg.make + ~name:(Format.asprintf "hash.%s" H.name) + ~descr:(Format.asprintf "A b58check-encoded hash (%s)" H.name) + ~destruct: + (fun s -> + match H.of_b58check_opt s with + | None -> + Error (Format.asprintf + "failed to decode b58check-encoded hash (%s): %S" + H.name s) + | Some v -> Ok v) + ~construct:H.to_b58check + () + + let param ?(name=H.name) ?(desc=H.title) t = + Cli_entries.param + ~name + ~desc (Cli_entries.parameter (fun _ str -> Lwt.return (of_b58check str))) t + + module Set = struct + include Set.Make(struct type nonrec t = t let compare = compare end) + exception Found of elt + let random_elt s = + let n = Random.int (cardinal s) in + try + ignore + (fold (fun x i -> if i = n then raise (Found x) ; i+1) s 0 : int) ; + assert false + with Found x -> x + let encoding = + Data_encoding.conv + elements + (fun l -> List.fold_left (fun m x -> add x m) empty l) + Data_encoding.(list encoding) + end + + module Table = struct + include Hashtbl.Make(struct + type t = H.t + let hash = + if H.size >= 64 then + fun h -> Int64.to_int (MBytes.get_int64 (H.to_bytes h) 0) + else if H.size >= 32 then + fun h -> Int32.to_int (MBytes.get_int32 (H.to_bytes h) 0) + else + fun h -> + let r = ref 0 in + let h = H.to_bytes h in + for i = 0 to H.size / 8 - 1 do + r := MBytes.get_uint8 h i + 8 * !r + done ; + !r + let equal = H.equal + end) + let encoding arg_encoding = + Data_encoding.conv + (fun h -> fold (fun k v l -> (k, v) :: l) h []) + (fun l -> + let h = create (List.length l) in + List.iter (fun (k,v) -> add h k v) l ; + h) + Data_encoding.(list (tup2 encoding arg_encoding)) + end + + module Map = struct + include Map.Make(struct type nonrec t = t let compare = compare end) + let encoding arg_encoding = + Data_encoding.conv + bindings + (fun l -> List.fold_left (fun m (k,v) -> add k v m) empty l) + Data_encoding.(list (tup2 encoding arg_encoding)) + end + +end + +module Extend_merkle_tree(H : Tezos_crypto.S.MERKLE_TREE) = struct + + include Extend(H) + + type elt = H.elt + let elt_bytes = H.elt_bytes + + let empty = hash_bytes [] + + include Tezos_crypto.Blake2B.Generic_Merkle_tree(struct + type nonrec t = t + type nonrec elt = elt + let empty = empty + let leaf x = hash_bytes [H.elt_bytes x] + let node x y = hash_bytes [to_bytes x; to_bytes y] + end) + + let path_encoding = + let open Data_encoding in + mu "path" + (fun path_encoding -> + union [ + case (Tag 240) + (obj2 + (req "path" path_encoding) + (req "right" encoding)) + (function Left (p, r) -> Some (p, r) | _ -> None) + (fun (p, r) -> Left (p, r)) ; + case (Tag 15) + (obj2 + (req "left" encoding) + (req "path" path_encoding)) + (function Right (r, p) -> Some (r, p) | _ -> None) + (fun (r, p) -> Right (r, p)) ; + case (Tag 0) + unit + (function Op -> Some () | _ -> None) + (fun () -> Op) + ]) + +end + +module type Name = Blake2B.Name +module type PrefixedName = Blake2B.PrefixedName +module Make_minimal = Blake2B.Make_minimal +module Make + (R : sig + val register_encoding: + prefix: string -> + length:int -> + to_raw: ('a -> string) -> + of_raw: (string -> 'a option) -> + wrap: ('a -> Base58.data) -> + 'a Base58.encoding + end) + (K : Blake2B.PrefixedName) = + Extend(Blake2B.Make(R)(K)) +module Make_merkle_tree + (R : sig + val register_encoding: + prefix: string -> + length:int -> + to_raw: ('a -> string) -> + of_raw: (string -> 'a option) -> + wrap: ('a -> Base58.data) -> + 'a Base58.encoding + end) + (K : PrefixedName) + (Contents: sig + type t + val to_bytes: t -> MBytes.t + end) = + Extend_merkle_tree(Blake2B.Make_merkle_tree(R)(K)(Contents)) +module Generic_Merkle_tree = Blake2B.Generic_Merkle_tree diff --git a/src/lib_base/blake2B.mli b/src/lib_base/blake2B.mli new file mode 100644 index 000000000..42b7c9d88 --- /dev/null +++ b/src/lib_base/blake2B.mli @@ -0,0 +1,75 @@ +(**************************************************************************) +(* *) +(* Copyright (c) 2014 - 2018. *) +(* Dynamic Ledger Solutions, Inc. *) +(* *) +(* All rights reserved. No warranty, explicit or implicit, provided. *) +(* *) +(**************************************************************************) + +(** Builds a new Hash type using Blake2B. *) + +(** The parameters for creating a new Hash type using + {!Make_Blake2B}. Both {!name} and {!title} are only informative, + used in error messages and serializers. *) + +module type Name = sig + val name : string + val title : string + val size : int option +end + +module type PrefixedName = sig + include Name + val b58check_prefix : string +end + +module Make_minimal (Name : Name) : S.MINIMAL_HASH +module Make + (Register : sig + val register_encoding: + prefix: string -> + length: int -> + to_raw: ('a -> string) -> + of_raw: (string -> 'a option) -> + wrap: ('a -> Tezos_crypto.Base58.data) -> + 'a Tezos_crypto.Base58.encoding + end) + (Name : PrefixedName) : S.INTERNAL_HASH + +module Make_merkle_tree + (R : sig + val register_encoding: + prefix: string -> + length:int -> + to_raw: ('a -> string) -> + of_raw: (string -> 'a option) -> + wrap: ('a -> Tezos_crypto.Base58.data) -> + 'a Tezos_crypto.Base58.encoding + end) + (K : PrefixedName) + (Contents: sig + type t + val to_bytes: t -> MBytes.t + end) : S.INTERNAL_MERKLE_TREE with type elt = Contents.t + +module Generic_Merkle_tree (H : sig + type t + type elt + val empty : t + val leaf : elt -> t + val node : t -> t -> t + end) : sig + val compute : H.elt list -> H.t + type path = + | Left of path * H.t + | Right of H.t * path + | Op + val compute_path: H.elt list -> int -> path + val check_path: path -> H.elt -> H.t * int +end + +module Extend (H: Tezos_crypto.S.HASH) + : S.INTERNAL_HASH with type t = H.t +module Extend_merkle_tree (H: Tezos_crypto.S.MERKLE_TREE) + : S.INTERNAL_MERKLE_TREE with type t = H.t and type elt = H.elt diff --git a/src/lib_base/block_hash.ml b/src/lib_base/block_hash.ml index 85bca9c64..055da0dc7 100644 --- a/src/lib_base/block_hash.ml +++ b/src/lib_base/block_hash.ml @@ -7,12 +7,12 @@ (* *) (**************************************************************************) -include Blake2B.Make (Base58) (struct +include Blake2B.Make (Tezos_crypto.Base58) (struct let name = "Block_hash" let title = "A Tezos block ID" - let b58check_prefix = Base58.Prefix.block_hash + let b58check_prefix = Tezos_crypto.Base58.Prefix.block_hash let size = None end) let () = - Base58.check_encoded_prefix b58check_encoding "B" 51 + Tezos_crypto.Base58.check_encoded_prefix b58check_encoding "B" 51 diff --git a/src/lib_base/block_hash.mli b/src/lib_base/block_hash.mli index 8cc7b165b..92f5c99bf 100644 --- a/src/lib_base/block_hash.mli +++ b/src/lib_base/block_hash.mli @@ -7,4 +7,4 @@ (* *) (**************************************************************************) -include Tezos_crypto.S.INTERNAL_HASH +include S.INTERNAL_HASH diff --git a/src/lib_base/context_hash.ml b/src/lib_base/context_hash.ml index 5a4c06550..51c9c821c 100644 --- a/src/lib_base/context_hash.ml +++ b/src/lib_base/context_hash.ml @@ -7,12 +7,12 @@ (* *) (**************************************************************************) -include Blake2B.Make (Base58) (struct +include Blake2B.Make (Tezos_crypto.Base58) (struct let name = "Context_hash" let title = "A hash of context" - let b58check_prefix = Base58.Prefix.context_hash + let b58check_prefix = Tezos_crypto.Base58.Prefix.context_hash let size = None end) let () = - Base58.check_encoded_prefix b58check_encoding "Co" 52 + Tezos_crypto.Base58.check_encoded_prefix b58check_encoding "Co" 52 diff --git a/src/lib_base/context_hash.mli b/src/lib_base/context_hash.mli index 8cc7b165b..92f5c99bf 100644 --- a/src/lib_base/context_hash.mli +++ b/src/lib_base/context_hash.mli @@ -7,4 +7,4 @@ (* *) (**************************************************************************) -include Tezos_crypto.S.INTERNAL_HASH +include S.INTERNAL_HASH diff --git a/src/lib_base/crypto_box.ml b/src/lib_base/crypto_box.ml new file mode 100644 index 000000000..5cc2dee89 --- /dev/null +++ b/src/lib_base/crypto_box.ml @@ -0,0 +1,32 @@ +(**************************************************************************) +(* *) +(* Copyright (c) 2014 - 2018. *) +(* Dynamic Ledger Solutions, Inc. *) +(* *) +(* All rights reserved. No warranty, explicit or implicit, provided. *) +(* *) +(**************************************************************************) + +include Tezos_crypto.Crypto_box + +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) + diff --git a/src/lib_base/crypto_box.mli b/src/lib_base/crypto_box.mli new file mode 100644 index 000000000..447598ddd --- /dev/null +++ b/src/lib_base/crypto_box.mli @@ -0,0 +1,14 @@ +(**************************************************************************) +(* *) +(* Copyright (c) 2014 - 2018. *) +(* Dynamic Ledger Solutions, Inc. *) +(* *) +(* All rights reserved. No warranty, explicit or implicit, provided. *) +(* *) +(**************************************************************************) + +include (module type of Tezos_crypto.Crypto_box) + +val public_key_encoding : public_key Data_encoding.t +val secret_key_encoding : secret_key Data_encoding.t +val nonce_encoding : nonce Data_encoding.t diff --git a/src/lib_base/ed25519.ml b/src/lib_base/ed25519.ml new file mode 100644 index 000000000..a17d3f392 --- /dev/null +++ b/src/lib_base/ed25519.ml @@ -0,0 +1,111 @@ + +module Public_key_hash = Blake2B.Extend (Tezos_crypto.Ed25519.Public_key_hash) + +module Public_key = struct + include Tezos_crypto.Ed25519.Public_key + let encoding = + let open Data_encoding in + splitted + ~json: + (describe + ~title: "An Ed25519 public key (Tezos_crypto.Base58Check encoded)" @@ + conv + (fun s -> to_b58check s) + (fun s -> + match of_b58check_opt s with + | Some x -> x + | None -> Data_encoding.Json.cannot_destruct + "Ed25519 public key: unexpected prefix.") + string) + ~binary: + (conv + to_bytes + of_bytes_exn + (Fixed.bytes size)) + let of_b58check s = + match of_b58check_opt s with + | Some x -> Ok x + | None -> + Error_monad.generic_error + "Failed to read a base58-encoded Ed25519 public key" + let param + ?(name="ed25519-public") + ?(desc="Ed25519 public key (b58check-encoded)") t = + Cli_entries.(param ~name ~desc + (parameter (fun _ str -> Lwt.return (of_b58check str))) t) + +end + +module Secret_key = struct + include Tezos_crypto.Ed25519.Secret_key + let encoding = + let open Data_encoding in + splitted + ~json: + (describe + ~title: "An Ed25519 secret key (Tezos_crypto.Base58Check encoded)" @@ + conv + (fun s -> to_b58check s) + (fun s -> + match of_b58check_opt s with + | Some x -> x + | None -> Data_encoding.Json.cannot_destruct + "Ed25519 secret key: unexpected prefix.") + string) + ~binary: + (conv + to_bytes + of_bytes_exn + (Fixed.bytes size)) + let of_b58check s = + match of_b58check_opt s with + | Some x -> Ok x + | None -> + Error_monad.generic_error + "Failed to read a base58-encoded Ed25519 secret key" + let param + ?(name="ed25519-secret") + ?(desc="Ed25519 secret key (b58check-encoded)") t = + Cli_entries.(param ~name ~desc + (parameter (fun _ str -> Lwt.return (of_b58check str))) t) +end + +module Signature = struct + include Tezos_crypto.Ed25519.Signature + let encoding = + let open Data_encoding in + splitted + ~json: + (describe + ~title: "An Ed25519 signature (Base58Check encoded)" @@ + conv + (fun s -> to_b58check s) + (fun s -> + match of_b58check_opt s with + | Some x -> x + | None -> Data_encoding.Json.cannot_destruct + "Ed25519 signature: unexpected prefix.") + string) + ~binary: + (conv + to_bytes + of_bytes_exn + (Fixed.bytes size)) + let of_b58check s = + match of_b58check_opt s with + | Some x -> Ok x + | None -> + Error_monad.generic_error + "Failed to read a base58-encoded Ed25519 signature" + let param + ?(name="ed25519-signature") + ?(desc="Ed25519 signature (b58check-encoded)") t = + Cli_entries.(param ~name ~desc + (parameter (fun _ str -> Lwt.return (of_b58check str))) t) +end + +include (Tezos_crypto.Ed25519 : (module type of Tezos_crypto.Ed25519) + with module Public_key_hash := Public_key_hash + and module Public_key := Public_key + and module Secret_key := Secret_key + and module Signature := Signature) diff --git a/src/lib_base/ed25519.mli b/src/lib_base/ed25519.mli new file mode 100644 index 000000000..92346ce7e --- /dev/null +++ b/src/lib_base/ed25519.mli @@ -0,0 +1,44 @@ + +open Error_monad + +module Public_key_hash : + S.INTERNAL_HASH with type t = Tezos_crypto.Ed25519.Public_key_hash.t + +module Public_key : sig + include (module type of Tezos_crypto.Ed25519.Public_key) + val encoding: t Data_encoding.t + val param: + ?name:string -> + ?desc:string -> + ('a, 'b, 'c) Cli_entries.params -> + (t -> 'a, 'b, 'c) Cli_entries.params + val of_b58check: string -> t tzresult +end + +module Secret_key : sig + include (module type of Tezos_crypto.Ed25519.Secret_key) + val encoding: t Data_encoding.t + val param: + ?name:string -> + ?desc:string -> + ('a, 'b, 'c) Cli_entries.params -> + (t -> 'a, 'b, 'c) Cli_entries.params + val of_b58check: string -> t tzresult +end + +module Signature : sig + include (module type of Tezos_crypto.Ed25519.Signature) + val encoding: t Data_encoding.t + val param: + ?name:string -> + ?desc:string -> + ('a, 'b, 'c) Cli_entries.params -> + (t -> 'a, 'b, 'c) Cli_entries.params + val of_b58check: string -> t tzresult +end + +include (module type of Tezos_crypto.Ed25519) + with module Public_key_hash := Public_key_hash + and module Public_key := Public_key + and module Secret_key := Secret_key + and module Signature := Signature diff --git a/src/lib_base/net_id.ml b/src/lib_base/net_id.ml index af7d4d060..aed7ee66e 100644 --- a/src/lib_base/net_id.ml +++ b/src/lib_base/net_id.ml @@ -7,166 +7,104 @@ (* *) (**************************************************************************) -open Error_monad +module Raw = struct -type t = string + type t = string -let name = "Net_id" -let title = "Network identifier" + let name = "Net_id" + let title = "Network identifier" -let size = 4 + let extract bh = + MBytes.substring (Block_hash.to_bytes bh) 0 4 + let hash_bytes l = extract (Block_hash.hash_bytes l) + let hash_string l = extract (Block_hash.hash_string l) -let extract bh = - MBytes.substring (Block_hash.to_bytes bh) 0 4 + let size = 4 -let hash_bytes l = extract (Block_hash.hash_bytes l) -let hash_string l = extract (Block_hash.hash_string l) + let compare = String.compare + let equal = String.equal + + let of_string s = + if String.length s <> size then None else Some s + let of_string_exn s = + match of_string s with + | None -> + let msg = + Printf.sprintf "%s.of_string: wrong string size (%d)" + name (String.length s) in + raise (Invalid_argument msg) + | Some h -> h + + let to_string s = s + let of_hex s = of_string (Hex.to_string (`Hex s)) + let of_hex_exn s = of_string_exn (Hex.to_string (`Hex s)) + let to_hex s = + let `Hex s = Hex.of_string (to_string s) in + s + + + let of_bytes_opt b = + if MBytes.length b <> size then + None + else + Some (MBytes.to_string b) + let of_bytes_exn b = + match of_bytes_opt b with + | None -> + let msg = + Printf.sprintf "%s.of_bytes: wrong string size (%d)" + name (MBytes.length b) in + raise (Invalid_argument msg) + | Some h -> h + let to_bytes = MBytes.of_string + + let read src off = of_bytes_exn @@ MBytes.sub src off size + let write dst off h = MBytes.blit (to_bytes h) 0 dst off size + + let path_length = 1 + let to_path key l = to_hex key :: l + let of_path path = + let path = String.concat "" path in + of_hex path + let of_path_exn path = + let path = String.concat "" path in + of_hex_exn path + + let prefix_path p = + let `Hex p = Hex.of_string p in + [ p ] + + let zero = + match of_hex (String.make (size * 2) '0') with + | Some c -> c + | None -> assert false + + type Tezos_crypto.Base58.data += Hash of t + + let b58check_encoding = + Tezos_crypto.Base58.register_encoding + ~prefix: Tezos_crypto.Base58.Prefix.net_id + ~length: size + ~wrap: (fun s -> Hash s) + ~of_raw:of_string ~to_raw: (fun h -> h) + + let of_b58check_opt s = + Tezos_crypto.Base58.simple_decode b58check_encoding s + let of_b58check_exn s = + match Tezos_crypto.Base58.simple_decode b58check_encoding s with + | Some x -> x + | None -> Format.kasprintf Pervasives.failwith "Unexpected hash (%s)" name + let to_b58check s = Tezos_crypto.Base58.simple_encode b58check_encoding s + let to_short_b58check = to_b58check + + let pp ppf t = + Format.pp_print_string ppf (to_b58check t) + + let pp_short ppf t = + Format.pp_print_string ppf (to_short_b58check t) + +end + +include Blake2B.Extend(Raw) let of_block_hash bh = hash_bytes [Block_hash.to_bytes bh] - -type Base58.data += Hash of t - -let of_string s = - if String.length s <> size then None else Some s -let of_string_exn s = - match of_string s with - | None -> - let msg = - Printf.sprintf "%s.of_string: wrong string size (%d)" - name (String.length s) in - raise (Invalid_argument msg) - | Some h -> h -let to_string s = s - -let of_hex s = of_string (Hex.to_string (`Hex s)) -let of_hex_exn s = of_string_exn (Hex.to_string (`Hex s)) -let to_hex s = - let `Hex s = Hex.of_string (to_string s) in - s - -let compare = String.compare -let equal = String.equal - -let of_bytes b = - if MBytes.length b <> size then - None - else - Some (MBytes.to_string b) -let of_bytes_exn b = - match of_bytes b with - | None -> - let msg = - Printf.sprintf "%s.of_bytes: wrong string size (%d)" - name (MBytes.length b) in - raise (Invalid_argument msg) - | Some h -> h -let to_bytes = MBytes.of_string - -let read src off = of_bytes_exn @@ MBytes.sub src off size -let write dst off h = MBytes.blit (to_bytes h) 0 dst off size - -let b58check_encoding = - Base58.register_encoding - ~prefix: Base58.Prefix.net_id - ~length: size - ~wrap: (fun s -> Hash s) - ~of_raw:of_string ~to_raw: (fun h -> h) - -let of_b58check_opt s = - Base58.simple_decode b58check_encoding s -let of_b58check_exn s = - match Base58.simple_decode b58check_encoding s with - | Some x -> x - | None -> Format.kasprintf Pervasives.failwith "Unexpected hash (%s)" name -let of_b58check s = - match Base58.simple_decode b58check_encoding s with - | Some x -> Ok x - | None -> generic_error "Unexpected hash (%s)" name -let to_b58check s = Base58.simple_encode b58check_encoding s -let to_short_b58check = to_b58check - -let encoding = - let open Data_encoding in - splitted - ~binary: (Fixed.string size) - ~json: - (describe ~title: (title ^ " (Base58Check-encoded Blake2B hash)") @@ - conv to_b58check (Data_encoding.Json.wrap_error of_b58check_exn) string) - -let param ?(name=name) ?(desc=title) t = - Cli_entries.(param ~name ~desc (parameter (fun _ str -> Lwt.return (of_b58check str))) t) - -let pp ppf t = - Format.pp_print_string ppf (to_b58check t) - -let pp_short ppf t = - Format.pp_print_string ppf (to_short_b58check t) - -module Set = struct - include Set.Make(struct type nonrec t = t let compare = compare end) - exception Found of elt - let random_elt s = - let n = Random.int (cardinal s) in - try - ignore - (fold (fun x i -> if i = n then raise (Found x) ; i+1) s 0 : int) ; - assert false - with Found x -> x - let encoding = - Data_encoding.conv - elements - (fun l -> List.fold_left (fun m x -> add x m) empty l) - Data_encoding.(list encoding) -end -let random_set_elt = Set.random_elt - -module Map = struct - include Map.Make(struct type nonrec t = t let compare = compare end) - let encoding arg_encoding = - Data_encoding.conv - bindings - (fun l -> List.fold_left (fun m (k,v) -> add k v m) empty l) - Data_encoding.(list (tup2 encoding arg_encoding)) -end - -let path_length = 1 -let to_path key l = to_hex key :: l -let of_path path = - let path = String.concat "" path in - of_hex path -let of_path_exn path = - let path = String.concat "" path in - of_hex_exn path - -let prefix_path p = - let `Hex p = Hex.of_string p in - [ p ] - -module Table = struct - include Hashtbl.Make(struct - type nonrec t = t - let hash = Hashtbl.hash - let equal = equal - end) -end - -let () = - Base58.check_encoded_prefix b58check_encoding "Net" 15 - -let zero = - match of_hex (String.make (size * 2) '0') with - | Some c -> c - | None -> assert false - - -let rpc_arg = - RPC_arg.make - ~name:(Format.asprintf "hash.%s" name) - ~descr:(Format.asprintf "A b58check-encoded hash (%s)" name) - ~destruct: - (fun s -> - match of_b58check_opt s with - | None -> Error "" - | Some v -> Ok v) - ~construct:to_b58check - () diff --git a/src/lib_base/net_id.mli b/src/lib_base/net_id.mli index 9f0ae74c4..659841cd4 100644 --- a/src/lib_base/net_id.mli +++ b/src/lib_base/net_id.mli @@ -7,5 +7,6 @@ (* *) (**************************************************************************) -include Tezos_crypto.S.INTERNAL_HASH +include S.INTERNAL_HASH + val of_block_hash: Block_hash.t -> t diff --git a/src/lib_base/operation_hash.ml b/src/lib_base/operation_hash.ml index 9d42ab52c..e99507434 100644 --- a/src/lib_base/operation_hash.ml +++ b/src/lib_base/operation_hash.ml @@ -7,13 +7,13 @@ (* *) (**************************************************************************) -include Blake2B.Make (Base58) (struct +include Blake2B.Make (Tezos_crypto.Base58) (struct let name = "Operation_hash" let title = "A Tezos operation ID" - let b58check_prefix = Base58.Prefix.operation_hash + let b58check_prefix = Tezos_crypto.Base58.Prefix.operation_hash let size = None end) let () = - Base58.check_encoded_prefix b58check_encoding "o" 51 + Tezos_crypto.Base58.check_encoded_prefix b58check_encoding "o" 51 diff --git a/src/lib_base/operation_hash.mli b/src/lib_base/operation_hash.mli index 8cc7b165b..92f5c99bf 100644 --- a/src/lib_base/operation_hash.mli +++ b/src/lib_base/operation_hash.mli @@ -7,4 +7,4 @@ (* *) (**************************************************************************) -include Tezos_crypto.S.INTERNAL_HASH +include S.INTERNAL_HASH diff --git a/src/lib_base/operation_list_hash.ml b/src/lib_base/operation_list_hash.ml index bffdaa7b9..9aae4c1ec 100644 --- a/src/lib_base/operation_list_hash.ml +++ b/src/lib_base/operation_list_hash.ml @@ -7,12 +7,12 @@ (* *) (**************************************************************************) -include Blake2B.Make_merkle_tree (Base58) (struct +include Blake2B.Make_merkle_tree (Tezos_crypto.Base58) (struct let name = "Operation_list_hash" let title = "A list of operations" - let b58check_prefix = Base58.Prefix.operation_list_hash + let b58check_prefix = Tezos_crypto.Base58.Prefix.operation_list_hash let size = None end) (Operation_hash) let () = - Base58.check_encoded_prefix b58check_encoding "Lo" 52 + Tezos_crypto.Base58.check_encoded_prefix b58check_encoding "Lo" 52 diff --git a/src/lib_base/operation_list_hash.mli b/src/lib_base/operation_list_hash.mli index bad2d8c5a..78b9a364b 100644 --- a/src/lib_base/operation_list_hash.mli +++ b/src/lib_base/operation_list_hash.mli @@ -7,5 +7,5 @@ (* *) (**************************************************************************) -include Tezos_crypto.S.INTERNAL_MERKLE_TREE with type elt = Operation_hash.t +include S.INTERNAL_MERKLE_TREE with type elt = Operation_hash.t diff --git a/src/lib_base/operation_list_list_hash.ml b/src/lib_base/operation_list_list_hash.ml index c80d68e69..96b340657 100644 --- a/src/lib_base/operation_list_list_hash.ml +++ b/src/lib_base/operation_list_list_hash.ml @@ -7,12 +7,12 @@ (* *) (**************************************************************************) -include Blake2B.Make_merkle_tree (Base58) (struct +include Blake2B.Make_merkle_tree (Tezos_crypto.Base58) (struct let name = "Operation_list_list_hash" let title = "A list of list of operations" - let b58check_prefix = Base58.Prefix.operation_list_list_hash + let b58check_prefix = Tezos_crypto.Base58.Prefix.operation_list_list_hash let size = None end) (Operation_list_hash) let () = - Base58.check_encoded_prefix b58check_encoding "LLo" 53 ; + Tezos_crypto.Base58.check_encoded_prefix b58check_encoding "LLo" 53 ; diff --git a/src/lib_base/operation_list_list_hash.mli b/src/lib_base/operation_list_list_hash.mli index d69fb3e60..fecb4916b 100644 --- a/src/lib_base/operation_list_list_hash.mli +++ b/src/lib_base/operation_list_list_hash.mli @@ -7,4 +7,4 @@ (* *) (**************************************************************************) -include Tezos_crypto.S.INTERNAL_MERKLE_TREE with type elt = Operation_list_hash.t +include S.INTERNAL_MERKLE_TREE with type elt = Operation_list_hash.t diff --git a/src/lib_base/p2p_identity.mli b/src/lib_base/p2p_identity.mli index 68cc85c27..59246a39a 100644 --- a/src/lib_base/p2p_identity.mli +++ b/src/lib_base/p2p_identity.mli @@ -27,3 +27,4 @@ val generate_with_animation : Format.formatter -> Crypto_box.target -> t (** [generate_with_animation ppf target] is a freshly minted identity whose proof of work stamp difficulty is at least equal to [target]. *) + diff --git a/src/lib_base/p2p_peer_id.ml b/src/lib_base/p2p_peer_id.ml index 7dfb5e1df..6eabf2609 100644 --- a/src/lib_base/p2p_peer_id.ml +++ b/src/lib_base/p2p_peer_id.ml @@ -7,5 +7,5 @@ (* *) (**************************************************************************) -include Crypto_box.Public_key_hash +include Blake2B.Extend(Crypto_box.Public_key_hash) diff --git a/src/lib_base/p2p_peer_id.mli b/src/lib_base/p2p_peer_id.mli index 6465f0d4e..a7371c132 100644 --- a/src/lib_base/p2p_peer_id.mli +++ b/src/lib_base/p2p_peer_id.mli @@ -7,4 +7,4 @@ (* *) (**************************************************************************) -include Tezos_crypto.S.INTERNAL_HASH with type t = Crypto_box.Public_key_hash.t +include S.INTERNAL_HASH with type t = Crypto_box.Public_key_hash.t diff --git a/src/lib_base/protocol_environment.ml b/src/lib_base/protocol_environment.ml index 40721fc7b..f153f5fc2 100644 --- a/src/lib_base/protocol_environment.ml +++ b/src/lib_base/protocol_environment.ml @@ -96,7 +96,6 @@ module MakeV1 module Time = Time module Ed25519 = Ed25519 module S = struct - include Tezos_crypto.S include S end module Block_hash = Block_hash @@ -191,7 +190,7 @@ module MakeV1 end module Base58 = struct - include Base58 + include Tezos_crypto.Base58 let simple_encode enc s = simple_encode enc s let simple_decode enc s = simple_decode enc s include Make(struct type context = Context.t end) diff --git a/src/lib_base/protocol_hash.ml b/src/lib_base/protocol_hash.ml index b7c303309..88c865fb7 100644 --- a/src/lib_base/protocol_hash.ml +++ b/src/lib_base/protocol_hash.ml @@ -7,13 +7,13 @@ (* *) (**************************************************************************) -include Blake2B.Make (Base58) (struct +include Blake2B.Make (Tezos_crypto.Base58) (struct let name = "Protocol_hash" let title = "A Tezos protocol ID" - let b58check_prefix = Base58.Prefix.protocol_hash + let b58check_prefix = Tezos_crypto.Base58.Prefix.protocol_hash let size = None end) let () = - Base58.check_encoded_prefix b58check_encoding "P" 51 + Tezos_crypto.Base58.check_encoded_prefix b58check_encoding "P" 51 diff --git a/src/lib_base/protocol_hash.mli b/src/lib_base/protocol_hash.mli index 8cc7b165b..92f5c99bf 100644 --- a/src/lib_base/protocol_hash.mli +++ b/src/lib_base/protocol_hash.mli @@ -7,4 +7,4 @@ (* *) (**************************************************************************) -include Tezos_crypto.S.INTERNAL_HASH +include S.INTERNAL_HASH diff --git a/src/lib_base/s.ml b/src/lib_base/s.ml index 400e14a7f..aa0a01c39 100644 --- a/src/lib_base/s.ml +++ b/src/lib_base/s.ml @@ -7,6 +7,8 @@ (* *) (**************************************************************************) +open Error_monad + module type T = sig type t @@ -40,3 +42,122 @@ module type HASHABLE = sig val hash_raw: MBytes.t -> hash end + +module type MINIMAL_HASH = Tezos_crypto.S.MINIMAL_HASH + +module type HASH = sig + + include Tezos_crypto.S.MINIMAL_HASH + + val encoding: t Data_encoding.t + + val to_b58check: t -> string + val to_short_b58check: t -> string + type Tezos_crypto.Base58.data += Hash of t + val b58check_encoding: t Tezos_crypto.Base58.encoding + + val of_b58check_exn: string -> t + val of_b58check_opt: string -> t option + + val pp: Format.formatter -> t -> unit + val pp_short: Format.formatter -> t -> unit + + val rpc_arg: t RPC_arg.t + + module Set : sig + include Set.S with type elt = t + val encoding: t Data_encoding.t + end + + module Map : sig + include Map.S with type key = t + val encoding: 'a Data_encoding.t -> 'a t Data_encoding.t + end + +end + +module type INTERNAL_HASH = sig + + include Tezos_crypto.S.HASH + + val zero: t + + val of_b58check: string -> t tzresult + val of_bytes: MBytes.t -> t tzresult + + val encoding: t Data_encoding.t + val rpc_arg: t RPC_arg.t + + val param: + ?name:string -> + ?desc:string -> + ('a, 'arg, 'ret) Cli_entries.params -> + (t -> 'a, 'arg, 'ret) Cli_entries.params + + module Set : sig + include Set.S with type elt = t + val random_elt: t -> elt + val encoding: t Data_encoding.t + end + + module Map : sig + include Map.S with type key = t + val encoding: 'a Data_encoding.t -> 'a t Data_encoding.t + end + + module Table : sig + include Hashtbl.S with type key = t + val encoding: 'a Data_encoding.t -> 'a t Data_encoding.t + end + +end + +module type MERKLE_TREE = sig + type elt + include HASH + val compute: elt list -> t + val empty: t + type path = + | Left of path * t + | Right of t * path + | Op + val compute_path: elt list -> int -> path + val check_path: path -> elt -> t * int + val path_encoding: path Data_encoding.t +end + +module type INTERNAL_MERKLE_TREE = sig + + include Tezos_crypto.S.MERKLE_TREE + + val path_encoding: path Data_encoding.t + + val of_b58check: string -> t tzresult + val of_bytes: MBytes.t -> t tzresult + + val encoding: t Data_encoding.t + val rpc_arg: t RPC_arg.t + + val param: + ?name:string -> + ?desc:string -> + ('a, 'arg, 'ret) Cli_entries.params -> + (t -> 'a, 'arg, 'ret) Cli_entries.params + + module Set : sig + include Set.S with type elt = t + val random_elt: t -> elt + val encoding: t Data_encoding.t + end + + module Map : sig + include Map.S with type key = t + val encoding: 'a Data_encoding.t -> 'a t Data_encoding.t + end + + module Table : sig + include Hashtbl.S with type key = t + val encoding: 'a Data_encoding.t -> 'a t Data_encoding.t + end + +end diff --git a/src/lib_base/tzPervasives.ml b/src/lib_base/tzPervasives.ml index 1a6e68220..018400ea1 100644 --- a/src/lib_base/tzPervasives.ml +++ b/src/lib_base/tzPervasives.ml @@ -12,9 +12,13 @@ include Tezos_stdlib_lwt include Tezos_data_encoding include Tezos_error_monad include Tezos_rpc -include Tezos_crypto include Tezos_micheline +module Ed25519 = Ed25519 +module Crypto_box = Crypto_box +module Base58 = Tezos_crypto.Base58 +module Rand = Tezos_crypto.Rand + module List = struct include List include Tezos_stdlib.TzList diff --git a/src/lib_base/tzPervasives.mli b/src/lib_base/tzPervasives.mli index a8cd94c0e..efaf6d0f2 100644 --- a/src/lib_base/tzPervasives.mli +++ b/src/lib_base/tzPervasives.mli @@ -9,10 +9,13 @@ include (module type of (struct include Tezos_stdlib end)) include (module type of (struct include Tezos_data_encoding end)) -include (module type of (struct include Tezos_stdlib_lwt end)) include (module type of (struct include Tezos_error_monad end)) include (module type of (struct include Tezos_rpc end)) -include (module type of (struct include Tezos_crypto end)) + +module Ed25519 = Ed25519 +module Crypto_box = Crypto_box +module Base58 = Tezos_crypto.Base58 +module Rand = Tezos_crypto.Rand module List : sig include (module type of (struct include List end)) diff --git a/src/lib_crypto/blake2B.ml b/src/lib_crypto/blake2B.ml index a79943708..ebdcee2de 100644 --- a/src/lib_crypto/blake2B.ml +++ b/src/lib_crypto/blake2B.ml @@ -55,13 +55,13 @@ module Make_minimal (K : Name) = struct let compare (Blake2b.Hash h1) (Blake2b.Hash h2) = Cstruct.compare h1 h2 let equal x y = compare x y = 0 - let of_bytes b = + let of_bytes_opt b = if MBytes.length b <> size then None else Some (Blake2b.Hash (Cstruct.of_bigarray b)) let of_bytes_exn b = - match of_bytes b with + match of_bytes_opt b with | None -> let msg = Printf.sprintf "%s.of_bytes: wrong string size (%d)" @@ -107,14 +107,6 @@ module Make_minimal (K : Name) = struct and p6 = if len > 10 then String.sub p 10 (len - 10) else "" in [ p1 ; p2 ; p3 ; p4 ; p5 ; p6 ] - module Table = struct - include Hashtbl.Make(struct - type nonrec t = t - let hash (Blake2b.Hash h) = Int64.to_int (Cstruct.BE.get_uint64 h 0) - let equal = equal - end) - end - end module Make (R : sig @@ -129,6 +121,11 @@ module Make (R : sig include Make_minimal(K) + let zero = + match of_hex (String.make (size * 2) '0') with + | Some c -> c + | None -> assert false + (* Serializers *) type Base58.data += Hash of t @@ -146,89 +143,22 @@ module Make (R : sig match Base58.simple_decode b58check_encoding s with | Some x -> x | None -> Format.kasprintf Pervasives.failwith "Unexpected hash (%s)" K.name - let of_b58check s = - match Base58.simple_decode b58check_encoding s with - | Some x -> Ok x - | None -> generic_error "Unexpected hash (%s)" K.name let to_b58check s = Base58.simple_encode b58check_encoding s let to_short_b58check s = String.sub (to_b58check s) 0 (10 + 2 * String.length K.b58check_prefix) - let rpc_arg = - RPC_arg.make - ~name:(Format.asprintf "hash.%s" K.name) - ~descr:(Format.asprintf "A b58check-encoded hash (%s)" K.name) - ~destruct: - (fun s -> - match of_b58check_opt s with - | None -> - Error (Format.asprintf - "failed to decode b58check-encoded hash (%s): %S" - K.name s) - | Some v -> Ok v) - ~construct:to_b58check - () - - let encoding = - let open Data_encoding in - splitted - ~binary: - (conv to_bytes of_bytes_exn (Fixed.bytes size)) - ~json: - (describe ~title: (K.title ^ " (Base58Check-encoded Blake2B hash)") @@ - conv to_b58check (Data_encoding.Json.wrap_error of_b58check_exn) string) - - let param ?(name=K.name) ?(desc=K.title) t = - Cli_entries.param - ~name - ~desc (Cli_entries.parameter (fun _ str -> Lwt.return (of_b58check str))) t - let pp ppf t = Format.pp_print_string ppf (to_b58check t) let pp_short ppf t = Format.pp_print_string ppf (to_short_b58check t) - module Set = struct - include Set.Make(struct type nonrec t = t let compare = compare end) - exception Found of elt - let random_elt s = - let n = Random.int (cardinal s) in - try - ignore - (fold (fun x i -> if i = n then raise (Found x) ; i+1) s 0 : int) ; - assert false - with Found x -> x - let encoding = - Data_encoding.conv - elements - (fun l -> List.fold_left (fun m x -> add x m) empty l) - Data_encoding.(list encoding) - end - - let random_set_elt = Set.random_elt - - module Map = struct - include Map.Make(struct type nonrec t = t let compare = compare end) - let encoding arg_encoding = - Data_encoding.conv - bindings - (fun l -> List.fold_left (fun m (k,v) -> add k v m) empty l) - Data_encoding.(list (tup2 encoding arg_encoding)) - end - - let zero = - match of_hex (String.make (size * 2) '0') with - | Some c -> c - | None -> assert false - end module Generic_Merkle_tree (H : sig type t type elt - val encoding : t Data_encoding.t val empty : t val leaf : elt -> t val node : t -> t -> t @@ -310,29 +240,6 @@ module Generic_Merkle_tree (H : sig let h, _, pos = check_path p h in h, pos - let path_encoding = - let open Data_encoding in - mu "path" - (fun path_encoding -> - union [ - case (Tag 240) - (obj2 - (req "path" path_encoding) - (req "right" H.encoding)) - (function Left (p, r) -> Some (p, r) | _ -> None) - (fun (p, r) -> Left (p, r)) ; - case (Tag 15) - (obj2 - (req "left" H.encoding) - (req "path" path_encoding)) - (function Right (r, p) -> Some (r, p) | _ -> None) - (fun (r, p) -> Right (r, p)) ; - case (Tag 0) - unit - (function Op -> Some () | _ -> None) - (fun () -> Op) - ]) - end module Make_merkle_tree @@ -354,13 +261,13 @@ module Make_merkle_tree include Make (R) (K) type elt = Contents.t + let elt_bytes = Contents.to_bytes let empty = hash_bytes [] include Generic_Merkle_tree(struct type nonrec t = t type nonrec elt = elt - let encoding = encoding let empty = empty let leaf x = hash_bytes [Contents.to_bytes x] let node x y = hash_bytes [to_bytes x; to_bytes y] diff --git a/src/lib_crypto/blake2B.mli b/src/lib_crypto/blake2B.mli index cc31c3e40..8dfd69f40 100644 --- a/src/lib_crypto/blake2B.mli +++ b/src/lib_crypto/blake2B.mli @@ -11,7 +11,7 @@ (** {2 Predefined Hashes } ****************************************************) -include S.INTERNAL_MINIMAL_HASH +include S.MINIMAL_HASH (** {2 Building Hashes} *******************************************************) @@ -31,7 +31,7 @@ module type PrefixedName = sig end (** Builds a new Hash type using Blake2B. *) -module Make_minimal (Name : Name) : S.INTERNAL_MINIMAL_HASH +module Make_minimal (Name : Name) : S.MINIMAL_HASH module Make (Register : sig val register_encoding: @@ -42,7 +42,7 @@ module Make wrap: ('a -> Base58.data) -> 'a Base58.encoding end) - (Name : PrefixedName) : S.INTERNAL_HASH + (Name : PrefixedName) : S.HASH (**/**) @@ -60,24 +60,11 @@ module Make_merkle_tree (Contents: sig type t val to_bytes: t -> MBytes.t - end) : sig - include S.INTERNAL_HASH - type elt = Contents.t - val empty: t - val compute: elt list -> t - type path = - | Left of path * t - | Right of t * path - | Op - val path_encoding: path Data_encoding.t - val compute_path: elt list -> int -> path - val check_path: path -> elt -> t * int -end + end) : S.MERKLE_TREE with type elt = Contents.t module Generic_Merkle_tree (H : sig type t type elt - val encoding : t Data_encoding.t val empty : t val leaf : elt -> t val node : t -> t -> t diff --git a/src/lib_crypto/crypto_box.ml b/src/lib_crypto/crypto_box.ml index d1ddd2257..b2f303b65 100644 --- a/src/lib_crypto/crypto_box.ml +++ b/src/lib_crypto/crypto_box.ml @@ -118,28 +118,14 @@ let generate_proof_of_work ?max pk target = loop (Nonce.increment nonce) (cpt + 1) in loop (random_nonce ()) 0 -let to_bigarray : type a. a Box.key -> MBytes.t = fun k -> - Cstruct.to_bigarray (Box.to_cstruct k) +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 of_bigarray f s = f (Cstruct.of_bigarray s) +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 public_key_encoding = - let open Data_encoding in - conv - to_bigarray - (of_bigarray Box.pk_of_cstruct_exn) - (Fixed.bytes Box.pkbytes) - -let secret_key_encoding = - let open Data_encoding in - conv - to_bigarray - (of_bigarray Box.sk_of_cstruct_exn) - (Fixed.bytes Box.skbytes) - -let nonce_encoding = - let open Data_encoding in - conv - (fun nonce -> Cstruct.to_bigarray (Nonce.to_cstruct nonce)) - (of_bigarray Nonce.of_cstruct_exn) - (Fixed.bytes Nonce.bytes) +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 diff --git a/src/lib_crypto/crypto_box.mli b/src/lib_crypto/crypto_box.mli index dd8a24944..78e8c0f43 100644 --- a/src/lib_crypto/crypto_box.mli +++ b/src/lib_crypto/crypto_box.mli @@ -13,7 +13,6 @@ type nonce val random_nonce : unit -> nonce val increment_nonce : ?step:int -> nonce -> nonce -val nonce_encoding : nonce Data_encoding.t type target val default_target : target @@ -21,12 +20,9 @@ val make_target : float -> target type secret_key type public_key -module Public_key_hash : S.INTERNAL_HASH +module Public_key_hash : S.HASH type channel_key -val public_key_encoding : public_key Data_encoding.t -val secret_key_encoding : secret_key Data_encoding.t - val hash : public_key -> Public_key_hash.t val zerobytes : int @@ -51,4 +47,14 @@ val fast_box_open_noalloc : channel_key -> nonce -> MBytes.t -> bool val check_proof_of_work : public_key -> nonce -> target -> bool val generate_proof_of_work : ?max:int -> public_key -> target -> nonce +val public_key_to_bigarray : public_key -> Cstruct.buffer +val public_key_of_bigarray : Cstruct.buffer -> public_key +val public_key_size : int +val secret_key_to_bigarray : secret_key -> Cstruct.buffer +val secret_key_of_bigarray : Cstruct.buffer -> secret_key +val secret_key_size : int + +val nonce_to_bigarray : nonce -> Cstruct.buffer +val nonce_of_bigarray : Cstruct.buffer -> nonce +val nonce_size : int diff --git a/src/lib_crypto/ed25519.ml b/src/lib_crypto/ed25519.ml index b16f9926a..2b64acfd4 100644 --- a/src/lib_crypto/ed25519.ml +++ b/src/lib_crypto/ed25519.ml @@ -19,12 +19,6 @@ let () = open Tweetnacl -let of_bigarray1 f x = - f (Cstruct.of_bigarray x) - -let to_bigarray1 f x = - Cstruct.to_bigarray (f x) - module Public_key = struct type t = Sign.public Sign.key @@ -58,10 +52,6 @@ module Public_key = struct | Some x -> x | None -> Pervasives.failwith (Printf.sprintf "%s is not an ed25519 public key" s) - let of_b58check s = - match Base58.simple_decode b58check_encoding s with - | Some x -> Ok x - | None -> generic_error "%s is not an ed25519 public key" s let to_b58check s = Base58.simple_encode b58check_encoding s let pp ppf t = Format.fprintf ppf "%s" (to_b58check t) @@ -75,49 +65,21 @@ module Public_key = struct let of_bytes_opt s = Sign.pk_of_cstruct (Cstruct.of_bigarray s) - let of_bytes s = - match of_bytes_opt s with - | None -> - generic_error "Could not deserialize Ed25519 public key (invalid format)" - | Some pk -> ok pk - let of_bytes_exn s = match of_bytes_opt s with | None -> Pervasives.invalid_arg "Ed25519.Public_key.of_bytes_exn: argument is not a serialized public key" | Some pk -> pk + let size = Sign.pkbytes let to_bytes pk = Cstruct.to_bigarray (Sign.to_cstruct pk) - let param ?(name="ed25519-public") ?(desc="Ed25519 public key (b58check-encoded)") t = - Cli_entries.(param ~name ~desc (parameter (fun _ str -> Lwt.return (of_b58check str))) t) - let () = Base58.check_encoded_prefix b58check_encoding "edpk" 54 - let encoding = - let open Data_encoding in - splitted - ~json: - (describe - ~title: "An Ed25519 public key (Base58Check encoded)" @@ - conv - (fun s -> Base58.simple_encode b58check_encoding s) - (fun s -> - match Base58.simple_decode b58check_encoding s with - | Some x -> x - | None -> Data_encoding.Json.cannot_destruct - "Ed25519 public key: unexpected prefix.") - string) - ~binary: - (conv - (to_bigarray1 Sign.to_cstruct) - (of_bigarray1 Sign.pk_of_cstruct_exn) - (Fixed.bytes Sign.pkbytes)) - let hash v = Public_key_hash.hash_bytes - [ to_bigarray1 Sign.to_cstruct v ] + [ Cstruct.to_bigarray (Sign.to_cstruct v) ] end @@ -160,10 +122,6 @@ module Secret_key = struct | Some x -> x | None -> Pervasives.failwith (Printf.sprintf "%s is not an ed25519 secret key" s) - let of_b58check s = - match of_b58check_opt s with - | Some x -> Ok x - | None -> generic_error "%s is not an ed25519 secret key" s let to_b58check s = Base58.simple_encode seed_encoding s let pp ppf t = Format.fprintf ppf "%s" (to_b58check t) @@ -174,45 +132,19 @@ module Secret_key = struct | 64 -> Sign.sk_of_cstruct s | _ -> None - let of_bytes s = - match of_bytes_opt s with - | None -> - generic_error "Could not deserialize Ed25519 seed (invalid format)" - | Some sk -> ok sk - let of_bytes_exn s = match of_bytes_opt s with | None -> Pervasives.invalid_arg "Ed25519.Secret_key.of_bytes_exn: argument is not a serialized seed" | Some sk -> sk - let to_bytes = to_bigarray1 Sign.seed - - let param ?(name="ed25519-secret") ?(desc="Ed25519 secret key (b58check-encoded)") t = - Cli_entries.(param ~name ~desc (parameter (fun _ str -> Lwt.return (of_b58check str))) t) + let to_bytes x = Cstruct.to_bigarray (Sign.seed x) + let size = Sign.seedbytes let () = Base58.check_encoded_prefix seed_encoding "edsk" 54 ; Base58.check_encoded_prefix secret_key_encoding "edsk" 98 - let encoding = - let open Data_encoding in - splitted - ~json: - (describe - ~title: "An Ed25519 secret key (Base58Check encoded)" @@ - conv - (fun s -> Base58.simple_encode seed_encoding s) - (fun s -> - match of_b58check_opt s with - | Some x -> x - | None -> Data_encoding.Json.cannot_destruct - "Ed25519 secret key: unexpected prefix.") - string) - ~binary: - (conv to_bytes (fun buf -> of_bytes_exn buf) - (dynamic_size (Variable.bytes))) - end let sign key msg = @@ -239,22 +171,12 @@ module Signature = struct | Some x -> x | None -> Pervasives.failwith (Printf.sprintf "%s is not an ed25519 signature" s) - let of_b58check s = - match Base58.simple_decode b58check_encoding s with - | Some x -> Ok x - | None -> generic_error "%s is not an ed25519 signature" s let to_b58check s = Base58.simple_encode b58check_encoding s let pp ppf t = Format.fprintf ppf "%s" (to_b58check t) let of_bytes_opt s = if MBytes.length s = Sign.bytes then Some s else None - let of_bytes s = - match of_bytes_opt s with - | None -> - generic_error "Could not deserialize Ed25519 signature (invalid format)" - | Some signature -> ok signature - let of_bytes_exn s = match of_bytes_opt s with | None -> @@ -262,29 +184,11 @@ module Signature = struct | Some signature -> signature let to_bytes x = x - - let param ?(name="signature") ?(desc="Signature (b58check-encoded)") t = - Cli_entries.(param ~name ~desc (parameter (fun _ str -> Lwt.return (of_b58check str))) t) + let size = Sign.bytes let () = Base58.check_encoded_prefix b58check_encoding "edsig" 99 - let encoding = - let open Data_encoding in - splitted - ~json: - (describe - ~title: "An Ed25519 signature (Base58Check encoded)" @@ - conv - (fun s -> Base58.simple_encode b58check_encoding s) - (fun s -> - match Base58.simple_decode b58check_encoding s with - | Some x -> x - | None -> Data_encoding.Json.cannot_destruct - "Ed25519 signature: unexpected prefix.") - string) - ~binary: (Fixed.bytes Sign.bytes) - let check public_key signature msg = Sign.verify_detached ~key:public_key ~signature:(Cstruct.of_bigarray signature) diff --git a/src/lib_crypto/ed25519.mli b/src/lib_crypto/ed25519.mli index c935e71ca..7c0689271 100644 --- a/src/lib_crypto/ed25519.mli +++ b/src/lib_crypto/ed25519.mli @@ -11,28 +11,20 @@ (** {2 Hashed public keys for user ID} ***************************************) -module Public_key_hash : S.INTERNAL_HASH +module Public_key_hash : S.HASH (** {2 Signature} ************************************************************) module Public_key : sig include Compare.S - val encoding: t Data_encoding.t val pp : Format.formatter -> t -> unit - val param: - ?name:string -> - ?desc:string -> - ('a, 'b, 'c) Cli_entries.params -> - (t -> 'a, 'b, 'c) Cli_entries.params - val hash: t -> Public_key_hash.t type Base58.data += | Public_key of t - val of_b58check: string -> t tzresult val of_b58check_exn: string -> t val of_b58check_opt: string -> t option val to_b58check: t -> string @@ -41,67 +33,54 @@ module Public_key : sig val of_hex: Hex.t -> t option val of_hex_exn: Hex.t -> t - val of_bytes: MBytes.t -> t tzresult val of_bytes_exn: MBytes.t -> t val of_bytes_opt: MBytes.t -> t option val to_bytes: t -> MBytes.t + val size: int + end module Secret_key : sig type t - val encoding: t Data_encoding.t val pp : Format.formatter -> t -> unit - val param: - ?name:string -> - ?desc:string -> - ('a, 'b, 'c) Cli_entries.params -> - (t -> 'a, 'b, 'c) Cli_entries.params - val to_public_key: t -> Public_key.t type Base58.data += | Secret_key of t - val of_b58check: string -> t tzresult val of_b58check_exn: string -> t val of_b58check_opt: string -> t option val to_b58check: t -> string - val of_bytes: MBytes.t -> t tzresult val of_bytes_exn: MBytes.t -> t val of_bytes_opt: MBytes.t -> t option val to_bytes: t -> MBytes.t + val size: int + end module Signature : sig type t - val encoding: t Data_encoding.t val pp : Format.formatter -> t -> unit - val param: - ?name:string -> - ?desc:string -> - ('a, 'b, 'c) Cli_entries.params -> - (t -> 'a, 'b, 'c) Cli_entries.params - type Base58.data += | Signature of t - val of_b58check: string -> t tzresult val of_b58check_exn: string -> t val of_b58check_opt: string -> t option val to_b58check: t -> string - val of_bytes: MBytes.t -> t tzresult val of_bytes_exn: MBytes.t -> t val of_bytes_opt: MBytes.t -> t option val to_bytes: t -> MBytes.t + val size: int + (** Check a signature *) val check: Public_key.t -> t -> MBytes.t -> bool diff --git a/src/lib_crypto/jbuild b/src/lib_crypto/jbuild index 0acec9ff6..d29306340 100644 --- a/src/lib_crypto/jbuild +++ b/src/lib_crypto/jbuild @@ -3,17 +3,10 @@ (library ((name tezos_crypto) (public_name tezos-crypto) - (flags (:standard -open Tezos_stdlib - -open Tezos_data_encoding - -open Tezos_stdlib_lwt - -open Tezos_rpc - -open Tezos_error_monad__Error_monad - -safe-string)) + (flags (:standard -safe-string + -open Tezos_stdlib)) (libraries (tezos-stdlib - tezos-stdlib-lwt - tezos-data-encoding - tezos-error-monad - tezos-rpc + lwt nocrypto blake2 tweetnacl diff --git a/src/lib_crypto/rand.mli b/src/lib_crypto/rand.mli index fe0a7bd1a..c37c3a2c5 100644 --- a/src/lib_crypto/rand.mli +++ b/src/lib_crypto/rand.mli @@ -7,10 +7,10 @@ (* *) (**************************************************************************) -val generate : int -> MBytes.t +val generate : int -> Cstruct.buffer (** [generate len] is [len] random bytes. *) -val generate_into : ?pos:int -> ?len:int -> MBytes.t -> unit +val generate_into : ?pos:int -> ?len:int -> Cstruct.buffer -> unit (** [generate_into ?pos ?len buf] writes [len] (default: [MBytes.length buf]) bytes in [buf] starting at [pos] (default: [0]). *) diff --git a/src/lib_crypto/s.ml b/src/lib_crypto/s.ml index 63fa45c5a..35310ceae 100644 --- a/src/lib_crypto/s.ml +++ b/src/lib_crypto/s.ml @@ -23,7 +23,7 @@ module type MINIMAL_HASH = sig val name: string val title: string - val hash_bytes: MBytes.t list -> t + val hash_bytes: Cstruct.buffer list -> t val hash_string: string list -> t val size: int (* in bytes *) val compare: t -> t -> int @@ -37,12 +37,12 @@ module type MINIMAL_HASH = sig val of_string: string -> t option val of_string_exn: string -> t - val to_bytes: t -> MBytes.t - val of_bytes: MBytes.t -> t option - val of_bytes_exn: MBytes.t -> t + val to_bytes: t -> Cstruct.buffer + val of_bytes_opt: Cstruct.buffer -> t option + val of_bytes_exn: Cstruct.buffer -> t - val read: MBytes.t -> int -> t - val write: MBytes.t -> int -> t -> unit + val read: Cstruct.buffer -> int -> t + val write: Cstruct.buffer -> int -> t -> unit val to_path: t -> string list -> string list val of_path: string list -> t option @@ -53,76 +53,42 @@ module type MINIMAL_HASH = sig end -module type INTERNAL_MINIMAL_HASH = sig - include MINIMAL_HASH - module Table : Hashtbl.S with type key = t -end - module type HASH = sig include MINIMAL_HASH - val of_b58check_exn: string -> t - val of_b58check_opt: string -> t option + val zero: t + val to_b58check: t -> string val to_short_b58check: t -> string - val encoding: t Data_encoding.t - val pp: Format.formatter -> t -> unit - val pp_short: Format.formatter -> t -> unit + + val of_b58check_exn: string -> t + val of_b58check_opt: string -> t option + type Base58.data += Hash of t val b58check_encoding: t Base58.encoding - val rpc_arg: t RPC_arg.t + val pp: Format.formatter -> t -> unit + val pp_short: Format.formatter -> t -> unit - module Set : sig - include Set.S with type elt = t - val encoding: t Data_encoding.t - end - - module Map : sig - include Map.S with type key = t - val encoding: 'a Data_encoding.t -> 'a t Data_encoding.t - end - -end - -module type INTERNAL_HASH = sig - include HASH - val of_b58check: string -> t tzresult - val param: - ?name:string -> - ?desc:string -> - ('a, 'arg, 'ret) Cli_entries.params -> - (t -> 'a, 'arg, 'ret) Cli_entries.params - val random_set_elt: Set.t -> t - module Table : Hashtbl.S with type key = t - val zero: t -end - -module type INTERNAL_MERKLE_TREE = sig - type elt - include INTERNAL_HASH - val compute: elt list -> t - val empty: t - type path = - | Left of path * t - | Right of t * path - | Op - val compute_path: elt list -> int -> path - val check_path: path -> elt -> t * int - val path_encoding: path Data_encoding.t end module type MERKLE_TREE = sig + type elt + val elt_bytes: elt -> Cstruct.buffer + include HASH + val compute: elt list -> t val empty: t + type path = | Left of path * t | Right of t * path | Op + val compute_path: elt list -> int -> path val check_path: path -> elt -> t * int - val path_encoding: path Data_encoding.t + end diff --git a/src/lib_crypto/test/jbuild b/src/lib_crypto/test/jbuild index de0c4f8d1..a15c3b065 100644 --- a/src/lib_crypto/test/jbuild +++ b/src/lib_crypto/test/jbuild @@ -4,7 +4,6 @@ ((names (test_merkle)) (libraries (tezos-stdlib tezos-error-monad - tezos-data-encoding tezos-crypto tezos-test-helpers)) (flags (:standard -w -9-32 @@ -12,7 +11,6 @@ -open Tezos_test_helpers -open Tezos_stdlib -open Tezos_error_monad - -open Tezos_data_encoding -open Tezos_crypto)))) (alias diff --git a/src/lib_crypto/test/test_merkle.ml b/src/lib_crypto/test/test_merkle.ml index 4a0fd3f3b..245b88786 100644 --- a/src/lib_crypto/test/test_merkle.ml +++ b/src/lib_crypto/test/test_merkle.ml @@ -30,9 +30,6 @@ module Merkle = Blake2B.Generic_Merkle_tree(struct let empty = Empty let leaf i = Leaf i let node x y = Node (x, y) - let encoding = - (* Fake... *) - Data_encoding.conv (fun _ -> 0) (fun _ -> Empty) Data_encoding.int31 end) let rec compare_list xs ys = diff --git a/src/lib_crypto/tezos-crypto.opam b/src/lib_crypto/tezos-crypto.opam index 2e43eed30..ad8f7651f 100644 --- a/src/lib_crypto/tezos-crypto.opam +++ b/src/lib_crypto/tezos-crypto.opam @@ -11,14 +11,12 @@ depends: [ "jbuilder" { build & >= "1.0+beta17" } "tezos-test-helpers" { test } "tezos-stdlib" - "tezos-stdlib-lwt" - "tezos-data-encoding" - "tezos-error-monad" - "tezos-rpc" + "lwt" "nocrypto" "blake2" "tweetnacl" "zarith" + "tezos-error-monad" { test } ] build: [ [ "jbuilder" "build" "-p" name "-j" jobs ] diff --git a/src/lib_protocol_compiler/jbuild_protocol_template b/src/lib_protocol_compiler/jbuild_protocol_template index bc7d87cd8..1f603b93e 100644 --- a/src/lib_protocol_compiler/jbuild_protocol_template +++ b/src/lib_protocol_compiler/jbuild_protocol_template @@ -56,8 +56,7 @@ let () = Format.kasprintf Jbuild_plugin.V1.send {| (rule ((targets (tezos_protocol_%s.cmo tezos_protocol_%s_dummy_byte.ml)) - (deps ((glob_files src/*.ml) - (glob_files src/*.mli) + (deps ((glob_files src/*.ml*) src/TEZOS_PROTOCOL)) (action (with-stdout-to ${path-no-dep:tezos_protocol_%s_dummy_byte.ml} (chdir ${ROOT} diff --git a/src/lib_protocol_environment_sigs/v1/s.mli b/src/lib_protocol_environment_sigs/v1/s.mli index 72383e45c..6d8c5d40d 100644 --- a/src/lib_protocol_environment_sigs/v1/s.mli +++ b/src/lib_protocol_environment_sigs/v1/s.mli @@ -75,7 +75,7 @@ module type MINIMAL_HASH = sig val of_string_exn: string -> t val to_bytes: t -> MBytes.t - val of_bytes: MBytes.t -> t option + val of_bytes_opt: MBytes.t -> t option val of_bytes_exn: MBytes.t -> t val read: MBytes.t -> int -> t @@ -94,16 +94,19 @@ module type HASH = sig include MINIMAL_HASH - val of_b58check_exn: string -> t - val of_b58check_opt: string -> t option + val encoding: t Data_encoding.t + val to_b58check: t -> string val to_short_b58check: t -> string - val encoding: t Data_encoding.t - val pp: Format.formatter -> t -> unit - val pp_short: Format.formatter -> t -> unit type Base58.data += Hash of t val b58check_encoding: t Base58.encoding + val of_b58check_exn: string -> t + val of_b58check_opt: string -> t option + + val pp: Format.formatter -> t -> unit + val pp_short: Format.formatter -> t -> unit + val rpc_arg: t RPC_arg.t module Set : sig diff --git a/src/lib_shell/distributed_db_functors.ml b/src/lib_shell/distributed_db_functors.ml index bd58bb27c..b75230fbb 100644 --- a/src/lib_shell/distributed_db_functors.ml +++ b/src/lib_shell/distributed_db_functors.ml @@ -493,7 +493,7 @@ end = struct ( Table.remove state.pending key ; acc ) else let requested_peer = - P2p_peer.Id.random_set_elt + P2p_peer.Id.Set.random_elt (if P2p_peer.Set.is_empty remaining_peers then active_peers else remaining_peers) in diff --git a/src/lib_storage/context.ml b/src/lib_storage/context.ml index b229eee9f..6242e3a61 100644 --- a/src/lib_storage/context.ml +++ b/src/lib_storage/context.ml @@ -37,7 +37,7 @@ module IrminBlake2B : Irmin.Hash.S with type t = Context_hash.t = struct let to_raw t = Cstruct.of_bigarray (Context_hash.to_bytes t) let of_raw t = - match Context_hash.of_bytes (Cstruct.to_bigarray t) with + match Context_hash.of_bytes_opt (Cstruct.to_bigarray t) with | Some t -> t | None -> let str = Cstruct.to_string t in