From 7807f7aa4d01b246491a972f7dddad9580d65a58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Tue, 5 Dec 2017 15:17:54 +0100 Subject: [PATCH] Context: switch to blake2B Get rid of the old SHA1 that was used by git... --- lib_crypto/base58.ml | 1 + lib_crypto/base58.mli | 1 + lib_crypto/blake2B.ml | 5 +++ lib_crypto/context_hash.ml | 18 +++++++++++ lib_crypto/context_hash.mli | 10 ++++++ lib_crypto/net_id.ml | 5 +++ lib_crypto/s.ml | 1 + lib_storage/context.ml | 62 ++++++++++++++++++++++--------------- lib_storage/context.mli | 21 +++++-------- lib_storage/store.ml | 4 +-- lib_storage/store.mli | 2 +- test/shell/test_context.ml | 8 ++--- test/shell/test_store.ml | 2 +- 13 files changed, 94 insertions(+), 46 deletions(-) create mode 100644 lib_crypto/context_hash.ml create mode 100644 lib_crypto/context_hash.mli diff --git a/lib_crypto/base58.ml b/lib_crypto/base58.ml index b52420d3d..c93712900 100644 --- a/lib_crypto/base58.ml +++ b/lib_crypto/base58.ml @@ -301,6 +301,7 @@ module Prefix = struct let operation_list_hash = "\133\233" (* Lo(52) *) let operation_list_list_hash = "\029\159\109" (* LLo(53) *) let protocol_hash = "\002\170" (* P(51) *) + let context_hash = "\079\199" (* Co(52) *) (* 20 *) let ed25519_public_key_hash = "\006\161\159" (* tz1(36) *) diff --git a/lib_crypto/base58.mli b/lib_crypto/base58.mli index e5ed76d87..c5ec777f9 100644 --- a/lib_crypto/base58.mli +++ b/lib_crypto/base58.mli @@ -16,6 +16,7 @@ module Prefix : sig val operation_list_hash: string val operation_list_list_hash: string val protocol_hash: string + val context_hash: string val ed25519_public_key_hash: string val cryptobox_public_key_hash: string val ed25519_public_key: string diff --git a/lib_crypto/blake2B.ml b/lib_crypto/blake2B.ml index aa311f578..c96c5d217 100644 --- a/lib_crypto/blake2B.ml +++ b/lib_crypto/blake2B.ml @@ -209,6 +209,11 @@ module Make (R : sig 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 diff --git a/lib_crypto/context_hash.ml b/lib_crypto/context_hash.ml new file mode 100644 index 000000000..317673447 --- /dev/null +++ b/lib_crypto/context_hash.ml @@ -0,0 +1,18 @@ +(**************************************************************************) +(* *) +(* Copyright (c) 2014 - 2017. *) +(* Dynamic Ledger Solutions, Inc. *) +(* *) +(* All rights reserved. No warranty, explicit or implicit, provided. *) +(* *) +(**************************************************************************) + +include Blake2B.Make (Base58) (struct + let name = "Context_hash" + let title = "A hash of context" + let b58check_prefix = Base58.Prefix.context_hash + let size = None + end) + +let () = + Base58.check_encoded_prefix b58check_encoding "Co" 52 diff --git a/lib_crypto/context_hash.mli b/lib_crypto/context_hash.mli new file mode 100644 index 000000000..c38290500 --- /dev/null +++ b/lib_crypto/context_hash.mli @@ -0,0 +1,10 @@ +(**************************************************************************) +(* *) +(* Copyright (c) 2014 - 2017. *) +(* Dynamic Ledger Solutions, Inc. *) +(* *) +(* All rights reserved. No warranty, explicit or implicit, provided. *) +(* *) +(**************************************************************************) + +include S.INTERNAL_HASH diff --git a/lib_crypto/net_id.ml b/lib_crypto/net_id.ml index 787e0cc3d..e3fe29aca 100644 --- a/lib_crypto/net_id.ml +++ b/lib_crypto/net_id.ml @@ -148,3 +148,8 @@ 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 diff --git a/lib_crypto/s.ml b/lib_crypto/s.ml index b44408115..2bc08ff01 100644 --- a/lib_crypto/s.ml +++ b/lib_crypto/s.ml @@ -94,6 +94,7 @@ module type INTERNAL_HASH = sig (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 diff --git a/lib_storage/context.ml b/lib_storage/context.ml index e5fb62fcb..087246dc7 100644 --- a/lib_storage/context.ml +++ b/lib_storage/context.ml @@ -29,13 +29,49 @@ module Metadata = struct let merge = Irmin.Merge.default t end +module IrminBlake2B : Irmin.Hash.S with type t = Context_hash.t = struct + + type t = Context_hash.t + + let digest_size = Context_hash.size + + 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 + | Some t -> t + | None -> + let str = Cstruct.to_string t in + Format.kasprintf invalid_arg "%s (%d)" str (String.length str) + + let t = Irmin.Type.like Irmin.Type.cstruct of_raw to_raw + + let digest t x = + Context_hash.hash_bytes + [Cstruct.to_bigarray (Irmin.Type.encode_cstruct t x)] + + let pp = Context_hash.pp + + let of_string x = + match Context_hash.of_b58check_exn x with + | exception (Invalid_argument s) -> Error (`Msg s) + | h -> Ok h + + let has_kind = function + | `SHA1 -> true + | _ -> false + + let to_raw_int c = + Int64.to_int @@ MBytes.get_int64 (Context_hash.to_bytes c) 0 + +end + module GitStore = Irmin_leveldb.Make (Metadata) (MBytesContent) (Irmin.Path.String_list) (Irmin.Branch.String) - (Irmin.Hash.SHA1) + (IrminBlake2B) type index = { path: string ; @@ -50,30 +86,6 @@ and context = { } type t = context -type commit = GitStore.Commit.Hash.t - -let dummy_commit = - match - GitStore.Commit.Hash.of_string "0000000000000000000000000000000000000000" - with - | Ok c -> c - | Error _ -> assert false - -let commit_encoding : commit Data_encoding.t = - let open Data_encoding in - conv - (fun c -> Cstruct.to_bigarray (Irmin.Type.encode_cstruct GitStore.Commit.Hash.t c)) - (fun c -> - match - Irmin.Type.decode_cstruct - GitStore.Commit.Hash.t - (Cstruct.of_bigarray c) - with - | Ok x -> x - | _ -> assert false - ) - bytes - (*-- Version Access and Update -----------------------------------------------*) let current_protocol_key = ["protocol"] diff --git a/lib_storage/context.mli b/lib_storage/context.mli index b29c3921d..a37a951c1 100644 --- a/lib_storage/context.mli +++ b/lib_storage/context.mli @@ -16,11 +16,6 @@ type index type t type context = t -type commit - -val dummy_commit: commit -val commit_encoding: commit Data_encoding.t - (** Open or initialize a versioned store at a given path. *) val init: ?patch_context:(context -> context Lwt.t) -> @@ -32,11 +27,11 @@ val commit_genesis: net_id:Net_id.t -> time:Time.t -> protocol:Protocol_hash.t -> - commit Lwt.t + Context_hash.t Lwt.t val commit_test_network_genesis: index -> Block_hash.t -> Time.t -> context -> - (Net_id.t * Block_hash.t * commit) tzresult Lwt.t + (Net_id.t * Block_hash.t * Context_hash.t) tzresult Lwt.t (** {2 Generic interface} ****************************************************) @@ -61,16 +56,16 @@ val fold_keys: (** {2 Accessing and Updating Versions} **************************************) -val exists: index -> commit -> bool Lwt.t -val checkout: index -> commit -> context option Lwt.t -val checkout_exn: index -> commit -> context Lwt.t +val exists: index -> Context_hash.t -> bool Lwt.t +val checkout: index -> Context_hash.t -> context option Lwt.t +val checkout_exn: index -> Context_hash.t -> context Lwt.t val commit: time:Time.t -> message:string -> context -> - commit Lwt.t -val set_head: index -> Net_id.t -> commit -> unit Lwt.t -val set_master: index -> commit -> unit Lwt.t + Context_hash.t Lwt.t +val set_head: index -> Net_id.t -> Context_hash.t -> unit Lwt.t +val set_master: index -> Context_hash.t -> unit Lwt.t (** {2 Predefined Fields} ****************************************************) diff --git a/lib_storage/store.ml b/lib_storage/store.ml index e06221b1a..bec45061a 100644 --- a/lib_storage/store.ml +++ b/lib_storage/store.ml @@ -86,7 +86,7 @@ module Block = struct max_operations_ttl: int ; max_number_of_operations: int list; max_operation_data_length: int; - context: Context.commit ; + context: Context_hash.t ; } module Contents = @@ -115,7 +115,7 @@ module Block = struct (req "max_operations_ttl" uint16) (req "max_number_of_operations" (list uint16)) (req "max_operation_data_length" uint16) - (req "context" Context.commit_encoding) + (req "context" Context_hash.encoding) (req "header" Block_header.encoding)) end)) diff --git a/lib_storage/store.mli b/lib_storage/store.mli index 5673d458b..f9209f0a3 100644 --- a/lib_storage/store.mli +++ b/lib_storage/store.mli @@ -90,7 +90,7 @@ module Block : sig max_operations_ttl: int ; max_number_of_operations: int list; max_operation_data_length: int; - context: Context.commit ; + context: Context_hash.t ; } module Contents : SINGLE_STORE diff --git a/test/shell/test_context.ml b/test/shell/test_context.ml index 527d1455a..2cd08de5e 100644 --- a/test/shell/test_context.ml +++ b/test/shell/test_context.ml @@ -84,10 +84,10 @@ let create_block3b idx block2_commit = type t = { idx: Context.index ; - genesis: Context.commit ; - block2: Context.commit ; - block3a: Context.commit ; - block3b: Context.commit ; + genesis: Context_hash.t ; + block2: Context_hash.t ; + block3a: Context_hash.t ; + block3b: Context_hash.t ; } let wrap_context_init f base_dir = diff --git a/test/shell/test_store.ml b/test/shell/test_store.ml index e19b13afb..e4d25304b 100644 --- a/test/shell/test_store.ml +++ b/test/shell/test_store.ml @@ -88,7 +88,7 @@ let lolblock ?(operations = []) header = } ; max_operations_ttl = 0 ; message = "" ; - context = Context.dummy_commit ; + context = Context_hash.zero ; max_number_of_operations = [] ; max_operation_data_length = 0 ; }