package octez-libs

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file main_protocol.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
(*****************************************************************************)
(*                                                                           *)
(* 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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Kzg.Bls
open Kzg.Utils
module SMap = Kzg.SMap

module type S = sig
  module PP : Polynomial_protocol.S

  include Plonk.Main_protocol.S

  module Gates : Plonk.Custom_gates.S

  module Perm : Plonk.Permutation_gate.S with module PP := PP

  module RangeCheck : Plonk.Range_check_gate.S with module PP := PP

  (** Returns (g, n), where n is the size of the circuit padded to the next
      power of two & g is a primitive n-th root of unity
   *)
  val get_gen_n_prover : prover_public_parameters -> scalar * int

  (** Returns (g, n), where n is the size of the circuit padded to the next
      power of two & g is a primitive n-th root of unity
   *)
  val get_gen_n_verifier : verifier_public_parameters -> scalar * int

  val filter_prv_pp_circuits :
    prover_public_parameters -> 'a SMap.t -> prover_public_parameters

  val filter_vrf_pp_circuits :
    verifier_public_parameters -> 'a SMap.t -> verifier_public_parameters

  (** Auxiliary information needed by the prover for the meta-verification in
      aPlonK *)
  type prover_aux = {
    answers : scalar SMap.t SMap.t list;
    batch : scalar SMap.t list;
    alpha : scalar;
    beta : scalar;
    gamma : scalar;
    delta : scalar;
    x : scalar;
    r : scalar;
    cms_answers : PP.Answers_commitment.t SMap.t;
    cms_pi : PP.Answers_commitment.t SMap.t;
    ids_batch : (scalar * int) SMap.t;
    t_answers : scalar list;
  }

  (** Auxiliary information needed by the verifier for the meta-verification in
      aPlonK *)
  type verifier_aux = {
    alpha : scalar;
    beta : scalar;
    gamma : scalar;
    delta : scalar;
    x : scalar;
    r : scalar;
  }

  type input_commit_funcs = {
    pi : scalar array -> PP.Answers_commitment.t;
    answers : Scalar.t SMap.t SMap.t list -> PP.Answers_commitment.t;
  }

  val prove_list :
    prover_public_parameters ->
    input_commit_funcs:input_commit_funcs SMap.t ->
    inputs:prover_inputs ->
    proof * prover_aux

  val verify_list :
    verifier_public_parameters ->
    proof
    * scalar SMap.t list
    * PP.Answers_commitment.public SMap.t
    * PP.Answers_commitment.public SMap.t
    * scalar list
    * (scalar * int) SMap.t ->
    bool * verifier_aux
end

module Make_impl (Super_PP : Polynomial_protocol.S) = struct
  include Plonk.Main_protocol.Make_impl (Super_PP)
  module Input_commitment = Input_commitment
  module PP = Super_PP

  type prover_aux = {
    answers : scalar SMap.t SMap.t list;
    batch : scalar SMap.t list;
    alpha : scalar;
    beta : scalar;
    gamma : scalar;
    delta : scalar;
    x : scalar;
    r : scalar;
    cms_answers : PP.Answers_commitment.t SMap.t;
    cms_pi : PP.Answers_commitment.t SMap.t;
    ids_batch : (scalar * int) SMap.t;
    t_answers : scalar list;
  }

  type verifier_aux = {
    alpha : scalar;
    beta : scalar;
    gamma : scalar;
    delta : scalar;
    x : scalar;
    r : scalar;
  }

  type input_commit_funcs = {
    pi : scalar array -> PP.Answers_commitment.t;
    answers : Scalar.t SMap.t SMap.t list -> PP.Answers_commitment.t;
  }

  let commit_pi (pp : prover_public_parameters) ic_funcs inputs =
    (* TODO: can we commit only to the hidden pi?*)
    let pi_infos =
      SMap.mapi
        (fun circuit_name inputs_list ->
          let commit = (SMap.find circuit_name ic_funcs).pi in
          let c = SMap.find circuit_name pp.circuits_map in
          let ic_size = List.fold_left ( + ) 0 c.input_com_sizes in
          let pi =
            List.map
              (fun s -> Array.sub s.witness ic_size c.public_input_size)
              inputs_list
          in
          ( {nb_proofs = List.length inputs_list; public = pi; commitments = []},
            commit @@ Array.concat pi ))
        inputs
    in
    (SMap.map fst pi_infos, SMap.map snd pi_infos)

  let compute_ids_batch (pp : prover_public_parameters) rd alpha x
      public_inputs_map answers cms_answers =
    let n_gen = (pp.common_pp.n, Domain.get pp.common_pp.domain 1) in
    let identities =
      Verifier.build_identities
        (SMap.map
           Verifier.circuit_verifier_pp_of_circuit_prover_pp
           pp.circuits_map)
        n_gen
        rd
        public_inputs_map
    in
    let merged_answers =
      let f _key m1 m2 = Some (SMap.union_disjoint m1 m2) in
      List.fold_left (SMap.union f) SMap.empty answers
    in
    let evaluated_ids = identities x merged_answers in
    SMap.mapi
      (fun circuit_name _ ->
        let ids =
          SMap.Aggregation.filter_by_circuit_name circuit_name evaluated_ids
        in
        (Fr_generation.batch alpha (SMap.values ids), SMap.cardinal ids))
      (* we use cms_answers cause it contains the circuit_names *)
      cms_answers

  let update_transcript_with_pi transcript cms_pi =
    SMap.fold
      (fun _ cm_pi transcript ->
        PP.Answers_commitment.(Transcript.expand public_t cm_pi) transcript)
      cms_pi
      transcript

  let update_prv_pp_transcript_with_pi (pp : prover_public_parameters) cms_pi =
    {
      pp with
      transcript =
        update_transcript_with_pi
          pp.transcript
          (SMap.map PP.Answers_commitment.(fun a -> a.public) cms_pi);
    }

  let prove_list (pp : prover_public_parameters) ~input_commit_funcs ~inputs =
    let public_inputs_map, cms_pi = commit_pi pp input_commit_funcs inputs in
    (* add the PI in the transcript *)
    let pp = update_prv_pp_transcript_with_pi pp cms_pi in
    let ( ( pp_proof,
            Super_PP.{answers; batch; alpha; x; r; cms_answers; t_answers} ),
          (perm_and_plook, wires_cm, rd) ) =
      Prover.prove_parameters
        ~pp_prove:
          (Super_PP.prove_super_aggregation
             ~commit_to_answers_map:
               (SMap.map (fun f -> f.answers) input_commit_funcs))
        pp
        ~inputs_map:inputs
    in
    let ids_batch =
      compute_ids_batch pp rd alpha x public_inputs_map answers cms_answers
    in
    ( {perm_and_plook; wires_cm; pp_proof},
      {
        answers;
        batch;
        alpha;
        beta = rd.beta;
        gamma = rd.gamma;
        delta = rd.delta;
        x;
        r;
        cms_answers;
        cms_pi;
        ids_batch;
        t_answers;
      } )

  let verify_list pp (proof, s_list, cms_answers, cms_pi, t_answers, ids_batch)
      =
    (* add the PI in the transcript *)
    let transcript = update_transcript_with_pi pp.transcript cms_pi in
    let transcript, _, rd, commitments, eval_points =
      (* Note that we don’t care about inputs here, because verify_parameters
         only cares about input_coms & identities that we don’t have here *)
      Verifier.verify_parameters
        ((pp.common_pp, pp.circuits_map), transcript)
        SMap.empty
        proof
    in
    let (kzg_verif, Super_PP.{alpha; x; r}), _transcript =
      Super_PP.verify_super_aggregation
        pp.common_pp.pp_public_parameters
        transcript
        ~n:pp.common_pp.n
        ~generator:pp.common_pp.generator
        ~commitments
        ~eval_points
        ~s_list
        ~cms_answers
        ~t_answers
        ~ids_batch
        proof.pp_proof
    in
    ( kzg_verif,
      {alpha; beta = rd.beta; gamma = rd.gamma; delta = rd.delta; x; r} )

  let get_gen_n_prover (prover_public_params : prover_public_parameters) =
    ( Domain.get prover_public_params.common_pp.domain 1,
      prover_public_params.common_pp.n )

  let get_gen_n_verifier (verifier_public_params : verifier_public_parameters) =
    ( verifier_public_params.common_pp.generator,
      verifier_public_params.common_pp.n )
end

module Make : functor (PP : Polynomial_protocol.S) ->
  S with module PP = PP and type public_inputs = Scalar.t array list =
  Make_impl

include Make (Polynomial_protocol)
OCaml

Innovation. Community. Security.