package tezos-protocol-016-PtMumbai

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

Source file zk_rollup_repr.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
(*****************************************************************************)
(*                                                                           *)
(* Open Source 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 Address = struct
  let prefix = "epx1"

  let encoded_size = 37

  let decoded_prefix = "\001\023\224\125"

  module H =
    Blake2B.Make
      (Base58)
      (struct
        let name = "Zk_rollup_hash"

        let title = "A zk rollup address"

        let b58check_prefix = decoded_prefix

        let size = Some 20
      end)

  include H

  let () = Base58.check_encoded_prefix b58check_encoding prefix encoded_size

  include Path_encoding.Make_hex (H)

  type error += (* `Permanent *) Error_zk_rollup_address_generation

  let () =
    let open Data_encoding in
    let msg = "Error while generating rollup address" in
    register_error_kind
      `Permanent
      ~id:"rollup.error_zk_rollup_address_generation"
      ~title:msg
      ~pp:(fun ppf () -> Format.fprintf ppf "%s" msg)
      ~description:msg
      unit
      (function Error_zk_rollup_address_generation -> Some () | _ -> None)
      (fun () -> Error_zk_rollup_address_generation)

  let from_nonce nonce =
    Data_encoding.Binary.to_bytes_opt Origination_nonce.encoding nonce
    |> function
    | None -> error Error_zk_rollup_address_generation
    | Some nonce -> ok @@ hash_bytes [nonce]

  let of_b58data = function H.Data h -> Some h | _ -> None
end

type t = Address.t

let to_scalar x =
  Zk_rollup_scalar.of_bits
    (Data_encoding.Binary.to_string_exn Address.encoding x)

type pending_list =
  | Empty of {next_index : int64}
  | Pending of {next_index : int64; length : int}

let pending_list_encoding : pending_list Data_encoding.t =
  let open Data_encoding in
  let empty_tag, pending_tag = (0, 1) in
  let empty_encoding =
    obj1 (req "next_index" Compact.(make ~tag_size:`Uint8 int64))
  in
  let pending_encoding =
    obj2
      (req "next_index" Compact.(make ~tag_size:`Uint8 int64))
      (req "length" uint16)
  in
  matching
    (function
      | Empty {next_index} -> matched empty_tag empty_encoding next_index
      | Pending {next_index; length} ->
          matched pending_tag pending_encoding (next_index, length))
    [
      case
        ~title:"Empty"
        (Tag empty_tag)
        empty_encoding
        (function Empty {next_index} -> Some next_index | _ -> None)
        (fun next_index -> Empty {next_index});
      case
        ~title:"Pending"
        (Tag pending_tag)
        pending_encoding
        (function
          | Pending {next_index; length} -> Some (next_index, length)
          | _ -> None)
        (fun (next_index, length) -> Pending {next_index; length});
    ]

module Index = struct
  type nonrec t = t

  let path_length = 1

  let to_path c l =
    let raw_key = Data_encoding.Binary.to_bytes_exn Address.encoding c in
    let (`Hex key) = Hex.of_bytes raw_key in
    key :: l

  let of_path = function
    | [key] ->
        Option.bind
          (Hex.to_bytes (`Hex key))
          (Data_encoding.Binary.of_bytes_opt Address.encoding)
    | _ -> None

  let rpc_arg = Address.rpc_arg

  let encoding = Address.encoding

  let compare = Address.compare
end

let in_memory_size (_ : t) =
  let open Cache_memory_helpers in
  h1w +! string_size_gen Address.size

module Internal_for_tests = struct
  let originated_zk_rollup nonce =
    let data =
      Data_encoding.Binary.to_bytes_exn Origination_nonce.encoding nonce
    in
    Address.hash_bytes [data]
end
OCaml

Innovation. Community. Security.