123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)openCommunicationmoduletypeS=sigmoduleMsg:Message.SmoduleD:Distributed_wrapper.Enriched_processwithtype'aM.step='aMsg.stepandtype'aM.request='aMsg.requestandtype'aM.reply='aMsg.replyandtype'aio='aLwt.tvalworker_proc:string->Distributed.Process_id.t->unit->'aD.tendmoduleMake(Main:Distribution.Main_protocol.S):S=structmoduleMsg=Message.Make(Main)moduleD=Distributed_wrapper.Make(Msg)openMain(**
Worker loop.
Implements the worker side of the proving protocol.
*)letworker_procpp_filemain_pid()=letopenDinlet*()=lift_io@@Lwt_io.printlf"Spawn"inlet*fd=lift_ioLwt_io.(open_file~mode:Inputpp_file)inlet*()=lift_io@@Lwt_io.printlf"Open PP file"inlet*s=lift_io@@Lwt_io.readfdinlet*()=lift_io@@Lwt_io.printlf"Read PP file"inletpp=Plompiler.Utils.of_bytesprover_public_parameters_t(Bytes.of_strings)inlet*()=lift_io@@Lwt_io.closefdinlet*()=lift_io@@Lwt_io.printlf"Loaded PP\n"inletrecloop()=let*{all_f_wires;wires_list_map;inputs_map;shifts_map;cm_aux_wires;_;}=handle_requestmain_pid~step:Msg.S_ctw~handler:(fun(Msg.Commit_to_wires{index;content})->Some(fun()->letcontent,rem=worker_commit_to_wiresppcontentinreturn(Msg.Commit_to_wires_res{index;content},rem)))inlet*{f_map;_},{beta;gamma}=handle_requestmain_pid~step:Msg.S_ctp~handler:(fun(Msg.Commit_to_plook{index;content})->Some(fun()->letreply,rem=commit_to_plook_rcppshifts_mapcontentwires_list_mapinreturn(Msg.Commit_to_plook_res{index;content=reply},(reply,rem))))inletevaluations=Prover.build_evaluationspp(Kzg.SMap.union_disjointall_f_wiresf_map)inletidentities=Prover.build_gates_plook_rc1_identities~shifts_mappp{beta;gamma;delta=Kzg.Bls.Scalar.zero}inputs_mapinletevaluated_ids=identitiesevaluationsinletids_keys=Kzg.SMap.bindingsevaluated_ids|>List.mapfstinlet*_transcript=handle_requestmain_pid~step:Msg.S_pppi~handler:(fun(Msg.PP_prepare_ids{index;content=transcript})->Some(fun()->return(Msg.PP_prepare_ids_res{index;content=ids_keys},transcript)))inlet*()=handle_requestmain_pid~step:Msg.S_ppctt~handler:(fun(Msg.PP_commit_to_t{index;content=all_ids_keys,alpha})->Some(fun()->letbatched_ids=batch_evaluated_ids~alphaevaluated_idsall_ids_keysinreturn(Msg.PP_commit_to_t_res{index;content=batched_ids},())))inletgenerator,_,_=get_gen_n_nbtppinlet*()=handle_requestmain_pid~step:Msg.S_ppkeax~handler:(fun(Msg.PP_KZG_eval_at_x{index;content=transcript})->Some(fun()->letsecrets_worker=[(all_f_wires,cm_aux_wires)]inletr=kzg_eval_at_xpptranscriptsecrets_workergeneratorinreturn(Msg.PP_KZG_eval_at_x_res{index;content=r},())))inlet*()=(* The next two functions are used to pad the query to
[distributed_prover_worker] with dummy values in the positions that
the worker does not have information about, namely, those of:
t_map, g_map, plook_map, which are ATM handled by the main thread *)letpad_secretsl=(List.init3@@Fun.constKzg.SMap.empty)@linletpad_prover_auxl=(List.init3@@Fun.constPP.PC.Commitment.empty_prover_aux)@linhandle_requestmain_pid~step:Msg.S_pcd~handler:(function|Msg.PC_Distribution{index;content=worker_msg}->Some(fun()->letsecrets=pad_secrets[all_f_wires]inletprover_aux=pad_prover_aux[cm_aux_wires]inletprvr_main_msg=PP.PC.distributed_prove_workersecretsprover_auxworker_msginreturn(Msg.PC_Distribution_res{index;content=prvr_main_msg},())))inloop()inloop()end