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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
(** 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
|