aboutsummaryrefslogtreecommitdiff
path: root/lib/field.ml
blob: 8840f5be823b912f398b9fbc32424c40506bf02b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
(** Finite field. *)

(** Module signature for a finite field. *)
module type S = sig
  type t
  (** A type representing an element in the finite field *)

  val zero : t
  (** The zero value, i.e. the additive identity. *)

  val one : t
  (** The one value, i.e. the multiplicative identity. *)

  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 ( ~- ) : 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

(** 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