aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2025-07-30 09:01:58 +0200
committerNicholas Rodrigues Lordello <[email protected]>2025-07-30 09:01:58 +0200
commit395fa4a06d5c4ac90f65838137728ed569bc4f90 (patch)
treed30e11af226a2d6db499099fe238b1fe58f63334
parentc2f1680b6718a01c128fbb83ba3c50b70c3c5d7c (diff)
Refactor Curve Into Functor
Curves can now be generically instantiated with a functor, depending on its parameterization (underlying field, Weirstrass `b` value, generator point and order). This makes things very nice! We also now define `E12` explicitely, with a test that `twist E2.g = E12.g`.
-rw-r--r--lib/curve.ml132
-rw-r--r--lib/curves.ml282
-rw-r--r--lib/curves.mli14
-rw-r--r--lib/field.ml23
-rw-r--r--lib/fields.mli4
-rw-r--r--lib/pairing.mli4
-rw-r--r--test/test_e.ml4
-rw-r--r--test/test_e12.ml4
-rw-r--r--test/test_e2.ml4
9 files changed, 265 insertions, 206 deletions
diff --git a/lib/curve.ml b/lib/curve.ml
index 214be2b..9b021ed 100644
--- a/lib/curve.ml
+++ b/lib/curve.ml
@@ -1,20 +1,13 @@
(** 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
- (** The field over which the curve is defined. *)
-
type t
(** A type representing an element in an elliptic curve. *)
+ type f
+ (** An element of field on which the curve is defined. *)
+
val inf : t
(** The point at infinity or additive identity. *)
@@ -24,27 +17,27 @@ module type S = sig
val n : Z.t
(** The order of the curve. *)
- val of_coords : F.t * F.t -> t
+ val of_coords : f * f -> t
(** [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. *)
+ [Invalid_argument] if the coordinates do not correspond to a point that
+ satisfies the elliptic curve equation or if the point is not torsio-free.
+ *)
- val to_coords : t -> (F.t * F.t) option
+ val to_coords : t -> (f * f) option
(** [to_coords a] returns the coordinates of [a]. *)
val of_string : string -> t
(** [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. *)
+ [Invalid_argument] if the coordinates do not correspond to a point that
+ satisfies the elliptic curve equation or 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
+ val of_coords_unchecked : f * f -> 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. *)
@@ -84,3 +77,104 @@ module type S = sig
val ( $* ) : Z.t -> t -> t
(** [scalar_mul]. *)
end
+
+module Make (CURVE : sig
+ module F : Field.S
+
+ val b : F.t
+ val g : F.t * F.t
+ val n : Z.t
+end) : S with type f = CURVE.F.t = struct
+ module F = CURVE.F
+
+ type t = Inf | Point of (f * f)
+ and f = F.t
+
+ let inf = Inf
+
+ let g =
+ let gx, gy = CURVE.g in
+ Point (gx, gy)
+
+ let n = CURVE.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) -> F.equal ax bx && F.equal ay by
+ | _ -> false
+
+ 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 = 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 F.(equal ax bx) then
+ if F.(equal ay ~-by) then Inf
+ else
+ let ax2 = F.(ax * ax) in
+ let m = F.((ax2 + ax2 + ax2) / (ay + ay)) in
+ line m ax ay bx by
+ else
+ 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 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 + CURVE.b))
+ in
+ if is_on_curve then () else invalid_arg "not on curve";
+ let is_in_canonical_subgroup = scalar_mul n p |> equal Inf in
+ if is_in_canonical_subgroup then p
+ else invalid_arg "not in canonical subgroup"
+
+ 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)
+ else failwith "curve point missing braces"
+ in
+ match strip_braces s |> String.split_on_char ';' with
+ | [ "∞" ] -> Inf
+ | [ 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
+ let ( $* ) = scalar_mul
+end
diff --git a/lib/curves.ml b/lib/curves.ml
index cb3d9f1..1182352 100644
--- a/lib/curves.ml
+++ b/lib/curves.ml
@@ -1,109 +1,4 @@
-let n =
- Z.of_string
- "52435875175126190479447740508185965837690552500527637822603658699938581184513"
-
-type 'a point = Inf | Point of 'a * 'a
-
-module Eimpl (E : sig
- module F : Field.S
-
- val b : F.t
- val g : F.t * F.t
-end) : Curve.S with type t = E.F.t point and type F.t = E.F.t = struct
- module F = E.F
-
- type t = F.t point
-
- let inf = Inf
-
- let g =
- let gx, gy = E.g in
- 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) -> F.equal ax bx && F.equal ay by
- | _ -> false
-
- 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 = 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 F.(equal ax bx) then
- if F.(equal ay ~-by) then Inf
- else
- let ax2 = F.(ax * ax) in
- let m = F.((ax2 + ax2 + ax2) / (ay + ay)) in
- line m ax ay bx by
- else
- 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 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 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 Curve.Not_in_canonical_subgroup
-
- 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)
- else failwith "curve point missing braces"
- in
- match strip_braces s |> String.split_on_char ';' with
- | [ "∞" ] -> Inf
- | [ 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
- let ( $* ) = scalar_mul
-end
-
-module E = Eimpl (struct
+module E = Curve.Make (struct
module F = Fields.Fp
let b = F.of_string "4"
@@ -115,68 +10,137 @@ module E = Eimpl (struct
of_string
"1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569"
)
-end)
-
-let g2 =
- Fields.Fp2.
- ( of_coeffs
- Fields.Fp.
- [|
- of_string
- "352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160";
- of_string
- "3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758";
- |],
- of_coeffs
- Fields.Fp.
- [|
- of_string
- "1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905";
- of_string
- "927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582";
- |] )
-let twist_coords =
- let w =
- Fields.Fp12.of_coeffs
- Fields.Fp.
- [|
- zero; one; zero; zero; zero; zero; zero; zero; zero; zero; zero; zero;
- |]
- in
- let w2 = Fields.Fp12.(w * w) in
- let w3 = Fields.Fp12.(w2 * w) in
- let w2inv = Fields.Fp12.inv w2 in
- let w3inv = Fields.Fp12.inv w3 in
- let iso a =
- let c = Fields.Fp2.to_coeffs a in
- let c0' = Fields.Fp.(c.(0) - c.(1)) in
- Fields.Fp12.of_coeffs
- Fields.Fp.
- [|
- c0'; zero; zero; zero; zero; zero; c.(1); zero; zero; zero; zero; zero;
- |]
- in
- fun (x, y) ->
- let nx, ny = (iso x, iso y) in
- Fields.Fp12.(nx * w2inv, ny * w3inv)
+ let n =
+ Z.of_string
+ "52435875175126190479447740508185965837690552500527637822603658699938581184513"
+end)
-module E2 = Eimpl (struct
+module E2 = Curve.Make (struct
module F = Fields.Fp2
let b = F.of_string "(4,4)"
- let g = g2
+
+ let g =
+ F.
+ ( of_coeffs
+ Fields.Fp.
+ [|
+ of_string
+ "352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160";
+ of_string
+ "3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758";
+ |],
+ of_coeffs
+ Fields.Fp.
+ [|
+ of_string
+ "1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905";
+ of_string
+ "927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582";
+ |] )
+
+ let n = E.n
end)
-module E12 = Eimpl (struct
+module E12 = Curve.Make (struct
module F = Fields.Fp12
let b = F.of_f @@ Fields.Fp.of_string "4"
- let g = twist_coords g2
+
+ let g =
+ F.
+ ( of_coeffs
+ Fields.Fp.
+ [|
+ zero;
+ zero;
+ zero;
+ zero;
+ of_string
+ "352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160";
+ zero;
+ zero;
+ zero;
+ zero;
+ zero;
+ of_string
+ "1353221637328373545892060349371360746048220186341936159219732281025920769516621702780080981380240920942561918531299";
+ zero;
+ |],
+ of_coeffs
+ Fields.Fp.
+ [|
+ zero;
+ zero;
+ zero;
+ of_string
+ "1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905";
+ zero;
+ zero;
+ zero;
+ zero;
+ zero;
+ of_string
+ "1472406309213353956798468635167385199506877017588472562248853724925831900559844855499883907296819622485916638215732";
+ zero;
+ zero;
+ |] )
+
+ let n = E.n
end)
-let twist = function
- | Inf -> Inf
- | Point (x, y) ->
- let x', y' = twist_coords (x, y) in
- Point (x', y')
+let twist =
+ let ws =
+ lazy
+ (let w =
+ Fields.Fp12.of_coeffs
+ Fields.Fp.
+ [|
+ zero;
+ one;
+ zero;
+ zero;
+ zero;
+ zero;
+ zero;
+ zero;
+ zero;
+ zero;
+ zero;
+ zero;
+ |]
+ in
+ let w2 = Fields.Fp12.(w * w) in
+ let w3 = Fields.Fp12.(w2 * w) in
+ let w2inv = Fields.Fp12.inv w2 in
+ let w3inv = Fields.Fp12.inv w3 in
+ (w2inv, w3inv))
+ in
+ let iso a =
+ let c = Fields.Fp2.to_coeffs a in
+ Fields.Fp12.of_coeffs
+ Fields.Fp.
+ [|
+ Fields.Fp.(c.(0) - c.(1));
+ zero;
+ zero;
+ zero;
+ zero;
+ zero;
+ c.(1);
+ zero;
+ zero;
+ zero;
+ zero;
+ zero;
+ |]
+ in
+ fun p ->
+ match E2.to_coords p with
+ | None -> E12.inf
+ | Some (x, y) ->
+ let nx, ny = (iso x, iso y) in
+ let w2inv, w3inv = Lazy.force ws in
+ let c = Fields.Fp12.(nx * w2inv, ny * w3inv) in
+ E12.Unsafe.of_coords_unchecked c
diff --git a/lib/curves.mli b/lib/curves.mli
index 72b5c22..35f2bd4 100644
--- a/lib/curves.mli
+++ b/lib/curves.mli
@@ -1,13 +1,13 @@
(** BLS12-381 curves used for pairings *)
-module E : Curve.S with type F.t = Fields.Fp.t
-(* The BLS12-381 curve over [Fields.Fp]. *)
+module E : Curve.S with type f = Fields.Fp.t
+(** The BLS12-381 curve over [Fields.Fp]. *)
-module E2 : Curve.S with type F.t = Fields.Fp2.t
-(* The BLS12-381 curve over [Fields.Fp2]. *)
+module E2 : Curve.S with type f = Fields.Fp2.t
+(** The BLS12-381 curve over [Fields.Fp2]. *)
-module E12 : Curve.S with type F.t = Fields.Fp12.t
-(* The BLS12-381 curve over [Fields.Fp12]. *)
+module E12 : Curve.S with type f = Fields.Fp12.t
+(** The BLS12-381 curve over [Fields.Fp12]. *)
val twist : E2.t -> E12.t
-(* [twist a] is the twisted point of [E2] in [E12] *)
+(** [twist a] is the twisted point of [E2] in [E12] *)
diff --git a/lib/field.ml b/lib/field.ml
index 3c4b2b6..5b11983 100644
--- a/lib/field.ml
+++ b/lib/field.ml
@@ -69,7 +69,7 @@ module type S = sig
end
(** Module signature for a polynomial extension field. *)
-module type Sx = sig
+module type SX = sig
include S
type f
@@ -85,12 +85,12 @@ module type Sx = sig
(** [to_coeffs a] are the polynomial coefficients of [a]. *)
end
-module Make (P : sig
+module Make (PRIME : sig
val p : Z.t
end) : S = struct
type t = Z.t
- let p = P.p
+ let p = PRIME.p
let mod_p x = Z.erem x p
let zero = Z.zero
let one = Z.one
@@ -112,16 +112,17 @@ end) : S = struct
let ( / ) = div
end
-module MakeExtension (P : sig
+module MakeExtension (POLY : sig
module F : S
val q : F.t array
-end) : Sx with type f = P.F.t = struct
- module F = P.F
+end) : SX with type f = POLY.F.t = struct
+ module F = POLY.F
type f = F.t
- let degree = Array.length P.q
+ let q = POLY.q
+ let degree = Array.length q
let () = assert (degree >= 2)
let of_f a = Array.init degree (function 0 -> a | _ -> F.zero)
@@ -166,11 +167,11 @@ end) : Sx with type f = P.F.t = struct
done
done;
for i = Array.length r - 1 downto degree do
- let q = r.(i) in
- if not (F.equal q F.zero) then
+ let qi = r.(i) in
+ if not (F.equal qi F.zero) then
for j = 0 to degree - 1 do
let j' = i - degree + j in
- r.(j') <- F.sub r.(j') (F.mul q P.q.(j))
+ r.(j') <- F.sub r.(j') (F.mul qi q.(j))
done
else ()
done;
@@ -210,7 +211,7 @@ end) : Sx with type f = P.F.t = struct
in
let t = ref (Array.append zero [| F.zero |]) in
let newt = ref (Array.append one [| F.zero |]) in
- let r = ref (Array.append P.q [| F.one |]) in
+ let r = ref (Array.append q [| F.one |]) in
let newr = ref (Array.append a [| F.zero |]) in
while Array.for_all F.(equal zero) !newr = false do
let q = ediv !r !newr in
diff --git a/lib/fields.mli b/lib/fields.mli
index 2ccc23b..a3d9adc 100644
--- a/lib/fields.mli
+++ b/lib/fields.mli
@@ -3,8 +3,8 @@
module Fp : Field.S
(** The prime field over which the BLS12-381 curve is defined. *)
-module Fp2 : Field.Sx with type f = Fp.t
+module Fp2 : Field.SX with type f = Fp.t
(** The polynomial field extension of degree 2 for BLS12-381 pairings. *)
-module Fp12 : Field.Sx with type f = Fp.t
+module Fp12 : Field.SX with type f = Fp.t
(** The polynomial field extension of degree 12 for BLS12-381 pairings. *)
diff --git a/lib/pairing.mli b/lib/pairing.mli
index fc66fcb..8d27b02 100644
--- a/lib/pairing.mli
+++ b/lib/pairing.mli
@@ -1,5 +1,5 @@
-module G1 : Curve.S with type t = Curves.E.t and type F.t = Fields.Fp.t
-module G2 : Curve.S with type t = Curves.E2.t and type F.t = Fields.Fp2.t
+module G1 : Curve.S with type t = Curves.E.t and type f = Fields.Fp.t
+module G2 : Curve.S with type t = Curves.E2.t and type f = Fields.Fp2.t
module Gt : sig
type t
diff --git a/test/test_e.ml b/test/test_e.ml
index 585cb20..e2b5fc8 100644
--- a/test/test_e.ml
+++ b/test/test_e.ml
@@ -23,7 +23,7 @@ let test_curve () =
E.of_string "{0;0}" |> ignore;
false
with
- | Plonkers.Curve.Not_on_curve -> true
+ | Invalid_argument _ -> true
| _ -> false)
let test_subgroup () =
@@ -35,7 +35,7 @@ let test_subgroup () =
|> ignore;
false
with
- | Plonkers.Curve.Not_in_canonical_subgroup -> true
+ | Invalid_argument _ -> true
| _ -> false)
let test_add () =
diff --git a/test/test_e12.ml b/test/test_e12.ml
index 2ee3f17..6be08a8 100644
--- a/test/test_e12.ml
+++ b/test/test_e12.ml
@@ -49,7 +49,7 @@ let test_curve () =
|> ignore;
false
with
- | Plonkers.Curve.Not_on_curve -> true
+ | Invalid_argument _ -> true
| _ -> false)
let test_subgroup () =
@@ -61,7 +61,7 @@ let test_subgroup () =
|> ignore;
false
with
- | Plonkers.Curve.Not_in_canonical_subgroup -> true
+ | Invalid_argument _ -> true
| _ -> false)
let test_add () =
diff --git a/test/test_e2.ml b/test/test_e2.ml
index 5e40e65..d4f951b 100644
--- a/test/test_e2.ml
+++ b/test/test_e2.ml
@@ -28,7 +28,7 @@ let test_curve () =
E2.of_string "{(0,0);(0,0)}" |> ignore;
false
with
- | Plonkers.Curve.Not_on_curve -> true
+ | Invalid_argument _ -> true
| _ -> false)
let test_subgroup () =
@@ -40,7 +40,7 @@ let test_subgroup () =
|> ignore;
false
with
- | Plonkers.Curve.Not_in_canonical_subgroup -> true
+ | Invalid_argument _ -> true
| _ -> false)
let test_add () =