aboutsummaryrefslogtreecommitdiff
path: root/lib/pairing.ml
blob: 33c09c695f861a3338f276db5a58c3202d2f408c (plain)
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
(** Boneh-Lynn-Shacham (BLS) pairing curves implementation. *)

(** Module signature BLS pairing curves. *)
module type S = sig
  type g1
  (** The elliptic curve element of the first pairing group. *)

  type g2
  (** The elliptic curve element of the second pairing group. *)

  (** The target group of the pairing, usually denoted as [GT]. *)
  module Gt : sig
    type t
    (** An element in the pairing target group. *)

    type f
    (** The underlying field type of the pairing target group. *)

    val id : t
    (** The group's identity element. *)

    val dot : t -> t -> t
    (** [dot a b] is the group operation applied to [a] and [b]. *)

    val inv : t -> t
    (** [inv a] is the inverse of [a]. Note that the invariant
        [equal id (dot a (inv a))] always holds. *)

    val equal : t -> t -> bool
    (** [equal a b] is true if [a] has the same value as [b] *)

    val to_f : t -> f
    (** [to_fp12 a] is the underlying field value of the group element [a]. *)

    val to_string : t -> string
    (** [to_string a] is the string representation of [a]. *)

    (** Unsafe pairing group construction functions. *)
    module Unsafe : sig
      val of_f_unchecked : f -> t
      (** [of_fp12_unchecked a] is a pairing element of value [a]. This function
          does not check whether the value actually belongs to the pairing group
          [Gt]. *)

      val of_string_unchecked : string -> t
      (** [of_string_unchecked s] returns the pairing element represented by
          [s]. This function does not check whether the value belongs to the
          group [Gt]. *)
    end
  end

  val e : g1 -> g2 -> Gt.t
  (** [e p q] is the elliptic curve pairing of [p] and [q]. *)

  val check : (g1 * g2) list -> bool
  (** [check pairs] checks whether the product of the pairings is equal to
      [Gt.id]. *)
end

(** BLS pairing curves construction. *)
module Make (BLS : sig
  module G1 : Curve.S
  module G2 : Curve.S
  module F : Field.SX

  val x : Z.t
  val g1_extend : G1.t -> (F.t * F.t) option
  val g2_twist : G2.t -> (F.t * F.t) option
end) :
  S with type g1 = BLS.G1.t and type g2 = BLS.G2.t and type Gt.f = BLS.F.t =
struct
  module G1 = BLS.G1
  module G2 = BLS.G2
  module F = BLS.F

  type g1 = G1.t
  type g2 = G2.t

  module Gt = struct
    type t = F.t
    type f = F.t

    let id = F.one
    let dot = F.mul
    let inv = F.inv
    let to_f a = a
    let equal = F.equal
    let to_string = F.to_string

    module Unsafe = struct
      let of_f_unchecked a = a
      let of_string_unchecked = F.of_string
    end
  end

  let ate_loop =
    lazy
      (let x = Z.abs BLS.x in
       let len = Z.numbits x - 1 in
       Array.init len (fun i ->
           let pos = len - i - 1 in
           Z.testbit x pos))

  let final_exponent = lazy Z.((pow F.p 12 - one) / G1.n)

  let line =
    let two = F.(one + one) in
    let three = F.(one + one + one) in
    let unwrap = function
      | Some x -> x
      | None -> invalid_arg "line with point at infinity"
    in
    let g1_extend p = BLS.g1_extend p |> unwrap in
    let g2_twist p = BLS.g2_twist p |> unwrap in
    fun p q r ->
      let xp, yp = g2_twist p in
      let xr, yr = g1_extend r in
      if G2.equal p q then
        let m = F.(three * xp * xp / (two * yp)) in
        F.((m * (xr - xp)) - (yr - yp))
      else if G2.(equal p (neg q)) then F.(xr - xp)
      else
        let xq, yq = g2_twist q in
        let m = F.((yq - yp) / (xq - xp)) in
        F.((m * (xr - xp)) - (yr - yp))

  let pairing pairs =
    let ate_loop = Lazy.force ate_loop in
    let final_exponent = Lazy.force final_exponent in
    let el = List.map (fun (p, q) -> (p, q, ref q)) pairs |> Array.of_list in
    let f =
      Array.fold_left
        (fun f bit ->
          let f = F.(f * f) in
          Array.fold_left
            (fun f (p, q, r) ->
              let t = line !r !r p in
              let f = F.(f * t) in
              r := G2.add !r !r;
              if bit then (
                let t = line !r q p in
                let f = F.(f * t) in
                r := G2.add !r q;
                f)
              else f)
            f el)
        F.one ate_loop
    in
    F.pow f final_exponent

  let e p q = pairing [ (p, q) ]
  let check pairs = pairing pairs |> Gt.(equal id)
end