aboutsummaryrefslogtreecommitdiff
path: root/lib/curve.ml
blob: 214be2b7d1a4634a949ee696bc12d4a772367d84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(** 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. *)

  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.t * F.t -> 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. *)

  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]. 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]. *)

  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