aboutsummaryrefslogtreecommitdiff
path: root/lib/fields.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/fields.ml')
-rw-r--r--lib/fields.ml34
1 files changed, 19 insertions, 15 deletions
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