aboutsummaryrefslogtreecommitdiff
path: root/test/test_pairing.ml
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_pairing.ml')
-rw-r--r--test/test_pairing.ml181
1 files changed, 178 insertions, 3 deletions
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
+
+ *)