diff options
Diffstat (limited to 'lib/fields.ml')
| -rw-r--r-- | lib/fields.ml | 216 |
1 files changed, 178 insertions, 38 deletions
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 |
