From 08c921a5ca703233185e9021d8cc50b620d08a39 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Fri, 1 Aug 2025 11:41:26 +0200 Subject: Add Generator Test We use the `py_ecc` generator, and the generator test now passes as expected. Note that we still haven't gotten to the bottom of why `py_ecc` and `@noble/curves` have different `Gt` groups. --- lib/pairing.ml | 45 ++++++++++++++++----------------------------- test/test_pairing.ml | 41 +++++++++++++++-------------------------- 2 files changed, 31 insertions(+), 55 deletions(-) diff --git a/lib/pairing.ml b/lib/pairing.ml index 68430dd..0ad5028 100644 --- a/lib/pairing.ml +++ b/lib/pairing.ml @@ -38,7 +38,6 @@ struct let pos = len - i - 1 in Z.testbit x pos)) - let negative_x = Z.sign BLS.x < 0 let final_exponent = lazy Z.((pow F.p 12 - one) / G1.n) let line = @@ -80,7 +79,7 @@ struct else (r', f')) (q, F.one) ate_loop in - if negative_x then f else f + f let e p q = let t = e' p q in @@ -97,33 +96,21 @@ module P = Make (struct let g = F.of_coeffs - Fields.Fp. - [| - of_string - "2819105605953691245277803056322684086884703000473961065716485506033588504203831029066448642358042597501014294104502"; - of_string - "1323968232986996742571315206151405965104242542339680722164220900812303524334628370163366153839984196298685227734799"; - of_string - "2987335049721312504428602988447616328830341722376962214011674875969052835043875658579425548512925634040144704192135"; - of_string - "3879723582452552452538684314479081967502111497413076598816163759028842927668327542875108457755966417881797966271311"; - of_string - "261508182517997003171385743374653339186059518494239543139839025878870012614975302676296704930880982238308326681253"; - of_string - "231488992246460459663813598342448669854473942105054381511346786719005883340876032043606739070883099647773793170614"; - of_string - "3993582095516422658773669068931361134188738159766715576187490305611759126554796569868053818105850661142222948198557"; - of_string - "1074773511698422344502264006159859710502164045911412750831641680783012525555872467108249271286757399121183508900634"; - of_string - "2727588299083545686739024317998512740561167011046940249988557419323068809019137624943703910267790601287073339193943"; - of_string - "493643299814437640914745677854369670041080344349607504656543355799077485536288866009245028091988146107059514546594"; - of_string - "734401332196641441839439105942623141234148957972407782257355060229193854324927417865401895596108124443575283868655"; - of_string - "2348330098288556420918672502923664952620152483128593484301759394583320358354186482723629999370241674973832318248497"; - |] + @@ Array.map Fields.Fp.of_string + [| + "3408834164464458755751340723502736743445402614640994055433339214424916442641449510670327077303074893591740399366532"; + "1017299873256115394687936133146817339783508925356207904663165117501336940438300180831508525168135575931758510829235"; + "1919447955347661378578718469488414822573562393353397544098339472673901193098010131520945815709482515519580400649694"; + "233557331756520805040932826416017142246700002714928636467514104620558679338483625829873520553081791155104026612174"; + "2882199334237819327910933128503231887000342950792587449681715803095618652567681747054312179958427092970985872417068"; + "579878855790610229145575079003483905368886712027403088810647643659132674704592975818766405092658613973478871626949"; + "3268798540077874559188095304926415053902298079662269128631117894259354335713488241073281881110999874366368799440511"; + "2688711983546324406847903726099677850388169649284572370749691792743294691894680263880362424577638309433508620982332"; + "3520140447471844017044610248175793726488927385800919015247604269617056365166670232096113218155158979455637700602719"; + "1555267885602801620621747613743921378408444002839854792606782732724136830563310004098024452104661167182680744848589"; + "2626389147790168154036854297373352480470342009417426750876875476996308393122833154115295241935326582225976246741447"; + "873321072950766150592434764742436722571574260472569072177205897167854308426485977134306337249021436737462538398830"; + |] let g1_to_coords p = G1.to_coords p |> Option.map (fun (x, y) -> (F.of_f x, F.of_f y)) diff --git a/test/test_pairing.ml b/test/test_pairing.ml index e4d3fb4..754e71e 100644 --- a/test/test_pairing.ml +++ b/test/test_pairing.ml @@ -44,27 +44,17 @@ let test_pairing () = let test_bilinearity () = let a = - f1 - ( "3155009934535908241854382503496913376741892294245863405401597462641760243133882235624423397847127767716934062555793", - "2234019121465620231805578079882835966262487276444547332538208418002132978001247983358670643174241518454728236294862" - ) + Z.of_string + "49635613352584733781027070618160862805950329685486063826980172894235303933946" in let b = - f2 - ( [| - "2647899611276867777535497861408512411465691530078703925817898320530901433230690986081010166625858940444369794593258"; - "2888256862829316108006975470566918581509317636890719189755447540552883010802407709863131290915043648752253159925232"; - |], - [| - "2192519177806038492881176172205451311854538798364777947095907984617867826862323453026127520221274922616271304335259"; - "3004620411053362146054721503134832661166005188461559731409518090929136380431931464671889917597096234887634360879239"; - |] ) + Z.of_string + "4249060523776624689160540763312478010481990280247166863389957742757316747171" in - let t = Pairing.e a b in - let tt = Gt.dot t t in - Alcotest.(check (pair gt gt)) - "equals" (tt, tt) - (Pairing.e G1.(add a a) b, Pairing.e a G2.(add b b)) + 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)) let test_inverse () = let a = @@ -81,16 +71,15 @@ let test_inverse () = in let ta = Pairing.e a G2.g in let tb = Pairing.e b G2.g in - Alcotest.(check gt) - "equals" - Gt.(dot ta (inv tb)) - (Pairing.e (G1.sub a b) G2.g) + 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 tests = Alcotest. [ - (* test_case "Generator" `Quick test_generator; *) - test_case "Pairing" `Quick test_pairing; - test_case "Bilinearity" `Quick test_bilinearity; - test_case "Inverse" `Quick test_inverse; + test_case "Generator" `Slow test_generator; + test_case "Pairing" `Slow test_pairing; + test_case "Bilinearity" `Slow test_bilinearity; + test_case "Inverse" `Slow test_inverse; ] -- cgit v1.2.3