diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2024-06-24 08:46:01 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2024-06-24 08:46:01 +0200 |
| commit | 9747206e7a0a08f6dc8c92730f0780e1b66cb23e (patch) | |
| tree | 2b85dfdabcb39ebeea50e7eb551d8cb66335ac8f /lib | |
| parent | 4b5936586e570b125b1c68d04b1479262af9c98b (diff) | |
Implement Polynomial Extension Fields
Notably, only `Fp[X]/1+x^2`, or `Fp2` is tested at the moment. More
tests for `Fp12` coming right up!
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/field.ml | 38 | ||||
| -rw-r--r-- | lib/fields.ml | 216 | ||||
| -rw-r--r-- | lib/fields.mli | 80 |
3 files changed, 271 insertions, 63 deletions
diff --git a/lib/field.ml b/lib/field.ml index 4b6408f..8840f5b 100644 --- a/lib/field.ml +++ b/lib/field.ml @@ -1,5 +1,6 @@ (** Finite field. *) +(** Module signature for a finite field. *) module type S = sig type t (** A type representing an element in the finite field *) @@ -18,8 +19,8 @@ module type S = sig [a]. *) val inv : t -> t - (** [inv a] is the multiplicative inverse of [a]. [mul a (inv a) = 1] for all - non-zero [a]. Can raise [Divide_by_zero]. *) + (** [inv a] is the multiplicative inverse of [a]. [mul a (inv a) = one] for + all non-zero [a]. Furthermore, we define [inv zero = zero]. *) val add : t -> t -> t (** [add a b] is the sum of [a] plus [b] *) @@ -33,7 +34,7 @@ module type S = sig val div : t -> t -> t (** [div a b] is the field division of [a] by [b]. It is equivalent to - [mul a (inv b)]. Can raise [Divide_by_zero]. *) + [mul a (inv b)]. Furthermore, we define [div a zero = zero]. *) val ( ~- ) : t -> t (** [neg]. *) @@ -53,3 +54,34 @@ module type S = sig val ( / ) : t -> t -> t (** [div]. *) end + +(** Construct a finite field from its definition. *) +module Make (F : sig + type t + + val zero : t + val one : t + val equal : t -> t -> bool + val neg : t -> t + val inv : t -> t + val add : t -> t -> t + val mul : t -> t -> t +end) : S with type t = F.t = struct + type t = F.t + + let zero = F.zero + let one = F.one + let equal = F.equal + let neg = F.neg + let inv = F.inv + let add = F.add + let sub a b = add a (neg b) + let mul = F.mul + let div a b = mul a (inv b) + let ( ~- ) = neg + let ( ~/ ) = inv + let ( + ) = add + let ( - ) = sub + let ( * ) = mul + let ( / ) = div +end diff --git a/lib/fields.ml b/lib/fields.ml index 2cdbbda..e9a6514 100644 --- a/lib/fields.ml +++ b/lib/fields.ml @@ -1,42 +1,182 @@ -let p = - Z.of_string - "21888242871839275222246405745257275088696311157297823662689037894645226208583" - -let p_minus_2 = Z.(p - ~$2) -let mod_p x = Z.erem x p - -module Fq = struct - type t = Z.t - - let zero = Z.zero - let one = Z.one - 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 sub a b = add a (neg b) - let mul a b = Z.mul a b |> mod_p - let div a b = mul a (inv b) - let ( ~- ) = neg - let ( ~/ ) = inv - let ( + ) = add - let ( - ) = sub - let ( * ) = mul - let ( / ) = div +module Fp = struct + let p = + Z.of_string + "21888242871839275222246405745257275088696311157297823662689037894645226208583" + + let p_minus_2 = Z.(p - ~$2) + let mod_p x = Z.erem x p + + include Field.Make (struct + type t = Z.t + + let zero = Z.zero + let one = Z.one + 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 ExtensionField = -functor - (Ext : sig - type t - end) - -> - struct - type t = Ext.t - end - -module Fq2 = ExtensionField (struct - type t = Fq.t * Fq.t -end) +module FpN (R : sig + val p : Fp.t array +end) = +struct + let degree = Array.length R.p + + include Field.Make (struct + type t = Fp.t array + + let zero = Array.make degree Fp.zero + let one = Array.init degree (function 0 -> Fp.one | _ -> Fp.zero) + 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 + + let mul a b = + let r = Array.make ((degree * 2) - 1) Fp.zero in + for i = 0 to degree - 1 do + for j = 0 to degree - 1 do + r.(i + j) <- Fp.add r.(i + j) (Fp.mul a.(i) b.(j)) + done + done; + for i = Array.length r - 1 downto degree do + let q = r.(i) in + if not (Fp.equal q Fp.zero) then + for j = 0 to degree - 1 do + let j' = i - degree + j in + r.(j') <- Fp.sub r.(j') (Fp.mul q R.p.(j)) + done + else () + done; + Array.sub r 0 degree + + let inv a = + let deg a = + let rec deg' = function + | 0 -> 0 + | i -> if Fp.equal a.(i) Fp.zero then deg' (i - 1) else i + in + deg' (Array.length a - 1) + in + let ediv a b = + let da = deg a in + let db = deg b in + let q = Array.make (da + 1) Fp.zero in + let r = Array.sub a 0 (da + 1) in + let c = b.(db) in + for i = da - db downto 0 do + let qd = Fp.div r.(db + i) c in + q.(i) <- qd; + for j = 0 to db do + r.(i + j) <- Fp.sub r.(i + j) (Fp.mul qd b.(j)) + done + done; + q + in + let submulq a b q = + let r = Array.copy a in + for i = 0 to deg b do + for j = 0 to deg q do + r.(i + j) <- Fp.sub r.(i + j) (Fp.mul b.(i) q.(j)) + done + done; + r + in + let t = ref (Array.append zero [| Fp.zero |]) in + let newt = ref (Array.append one [| Fp.zero |]) in + let r = ref (Array.append R.p [| Fp.one |]) in + let newr = ref (Array.append a [| Fp.zero |]) in + while Array.for_all Fp.(equal zero) !newr = false do + let q = ediv !r !newr in + let newr' = submulq !r !newr q in + let newt' = submulq !t !newt q in + r := !newr; + newr := newr'; + t := !newt; + newt := newt' + done; + let a0 = Fp.inv !r.(0) in + Array.map (Fp.mul a0) (Array.sub !t 0 degree) + 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 = Array.length R.p 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 + include FpN (struct + let p = [| Fp.one; Fp.zero |] + end) + + type coeffs = Fp.t * Fp.t + + let of_coeffs (c0, c1) = [| c0; c1 |] + let to_coeffs a = (a.(0), a.(1)) +end + +module Fp12 = struct + include FpN (struct + let p = + [| + Fp.of_string "82"; + Fp.zero; + Fp.zero; + Fp.zero; + Fp.zero; + Fp.zero; + Fp.of_string "-18"; + Fp.zero; + Fp.zero; + Fp.zero; + Fp.zero; + Fp.zero; + |] + end) + + type coeffs = + Fp.t + * Fp.t + * Fp.t + * Fp.t + * Fp.t + * Fp.t + * Fp.t + * Fp.t + * Fp.t + * Fp.t + * Fp.t + * Fp.t + + let of_coeffs (c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11) = + [| c0; c1; c2; c3; c4; c5; c6; c7; c8; c9; c10; c11 |] + + let to_coeffs a = + ( a.(0), + a.(1), + a.(2), + a.(3), + a.(4), + a.(5), + a.(6), + a.(7), + a.(8), + a.(9), + a.(10), + a.(11) ) +end diff --git a/lib/fields.mli b/lib/fields.mli index 3fc15dc..4ecad01 100644 --- a/lib/fields.mli +++ b/lib/fields.mli @@ -1,6 +1,6 @@ (** Finite fields used by the BN254 curve and pairings *) -module Fq : sig +module Fp : sig include Field.S val of_string : string -> t @@ -11,24 +11,60 @@ module Fq : sig end (* The prime field over which the BN254 curve is defined. *) -module Fq2 : Field.S with type t = Fq.t * Fq.t -(* The extension field of degree 2 over [Fq]. *) - -(* - module Fq12 : - S - with type t = - Fq.t - * Fq.t - * Fq.t - * Fq.t - * Fq.t - * Fq.t - * Fq.t - * Fq.t - * Fq.t - * Fq.t - * Fq.t - * Fq.t - (* The extension field of degree 12 over [Fq]. *) -*) +module Fp2 : sig + include Field.S + + type coeffs = Fp.t * Fp.t + (** Polynomial coefficients of the extension field. *) + + 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]. *) + + val to_coeffs : t -> coeffs + (** [to_coeffs a] are the polynomial coefficients of [a]. *) +end +(* The polynomial field extension of degree 2 for BN254 pairings. *) + +module Fp12 : sig + include Field.S + + type coeffs = + Fp.t + * Fp.t + * Fp.t + * Fp.t + * Fp.t + * Fp.t + * Fp.t + * Fp.t + * Fp.t + * Fp.t + * Fp.t + * Fp.t + (** Polynomial coefficients of the extension field. *) + + 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]. *) + + val to_coeffs : t -> coeffs + (** [to_coeffs a] are the polynomial coefficients of [a]. *) +end +(* The polynomial field extension of degree 12 for BN254 pairings. *) |
