aboutsummaryrefslogtreecommitdiff
path: root/lib/fields.ml
blob: 2cdbbdaeaaeac19e4b8cac36799eacc0854ea651 (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
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
  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)