blob: 68430ddaf8daaa0a5bf3d9448ab60d1cbe3ff213 (
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
|
module Make (BLS : sig
module G1 : Curve.S
module G2 : Curve.S
module F : Field.SX
val x : Z.t
val g : F.t
val g1_to_coords : G1.t -> (F.t * F.t) option
val g2_to_coords : G2.t -> (F.t * F.t) option
end) =
struct
module G1 = BLS.G1
module G2 = BLS.G2
module F = BLS.F
module Gt = struct
type t = F.t
let id = F.one
let g = BLS.g
let dot = F.mul
let inv = F.inv
let to_f a = a
let equal = F.equal
let to_string = F.to_string
module Unsafe = struct
let of_f_unchecked a = a
let of_string_unchecked = F.of_string
end
end
let ate_loop =
lazy
(let x = Z.abs BLS.x in
let len = Z.numbits x - 1 in
Array.init len (fun i ->
let pos = len - i - 1 in
Z.testbit x pos))
let negative_x = Z.sign BLS.x < 0
let final_exponent = lazy Z.((pow F.p 12 - one) / G1.n)
let line =
let two = F.(one + one) in
let three = F.(one + one + one) in
let unwrap = function
| Some x -> x
| None -> invalid_arg "line with point at infinity"
in
let g1_to_coords p = BLS.g1_to_coords p |> unwrap in
let g2_to_coords p = BLS.g2_to_coords p |> unwrap in
fun p q r ->
let xp, yp = g2_to_coords p in
let xr, yr = g1_to_coords r in
if G2.equal p q then
let m = F.(three * xp * xp / (two * yp)) in
F.((m * (xr - xp)) - (yr - yp))
else if G2.(equal p (neg q)) then F.(xr - xp)
else
let xq, yq = g2_to_coords q in
let m = F.((yq - yp) / (xq - xp)) in
F.((m * (xr - xp)) - (yr - yp))
let e' p q =
if G1.(equal p inf) || G2.(equal q inf) then F.one
else
let ate_loop = Lazy.force ate_loop in
let _, f =
Array.fold_left
(fun (r, f) bit ->
let t = line r r p in
let f' = F.(f * f * t) in
let r' = G2.add r r in
if bit then
let t = line r' q p in
let f'' = F.(f' * t) in
let r'' = G2.add r' q in
(r'', f'')
else (r', f'))
(q, F.one) ate_loop
in
if negative_x then f else f
let e p q =
let t = e' p q in
let exp = Lazy.force final_exponent in
F.pow t exp
end
module P = Make (struct
module G1 = Curves.E
module G2 = Curves.E2
module F = Fields.Fp12
let x = Z.of_string "-0xd201000000010000"
let g =
F.of_coeffs
Fields.Fp.
[|
of_string
"2819105605953691245277803056322684086884703000473961065716485506033588504203831029066448642358042597501014294104502";
of_string
"1323968232986996742571315206151405965104242542339680722164220900812303524334628370163366153839984196298685227734799";
of_string
"2987335049721312504428602988447616328830341722376962214011674875969052835043875658579425548512925634040144704192135";
of_string
"3879723582452552452538684314479081967502111497413076598816163759028842927668327542875108457755966417881797966271311";
of_string
"261508182517997003171385743374653339186059518494239543139839025878870012614975302676296704930880982238308326681253";
of_string
"231488992246460459663813598342448669854473942105054381511346786719005883340876032043606739070883099647773793170614";
of_string
"3993582095516422658773669068931361134188738159766715576187490305611759126554796569868053818105850661142222948198557";
of_string
"1074773511698422344502264006159859710502164045911412750831641680783012525555872467108249271286757399121183508900634";
of_string
"2727588299083545686739024317998512740561167011046940249988557419323068809019137624943703910267790601287073339193943";
of_string
"493643299814437640914745677854369670041080344349607504656543355799077485536288866009245028091988146107059514546594";
of_string
"734401332196641441839439105942623141234148957972407782257355060229193854324927417865401895596108124443575283868655";
of_string
"2348330098288556420918672502923664952620152483128593484301759394583320358354186482723629999370241674973832318248497";
|]
let g1_to_coords p =
G1.to_coords p |> Option.map (fun (x, y) -> (F.of_f x, F.of_f y))
let g2_to_coords p = Curves.E12.to_coords @@ Curves.twist p
end)
module G1 = P.G1
module G2 = P.G2
module Gt = P.Gt
let e' = P.e'
let e = P.e
|