aboutsummaryrefslogtreecommitdiff
path: root/lib/bls12_381.ml
blob: 6cb329be923a51e0a6c1fb836bb324d3ddc8968e (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
module Fields = struct
  module Fp = Field.Make (struct
    let p =
      Z.of_string
        "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787"
  end)

  module Fp2 = Field.MakeExtension (struct
    module F = Fp

    let q = Fp.[| one; zero |]
  end)

  module Fp12 = Field.MakeExtension (struct
    module F = Fp

    let q =
      Fp.
        [|
          of_string "2";
          zero;
          zero;
          zero;
          zero;
          zero;
          of_string "-2";
          zero;
          zero;
          zero;
          zero;
          zero;
        |]
  end)
end

module Curves = struct
  open Fields

  let n =
    Z.of_string
      "52435875175126190479447740508185965837690552500527637822603658699938581184513"

  module E = Curve.Make (struct
    module F = Fp

    let b = Fp.of_string "4"

    let g =
      Fp.
        ( of_string
            "3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507",
          of_string
            "1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569"
        )

    let n = n
  end)

  module E2 = Curve.Make (struct
    module F = Fp2

    let of_string_coeffs s = Fp2.of_coeffs @@ Array.map Fp.of_string s
    let b = of_string_coeffs [| "4"; "4" |]

    let g =
      ( of_string_coeffs
          [|
            "352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160";
            "3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758";
          |],
        of_string_coeffs
          [|
            "1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905";
            "927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582";
          |] )

    let n = n
  end)
end

module Pairing = Pairing.Make (struct
  open Fields
  open Curves
  module G1 = E
  module G2 = E2
  module F = Fp12

  let x = Z.of_string "-0xd201000000010000"
  let k = 12

  let twist =
    let o, z = Fp.(one, zero) in
    let ws =
      lazy
        (let w = Fp12.of_coeffs [| z; o; z; z; z; z; z; z; z; z; z; z |] in
         let w2 = Fp12.(w * w) in
         let w3 = Fp12.(w2 * w) in
         let w2inv = Fp12.inv w2 in
         let w3inv = Fp12.inv w3 in
         (w2inv, w3inv))
    in
    let iso a =
      let c = Fp2.to_coeffs a in
      let r0 = Fp.(c.(0) - c.(1)) in
      let r6 = c.(1) in
      Fp12.of_coeffs [| r0; z; z; z; z; z; r6; z; z; z; z; z |]
    in
    fun p ->
      match E2.to_coords p with
      | None -> None
      | Some (x, y) ->
          let nx, ny = (iso x, iso y) in
          let w2inv, w3inv = Lazy.force ws in
          Some Fp12.(nx * w2inv, ny * w3inv)
end)

module G1 = Curves.E
module G2 = Curves.E2
module Gt = Pairing.Gt

let pairing = Pairing.check