package bls12-381

  1. Overview
  2. Docs

Source file Fq12.ml

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
module Fq12_stubs = Rustc_bls12_381_bindings.Fq12 (Rustc_bls12_381_stubs)

exception Not_in_field of Bytes.t

let size_in_bytes = 576

type t = Bytes.t

let empty () = Bytes.make size_in_bytes '\000'

let order =
  let fq_order =
    Z.of_string
      "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787"
  in
  Z.pow fq_order 12

let check_bytes bs =
  if Bytes.length bs = size_in_bytes then
    Fq12_stubs.check_bytes (Ctypes.ocaml_bytes_start bs)
  else false

let of_bytes_opt bs = if check_bytes bs then Some bs else None

let of_bytes_exn g = if check_bytes g then g else raise (Not_in_field g)

let to_bytes s = s

let zero =
  let g = empty () in
  Fq12_stubs.zero (Ctypes.ocaml_bytes_start g) ;
  g

let one =
  let g = empty () in
  Fq12_stubs.one (Ctypes.ocaml_bytes_start g) ;
  g

let is_zero g = Fq12_stubs.is_zero (Ctypes.ocaml_bytes_start g)

let is_one g = Fq12_stubs.is_one (Ctypes.ocaml_bytes_start g)

let random ?state () =
  ignore state ;
  let g = empty () in
  Fq12_stubs.random (Ctypes.ocaml_bytes_start g) ;
  g

let rec non_null_random ?state () =
  ignore state ;
  let r = random () in
  if is_zero r then non_null_random () else r

let add g1 g2 =
  assert (Bytes.length g1 = size_in_bytes) ;
  assert (Bytes.length g2 = size_in_bytes) ;
  let g = empty () in
  Fq12_stubs.add
    (Ctypes.ocaml_bytes_start g)
    (Ctypes.ocaml_bytes_start g1)
    (Ctypes.ocaml_bytes_start g2) ;
  g

let ( + ) = add

let mul g1 g2 =
  assert (Bytes.length g1 = size_in_bytes) ;
  assert (Bytes.length g2 = size_in_bytes) ;
  let g = empty () in
  Fq12_stubs.mul
    (Ctypes.ocaml_bytes_start g)
    (Ctypes.ocaml_bytes_start g1)
    (Ctypes.ocaml_bytes_start g2) ;
  g

let ( * ) = mul

let eq g1 g2 =
  assert (Bytes.length g1 = size_in_bytes) ;
  assert (Bytes.length g2 = size_in_bytes) ;
  Fq12_stubs.eq (Ctypes.ocaml_bytes_start g1) (Ctypes.ocaml_bytes_start g2)

let negate g =
  assert (Bytes.length g = size_in_bytes) ;
  let opposite_buffer = empty () in
  Fq12_stubs.negate
    (Ctypes.ocaml_bytes_start opposite_buffer)
    (Ctypes.ocaml_bytes_start g) ;
  opposite_buffer

let ( - ) = negate

let square g =
  assert (Bytes.length g = size_in_bytes) ;
  let buffer = empty () in
  Fq12_stubs.square
    (Ctypes.ocaml_bytes_start buffer)
    (Ctypes.ocaml_bytes_start g) ;
  buffer

let double g =
  assert (Bytes.length g = size_in_bytes) ;
  let buffer = empty () in
  Fq12_stubs.double
    (Ctypes.ocaml_bytes_start buffer)
    (Ctypes.ocaml_bytes_start g) ;
  buffer

let inverse_exn g =
  assert (Bytes.length g = size_in_bytes) ;
  let inverse_buffer = empty () in
  Fq12_stubs.unsafe_inverse
    (Ctypes.ocaml_bytes_start inverse_buffer)
    (Ctypes.ocaml_bytes_start g) ;
  inverse_buffer

let inverse_opt g =
  if is_zero g then None
  else
    let inverse_buffer = empty () in
    Fq12_stubs.unsafe_inverse
      (Ctypes.ocaml_bytes_start inverse_buffer)
      (Ctypes.ocaml_bytes_start g) ;
    Some inverse_buffer

