diff options
Diffstat (limited to 'lib/pairing.ml')
| -rw-r--r-- | lib/pairing.ml | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/lib/pairing.ml b/lib/pairing.ml index 33c09c6..9bda78d 100644 --- a/lib/pairing.ml +++ b/lib/pairing.ml @@ -61,11 +61,11 @@ end module Make (BLS : sig module G1 : Curve.S module G2 : Curve.S - module F : Field.SX + module F : Field.SX with type f = G1.f val x : Z.t - val g1_extend : G1.t -> (F.t * F.t) option - val g2_twist : G2.t -> (F.t * F.t) option + 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 @@ -101,38 +101,43 @@ struct let pos = len - i - 1 in Z.testbit x pos)) - let final_exponent = lazy Z.((pow F.p 12 - one) / G1.n) + 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 unwrap = function - | Some x -> x + let twist q = + match BLS.twist q with + | Some q -> q | 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 + 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 = g2_twist q in + 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.map (fun (p, q) -> (p, q, ref q)) pairs |> Array.of_list 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 - Array.fold_left + List.fold_left (fun f (p, q, r) -> let t = line !r !r p in let f = F.(f * t) in |
