123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357(*****************************************************************************)(* *)(* MIT License *)(* Copyright (c) 2022 Nomadic Labs <contact@nomadic-labs.com> *)(* *)(* 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. *)(* *)(*****************************************************************************)moduletypeS=sig(** Plonk Main Protocol *)moduleMP:Distribution.Main_protocol.S(** Type witness for a protocol step. *)type'astep=|S_ctw:[`Commit_to_wires]step|S_ctp:[`Commit_to_plook]step|S_pppi:[`PP_prepare_ids]step|S_ppctt:[`PP_commit_to_t]step|S_ppkeax:[`PP_kzg_eval_at_x]step|S_pcd:[`PC_distribution]step(* All message payloads need to have an index, so that
they can be sorted back by the master. *)typectw_payload={index:int;content:MP.worker_inputsKzg.SMap.t}typectpap_payload={index:int;content:Kzg.Utils.Transcript.t}typepppi_payload={index:int;content:Kzg.Utils.Transcript.t}typeppctt_payload={index:int;content:stringlist*Kzg.Bls.Scalar.t}typeppkeax_payload={index:int;content:Kzg.Utils.Transcript.t}typepcd_payload={index:int;content:MP.PP.PC.worker_msg}type'steprequest=|Commit_to_wires:ctw_payload->[`Commit_to_wires]request|Commit_to_plook:ctpap_payload->[`Commit_to_plook]request|PP_prepare_ids:pppi_payload->[`PP_prepare_ids]request|PP_commit_to_t:ppctt_payload->[`PP_commit_to_t]request|PP_KZG_eval_at_x:ppkeax_payload->[`PP_kzg_eval_at_x]request|PC_Distribution:pcd_payload->[`PC_distribution]requesttypectw_res_payload={index:int;content:MP.commit_to_wires_reply}typectpap_res_payload={index:int;content:MP.commit_to_plook_rc_reply}typepppi_res_payload={index:int;content:stringlist}typeppctt_res_payload={index:int;content:Plonk.Identities.Evaluations.t;}typeppkeax_res_payload={index:int;content:MP.PP.PC.answerlist}typepcd_res_payload={index:int;content:MP.PP.PC.main_prover_msg}type'stepreply=|Commit_to_wires_res:ctw_res_payload->[`Commit_to_wires]reply|Commit_to_plook_res:ctpap_res_payload->[`Commit_to_plook]reply|PP_prepare_ids_res:pppi_res_payload->[`PP_prepare_ids]reply|PP_commit_to_t_res:ppctt_res_payload->[`PP_commit_to_t]reply|PP_KZG_eval_at_x_res:ppkeax_res_payload->[`PP_kzg_eval_at_x]reply|PC_Distribution_res:pcd_res_payload->[`PC_distribution]replytypet=bytesvalrequest_step:'steprequest->'stepstepvalof_request:'arequest->tvalof_reply:'areply->tvalto_request:'stepstep->t->'steprequestoptionvalto_reply:'stepstep->t->'stepreplyoptionvalindex:t->intvalstring_of_message:t->stringendmoduleMake=functor(MP:Distribution.Main_protocol.S)->structmoduleMP=MP(** Type witness for a protocol step. *)type'astep=|S_ctw:[`Commit_to_wires]step|S_ctp:[`Commit_to_plook]step|S_pppi:[`PP_prepare_ids]step|S_ppctt:[`PP_commit_to_t]step|S_ppkeax:[`PP_kzg_eval_at_x]step|S_pcd:[`PC_distribution]step(* All message payloads need to have an index, so that
they can be sorted back by the master. *)typectw_payload={index:int;content:MP.worker_inputsKzg.SMap.t}[@@derivingrepr]typectpap_payload={index:int;content:Kzg.Utils.Transcript.t}[@@derivingrepr]typepppi_payload={index:int;content:Kzg.Utils.Transcript.t}[@@derivingrepr]typeppctt_payload={index:int;content:stringlist*Kzg.Bls.Scalar.t}[@@derivingrepr]typeppkeax_payload={index:int;content:Kzg.Utils.Transcript.t}[@@derivingrepr]typepcd_payload={index:int;content:MP.PP.PC.worker_msg}[@@derivingrepr]type'steprequest=|Commit_to_wires:ctw_payload->[`Commit_to_wires]request|Commit_to_plook:ctpap_payload->[`Commit_to_plook]request|PP_prepare_ids:pppi_payload->[`PP_prepare_ids]request|PP_commit_to_t:ppctt_payload->[`PP_commit_to_t]request|PP_KZG_eval_at_x:ppkeax_payload->[`PP_kzg_eval_at_x]request|PC_Distribution:pcd_payload->[`PC_distribution]requesttypectw_res_payload={index:int;content:MP.commit_to_wires_reply}[@@derivingrepr]typectpap_res_payload={index:int;content:MP.commit_to_plook_rc_reply;}[@@derivingrepr]typepppi_res_payload={index:int;content:stringlist}[@@derivingrepr]typeppctt_res_payload={index:int;content:Plonk.Identities.Evaluations.t;}[@@derivingrepr]typeppkeax_res_payload={index:int;content:MP.PP.PC.answerlist}[@@derivingrepr]typepcd_res_payload={index:int;content:MP.PP.PC.main_prover_msg}[@@derivingrepr]type'stepreply=|Commit_to_wires_res:ctw_res_payload->[`Commit_to_wires]reply|Commit_to_plook_res:ctpap_res_payload->[`Commit_to_plook]reply|PP_prepare_ids_res:pppi_res_payload->[`PP_prepare_ids]reply|PP_commit_to_t_res:ppctt_res_payload->[`PP_commit_to_t]reply|PP_KZG_eval_at_x_res:ppkeax_res_payload->[`PP_kzg_eval_at_x]reply|PC_Distribution_res:pcd_res_payload->[`PC_distribution]reply(* Non-GADT versions of [request] and [reply], used to derive a [Repr.t] *)typef_request=|FCommit_to_wires:ctw_payload->f_request|FCommit_to_plook:ctpap_payload->f_request|FPP_prepare_ids:pppi_payload->f_request|FPP_commit_to_t:ppctt_payload->f_request|FPP_KZG_eval_at_x:ppkeax_payload->f_request|FPC_Distribution:pcd_payload->f_request[@@derivingrepr]letforget_request:typea.arequest->f_request=function|Commit_to_wiresp->FCommit_to_wiresp|Commit_to_plookp->FCommit_to_plookp|PP_prepare_idsp->FPP_prepare_idsp|PP_commit_to_tp->FPP_commit_to_tp|PP_KZG_eval_at_xp->FPP_KZG_eval_at_xp|PC_Distributionp->FPC_Distributionptypef_reply=|FCommit_to_wires_res:ctw_res_payload->f_reply|FCommit_to_plook_res:ctpap_res_payload->f_reply|FPP_prepare_ids_res:pppi_res_payload->f_reply|FPP_commit_to_t_res:ppctt_res_payload->f_reply|FPP_KZG_eval_at_x_res:ppkeax_res_payload->f_reply|FPC_Distribution_res:pcd_res_payload->f_reply[@@derivingrepr]letforget_reply:typea.areply->f_reply=function|Commit_to_wires_resp->FCommit_to_wires_resp|Commit_to_plook_resp->FCommit_to_plook_resp|PP_prepare_ids_resp->FPP_prepare_ids_resp|PP_commit_to_t_resp->FPP_commit_to_t_resp|PP_KZG_eval_at_x_resp->FPP_KZG_eval_at_x_resp|PC_Distribution_resp->FPC_Distribution_resptypemsg=Request:f_request->msg|Reply:f_reply->msg[@@derivingrepr]typet=bytesletof_bytes_exnrb=Result.get_ok@@Repr.(unstage@@Repr.of_bin_stringr)(Bytes.to_stringb)letto_bytesrv=Bytes.of_stringRepr.((unstage@@to_bin_stringr)v)letrequest_step:types.srequest->sstep=function|Commit_to_wires_p->S_ctw|Commit_to_plook_p->S_ctp|PP_prepare_ids_p->S_pppi|PP_commit_to_t_p->S_ppctt|PP_KZG_eval_at_x_p->S_ppkeax|PC_Distribution_p->S_pcdletof_request:'arequest->t=funr->to_bytesmsg_t@@Request(forget_requestr)letof_reply:'areply->t=funr->to_bytesmsg_t@@Reply(forget_replyr)letto_reply:types.sstep->t->sreplyoption=funstept->letm=of_bytes_exnmsg_ttinmatch(step,m)with|S_ctw,Reply(FCommit_to_wires_resp)->Some(Commit_to_wires_resp)|S_ctp,Reply(FCommit_to_plook_resp)->Some(Commit_to_plook_resp)|S_pppi,Reply(FPP_prepare_ids_resp)->Some(PP_prepare_ids_resp)|S_ppctt,Reply(FPP_commit_to_t_resp)->Some(PP_commit_to_t_resp)|S_ppkeax,Reply(FPP_KZG_eval_at_x_resp)->Some(PP_KZG_eval_at_x_resp)|S_pcd,Reply(FPC_Distribution_resp)->Some(PC_Distribution_resp)|_->Noneletto_request:types.sstep->t->srequestoption=funstept->letm=of_bytes_exnmsg_ttinmatch(step,m)with|S_ctw,Request(FCommit_to_wiresp)->Some(Commit_to_wiresp)|S_ctp,Request(FCommit_to_plookp)->Some(Commit_to_plookp)|S_pppi,Request(FPP_prepare_idsp)->Some(PP_prepare_idsp)|S_ppctt,Request(FPP_commit_to_tp)->Some(PP_commit_to_tp)|S_ppkeax,Request(FPP_KZG_eval_at_xp)->Some(PP_KZG_eval_at_xp)|S_pcd,Request(FPC_Distributionp)->Some(PC_Distributionp)|_->Noneletindex:t->int=funt->letm=of_bytes_exnmsg_ttinmatchmwith|Request(FCommit_to_wires{index;_})|Reply(FCommit_to_wires_res{index;_})|Request(FCommit_to_plook{index;_})|Reply(FCommit_to_plook_res{index;_})|Request(FPP_prepare_ids{index;_})|Reply(FPP_prepare_ids_res{index;_})|Request(FPP_commit_to_t{index;_})|Reply(FPP_commit_to_t_res{index;_})|Request(FPP_KZG_eval_at_x{index;_})|Reply(FPP_KZG_eval_at_x_res{index;_})|Request(FPC_Distribution{index;_})|Reply(FPC_Distribution_res{index;_})->indexletstring_of_bytesbytes=letbytes=float_of_intbytesinifbytes<=1024.thenPrintf.sprintf"%3.2f B "byteselseletkilobytes=bytes/.1024.inifkilobytes<=1024.thenPrintf.sprintf"%3.2f KB"kilobyteselseletmegabytes=kilobytes/.1024.inifmegabytes<=1024.thenPrintf.sprintf"%3.2f MB"megabyteselseletgigabytes=megabytes/.1024.inPrintf.sprintf"%.2f GB"gigabytesletstring_of_message:t->string=funt->letm=of_bytes_exnmsg_ttinmatchmwith|Request(FCommit_to_wires{index;content=_})->Format.sprintf"Commit_to_wires: %d (%s)"index(string_of_bytes@@Bytes.lengtht)|Reply(FCommit_to_wires_res{index;content=_})->Format.sprintf"Commit_to_wires_res: %d (%s)"index(string_of_bytes@@Bytes.lengtht)|Request(FCommit_to_plook{index;content=_})->Format.sprintf"Commit_to_plook: %d (%s)"index(string_of_bytes@@Bytes.lengtht)|Reply(FCommit_to_plook_res{index;content=_})->Format.sprintf"Commit_to_plook_res: %d (%s)"index(string_of_bytes@@Bytes.lengtht)|Request(FPP_prepare_ids{index;content=_})->Format.sprintf"PP_prepare_ids: %d (%s)"index(string_of_bytes@@Bytes.lengtht)|Reply(FPP_prepare_ids_res{index;content=_})->Format.sprintf"PP_prepare_ids_res: %d (%s)"index(string_of_bytes@@Bytes.lengtht)|Request(FPP_commit_to_t{index;content=_})->Format.sprintf"PP_commit_to_t: %d (%s)"index(string_of_bytes@@Bytes.lengtht)|Reply(FPP_commit_to_t_res{index;content=_})->Format.sprintf"PP_commit_to_t_res: %d (%s)"index(string_of_bytes@@Bytes.lengtht)|Request(FPP_KZG_eval_at_x{index;content=_})->Format.sprintf"PP_KZG_eval_at_x: %d (%s)"index(string_of_bytes@@Bytes.lengtht)|Reply(FPP_KZG_eval_at_x_res{index;content=_})->Format.sprintf"PP_KZG_eval_at_x_res: %d (%s)"index(string_of_bytes@@Bytes.lengtht)|Request(FPC_Distribution{index;content=_})->Format.sprintf"PC_Distribution: %d (%s)"index(string_of_bytes@@Bytes.lengtht)|Reply(FPC_Distribution_res{index;content=_})->Format.sprintf"PC_Distribution_res: %d (%s)"index(string_of_bytes@@Bytes.lengtht)end