diff --git a/src/lib_protocol_environment/environment_V15.ml b/src/lib_protocol_environment/environment_V15.ml index 51e7db2003e16e6c854e9589aa7a0ac46a26f7ad..62db2059bbba178eea62f9f3d2cd068c8f4adfbc 100644 --- a/src/lib_protocol_environment/environment_V15.ml +++ b/src/lib_protocol_environment/environment_V15.ml @@ -1610,4 +1610,70 @@ struct module Merkelized_payload_hashes_hash = Tezos_crypto.Hashed.Smart_rollup_merkelized_payload_hashes_hash end + + module Riscv = struct + module Backend = Octez_riscv_pvm.Backend + module Storage = Octez_riscv_pvm.Storage + + type state = Backend.state + + type proof = Backend.proof + + type output_info = Backend.output_info = { + message_index : Z.t; + outbox_level : Bounded.Non_negative_int32.t; + } + + type output = Backend.output = { + info : output_info; + encoded_message : string; + } + + type output_proof = Backend.output_proof + + type hash = Backend.hash + + type input = Backend.input = + | Inbox_message of int32 * int64 * string + | Reveal of string + + type input_request = Backend.input_request = + | No_input_required + | Initial + | First_after of int32 * int64 + | Needs_reveal of string + + let state_hash state = Backend.state_hash state + + let empty_state () = Storage.empty () + + let proof_start_state proof = Backend.proof_start_state proof + + let proof_stop_state proof = Backend.proof_stop_state proof + + let proof_to_bytes proof = Backend.serialise_proof proof + + let bytes_to_proof bytes = Backend.deserialise_proof bytes + + let install_boot_sector state boot_sector = + Backend.install_boot_sector state boot_sector + + let verify_proof input proof = Backend.verify_proof input proof + + let output_info_of_output_proof output_proof = + Backend.output_info_of_output_proof output_proof + + let state_of_output_proof output_proof = + Backend.state_of_output_proof output_proof + + let verify_output_proof output_proof = + Backend.verify_output_proof output_proof + + let output_proof_to_bytes output_proof = + Backend.serialise_output_proof output_proof + + let bytes_to_output_proof bytes = Backend.deserialise_output_proof bytes + + let get_current_level state = Backend.get_current_level state + end end diff --git a/src/lib_protocol_environment/sigs/v15.in.ml b/src/lib_protocol_environment/sigs/v15.in.ml index a9820536d50e684bc62a89af4dc89566374abf3f..e86731c9cbd01ef9d9827556e9720aae8d280e88 100644 --- a/src/lib_protocol_environment/sigs/v15.in.ml +++ b/src/lib_protocol_environment/sigs/v15.in.ml @@ -142,4 +142,6 @@ module type T = sig module Skip_list : [%sig "v15/skip_list.mli"] module Smart_rollup : [%sig "v15/smart_rollup.mli"] + + module Riscv : [%sig "v15/riscv.mli"] end diff --git a/src/lib_protocol_environment/sigs/v15.ml b/src/lib_protocol_environment/sigs/v15.ml index 973d4e40d4e2530ab92fb762854d0383bc3b78d2..d899e0503a564fe7a7c2220ad16897ee3a002506 100644 --- a/src/lib_protocol_environment/sigs/v15.ml +++ b/src/lib_protocol_environment/sigs/v15.ml @@ -12771,4 +12771,67 @@ module Merkelized_payload_hashes_hash : S.HASH end # 144 "v15.in.ml" + + module Riscv : sig +# 1 "v15/riscv.mli" +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* Copyright (c) 2025 TriliTech *) +(* *) +(*****************************************************************************) + +type state + +type proof + +type output_info = { + message_index : Z.t; + outbox_level : Bounded.Non_negative_int32.t; +} + +type output = {info : output_info; encoded_message : string} + +type output_proof + +type hash = Smart_rollup.State_hash.t + +type input = Inbox_message of int32 * int64 * string | Reveal of string + +type input_request = + | No_input_required + | Initial + | First_after of int32 * int64 + | Needs_reveal of string + +val state_hash : state -> hash + +val empty_state : unit -> state + +val proof_start_state : proof -> hash + +val proof_stop_state : proof -> hash + +val proof_to_bytes : proof -> bytes + +val bytes_to_proof : bytes -> (proof, string) result + +val install_boot_sector : state -> string -> state Lwt.t + +val verify_proof : input option -> proof -> input_request option + +val output_info_of_output_proof : output_proof -> output_info + +val state_of_output_proof : output_proof -> hash + +val verify_output_proof : output_proof -> output option + +val output_proof_to_bytes : output_proof -> bytes + +val bytes_to_output_proof : bytes -> (output_proof, string) result + +val get_current_level : state -> int32 option Lwt.t +end +# 146 "v15.in.ml" + end diff --git a/src/lib_protocol_environment/sigs/v15/riscv.mli b/src/lib_protocol_environment/sigs/v15/riscv.mli new file mode 100644 index 0000000000000000000000000000000000000000..3d99e59d9a2a69b3ab078562cd32908bc190b264 --- /dev/null +++ b/src/lib_protocol_environment/sigs/v15/riscv.mli @@ -0,0 +1,57 @@ +(*****************************************************************************) +(* *) +(* SPDX-License-Identifier: MIT *) +(* Copyright (c) 2025 TriliTech *) +(* *) +(*****************************************************************************) + +type state + +type proof + +type output_info = { + message_index : Z.t; + outbox_level : Bounded.Non_negative_int32.t; +} + +type output = {info : output_info; encoded_message : string} + +type output_proof + +type hash = Smart_rollup.State_hash.t + +type input = Inbox_message of int32 * int64 * string | Reveal of string + +type input_request = + | No_input_required + | Initial + | First_after of int32 * int64 + | Needs_reveal of string + +val state_hash : state -> hash + +val empty_state : unit -> state + +val proof_start_state : proof -> hash + +val proof_stop_state : proof -> hash + +val proof_to_bytes : proof -> bytes + +val bytes_to_proof : bytes -> (proof, string) result + +val install_boot_sector : state -> string -> state Lwt.t + +val verify_proof : input option -> proof -> input_request option + +val output_info_of_output_proof : output_proof -> output_info + +val state_of_output_proof : output_proof -> hash + +val verify_output_proof : output_proof -> output option + +val output_proof_to_bytes : output_proof -> bytes + +val bytes_to_output_proof : bytes -> (output_proof, string) result + +val get_current_level : state -> int32 option Lwt.t diff --git a/src/proto_alpha/lib_protocol/sc_rollup_riscv.ml b/src/proto_alpha/lib_protocol/sc_rollup_riscv.ml index 9065fd987f289951c02e22af57500c0bbf036778..b1de49f57d1d316981036d3ccb4875449f66e4c4 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_riscv.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_riscv.ml @@ -6,125 +6,6 @@ (* *) (*****************************************************************************) -(* TODO RV-374: Move `Riscv_proto_env_sig` and `Riscv_proto_env` to protocol - environment *) -module type Riscv_proto_env_sig = sig - type state - - type proof - - type output_info = { - outbox_level : Bounded.Non_negative_int32.t; - message_index : Z.t; - } - - type output = {info : output_info; encoded_message : string} - - type output_proof - - type hash = Smart_rollup.State_hash.t - - type input = Inbox_message of int32 * int64 * string | Reveal of string - - type input_request = - | No_input_required - | Initial - | First_after of int32 * int64 - | Needs_reveal of string - - val state_hash : state -> hash - - val empty_state : unit -> state - - val proof_start_state : proof -> hash - - val proof_stop_state : proof -> hash - - val proof_to_bytes : proof -> bytes - - val bytes_to_proof : bytes -> (proof, string) result - - val install_boot_sector : state -> string -> state - - val verify_proof : input option -> proof -> input_request option - - val output_info_of_output_proof : output_proof -> output_info - - val state_of_output_proof : output_proof -> hash - - val verify_output_proof : output_proof -> output option - - val output_proof_to_bytes : output_proof -> bytes - - val bytes_to_output_proof : bytes -> (output_proof, string) result - - val get_current_level : state -> int32 option -end - -module Riscv_proto_env : Riscv_proto_env_sig = struct - type state = { - payload : string; - level : Raw_level_repr.t option; - message_counter : Z.t; - tick : Z.t; - } - - type proof = unit - - type output_info = { - outbox_level : Bounded.Non_negative_int32.t; - message_index : Z.t; - } - - type output = {info : output_info; encoded_message : string} - - type output_proof = unit - - type hash = Smart_rollup.State_hash.t - - type input = Inbox_message of int32 * int64 * string | Reveal of string - - type input_request = - | No_input_required - | Initial - | First_after of int32 * int64 - | Needs_reveal of string - - (* In order to synchronise with the node implementation of the PVM at genesis, - * we set the state hash to be the initial state hash of the node - * implementation. *) - let state_hash _state = - Smart_rollup.State_hash.of_b58check_exn - "srs129JscUr3XsPcNFUEiKqVNP38tn8oksbGir1qYXgQs8QD7bcNNd" - - let empty_state () = - {payload = ""; level = None; message_counter = Z.zero; tick = Z.zero} - - let proof_start_state _proof = assert false - - let proof_stop_state _proof = assert false - - let proof_to_bytes _proof = assert false - - let bytes_to_proof _bytes = assert false - - let install_boot_sector state boot_sector = {state with payload = boot_sector} - - let verify_proof _input _proof = assert false - - let output_info_of_output_proof _output_proof = assert false - - let state_of_output_proof _output_proof = assert false - - let verify_output_proof _output_proof = assert false - - let output_proof_to_bytes _output_proof = assert false - - let bytes_to_output_proof _bytes = assert false - - let get_current_level _state = assert false -end - type error += RISCV_proof_production_failed type error += RISCV_proof_verification_failed @@ -166,35 +47,35 @@ let () = module PS = Sc_rollup_PVM_sig -type state = Riscv_proto_env.state +type state = Riscv.state -type proof = Riscv_proto_env.proof +type proof = Riscv.proof -let make_empty_state = Riscv_proto_env.empty_state +let make_empty_state = Riscv.empty_state (** If [None] is returned, converting from raw data encoded strings / PVM raw data to Protocol types failed *) -let from_riscv_input_request (input_request : Riscv_proto_env.input_request) : +let from_riscv_input_request (input_request : Riscv.input_request) : PS.input_request option = let open Option_syntax in match input_request with - | Riscv_proto_env.No_input_required -> return PS.No_input_required - | Riscv_proto_env.Initial -> return PS.Initial - | Riscv_proto_env.First_after (level, index) -> + | Riscv.No_input_required -> return PS.No_input_required + | Riscv.Initial -> return PS.Initial + | Riscv.First_after (level, index) -> let* raw_level = Option.of_result @@ Raw_level_repr.of_int32 level in return @@ PS.First_after (raw_level, Z.of_int64 index) - | Riscv_proto_env.Needs_reveal raw_string -> + | Riscv.Needs_reveal raw_string -> let* reveal_data = Data_encoding.Binary.of_string_opt PS.reveal_encoding raw_string in return @@ PS.Needs_reveal reveal_data (** If [None] is returned, converting from raw data encoded strings / protocol raw data to Riscv types failed *) -let to_riscv_input (input : PS.input) : Riscv_proto_env.input option = +let to_riscv_input (input : PS.input) : Riscv.input option = let open Option_syntax in match input with | PS.Inbox_message {inbox_level; message_counter; payload} -> return - @@ Riscv_proto_env.Inbox_message + @@ Riscv.Inbox_message ( Raw_level_repr.to_int32 inbox_level, Z.to_int64 message_counter, Sc_rollup_inbox_message_repr.unsafe_to_string payload ) @@ -202,10 +83,10 @@ let to_riscv_input (input : PS.input) : Riscv_proto_env.input option = let* raw_reveal_data = Data_encoding.Binary.to_string_opt PS.reveal_data_encoding reveal_data in - return @@ Riscv_proto_env.Reveal raw_reveal_data + return @@ Riscv.Reveal raw_reveal_data (** If [None] is returned, converting from raw data encoded strings / PVM raw data to Protocol types failed *) -let from_riscv_output (output : Riscv_proto_env.output) : PS.output option = +let from_riscv_output (output : Riscv.output) : PS.output option = let open Option_syntax in let* message = Data_encoding.Binary.of_string_opt @@ -225,46 +106,43 @@ let from_riscv_output (output : Riscv_proto_env.output) : PS.output option = module Protocol_implementation : Sc_rollup_PVM_sig.PROTO_VERIFICATION with type context = unit - and type state = Riscv_proto_env.state - and type proof = Riscv_proto_env.proof = struct + and type state = Riscv.state + and type proof = Riscv.proof = struct let parse_boot_sector s = Some s let pp _state = Lwt.return @@ fun fmt () -> Format.pp_print_string fmt "" - type output_proof = Riscv_proto_env.output_proof + type output_proof = Riscv.output_proof - type state = Riscv_proto_env.state + type state = Riscv.state type context = unit type hash = Smart_rollup.State_hash.t - type proof = Riscv_proto_env.proof + type proof = Riscv.proof let proof_encoding : proof Data_encoding.t = let open Data_encoding in - conv_with_guard - Riscv_proto_env.proof_to_bytes - Riscv_proto_env.bytes_to_proof - (bytes Hex) + conv_with_guard Riscv.proof_to_bytes Riscv.bytes_to_proof (bytes Hex) - let proof_start_state state = Riscv_proto_env.proof_start_state state + let proof_start_state state = Riscv.proof_start_state state - let proof_stop_state state = Riscv_proto_env.proof_stop_state state + let proof_stop_state state = Riscv.proof_stop_state state - let state_hash state = Lwt.return @@ Riscv_proto_env.state_hash state + let state_hash state = Lwt.return @@ Riscv.state_hash state let initial_state ~empty = Lwt.return empty let install_boot_sector state boot_sector = - Lwt.return @@ Riscv_proto_env.install_boot_sector state boot_sector + Riscv.install_boot_sector state boot_sector let verify_proof ~is_reveal_enabled:_ input proof = let input_request = let open Option_syntax in let* input = Option.map to_riscv_input input in - let* input_request = Riscv_proto_env.verify_proof input proof in + let* input_request = Riscv.verify_proof input proof in from_riscv_input_request input_request in match input_request with @@ -274,12 +152,12 @@ module Protocol_implementation : let output_proof_encoding = let open Data_encoding in conv_with_guard - Riscv_proto_env.output_proof_to_bytes - Riscv_proto_env.bytes_to_output_proof + Riscv.output_proof_to_bytes + Riscv.bytes_to_output_proof (bytes Hex) let output_info_of_output_proof output_proof : PS.output_info = - let open Riscv_proto_env in + let open Riscv in let {outbox_level; message_index} = output_info_of_output_proof output_proof in @@ -289,13 +167,13 @@ module Protocol_implementation : } let state_of_output_proof output_proof = - Riscv_proto_env.state_of_output_proof output_proof + Riscv.state_of_output_proof output_proof let verify_output_proof output_proof = let open Lwt_result_syntax in let output = let open Option_syntax in - let* output = Riscv_proto_env.verify_output_proof output_proof in + let* output = Riscv.verify_output_proof output_proof in from_riscv_output output in match output with @@ -317,13 +195,8 @@ module Protocol_implementation : dissection) let get_current_level state = - let level = Riscv_proto_env.get_current_level state in - let level = - Option.bind level @@ fun level -> - (* Assuming 1 second / level, this errors after ~65 years i.e. year 2090 *) - match Raw_level_repr.of_int32 level with - | Error _ -> None - | Ok level -> Some level - in - Lwt.return level + let open Lwt_option_syntax in + let* level = Riscv.get_current_level state in + (* Assuming 1 second / level, this errors after ~65 years i.e. year 2090 *) + Lwt.return @@ Option.of_result @@ Raw_level_repr.of_int32 level end diff --git a/src/proto_alpha/lib_protocol/sc_rollup_riscv.mli b/src/proto_alpha/lib_protocol/sc_rollup_riscv.mli index 9ad3d228d5225826a75fc12bfe355ec7690b0e70..7d69dae460eeb233c478a3da6506d6ccada6ea68 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_riscv.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_riscv.mli @@ -6,75 +6,18 @@ (* *) (*****************************************************************************) -(* TODO RV-374: Move `Riscv_proto_env_sig` and `Riscv_proto_env` to protocol - environment *) -module type Riscv_proto_env_sig = sig - type state - - type proof - - type output_info = { - outbox_level : Bounded.Non_negative_int32.t; - message_index : Z.t; - } - - type output = {info : output_info; encoded_message : string} - - type output_proof - - type hash = Smart_rollup.State_hash.t - - type input = Inbox_message of int32 * int64 * string | Reveal of string - - type input_request = - | No_input_required - | Initial - | First_after of int32 * int64 - | Needs_reveal of string - - val state_hash : state -> hash - - val empty_state : unit -> state - - val proof_start_state : proof -> hash - - val proof_stop_state : proof -> hash - - val proof_to_bytes : proof -> bytes - - val bytes_to_proof : bytes -> (proof, string) result - - val install_boot_sector : state -> string -> state - - val verify_proof : input option -> proof -> input_request option - - val output_info_of_output_proof : output_proof -> output_info - - val state_of_output_proof : output_proof -> hash - - val verify_output_proof : output_proof -> output option - - val output_proof_to_bytes : output_proof -> bytes - - val bytes_to_output_proof : bytes -> (output_proof, string) result - - val get_current_level : state -> int32 option -end - -module Riscv_proto_env : Riscv_proto_env_sig - type error += RISCV_proof_verification_failed type error += RISCV_proof_production_failed -type state = Riscv_proto_env.state +type state = Riscv.state -type proof = Riscv_proto_env.proof +type proof = Riscv.proof val make_empty_state : unit -> state module Protocol_implementation : Sc_rollup_PVM_sig.PROTO_VERIFICATION with type context = unit - and type state = Riscv_proto_env.state - and type proof = Riscv_proto_env.proof + and type state = Riscv.state + and type proof = Riscv.proof