aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2024-10-06 15:02:02 +0200
committerNicholas Rodrigues Lordello <[email protected]>2024-10-06 15:07:17 +0200
commit2bf12c1d8c9c879d9d3907457e36e8b1b3217315 (patch)
treeab34c9871d7fb9f8ee36ebb495a0531c72ba2c84
parentee04df44b06c4cfe566cb5d10f0b575f6ae99683 (diff)
Fully Test EC Implementation
Fully test the elliptic curve implementations, including the curve over Fp2 and Fp12, as well as twisting points from E2 to E12.
-rw-r--r--lib/curve.ml30
-rw-r--r--lib/curves.ml23
-rw-r--r--lib/curves.mli7
-rw-r--r--lib/plonkers.ml2
-rw-r--r--test/test_curves.ml34
-rw-r--r--test/test_e.ml23
-rw-r--r--test/test_e12.ml117
-rw-r--r--test/test_e2.ml125
-rw-r--r--test/test_plonkers.ml2
9 files changed, 305 insertions, 58 deletions
diff --git a/lib/curve.ml b/lib/curve.ml
index fe8a20a..4d1fa5b 100644
--- a/lib/curve.ml
+++ b/lib/curve.ml
@@ -1,5 +1,12 @@
(** Elliptic Curve. *)
+exception Not_on_curve
+(* Raised when creating a curve element with a point that not on the curve. *)
+
+exception Not_in_canonical_subgroup
+(* Raised when creating a curve element with a point that not part of the
+ canonical subgroup defined by the curve's generator point. *)
+
(** Module signature for an elliptic curve. *)
module type S = sig
module F : Field.S
@@ -18,17 +25,36 @@ module type S = sig
(** The order of the curve. *)
val of_coords : F.t * F.t -> t
- (** [of_coords x y] is the curve element with coordinates [x] and [y]. *)
+ (** [of_coords x y] is the curve element with coordinates [x] and [y]. Raises
+ [Not_on_curve] if the coordinates do not correspond to a point that
+ satisfies the elliptic curve equation, and [Not_in_canonical_subgroup] if
+ the point is not torsio-free. *)
val to_coords : t -> (F.t * F.t) option
(** [to_coords a] returns the coordinates of [a]. *)
val of_string : string -> t
- (** [of_string s] returns the curve element represented by [s]. *)
+ (** [of_string s] returns the curve element represented by [s]. Raises
+ [Not_on_curve] if the coordinates do not correspond to a point that
+ satisfies the elliptic curve equation, and [Not_in_canonical_subgroup] if
+ the point is not torsio-free. *)
val to_string : t -> string
(** [to_string a] returns the string representation of [a]. *)
+ (** Unsafe point construction functions. *)
+ module Unsafe : sig
+ val of_coords_unchecked : F.t * F.t -> t
+ (** [of_coords_unchecked a] is the curve element with coordinates [x] and
+ [y]. This function does not check whether the coordinates are actually
+ on the curve or torsion free. *)
+
+ val of_string_unchecked : string -> t
+ (** [of_string_unchecked s] returns the curve element represented by [s].
+ This function does not check whether the coordinates are actually on the
+ curve or torsion free. *)
+ end
+
val equal : t -> t -> bool
(** [equal a b] returns [true] if [a] is equal to [b]. *)
diff --git a/lib/curves.ml b/lib/curves.ml
index 7d6841a..16d5083 100644
--- a/lib/curves.ml
+++ b/lib/curves.ml
@@ -1,6 +1,3 @@
-exception Not_on_curve
-exception Not_in_canonical_subgroup
-
let n =
Z.of_string
"52435875175126190479447740508185965837690552500527637822603658699938581184513"
@@ -77,11 +74,12 @@ end) : Curve.S with type t = E.F.t point = struct
let x3 = F.(x * x * x) in
F.(equal y2 (x3 + E.b))
in
- if is_on_curve then () else raise Not_on_curve;
+ if is_on_curve then () else raise Curve.Not_on_curve;
let is_in_canonical_subgroup = scalar_mul n p |> equal Inf in
- if is_in_canonical_subgroup then p else raise Not_in_canonical_subgroup
+ if is_in_canonical_subgroup then p
+ else raise Curve.Not_in_canonical_subgroup
- let of_string s =
+ let of_string_f s f =
let strip_braces s =
if String.starts_with ~prefix:"{" s && String.ends_with ~suffix:"}" s then
String.sub s 1 (String.length s - 2)
@@ -89,9 +87,16 @@ end) : Curve.S with type t = E.F.t point = struct
in
match strip_braces s |> String.split_on_char ';' with
| [ "∞" ] -> Inf
- | [ x; y ] -> of_coords (F.of_string x, F.of_string y)
+ | [ x; y ] -> f (F.of_string x, F.of_string y)
| _ -> invalid_arg "wrong number of elements, expected {x;y}"
+ let of_string s = of_string_f s of_coords
+
+ module Unsafe = struct
+ let of_coords_unchecked (x, y) = Point (x, y)
+ let of_string_unchecked s = of_string_f s of_coords_unchecked
+ end
+
let ( ~- ) = neg
let ( + ) = add
let ( - ) = sub
@@ -128,6 +133,8 @@ let twist_coords (x, y) =
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 c0, c1 = Fp2.to_coeffs a in
let c0' = Fp.(c0 - c1) in
@@ -135,7 +142,7 @@ let twist_coords (x, y) =
|> Fp12.of_coeffs
in
let nx, ny = (iso x, iso y) in
- Fp12.(nx * w2, ny * w3)
+ Fp12.(nx * w2inv, ny * w3inv)
module E2 = Eimpl (struct
module F = Fields.Fp2
diff --git a/lib/curves.mli b/lib/curves.mli
index e760d9e..ce66b73 100644
--- a/lib/curves.mli
+++ b/lib/curves.mli
@@ -1,12 +1,5 @@
(** BLS12-381 curves used for pairings *)
-exception Not_on_curve
-(* Raised when creating a curve element with a point that not on the curve. *)
-
-exception Not_in_canonical_subgroup
-(* Raised when creating a curve element with a point that not part of the
- canonical subgroup defined by the curve's generator point. *)
-
module E : Curve.S
(* The BLS12-381 curve over [Fields.Fp]. *)
diff --git a/lib/plonkers.ml b/lib/plonkers.ml
index f18fe19..9c7c858 100644
--- a/lib/plonkers.ml
+++ b/lib/plonkers.ml
@@ -1,2 +1,4 @@
+module Field = Field
module Fields = Fields
+module Curve = Curve
module Curves = Curves
diff --git a/test/test_curves.ml b/test/test_curves.ml
new file mode 100644
index 0000000..d3067cc
--- /dev/null
+++ b/test/test_curves.ml
@@ -0,0 +1,34 @@
+module E2 = Plonkers.Curves.E2
+module E12 = Plonkers.Curves.E12
+
+let twist = Plonkers.Curves.twist
+let f2 x y = E2.Unsafe.of_coords_unchecked E2.F.(of_string x, of_string y)
+let e12 = Testable.make E12.to_string E12.equal
+let f12 x y = E12.Unsafe.of_coords_unchecked E12.F.(of_string x, of_string y)
+
+let test_twist () =
+ let a =
+ f2
+ "(2109508776193857273074766488410969484271072993610345872414603016106553977333420070441822701292764210251579683428522,3122689516821528327894678317146234429793657826538065999063801814299529533586972120357919860380161818422867843226102)"
+ "(1096288604042692121608580288022426533189175239933724612935896261545208643656951679037482053510338623653634164811915,1170946969994232162449075722035284212661104220582295241050315080132560517926924365377907307339371864116051233088516)"
+ in
+ let b =
+ f2
+ "(1249927438163201574866859585937475005480631005557973853506467103895431793859087897855543227858106779532090068273481,3316136808132952082246855662072348414235919944257935059559541300069920601748305368149334910959656758372765105616868)"
+ "(81743811270164858346488114131996653093309264515814483449447422378937968491505855199671526555077281996286356618484,1084133614285958528564866737145829519643307320120470464135106404047274008544004224502685177052190672561703116812406)"
+ in
+ Alcotest.(check e12)
+ "equals"
+ (E2.add a b |> twist)
+ (E12.add (twist a) (twist b));
+ Alcotest.(check e12)
+ "equals"
+ (f2
+ "(1540172915528940641891926116690100808044692623394504676677921357014659026860895709663805875815315907372328317299798,3225331128895415615728432739064042093474368865193551026434624504244808875880021719074327461050513525930991560321555)"
+ "(3821166946900648900183417606277705329456497739035474648486543021526386081458734393093714179436014613784026951537426,3271903498375440374313099549367347360713300158269487781915162011178571021409894476750657145495514462848190134148570)"
+ |> twist)
+ (f12
+ "(0,0,0,0,1540172915528940641891926116690100808044692623394504676677921357014659026860895709663805875815315907372328317299798,0,0,0,0,0,2843783884294071183627148224054922720993279530869027117544380641677090749754981936926604607182106641298278757790772,0)"
+ "(0,0,0,3821166946900648900183417606277705329456497739035474648486543021526386081458734393093714179436014613784026951537426,0,0,0,0,0,3727777830959063130482630797280725172185284029556014452046367630950124120466417906271159112158765588569975863865359,0,0)")
+
+let tests = Alcotest.[ test_case "Twist" `Quick test_twist ]
diff --git a/test/test_e.ml b/test/test_e.ml
index 0238544..d287831 100644
--- a/test/test_e.ml
+++ b/test/test_e.ml
@@ -1,22 +1,19 @@
module E = Plonkers.Curves.E
let e = Testable.make E.to_string E.equal
-let f x y = E.of_coords E.F.(of_string x, of_string y)
+let f x y = E.Unsafe.of_coords_unchecked E.F.(of_string x, of_string y)
let test_strconv () =
let a =
f
- "2376759339040134764212872788353941124992112438470797710505260327947509392797392045352255582857163267674668587297510"
- "2680611318541745317201589928800822277854035293387537860457713871028948345023926973028031532994931068370323829388300"
+ "704648225832996754434548433071026029921434261490324062624046528007387592893305783031149959865132570914021074338890"
+ "2790809590887885290880356803296049708176452905760090772131241016249472860044571856060358509135396867961864286840213"
in
- Alcotest.(check string)
- "equals"
- "{2376759339040134764212872788353941124992112438470797710505260327947509392797392045352255582857163267674668587297510;2680611318541745317201589928800822277854035293387537860457713871028948345023926973028031532994931068370323829388300}"
- (E.to_string a);
- Alcotest.(check e)
- "equals" a
- (E.of_string
- "{2376759339040134764212872788353941124992112438470797710505260327947509392797392045352255582857163267674668587297510;2680611318541745317201589928800822277854035293387537860457713871028948345023926973028031532994931068370323829388300}")
+ let s =
+ "{704648225832996754434548433071026029921434261490324062624046528007387592893305783031149959865132570914021074338890;2790809590887885290880356803296049708176452905760090772131241016249472860044571856060358509135396867961864286840213}"
+ in
+ Alcotest.(check string) "equals" s (E.to_string a);
+ Alcotest.(check e) "equals" a (E.of_string s)
let test_curve () =
Alcotest.(check bool)
@@ -25,7 +22,7 @@ let test_curve () =
E.of_string "{0;0}" |> ignore;
false
with
- | Plonkers.Curves.Not_on_curve -> true
+ | Plonkers.Curve.Not_on_curve -> true
| _ -> false)
let test_subgroup () =
@@ -37,7 +34,7 @@ let test_subgroup () =
|> ignore;
false
with
- | Plonkers.Curves.Not_in_canonical_subgroup -> true
+ | Plonkers.Curve.Not_in_canonical_subgroup -> true
| _ -> false)
let test_add () =
diff --git a/test/test_e12.ml b/test/test_e12.ml
new file mode 100644
index 0000000..0af2df6
--- /dev/null
+++ b/test/test_e12.ml
@@ -0,0 +1,117 @@
+module E12 = Plonkers.Curves.E12
+
+let e12 = Testable.make E12.to_string E12.equal
+let f x y = E12.Unsafe.of_coords_unchecked E12.F.(of_string x, of_string y)
+
+let test_strconv () =
+ let a =
+ f
+ "(0,0,0,0,3091671843177195696877148918276666486988770529143376300427304497723309492066409434415541799043648972299038428357487,0,0,0,0,0,3815085543371984344309954270809206701981910198711326563248651510366005113177104447633168433375165299595858892446448,0)"
+ "(0,0,0,3216482895116454205593926684868111924113047860439420435341632962637183676219338960167585843036993749097939480933003,0,0,0,0,0,2109732944141953385179814554641632616877235126971969222393178311770562775710586207386814389023669882647548613327750,0,0)"
+ in
+ let s =
+ "{(0,0,0,0,3091671843177195696877148918276666486988770529143376300427304497723309492066409434415541799043648972299038428357487,0,0,0,0,0,3815085543371984344309954270809206701981910198711326563248651510366005113177104447633168433375165299595858892446448,0);(0,0,0,3216482895116454205593926684868111924113047860439420435341632962637183676219338960167585843036993749097939480933003,0,0,0,0,0,2109732944141953385179814554641632616877235126971969222393178311770562775710586207386814389023669882647548613327750,0,0)}"
+ in
+ Alcotest.(check string) "equals" s (E12.to_string a);
+ Alcotest.(check e12) "equals" a (E12.of_string s)
+
+let test_curve () =
+ Alcotest.(check bool)
+ "fails" true
+ (try
+ E12.of_string "{(0,0,0,0,0,0,0,0,0,0,0,0);(0,0,0,0,0,0,0,0,0,0,0,0)}"
+ |> ignore;
+ false
+ with
+ | Plonkers.Curve.Not_on_curve -> true
+ | _ -> false)
+
+let test_subgroup () =
+ Alcotest.(check bool)
+ "fails" true
+ (try
+ E12.of_string
+ "{(0,0,0,0,1892439472479153589801064982851113576513855369383752766070453260535595479925033899105674578530308757833348467758334,0,0,0,0,0,3933473072416727749376352748178029259523619754763125185527750551907955966209860004099303009334745298312250252056103,0);(0,0,0,794551614141569136399571058573462200719130525886688829309738555530265772199592670562441013794848828639618754399483,0,0,0,0,0,3192958957310966052428309415378437932193540333890835514256315804018770631464911425978565124930935819694042257078857,0,0)}"
+ |> ignore;
+ false
+ with
+ | Plonkers.Curve.Not_in_canonical_subgroup -> true
+ | _ -> false)
+
+let test_add () =
+ Alcotest.(check e12)
+ "equals"
+ (f
+ "(0,0,0,0,2929451176954387799194033409256344834898378201802754981173591542622666580295883587745860521250662884121876062549983,0,0,0,0,0,3461759878132083262758004943698898296800121697473199295622886337999821087592477923031706633208800009194815413801518,0)"
+ "(0,0,0,942624260271445495579165381526574569202111666876112362964832930049127003568811892520272165788145686052177002469346,0,0,0,0,0,3359602987414003489897229475537961953001886362351876752895373868269680338526524405951089727852832481417055207619739,0,0)")
+ (E12.add
+ (f
+ "(0,0,0,0,3577779649806248054643775548832767759316348940252471854726074628830920332172047644506373975311425491914524969724781,0,0,0,0,0,2399602958687966675444499051795357243399654745487511551062570132634625817263531954834246389717858349513538423979839,0)"
+ "(0,0,0,3469592015573153874932067971174117536042353913949658146829883594661681707207832993352017346593533677053567177917365,0,0,0,0,0,1151400766283648126691959067795471138907053142308666569304665656476264270870733893928854872692788980876149632731266,0,0)")
+ (f
+ "(0,0,0,0,3359139998119707196528515154517032529878638458125880871059769187012893082790375046547813363647182669061408112484760,0,0,0,0,0,62497013792237496829604596520223735291518619338562343287921465074342037807885618452846713288828357151810745569900,0)"
+ "(0,0,0,2160638480464909694045638391084460619982867776766949998529832158649043460368937062272954230999807453239744952753962,0,0,0,0,0,1126329043102416166228707569418335696072154646728752961333880357646274870334482490410779225488804211857673854639763,0,0)"))
+
+let test_sub () =
+ Alcotest.(check e12)
+ "equals"
+ (f
+ "(0,0,0,0,260820864302800499055951456737734696520749024683574951158372641177953505726197462888820002139337144943486830755127,0,0,0,0,0,2309504807264682131023954645694244888958846929437569256970234438110912672798339047228059898746397460880680243113234,0)"
+ "(0,0,0,1884262644549124297286999423197037917702698957768720465436456735668246940838254360191765967749870609253415859480075,0,0,0,0,0,1458765347227296194079361565401588615835349340270921870155075823051943355511654368453233851359781030485555331973821,0,0)")
+ (E12.sub
+ (f
+ "(0,0,0,0,2548602455258519267027441838112041855729507725494336811234353266047952196432771974216852033238608280273800578504341,0,0,0,0,0,2506983850042387088709693983583400906243074258926729247169056174398175244377107659017228070877516224003507328999567,0)"
+ "(0,0,0,1697001650665816015571508376404481973733665579127989008532059292342460117240801454003662803080007480979986625768578,0,0,0,0,0,786940995340872797209857163071225469740844223100851966708730789135747927137475425247846950807186157855050793117595,0,0)")
+ (f
+ "(0,0,0,0,335585570380237738269479499019064005630181432684740610104387294850079646947794944339384592807744830934034358319148,0,0,0,0,0,3928039984576404929321729629401457719785598103596404127762810286191874666699102144281999415955166246019051362402625,0)"
+ "(0,0,0,2738843309836194684111788072521876850689450836525674623422145438705528038163470601918566915043088888961341964517564,0,0,0,0,0,1712943099674700879795417735364378450430564895281158485064468710245655725591367633459097560637616611829823884827868,0,0)"))
+
+let test_scalar_mul () =
+ Alcotest.(check e12)
+ "equals"
+ (f
+ "(0,0,0,0,3357855297579876784089148534308911222130978631684443825955438012453799536767627749242490964002530400421373781469533,0,0,0,0,0,2677804744293618541446194559207414126615217723294705750716365310934351437624898175352847955786724875817376614717219,0)"
+ "(0,0,0,1915502024687773287090662296267210584286994097198288317665526658659200922203992392110288118787661761019927810880977,0,0,0,0,0,1760921192099020069214533105397817595823932059994647772858020372402134581938309465971031516925918886223193490944630,0,0)")
+ (E12.scalar_mul
+ (Z.of_string
+ "703594376515145504600149806566586228383250905518012106197988171833561726440")
+ (f
+ "(0,0,0,0,1749021454757251805466302363090849032461428103311018075473114210413137797790930850580625254459899665288368449856937,0,0,0,0,0,2186129250262469085814801856197751909890991632841241156466941822089525090504923468858070412713902752872342023901749,0)"
+ "(0,0,0,886919149452047606753370501423204305132698474831088324208349828000112295671876445794020761886983391377923459546785,0,0,0,0,0,2884680018184262851673782573133878840881948626212500716086567795728465064168792509223042657485292396482495135092395,0,0)"))
+
+let test_neg () =
+ Alcotest.(check e12)
+ "equals"
+ (f
+ "(0,0,0,0,2510731992258037488917923072779386912130593748560772070925187720759618936807988199231840501866378139127146033382420,0,0,0,0,0,139994380365044868720503499540211498999674505225457299682186297734122616101065195677155394940127752473050846199674,0)"
+ "(0,0,0,2953323231939446131835641091801583289848813559944641025189986405134032624530476053107287527843032990989824459319311,0,0,0,0,0,3006319017089597825899390606645412383505581997773614203793702942169217973661078287028360663533881006950619741641054,0,0)")
+ (E12.neg
+ (f
+ "(0,0,0,0,2510731992258037488917923072779386912130593748560772070925187720759618936807988199231840501866378139127146033382420,0,0,0,0,0,139994380365044868720503499540211498999674505225457299682186297734122616101065195677155394940127752473050846199674,0)"
+ "(0,0,0,1049086323282221261582148733934320866708069259994366860142071730989999025960361811335400101285982673048069813240476,0,0,0,0,0,996090538132069567518399219090491773051300822165393681538355193954813676829759577414326965595134657087274530918733,0,0)"));
+ let a =
+ f
+ "(0,0,0,0,174596558160867992931929381678564420867047966812951865620128480620036445313093830658810814465792025017812429878751,0,0,0,0,0,3492629321158940080093431519188658666684469676506878174071465032762267492492939017933177917408814913264887997167785,0)"
+ "(0,0,0,2480687101688729026958456527114342859137384944778496301906088322119864429059527285116213514015556557832700420458645,0,0,0,0,0,1764449632710224012943205738233577179745318497967961642347638292997400684086372242247269616317573230446730496336696,0,0)"
+ in
+ Alcotest.(check e12) "equals" E12.inf (E12.add a (E12.neg a))
+
+let test_g () =
+ Alcotest.(check e12)
+ "equals" E12.g
+ (f
+ "(0,0,0,0,352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160,0,0,0,0,0,1353221637328373545892060349371360746048220186341936159219732281025920769516621702780080981380240920942561918531299,0)"
+ "(0,0,0,1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905,0,0,0,0,0,1472406309213353956798468635167385199506877017588472562248853724925831900559844855499883907296819622485916638215732,0,0)")
+
+let tests =
+ Alcotest.
+ [
+ test_case "String Conversion" `Quick test_strconv;
+ test_case "Is On Curve" `Quick test_curve;
+ test_case "Is In Canonical Subgroup" `Quick test_subgroup;
+ test_case "Addition" `Quick test_add;
+ test_case "Subtraction" `Quick test_sub;
+ test_case "Scalar Multiplication" `Quick test_scalar_mul;
+ test_case "Additive Inverse" `Quick test_neg;
+ test_case "Generator" `Quick test_g;
+ ]
diff --git a/test/test_e2.ml b/test/test_e2.ml
index 3d32e2e..3dd5c22 100644
--- a/test/test_e2.ml
+++ b/test/test_e2.ml
@@ -1,17 +1,20 @@
module E2 = Plonkers.Curves.E2
-let e = Testable.make E2.to_string E2.equal
-let f x y = E2.of_coords E2.F.(of_string x, of_string y)
+let e2 = Testable.make E2.to_string E2.equal
+let f x y = E2.Unsafe.of_coords_unchecked E2.F.(of_string x, of_string y)
let test_strconv () =
- let a = f "(3923956693534187645032295788275476556797005515401770698112017479322355330641673615130042139434743269864444600110358,3756061633580935129993713607807160763781916371393378245686685529371403362444782981986040174265375059065676026913571)" "(218832824795677895374510600285671262542496010031251688921012107594797186788029893705858653131702371905931715865189,3031692580053002544887243919412706690059252512665311278727474095626933035689247794033137231742422077733611734919005)" in
- Alcotest.(check string)
- "equals"
- "{(3923956693534187645032295788275476556797005515401770698112017479322355330641673615130042139434743269864444600110358,3756061633580935129993713607807160763781916371393378245686685529371403362444782981986040174265375059065676026913571);(218832824795677895374510600285671262542496010031251688921012107594797186788029893705858653131702371905931715865189,3031692580053002544887243919412706690059252512665311278727474095626933035689247794033137231742422077733611734919005)}"
- (E2.to_string a);
- Alcotest.(check e)
- "equals" a
- (E2.of_string "{(3923956693534187645032295788275476556797005515401770698112017479322355330641673615130042139434743269864444600110358,3756061633580935129993713607807160763781916371393378245686685529371403362444782981986040174265375059065676026913571);(218832824795677895374510600285671262542496010031251688921012107594797186788029893705858653131702371905931715865189,3031692580053002544887243919412706690059252512665311278727474095626933035689247794033137231742422077733611734919005)}")
+ let a =
+ f
+ "(1995966487400060513418002023207574157295305948670062141574847467571155809406706098785462170240887564093686629877623,662886336588505248783096013641071887822498506927882698632762069520277859710519968066621437591913838510294024221845)"
+ "(2118133940922730619706689117607342224796346036207279785462459268901402082964459754036268590522181697629582678327818,3180158093976603722423949631887924556492770394116171123059316509806286695323674296949805082557706278821978684422237)"
+ in
+ let s =
+ "{(1995966487400060513418002023207574157295305948670062141574847467571155809406706098785462170240887564093686629877623,662886336588505248783096013641071887822498506927882698632762069520277859710519968066621437591913838510294024221845);(2118133940922730619706689117607342224796346036207279785462459268901402082964459754036268590522181697629582678327818,3180158093976603722423949631887924556492770394116171123059316509806286695323674296949805082557706278821978684422237)}"
+ in
+ Alcotest.(check string) "equals" s (E2.to_string a);
+ Alcotest.(check e2) "equals" a (E2.of_string s)
+
let test_curve () =
Alcotest.(check bool)
"fails" true
@@ -19,48 +22,109 @@ let test_curve () =
E2.of_string "{(0,0);(0,0)}" |> ignore;
false
with
- | Plonkers.Curves.Not_on_curve -> true
+ | Plonkers.Curve.Not_on_curve -> true
| _ -> false)
+
let test_subgroup () =
Alcotest.(check bool)
"fails" true
(try
- E2.of_string "{(1892439472479153589801064982851113576513855369383752766070453260535595479925033899105674578530308757833348467758334,1754566506869274301718190827735363782447329239031987366461838092103444111363078178418905338941768026382060426750966);(794551614141569136399571058573462200719130525886688829309738555530265772199592670562441013794848828639618754399483,3178059973541833847838400063594433908549328373729351972490312027443775384638577658076883634527704803989808995997410)}" |> ignore;
+ E2.of_string
+ "{(1892439472479153589801064982851113576513855369383752766070453260535595479925033899105674578530308757833348467758334,1754566506869274301718190827735363782447329239031987366461838092103444111363078178418905338941768026382060426750966);(794551614141569136399571058573462200719130525886688829309738555530265772199592670562441013794848828639618754399483,3178059973541833847838400063594433908549328373729351972490312027443775384638577658076883634527704803989808995997410)}"
+ |> ignore;
false
with
- | Plonkers.Curves.Not_in_canonical_subgroup -> true
+ | Plonkers.Curve.Not_in_canonical_subgroup -> true
| _ -> false)
+
let test_add () =
Alcotest.(check e2)
"equals"
- (f "(2781343196420519423632422433587022540727162848306722364576877777060569884337547027596131977437417838859630676701276,1562467798215687776242522022358259549420197197101293070974570291387202520220768782383682920655951887531502364910943)" "(3457841611713041343433378215845200350595339514173366640511044400430736079303588026590492575458181436146388806048227,7228685485454948383961090818997050729503296774515502493264656775599897514746848883912818267380288160758952637281)")
+ (f
+ "(499713417605359223674748473758724973948438124705509202302245471290609292306320586538959574203334940457575903991711,3849381523047393672660382657163613807212712961677974554645092181592292011827458146770352521145227167398610078418185)"
+ "(3750738009713936929372241922830934361036892789616987325774050932170221795352544491858695643172294211282599110319834,3561628900031869771392652210785168204755113200504180138415139599905464331701794168749876553728218422931049173996790)")
(E2.add
- (f "(599086761411406415954402349202245343302961031710865047455679311068681525149248601596200133628584188948522354931706,3511452255954981131282546809158670518933017501665934770275055677464908859882909088952822745951471523295910594620902)" "(1858532949424024310983629891100206243481849330010648636074347012778532917202183541352107441782262852186868167906111,725462570126405273129434809948141447008736630543695800107361780351341875465831434362474077467747725130225103267715)")
- (f "(986494921070683276373543285472007888620184198150171094905637237552953519277411505118726407466333007412685045565931,2016401256337455957339336505249631820209884653501871827809766163209347917563971639588398616420005472314360364831469)" "(3297017169562975982847858172017863660737484791751375684556582394877628290551172601568539124399307623499009873598715,3431600145727956472898569788713031962824194739791483457998849536322695338531728554192550359623310559846707423449600)"))
+ (f
+ "(22988558892716969390996985732585240683297793773456313893700754641768024025112560131028908246472269831559878289575,2129706665424714060840367155348060356419393088613610263996150242693619717302217969445633229711091283126869298324191)"
+ "(326344468552893907028622086721957524007052308610498663186676327256007196980344862961459048761730914514101678031123,3578939464694739647421989794082523254572164841250176471270721235280032616122779184472701860960655547698666209531163)")
+ (f
+ "(3695633206715132410492337765882907918857795221238051472281688705875004174483167692618918438807066911317626485478957,2976733649841946843632279759860096327423816839648446597596824846941966186914175551584845353423964217077348053962065)"
+ "(49466427428220367265805901178013626540803904016880618718453870927457307757178707263045811558641515336191874310112,2193129241285505962248559104914181807138658074292296828774608320812063550960488358009160505763316933125829480800311)"))
+
let test_sub () =
Alcotest.(check e2)
"equals"
- (f "(1107616141833631890648220034802985012266585522465503062630624461469787068817507662710486135292720797212734918198603,2475301156396341518860712215478236964158637090539965287548370374197365724799357580121235669362940434636566182494987)" "(870790757455156722761569961180122448867292520888798362106398811030352353495900008983755243236944998134351895598415,3397233965917193203508893996752163354491083837521365739261804011181926354684486210978984258146460301225646145999715)")
+ (f
+ "(2312381534305864038524856410951813412729532000275307896810795592963361639762761897364492993639180834599053426654921,2941946573854091849965088183943891771101444533626394142063742950941901356314086230253727457469500594733301574969126)"
+ "(3177038204107211918017473977040505404653337824416553912249365797499278408762074130788569657467795326880900042739300,3911466467618211275615446067985091611817124681549927950440782870032642030586794441825300408067249342511123677227979)")
(E2.sub
- (f "(3270310664207088013254320850731367986355912543633525689785953976375884082419887229176064436799656868307602730661122,2635383553308960798103821213099798739134889006717670307440032270217455570436208767533188105071643978079872249820753)" "(2500912375665846368322740960797430066858985989087673976899719672987173330102143464287179769799965720377994734709570,2144057285800108792412733050101667056571828997793081230558422324103798194453682994763365415691475509985762303855717)")
- (f "(1002981184304042612904273749964727487113808230133976959870733118775016088585411325500383128284551807500598508585644,598424802931660017567261609928489437916087628133865008176623363617479728506523154891647018514707820974678495782908)" "(2779356086241559529056963756527484755728688125754147609395562536891566602273974746860594639939216922892175330447034,3034817612905701460940481106756293661790214078415811023668574321046776481025701166890033025634359455996376523478767)"))
+ (f
+ "(3084805454959641404340859217267539384943233503012252203041979288484567877164842557655530989970590180692844056705034,1032626932734486057118525818914103317004948135891523645620202784883454249860761416804451664418540921374537112657339)"
+ "(567609968615494874083532393538964631912521339405909452432595170197158682938070010563153437595578466221475239150664,3965723577064707140049831298501225403010472139339519470330943732886849966270836690577682873296098666976942450913027)")
+ (f
+ "(1437969629809004608879055525849883700261548412934340004984286831449951101506825523108895949333702907561636531462862,3378050752672563498305643671412654890539264706582196808663756002199076494302859579346688785007817086448260264744631)"
+ "(3688003734830478721040484715021921439921663750934892483251989073660346159418502949754053418120146012703162584349321,294600807465112254390070142232849516815757700388555441412905845762862521178052505971852404123896853420248399045909)"))
+
let test_scalar_mul () =
Alcotest.(check e2)
"equals"
- (f "(3810130678824403915647893326329407688276314060503782614041947303461689009432356906821404199448178849347940313756085,2220495839665515827548078136227893391699436344542032984335607669783765851101052216092924101081718980461722245873556)" "(1324329677491030278489696226427690673221937855962470233412076025336658621394897786383060111592866096007331531129621,3512664666449059568403903861280589932910233223607036913190860947335458748854213831919418684756565331416555243042063)")
+ (f
+ "(1092894641180253280627824475749822745538993676284211343464536443667704016213631359345660547916686207447599304492990,3324099030654709813055482585412186377145855773893664369906420608900967641626669259098384400094403575314217078271321)"
+ "(1687432397129777546869295921172489186821991613986891301985886124965631277467866465780351130557754827159028199904720,2313960103129372774774734178634617919906919386825218848510448126559550974987820849911034145924048313343848913976180)")
(E2.scalar_mul
- (f "(3523957907858391262907088016377865078521764246069022944438198465831924410822656113341965583598931999664598946541493,748349505869728007229892226148681803063559050483969510584997534258252794397186898676963942671456991144909405512254)" "(61385783622782903467689592421665098262656188504623244266983011351886936254296884079210942788445047788491270481484,3463618385364777232681821644033997541936274771912546823871472375117920764428834810172532177083933038531369619276039)")
- (Z.of_string "3815964398135289600381093205750983240026391694776236280842690252393462186522"))
+ (Z.of_string
+ "18231751679696756095586214113699656074182459369522013003988412807673749568724")
+ (f
+ "(2514664268577432573560291176315683541072067402533046184786583901096804522469891814129602032754241329292791735547731,1538891981432352671530281965293325684836353821970151860371636345672221993717129428051893539595256156434325115353328)"
+ "(1020197595243302833683556943176598276009062246952019251126700930527258255249495123842828836305850271186575937412091,957529907995660594751569367214519238040770457577242766357201002881030298493381525670720382223549185747922813057348)"))
+
let test_neg () =
Alcotest.(check e2)
"equals"
- (f "(2832125252114490079090653551459419524550307378594993307426217469035124656817814921206351452333476502825851582942923,2774490150344266921534652087680983095842469113614636003252657826642251958285432375564301828410555769234605178724106)" "(3907853156448358489448342566233259975620515054034191917571458781588245723702401939835730459276085380301654647561464,621222305739086507417127305122520269663755897943923075633864566129632606833658144665628603712138110532834908367647)")
- (E2.neg (f "(2832125252114490079090653551459419524550307378594993307426217469035124656817814921206351452333476502825851582942923,2774490150344266921534652087680983095842469113614636003252657826642251958285432375564301828410555769234605178724106)" "(94556398773308903969447259502644180936367765904815967760599354535785926788435924606957169852930283736239624998323,3381187249482580886000662520613383886893126921995084809698193569994399043657179719777059025416877553505059364192140)"));
- let a = f "(3830461410083936889354928320639781774706170548021823825963924131487021843625227208879673806496421446697530627344289,797122006267364646383517126124142012780153690255487734905658092239678791458112328220739855822765190574893521825364)" "(3662537629577369606153265166748780089980738909813423444965726649520816736216443130712496521548460748658055145504590,2709007645146089274914263692534911298242702699525491524191698098321889897252066448584301600091985544894580898484468)" in
- Alcotest.(check e2)
+ (f
+ "(1545269364848460984490225670755503760473037616438596925671109140482515442704584830885661527842646221605111107590693,1267478445196980059681345977877996221353505182887596481010812841900715170279481217659186985334416192910417976590132)"
+ "(2138086320981055187350247210500555749249791226828379441597708773600979851439979713108017155176731805322171737993018,2923510363463074904143568736318190088176676574358556535023319924451820130012383135761860188454542388330577656123700)")
+ (E2.neg
+ (f
+ "(1545269364848460984490225670755503760473037616438596925671109140482515442704584830885661527842646221605111107590693,1267478445196980059681345977877996221353505182887596481010812841900715170279481217659186985334416192910417976590132)"
+ "(1864323234240612206067542615235348407307091593110628443734349362523051799050858151334670473952283858715722534566769,1078899191758592489274221089417714068380206245580451350308738211672211520478454728680827440674473275707316616436087)"));
+ let a =
+ f
+ "(2145319835080253046126157108405019899066932648811331837314642288549615338163523922360993646054737343377628571487304,1358459155514781387151184879883145700362295043361724816677598042273140202854606635229527873146151979028028462660866)"
+ "(2261863074976855596039999928645826587478562702008490591716114111186951516904330390847333904655429074513962463071272,2015356306298728612773777903102863638865287815742386042734473839158493228418734519947389685935269331153485256794451)"
+ in
+ Alcotest.(check e2) "equals" E2.inf (E2.add a (E2.neg a))
+
+let test_twist () =
+ let module E12 = Plonkers.Curves.E12 in
+ let e12 = Testable.make E12.to_string E12.equal in
+ let f12 x y =
+ E12.Unsafe.of_coords_unchecked E12.F.(of_string x, of_string y)
+ in
+ let twist = Plonkers.Curves.twist in
+ let a =
+ f
+ "(3669611369294948974634765224261160863259976623025174450692974024135660938677293949484127780340633015696505838316130,3090687185578156925132141476322078020023392467014425193206309553370795378348206159236670341492900186918308124507493)"
+ "(2779970239140385631920565954330663246292556112368464275383215059538548403396374960378890105420876329855513347894085,3097437972523464042565784881677563184964797100562369589471018391346730349842749358034624136069384567019997582090957)"
+ in
+ let b =
+ f
+ "(1053300572803981667384367752902994951183157118132662809599648130824356188957537236411558781666933412964027713776540,1445977520422141287162517647930196440114009706588757547597541156345622544583208585670790387386046081159986177959998)"
+ "(3689824775759870994521791010305891175032741515121577774537433898953647904897504950875889040684809164534200503484058,2853882551173372307457093223859059530229833448859184381902653127266710787574249534844087593772863369427604788749655)"
+ in
+ Alcotest.(check e12)
+ "equals"
+ (E2.add a b |> twist)
+ (E12.add (twist a) (twist b));
+ Alcotest.(check e12)
"equals"
- E2.inf
- (E2.add a (E2.neg a));
+ (f
+ "(2606921651592105754475529398132710526491771329060996833171782094578621322484869256778991328135243962740388516701670,2825253982791353034679973984849711146583580678189915790389883143879948292831336084622718804815562162657047466258624)"
+ "(1589868014688087566534303814516145532269762769759394338531872847925266893368410252824018876911895493136813971365761,778718326085510634191669420774555971516341962061804758334244302026416954528851727912605155423863203911476334494468)"
+ |> twist)
+ (f12
+ "(0,0,0,0,2606921651592105754475529398132710526491771329060996833171782094578621322484869256778991328135243962740388516701670,0,0,0,0,0,109166165599623640102222293358500310045904674564459478609050524650663485173233413921863738340159099958329474778477,0)"
+ "(0,0,0,1589868014688087566534303814516145532269762769759394338531872847925266893368410252824018876911895493136813971365761,0,0,0,0,0,1595629933309545230537577715997157297901731006120709152567214795112590855825639669765636953820491687406278317844247,0,0)")
let tests =
Alcotest.
@@ -68,4 +132,9 @@ let tests =
test_case "String Conversion" `Quick test_strconv;
test_case "Is On Curve" `Quick test_curve;
test_case "Is In Canonical Subgroup" `Quick test_subgroup;
+ test_case "Addition" `Quick test_add;
+ test_case "Subtraction" `Quick test_sub;
+ test_case "Scalar Multiplication" `Quick test_scalar_mul;
+ test_case "Additive Inverse" `Quick test_neg;
+ test_case "Twist" `Quick test_twist;
]
diff --git a/test/test_plonkers.ml b/test/test_plonkers.ml
index 17aaf19..90e94db 100644
--- a/test/test_plonkers.ml
+++ b/test/test_plonkers.ml
@@ -6,4 +6,6 @@ let () =
("Fp12", Test_fp12.tests);
("E", Test_e.tests);
("E2", Test_e2.tests);
+ ("E12", Test_e12.tests);
+ ("Curves", Test_curves.tests);
]