aboutsummaryrefslogtreecommitdiff
path: root/lib
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 /lib
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.
Diffstat (limited to 'lib')
-rw-r--r--lib/curve.ml30
-rw-r--r--lib/curves.ml23
-rw-r--r--lib/curves.mli7
-rw-r--r--lib/plonkers.ml2
4 files changed, 45 insertions, 17 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