aboutsummaryrefslogtreecommitdiff
path: root/test/test_pairing.ml
blob: 2d698baa52d29d6bfcad60d0e77f4a4d5d45641c (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
module Pairing = Plonkers.Bls12_381.Pairing
module E = Plonkers.Bls12_381.Curves.E
module E2 = Plonkers.Bls12_381.Curves.E2
module Gt = Plonkers.Bls12_381.Pairing.Gt

let gt = Primitives.Testables.gt
let f1 = Primitives.Literals.e
let f2 = Primitives.Literals.e2
let ft = Primitives.Literals.gt

let test_generator () =
  Alcotest.(check gt)
    "equals"
    (ft
       [|
         "3408834164464458755751340723502736743445402614640994055433339214424916442641449510670327077303074893591740399366532";
         "1017299873256115394687936133146817339783508925356207904663165117501336940438300180831508525168135575931758510829235";
         "1919447955347661378578718469488414822573562393353397544098339472673901193098010131520945815709482515519580400649694";
         "233557331756520805040932826416017142246700002714928636467514104620558679338483625829873520553081791155104026612174";
         "2882199334237819327910933128503231887000342950792587449681715803095618652567681747054312179958427092970985872417068";
         "579878855790610229145575079003483905368886712027403088810647643659132674704592975818766405092658613973478871626949";
         "3268798540077874559188095304926415053902298079662269128631117894259354335713488241073281881110999874366368799440511";
         "2688711983546324406847903726099677850388169649284572370749691792743294691894680263880362424577638309433508620982332";
         "3520140447471844017044610248175793726488927385800919015247604269617056365166670232096113218155158979455637700602719";
         "1555267885602801620621747613743921378408444002839854792606782732724136830563310004098024452104661167182680744848589";
         "2626389147790168154036854297373352480470342009417426750876875476996308393122833154115295241935326582225976246741447";
         "873321072950766150592434764742436722571574260472569072177205897167854308426485977134306337249021436737462538398830";
       |])
    Pairing.(e E.g E2.g)

let test_pairing () =
  Alcotest.(check gt)
    "equals"
    (ft
       [|
         "448697226789963268348912232951073713452968894642929214443994147359412138639526655164503406978236522025734912852606";
         "151261630108668103390829365027365404889205408289235816621708275624780812413761415129004835746255625267607732639563";
         "2909423043708809630024534763367123821438139353180175913125076930835302419897651130667995073651726848611333745205986";
         "952434542917010958697197601576391780800301526127664682598671275809818228531516224262590129674712650458795890584553";
         "1478486475752483299362722402760792915820803227192648258365429143818886181422874016577411765850893129689832973264031";
         "652834415768413017856200607794773610957395062979681182931987046704660733141291027961343651372030380321651719802520";
         "951656253138365825024367899445523415181713017704780705176410361641958854486267870078190603032432549582347900417624";
         "1991042670420833845569811948628174796662344145916123310837646809687525541032370298949411402142207368319354069329317";
         "2920440541157943770850967007055029075931555684879417457120436377239540586798148490244301682350449674796660182631922";
         "3554199917981379540114579517242213458150684556713432050906212290132174074734340424484104596198947386114933622334567";
         "1091458823916905664137346388296576506864216891159041434853650685402092824487985812791698734521558868411023155785769";
         "43181594668188258041148789943396863898539176535733231639633582884332884962913532441125648697214326572900496337037";
       |])
    (Pairing.e
       (f1
          ( "1759244594739438101652572364306631993712897577542654736932660460743445183171945408603399266474637228371219384150089",
            "2746850152127019659862408918732227356169104535991470589122155020934454571292242598343702169898773975726309716165198"
          ))
       (f2
          ( [|
              "1256740661954421835404627287451128275298916640367011495362432993289541383031490867768256365295187693751267667845404";
              "3717522504942038420636575612519050548073203872805747528897870222147344374794751097864928254417486766328132878640824";
            |],
            [|
              "2512342532199657251630152291613780551312453521116783301133995957210838938697636876773382152437204041044762830468417";
              "77870636695957247485070729371505785036022507830356763789646790529980989972767478012625350608049848760401081873614";
            |] )))

module G1 = E
module G2 = E2
module Fp = Plonkers.Bls12_381.Fields.Fp
module Fp2 = Plonkers.Bls12_381.Fields.Fp2
module Fp12 = Plonkers.Bls12_381.Fields.Fp12

let reference_pairing (pt : G1.t) (q : G2.t) : Fp12.t =
  let extend p =
    match G1.to_coords p with
    | None -> failwith "point at infinity"
    | Some (x, y) -> Fp12.(of_f x, of_f y)
  in
  let twist p =
    let o, z = Fp.(one, zero) in
    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
    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
    match G2.to_coords p with
    | None -> failwith "point at infinity"
    | Some (x, y) ->
        let nx, ny = (iso x, iso y) in
        Fp12.(nx * w2inv, ny * w3inv)
  in
  let line_function q1 q2 p =
    let x1, y1 = twist q1 in
    let x2, y2 = twist q2 in
    let x, y = extend p in
    let two = Fp12.(one + one) in
    let three = Fp12.(two + one) in
    let extra l = Fp12.((l * (x - x1)) + y1 - y) in
    if G2.equal q1 q2 then
      let l = Fp12.(three * x1 * x1 / (two * y1)) in
      extra l
    else if G2.(equal q1 ~-q2) then Fp12.(x - x1)
    else
      let l = Fp12.((y2 - y1) / (x2 - x1)) in
      extra l
  in
  let ci =
    [|
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      -1;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      -1;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      0;
      -1;
      0;
      0;
      -1;
      0;
      -1;
      -1;
    |]
  in
  let l = 63 in
  let c =
    Array.to_seqi ci
    |> Seq.fold_left
         (fun c (i, bit) ->
           let ci = Z.(~$bit * (one lsl i)) in
           Z.(c + ci))
         Z.zero
  in
  Alcotest.(check string) "c computation" "-d201000000010000" (Z.format "%x" c);
  let k = 12 in
  let r = G1.n in
  let miller_loop () =
    let f = ref Fp12.one in
    let t = ref q in
    (if ci.(l) = -1 then t := G2.(~- (!t)));
    for i = l - 1 downto 0 do
      (f := Fp12.(!f * !f * line_function !t !t pt));
      (t := G2.(!t + !t));
      if ci.(i) = 1 then (
        (f := Fp12.(!f * line_function !t q pt));
        t := G2.(!t + q))
      else if ci.(i) = -1 then (
        (f := Fp12.(!f * line_function !t G2.(~-q) pt));
        t := G2.(!t - q))
    done;
    f := Fp12.(pow !f Z.(((pow p k) - one) / r));
    !f
  in
  miller_loop ()

(* https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-pairing-friendly-curves-11#name-test-vectors-of-optimal-ate *)
let test_vector () =
  Alcotest.(check Primitives.Testables.fp12)
    "equals"
    (Primitives.Literals.fp12
       [|
         "0x11619b45f61edfe3b47a15fac19442526ff489dcda25e59121d9931438907dfd448299a87dde3a649bdba96e84d54558";
         "0x153ce14a76a53e205ba8f275ef1137c56a566f638b52d34ba3bf3bf22f277d70f76316218c0dfd583a394b8448d2be7f";
         "0x095668fb4a02fe930ed44767834c915b283b1c6ca98c047bd4c272e9ac3f3ba6ff0b05a93e59c71fba77bce995f04692";
         "0x16deedaa683124fe7260085184d88f7d036b86f53bb5b7f1fc5e248814782065413e7d958d17960109ea006b2afdeb5f";
         "0x09c92cf02f3cd3d2f9d34bc44eee0dd50314ed44ca5d30ce6a9ec0539be7a86b121edc61839ccc908c4bdde256cd6048";
         "0x111061f398efc2a97ff825b04d21089e24fd8b93a47e41e60eae7e9b2a38d54fa4dedced0811c34ce528781ab9e929c7";
         "0x01ecfcf31c86257ab00b4709c33f1c9c4e007659dd5ffc4a735192167ce197058cfb4c94225e7f1b6c26ad9ba68f63bc";
         "0x08890726743a1f94a8193a166800b7787744a8ad8e2f9365db76863e894b7a11d83f90d873567e9d645ccf725b32d26f";
         "0x0e61c752414ca5dfd258e9606bac08daec29b3e2c57062669556954fb227d3f1260eedf25446a086b0844bcd43646c10";
         "0x0fe63f185f56dd29150fc498bbeea78969e7e783043620db33f75a05a0a2ce5c442beaff9da195ff15164c00ab66bdde";
         "0x10900338a92ed0b47af211636f7cfdec717b7ee43900eee9b5fc24f0000c5874d4801372db478987691c566a8c474978";
         "0x1454814f3085f0e6602247671bc408bbce2007201536818c901dbd4d2095dd86c1ec8b888e59611f60a301af7776be3d";
       |])
    (reference_pairing
       (f1
          ( "0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb",
            "0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1"
          ))
       (f2
          ( [|
              "0x024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8";
              "0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e";
            |],
            [|
              "0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801";
              "0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be";
            |] )))

let test_bilinearity () =
  let a =
    Z.of_string
      "49635613352584733781027070618160862805950329685486063826980172894235303933946"
  in
  let b =
    Z.of_string
      "4249060523776624689160540763312478010481990280247166863389957742757316747171"
  in
  Alcotest.(check bool)
    "equals" true
    (Pairing.check
       [
         (E.(scalar_mul a g), E2.(scalar_mul b g));
         (E.(neg @@ scalar_mul b g), E2.(scalar_mul a g));
       ])

let test_inverse () =
  let a =
    f1
      ( "91452683776864857297317878636149143411849300822645600833640900844653175011086096281183060418629991226462334719230",
        "2771533846423619448760061452592233431921571821664938471039058787579168163258312047422278741446979265860731259685588"
      )
  in
  let t = Pairing.e a E2.g in
  let tinv = Pairing.e E.(neg a) E2.g in
  Alcotest.(check gt) "equals" (Gt.inv t) tinv;
  Alcotest.(check gt) "equals" Gt.id (Gt.dot t tinv)

let tests =
  Alcotest.
    [
      test_case "Generator" `Slow test_generator;
      test_case "Pairing" `Slow test_pairing;
      test_case "Vector" `Slow test_vector;
      test_case "Bilinearity" `Slow test_bilinearity;
      test_case "Inverse" `Slow test_inverse;
    ]

(*

  2675223149320665921521646202693247640790817874364255298732398972560239127864099887300921329285059103920214926247256
  3268798540077874559188095304926415053902298079662269128631117894259354335713488241073281881110999874366368799440511
  1437178847597838002205538891928304392505606959215308674013885606166925907773842499174371404735625830937323828692626
  3520140447471844017044610248175793726488927385800919015247604269617056365166670232096113218155158979455637700602719
  1506178926806320088529997600140680210913802140271006315226533143967895395199677036726919792764738011159067846598728
  2626389147790168154036854297373352480470342009417426750876875476996308393122833154115295241935326582225976246741447
  296397698419227591881949966489408966385204245298227609919201225879400018157857419730816679383241778672627140748220
  1313697571675342986569886099636226306168713170654435514582366343380736958596157600562325204551377354604385651577455
  2213584337862344967755109385575965635901738814384224456257761298779336140589044234514789656471272705700109501099024
  2447141669618865772796042211991982778148438817099153092725275403399894819927527860344663177024354496855213527711198
  2549209626480291013679779981989983528616421847439035724344204595297044667359758911489614886787335613326952862534008
  3129088482270901242825355060993467433985308559466438813154852238956177342064351887308381291879994227300431734160957

  3408834164464458755751340723502736743445402614640994055433339214424916442641449510670327077303074893591740399366532
  2985109681965551998729853692589086816773373894582799980668893018622694710052537683611179103960880088106135761730552
  1919447955347661378578718469488414822573562393353397544098339472673901193098010131520945815709482515519580400649694
  3768852223465146588376856999319887014310182817224079248864544031503472971152354238612814108575933872882790245947613
  2882199334237819327910933128503231887000342950792587449681715803095618652567681747054312179958427092970985872417068
  3422530699431057164272214746732420251187996107911604796521410492464898975786244888623921224036357050064415400932838
  3268798540077874559188095304926415053902298079662269128631117894259354335713488241073281881110999874366368799440511
X 1313697571675342986569886099636226306168713170654435514582366343380736958596157600562325204551377354604385651577455
  3520140447471844017044610248175793726488927385800919015247604269617056365166670232096113218155158979455637700602719
X 2447141669618865772796042211991982778148438817099153092725275403399894819927527860344663177024354496855213527711198
  2626389147790168154036854297373352480470342009417426750876875476996308393122833154115295241935326582225976246741447
X 3129088482270901242825355060993467433985308559466438813154852238956177342064351887308381291879994227300431734160957

 *)