From cec383521a82b68dc019e58d9b5a0056794f91fe Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Mon, 11 Aug 2025 09:26:35 +0200 Subject: 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. --- docs/journal/pages/pairings.md | 2 +- lib/bls12_381.ml | 6 +- lib/pairing.ml | 33 ++++---- test/test_pairing.ml | 181 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 200 insertions(+), 22 deletions(-) diff --git a/docs/journal/pages/pairings.md b/docs/journal/pages/pairings.md index c65d99b..816e81a 100644 --- a/docs/journal/pages/pairings.md +++ b/docs/journal/pages/pairings.md @@ -15,7 +15,7 @@ By some black magic, mathematicians found a function `e` that behaves like the m For BLS12-381: - `G1` is just the elliptic curve BLS12-381 -- `G2` is... well complicated... _technically_, it is the BLS12-381 curve over a specific 12th degree polynomial extension field `Fp12`, with a chosen generator point. In practice, there exists another related curve over a 2nd degree polynomial extension field `Fp2` with a twisting operation that efficiently maps `E'[Fp2] -> E[Fp12]`. This allows you to do operations much more efficiently in the `E'[Fp2]` curve, and then "convert" it to a point in `E[Fp12]` for the pairing computation itself. +- `G2` is... well complicated... _technically_, it is the BLS12-381 curve over a specific 12th degree polynomial extension field `Fp12`, with a chosen generator point. In practice, there exists another related curve over a 2nd degree polynomial extension field `Fp2` with a twisting operation that efficiently maps `E'[Fp2] -> E[Fp12]`. This allows you to do elliptic curve arithmetic much more efficiently in the `E'[Fp2]` curve, and then "convert" it to a point in `E[Fp12]` for the pairing computation itself. - `GT` is a group of elements in `Fp12`, where the: - The identity element is `1` - The group operation is field multiplication 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 diff --git a/test/test_pairing.ml b/test/test_pairing.ml index edd7788..2d698ba 100644 --- a/test/test_pairing.ml +++ b/test/test_pairing.ml @@ -61,11 +61,156 @@ let test_pairing () = "77870636695957247485070729371505785036022507830356763789646790529980989972767478012625350608049848760401081873614"; |] ))) +module G1 = E +module G2 = E2 +module Fp = Plonkers.Bls12_381.Fields.Fp +module Fp2 = Plonkers.Bls12_381.Fields.Fp2 +module Fp12 = Plonkers.Bls12_381.Fields.Fp12 + +let reference_pairing (pt : G1.t) (q : G2.t) : Fp12.t = + let extend p = + match G1.to_coords p with + | None -> failwith "point at infinity" + | Some (x, y) -> Fp12.(of_f x, of_f y) + in + let twist p = + let o, z = Fp.(one, zero) in + let w = Fp12.of_coeffs [| z; o; z; z; z; z; z; z; z; z; z; z |] in + let w2 = Fp12.(w * w) in + let w3 = Fp12.(w2 * w) in + let w2inv = Fp12.inv w2 in + let w3inv = Fp12.inv w3 in + let iso a = + let c = Fp2.to_coeffs a in + let r0 = Fp.(c.(0) - c.(1)) in + let r6 = c.(1) in + Fp12.of_coeffs [| r0; z; z; z; z; z; r6; z; z; z; z; z |] + in + match G2.to_coords p with + | None -> failwith "point at infinity" + | Some (x, y) -> + let nx, ny = (iso x, iso y) in + Fp12.(nx * w2inv, ny * w3inv) + in + let line_function q1 q2 p = + let x1, y1 = twist q1 in + let x2, y2 = twist q2 in + let x, y = extend p in + let two = Fp12.(one + one) in + let three = Fp12.(two + one) in + let extra l = Fp12.((l * (x - x1)) + y1 - y) in + if G2.equal q1 q2 then + let l = Fp12.(three * x1 * x1 / (two * y1)) in + extra l + else if G2.(equal q1 ~-q2) then Fp12.(x - x1) + else + let l = Fp12.((y2 - y1) / (x2 - x1)) in + extra l + in + let ci = + [| + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + -1; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + -1; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + 0; + -1; + 0; + 0; + -1; + 0; + -1; + -1; + |] + in + let l = 63 in + let c = + Array.to_seqi ci + |> Seq.fold_left + (fun c (i, bit) -> + let ci = Z.(~$bit * (one lsl i)) in + Z.(c + ci)) + Z.zero + in + Alcotest.(check string) "c computation" "-d201000000010000" (Z.format "%x" c); + let k = 12 in + let r = G1.n in + let miller_loop () = + let f = ref Fp12.one in + let t = ref q in + (if ci.(l) = -1 then t := G2.(~- (!t))); + for i = l - 1 downto 0 do + (f := Fp12.(!f * !f * line_function !t !t pt)); + (t := G2.(!t + !t)); + if ci.(i) = 1 then ( + (f := Fp12.(!f * line_function !t q pt)); + t := G2.(!t + q)) + else if ci.(i) = -1 then ( + (f := Fp12.(!f * line_function !t G2.(~-q) pt)); + t := G2.(!t - q)) + done; + f := Fp12.(pow !f Z.(((pow p k) - one) / r)); + !f + in + miller_loop () + (* https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-pairing-friendly-curves-11#name-test-vectors-of-optimal-ate *) let test_vector () = - Alcotest.(check gt) + Alcotest.(check Primitives.Testables.fp12) "equals" - (ft + (Primitives.Literals.fp12 [| "0x11619b45f61edfe3b47a15fac19442526ff489dcda25e59121d9931438907dfd448299a87dde3a649bdba96e84d54558"; "0x153ce14a76a53e205ba8f275ef1137c56a566f638b52d34ba3bf3bf22f277d70f76316218c0dfd583a394b8448d2be7f"; @@ -80,7 +225,7 @@ let test_vector () = "0x10900338a92ed0b47af211636f7cfdec717b7ee43900eee9b5fc24f0000c5874d4801372db478987691c566a8c474978"; "0x1454814f3085f0e6602247671bc408bbce2007201536818c901dbd4d2095dd86c1ec8b888e59611f60a301af7776be3d"; |]) - (Pairing.e + (reference_pairing (f1 ( "0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb", "0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1" @@ -133,3 +278,33 @@ let tests = test_case "Bilinearity" `Slow test_bilinearity; test_case "Inverse" `Slow test_inverse; ] + +(* + + 2675223149320665921521646202693247640790817874364255298732398972560239127864099887300921329285059103920214926247256 + 3268798540077874559188095304926415053902298079662269128631117894259354335713488241073281881110999874366368799440511 + 1437178847597838002205538891928304392505606959215308674013885606166925907773842499174371404735625830937323828692626 + 3520140447471844017044610248175793726488927385800919015247604269617056365166670232096113218155158979455637700602719 + 1506178926806320088529997600140680210913802140271006315226533143967895395199677036726919792764738011159067846598728 + 2626389147790168154036854297373352480470342009417426750876875476996308393122833154115295241935326582225976246741447 + 296397698419227591881949966489408966385204245298227609919201225879400018157857419730816679383241778672627140748220 + 1313697571675342986569886099636226306168713170654435514582366343380736958596157600562325204551377354604385651577455 + 2213584337862344967755109385575965635901738814384224456257761298779336140589044234514789656471272705700109501099024 + 2447141669618865772796042211991982778148438817099153092725275403399894819927527860344663177024354496855213527711198 + 2549209626480291013679779981989983528616421847439035724344204595297044667359758911489614886787335613326952862534008 + 3129088482270901242825355060993467433985308559466438813154852238956177342064351887308381291879994227300431734160957 + + 3408834164464458755751340723502736743445402614640994055433339214424916442641449510670327077303074893591740399366532 + 2985109681965551998729853692589086816773373894582799980668893018622694710052537683611179103960880088106135761730552 + 1919447955347661378578718469488414822573562393353397544098339472673901193098010131520945815709482515519580400649694 + 3768852223465146588376856999319887014310182817224079248864544031503472971152354238612814108575933872882790245947613 + 2882199334237819327910933128503231887000342950792587449681715803095618652567681747054312179958427092970985872417068 + 3422530699431057164272214746732420251187996107911604796521410492464898975786244888623921224036357050064415400932838 + 3268798540077874559188095304926415053902298079662269128631117894259354335713488241073281881110999874366368799440511 +X 1313697571675342986569886099636226306168713170654435514582366343380736958596157600562325204551377354604385651577455 + 3520140447471844017044610248175793726488927385800919015247604269617056365166670232096113218155158979455637700602719 +X 2447141669618865772796042211991982778148438817099153092725275403399894819927527860344663177024354496855213527711198 + 2626389147790168154036854297373352480470342009417426750876875476996308393122833154115295241935326582225976246741447 +X 3129088482270901242825355060993467433985308559466438813154852238956177342064351887308381291879994227300431734160957 + + *) -- cgit v1.2.3