(** Elliptic Curve. *) (** Module signature for an elliptic curve. *) module type S = sig 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. *) val g : t (** The generator point. *) val n : Z.t (** The order of the curve. *) val of_coords : f * f -> t (** [of_coords x y] is the curve element with coordinates [x] and [y]. Raises [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 * 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 [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 * 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. *) 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]. *) val neg : t -> t (** [neg a] is the additive inverse of [a]. [add a (neg a) = zero] for all [a]. *) val add : t -> t -> t (** [add a b] is the sum of [a] plus [b] *) val sub : t -> t -> t (** [sub a b] is the difference of [a] minus [b]. It is equivalent to [add a (neg b)]. *) val scalar_mul : Z.t -> t -> t (** [scalar_mul k a] is the scalar multiplication of [a] by [k]. *) val ( ~- ) : t -> t (** [neg]. *) val ( + ) : t -> t -> t (** [add]. *) val ( - ) : t -> t -> t (** [sub]. *) 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