diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2024-06-24 08:46:01 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2024-06-24 08:46:01 +0200 |
| commit | 9747206e7a0a08f6dc8c92730f0780e1b66cb23e (patch) | |
| tree | 2b85dfdabcb39ebeea50e7eb551d8cb66335ac8f /test | |
| parent | 4b5936586e570b125b1c68d04b1479262af9c98b (diff) | |
Implement Polynomial Extension Fields
Notably, only `Fp[X]/1+x^2`, or `Fp2` is tested at the moment. More
tests for `Fp12` coming right up!
Diffstat (limited to 'test')
| -rw-r--r-- | test/test_fp.ml | 104 | ||||
| -rw-r--r-- | test/test_fp2.ml | 141 | ||||
| -rw-r--r-- | test/test_plonkers.ml | 3 |
3 files changed, 213 insertions, 35 deletions
diff --git a/test/test_fp.ml b/test/test_fp.ml index 2189c7d..ca8f9e4 100644 --- a/test/test_fp.ml +++ b/test/test_fp.ml @@ -1,99 +1,134 @@ -module Fq = Plonkers.Fields.Fq +module Fp = Plonkers.Fields.Fp -let fq = Testable.make Fq.to_string Fq.equal -let f = Fq.of_string +let fp = Testable.make Fp.to_string Fp.equal +let f = Fp.of_string let test_add () = - Alcotest.(check fq) + Alcotest.(check fp) "equals" (f "5034212870056682811541563993093442956024207074167934414259669251040086104221") - (Fq.add + (Fp.add (f "3019612232293951348432769707427639303474751231631017441050730061639747788964") (f "2014600637762731463108794285665803652549455842536916973208939189400338315257")); - Alcotest.(check fq) - "equals" Fq.zero - (Fq.add + Alcotest.(check fp) + "equals" Fp.zero + (Fp.add (f "21888242871839275222246405745257275088696311157297823662689037894645226208582") - Fq.one); - Alcotest.(check fq) + Fp.one); + Alcotest.(check fp) "equals" (f "7363893478117210948657131929839491162862302555393162961985689652909776739235") - (Fq.add + (Fp.add (f "7363893478117210948657131929839491162862302555393162961985689652909776739235") - Fq.zero) + Fp.zero) let test_sub () = - Alcotest.(check fq) + Alcotest.(check fp) "equals" (f "1647997538064335793847192302859723563372189463772006638527106196094592175035") - (Fq.sub + (Fp.sub (f "17522329235179667504196475280824254606128390327458085261948007699233694805172") (f "15874331697115331710349282977964531042756200863686078623420901503139102630137")); - Alcotest.(check fq) + Alcotest.(check fp) "equals" (f "21888242871839275222246405745257275088696311157297823662689037894645226208582") - (Fq.sub Fq.zero Fq.one); - Alcotest.(check fq) + (Fp.sub Fp.zero Fp.one); + Alcotest.(check fp) "equals" (f "1550460075629904631277637708383660261580971272511193857451434052003108082807") - (Fq.add + (Fp.add (f "1550460075629904631277637708383660261580971272511193857451434052003108082807") - Fq.zero) + Fp.zero) let test_mul () = - Alcotest.(check fq) + Alcotest.(check fp) "equals" (f "20539703399960180369319660518633290048071922000666914740466234074847854697757") - (Fq.mul + (Fp.mul (f "16652515618518207323315643285221741056245295412987863357779458647516886387587") (f "15388577223719168739739036375931130294575142146897880107473273404618844312776")); - Alcotest.(check fq) - "equals" Fq.zero - (Fq.mul + Alcotest.(check fp) + "equals" Fp.zero + (Fp.mul (f "10935302729977221140784701859408855206669147633587285404353277660075789680922") - Fq.zero) + Fp.zero) let test_div () = - Alcotest.(check fq) + Alcotest.(check fp) + "equals" Fp.zero + (Fp.div + (f + "18116615978164648621644052928628515074324534642679951563606417178540650964031") + Fp.zero); + Alcotest.(check fp) "equals" (f "7363953525538778156567393633860492517850968441297156550375719598579598367322") - (Fq.div + (Fp.div (f "7363953525538778156567393633860492517850968441297156550375719598579598367322") - Fq.one) + Fp.one); + Alcotest.(check fp) + "equals" + (f + "7790294916104464264789490491726462498925424319049857352064036320409088886798") + (Fp.div + (f + "4322946717978085483115008694110144245819367528797529184678953776094386598796") + (f + "14108716032053518963412902325315866377355891041801503021777950559570482464634")) + +let test_neg () = + Alcotest.(check fp) + "equals" + (f + "21888242871839275222246405745257275088696311157297823662689037894645226208565") + (Fp.neg (f "18")); + Alcotest.(check fp) + "equals" + (f + "21888242871839275222246405745257275088696311157297823662689037894645226208565") + (f "-18"); + Alcotest.(check fp) + "equals" Fp.zero + (let a = + f + "17892529277404961739509303065517565646363806323806017705817770169891693557191" + in + Fp.add a (Fp.neg a)) let test_inv () = - Alcotest.(check fq) + Alcotest.(check fp) "equals" Fp.zero (Fp.inv Fp.zero); + Alcotest.(check fp) "equals" (f "14812345552713361047837749161990489217154524375614120373819285291721752372748") - (Fq.inv + (Fp.inv (f "1160181877204925047836599448201084379181489517022839288462621953652233427695")); - Alcotest.(check fq) - "equals" Fq.one + Alcotest.(check fp) + "equals" Fp.one (let a = f "17876831255806984619368587498426968568242044248616647019388897055879503636463" in - Fq.mul a (Fq.inv a)) + Fp.mul a (Fp.inv a)) let tests = Alcotest. @@ -101,6 +136,7 @@ let tests = test_case "Addition" `Quick test_add; test_case "Subtraction" `Quick test_sub; test_case "Multiplication" `Quick test_mul; - test_case "Division" `Quick test_mul; + test_case "Division" `Quick test_div; + test_case "Additive Inverse" `Quick test_neg; test_case "Multiplicative Inverse" `Quick test_inv; ] diff --git a/test/test_fp2.ml b/test/test_fp2.ml new file mode 100644 index 0000000..f62748e --- /dev/null +++ b/test/test_fp2.ml @@ -0,0 +1,141 @@ +module Fp = Plonkers.Fields.Fp +module Fp2 = Plonkers.Fields.Fp2 + +let fp2 = Testable.make Fp2.to_string Fp2.equal +let f a b = Fp2.of_coeffs Fp.(of_string a, of_string b) + +let test_strconv () = + let a = + f + "10107500267655288533860407941350647620522758874612469022754124115798347851255" + "4272490765421360074221748579109824733865223751122700688342039244846082709245" + in + Alcotest.(check string) + "equals" + "(10107500267655288533860407941350647620522758874612469022754124115798347851255, \ + 4272490765421360074221748579109824733865223751122700688342039244846082709245)" + (Fp2.to_string a); + Alcotest.(check fp2) + "equals" a + (Fp2.of_string + "(10107500267655288533860407941350647620522758874612469022754124115798347851255, \ + 4272490765421360074221748579109824733865223751122700688342039244846082709245)") + +let test_add () = + Alcotest.(check fp2) + "equals" + (f + "2863935846698150146497381873035561276727058128903565528503671733559822376250" + "14401928050079391824950970163870648129396875197878295136665664481048699468020") + (Fp2.add + (f + "14324043627524728108674506634258156009885262437468906348349536322248493484364" + "18029279639026325195296840766401583521358381131411036066373305410616713471095") + (f + "10428135091012697260069280984034680355538106848732482842843173305956555100469" + "18260891282892341851900535142726339696734805223765082732981396965077212205508")) + +let test_sub () = + Alcotest.(check fp2) + "equals" + (f + "15120500054601708444564040485468985282448658084430827774523947688586748960053" + "9100764362236999328461500289225313390908509119277966267226462796263537316381") + (Fp2.sub + (f + "14293698145849566696549634796503386029017218475989635574509430094951523306687" + "19456315000973730806536321027874670859833970960897936694996600353725317883622") + (f + "21061440963087133474232000056291675835264871548856631462674520301010000555217" + "10355550638736731478074820738649357468925461841619970427770137557461780567241")) + +let test_mul () = + Alcotest.(check fp2) + "equals" + (f + "11432323160814065864909960700129434587946493783507438700207928706738844119449" + "3577409691447205319998726389612822121306006806114173782377028664710279109705") + (Fp2.mul + (f + "11986626879605260056244902575435385107082913504490111545527498340756305531092" + "4818173862427609729222592608595100262022473140100027294220533759712947640512") + (f + "10960597259346866798564665428177212161645337477705428048049421023510569201652" + "17436818457664572454171416757524427055427814808946492827966722145668155530853")) + +let test_div () = + Alcotest.(check fp2) + "equals" + (f + "13419365922711575620625084257870785388560921645234544455343469677828280907673" + "10223115143172140697158827396733687716959251907957043435822040517781915535192") + (Fp2.div + (f + "5246975579657097196126930177221162000731887408914575127136817635764078403513" + "8292723131410576043572459680046943309298666101925027054973960663999641620758") + (f + "1886591325401429366956197735571233984030593880349024014320016420187469008836" + "1695527069488388814625626614375814226660393081483975270883481133990211272753")); + Alcotest.(check fp2) + "equals" Fp2.zero + (Fp2.div + (f + "15575901816686564911695664888216387456052221682627972942517791604577648718373" + "4074937799975106177793779962697923305068793270789454615564141613169465455800") + Fp2.zero) + +let test_neg () = + let a = + f + "15368678686417640939410004345384562254158052587497136227450829712392291541108" + "12426777655707647255726782198814182406150331554284032768572910376412464025883" + in + Alcotest.(check fp2) + "equals" + (f + "6519564185421634282836401399872712834538258569800687435238208182252934667475" + "9461465216131627966519623546443092682545979603013790894116127518232762182700") + (Fp2.neg a); + Alcotest.(check fp2) "equals" Fp2.zero (Fp2.add a (Fp2.neg a)) + +(* + ======================================================================== + multiplication + (11432323160814065864909960700129434587946493783507438700207928706738844119449, 3577409691447205319998726389612822121306006806114173782377028664710279109705) + = (11986626879605260056244902575435385107082913504490111545527498340756305531092, 4818173862427609729222592608595100262022473140100027294220533759712947640512) + * (10960597259346866798564665428177212161645337477705428048049421023510569201652, 17436818457664572454171416757524427055427814808946492827966722145668155530853) + division + (13419365922711575620625084257870785388560921645234544455343469677828280907673, 10223115143172140697158827396733687716959251907957043435822040517781915535192) + = (5246975579657097196126930177221162000731887408914575127136817635764078403513, 8292723131410576043572459680046943309298666101925027054973960663999641620758) + / (1886591325401429366956197735571233984030593880349024014320016420187469008836, 1695527069488388814625626614375814226660393081483975270883481133990211272753) + multiplicative inverse + (1787014003341926122725734505165381529419163570425444088707405348791540627564, 3072592406275333378136945391036461572636993084074366352517366836269594879853) + = ~/(5846831721439900768152735149922159251212076902324998439686567351122268450217, 15974811941941150933977163259953349229614582170845394085548476048658545196484) +*) + +let test_inv () = + let a = + f + "5846831721439900768152735149922159251212076902324998439686567351122268450217" + "15974811941941150933977163259953349229614582170845394085548476048658545196484" + in + Alcotest.(check fp2) + "equals" + (f + "1787014003341926122725734505165381529419163570425444088707405348791540627564" + "3072592406275333378136945391036461572636993084074366352517366836269594879853") + (Fp2.inv a); + Alcotest.(check fp2) "equals" Fp2.one (Fp2.mul a (Fp2.inv a)); + Alcotest.(check fp2) "equals" Fp2.zero (Fp2.inv Fp2.zero); + Alcotest.(check fp2) "equals" Fp2.one (Fp2.inv Fp2.one) + +let tests = + Alcotest. + [ + test_case "Addition" `Quick test_add; + test_case "Subtraction" `Quick test_sub; + test_case "Multiplication" `Quick test_mul; + test_case "Division" `Quick test_div; + test_case "Additive Inverse" `Quick test_neg; + test_case "Multiplicative Inverse" `Quick test_inv; + ] diff --git a/test/test_plonkers.ml b/test/test_plonkers.ml index 16db4ca..672cad5 100644 --- a/test/test_plonkers.ml +++ b/test/test_plonkers.ml @@ -1 +1,2 @@ -let () = Alcotest.run "Plonkers" [ ("Fp", Test_fp.tests) ] +let () = + Alcotest.run "Plonkers" [ ("Fp", Test_fp.tests); ("Fp2", Test_fp2.tests) ] |
