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 983c1ef176cc61958cd2f5c4791ca64bde89424b..d44edd6e42a8822ecd51021af94c06570ecc7db0 100644 --- a/src/proto_alpha/bin_sc_rollup_node/RPC_server.ml +++ b/src/proto_alpha/bin_sc_rollup_node/RPC_server.ml @@ -277,9 +277,11 @@ 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 - Sc_rollup.Reveal_hash.Map.add hash page map) - Sc_rollup.Reveal_hash.Map.empty + 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 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 2add615ac90d7b89df38db7a76ccd0d19a41ca64..50a25ce43a60282d265062c4284242939334d936 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 8fab92e9619313553c985fe413d592f92077d438..e09a73676768eb884ea9bec3a737584ee1607583 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 8642a5da81542af2ebbde089eb100e0c32aa4440..436d42bb9c44f77598fa1ed9e387a45bd54831a6 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} @@ -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 @@ -250,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 bdffe3bc9618a032506621e207f955757587046c..19debadd0b1ea7850747659a482d0b1b92f4901a 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 e0b7fe4845deb17e9f985c517095bb3ead1fe1fc..8f64a279f327d9c489edc84599c3a47767b4fc1d 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 1f95fe0c57678a49550e0a236e739a31956973d0..4f559f6862b46b1e48cdea4212c79e53becaff97 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 f0a5b0515adf83d97ae85c80189ee81c303f28ec..dc32c2bdd5a56761e0d2515622c931e026614dd5 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 be644c893f68664e39b4f8b553369df679a79617..a225a5a4f2ca6ed573d1c6bb14419f95fc07535b 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 += @@ -158,7 +157,13 @@ 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 : sig + include Dac_preimage_data_manager.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 +177,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 +291,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 +364,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_dal/dac_preimage_data_manager.ml b/src/proto_alpha/lib_dal/dac_preimage_data_manager.ml index 5bcd8da48b32bd8cfceb6bfb4239ffe4937ba5e8..1cd86db1b40576cfb77daac7047cc3df61b5f5fe 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 : Environment.S.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 : Environment.S.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 e5ca1d133149dce9edb6e7fc60111863fed603ab..5c50ab259ca1ba4989731b4e82b739531e2e7c4c 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 @@ -237,6 +236,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 +482,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; ] diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index b679da82972d5d7dfbbba3a4e3d93667cadacd14..a3c629dcaf16eeb0f7a065d5cdf97617cc379b98 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 4014a7a24ee5eff4f9163991836772f036a3566b..344ed7a77746300d5627c54a56f5f613a0ee80c9 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -3269,10 +3269,8 @@ module Sc_rollup : sig module Input_hash : S.HASH - module Reveal_hash : S.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 @@ -3799,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 4068f716b5992e0d2f960de4a3bb3b5af96936de..739b8eaa7af8ffeee34d3e54945c076ca9d0aaab 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 7a044fbb0905270ae9d1366d5357deb74cb2d7dd..618f9f00a5d70cab89c223f64fe7649f3e5df545 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_PVM_sig.ml @@ -202,45 +202,8 @@ module Input_hash = let size = Some 20 end) -module Reveal_hash = struct - (* Reserve the first byte in the encoding to support multi-versioning - in the future. *) - module V0 = struct - include - Blake2B.Make - (Base58) - (struct - let name = "Sc_rollup_reveal_data_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 - - include V0 - - let encoding = - let open Data_encoding in - union - ~tag_size:`Uint8 - [ - case - ~title:"Reveal_data_hash_v0" - (Tag 0) - V0.encoding - (fun s -> Some s) - (fun s -> s); - ] -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 @@ -252,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 = @@ -333,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 @@ -356,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 48dd7216682b01f8ee731ff075957fe66c0254df..7682ab3f10e3506db8809c934f46eb295b38f4d7 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 75e8c46e0f345a4ce94e7778d3deedc12ff15845..809651adfb0c9befaa07a1dc309c3b129fb6661c 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_proof_repr.ml @@ -328,9 +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 data_hash = Sc_rollup_PVM_sig.Reveal_hash.hash_string [data] in + let scheme = Sc_rollup_reveal_hash.scheme_of_hash expected_hash 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 @@ -356,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 652b28109f4e916a682f42af61c897f6d53b747f..38e04326097fb7c09c531d0629381518f8b87805 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 0000000000000000000000000000000000000000..fb378ecc5d026fae52cef5398ce0b68e8bd9b403 --- /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 0000000000000000000000000000000000000000..084e09fc4e993b6980d68764811e302b28c76fcb --- /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 330e67005462ed1ee257a7d099e6a277b2d9a594..5d42b790629f3d7b70bde2830c839c67ce7a0e75 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_wasm.ml @@ -281,14 +281,17 @@ 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) | 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 + (Sc_rollup_reveal_hash.zero + ~scheme:Sc_rollup_reveal_hash.Blake2B))) | Reveal_required Wasm_2_0_0.Reveal_metadata -> Waiting_for_reveal Reveal_metadata