123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2021 Nomadic Labs <contact@nomadic-labs.com> *)(* Copyright (c) 2022 Trili Tech, <contact@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. *)(* *)(*****************************************************************************)(** This module introduces the semantics of Proof-generating Virtual Machines.
A PVM defines an operational semantics for some computational
model. The specificity of PVMs, in comparison with standard virtual
machines, is their ability to generate and to validate a *compact*
proof that a given atomic execution step turned a given state into
another one.
In the smart-contract rollups, PVMs are used for two purposes:
- They allow for the externalization of rollup execution by
completely specifying the operational semantics of a given
rollup. This standardization of the semantics gives a unique and
executable source of truth about the interpretation of
smart-contract rollup inboxes, seen as a transformation of a rollup
state.
- They allow for the validation or refutation of a claim that the
processing of some messages led to a given new rollup state (given
an actual source of truth about the nature of these messages).
*)openSc_rollup_repr(** An input to a PVM is the [message_counter] element of an inbox at
a given [inbox_level] and contains a given [payload]. *)typeinput={inbox_level:Raw_level_repr.t;message_counter:Z.t;payload:string;}letinput_encoding=letopenData_encodinginconv(fun{inbox_level;message_counter;payload}->(inbox_level,message_counter,payload))(fun(inbox_level,message_counter,payload)->{inbox_level;message_counter;payload})(obj3(req"inbox_level"Raw_level_repr.encoding)(req"message_counter"n)(req"payload"string))letinput_equal(a:input)(b:input):bool=let{inbox_level;message_counter;payload}=ain(* To be robust to the addition of fields in [input] *)Raw_level_repr.equalinbox_levelb.inbox_level&&Z.equalmessage_counterb.message_counter&&String.equalpayloadb.payload(** The PVM's current input expectations. [No_input_required] is if the
machine is busy and has no need for new input. [Initial] will be if
the machine has never received an input so expects the very first
item in the inbox. [First_after (level, counter)] will expect
whatever comes next after that position in the inbox. *)typeinput_request=|No_input_required|Initial|First_afterofRaw_level_repr.t*Z.tletinput_request_encoding=letopenData_encodinginunion~tag_size:`Uint8[case~title:"No_input_required"(Tag0)(obj1(req"no_input_required"unit))(functionNo_input_required->Some()|_->None)(fun()->No_input_required);case~title:"Initial"(Tag1)(obj1(req"initial"unit))(functionInitial->Some()|_->None)(fun()->Initial);case~title:"First_after"(Tag2)(tup2Raw_level_repr.encodingn)(function|First_after(level,counter)->Some(level,counter)|_->None)(fun(level,counter)->First_after(level,counter));]letpp_input_requestfmtrequest=matchrequestwith|No_input_required->Format.fprintffmt"No_input_required"|Initial->Format.fprintffmt"Initial"|First_after(l,n)->Format.fprintffmt"First_after (level = %a, counter = %a)"Raw_level_repr.pplZ.pp_printnletinput_request_equalab=match(a,b)with|No_input_required,No_input_required->true|No_input_required,_->false|Initial,Initial->true|Initial,_->false|First_after(l,n),First_after(m,o)->Raw_level_repr.equallm&&Z.equalno|First_after_,_->falsetypeoutput={outbox_level:Raw_level_repr.t;message_index:Z.t;message:Sc_rollup_outbox_message_repr.t;}letoutput_encoding=letopenData_encodinginconv(fun{outbox_level;message_index;message}->(outbox_level,message_index,message))(fun(outbox_level,message_index,message)->{outbox_level;message_index;message})(obj3(req"outbox_level"Raw_level_repr.encoding)(req"message_index"n)(req"message"Sc_rollup_outbox_message_repr.encoding))letpp_outputfmt{outbox_level;message_index;message}=Format.fprintffmt"@[%a@;%a@;%a@;@]"Raw_level_repr.ppoutbox_levelZ.pp_printmessage_indexSc_rollup_outbox_message_repr.ppmessagemoduletypeS=sig(**
The state of the PVM denotes a state of the rollup.
We classify states into two categories: "internal states" do
not require any external information to be executed while
"input states" are waiting for some information from the
inbox to be executable.
*)typestate(** A state is initialized in a given context. A [context]
represents the executable environment needed by the state to
exist. Typically, the rollup node storage can be part of this
context to allow the PVM state to be persistent. *)typecontext(** A [hash] characterizes the contents of a state. *)typehash=State_hash.t(** During interactive refutation games, a player may need to
provide a proof that a given execution step is valid. The PVM
implementation is responsible for ensuring that this proof type
has the correct semantics:
A proof [p] has four parameters:
[start_hash := proof_start_state p]
[stop_hash := proof_stop_state p]
[input_requested := proof_input_requested p]
[input_given := proof_input_given p]
The following predicate must hold of a valid proof:
[exists start_state, stop_state.
(state_hash start_state == start_hash)
AND (Option.map state_hash stop_state == stop_hash)
AND (is_input_state start_state == input_requested)
AND (match (input_given, input_requested) with
| (None, No_input_required) -> eval start_state == stop_state
| (None, Initial) -> stop_state == None
| (None, First_after (l, n)) -> stop_state == None
| (Some input, No_input_required) -> true
| (Some input, Initial) ->
set_input input_given start_state == stop_state
| (Some input, First_after (l, n)) ->
set_input input_given start_state == stop_state)]
In natural language---the two hash parameters [start_hash] and
[stop_hash] must have actual [state] values (or possibly [None] in
the case of [stop_hash]) of which they are the hashes. The
[input_requested] parameter must be the correct request from the
[start_hash], given according to [is_input_state]. Finally there
are four possibilities of [input_requested] and [input_given].
- if no input is required, or given, the proof is a simple [eval]
step ;
- if input was required but not given, the [stop_hash] must be
[None] (the machine is blocked) ;
- if no input was required but some was given, this makes no sense
and it doesn't matter if the proof is valid or invalid (this
case will be ruled out by the inbox proof anyway) ;
- finally, if input was required and given, the proof is a
[set_input] step. *)typeproof(** [proof]s are embedded in L1 refutation game operations using
[proof_encoding]. Given that the size of L1 operations are
limited, it is of *critical* importance to make sure that
no execution step of the PVM can generate proofs that do not
fit in L1 operations when encoded. If such a proof existed,
the rollup could get stuck. *)valproof_encoding:proofData_encoding.t(** [proof_start_state proof] returns the initial state hash of the
[proof] execution step. *)valproof_start_state:proof->hash(** [proof_stop_state proof] returns the final state hash of the
[proof] execution step. *)valproof_stop_state:proof->hashoption(** [proof_input_requested proof] returns the [input_request] status
of the start state of the proof, as given by [is_input_state].
This must match with the inbox proof to complete a valid
refutation game proof. *)valproof_input_requested:proof->input_request(** [proof_input_given proof] returns the [input], if any, provided to
the start state of the proof using the [set_input] function. If
[None], the proof is an [eval] step instead, or the machine is
blocked because the inbox is fully read. This must match with the
inbox proof to complete a valid refutation game proof. *)valproof_input_given:proof->inputoption(** [state_hash state] returns a compressed representation of [state]. *)valstate_hash:state->hashLwt.t(** [initial_state context boot_sector] is the initial state of the PVM,
which is a pure function of [boot_sector].
The [context] argument is required for technical reasons and does
not impact the result. *)valinitial_state:context->string->stateLwt.t(** [is_input_state state] returns the input expectations of the
[state]---does it need input, and if so, how far through the inbox
has it read so far? *)valis_input_state:state->input_requestLwt.t(** [set_input (level, n, msg) state] sets [msg] in [state] as
the next message to be processed. This input message is assumed
to be the number [n] in the inbox messages at the given
[level]. The input message must be the message next to the
previous message processed by the rollup. *)valset_input:input->state->stateLwt.t(** [eval s0] returns a state [s1] resulting from the
execution of an atomic step of the rollup at state [s0]. *)valeval:state->stateLwt.t(** This checks the proof. See the doc-string for the [proof] type. *)valverify_proof:proof->boolLwt.t(** [produce_proof ctxt input_given state] should return a [proof] for
the PVM step starting from [state], if possible. This may fail for
a few reasons:
- the [input_given] doesn't match the expectations of [state] ;
- the [context] for this instance of the PVM doesn't have access
to enough of the [state] to build the proof. *)valproduce_proof:context->inputoption->state->(proof,error)resultLwt.t(** The following type is inhabited by the proofs that a given [output]
is part of the outbox of a given [state]. *)typeoutput_proof(** [output_proof_encoding] encoding value for [output_proof]s. *)valoutput_proof_encoding:output_proofData_encoding.t(** [output_of_output_proof proof] returns the [output] that is
referred to in [proof]'s statement. *)valoutput_of_output_proof:output_proof->output(** [state_of_output_proof proof] returns the [state] hash that is
referred to in [proof]'s statement. *)valstate_of_output_proof:output_proof->hash(** [verify_output_proof output_proof] returns [true] iff [proof] is
a valid witness that its [output] is part of its [state]'s outbox. *)valverify_output_proof:output_proof->boolLwt.t(** [produce_output_proof ctxt state output] returns a proof
that witnesses the fact that [output] is part of [state]'s
outbox. *)valproduce_output_proof:context->state->output->(output_proof,error)resultLwt.tend