diff options
| -rw-r--r-- | lib/curve.ml | 6 | ||||
| -rw-r--r-- | lib/curves.ml | 19 | ||||
| -rw-r--r-- | lib/field.ml | 6 | ||||
| -rw-r--r-- | lib/fields.ml | 34 | ||||
| -rw-r--r-- | lib/fields.mli | 22 |
5 files changed, 49 insertions, 38 deletions
diff --git a/lib/curve.ml b/lib/curve.ml index 896cce1..dd76690 100644 --- a/lib/curve.ml +++ b/lib/curve.ml @@ -14,6 +14,12 @@ module type S = sig val n : Z.t (** The order of the curve. *) + val of_string : string -> t + (** [of_string s] returns the curve element represented by [s]. *) + + val to_string : t -> string + (** [to_string a] returns the string representation of [a]. *) + val equal : t -> t -> bool (** [equal a b] returns [true] if [a] is equal to [b]. *) diff --git a/lib/curves.ml b/lib/curves.ml index 082a752..25d1afe 100644 --- a/lib/curves.ml +++ b/lib/curves.ml @@ -31,8 +31,23 @@ end) : Curve.S with type t = E.F.t point = struct let neg = function Inf -> Inf | Point (x, y) -> Point (x, E.F.neg y) let add a b = - let _ = (a, b) in - failwith "todo" + let line m ax ay bx _ = + let rx = E.F.((m * m) - ax - bx) in + let ry = E.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 + else + let ax2 = E.F.(ax + ax) in + let m = E.F.((ax2 + ax2 + ax2) / (ay + ay)) in + line m ax ay bx by + else + let m = E.F.((ay - by) / (ax - bx)) in + line m ax ay bx by let sub a b = add a (neg b) diff --git a/lib/field.ml b/lib/field.ml index df59673..8f72c37 100644 --- a/lib/field.ml +++ b/lib/field.ml @@ -11,6 +11,12 @@ module type S = sig val one : t (** The one value, i.e. the multiplicative identity. *) + val of_string : string -> t + (** [of_string s] returns the field element represented by [s]. *) + + val to_string : t -> string + (** [to_string a] returns the string representation of [a]. *) + val equal : t -> t -> bool (** [equal a b] returns [true] if [a] is equal to [b]. *) diff --git a/lib/fields.ml b/lib/fields.ml index 2523bba..912407c 100644 --- a/lib/fields.ml +++ b/lib/fields.ml @@ -3,6 +3,8 @@ module Fimpl (F : sig val zero : t val one : t + val of_string : string -> t + val to_string : t -> string val equal : t -> t -> bool val neg : t -> t val inv : t -> t @@ -13,6 +15,8 @@ end) : Field.S with type t = F.t = struct let zero = F.zero let one = F.one + let of_string = F.of_string + let to_string = F.to_string let equal = F.equal let neg = F.neg let inv = F.inv @@ -41,15 +45,14 @@ module Fp = struct let zero = Z.zero let one = Z.one + let of_string s = Z.of_string s |> mod_p + let to_string = Z.to_string let equal = Z.equal let neg a = Z.neg a |> mod_p let inv a = Z.powm a p_minus_2 p let add a b = Z.add a b |> mod_p let mul a b = Z.mul a b |> mod_p end) - - let of_string s = Z.of_string s |> mod_p - let to_string = Z.to_string end module FpN (R : sig @@ -63,6 +66,19 @@ struct let zero = Array.make degree Fp.zero let one = Array.init degree (function 0 -> Fp.one | _ -> Fp.zero) + + let of_string s = + let a = + Scanf.sscanf s "(%[^)])" (String.split_on_char ',') + |> List.map String.trim |> List.map Fp.of_string |> Array.of_list + in + if Array.length a = degree then a + else invalid_arg "wrong number of elements" + + let to_string a = + Array.map Fp.to_string a |> Array.to_list |> String.concat ", " + |> Printf.sprintf "(%s)" + let equal = Array.for_all2 Fp.equal let neg a = Array.map Fp.neg a let add a b = Array.map2 Fp.add a b @@ -135,18 +151,6 @@ struct end) let of_fp a = Array.init degree (function 0 -> a | _ -> Fp.zero) - - let of_string s = - let a = - Scanf.sscanf s "(%[^)])" (String.split_on_char ',') - |> List.map String.trim |> List.map Fp.of_string |> Array.of_list - in - if Array.length a = degree then a - else invalid_arg "wrong number of elements" - - let to_string a = - Array.map Fp.to_string a |> Array.to_list |> String.concat ", " - |> Printf.sprintf "(%s)" end module Fp2 = struct diff --git a/lib/fields.mli b/lib/fields.mli index 546618a..18bc142 100644 --- a/lib/fields.mli +++ b/lib/fields.mli @@ -1,14 +1,6 @@ (** Finite fields used by the BLS12-381 curve and pairings *) -module Fp : sig - include Field.S - - val of_string : string -> t - (** [of_string s] returns the field element represented by [s]. *) - - val to_string : t -> string - (** [to_string a] returns the string representation of [a]. *) -end +module Fp : Field.S (* The prime field over which the BLS12-381 curve is defined. *) module Fp2 : sig @@ -20,12 +12,6 @@ module Fp2 : sig val of_fp : Fp.t -> t (** [of_fp a] returns maps a field element to the extension field. *) - val of_string : string -> t - (** [of_string s] returns the field element represented by [s]. *) - - val to_string : t -> string - (** [to_string a] returns the string representation of [a]. *) - val of_coeffs : coeffs -> t (** [of_coeffs c] is the field element with polynomial coefficients [c]. *) @@ -55,12 +41,6 @@ module Fp12 : sig val of_fp : Fp.t -> t (** [of_fp a] returns maps a field element to the extension field. *) - val of_string : string -> t - (** [of_string s] returns the field element represented by [s]. *) - - val to_string : t -> string - (** [to_string a] returns the string representation of [a]. *) - val of_coeffs : coeffs -> t (** [of_coeffs c] is the field element with polynomial coefficients [c]. *) |
