Source file encoding.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
(*****************************************************************************)
(*                                                                           *)
(* 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 Lang_stdlib

module Encodings (L : LIB) = struct
  open L

  (**
    Encoding type for encapsulating encoding/decoding/input functions.
    This type enables us to use more structured types for data
    in circuits.
    For that, encoding is parameterized by 3 types:
    - 'oh is the type of the high-level OCaml representation
    - 'u is the unpacked type, i.e. a collection of atomic reprs.
    - `p is the packed type, i.e the inner type of Plompiler's repr.

    For example, for the representation of a point (pair of scalars),
    one might have:
    ( {x:int; y:int},
      scalar repr * scalar repr,
      scalar * scalar
    ) encoding

    The first type, the record [{x:int; y:int}], represents an OCaml point,
    which becomes the argument taken by the [input] function.

    The second type, [scalar repr * scalar repr], is an unpacking of the
    encoding. This is used for the result of [decode]. We can use any type
    isomorphic to [scalar repr * scalar repr] here.

    The last type must be [scalar * scalar], as an encoding of a point will be
    of the type [(scalar * scalar) repr].
  *)

  type ('oh, 'u, 'p) encoding = {
    encode : 'u -> 'p repr;
    decode : 'p repr -> 'u;
    input : 'oh -> 'p Input.t;
    of_input : 'p Input.t -> 'oh;
  }

  (**
    The function [conv] defines conversions for [encoding]s, by changing
    the higher-level ['u] and and ['oh] types.
  *)
  let conv :
      ('u1 -> 'u0) ->
      ('u0 -> 'u1) ->
      ('o1 -> 'o0) ->
      ('o0 -> 'o1) ->
      ('o0, 'u0, 'p) encoding ->
      ('o1, 'u1, 'p) encoding =
   fun f g fi gi e ->
    let encode a = e.encode @@ f a in
    let decode b = g @@ e.decode b in
    let input x = e.input @@ fi x in
    let of_input x = gi @@ e.of_input x in
    {encode; decode; input; of_input}

  let with_implicit_bool_check :
      ('p repr -> bool repr t) -> ('o, 'u, 'p) encoding -> ('o, 'u, 'p) encoding
      =
   fun bc e ->
    {e with input = (fun x -> Input.with_implicit_bool_check bc @@ e.input x)}

  let with_assertion :
      ('p repr -> unit repr t) -> ('o, 'u, 'p) encoding -> ('o, 'u, 'p) encoding
      =
   fun assertion e ->
    {e with input = (fun x -> Input.with_assertion assertion @@ e.input x)}

  (** Encoding combinators *)
  let scalar_encoding =
    let encode x = x in
    let decode x = x in
    let input = Input.scalar in
    let of_input = Input.to_scalar in
    {encode; decode; input; of_input}

  let bool_encoding =
    let encode x = x in
    let decode x = x in
    let input = Input.bool in
    let of_input = Input.to_bool in
    {encode; decode; input; of_input}

  let list_encoding (e : _ encoding) =
    let encode a = to_list @@ List.map e.encode a in
    let decode x = List.map e.decode (of_list x) in
    let input a = Input.list @@ List.map e.input a in
    let of_input x = List.map e.of_input (Input.to_list x) in
    {encode; decode; input; of_input}

  (* Encoding for lists, where we keep the repr of the list itself, not a list
     of repr *)
  let atomic_list_encoding :
      ('a, 'b repr, 'c) encoding -> ('a list, 'b list repr, 'c list) encoding =
   fun e ->
    let encode a = to_list @@ List.map e.encode (of_list a) in
    let decode x = to_list @@ List.map e.decode (of_list x) in
    let input a = Input.list @@ List.map e.input a in
    let of_input x = List.map e.of_input (Input.to_list x) in
    {encode; decode; input; of_input}

  let obj2_encoding (el : _ encoding) (er : _ encoding) =
    let encode (a, b) = pair (el.encode a) (er.encode b) in
    let decode p =
      let a, b = of_pair p in
      (el.decode a, er.decode b)
    in
    let input (a, f) = Input.pair (el.input a) (er.input f) in
    let of_input p =
      let a, b = Input.to_pair p in
      (el.of_input a, er.of_input b)
    in
    {encode; decode; input; of_input}

  let atomic_obj2_encoding :
      ('a, 'b repr, 'c) encoding ->
      ('d, 'e repr, 'f) encoding ->
      ('a * 'd, ('b * 'e) repr, 'c * 'f) encoding =
   fun el er ->
    let encode p =
      let a, b = of_pair p in
      pair (el.encode a) (er.encode b)
    in
    let decode p =
      let a, b = of_pair p in
      pair (el.decode a) (er.decode b)
    in
    let input (a, f) = Input.pair (el.input a) (er.input f) in
    let of_input p =
      let a, b = Input.to_pair p in
      (el.of_input a, er.of_input b)
    in
    {encode; decode; input; of_input}

  let obj3_encoding e0 e1 e2 = obj2_encoding e0 (obj2_encoding e1 e2)

  let atomic_obj3_encoding e0 e1 e2 =
    atomic_obj2_encoding e0 (atomic_obj2_encoding e1 e2)

  let obj4_encoding e0 e1 e2 e3 = obj2_encoding e0 (obj3_encoding e1 e2 e3)

  let atomic_obj4_encoding e0 e1 e2 e3 =
    atomic_obj2_encoding e0 (atomic_obj3_encoding e1 e2 e3)

  let obj5_encoding e0 e1 e2 e3 e4 =
    obj2_encoding e0 (obj4_encoding e1 e2 e3 e4)

  let atomic_obj5_encoding e0 e1 e2 e3 e4 =
    atomic_obj2_encoding e0 (atomic_obj4_encoding e1 e2 e3 e4)

  let obj6_encoding e0 e1 e2 e3 e4 e5 =
    obj2_encoding e0 (obj5_encoding e1 e2 e3 e4 e5)

  let atomic_obj6_encoding e0 e1 e2 e3 e4 e5 =
    atomic_obj2_encoding e0 (atomic_obj5_encoding e1 e2 e3 e4 e5)

  let obj7_encoding e0 e1 e2 e3 e4 e5 e6 =
    obj2_encoding e0 (obj6_encoding e1 e2 e3 e4 e5 e6)

  let atomic_obj7_encoding e0 e1 e2 e3 e4 e5 e6 =
    atomic_obj2_encoding e0 (atomic_obj6_encoding e1 e2 e3 e4 e5 e6)

  let obj8_encoding e0 e1 e2 e3 e4 e5 e6 e7 =
    obj2_encoding e0 (obj7_encoding e1 e2 e3 e4 e5 e6 e7)

  let atomic_obj8_encoding e0 e1 e2 e3 e4 e5 e6 e7 =
    atomic_obj2_encoding e0 (atomic_obj7_encoding e1 e2 e3 e4 e5 e6 e7)

  let obj9_encoding e0 e1 e2 e3 e4 e5 e6 e7 e8 =
    obj2_encoding e0 (obj8_encoding e1 e2 e3 e4 e5 e6 e7 e8)

  let atomic_obj9_encoding e0 e1 e2 e3 e4 e5 e6 e7 e8 =
    atomic_obj2_encoding e0 (atomic_obj8_encoding e1 e2 e3 e4 e5 e6 e7 e8)

  let obj10_encoding e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 =
    obj2_encoding e0 (obj9_encoding e1 e2 e3 e4 e5 e6 e7 e8 e9)

  let atomic_obj10_encoding e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 =
    atomic_obj2_encoding e0 (atomic_obj9_encoding e1 e2 e3 e4 e5 e6 e7 e8 e9)
end