123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)openPlonk.BlsopenPlonk.UtilsopenPlonk.IdentitiesmoduleSMap=Plonk.SMapmoduletypeS=sigmodulePC:Polynomial_commitment.SmoduleAnswers_commitment:Plonk.Input_commitment.SincludePlonk.Polynomial_protocol.SwithmodulePC:=PC(** Auxiliary information needed by the prover for the meta-verification in
aPlonK *)typeprover_aux={answers:Scalar.tSMap.tSMap.tlist;batch:Scalar.tSMap.tlist;alpha:Scalar.t;x:Scalar.t;r:Scalar.t;cms_answers:Answers_commitment.tSMap.t;t_answers:Scalar.tlist;}(** Auxiliary information needed by the verifier for the meta-verification in
aPlonK *)typeverifier_aux={alpha:Scalar.t;x:Scalar.t;r:Scalar.t}valupdate_transcript_with_formatted_answers:transcript->(Scalar.tSMap.tSMap.tlist->Answers_commitment.t)SMap.t->Scalar.tSMap.tSMap.tlist->Scalar.tlist*Answers_commitment.tSMap.t*transcriptvalprove_super_aggregation:prover_public_parameters->transcript->commit_to_answers_map:(Scalar.tSMap.tSMap.tlist->Answers_commitment.t)SMap.t->n:int->generator:Scalar.t->secrets:(Poly.tSMap.t*PC.Commitment.prover_aux)list->eval_points:eval_pointlistlist->evaluations:Evaluations.tSMap.t->identities:prover_identities->nb_of_t_chunks:int->(proof*prover_aux)*transcriptvalverify_super_aggregation:verifier_public_parameters->transcript->n:int->generator:Scalar.t->commitments:PC.Commitment.tlist->eval_points:eval_pointlistlist->s_list:Scalar.tSMap.tlist->cms_answers:Answers_commitment.publicSMap.t->t_answers:Scalar.tlist->ids_batch:(Scalar.t*int)SMap.t->proof->(bool*verifier_aux)*PC.transcriptendmoduleMake_impl(Super_PC:Polynomial_commitment.S)(Answers_commitment:Plonk.Input_commitment.S)=structincludePlonk.Polynomial_protocol.Make_impl(Super_PC)modulePC=Super_PCmoduleAnswers_commitment=Answers_commitmenttypeprover_aux={answers:Scalar.tSMap.tSMap.tlist;batch:Scalar.tSMap.tlist;alpha:Scalar.t;x:Scalar.t;r:Scalar.t;cms_answers:Answers_commitment.tSMap.t;t_answers:Scalar.tlist;}typeverifier_aux={alpha:Scalar.t;x:Scalar.t;r:Scalar.t}(* Expand the transcript with all answers. Circuit answers are involved
in commiment form, whereas t_answers are included in the clear, given
that they are exposed in the proof *)letupdate_transcript_with_answerstranscriptcms_answerst_answers=SMap.fold(funcircuit_namecm_answerstranscript->transcript|>Transcript.expandRepr.stringcircuit_name|>Transcript.expandAnswers_commitment.public_tcm_answers)cms_answerstranscript|>Transcript.expand(Repr.listScalar.t)t_answersletverify_t~n~x~alpha~t_answers~ids_batch=letsum_ids,_=SMap.fold(fun_(id_batch,size)(acc,acc_alpha)->Scalar.(acc+(id_batch*acc_alpha),acc_alpha*powalpha(Z.of_intsize)))ids_batch(Scalar.zero,Scalar.one)inverify_tnxsum_idst_answersletupdate_transcript_with_formatted_answerstranscriptcm_to_answers_mapanswer_list=lett_answers,circuit_answers=(List.hdanswer_list,List.tlanswer_list)inlett_answers=t_answers|>SMap.values|>List.concat_mapSMap.valuesinletcms_answers=SMap.mapi(funcircuit_namecommit->List.map(SMap.Aggregation.select_answers_by_circuitcircuit_name)circuit_answers|>commit)cm_to_answers_mapin(* We treat t answers as Public inputs instead of through the
input_commitment because they are not circuit-specific *)lettranscript=update_transcript_with_answerstranscript(SMap.mapAnswers_commitment.(funa->a.public)cms_answers)t_answersin(t_answers,cms_answers,transcript)letprove_super_aggregationpc_public_parameterstranscript~commit_to_answers_map~n~generator~secrets~eval_points~evaluations~identities~nb_of_t_chunks=let((alpha,x,answer_list,cm_t),polys_list,prover_aux_list,query_list,transcript)=prove_auxpc_public_parameterstranscriptngeneratorsecretseval_pointsevaluationsidentitiesnb_of_t_chunksinlett_answers,cms_answers,transcript=update_transcript_with_formatted_answerstranscriptcommit_to_answers_mapanswer_listinlet(pc_proof,Super_PC.{r;s_list}),transcript=Super_PC.prove_super_aggregationpc_public_parameterstranscriptpolys_listprover_aux_listquery_listanswer_listin(({cm_t;pc_proof;pc_answers=[]},{answers=answer_list;cms_answers;batch=s_list;alpha;x;r;t_answers;}),transcript)letverify_super_aggregationpc_public_parameterstranscript~n~generator~commitments~eval_points~s_list~cms_answers~t_answers~ids_batchproof=letalpha,x,transcript,cmts,query_list=verify_auxtranscriptgeneratorcommitmentseval_pointsproofinlettranscript=update_transcript_with_answerstranscriptcms_answerst_answersin(* Step 2a: KZG.verify proofs for witness combinations *)letpc_verif,r,transcript=Super_PC.verify_super_aggregationpc_public_parameterstranscriptcmtsquery_lists_listproof.pc_proofinletpp_verif=verify_t~n~x~alpha~t_answers~ids_batchin((pc_verif&&pp_verif,{alpha;x;r}),transcript)endmoduleMake_aggregation:functor(PC:Polynomial_commitment.S)(Answers_commitment:Plonk.Input_commitment.S)->SwithmoduleAnswers_commitment=Answers_commitmentwithmodulePC=PC=Make_implmoduleKZG_Answers_commitment=Plonk.Input_commitment.Make(Plonk.Polynomial_commitment.Commitment)includeMake_aggregation(Polynomial_commitment)(KZG_Answers_commitment)