Source file pairing.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
(*****************************************************************************)
(*                                                                           *)
(* Copyright (c) 2020-2021 Danny Willems <be.danny.willems@gmail.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 RAW_STUBS = sig
  val final_exponentiation : Bytes.t -> Bytes.t

  val miller_loop_simple : Bytes.t -> Bytes.t -> Bytes.t

  val pairing : Bytes.t -> Bytes.t -> Bytes.t

  val miller_loop_2 : Bytes.t -> Bytes.t -> Bytes.t -> Bytes.t -> Bytes.t

  val miller_loop_3 :
    Bytes.t -> Bytes.t -> Bytes.t -> Bytes.t -> Bytes.t -> Bytes.t -> Bytes.t

  val miller_loop_4 :
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t

  val miller_loop_5 :
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t

  val miller_loop_6 :
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t ->
    Bytes.t
end

module Make
    (G1 : G1.UNCOMPRESSED)
    (G2 : G2.UNCOMPRESSED)
    (GT : Fq12.T)
    (Stubs : RAW_STUBS) : sig
  exception FailToComputeFinalExponentiation of GT.t

  val miller_loop : (G1.t * G2.t) list -> GT.t

  (** Compute the miller loop on a single tuple of point *)
  val miller_loop_simple : G1.t -> G2.t -> GT.t

  (** Compute a pairing result of a list of points *)
  val pairing : G1.t -> G2.t -> GT.t

  (** Compute the final exponentiation of the given point. Returns a [None] if
      the point is null *)
  val final_exponentiation_opt : GT.t -> GT.t option

  (** Compute the final exponentiation of the given point. Raise
      [FailToComputeFinalExponentiation] if the point is null *)
  val final_exponentiation_exn : GT.t -> GT.t
end = struct
  module G1 = G1
  module G2 = G2
  module GT = GT

  exception FailToComputeFinalExponentiation of GT.t

  let miller_loop_simple (g1 : G1.t) (g2 : G2.t) : GT.t =
    let res = Stubs.miller_loop_simple (G1.to_bytes g1) (G2.to_bytes g2) in
    GT.of_bytes_exn res

  let pairing (g1 : G1.t) (g2 : G2.t) : GT.t =
    let res = Stubs.pairing (G1.to_bytes g1) (G2.to_bytes g2) in
    GT.of_bytes_exn res

  let final_exponentiation_exn (e : GT.t) : GT.t =
    if GT.is_zero e then raise (FailToComputeFinalExponentiation e)
    else
      let res = Stubs.final_exponentiation (GT.to_bytes e) in
      GT.of_bytes_exn res

  let final_exponentiation_opt (e : GT.t) : GT.t option =
    if GT.is_zero e then None
    else
      let res = Stubs.final_exponentiation (GT.to_bytes e) in
      GT.of_bytes_opt res

  let miller_loop (xs : (G1.t * G2.t) list) : GT.t =
    let rec f acc xs =
      match xs with
      | [] -> acc
      | [(g1, g2)] -> GT.mul (miller_loop_simple g1 g2) acc
      | [(g1_1, g2_1); (g1_2, g2_2)] ->
          let res =
            Stubs.miller_loop_2
              (G1.to_bytes g1_1)
              (G1.to_bytes g1_2)
              (G2.to_bytes g2_1)
              (G2.to_bytes g2_2)
          in
          GT.mul (GT.of_bytes_exn res) acc
      | [(g1_1, g2_1); (g1_2, g2_2); (g1_3, g2_3)] ->
          let res =
            Stubs.miller_loop_3
              (G1.to_bytes g1_1)
              (G1.to_bytes g1_2)
              (G1.to_bytes g1_3)
              (G2.to_bytes g2_1)
              (G2.to_bytes g2_2)
              (G2.to_bytes g2_3)
          in
          GT.mul (GT.of_bytes_exn res) acc
      | [(g1_1, g2_1); (g1_2, g2_2); (g1_3, g2_3); (g1_4, g2_4)] ->
          let res =
            Stubs.miller_loop_4
              (G1.to_bytes g1_1)
              (G1.to_bytes g1_2)
              (G1.to_bytes g1_3)
              (G1.to_bytes g1_4)
              (G2.to_bytes g2_1)
              (G2.to_bytes g2_2)
              (G2.to_bytes g2_3)
              (G2.to_bytes g2_4)
          in
          GT.mul (GT.of_bytes_exn res) acc
      | [(g1_1, g2_1); (g1_2, g2_2); (g1_3, g2_3); (g1_4, g2_4); (g1_5, g2_5)]
        ->
          let res =
            Stubs.miller_loop_5
              (G1.to_bytes g1_1)
              (G1.to_bytes g1_2)
              (G1.to_bytes g1_3)
              (G1.to_bytes g1_4)
              (G1.to_bytes g1_5)
              (G2.to_bytes g2_1)
              (G2.to_bytes g2_2)
              (G2.to_bytes g2_3)
              (G2.to_bytes g2_4)
              (G2.to_bytes g2_5)
          in
          GT.mul (GT.of_bytes_exn res) acc
      | (g1_1, g2_1)
        :: (g1_2, g2_2)
           :: (g1_3, g2_3) :: (g1_4, g2_4) :: (g1_5, g2_5) :: (g1_6, g2_6) :: xs
        ->
          let res =
            Stubs.miller_loop_6
              (G1.to_bytes g1_1)
              (G1.to_bytes g1_2)
              (G1.to_bytes g1_3)
              (G1.to_bytes g1_4)
              (G1.to_bytes g1_5)
              (G1.to_bytes g1_6)
              (G2.to_bytes g2_1)
              (G2.to_bytes g2_2)
              (G2.to_bytes g2_3)
              (G2.to_bytes g2_4)
              (G2.to_bytes g2_5)
              (G2.to_bytes g2_6)
          in
          let acc = GT.mul (GT.of_bytes_exn res) acc in
          f acc xs
    in
    if List.length xs = 0 then failwith "Empty list of points given"
    else f GT.one xs
end