(** 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 with type f = G1.f val x : Z.t val k : int val 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 BLS.k - one) / G1.n) let line = let two = F.(one + one) in let three = F.(one + one + one) in let twist q = match BLS.twist q with | Some q -> q | None -> invalid_arg "line with point at infinity" in fun p q (xr, yr) -> let xp, yp = twist p 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 = 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.filter_map (fun (p, q) -> match (G1.to_coords p, G2.(equal q inf)) with | Some (px, py), false -> Some (F.(of_f px, of_f py), q, ref q) | _ -> None) pairs in let f = Array.fold_left (fun f bit -> let f = F.(f * f) in List.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