aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2025-08-11 09:26:35 +0200
committerNicholas Rodrigues Lordello <[email protected]>2025-08-11 09:26:35 +0200
commitcec383521a82b68dc019e58d9b5a0056794f91fe (patch)
treea1ec5a463b81a2d88dfbf1e1e7ee94233d04a26b /lib
parent562857071b2aee9b37f042a1b521cb0b9de8b729 (diff)
Experiment With Reference Implementation
We experiment with the test vector from the IETF draft "Pairing-Friendly Curves", but the implementation does not yield the same test result (some coefficients are the same though)... I don't know why, and I cannot find a reference implementation that _does_ return the pairing value from the test vector.
Diffstat (limited to 'lib')
-rw-r--r--lib/bls12_381.ml6
-rw-r--r--lib/pairing.ml33
2 files changed, 21 insertions, 18 deletions
diff --git a/lib/bls12_381.ml b/lib/bls12_381.ml
index d9399f2..6cb329b 100644
--- a/lib/bls12_381.ml
+++ b/lib/bls12_381.ml
@@ -86,11 +86,9 @@ module Pairing = Pairing.Make (struct
module F = Fp12
let x = Z.of_string "-0xd201000000010000"
+ let k = 12
- let g1_extend p =
- E.to_coords p |> Option.map (fun (x, y) -> (Fp12.of_f x, Fp12.of_f y))
-
- let g2_twist =
+ let twist =
let o, z = Fp.(one, zero) in
let ws =
lazy
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