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; ]