package octez-libs

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

Source file message.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
(*****************************************************************************)
(*                                                                           *)
(* 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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

module type S = sig
  (** Plonk Main Protocol *)
  module MP : Distribution.Main_protocol.S

  (** Type witness for a protocol step. *)
  type 'a step =
    | 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. *)
  type ctw_payload = {index : int; content : MP.worker_inputs Kzg.SMap.t}

  type ctpap_payload = {index : int; content : Kzg.Utils.Transcript.t}

  type pppi_payload = {index : int; content : Kzg.Utils.Transcript.t}

  type ppctt_payload = {index : int; content : string list * Kzg.Bls.Scalar.t}

  type ppkeax_payload = {index : int; content : Kzg.Utils.Transcript.t}

  type pcd_payload = {index : int; content : MP.PP.PC.worker_msg}

  type 'step request =
    | 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] request

  type ctw_res_payload = {index : int; content : MP.commit_to_wires_reply}

  type ctpap_res_payload = {index : int; content : MP.commit_to_plook_rc_reply}

  type pppi_res_payload = {index : int; content : string list}

  type ppctt_res_payload = {
    index : int;
    content : Plonk.Identities.Evaluations.t;
  }

  type ppkeax_res_payload = {index : int; content : MP.PP.PC.answer list}

  type pcd_res_payload = {index : int; content : MP.PP.PC.main_prover_msg}

  type 'step reply =
    | 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

  type t = bytes

  val request_step : 'step request -> 'step step

  val of_request : 'a request -> t

  val of_reply : 'a reply -> t

  val to_request : 'step step -> t -> 'step request option

  val to_reply : 'step step -> t -> 'step reply option

  val index : t -> int

  val string_of_message : t -> string
end

