aboutsummaryrefslogtreecommitdiff
path: root/lib/fields.ml
blob: f18977b693c4d83657fa91e937e11e87561f9ca8 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
module Fimpl (F : sig
  type t

  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
  val add : t -> t -> t
  val mul : t -> t -> t
end) : Field.S with type t = F.t = struct
  type t = F.t

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

module Fp = struct
  let p =
    Z.of_string
      "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787"

  let p_minus_2 = Z.(p - ~$2)
  let mod_p x = Z.erem x p

  include Fimpl (struct
    type t = Z.t

    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)
end

module FpN (R : sig
  val p : Fp.t array
end) =
struct
  let degree = Array.length R.p

  include Fimpl (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 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 failwith "field element missing parentheses"
      in
      let a =
        strip_parens s |> String.split_on_char ',' |> 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

    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)
end

module Fp2 = struct
  include FpN (struct
    let p = Fp.[| one; 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 "2";
          zero;
          zero;
          zero;
          zero;
          zero;
          of_string "-2";
          zero;
          zero;
          zero;
          zero;
          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