diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2024-09-14 18:23:28 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2024-09-29 09:39:02 +0200 |
| commit | 2efcbfd67799537be7ebdb2d18495076b9ddcacc (patch) | |
| tree | 5c85f668ed08b9dac9d8c7c2eff4af6ea8e8d1c7 /lib | |
| parent | 97a35bd8aefecc502de05a7d040b8d7bbda15216 (diff) | |
Implemented Elliptic Curve Operations
This includes operations for all 3 curves relevant to PLONK.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/curve.ml | 9 | ||||
| -rw-r--r-- | lib/curves.ml | 82 | ||||
| -rw-r--r-- | lib/curves.mli | 9 | ||||
| -rw-r--r-- | lib/fields.ml | 11 |
4 files changed, 85 insertions, 26 deletions
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 |