module Make =
functor
  (MP : Distribution.Main_protocol.S)
  ->
  struct
    module MP = MP

    (** Type witness for a protocol step. *)
    type 'a step =
      | 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. *)
    type ctw_payload = {index : int; content : MP.worker_inputs Kzg.SMap.t}
    [@@deriving repr]

    type ctpap_payload = {index : int; content : Kzg.Utils.Transcript.t}
    [@@deriving repr]

    type pppi_payload = {index : int; content : Kzg.Utils.Transcript.t}
    [@@deriving repr]

    type ppctt_payload = {index : int; content : string list * Kzg.Bls.Scalar.t}
    [@@deriving repr]

    type ppkeax_payload = {index : int; content : Kzg.Utils.Transcript.t}
    [@@deriving repr]

    type pcd_payload = {index : int; content : MP.PP.PC.worker_msg}
    [@@deriving repr]

    type 'step request =
      | 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] request

    type ctw_res_payload = {index : int; content : MP.commit_to_wires_reply}
    [@@deriving repr]

    type ctpap_res_payload = {
      index : int;
      content : MP.commit_to_plook_rc_reply;
    }
    [@@deriving repr]

    type pppi_res_payload = {index : int; content : string list}
    [@@deriving repr]

    type ppctt_res_payload = {
      index : int;
      content : Plonk.Identities.Evaluations.t;
    }
    [@@deriving repr]

    type ppkeax_res_payload = {index : int; content : MP.PP.PC.answer list}
    [@@deriving repr]

    type pcd_res_payload = {index : int; content : MP.PP.PC.main_prover_msg}
    [@@deriving repr]

    type 'step reply =
      | 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] *)
    type f_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
    [@@deriving repr]

    let forget_request : type a. a request -> f_request = function
      | Commit_to_wires p -> FCommit_to_wires p
      | Commit_to_plook p -> FCommit_to_plook p
      | PP_prepare_ids p -> FPP_prepare_ids p
      | PP_commit_to_t p -> FPP_commit_to_t p
      | PP_KZG_eval_at_x p -> FPP_KZG_eval_at_x p
      | PC_Distribution p -> FPC_Distribution p

    type f_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
    [@@deriving repr]

    let forget_reply : type a. a reply -> f_reply = function
      | Commit_to_wires_res p -> FCommit_to_wires_res p
      | Commit_to_plook_res p -> FCommit_to_plook_res p
      | PP_prepare_ids_res p -> FPP_prepare_ids_res p
      | PP_commit_to_t_res p -> FPP_commit_to_t_res p
      | PP_KZG_eval_at_x_res p -> FPP_KZG_eval_at_x_res p
      | PC_Distribution_res p -> FPC_Distribution_res p

    type msg = Request : f_request -> msg | Reply : f_reply -> msg
    [@@deriving repr]

    type t = bytes

    let of_bytes_exn r b =
      Result.get_ok
      @@ Repr.(unstage @@ Repr.of_bin_string r) (Bytes.to_string b)

    let to_bytes r v = Bytes.of_string Repr.((unstage @@ to_bin_string r) v)

    let request_step : type s. s request -> s step = 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_pcd

    let of_request : 'a request -> t =
     fun r -> to_bytes msg_t @@ Request (forget_request r)

    let of_reply : 'a reply -> t =
     fun r -> to_bytes msg_t @@ Reply (forget_reply r)

    let to_reply : type s. s step -> t -> s reply option =
     fun step t ->
      let m = of_bytes_exn msg_t t in
      match (step, m) with
      | S_ctw, Reply (FCommit_to_wires_res p) -> Some (Commit_to_wires_res p)
      | S_ctp, Reply (FCommit_to_plook_res p) -> Some (Commit_to_plook_res p)
      | S_pppi, Reply (FPP_prepare_ids_res p) -> Some (PP_prepare_ids_res p)
      | S_ppctt, Reply (FPP_commit_to_t_res p) -> Some (PP_commit_to_t_res p)
      | S_ppkeax, Reply (FPP_KZG_eval_at_x_res p) ->
          Some (PP_KZG_eval_at_x_res p)
      | S_pcd, Reply (FPC_Distribution_res p) -> Some (PC_Distribution_res p)
      | _ -> None

    let to_request : type s. s step -> t -> s request option =
     fun step t ->
      let m = of_bytes_exn msg_t t in
      match (step, m) with
      | S_ctw, Request (FCommit_to_wires p) -> Some (Commit_to_wires p)
      | S_ctp, Request (FCommit_to_plook p) -> Some (Commit_to_plook p)
      | S_pppi, Request (FPP_prepare_ids p) -> Some (PP_prepare_ids p)
      | S_ppctt, Request (FPP_commit_to_t p) -> Some (PP_commit_to_t p)
      | S_ppkeax, Request (FPP_KZG_eval_at_x p) -> Some (PP_KZG_eval_at_x p)
      | S_pcd, Request (FPC_Distribution p) -> Some (PC_Distribution p)
      | _ -> None

    let index : t -> int =
     fun t ->
      let m = of_bytes_exn msg_t t in
      match m with
      | 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; _}) ->
          index

    let string_of_bytes bytes =
      let bytes = float_of_int bytes in
      if bytes <= 1024. then Printf.sprintf "%3.2f B " bytes
      else
        let kilobytes = bytes /. 1024. in
        if kilobytes <= 1024. then Printf.sprintf "%3.2f KB" kilobytes
        else
          let megabytes = kilobytes /. 1024. in
          if megabytes <= 1024. then Printf.sprintf "%3.2f MB" megabytes
          else
            let gigabytes = megabytes /. 1024. in
            Printf.sprintf "%.2f GB" gigabytes

    let string_of_message : t -> string =
     fun t ->
      let m = of_bytes_exn msg_t t in
      match m with
      | Request (FCommit_to_wires {index; content = _}) ->
          Format.sprintf
            "Commit_to_wires: %d (%s)"
            index
            (string_of_bytes @@ Bytes.length t)
      | Reply (FCommit_to_wires_res {index; content = _}) ->
          Format.sprintf
            "Commit_to_wires_res: %d (%s)"
            index
            (string_of_bytes @@ Bytes.length t)
      | Request (FCommit_to_plook {index; content = _}) ->
          Format.sprintf
            "Commit_to_plook: %d (%s)"
            index
            (string_of_bytes @@ Bytes.length t)
      | Reply (FCommit_to_plook_res {index; content = _}) ->
          Format.sprintf
            "Commit_to_plook_res: %d (%s)"
            index
            (string_of_bytes @@ Bytes.length t)
      | Request (FPP_prepare_ids {index; content = _}) ->
          Format.sprintf
            "PP_prepare_ids: %d (%s)"
            index
            (string_of_bytes @@ Bytes.length t)
      | Reply (FPP_prepare_ids_res {index; content = _}) ->
          Format.sprintf
            "PP_prepare_ids_res: %d (%s)"
            index
            (string_of_bytes @@ Bytes.length t)
      | Request (FPP_commit_to_t {index; content = _}) ->
          Format.sprintf
            "PP_commit_to_t: %d (%s)"
            index
            (string_of_bytes @@ Bytes.length t)
      | Reply (FPP_commit_to_t_res {index; content = _}) ->
          Format.sprintf
            "PP_commit_to_t_res: %d (%s)"
            index
            (string_of_bytes @@ Bytes.length t)
      | Request (FPP_KZG_eval_at_x {index; content = _}) ->
          Format.sprintf
            "PP_KZG_eval_at_x: %d (%s)"
            index
            (string_of_bytes @@ Bytes.length t)
      | Reply (FPP_KZG_eval_at_x_res {index; content = _}) ->
          Format.sprintf
            "PP_KZG_eval_at_x_res: %d (%s)"
            index
            (string_of_bytes @@ Bytes.length t)
      | Request (FPC_Distribution {index; content = _}) ->
          Format.sprintf
            "PC_Distribution: %d (%s)"
            index
            (string_of_bytes @@ Bytes.length t)
      | Reply (FPC_Distribution_res {index; content = _}) ->
          Format.sprintf
            "PC_Distribution_res: %d (%s)"
            index
            (string_of_bytes @@ Bytes.length t)
  end
OCaml

Innovation. Community. Security.