From 2efcbfd67799537be7ebdb2d18495076b9ddcacc Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Sat, 14 Sep 2024 18:23:28 +0200 Subject: Implemented Elliptic Curve Operations This includes operations for all 3 curves relevant to PLONK. --- .gitignore | 1 + docs/journal/pages/torsion.md | 6 +++- dune-project | 4 +-- lib/curve.ml | 9 +++++ lib/curves.ml | 82 +++++++++++++++++++++++++++++++------------ lib/curves.mli | 9 ++++- lib/fields.ml | 11 ++++-- plonkers.opam | 3 +- test/test_e.ml | 53 ++++++++++++++++++++++++++++ test/test_fp12.ml | 26 ++------------ test/test_fp2.ml | 6 ++-- test/test_plonkers.ml | 5 ++- 12 files changed, 156 insertions(+), 59 deletions(-) create mode 100644 test/test_e.ml diff --git a/.gitignore b/.gitignore index 69fa449..876b9e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ _build/ +_opam/ diff --git a/docs/journal/pages/torsion.md b/docs/journal/pages/torsion.md index 947c85e..a439d35 100644 --- a/docs/journal/pages/torsion.md +++ b/docs/journal/pages/torsion.md @@ -2,7 +2,11 @@ # Torsion -TODO +One important gotcha for elliptic curves with a cofactor, is that it is possible to find points which satisfy the curve equation, but that are not part of the subgroup defined by the generator point (i.e., a point that cannot be expressed as `n * G` for some scalar `n` and the generator point `G`). This is referred to as "torsion". + +It is important to check that points are part of the main subgroup when they are constructed from coordinates, as points with torsion can be used in various attacks. The simplest way to check that a point is part of the main subgroup is to multiply it by the curve order. If the result is the point at infinity, then the point is part of the main subgroup, otherwise it is not (as the other subgroup orders are coprime with the main subgroup order; _I think_). + +There are more performant methods to check for subgroup membership using endomorphisms (an endomorphism in this context is a "trick"/optimization for computing very specific scalar multiplication on points - using a specific endomorphism, you can more quickly check subgroup inclusion - for more details, see links in resources). Since this is not particularly interesting in the context of PLONK, I decided no to implement this and simply use the scalar multiplication with the group order check for subgroup membership. # Resources diff --git a/dune-project b/dune-project index d4c66cc..fef700e 100644 --- a/dune-project +++ b/dune-project @@ -19,6 +19,6 @@ (name plonkers) (synopsis "PLONK From Scratch") (description "A from-scratch implementation of a PLONK zero knowledge proof for educational purposes.") - (depends ocaml dune zarith) + (depends ocaml dune zarith (alcotest :with-test)) (tags - (topics "zero knowledge" zkp plonk))) + (topics plonk "zero knowledge" zksnark))) diff --git a/lib/curve.ml b/lib/curve.ml index dd76690..fe8a20a 100644 --- a/lib/curve.ml +++ b/lib/curve.ml @@ -2,6 +2,9 @@ (** Module signature for an elliptic curve. *) module type S = sig + module F : Field.S + (** The field over which the curve is defined. *) + type t (** A type representing an element in an elliptic curve. *) @@ -14,6 +17,12 @@ module type S = sig val n : Z.t (** 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]. *) + + 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]. *) diff --git a/lib/curves.ml b/lib/curves.ml index 25d1afe..7d6841a 100644 --- a/lib/curves.ml +++ b/lib/curves.ml @@ -1,4 +1,5 @@ -open Fields +exception Not_on_curve +exception Not_in_canonical_subgroup let n = Z.of_string @@ -12,7 +13,9 @@ module Eimpl (E : sig val b : F.t val g : F.t * F.t end) : Curve.S with type t = E.F.t point = struct - type t = E.F.t point + module F = E.F + + type t = F.t point let inf = Inf @@ -21,39 +24,73 @@ end) : Curve.S with type t = E.F.t point = struct Point (gx, gy) let n = n + let to_coords = function Inf -> None | Point (x, y) -> Some (x, y) + + let to_string = function + | Inf -> "{∞}" + | Point (x, y) -> Printf.sprintf "{%s;%s}" (F.to_string x) (F.to_string y) let equal a b = match (a, b) with | Inf, Inf -> true - | Point (ax, ay), Point (bx, by) -> E.F.equal ax bx && E.F.equal ay by + | Point (ax, ay), Point (bx, by) -> F.equal ax bx && F.equal ay by | _ -> false - let neg = function Inf -> Inf | Point (x, y) -> Point (x, E.F.neg y) + let neg = function Inf -> Inf | Point (x, y) -> Point (x, F.neg y) let add a b = let line m ax ay bx _ = - let rx = E.F.((m * m) - ax - bx) in - let ry = E.F.(~-(ay + (m * (rx - ax)))) in + let rx = F.((m * m) - ax - bx) in + let ry = F.(~-(ay + (m * (rx - ax)))) in Point (rx, ry) in match (a, b) with | Inf, p | p, Inf -> p | Point (ax, ay), Point (bx, by) -> - if E.F.(equal ax bx) then - if E.F.(equal ay ~-by) then Inf + if F.(equal ax bx) then + if F.(equal ay ~-by) then Inf else - let ax2 = E.F.(ax + ax) in - let m = E.F.((ax2 + ax2 + ax2) / (ay + ay)) in + let ax2 = F.(ax * ax) in + let m = F.((ax2 + ax2 + ax2) / (ay + ay)) in line m ax ay bx by else - let m = E.F.((ay - by) / (ax - bx)) in + let m = F.((ay - by) / (ax - bx)) in line m ax ay bx by let sub a b = add a (neg b) let scalar_mul i a = - let _ = (i, a) in - failwith "todo" + let rec m acc i a = + let acc' = if Z.testbit i 0 then add acc a else acc in + let i' = Z.shift_right i 1 in + if Z.(equal i' zero) then acc' + else + let a' = add a a in + m acc' i' a' + in + m Inf i a + + let of_coords (x, y) = + let p = Point (x, y) in + let is_on_curve = + let y2 = F.(y * y) in + let x3 = F.(x * x * x) in + F.(equal y2 (x3 + E.b)) + in + if is_on_curve then () else raise 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 + + let of_string s = + let strip_braces s = + if String.starts_with ~prefix:"{" s && String.ends_with ~suffix:"}" s then + String.sub s 1 (String.length s - 2) + else failwith "curve point missing braces" + in + match strip_braces s |> String.split_on_char ';' with + | [ "∞" ] -> Inf + | [ x; y ] -> of_coords (F.of_string x, F.of_string y) + | _ -> invalid_arg "wrong number of elements, expected {x;y}" let ( ~- ) = neg let ( + ) = add @@ -61,13 +98,13 @@ end) : Curve.S with type t = E.F.t point = struct let ( $* ) = scalar_mul end -module E1 = Eimpl (struct - module F = Fp +module E = Eimpl (struct + module F = Fields.Fp - let b = Fp.of_string "4" + let b = F.of_string "4" let g = - Fp. + F. ( of_string "3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507", of_string @@ -76,7 +113,7 @@ module E1 = Eimpl (struct end) let g2 = - Fp2. + Fields.Fp2. ( of_string "(352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160,3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758)", of_string @@ -84,6 +121,7 @@ let g2 = ) let twist_coords (x, y) = + let open Fields in let w = Fp.(zero, one, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero) |> Fp12.of_coeffs @@ -100,16 +138,16 @@ let twist_coords (x, y) = Fp12.(nx * w2, ny * w3) module E2 = Eimpl (struct - module F = Fp2 + module F = Fields.Fp2 - let b = Fp2.of_string "(4,4)" + let b = F.of_string "(4,4)" let g = g2 end) module E12 = Eimpl (struct - module F = Fp12 + module F = Fields.Fp12 - let b = Fp.of_string "4" |> Fp12.of_fp + let b = Fields.Fp.of_string "4" |> F.of_fp let g = twist_coords g2 end) diff --git a/lib/curves.mli b/lib/curves.mli index 667e943..e760d9e 100644 --- a/lib/curves.mli +++ b/lib/curves.mli @@ -1,6 +1,13 @@ (** BLS12-381 curves used for pairings *) -module E1 : Curve.S +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]. *) module E2 : Curve.S diff --git a/lib/fields.ml b/lib/fields.ml index 912407c..f18977b 100644 --- a/lib/fields.ml +++ b/lib/fields.ml @@ -68,15 +68,20 @@ struct let one = Array.init degree (function 0 -> Fp.one | _ -> Fp.zero) let of_string s = + let strip_parens s = + if String.starts_with ~prefix:"(" s && String.ends_with ~suffix:")" s + then String.sub s 1 (String.length s - 2) + else failwith "field element missing parentheses" + in let a = - Scanf.sscanf s "(%[^)])" (String.split_on_char ',') - |> List.map String.trim |> List.map Fp.of_string |> Array.of_list + strip_parens s |> String.split_on_char ',' |> List.map Fp.of_string + |> Array.of_list in if Array.length a = degree then a else invalid_arg "wrong number of elements" let to_string a = - Array.map Fp.to_string a |> Array.to_list |> String.concat ", " + Array.map Fp.to_string a |> Array.to_list |> String.concat "," |> Printf.sprintf "(%s)" let equal = Array.for_all2 Fp.equal diff --git a/plonkers.opam b/plonkers.opam index dcef03d..e030cc0 100644 --- a/plonkers.opam +++ b/plonkers.opam @@ -6,7 +6,7 @@ description: maintainer: ["Nicholas Rodrigues Lordello"] authors: ["Nicholas Rodrigues Lordello"] license: "GPL-3.0-only" -tags: ["topics" "zero knowledge" "zkp" "plonk"] +tags: ["topics" "plonk" "zero knowledge" "zksnark"] homepage: "https://sr.ht/~nlordell/plonkers" doc: "https://git.sr.ht/~nlordell/plonkers" bug-reports: "https://todo.sr.ht/~nlordell/plonkers" @@ -14,6 +14,7 @@ depends: [ "ocaml" "dune" {>= "3.15"} "zarith" + "alcotest" {with-test} "odoc" {with-doc} ] build: [ diff --git a/test/test_e.ml b/test/test_e.ml new file mode 100644 index 0000000..4ed48f3 --- /dev/null +++ b/test/test_e.ml @@ -0,0 +1,53 @@ +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 test_strconv () = + let a = + f + "3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507" + "1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569" + in + Alcotest.(check string) + "equals" + "{3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507;1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569}" + (E.to_string a); + Alcotest.(check e) + "equals" a + (E.of_string + "{3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507;1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569}") + +let test_curve () = + Alcotest.(check bool) + "fails" true + (try + E.of_string "{0;0}" |> ignore; + false + with + | Plonkers.Curves.Not_on_curve -> true + | _ -> false) + +let test_subgroup () = + let fails s = + Alcotest.(check bool) + "fails" true + (try + E.of_string s |> ignore; + false + with + | Plonkers.Curves.Not_in_canonical_subgroup -> true + | _ -> false) + in + fails + "{980669326159545649263804252026951545804569479867856393900195820932308021605342594512359315664807832835593476691977;2495572288805567003614829204459695097025160810248948605097890007546743160366866086918562910297675348438728151671429}"; + fails + "{1069666319329652829987469716448407827813054733044804636498977267777209654377372971020541102311296686223746457925861;2002055975540879804411492431965632823513627576867073743429207373519001811700965967624073843443645677059600261577048}" + +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; + ] diff --git a/test/test_fp12.ml b/test/test_fp12.ml index 2918018..e88b96b 100644 --- a/test/test_fp12.ml +++ b/test/test_fp12.ml @@ -37,34 +37,12 @@ let test_strconv () = in Alcotest.(check string) "equals" - "(2916030131112425910736728833294966280877348127497066861802260324152389116184665963977658957216391196180579077474202, \ - 1519862437235303187188961557435602349317662069421862747209181232217613587604953767705018210876984976062405524645622, \ - 296645580800680675009621881920997395213790816778008448436221746142308017377314413585580661715141084537701545554168, \ - 1981557211956889095227901276564616817712990386953892187870249475222326830471202728108345999615285753394142232132312, \ - 833361220704354884434302513333442088322974161003072803264824357410372777926316698647896734024111451895881451892941, \ - 2711755601893075919832148493574676626069798533950866837790424822014009949251210477322950287178868808802513013310092, \ - 371213053563436252645940045458060653872935108881239949520789125958735351804960527435344045802826289621926519389359, \ - 504151947300534428538773393472324784669777301827817398729819216017440270289061739490404467920801765270857364809526, \ - 3563993255617322600585910657699631528415397776575273554957383151855598758349437838089591153976674032653746780002367, \ - 1396274584009387615887886835913193518070291009462177144922465656390031512961216497255957290669783178741610222179461, \ - 1791645971276582626341761994075837917473870474245353102664092427839586202945803660199735855654457386807016012521755, \ - 171029163675750402723404477137748189341455451129895111449568566066650114642345344282976819697533131681587059212847)" + "(2916030131112425910736728833294966280877348127497066861802260324152389116184665963977658957216391196180579077474202,1519862437235303187188961557435602349317662069421862747209181232217613587604953767705018210876984976062405524645622,296645580800680675009621881920997395213790816778008448436221746142308017377314413585580661715141084537701545554168,1981557211956889095227901276564616817712990386953892187870249475222326830471202728108345999615285753394142232132312,833361220704354884434302513333442088322974161003072803264824357410372777926316698647896734024111451895881451892941,2711755601893075919832148493574676626069798533950866837790424822014009949251210477322950287178868808802513013310092,371213053563436252645940045458060653872935108881239949520789125958735351804960527435344045802826289621926519389359,504151947300534428538773393472324784669777301827817398729819216017440270289061739490404467920801765270857364809526,3563993255617322600585910657699631528415397776575273554957383151855598758349437838089591153976674032653746780002367,1396274584009387615887886835913193518070291009462177144922465656390031512961216497255957290669783178741610222179461,1791645971276582626341761994075837917473870474245353102664092427839586202945803660199735855654457386807016012521755,171029163675750402723404477137748189341455451129895111449568566066650114642345344282976819697533131681587059212847)" (Fp12.to_string a); Alcotest.(check fp12) "equals" a (Fp12.of_string - "(2916030131112425910736728833294966280877348127497066861802260324152389116184665963977658957216391196180579077474202, \ - 1519862437235303187188961557435602349317662069421862747209181232217613587604953767705018210876984976062405524645622, \ - 296645580800680675009621881920997395213790816778008448436221746142308017377314413585580661715141084537701545554168, \ - 1981557211956889095227901276564616817712990386953892187870249475222326830471202728108345999615285753394142232132312, \ - 833361220704354884434302513333442088322974161003072803264824357410372777926316698647896734024111451895881451892941, \ - 2711755601893075919832148493574676626069798533950866837790424822014009949251210477322950287178868808802513013310092, \ - 371213053563436252645940045458060653872935108881239949520789125958735351804960527435344045802826289621926519389359, \ - 504151947300534428538773393472324784669777301827817398729819216017440270289061739490404467920801765270857364809526, \ - 3563993255617322600585910657699631528415397776575273554957383151855598758349437838089591153976674032653746780002367, \ - 1396274584009387615887886835913193518070291009462177144922465656390031512961216497255957290669783178741610222179461, \ - 1791645971276582626341761994075837917473870474245353102664092427839586202945803660199735855654457386807016012521755, \ - 171029163675750402723404477137748189341455451129895111449568566066650114642345344282976819697533131681587059212847)") + "(2916030131112425910736728833294966280877348127497066861802260324152389116184665963977658957216391196180579077474202,1519862437235303187188961557435602349317662069421862747209181232217613587604953767705018210876984976062405524645622,296645580800680675009621881920997395213790816778008448436221746142308017377314413585580661715141084537701545554168,1981557211956889095227901276564616817712990386953892187870249475222326830471202728108345999615285753394142232132312,833361220704354884434302513333442088322974161003072803264824357410372777926316698647896734024111451895881451892941,2711755601893075919832148493574676626069798533950866837790424822014009949251210477322950287178868808802513013310092,371213053563436252645940045458060653872935108881239949520789125958735351804960527435344045802826289621926519389359,504151947300534428538773393472324784669777301827817398729819216017440270289061739490404467920801765270857364809526,3563993255617322600585910657699631528415397776575273554957383151855598758349437838089591153976674032653746780002367,1396274584009387615887886835913193518070291009462177144922465656390031512961216497255957290669783178741610222179461,1791645971276582626341761994075837917473870474245353102664092427839586202945803660199735855654457386807016012521755,171029163675750402723404477137748189341455451129895111449568566066650114642345344282976819697533131681587059212847)") let test_add () = Alcotest.(check fp12) diff --git a/test/test_fp2.ml b/test/test_fp2.ml index e0bb5ba..c108cfc 100644 --- a/test/test_fp2.ml +++ b/test/test_fp2.ml @@ -12,14 +12,12 @@ let test_strconv () = in Alcotest.(check string) "equals" - "(3216159770949743003016282596382810410524821318260601360202344327568616081946682522800622973525694628320886337554707, \ - 3042776432360195460319296407183823967792916594876398306463713661277272294244991791711647871371679651495395139475044)" + "(3216159770949743003016282596382810410524821318260601360202344327568616081946682522800622973525694628320886337554707,3042776432360195460319296407183823967792916594876398306463713661277272294244991791711647871371679651495395139475044)" (Fp2.to_string a); Alcotest.(check fp2) "equals" a (Fp2.of_string - "(3216159770949743003016282596382810410524821318260601360202344327568616081946682522800622973525694628320886337554707, \ - 3042776432360195460319296407183823967792916594876398306463713661277272294244991791711647871371679651495395139475044)") + "(3216159770949743003016282596382810410524821318260601360202344327568616081946682522800622973525694628320886337554707,3042776432360195460319296407183823967792916594876398306463713661277272294244991791711647871371679651495395139475044)") let test_add () = Alcotest.(check fp2) diff --git a/test/test_plonkers.ml b/test/test_plonkers.ml index 31859c4..9dc396a 100644 --- a/test/test_plonkers.ml +++ b/test/test_plonkers.ml @@ -1,5 +1,8 @@ let () = Alcotest.run "Plonkers" [ - ("Fp", Test_fp.tests); ("Fp2", Test_fp2.tests); ("Fp12", Test_fp12.tests); + ("Fp", Test_fp.tests); + ("Fp2", Test_fp2.tests); + ("Fp12", Test_fp12.tests); + ("E", Test_e.tests); ] -- cgit v1.2.3