(** Finite field. *) (** Module signature for a finite field. *) module type S = sig type t (** A type representing an element in the finite field. *) val p : Z.t (** The prime modulus of the field. *) val zero : t (** The zero value, i.e. the additive identity. *) 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]. *) val neg : t -> t (** [neg a] is the additive inverse of [a]. [add a (neg a) = zero] for all [a]. *) val inv : t -> t (** [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] *) val sub : t -> t -> t (** [sub a b] is the difference of [a] minus [b]. It is equivalent to [add a (neg b)]. *) val mul : t -> t -> t (** [add a b] is the product of [a] times [b] *) val div : t -> t -> t (** [div a b] is the field division of [a] by [b]. It is equivalent to [mul a (inv b)]. Furthermore, we define [div a zero = zero]. *) val pow : t -> Z.t -> t (** [pow a exp] is [a] to the power of [exp]. [pow zero zero] is defined to be [zero]. *) val ( ~- ) : t -> t (** [neg]. *) val ( ~/ ) : t -> t (** [inv]. *) val ( + ) : t -> t -> t (** [add]. *) val ( - ) : t -> t -> t (** [sub]. *) val ( * ) : t -> t -> t (** [mul]. *) val ( / ) : t -> t -> t (** [div]. *) end (** Module signature for a polynomial extension field. *) module type SX = sig include S type f (** The field over which the polynomial is defined. *) val of_f : f -> t (** [of_f a] maps an underlying field element to the extension field. *) val of_coeffs : f array -> t (** [of_coeffs c] is the field element with polynomial coefficients [c]. *) val to_coeffs : t -> f array (** [to_coeffs a] are the polynomial coefficients of [a]. *) end module Make (PRIME : sig val p : Z.t end) : S = struct type t = Z.t let p = PRIME.p let mod_p x = Z.erem x p 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 add a b = Z.add a b |> mod_p let mul a b = Z.mul a b |> mod_p let pow a exp = if equal a zero then zero else Z.powm a exp p let inv a = pow a Z.minus_one let sub a b = add a (neg b) let div a b = mul a (inv b) let ( ~- ) = neg let ( ~/ ) = inv let ( + ) = add let ( - ) = sub let ( * ) = mul let ( / ) = div end module MakeExtension (POLY : sig module F : S val q : F.t array end) : SX with type f = POLY.F.t = struct module F = POLY.F type f = F.t let q = POLY.q let degree = Array.length q let () = assert (degree >= 2) let of_f a = Array.init degree (function 0 -> a | _ -> F.zero) let of_coeffs a = if Array.length a == degree then Array.copy a else invalid_arg "wrong number of elements" let to_coeffs = Array.copy type t = f array let p = F.p let zero = Array.make degree F.zero let one = Array.init degree (function 0 -> F.one | _ -> F.zero) let of_string s = let strip_parens s = if String.starts_with ~prefix:"(" s && String.ends_with ~suffix:")" s then String.sub s 1 (String.length s - 2) else invalid_arg "field element missing parentheses" in let a = strip_parens s |> String.split_on_char ',' |> List.map F.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 F.to_string a |> Array.to_list |> String.concat "," |> Printf.sprintf "(%s)" let equal = Array.for_all2 F.equal let neg a = Array.map F.neg a let add a b = Array.map2 F.add a b let mul2 a b = let a0, a1 = (a.(0), a.(1)) in let b0, b1 = (b.(0), b.(1)) in let t1 = F.mul a0 b0 in let t2 = F.mul a1 b1 in let r0 = F.sub t1 t2 in let r1 = F.(sub (mul (add a0 a1) (add b0 b1)) (add t1 t2)) in [| r0; r1 |] let muln a b = let r = Array.make ((degree * 2) - 1) F.zero in for i = 0 to degree - 1 do for j = 0 to degree - 1 do r.(i + j) <- F.add r.(i + j) (F.mul a.(i) b.(j)) done done; for i = Array.length r - 1 downto degree do let qi = r.(i) in if not (F.equal qi F.zero) then for j = 0 to degree - 1 do let j' = i - degree + j in r.(j') <- F.sub r.(j') (F.mul qi q.(j)) done else () done; Array.sub r 0 degree let inv2 a = let a0, a1 = (a.(0), a.(1)) in let factor = F.(inv (add (mul a0 a0) (mul a1 a1))) in F.[| mul factor a0; mul factor (neg a1) |] let invn a = let deg a = let rec deg' = function | 0 -> 0 | i -> if F.equal a.(i) F.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) F.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 = F.div r.(db + i) c in q.(i) <- qd; for j = 0 to db do r.(i + j) <- F.sub r.(i + j) (F.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) <- F.sub r.(i + j) (F.mul b.(i) q.(j)) done done; r in let t = ref (Array.append zero [| F.zero |]) in let newt = ref (Array.append one [| F.zero |]) in let r = ref (Array.append q [| F.one |]) in let newr = ref (Array.append a [| F.zero |]) in while Array.for_all F.(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 = F.inv !r.(0) in Array.map (F.mul a0) (Array.sub !t 0 degree) let mul, inv = if degree = 2 then (mul2, inv2) else (muln, invn) let pow a exp = if Array.to_seq a |> Seq.drop 1 |> Seq.for_all F.(equal zero) then let f = a.(0) in if F.(equal zero f) then zero else if F.(equal one f) then one else if Z.(equal exp zero) then one else of_f @@ F.pow a.(0) exp else if Z.(equal exp zero) then one else if Z.(equal exp one) then a else let abs = Z.abs exp in let nbits = Z.numbits abs in let rec loop pow bit res = let res' = if Z.testbit abs bit then mul res pow else res in let bit' = bit + 1 in if bit' = nbits then res' else let pow' = mul pow pow in loop pow' bit' res' in let res = loop a 0 one in if Z.sign exp >= 0 then res else inv res let sub a b = add a (neg b) let div a b = mul a (inv b) let ( ~- ) = neg let ( ~/ ) = inv let ( + ) = add let ( - ) = sub let ( * ) = mul let ( / ) = div end