let pow x n =
  let res = empty () in
  let n = Z.erem n (Z.pred order) in
  (* sign is removed by to_bits, but that's fine because we used mod before *)
  let n = Bytes.of_string (Z.to_bits n) in
  let bytes_size_n = Bytes.length n in
  let padded_n =
    Bytes.init size_in_bytes (fun i ->
        if i < bytes_size_n then Bytes.get n i else char_of_int 0)
  in
  Fq12_stubs.pow
    (Ctypes.ocaml_bytes_start res)
    (Ctypes.ocaml_bytes_start (to_bytes x))
    (Ctypes.ocaml_bytes_start padded_n) ;
  res

let ( ** ) = pow

let of_z x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 =
  let x0 = Bytes.of_string (Z.to_bits x0) in
  let x1 = Bytes.of_string (Z.to_bits x1) in
  let x2 = Bytes.of_string (Z.to_bits x2) in
  let x3 = Bytes.of_string (Z.to_bits x3) in
  let x4 = Bytes.of_string (Z.to_bits x4) in
  let x5 = Bytes.of_string (Z.to_bits x5) in
  let x6 = Bytes.of_string (Z.to_bits x6) in
  let x7 = Bytes.of_string (Z.to_bits x7) in
  let x8 = Bytes.of_string (Z.to_bits x8) in
  let x9 = Bytes.of_string (Z.to_bits x9) in
  let x10 = Bytes.of_string (Z.to_bits x10) in
  let x11 = Bytes.of_string (Z.to_bits x11) in
  let g = empty () in
  Bytes.blit x0 0 g 0 (min (Bytes.length x0) 48) ;
  Bytes.blit x1 0 g 48 (min (Bytes.length x1) 48) ;
  Bytes.blit x2 0 g 96 (min (Bytes.length x2) 48) ;
  Bytes.blit x3 0 g 144 (min (Bytes.length x3) 48) ;
  Bytes.blit x4 0 g 192 (min (Bytes.length x4) 48) ;
  Bytes.blit x5 0 g 240 (min (Bytes.length x5) 48) ;
  Bytes.blit x6 0 g 288 (min (Bytes.length x6) 48) ;
  Bytes.blit x7 0 g 336 (min (Bytes.length x7) 48) ;
  Bytes.blit x8 0 g 384 (min (Bytes.length x8) 48) ;
  Bytes.blit x9 0 g 432 (min (Bytes.length x9) 48) ;
  Bytes.blit x10 0 g 480 (min (Bytes.length x10) 48) ;
  Bytes.blit x11 0 g 528 (min (Bytes.length x11) 48) ;
  of_bytes_exn g

let of_string x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 =
  let x0 = Bytes.of_string (Z.to_bits (Z.of_string x0)) in
  let x1 = Bytes.of_string (Z.to_bits (Z.of_string x1)) in
  let x2 = Bytes.of_string (Z.to_bits (Z.of_string x2)) in
  let x3 = Bytes.of_string (Z.to_bits (Z.of_string x3)) in
  let x4 = Bytes.of_string (Z.to_bits (Z.of_string x4)) in
  let x5 = Bytes.of_string (Z.to_bits (Z.of_string x5)) in
  let x6 = Bytes.of_string (Z.to_bits (Z.of_string x6)) in
  let x7 = Bytes.of_string (Z.to_bits (Z.of_string x7)) in
  let x8 = Bytes.of_string (Z.to_bits (Z.of_string x8)) in
  let x9 = Bytes.of_string (Z.to_bits (Z.of_string x9)) in
  let x10 = Bytes.of_string (Z.to_bits (Z.of_string x10)) in
  let x11 = Bytes.of_string (Z.to_bits (Z.of_string x11)) in
  let g = empty () in
  Bytes.blit x0 0 g 0 (min (Bytes.length x0) 48) ;
  Bytes.blit x1 0 g 48 (min (Bytes.length x1) 48) ;
  Bytes.blit x2 0 g 96 (min (Bytes.length x2) 48) ;
  Bytes.blit x3 0 g 144 (min (Bytes.length x3) 48) ;
  Bytes.blit x4 0 g 192 (min (Bytes.length x4) 48) ;
  Bytes.blit x5 0 g 240 (min (Bytes.length x5) 48) ;
  Bytes.blit x6 0 g 288 (min (Bytes.length x6) 48) ;
  Bytes.blit x7 0 g 336 (min (Bytes.length x7) 48) ;
  Bytes.blit x8 0 g 384 (min (Bytes.length x8) 48) ;
  Bytes.blit x9 0 g 432 (min (Bytes.length x9) 48) ;
  Bytes.blit x10 0 g 480 (min (Bytes.length x10) 48) ;
  Bytes.blit x11 0 g 528 (min (Bytes.length x11) 48) ;
  of_bytes_exn g

let div_exn a b =
  if b = zero then raise Division_by_zero else mul a (inverse_exn b)

let div_opt a b = if b = zero then None else Some (mul a (inverse_exn b))

let ( / ) = div_exn

let ( = ) = eq
OCaml

Innovation. Community. Security.