aboutsummaryrefslogtreecommitdiff
path: root/lib/curves.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/curves.ml')
-rw-r--r--lib/curves.ml82
1 files changed, 60 insertions, 22 deletions
diff --git a/lib/curves.ml b/lib/curves.ml
index 25d1afe..7d6841a 100644
--- a/lib/curves.ml
+++ b/lib/curves.ml
@@ -1,4 +1,5 @@
-open Fields
+exception Not_on_curve
+exception Not_in_canonical_subgroup
let n =
Z.of_string
@@ -12,7 +13,9 @@ module Eimpl (E : sig
val b : F.t
val g : F.t * F.t
end) : Curve.S with type t = E.F.t point = struct
- type t = E.F.t point
+ module F = E.F
+
+ type t = F.t point
let inf = Inf
@@ -21,39 +24,73 @@ end) : Curve.S with type t = E.F.t point = struct
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) -> E.F.equal ax bx && E.F.equal ay by
+ | 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, E.F.neg y)
+ 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 = E.F.((m * m) - ax - bx) in
- let ry = E.F.(~-(ay + (m * (rx - ax)))) in
+ 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 E.F.(equal ax bx) then
- if E.F.(equal ay ~-by) then Inf
+ if F.(equal ax bx) then
+ if F.(equal ay ~-by) then Inf
else
- let ax2 = E.F.(ax + ax) in
- let m = E.F.((ax2 + ax2 + ax2) / (ay + ay)) in
+ let ax2 = F.(ax * ax) in
+ let m = F.((ax2 + ax2 + ax2) / (ay + ay)) in
line m ax ay bx by
else
- let m = E.F.((ay - by) / (ax - bx)) in
+ 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 _ = (i, a) in
- failwith "todo"
+ 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 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
+
+ let of_string s =
+ 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 ] -> of_coords (F.of_string x, F.of_string y)
+ | _ -> invalid_arg "wrong number of elements, expected {x;y}"
let ( ~- ) = neg
let ( + ) = add
@@ -61,13 +98,13 @@ end) : Curve.S with type t = E.F.t point = struct
let ( $* ) = scalar_mul
end
-module E1 = Eimpl (struct
- module F = Fp
+module E = Eimpl (struct
+ module F = Fields.Fp
- let b = Fp.of_string "4"
+ let b = F.of_string "4"
let g =
- Fp.
+ F.
( of_string
"3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507",
of_string
@@ -76,7 +113,7 @@ module E1 = Eimpl (struct
end)
let g2 =
- Fp2.
+ Fields.Fp2.
( of_string
"(352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160,3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758)",
of_string
@@ -84,6 +121,7 @@ let g2 =
)
let twist_coords (x, y) =
+ let open Fields in
let w =
Fp.(zero, one, zero, zero, zero, zero, zero, zero, zero, zero, zero, zero)
|> Fp12.of_coeffs
@@ -100,16 +138,16 @@ let twist_coords (x, y) =
Fp12.(nx * w2, ny * w3)
module E2 = Eimpl (struct
- module F = Fp2
+ module F = Fields.Fp2
- let b = Fp2.of_string "(4,4)"
+ let b = F.of_string "(4,4)"
let g = g2
end)
module E12 = Eimpl (struct
- module F = Fp12
+ module F = Fields.Fp12
- let b = Fp.of_string "4" |> Fp12.of_fp
+ let b = Fields.Fp.of_string "4" |> F.of_fp
let g = twist_coords g2
end)