From 7d4e4f3be228254c3c01a5ac1671977ff04ee943 Mon Sep 17 00:00:00 2001 From: Andrea Cerone Date: Thu, 24 Nov 2022 12:01:38 +0000 Subject: [PATCH 1/4] Dac/Test: check that hash pages do not exceed page length --- .../lib_dal/test/test_dac_pages_encoding.ml | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/proto_alpha/lib_dal/test/test_dac_pages_encoding.ml b/src/proto_alpha/lib_dal/test/test_dac_pages_encoding.ml index e5ca1d133149..08c94545ce8b 100644 --- a/src/proto_alpha/lib_dal/test/test_dac_pages_encoding.ml +++ b/src/proto_alpha/lib_dal/test/test_dac_pages_encoding.ml @@ -237,6 +237,52 @@ module Merkle_tree = struct payload retrieved_payload + let multiple_pages_roundtrip_do_not_exceed_page_size () = + (* Check that a bug related to the size of hashes has been fixed. + Before the bug was fixed: the `Sc_rollup.Reveal_hash` module borrowed + the size function from the underlying hash module, meaning that it + would return `31` for the size, rather than the actual hash size + which is `32`. For a page that is exactly `98` bytes long, this would + mean that the serialization algorithm will compute the number of + hashes per page to be `(98-5)/31 = 3`, but the actual hash pages will + have size `32 * 3 + 5 = 101` bytes. This will cause the check on a page + size to fail, when serializing a page. With 98 bytes per page, 93 + bytes will be reserved for the payload in content pages. + Before the patch was applied, trying to + serialize a payload of `93 * 3 = 279` bytes with a page size of + 98 bytes would have caused to try to serialize a page containing + 3 hashes of 32 bytes each, resulting in a page of `101 bytes` and + causing the serialization to fail. + *) + let open Lwt_result_syntax in + let module Backend = Make_Merkle_tree_V0_backend () in + let max_page_size = 98 in + (* 279 bytes of payload *) + let payload = + Bytes.of_string + "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do \ + eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim \ + ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut \ + aliquip ex ea commodo consequat. Duis aute irure dolor in \ + reprehenderit in volup" + in + let* hash = + lift + @@ serialize_payload + ~max_page_size + payload + ~for_each_page:Backend.save_page + in + let* retrieved_payload = + lift + @@ deserialize_payload hash ~retrieve_page_from_hash:Backend.load_page + in + assert_equal_bytes + ~loc:__LOC__ + "Deserialized payload do not match with original" + payload + retrieved_payload + let long_content_roundtrip () = (* To ensure that the serialization and deserialization process work as expected, we test a roundtrip for a reasonably long text. We also @@ -437,4 +483,8 @@ let tests = "Serialization and deserialization of very long contents is correct." `Quick Merkle_tree.V0.long_content_roundtrip; + Tztest.tztest + "Hashes pages are not larger than expected" + `Quick + Merkle_tree.V0.multiple_pages_roundtrip_do_not_exceed_page_size; ] -- GitLab From 5b5cc1a216476bfa4cdeeffd71c66034d4f3e23f Mon Sep 17 00:00:00 2001 From: Andrea Cerone Date: Wed, 23 Nov 2022 16:03:31 +0000 Subject: [PATCH 2/4] Proto: remove incorrect functions from Reveal_hash --- src/proto_alpha/lib_dal/dac_pages_encoding.ml | 2 +- .../lib_dal/dac_preimage_data_manager.ml | 2 +- .../lib_protocol/alpha_context.mli | 28 +++++++++- .../lib_protocol/sc_rollup_PVM_sig.ml | 54 +++++++++++++++++-- 4 files changed, 80 insertions(+), 6 deletions(-) diff --git a/src/proto_alpha/lib_dal/dac_pages_encoding.ml b/src/proto_alpha/lib_dal/dac_pages_encoding.ml index be644c893f68..cdfe7440d0f9 100644 --- a/src/proto_alpha/lib_dal/dac_pages_encoding.ml +++ b/src/proto_alpha/lib_dal/dac_pages_encoding.ml @@ -158,7 +158,7 @@ module Merkle_tree = struct let hashes_version_tag = (2 * V.hashes_version) + 1 end - module Make (Hashing_scheme : Environment.S.HASH) (V : VERSION) = struct + module Make (Hashing_scheme : Sc_rollup.REVEAL_HASH) (V : VERSION) = struct let hash bytes = Hashing_scheme.hash_bytes [bytes] let hash_encoding = Hashing_scheme.encoding diff --git a/src/proto_alpha/lib_dal/dac_preimage_data_manager.ml b/src/proto_alpha/lib_dal/dac_preimage_data_manager.ml index 5bcd8da48b32..729b4d6c8684 100644 --- a/src/proto_alpha/lib_dal/dac_preimage_data_manager.ml +++ b/src/proto_alpha/lib_dal/dac_preimage_data_manager.ml @@ -42,7 +42,7 @@ let () = | Cannot_write_dac_page_to_disk b58_hash -> Some b58_hash | _ -> None) (fun b58_hash -> Cannot_write_dac_page_to_disk b58_hash) -module Make (Hash : Environment.S.HASH) = struct +module Make (Hash : Sc_rollup.REVEAL_HASH) = struct let path data_dir hash = Filename.(concat data_dir @@ Hash.to_b58check hash) let save_bytes data_dir hash page_contents = diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 4014a7a24ee5..a8af12c7c910 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -3269,7 +3269,33 @@ module Sc_rollup : sig module Input_hash : S.HASH - module Reveal_hash : S.HASH + module type REVEAL_HASH = sig + type t + + module Map : Map.S with type key = t + + val size : int + + val zero : t + + val pp : Format.formatter -> t -> unit + + val equal : t -> t -> bool + + val compare : t -> t -> int + + val of_b58check_opt : string -> t option + + val encoding : t Data_encoding.t + + val hash_string : ?key:string -> string list -> t + + val hash_bytes : ?key:bytes -> bytes list -> t + + val to_b58check : t -> string + end + + module Reveal_hash : REVEAL_HASH type reveal = | Reveal_raw_data of Reveal_hash.t diff --git a/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml b/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml index 7a044fbb0905..41850a5c0087 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml @@ -202,10 +202,36 @@ module Input_hash = let size = Some 20 end) +module type REVEAL_HASH = sig + type t + + module Map : Map.S with type key = t + + val size : int + + val zero : t + + val pp : Format.formatter -> t -> unit + + val equal : t -> t -> bool + + val compare : t -> t -> int + + val of_b58check_opt : string -> t option + + val encoding : t Data_encoding.t + + val hash_string : ?key:string -> string list -> t + + val hash_bytes : ?key:bytes -> bytes list -> t + + val to_b58check : t -> string +end + module Reveal_hash = struct (* Reserve the first byte in the encoding to support multi-versioning in the future. *) - module V0 = struct + module Blake2B = struct include Blake2B.Make (Base58) @@ -223,7 +249,23 @@ module Reveal_hash = struct let () = Base58.check_encoded_prefix b58check_encoding "scrrh1" 54 end - include V0 + type t = Blake2B.t + + module Map = Blake2B.Map + + let zero = Blake2B.zero + + let pp = Blake2B.pp + + let equal = Blake2B.equal + + let compare = Blake2B.compare + + let of_b58check_opt = Blake2B.of_b58check_opt + + (* Size of the hash is the size of the inner Blake2B plus one byte for the + tag used to identify the hashing scheme. *) + let size = Blake2B.size + 1 let encoding = let open Data_encoding in @@ -233,10 +275,16 @@ module Reveal_hash = struct case ~title:"Reveal_data_hash_v0" (Tag 0) - V0.encoding + Blake2B.encoding (fun s -> Some s) (fun s -> s); ] + + let hash_string = Blake2B.hash_string + + let hash_bytes = Blake2B.hash_bytes + + let to_b58check = Blake2B.to_b58check end type reveal = -- GitLab From 6a08b70fcee3407fe9d9cc93a96ba501a989c150 Mon Sep 17 00:00:00 2001 From: Andrea Cerone Date: Thu, 24 Nov 2022 13:48:08 +0000 Subject: [PATCH 3/4] Proto: Minimal interface for multi-reveal-hash --- .../bin_sc_rollup_node/RPC_server.ml | 4 +- src/proto_alpha/bin_sc_rollup_node/reveals.ml | 6 +- src/proto_alpha/lib_dal/dac_pages_encoding.ml | 20 +++- .../lib_protocol/alpha_context.mli | 12 ++- .../lib_protocol/sc_rollup_PVM_sig.ml | 95 +++++++++++++++---- .../lib_protocol/sc_rollup_proof_repr.ml | 8 +- .../lib_protocol/sc_rollup_wasm.ml | 4 +- 7 files changed, 117 insertions(+), 32 deletions(-) diff --git a/src/proto_alpha/bin_sc_rollup_node/RPC_server.ml b/src/proto_alpha/bin_sc_rollup_node/RPC_server.ml index 983c1ef176cc..9d7f4c44a692 100644 --- a/src/proto_alpha/bin_sc_rollup_node/RPC_server.ml +++ b/src/proto_alpha/bin_sc_rollup_node/RPC_server.ml @@ -277,7 +277,9 @@ module Make (Simulation : Simulation.S) (Batcher : Batcher.S) = struct let map = List.fold_left (fun map page -> - let hash = Sc_rollup.Reveal_hash.hash_string [page] in + let hash = + Sc_rollup.Reveal_hash.(hash_string ~scheme:Blake2B [page]) + in Sc_rollup.Reveal_hash.Map.add hash page map) Sc_rollup.Reveal_hash.Map.empty pages diff --git a/src/proto_alpha/bin_sc_rollup_node/reveals.ml b/src/proto_alpha/bin_sc_rollup_node/reveals.ml index 8642a5da8154..c7b0a268d845 100644 --- a/src/proto_alpha/bin_sc_rollup_node/reveals.ml +++ b/src/proto_alpha/bin_sc_rollup_node/reveals.ml @@ -122,7 +122,9 @@ let get ~data_dir ~pvm_name ~hash = let filename = path data_dir pvm_name hash in let* contents = file_contents filename in let*? () = - let contents_hash = Reveal_hash.hash_string [contents] in + let contents_hash = + Reveal_hash.hash_string ~scheme:Reveal_hash.Blake2B [contents] + in error_unless (Reveal_hash.equal contents_hash hash) (Wrong_hash {found = contents_hash; expected = hash}) @@ -215,7 +217,7 @@ module Arith = struct | None -> chunk | Some h -> Format.asprintf "%s hash:%a" chunk Reveal_hash.pp h in - let hash = Reveal_hash.hash_string [cell] in + let hash = Reveal_hash.hash_string ~scheme:Blake2B [cell] in aux (Some hash) ((hash, cell) :: linked_chunks) rev_chunks in aux None [] rev_chunks diff --git a/src/proto_alpha/lib_dal/dac_pages_encoding.ml b/src/proto_alpha/lib_dal/dac_pages_encoding.ml index cdfe7440d0f9..1038285965de 100644 --- a/src/proto_alpha/lib_dal/dac_pages_encoding.ml +++ b/src/proto_alpha/lib_dal/dac_pages_encoding.ml @@ -158,7 +158,13 @@ module Merkle_tree = struct let hashes_version_tag = (2 * V.hashes_version) + 1 end - module Make (Hashing_scheme : Sc_rollup.REVEAL_HASH) (V : VERSION) = struct + module Make (Hashing_scheme : sig + include Sc_rollup.REVEAL_HASH + + val scheme : supported_hashes + end) + (V : VERSION) = + struct let hash bytes = Hashing_scheme.hash_bytes [bytes] let hash_encoding = Hashing_scheme.encoding @@ -172,7 +178,7 @@ module Merkle_tree = struct bytes. *) let page_preamble_size = 5 - let hash_bytes_size = Hashing_scheme.size + let hash_bytes_size = Hashing_scheme.size ~scheme:Hashing_scheme.scheme (** Payload pages are encoded as follows: the first byte is an integer, which is corresponds to either `payload_version` (for payload pages) or @@ -286,7 +292,9 @@ module Merkle_tree = struct reasons. They are reversed again before the recursive tailcall is performed.*) let hashes_with_serialized_pages = - List.rev_map (fun page -> (hash page, page)) serialized_pages + List.rev_map + (fun page -> (hash ~scheme:Hashing_scheme.scheme page, page)) + serialized_pages in let* () = @@ -357,7 +365,11 @@ module Merkle_tree = struct module V0 = Make - (Sc_rollup.Reveal_hash) + (struct + include Sc_rollup.Reveal_hash + + let scheme = Sc_rollup.Reveal_hash.Blake2B + end) (Make_version (struct (* Cntents_version_tag used in contents pages is 0. *) let contents_version = 0 diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index a8af12c7c910..cf32e05f6ff2 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -3272,11 +3272,13 @@ module Sc_rollup : sig module type REVEAL_HASH = sig type t + type supported_hashes = Blake2B + module Map : Map.S with type key = t - val size : int + val size : scheme:supported_hashes -> int - val zero : t + val zero : scheme:supported_hashes -> t val pp : Format.formatter -> t -> unit @@ -3288,11 +3290,13 @@ module Sc_rollup : sig val encoding : t Data_encoding.t - val hash_string : ?key:string -> string list -> t + val hash_string : scheme:supported_hashes -> ?key:string -> string list -> t - val hash_bytes : ?key:bytes -> bytes list -> t + val hash_bytes : scheme:supported_hashes -> ?key:bytes -> bytes list -> t val to_b58check : t -> string + + val scheme_of_hash : t -> supported_hashes end module Reveal_hash : REVEAL_HASH diff --git a/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml b/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml index 41850a5c0087..6115decc14b1 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml @@ -203,29 +203,65 @@ module Input_hash = end) module type REVEAL_HASH = sig + (** The type of a reveal hash. *) type t + (** The hashing schemes supported by the reveal hash. *) + type supported_hashes = Blake2B + + (** A Map module for storing reveal-hash-indexed values. *) module Map : Map.S with type key = t - val size : int + (** [size ~scheme] returns the size of reveal hashes using the [scheme] + specified in input. *) + val size : scheme:supported_hashes -> int - val zero : t + (** [zero ~scheme] returns the reveal hash corresponding to the zero hash + for the [scheme] specified in input. *) + val zero : scheme:supported_hashes -> t + (** Formatting function for reveal-hashes. *) val pp : Format.formatter -> t -> unit + (** [equal hash1 hash2] checks if the two reveal-hashes [hash1] and [hash2] + are equal. This function must preserve the equality of individual + supported hashing schemes. If [hash1] and [hash2] are hashes obtained + from the same supported hashing scheme, then the [equal] function from + that hashing scheme is used to determine whether they are equivalent. + Otherwise, they are different. *) val equal : t -> t -> bool + (** [compare hash1 hash2] compares the values of the reveal hashes [hash1] + and [hash2]. This function must preserve the ordering of individual + supported hashing scheme. If [hash1] and [hash2] are reveal-hashes + obtained from the same hashing scheme, then [compare hash1 hash2] + should return the same result of the compare function exposed + by the hash module corresponding to their hashing scheme. *) val compare : t -> t -> int + (* [of_b58check_opt base58_hash] returns the reveal-hash, if any, + whose base58 encoding is [base58_hash]. To avoid ambiguity, + the [S.HASH] modules supported should avoid using the same prefix + for the base58 encoding. *) val of_b58check_opt : string -> t option + (* The encoding of reveal hashes. *) val encoding : t Data_encoding.t - val hash_string : ?key:string -> string list -> t + (* [hash_string ~scheme ?key strings] hashes [strings] using the + supported hashing [scheme] given in input. *) + val hash_string : scheme:supported_hashes -> ?key:string -> string list -> t - val hash_bytes : ?key:bytes -> bytes list -> t + (* [hash_bytes ~scheme ?key strings] hashes [bytes] using the + supported hashing [scheme] given in input. *) + val hash_bytes : scheme:supported_hashes -> ?key:bytes -> bytes list -> t + (* [to_b58check hash] returns the base58 encoded reveal hash. *) val to_b58check : t -> string + + (* [scheme_of_hash] hash returns the supported hashing scheme + that was used to obtain [hash]. *) + val scheme_of_hash : t -> supported_hashes end module Reveal_hash = struct @@ -236,7 +272,7 @@ module Reveal_hash = struct Blake2B.Make (Base58) (struct - let name = "Sc_rollup_reveal_data_hash" + let name = "Sc_rollup_reveal_data_blake2b_hash" let title = "A smart contract rollup reveal hash" @@ -249,23 +285,38 @@ module Reveal_hash = struct let () = Base58.check_encoded_prefix b58check_encoding "scrrh1" 54 end - type t = Blake2B.t + type supported_hashes = Blake2B + + type t = Blake2B of Blake2B.t - module Map = Blake2B.Map + let zero ~(scheme : supported_hashes) = + match scheme with Blake2B -> Blake2B Blake2B.zero - let zero = Blake2B.zero + let pp ppf hash = match hash with Blake2B hash -> Blake2B.pp ppf hash - let pp = Blake2B.pp + let equal h1 h2 = + match (h1, h2) with Blake2B h1, Blake2B h2 -> Blake2B.equal h1 h2 - let equal = Blake2B.equal + let compare h1 h2 = + match (h1, h2) with Blake2B h1, Blake2B h2 -> Blake2B.compare h1 h2 - let compare = Blake2B.compare + module Map = Map.Make (struct + type tmp = t - let of_b58check_opt = Blake2B.of_b58check_opt + type t = tmp - (* Size of the hash is the size of the inner Blake2B plus one byte for the + let compare = compare + end) + + let of_b58check_opt b58_hash = + b58_hash |> Blake2B.of_b58check_opt |> Option.map (fun hash -> Blake2B hash) + + (* Size of the hash is the size of the inner hash plus one byte for the tag used to identify the hashing scheme. *) - let size = Blake2B.size + 1 + let size ~(scheme : supported_hashes) = + let tag_size = 1 in + let size_without_tag = match scheme with Blake2B -> Blake2B.size in + tag_size + size_without_tag let encoding = let open Data_encoding in @@ -276,15 +327,21 @@ module Reveal_hash = struct ~title:"Reveal_data_hash_v0" (Tag 0) Blake2B.encoding - (fun s -> Some s) - (fun s -> s); + (fun (Blake2B s) -> Some s) + (fun s -> Blake2B s); ] - let hash_string = Blake2B.hash_string + let hash_string ~(scheme : supported_hashes) ?key strings = + match scheme with Blake2B -> Blake2B (Blake2B.hash_string ?key strings) + + let hash_bytes ~(scheme : supported_hashes) ?key bytes = + match scheme with Blake2B -> Blake2B (Blake2B.hash_bytes ?key bytes) - let hash_bytes = Blake2B.hash_bytes + let to_b58check hash = + match hash with Blake2B hash -> Blake2B.to_b58check hash - let to_b58check = Blake2B.to_b58check + let scheme_of_hash hash = + match hash with Blake2B _hash -> (Blake2B : supported_hashes) end type reveal = diff --git a/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.ml b/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.ml index 75e8c46e0f34..1a6c82ecccf0 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.ml @@ -328,7 +328,13 @@ let valid ~metadata snapshot commit_level dal_snapshot dal_parameters input request." | ( Some (Reveal_proof (Raw_data_proof data)), Needs_reveal (Reveal_raw_data expected_hash) ) -> - let data_hash = Sc_rollup_PVM_sig.Reveal_hash.hash_string [data] in + let scheme = + Sc_rollup_PVM_sig.Reveal_hash.scheme_of_hash expected_hash + in + + let data_hash = + Sc_rollup_PVM_sig.Reveal_hash.hash_string ~scheme [data] + in check (Sc_rollup_PVM_sig.Reveal_hash.equal data_hash expected_hash) "Invalid reveal" diff --git a/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml b/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml index 330e67005462..6ecdcc2c3b98 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml @@ -288,7 +288,9 @@ module V2_0_0 = struct | None -> (* In case of an invalid hash, the rollup is blocked. Any commitment will be invalid. *) - Waiting_for_reveal (Reveal_raw_data Reveal_hash.zero)) + Waiting_for_reveal + (Reveal_raw_data (Reveal_hash.zero ~scheme:Reveal_hash.Blake2B)) + ) | Reveal_required Wasm_2_0_0.Reveal_metadata -> Waiting_for_reveal Reveal_metadata -- GitLab From 792cc93979dc8d47f26b5e8a3370d8277767093d Mon Sep 17 00:00:00 2001 From: Thomas Letan Date: Fri, 25 Nov 2022 13:57:13 +0100 Subject: [PATCH 4/4] Proto: move Sc_rollup_reveal_hash to own module --- .../bin_sc_rollup_node/RPC_server.ml | 6 +- .../bin_sc_rollup_node/fueled_pvm.ml | 6 +- .../main_sc_rollup_node_alpha.ml | 4 +- src/proto_alpha/bin_sc_rollup_node/reveals.ml | 4 +- .../bin_sc_rollup_node/reveals.mli | 8 +- .../bin_sc_rollup_node/simulation.ml | 6 +- .../bin_sc_rollup_node/simulation.mli | 5 +- .../bin_sc_rollup_node/wasm_2_0_0_pvm.ml | 2 +- src/proto_alpha/lib_dal/dac_pages_encoding.ml | 7 +- .../lib_dal/dac_preimage_data_manager.ml | 7 +- .../lib_dal/test/test_dac_pages_encoding.ml | 7 +- src/proto_alpha/lib_protocol/TEZOS_PROTOCOL | 1 + .../lib_protocol/alpha_context.mli | 36 +---- src/proto_alpha/lib_protocol/dune | 4 + .../lib_protocol/sc_rollup_PVM_sig.ml | 150 +----------------- .../lib_protocol/sc_rollup_arith.ml | 2 +- .../lib_protocol/sc_rollup_proof_repr.ml | 12 +- .../lib_protocol/sc_rollup_proof_repr.mli | 2 +- .../lib_protocol/sc_rollup_reveal_hash.ml | 103 ++++++++++++ .../lib_protocol/sc_rollup_reveal_hash.mli | 85 ++++++++++ .../lib_protocol/sc_rollup_wasm.ml | 7 +- 21 files changed, 240 insertions(+), 224 deletions(-) create mode 100644 src/proto_alpha/lib_protocol/sc_rollup_reveal_hash.ml create mode 100644 src/proto_alpha/lib_protocol/sc_rollup_reveal_hash.mli diff --git a/src/proto_alpha/bin_sc_rollup_node/RPC_server.ml b/src/proto_alpha/bin_sc_rollup_node/RPC_server.ml index 9d7f4c44a692..d44edd6e42a8 100644 --- a/src/proto_alpha/bin_sc_rollup_node/RPC_server.ml +++ b/src/proto_alpha/bin_sc_rollup_node/RPC_server.ml @@ -278,10 +278,10 @@ module Make (Simulation : Simulation.S) (Batcher : Batcher.S) = struct List.fold_left (fun map page -> let hash = - Sc_rollup.Reveal_hash.(hash_string ~scheme:Blake2B [page]) + Sc_rollup_reveal_hash.(hash_string ~scheme:Blake2B [page]) in - Sc_rollup.Reveal_hash.Map.add hash page map) - Sc_rollup.Reveal_hash.Map.empty + Sc_rollup_reveal_hash.Map.add hash page map) + Sc_rollup_reveal_hash.Map.empty pages in Some map diff --git a/src/proto_alpha/bin_sc_rollup_node/fueled_pvm.ml b/src/proto_alpha/bin_sc_rollup_node/fueled_pvm.ml index 2add615ac90d..50a25ce43a60 100644 --- a/src/proto_alpha/bin_sc_rollup_node/fueled_pvm.ml +++ b/src/proto_alpha/bin_sc_rollup_node/fueled_pvm.ml @@ -57,7 +57,7 @@ module type S = sig [reveal_map] is provided, it is used as an additional source of data for revelation ticks. *) val eval_messages : - ?reveal_map:string Sc_rollup.Reveal_hash.Map.t -> + ?reveal_map:string Sc_rollup_reveal_hash.Map.t -> fuel:fuel -> _ Node_context.t -> message_counter_offset:int -> @@ -84,7 +84,7 @@ module Make (PVM : Pvm.S) = struct let found_in_map = match reveal_map with | None -> None - | Some map -> Sc_rollup.Reveal_hash.Map.find_opt hash map + | Some map -> Sc_rollup_reveal_hash.Map.find_opt hash map in match found_in_map with | Some data -> return data @@ -119,7 +119,7 @@ module Make (PVM : Pvm.S) = struct (* The 32-byte payload represents the encoded [Reveal_hash.t]. We must decode it properly, instead of converting it byte-for-byte. *) Tezos_webassembly_interpreter.Reveal.reveal_hash_to_string hash - |> Data_encoding.Binary.of_string_exn Sc_rollup.Reveal_hash.encoding + |> Data_encoding.Binary.of_string_exn Sc_rollup_reveal_hash.encoding in let*! data = get_reveal ~data_dir:node_ctxt.data_dir reveal_map hash diff --git a/src/proto_alpha/bin_sc_rollup_node/main_sc_rollup_node_alpha.ml b/src/proto_alpha/bin_sc_rollup_node/main_sc_rollup_node_alpha.ml index 8fab92e96193..e09a73676768 100644 --- a/src/proto_alpha/bin_sc_rollup_node/main_sc_rollup_node_alpha.ml +++ b/src/proto_alpha/bin_sc_rollup_node/main_sc_rollup_node_alpha.ml @@ -382,9 +382,7 @@ let import_command = (fun (data_dir, filename, pvm_kind) cctxt -> let open Lwt_result_syntax in let*? hash = Reveals.import ~data_dir pvm_kind ~filename in - let*! () = - cctxt#message "%a" Protocol.Alpha_context.Sc_rollup.Reveal_hash.pp hash - in + let*! () = cctxt#message "%a" Protocol.Sc_rollup_reveal_hash.pp hash in return_unit) let sc_rollup_commands () = diff --git a/src/proto_alpha/bin_sc_rollup_node/reveals.ml b/src/proto_alpha/bin_sc_rollup_node/reveals.ml index c7b0a268d845..436d42bb9c44 100644 --- a/src/proto_alpha/bin_sc_rollup_node/reveals.ml +++ b/src/proto_alpha/bin_sc_rollup_node/reveals.ml @@ -23,7 +23,7 @@ (* *) (*****************************************************************************) -module Reveal_hash = Protocol.Alpha_context.Sc_rollup.Reveal_hash +module Reveal_hash = Protocol.Sc_rollup_reveal_hash type error += | Wrong_hash of {found : Reveal_hash.t; expected : Reveal_hash.t} @@ -252,7 +252,7 @@ module Arith = struct let linked_hashed_chunks = chunkify source in let chunks_map = linked_hashed_chunks |> List.to_seq - |> Protocol.Alpha_context.Sc_rollup.Reveal_hash.Map.of_seq + |> Protocol.Sc_rollup_reveal_hash.Map.of_seq in let+ hash = first_hash linked_hashed_chunks in (chunks_map, hash) diff --git a/src/proto_alpha/bin_sc_rollup_node/reveals.mli b/src/proto_alpha/bin_sc_rollup_node/reveals.mli index bdffe3bc9618..19debadd0b1e 100644 --- a/src/proto_alpha/bin_sc_rollup_node/reveals.mli +++ b/src/proto_alpha/bin_sc_rollup_node/reveals.mli @@ -69,7 +69,7 @@ type source = val get : data_dir:string -> pvm_name:string -> - hash:Sc_rollup.Reveal_hash.t -> + hash:Protocol.Sc_rollup_reveal_hash.t -> string tzresult Lwt.t (** [import ~data_dir pvm_kind ~filename] turns the content of ~filename into a @@ -79,7 +79,7 @@ val import : data_dir:string -> Sc_rollup.Kind.t -> filename:string -> - Sc_rollup.Reveal_hash.t tzresult + Protocol.Sc_rollup_reveal_hash.t tzresult (** [chunkify pvm_kind source] turns the content of ~filename into a chunk of pages of (at most) 4KB. It returns the map of chunks and the hash of the @@ -87,4 +87,6 @@ val import : val chunkify : Sc_rollup.Kind.t -> source -> - (string Sc_rollup.Reveal_hash.Map.t * Sc_rollup.Reveal_hash.t) tzresult + (string Protocol.Sc_rollup_reveal_hash.Map.t + * Protocol.Sc_rollup_reveal_hash.t) + tzresult diff --git a/src/proto_alpha/bin_sc_rollup_node/simulation.ml b/src/proto_alpha/bin_sc_rollup_node/simulation.ml index e0b7fe4845de..8f64a279f327 100644 --- a/src/proto_alpha/bin_sc_rollup_node/simulation.ml +++ b/src/proto_alpha/bin_sc_rollup_node/simulation.ml @@ -43,7 +43,7 @@ module type S = sig ctxt : Context.ro; inbox_level : Raw_level.t; state : PVM.state; - reveal_map : string Sc_rollup.Reveal_hash.Map.t option; + reveal_map : string Sc_rollup_reveal_hash.Map.t option; nb_messages_period : int64; nb_messages_inbox : int; level_position : level_position; @@ -52,7 +52,7 @@ module type S = sig val start_simulation : Node_context.ro -> - reveal_map:string Sc_rollup.Reveal_hash.Map.t option -> + reveal_map:string Sc_rollup_reveal_hash.Map.t option -> Layer1.head -> t tzresult Lwt.t @@ -82,7 +82,7 @@ module Make (Interpreter : Interpreter.S) : ctxt : Context.ro; inbox_level : Raw_level.t; state : PVM.state; - reveal_map : string Sc_rollup.Reveal_hash.Map.t option; + reveal_map : string Sc_rollup_reveal_hash.Map.t option; nb_messages_period : int64; nb_messages_inbox : int; level_position : level_position; diff --git a/src/proto_alpha/bin_sc_rollup_node/simulation.mli b/src/proto_alpha/bin_sc_rollup_node/simulation.mli index 1f95fe0c5767..4f559f6862b4 100644 --- a/src/proto_alpha/bin_sc_rollup_node/simulation.mli +++ b/src/proto_alpha/bin_sc_rollup_node/simulation.mli @@ -23,6 +23,7 @@ (* *) (*****************************************************************************) +open Protocol open Protocol.Alpha_context module type S = sig @@ -43,7 +44,7 @@ module type S = sig ctxt : Context.ro; inbox_level : Raw_level.t; state : PVM.state; - reveal_map : string Sc_rollup.Reveal_hash.Map.t option; + reveal_map : string Sc_rollup_reveal_hash.Map.t option; nb_messages_period : int64; nb_messages_inbox : int; level_position : level_position; @@ -54,7 +55,7 @@ module type S = sig {e on top} of [block], i.e. for an hypothetical new inbox (level). *) val start_simulation : Node_context.ro -> - reveal_map:string Sc_rollup.Reveal_hash.Map.t option -> + reveal_map:string Sc_rollup_reveal_hash.Map.t option -> Layer1.head -> t tzresult Lwt.t diff --git a/src/proto_alpha/bin_sc_rollup_node/wasm_2_0_0_pvm.ml b/src/proto_alpha/bin_sc_rollup_node/wasm_2_0_0_pvm.ml index f0a5b0515adf..dc32c2bdd5a5 100644 --- a/src/proto_alpha/bin_sc_rollup_node/wasm_2_0_0_pvm.ml +++ b/src/proto_alpha/bin_sc_rollup_node/wasm_2_0_0_pvm.ml @@ -79,7 +79,7 @@ module Impl : Pvm.S = struct | Waiting_for_reveal (Sc_rollup.Reveal_raw_data hash) -> Format.asprintf "Waiting for preimage reveal %a" - Sc_rollup.Reveal_hash.pp + Sc_rollup_reveal_hash.pp hash | Waiting_for_reveal Sc_rollup.Reveal_metadata -> "Waiting for metadata" | Waiting_for_reveal (Sc_rollup.Request_dal_page page_id) -> diff --git a/src/proto_alpha/lib_dal/dac_pages_encoding.ml b/src/proto_alpha/lib_dal/dac_pages_encoding.ml index 1038285965de..a225a5a4f2ca 100644 --- a/src/proto_alpha/lib_dal/dac_pages_encoding.ml +++ b/src/proto_alpha/lib_dal/dac_pages_encoding.ml @@ -31,7 +31,6 @@ *) open Protocol -open Alpha_context open Environment.Error_monad type error += @@ -159,7 +158,7 @@ module Merkle_tree = struct end module Make (Hashing_scheme : sig - include Sc_rollup.REVEAL_HASH + include Dac_preimage_data_manager.REVEAL_HASH val scheme : supported_hashes end) @@ -366,9 +365,9 @@ module Merkle_tree = struct module V0 = Make (struct - include Sc_rollup.Reveal_hash + include Sc_rollup_reveal_hash - let scheme = Sc_rollup.Reveal_hash.Blake2B + let scheme = Sc_rollup_reveal_hash.Blake2B end) (Make_version (struct (* Cntents_version_tag used in contents pages is 0. *) diff --git a/src/proto_alpha/lib_dal/dac_preimage_data_manager.ml b/src/proto_alpha/lib_dal/dac_preimage_data_manager.ml index 729b4d6c8684..1cd86db1b405 100644 --- a/src/proto_alpha/lib_dal/dac_preimage_data_manager.ml +++ b/src/proto_alpha/lib_dal/dac_preimage_data_manager.ml @@ -24,7 +24,6 @@ (*****************************************************************************) open Protocol -open Alpha_context open Environment.Error_monad type error += Cannot_write_dac_page_to_disk of string @@ -42,7 +41,9 @@ let () = | Cannot_write_dac_page_to_disk b58_hash -> Some b58_hash | _ -> None) (fun b58_hash -> Cannot_write_dac_page_to_disk b58_hash) -module Make (Hash : Sc_rollup.REVEAL_HASH) = struct +module type REVEAL_HASH = module type of Sc_rollup_reveal_hash + +module Make (Hash : REVEAL_HASH) = struct let path data_dir hash = Filename.(concat data_dir @@ Hash.to_b58check hash) let save_bytes data_dir hash page_contents = @@ -57,4 +58,4 @@ module Make (Hash : Sc_rollup.REVEAL_HASH) = struct | Error _ -> tzfail @@ Cannot_write_dac_page_to_disk (Hash.to_b58check hash) end -module Reveal_hash = Make (Sc_rollup.Reveal_hash) +module Reveal_hash = Make (Sc_rollup_reveal_hash) diff --git a/src/proto_alpha/lib_dal/test/test_dac_pages_encoding.ml b/src/proto_alpha/lib_dal/test/test_dac_pages_encoding.ml index 08c94545ce8b..5c50ab259ca1 100644 --- a/src/proto_alpha/lib_dal/test/test_dac_pages_encoding.ml +++ b/src/proto_alpha/lib_dal/test/test_dac_pages_encoding.ml @@ -35,14 +35,13 @@ Add tests to check actual (sequences of) bytes in serialized pages. *) open Protocol -open Alpha_context let lift f = Lwt.map Environment.wrap_tzresult f (* Tests are run against a mock storage backend where a Hash-indexed/Bytes-valued Map is used to simulate adding and retrieving files to a directory. *) -module Hashes_map = Sc_rollup.Reveal_hash.Map +module Hashes_map = Sc_rollup_reveal_hash.Map type hashes_map = bytes Hashes_map.t @@ -50,8 +49,8 @@ module Make_Merkle_tree_V0_backend () = struct open Environment.Error_monad type error += - | Page_already_saved of Sc_rollup.Reveal_hash.t - | Page_is_missing of Sc_rollup.Reveal_hash.t + | Page_already_saved of Sc_rollup_reveal_hash.t + | Page_is_missing of Sc_rollup_reveal_hash.t let backend = ref Hashes_map.empty diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index b679da82972d..a3c629dcaf16 100644 --- a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL +++ b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL @@ -14,6 +14,7 @@ "Blinded_public_key_hash", "Block_payload_hash", "Tx_rollup_prefixes", + "Sc_rollup_reveal_hash", "Merkle_list", "Bitset", "Bounded_history_repr", diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index cf32e05f6ff2..344ed7a77746 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -3269,40 +3269,8 @@ module Sc_rollup : sig module Input_hash : S.HASH - module type REVEAL_HASH = sig - type t - - type supported_hashes = Blake2B - - module Map : Map.S with type key = t - - val size : scheme:supported_hashes -> int - - val zero : scheme:supported_hashes -> t - - val pp : Format.formatter -> t -> unit - - val equal : t -> t -> bool - - val compare : t -> t -> int - - val of_b58check_opt : string -> t option - - val encoding : t Data_encoding.t - - val hash_string : scheme:supported_hashes -> ?key:string -> string list -> t - - val hash_bytes : scheme:supported_hashes -> ?key:bytes -> bytes list -> t - - val to_b58check : t -> string - - val scheme_of_hash : t -> supported_hashes - end - - module Reveal_hash : REVEAL_HASH - type reveal = - | Reveal_raw_data of Reveal_hash.t + | Reveal_raw_data of Sc_rollup_reveal_hash.t | Reveal_metadata | Request_dal_page of Dal.Page.t @@ -3829,7 +3797,7 @@ module Sc_rollup : sig val proof_encoding : proof Data_encoding.t - val reveal : Reveal_hash.t -> string option Lwt.t + val reveal : Sc_rollup_reveal_hash.t -> string option Lwt.t module Inbox_with_history : sig val inbox : Inbox.history_proof diff --git a/src/proto_alpha/lib_protocol/dune b/src/proto_alpha/lib_protocol/dune index 4068f716b599..739b8eaa7af8 100644 --- a/src/proto_alpha/lib_protocol/dune +++ b/src/proto_alpha/lib_protocol/dune @@ -44,6 +44,7 @@ Blinded_public_key_hash Block_payload_hash Tx_rollup_prefixes + Sc_rollup_reveal_hash Merkle_list Bitset Bounded_history_repr @@ -317,6 +318,7 @@ blinded_public_key_hash.ml blinded_public_key_hash.mli block_payload_hash.ml block_payload_hash.mli tx_rollup_prefixes.ml tx_rollup_prefixes.mli + sc_rollup_reveal_hash.ml sc_rollup_reveal_hash.mli merkle_list.ml merkle_list.mli bitset.ml bitset.mli bounded_history_repr.ml bounded_history_repr.mli @@ -575,6 +577,7 @@ blinded_public_key_hash.ml blinded_public_key_hash.mli block_payload_hash.ml block_payload_hash.mli tx_rollup_prefixes.ml tx_rollup_prefixes.mli + sc_rollup_reveal_hash.ml sc_rollup_reveal_hash.mli merkle_list.ml merkle_list.mli bitset.ml bitset.mli bounded_history_repr.ml bounded_history_repr.mli @@ -838,6 +841,7 @@ blinded_public_key_hash.ml blinded_public_key_hash.mli block_payload_hash.ml block_payload_hash.mli tx_rollup_prefixes.ml tx_rollup_prefixes.mli + sc_rollup_reveal_hash.ml sc_rollup_reveal_hash.mli merkle_list.ml merkle_list.mli bitset.ml bitset.mli bounded_history_repr.ml bounded_history_repr.mli diff --git a/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml b/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml index 6115decc14b1..618f9f00a5d7 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml @@ -202,150 +202,8 @@ module Input_hash = let size = Some 20 end) -module type REVEAL_HASH = sig - (** The type of a reveal hash. *) - type t - - (** The hashing schemes supported by the reveal hash. *) - type supported_hashes = Blake2B - - (** A Map module for storing reveal-hash-indexed values. *) - module Map : Map.S with type key = t - - (** [size ~scheme] returns the size of reveal hashes using the [scheme] - specified in input. *) - val size : scheme:supported_hashes -> int - - (** [zero ~scheme] returns the reveal hash corresponding to the zero hash - for the [scheme] specified in input. *) - val zero : scheme:supported_hashes -> t - - (** Formatting function for reveal-hashes. *) - val pp : Format.formatter -> t -> unit - - (** [equal hash1 hash2] checks if the two reveal-hashes [hash1] and [hash2] - are equal. This function must preserve the equality of individual - supported hashing schemes. If [hash1] and [hash2] are hashes obtained - from the same supported hashing scheme, then the [equal] function from - that hashing scheme is used to determine whether they are equivalent. - Otherwise, they are different. *) - val equal : t -> t -> bool - - (** [compare hash1 hash2] compares the values of the reveal hashes [hash1] - and [hash2]. This function must preserve the ordering of individual - supported hashing scheme. If [hash1] and [hash2] are reveal-hashes - obtained from the same hashing scheme, then [compare hash1 hash2] - should return the same result of the compare function exposed - by the hash module corresponding to their hashing scheme. *) - val compare : t -> t -> int - - (* [of_b58check_opt base58_hash] returns the reveal-hash, if any, - whose base58 encoding is [base58_hash]. To avoid ambiguity, - the [S.HASH] modules supported should avoid using the same prefix - for the base58 encoding. *) - val of_b58check_opt : string -> t option - - (* The encoding of reveal hashes. *) - val encoding : t Data_encoding.t - - (* [hash_string ~scheme ?key strings] hashes [strings] using the - supported hashing [scheme] given in input. *) - val hash_string : scheme:supported_hashes -> ?key:string -> string list -> t - - (* [hash_bytes ~scheme ?key strings] hashes [bytes] using the - supported hashing [scheme] given in input. *) - val hash_bytes : scheme:supported_hashes -> ?key:bytes -> bytes list -> t - - (* [to_b58check hash] returns the base58 encoded reveal hash. *) - val to_b58check : t -> string - - (* [scheme_of_hash] hash returns the supported hashing scheme - that was used to obtain [hash]. *) - val scheme_of_hash : t -> supported_hashes -end - -module Reveal_hash = struct - (* Reserve the first byte in the encoding to support multi-versioning - in the future. *) - module Blake2B = struct - include - Blake2B.Make - (Base58) - (struct - let name = "Sc_rollup_reveal_data_blake2b_hash" - - let title = "A smart contract rollup reveal hash" - - let b58check_prefix = - "\017\144\121\209\203" (* "scrrh1(54)" decoded from Base58. *) - - let size = Some 31 - end) - - let () = Base58.check_encoded_prefix b58check_encoding "scrrh1" 54 - end - - type supported_hashes = Blake2B - - type t = Blake2B of Blake2B.t - - let zero ~(scheme : supported_hashes) = - match scheme with Blake2B -> Blake2B Blake2B.zero - - let pp ppf hash = match hash with Blake2B hash -> Blake2B.pp ppf hash - - let equal h1 h2 = - match (h1, h2) with Blake2B h1, Blake2B h2 -> Blake2B.equal h1 h2 - - let compare h1 h2 = - match (h1, h2) with Blake2B h1, Blake2B h2 -> Blake2B.compare h1 h2 - - module Map = Map.Make (struct - type tmp = t - - type t = tmp - - let compare = compare - end) - - let of_b58check_opt b58_hash = - b58_hash |> Blake2B.of_b58check_opt |> Option.map (fun hash -> Blake2B hash) - - (* Size of the hash is the size of the inner hash plus one byte for the - tag used to identify the hashing scheme. *) - let size ~(scheme : supported_hashes) = - let tag_size = 1 in - let size_without_tag = match scheme with Blake2B -> Blake2B.size in - tag_size + size_without_tag - - let encoding = - let open Data_encoding in - union - ~tag_size:`Uint8 - [ - case - ~title:"Reveal_data_hash_v0" - (Tag 0) - Blake2B.encoding - (fun (Blake2B s) -> Some s) - (fun s -> Blake2B s); - ] - - let hash_string ~(scheme : supported_hashes) ?key strings = - match scheme with Blake2B -> Blake2B (Blake2B.hash_string ?key strings) - - let hash_bytes ~(scheme : supported_hashes) ?key bytes = - match scheme with Blake2B -> Blake2B (Blake2B.hash_bytes ?key bytes) - - let to_b58check hash = - match hash with Blake2B hash -> Blake2B.to_b58check hash - - let scheme_of_hash hash = - match hash with Blake2B _hash -> (Blake2B : supported_hashes) -end - type reveal = - | Reveal_raw_data of Reveal_hash.t + | Reveal_raw_data of Sc_rollup_reveal_hash.t | Reveal_metadata | Request_dal_page of Dal_slot_repr.Page.t @@ -357,7 +215,7 @@ let reveal_encoding = (Tag 0) (obj2 (req "reveal_kind" (constant "reveal_raw_data")) - (req "input_hash" Reveal_hash.encoding)) + (req "input_hash" Sc_rollup_reveal_hash.encoding)) (function Reveal_raw_data s -> Some ((), s) | _ -> None) (fun ((), s) -> Reveal_raw_data s) and case_metadata = @@ -438,7 +296,7 @@ let input_request_encoding = ] let pp_reveal fmt = function - | Reveal_raw_data hash -> Reveal_hash.pp fmt hash + | Reveal_raw_data hash -> Sc_rollup_reveal_hash.pp fmt hash | Reveal_metadata -> Format.pp_print_string fmt "Reveal metadata" | Request_dal_page id -> Dal_slot_repr.Page.pp fmt id @@ -461,7 +319,7 @@ let pp_input_request fmt request = let reveal_equal p1 p2 = match (p1, p2) with - | Reveal_raw_data h1, Reveal_raw_data h2 -> Reveal_hash.equal h1 h2 + | Reveal_raw_data h1, Reveal_raw_data h2 -> Sc_rollup_reveal_hash.equal h1 h2 | Reveal_metadata, Reveal_metadata -> true | Request_dal_page a, Request_dal_page b -> Dal_slot_repr.Page.equal a b | (Reveal_raw_data _ | Reveal_metadata | Request_dal_page _), _ -> false diff --git a/src/proto_alpha/lib_protocol/sc_rollup_arith.ml b/src/proto_alpha/lib_protocol/sc_rollup_arith.ml index 48dd7216682b..7682ab3f10e3 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_arith.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_arith.ml @@ -1222,7 +1222,7 @@ module Make (Context : P) : let evaluate_preimage_request hash = let open Monad.Syntax in - match PS.Reveal_hash.of_b58check_opt hash with + match Sc_rollup_reveal_hash.of_b58check_opt hash with | None -> stop_evaluating false | Some hash -> let* () = Required_reveal.set (Some (Reveal_raw_data hash)) in diff --git a/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.ml b/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.ml index 1a6c82ecccf0..809651adfb0c 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.ml @@ -328,15 +328,11 @@ let valid ~metadata snapshot commit_level dal_snapshot dal_parameters input request." | ( Some (Reveal_proof (Raw_data_proof data)), Needs_reveal (Reveal_raw_data expected_hash) ) -> - let scheme = - Sc_rollup_PVM_sig.Reveal_hash.scheme_of_hash expected_hash - in + let scheme = Sc_rollup_reveal_hash.scheme_of_hash expected_hash in - let data_hash = - Sc_rollup_PVM_sig.Reveal_hash.hash_string ~scheme [data] - in + let data_hash = Sc_rollup_reveal_hash.hash_string ~scheme [data] in check - (Sc_rollup_PVM_sig.Reveal_hash.equal data_hash expected_hash) + (Sc_rollup_reveal_hash.equal data_hash expected_hash) "Invalid reveal" | Some (Reveal_proof Metadata_proof), Needs_reveal Reveal_metadata -> return_unit @@ -362,7 +358,7 @@ module type PVM_with_context_and_state = sig val proof_encoding : proof Data_encoding.t - val reveal : Sc_rollup_PVM_sig.Reveal_hash.t -> string option Lwt.t + val reveal : Sc_rollup_reveal_hash.t -> string option Lwt.t module Inbox_with_history : sig val inbox : Sc_rollup_inbox_repr.history_proof diff --git a/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.mli b/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.mli index 652b28109f4e..38e04326097f 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.mli @@ -155,7 +155,7 @@ module type PVM_with_context_and_state = sig val proof_encoding : proof Data_encoding.t - val reveal : Sc_rollup_PVM_sig.Reveal_hash.t -> string option Lwt.t + val reveal : Sc_rollup_reveal_hash.t -> string option Lwt.t module Inbox_with_history : sig val inbox : Sc_rollup_inbox_repr.history_proof diff --git a/src/proto_alpha/lib_protocol/sc_rollup_reveal_hash.ml b/src/proto_alpha/lib_protocol/sc_rollup_reveal_hash.ml new file mode 100644 index 000000000000..fb378ecc5d02 --- /dev/null +++ b/src/proto_alpha/lib_protocol/sc_rollup_reveal_hash.ml @@ -0,0 +1,103 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2022 Nomadic Labs *) +(* Copyright (c) 2022 Trili Tech, *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +(* Reserve the first byte in the encoding to support multi-versioning + in the future. *) +module Blake2B = struct + include + Blake2B.Make + (Base58) + (struct + let name = "Sc_rollup_reveal_data_blake2b_hash" + + let title = "A smart contract rollup reveal hash" + + let b58check_prefix = + "\017\144\121\209\203" (* "scrrh1(54)" decoded from Base58. *) + + let size = Some 31 + end) + + let () = Base58.check_encoded_prefix b58check_encoding "scrrh1" 54 +end + +type supported_hashes = Blake2B + +type t = Blake2B of Blake2B.t + +let zero ~(scheme : supported_hashes) = + match scheme with Blake2B -> Blake2B Blake2B.zero + +let pp ppf hash = match hash with Blake2B hash -> Blake2B.pp ppf hash + +let equal h1 h2 = + match (h1, h2) with Blake2B h1, Blake2B h2 -> Blake2B.equal h1 h2 + +let compare h1 h2 = + match (h1, h2) with Blake2B h1, Blake2B h2 -> Blake2B.compare h1 h2 + +module Map = Map.Make (struct + type tmp = t + + type t = tmp + + let compare = compare +end) + +let of_b58check_opt b58_hash = + b58_hash |> Blake2B.of_b58check_opt |> Option.map (fun hash -> Blake2B hash) + +(* Size of the hash is the size of the inner hash plus one byte for the + tag used to identify the hashing scheme. *) +let size ~(scheme : supported_hashes) = + let tag_size = 1 in + let size_without_tag = match scheme with Blake2B -> Blake2B.size in + tag_size + size_without_tag + +let encoding = + let open Data_encoding in + union + ~tag_size:`Uint8 + [ + case + ~title:"Reveal_data_hash_v0" + (Tag 0) + Blake2B.encoding + (fun (Blake2B s) -> Some s) + (fun s -> Blake2B s); + ] + +let hash_string ~(scheme : supported_hashes) ?key strings = + match scheme with Blake2B -> Blake2B (Blake2B.hash_string ?key strings) + +let hash_bytes ~(scheme : supported_hashes) ?key bytes = + match scheme with Blake2B -> Blake2B (Blake2B.hash_bytes ?key bytes) + +let to_b58check hash = + match hash with Blake2B hash -> Blake2B.to_b58check hash + +let scheme_of_hash hash = + match hash with Blake2B _hash -> (Blake2B : supported_hashes) diff --git a/src/proto_alpha/lib_protocol/sc_rollup_reveal_hash.mli b/src/proto_alpha/lib_protocol/sc_rollup_reveal_hash.mli new file mode 100644 index 000000000000..084e09fc4e99 --- /dev/null +++ b/src/proto_alpha/lib_protocol/sc_rollup_reveal_hash.mli @@ -0,0 +1,85 @@ +(*****************************************************************************) +(* *) +(* Open Source License *) +(* Copyright (c) 2022 Nomadic Labs *) +(* Copyright (c) 2022 Trili Tech, *) +(* *) +(* Permission is hereby granted, free of charge, to any person obtaining a *) +(* copy of this software and associated documentation files (the "Software"),*) +(* to deal in the Software without restriction, including without limitation *) +(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) +(* and/or sell copies of the Software, and to permit persons to whom the *) +(* Software is furnished to do so, subject to the following conditions: *) +(* *) +(* The above copyright notice and this permission notice shall be included *) +(* in all copies or substantial portions of the Software. *) +(* *) +(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) +(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) +(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) +(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) +(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) +(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) +(* DEALINGS IN THE SOFTWARE. *) +(* *) +(*****************************************************************************) + +(** The type of a reveal hash. *) +type t + +(** The hashing schemes supported by the reveal hash. *) +type supported_hashes = Blake2B + +(** A Map module for storing reveal-hash-indexed values. *) +module Map : Map.S with type key = t + +(** [size ~scheme] returns the size of reveal hashes using the [scheme] + specified in input. *) +val size : scheme:supported_hashes -> int + +(** [zero ~scheme] returns the reveal hash corresponding to the zero hash + for the [scheme] specified in input. *) +val zero : scheme:supported_hashes -> t + +(** Formatting function for reveal-hashes. *) +val pp : Format.formatter -> t -> unit + +(** [equal hash1 hash2] checks if the two reveal-hashes [hash1] and [hash2] + are equal. This function must preserve the equality of individual + supported hashing schemes. If [hash1] and [hash2] are hashes obtained + from the same supported hashing scheme, then the [equal] function from + that hashing scheme is used to determine whether they are equivalent. + Otherwise, they are different. *) +val equal : t -> t -> bool + +(** [compare hash1 hash2] compares the values of the reveal hashes [hash1] + and [hash2]. This function must preserve the ordering of individual + supported hashing scheme. If [hash1] and [hash2] are reveal-hashes + obtained from the same hashing scheme, then [compare hash1 hash2] + should return the same result of the compare function exposed + by the hash module corresponding to their hashing scheme. *) +val compare : t -> t -> int + +(* [of_b58check_opt base58_hash] returns the reveal-hash, if any, + whose base58 encoding is [base58_hash]. To avoid ambiguity, + the [S.HASH] modules supported should avoid using the same prefix + for the base58 encoding. *) +val of_b58check_opt : string -> t option + +(* The encoding of reveal hashes. *) +val encoding : t Data_encoding.t + +(* [hash_string ~scheme ?key strings] hashes [strings] using the + supported hashing [scheme] given in input. *) +val hash_string : scheme:supported_hashes -> ?key:string -> string list -> t + +(* [hash_bytes ~scheme ?key strings] hashes [bytes] using the + supported hashing [scheme] given in input. *) +val hash_bytes : scheme:supported_hashes -> ?key:bytes -> bytes list -> t + +(* [to_b58check hash] returns the base58 encoded reveal hash. *) +val to_b58check : t -> string + +(* [scheme_of_hash] hash returns the supported hashing scheme + that was used to obtain [hash]. *) +val scheme_of_hash : t -> supported_hashes diff --git a/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml b/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml index 6ecdcc2c3b98..5d42b790629f 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml @@ -281,7 +281,7 @@ module V2_0_0 = struct | Reveal_required (Wasm_2_0_0.Reveal_raw_data hash) -> ( match Data_encoding.Binary.of_string_opt - Reveal_hash.encoding + Sc_rollup_reveal_hash.encoding (Wasm_2_0_0.reveal_hash_to_string hash) with | Some hash -> Waiting_for_reveal (Reveal_raw_data hash) @@ -289,8 +289,9 @@ module V2_0_0 = struct (* In case of an invalid hash, the rollup is blocked. Any commitment will be invalid. *) Waiting_for_reveal - (Reveal_raw_data (Reveal_hash.zero ~scheme:Reveal_hash.Blake2B)) - ) + (Reveal_raw_data + (Sc_rollup_reveal_hash.zero + ~scheme:Sc_rollup_reveal_hash.Blake2B))) | Reveal_required Wasm_2_0_0.Reveal_metadata -> Waiting_for_reveal Reveal_metadata -- GitLab