diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2025-08-01 13:42:15 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2025-08-01 16:03:13 +0200 |
| commit | e77561ac7f94668039adf9db57f0c6f9248eeb16 (patch) | |
| tree | 5cb6b9acc88f034d30a86aff777f5e7621e880f4 /lib/curve.ml | |
| parent | 89f936fff830cd245d47fb5fb6e109c56216c7af (diff) | |
Use Projective Coordinatesfeat/projective-coordinates
I modified the curve implementation to use projective coordinates, but
there wasn't a material speedup of the BLS12-381 pairing implementation.
I suspect it is related to the fact that I am not implementing the
Miller Loop in a way that takes advantage of the coordinate projection,
which makes things take more time in the end.
Diffstat (limited to 'lib/curve.ml')
| -rw-r--r-- | lib/curve.ml | 228 |
1 files changed, 164 insertions, 64 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 |
