aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2025-08-01 14:49:14 +0200
committerNicholas Rodrigues Lordello <[email protected]>2025-08-01 14:49:14 +0200
commit89f936fff830cd245d47fb5fb6e109c56216c7af (patch)
tree966252ee41c40c721d8918701bc0728d92101f25
parent08c921a5ca703233185e9021d8cc50b620d08a39 (diff)
Batched Pairing Computation
We batch pairing computations for efficient checking. This allows to more efficiently implement things like BLS signatures (where we check that some pairing dot another is equal to 1). This is because the exponentiation in Fp12 is a _huge_ computational effort (and what takes the most time in the pairing computation). It looks like `@noble/curves` implements a pairing to another `Gt` that is much more efficient to compute (as we do not need to untwist coordinates at all, meaning no field inversion, and no computations in Fp12), but I will hold off on implementing this until there is an actual need for it (in case the current pairing implementation is too slow).
-rw-r--r--lib/pairing.ml52
-rw-r--r--lib/pairing.mli9
-rw-r--r--test/test_pairing.ml26
3 files changed, 42 insertions, 45 deletions
diff --git a/lib/pairing.ml b/lib/pairing.ml
index 0ad5028..ef67901 100644
--- a/lib/pairing.ml
+++ b/lib/pairing.ml
@@ -61,30 +61,32 @@ struct
let m = F.((yq - yp) / (xq - xp)) in
F.((m * (xr - xp)) - (yr - yp))
- let e' p q =
- if G1.(equal p inf) || G2.(equal q inf) then F.one
- else
- let ate_loop = Lazy.force ate_loop in
- let _, f =
- Array.fold_left
- (fun (r, f) bit ->
- let t = line r r p in
- let f' = F.(f * f * t) in
- let r' = G2.add r r in
- if bit then
- let t = line r' q p in
- let f'' = F.(f' * t) in
- let r'' = G2.add r' q in
- (r'', f'')
- else (r', f'))
- (q, F.one) ate_loop
- in
- f
-
- let e p q =
- let t = e' p q in
- let exp = Lazy.force final_exponent in
- F.pow t exp
+ 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
module P = Make (struct
@@ -122,5 +124,5 @@ module G1 = P.G1
module G2 = P.G2
module Gt = P.Gt
-let e' = P.e'
let e = P.e
+let check = P.check
diff --git a/lib/pairing.mli b/lib/pairing.mli
index a76343d..3d63df6 100644
--- a/lib/pairing.mli
+++ b/lib/pairing.mli
@@ -40,10 +40,9 @@ module Gt : sig
end
end
-val e' : G1.t -> G2.t -> Fields.Fp12.t
-(** [e p q] is the elliptic curve pairing of [p] and [q] without final
- exponentiation. This is useful when you only want to compare pairings,
- without actually mapping them to [Gt]. *)
-
val e : G1.t -> G2.t -> Gt.t
(** [e p q] is the elliptic curve pairing of [p] and [q]. *)
+
+val check : (G1.t * G2.t) list -> bool
+(** [check pairs] checks whether the product of the pairings is equal to
+ [Gt.id]. *)
diff --git a/test/test_pairing.ml b/test/test_pairing.ml
index 754e71e..82e4ab3 100644
--- a/test/test_pairing.ml
+++ b/test/test_pairing.ml
@@ -51,10 +51,13 @@ let test_bilinearity () =
Z.of_string
"4249060523776624689160540763312478010481990280247166863389957742757316747171"
in
- Alcotest.(check gt)
- "equals"
- (Pairing.e G1.(scalar_mul a g) G2.(scalar_mul b g))
- (Pairing.e G1.(scalar_mul b g) G2.(scalar_mul a g))
+ Alcotest.(check bool)
+ "equals" true
+ (Pairing.check
+ [
+ (G1.(scalar_mul a g), G2.(scalar_mul b g));
+ (G1.(neg @@ scalar_mul b g), G2.(scalar_mul a g));
+ ])
let test_inverse () =
let a =
@@ -63,17 +66,10 @@ let test_inverse () =
"2771533846423619448760061452592233431921571821664938471039058787579168163258312047422278741446979265860731259685588"
)
in
- let b =
- f1
- ( "1370766558783140535152500693223113089394078759152774320389619861811343135317811753270627096528148539713374385472074",
- "1158300532618179468953717893171547786621985273328026665132493211269013369890783422334256435364557531790375173858380"
- )
- in
- let ta = Pairing.e a G2.g in
- let tb = Pairing.e b G2.g in
- let tbinv = Gt.inv tb in
- Alcotest.(check gt) "equals" Gt.id (Gt.dot tb tbinv);
- Alcotest.(check gt) "equals" Gt.(dot ta tbinv) (Pairing.e (G1.sub a b) G2.g)
+ let t = Pairing.e a G2.g in
+ let tinv = Pairing.e G1.(neg a) G2.g in
+ Alcotest.(check gt) "equals" (Gt.inv t) tinv;
+ Alcotest.(check gt) "equals" Gt.id (Gt.dot t tinv)
let tests =
Alcotest.