blob: 0ad50283d588453f99bdc0f28ebf6d17a477db58 (
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
|
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 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
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
@@ Array.map Fields.Fp.of_string
[|
"3408834164464458755751340723502736743445402614640994055433339214424916442641449510670327077303074893591740399366532";
"1017299873256115394687936133146817339783508925356207904663165117501336940438300180831508525168135575931758510829235";
"1919447955347661378578718469488414822573562393353397544098339472673901193098010131520945815709482515519580400649694";
"233557331756520805040932826416017142246700002714928636467514104620558679338483625829873520553081791155104026612174";
"2882199334237819327910933128503231887000342950792587449681715803095618652567681747054312179958427092970985872417068";
"579878855790610229145575079003483905368886712027403088810647643659132674704592975818766405092658613973478871626949";
"3268798540077874559188095304926415053902298079662269128631117894259354335713488241073281881110999874366368799440511";
"2688711983546324406847903726099677850388169649284572370749691792743294691894680263880362424577638309433508620982332";
"3520140447471844017044610248175793726488927385800919015247604269617056365166670232096113218155158979455637700602719";
"1555267885602801620621747613743921378408444002839854792606782732724136830563310004098024452104661167182680744848589";
"2626389147790168154036854297373352480470342009417426750876875476996308393122833154115295241935326582225976246741447";
"873321072950766150592434764742436722571574260472569072177205897167854308426485977134306337249021436737462538398830";
|]
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
|