aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/curve.ml228
-rw-r--r--lib/curves.ml3
-rw-r--r--lib/pairing.ml2
-rw-r--r--test/test_e.ml10
-rw-r--r--test/test_e12.ml35
-rw-r--r--test/test_e2.ml15
6 files changed, 228 insertions, 65 deletions
diff --git a/lib/curve.ml b/lib/curve.ml
index 9b021ed..2146e79 100644
--- a/lib/curve.ml
+++ b/lib/curve.ml
@@ -18,13 +18,22 @@ module type S = sig
(** 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.
- *)
+ (** [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]. *)
+ (** [to_coords p] returns the coordinates of [p]. *)
+
+ val of_projective_coords : f * f * f -> t
+ (** [of_projective_coords (x, y, z)] is the curve element with projective
+ coordinates [x], [y], and [z] ([(x/z, y/z)] in affine space). Raises
+ [Invalid_argument] if the point is not torsion-free. *)
+
+ val to_projective_coords : t -> (f * f * f) option
+ (** [to_projective_coords p] returns the projective coordinates of [p].
+ Returns [None] if the point is at infinity. *)
val of_string : string -> t
(** [of_string s] returns the curve element represented by [s]. Raises
@@ -33,14 +42,20 @@ module type S = sig
*)
val to_string : t -> string
- (** [to_string a] returns the string representation of [a]. *)
+ (** [to_string p] returns the string representation of [p]. *)
(** 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. *)
+ (** [of_coords_unchecked (x, y)] 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_projective_coords_unchecked : f * f * f -> t
+ (** [of_projective_coords_unchecked (x, y, z)] is the curve element with
+ projective coordinates [x], [y], and [z] ([(x/z, y/z)] in affine space).
+ 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].
@@ -49,21 +64,25 @@ module type S = sig
end
val equal : t -> t -> bool
- (** [equal a b] returns [true] if [a] is equal to [b]. *)
+ (** [equal p q] returns [true] if [p] is equal to [q]. *)
val neg : t -> t
- (** [neg a] is the additive inverse of [a]. [add a (neg a) = zero] for all
- [a]. *)
+ (** [neg a] is the additive inverse of [p]. [add a (neg a) = zero] for all
+ [p]. *)
+
+ val double : t -> t
+ (** [double a] is the point [p] added to itself. It is equivalent to [add a a]
+ and included as an optimization. *)
val add : t -> t -> t
- (** [add a b] is the sum of [a] plus [b] *)
+ (** [add p q] is the sum of [p] plus [q] *)
val sub : t -> t -> t
- (** [sub a b] is the difference of [a] minus [b]. It is equivalent to
+ (** [sub p q] is the difference of [p] minus [q]. 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]. *)
+ (** [scalar_mul k a] is the scalar multiplication of [p] by [k]. *)
val ( ~- ) : t -> t
(** [neg]. *)
@@ -74,6 +93,9 @@ module type S = sig
val ( - ) : t -> t -> t
(** [sub]. *)
+ val ( ~++ ) : t -> t
+ (** [double]. *)
+
val ( $* ) : Z.t -> t -> t
(** [scalar_mul]. *)
end
@@ -81,79 +103,155 @@ end
module Make (CURVE : sig
module F : Field.S
+ val a : F.t
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)
+ let curve_b3 = F.(CURVE.b + CURVE.b + CURVE.b)
+
+ type t = f * f * f
and f = F.t
- let inf = Inf
+ let inf = F.(zero, one, zero)
let g =
let gx, gy = CURVE.g in
- Point (gx, gy)
+ (gx, gy, F.one)
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 to_coords (x, y, z) =
+ if F.(equal z zero) then None
+ else
+ let zinv = F.inv z in
+ Some F.(x * zinv, y * zinv)
+
+ let to_projective_coords (x, y, z) =
+ if F.(equal z zero) then None else Some (x, y, z)
+
+ let to_string p =
+ match to_coords p with
+ | None -> "{∞}"
+ | Some (x, y) -> Printf.sprintf "{%s;%s}" (F.to_string x) (F.to_string y)
+
+ let equal (px, py, pz) (qx, qy, qz) =
+ let pxz = F.mul px qz in
+ let pyz = F.mul py qz in
+ let qxz = F.mul qx pz in
+ let qyz = F.mul qy pz in
+ F.equal pxz qxz && F.equal pyz qyz
+
+ let neg (x, y, z) = (x, F.neg y, z)
+
+ let double (px, py, pz) =
+ let t0 = F.mul px px in
+ let t1 = F.mul py py in
+ let t2 = F.mul pz pz in
+ let t3 = F.mul px py in
+ let t3 = F.add t3 t3 in
+ let rz = F.mul px pz in
+ let rz = F.add rz rz in
+ let rx = F.mul CURVE.a rz in
+ let ry = F.mul curve_b3 t2 in
+ let ry = F.add rx ry in
+ let rx = F.sub t1 ry in
+ let ry = F.add t1 ry in
+ let ry = F.mul rx ry in
+ let rx = F.mul t3 rx in
+ let rz = F.mul curve_b3 rz in
+ let t2 = F.mul CURVE.a t2 in
+ let t3 = F.sub t0 t2 in
+ let t3 = F.mul CURVE.a t3 in
+ let t3 = F.add t3 rz in
+ let rz = F.add t0 t0 in
+ let t0 = F.add rz t0 in
+ let t0 = F.add t0 t2 in
+ let t0 = F.mul t0 t3 in
+ let ry = F.add ry t0 in
+ let t2 = F.mul py pz in
+ let t2 = F.add t2 t2 in
+ let t0 = F.mul t2 t3 in
+ let rx = F.sub rx t0 in
+ let rz = F.mul t2 t1 in
+ let rz = F.add rz rz in
+ let rz = F.add rz rz in
+ (rx, ry, rz)
+
+ let add (px, py, pz) (qx, qy, qz) =
+ let t0 = F.mul px qx in
+ let t1 = F.mul py qy in
+ let t2 = F.mul pz qz in
+ let t3 = F.add px py in
+ let t4 = F.add qx qy in
+ let t3 = F.mul t3 t4 in
+ let t4 = F.add t0 t1 in
+ let t3 = F.sub t3 t4 in
+ let t4 = F.add px pz in
+ let t5 = F.add qx qz in
+ let t4 = F.mul t4 t5 in
+ let t5 = F.add t0 t2 in
+ let t4 = F.sub t4 t5 in
+ let t5 = F.add py pz in
+ let rx = F.add qy qz in
+ let t5 = F.mul t5 rx in
+ let rx = F.add t1 t2 in
+ let t5 = F.sub t5 rx in
+ let rz = F.mul CURVE.a t4 in
+ let rx = F.mul curve_b3 t2 in
+ let rz = F.add rx rz in
+ let rx = F.sub t1 rz in
+ let rz = F.add t1 rz in
+ let ry = F.mul rx rz in
+ let t1 = F.add t0 t0 in
+ let t1 = F.add t1 t0 in
+ let t2 = F.mul CURVE.a t2 in
+ let t4 = F.mul curve_b3 t4 in
+ let t1 = F.add t1 t2 in
+ let t2 = F.sub t0 t2 in
+ let t2 = F.mul CURVE.a t2 in
+ let t4 = F.add t4 t2 in
+ let t0 = F.mul t1 t4 in
+ let ry = F.add ry t0 in
+ let t0 = F.mul t5 t4 in
+ let rx = F.mul t3 rx in
+ let rx = F.sub rx t0 in
+ let t0 = F.mul t3 t1 in
+ let rz = F.mul t5 rz in
+ let rz = F.add rz t0 in
+ (rx, ry, rz)
+
+ let sub p q = add p (neg q)
+
+ let scalar_mul i p =
+ let rec m acc i p =
+ let acc' = if Z.testbit i 0 then add acc p 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
+ let a' = double p in
m acc' i' a'
in
- m Inf i a
+ m inf i p
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"
+ if not is_on_curve then invalid_arg "not on curve";
+ let p = (x, y, F.one) in
+ let is_in_canonical_subgroup = scalar_mul n p |> equal inf in
+ if not is_in_canonical_subgroup then invalid_arg "not in canonical subgroup";
+ p
+
+ let of_projective_coords p =
+ match to_coords p with
+ | Some p' -> of_coords p'
+ | None -> invalid_arg "invalid point projection"
let of_string_f s f =
let strip_braces s =
@@ -162,19 +260,21 @@ end) : S with type f = CURVE.F.t = struct
else failwith "curve point missing braces"
in
match strip_braces s |> String.split_on_char ';' with
- | [ "∞" ] -> Inf
+ | [ "∞" ] -> 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_coords_unchecked (x, y) = (x, y, F.one)
+ let of_projective_coords_unchecked p = p
let of_string_unchecked s = of_string_f s of_coords_unchecked
end
let ( ~- ) = neg
let ( + ) = add
let ( - ) = sub
+ let ( ~++ ) = double
let ( $* ) = scalar_mul
end
diff --git a/lib/curves.ml b/lib/curves.ml
index 1182352..440a66d 100644
--- a/lib/curves.ml
+++ b/lib/curves.ml
@@ -1,6 +1,7 @@
module E = Curve.Make (struct
module F = Fields.Fp
+ let a = F.zero
let b = F.of_string "4"
let g =
@@ -19,6 +20,7 @@ end)
module E2 = Curve.Make (struct
module F = Fields.Fp2
+ let a = F.zero
let b = F.of_string "(4,4)"
let g =
@@ -46,6 +48,7 @@ end)
module E12 = Curve.Make (struct
module F = Fields.Fp12
+ let a = F.zero
let b = F.of_f @@ Fields.Fp.of_string "4"
let g =
diff --git a/lib/pairing.ml b/lib/pairing.ml
index ef67901..daed96c 100644
--- a/lib/pairing.ml
+++ b/lib/pairing.ml
@@ -73,7 +73,7 @@ struct
(fun f (p, q, r) ->
let t = line !r !r p in
let f = F.(f * t) in
- r := G2.add !r !r;
+ r := G2.double !r;
if bit then (
let t = line !r q p in
let f = F.(f * t) in
diff --git a/test/test_e.ml b/test/test_e.ml
index e2b5fc8..880b545 100644
--- a/test/test_e.ml
+++ b/test/test_e.ml
@@ -55,6 +55,15 @@ let test_add () =
"3751513699951780822806204149545068929347461752396128233032372349172282127292099402193824058844455410431018547944025"
)))
+let test_double () =
+ let a =
+ f
+ ( "1948821978587447897789631658127568747564453341960450352257323012875526005675549792824154216114310204101716468014510",
+ "3805369745912496441131578309581807518996252052482176821838803458068084239405605134308394924115132794385888965780503"
+ )
+ in
+ Alcotest.(check e) "equals" (E.add a a) (E.double a)
+
let test_sub () =
Alcotest.(check e)
"equals"
@@ -114,6 +123,7 @@ let tests =
test_case "Is On Curve" `Quick test_curve;
test_case "Is In Canonical Subgroup" `Quick test_subgroup;
test_case "Addition" `Quick test_add;
+ test_case "Double" `Quick test_double;
test_case "Subtraction" `Quick test_sub;
test_case "Scalar Multiplication" `Quick test_scalar_mul;
test_case "Additive Inverse" `Quick test_neg;
diff --git a/test/test_e12.ml b/test/test_e12.ml
index 6be08a8..8c76bc6 100644
--- a/test/test_e12.ml
+++ b/test/test_e12.ml
@@ -156,6 +156,40 @@ let test_add () =
"0";
|] )))
+let test_double () =
+ let a =
+ f
+ ( [|
+ "0";
+ "0";
+ "0";
+ "0";
+ "864680025495851032298585449242495442172586480369653270569427520081920647109443766686964546009031896772231151484739";
+ "0";
+ "0";
+ "0";
+ "0";
+ "0";
+ "1631392878509829468330827308350745651550160006042966046936061802639012597909369897842804975745918562123972568708986";
+ "0";
+ |],
+ [|
+ "0";
+ "0";
+ "0";
+ "3175875744091331376881702916348722059990202461121243501806124053462822942076308690447194451748015149897939410507835";
+ "0";
+ "0";
+ "0";
+ "0";
+ "0";
+ "1261674949629831053147743653050474475817726050930614703730644111735569882980952175496663479511045926628196951977486";
+ "0";
+ "0";
+ |] )
+ in
+ Alcotest.(check e12) "equals" (E12.add a a) (E12.double a)
+
let test_sub () =
Alcotest.(check e12)
"equals"
@@ -448,6 +482,7 @@ let tests =
test_case "Is On Curve" `Quick test_curve;
test_case "Is In Canonical Subgroup" `Quick test_subgroup;
test_case "Addition" `Quick test_add;
+ test_case "Double" `Quick test_double;
test_case "Subtraction" `Quick test_sub;
test_case "Scalar Multiplication" `Quick test_scalar_mul;
test_case "Additive Inverse" `Quick test_neg;
diff --git a/test/test_e2.ml b/test/test_e2.ml
index d4f951b..fe02647 100644
--- a/test/test_e2.ml
+++ b/test/test_e2.ml
@@ -75,6 +75,20 @@ let test_add () =
"2193129241285505962248559104914181807138658074292296828774608320812063550960488358009160505763316933125829480800311";
|] )))
+let test_double () =
+ let a =
+ f
+ ( [|
+ "2630340632582992300068504223042664782235656344249493885516510645984188976796605076720730074664979072939686934257721";
+ "3837313288726631479216536661083348837649337798727188764618972962388445003540771017271319539046080622692222479670778";
+ |],
+ [|
+ "1683267476147804034933489382796354199818919863598454325548320805934667239588199593331393365665593904784252069691594";
+ "1024386153970382602226736749102780316785763625305478417896374484204124772205537751029991953527668491051585823287162";
+ |] )
+ in
+ Alcotest.(check e2) "equals" (E2.add a a) (E2.double a)
+
let test_sub () =
Alcotest.(check e2)
"equals"
@@ -174,6 +188,7 @@ let tests =
test_case "Is On Curve" `Quick test_curve;
test_case "Is In Canonical Subgroup" `Quick test_subgroup;
test_case "Addition" `Quick test_add;
+ test_case "Double" `Quick test_double;
test_case "Subtraction" `Quick test_sub;
test_case "Scalar Multiplication" `Quick test_scalar_mul;
test_case "Additive Inverse" `Quick test_neg;