eth_pairings_cpp 0.1.1

EIP1962 reference implementation in C++
Documentation
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#ifndef H_MNTengine
#define H_MNTengine

#include "../common.h"
#include "../curve.h"
#include "../fp.h"
#include "../extension_towers/fp2.h"
#include "../extension_towers/fp4.h"

template <class F, usize N>
struct AteDoubleCoefficients
{
    F c_h, c_4c, c_j, c_l;
};

template <class F, usize N>
struct AteAdditionCoefficients
{
    F c_l1, c_rz;
};

template <class F, usize N>
struct ExtendedCoordinates
{
    F x, y, z, t;
};

template <class F, usize N>
struct PrecomputedG1
{
    Fp<N> x, y;
    F x_by_twist, y_by_twist;
};

template <class F, usize N>
struct PrecomputedG2
{
    F x, y, x_over_twist, y_over_twist;
    std::vector<AteDoubleCoefficients<F, N>> double_coefficients;
    std::vector<AteAdditionCoefficients<F, N>> addition_coefficients;
};

template <class F1, class F2, usize N>
class MNTengine
{
    std::vector<u64> x;
    bool x_is_negative;
    std::vector<u64> exp_w0, exp_w1;
    bool exp_w0_is_negative;
    WeierstrassCurve<F1> const &curve_twist;
    F1 twist;

public:
    MNTengine(std::vector<u64> x, bool x_is_negative, std::vector<u64> exp_w0, std::vector<u64> exp_w1, bool exp_w0_is_negative,
              WeierstrassCurve<F1> const &curve_twist, F1 twist) : x(x), x_is_negative(x_is_negative), exp_w0(exp_w0), exp_w1(exp_w1), exp_w0_is_negative(exp_w0_is_negative), curve_twist(curve_twist), twist(twist)
    {
    }

    template <class C>
    std::optional<F2>
    pair(std::vector<std::tuple<CurvePoint<Fp<N>>, CurvePoint<F1>>> const &points, C const &context) const
    {
        if (points.size() == 0)
        {
            return {};
        }
        auto res = miller_loop(points, context);
        return final_exponentiation(res);
    }

private:
    template <class C>
    F2 miller_loop(std::vector<std::tuple<CurvePoint<Fp<N>>, CurvePoint<F1>>> const &points, C const &context) const
    {
        auto f = F2::one(context);
        for (auto it = points.cbegin(); it != points.cend(); it++)
        {
            f.mul(ate_pairing_loop(std::get<0>(*it), std::get<1>(*it), context));
        }
        return f;
    }

    template <class C>
    F2 ate_pairing_loop(
        CurvePoint<Fp<N>> const &point,
        CurvePoint<F1> const &twist_point, C const &context) const
    {
        assert(point.is_normalized());
        assert(twist_point.is_normalized());
        auto const twist_inv = this->twist.inverse().value();

        auto const p = precompute_g1(point);
        auto const q = precompute_g2(twist_point, twist_inv, context);
        auto l1_coeff = F1::zero(context);
        l1_coeff.c0 = p.x;
        l1_coeff.sub(q.x_over_twist);

        auto f = F2::one(context);

        usize dbl_idx = 0;
        usize add_idx = 0;
        // The for loop is executed for all bits (EXCEPT the MSB itself) of
        auto it = RevBitIterator(this->x);
        it.before(); // skip 1
        while (it.before())
        {
            auto const bit = *it;

            auto const dc = q.double_coefficients[dbl_idx];
            dbl_idx += 1;

            auto g_rr_at_p = F2::zero(context);

            auto t0 = dc.c_j;
            t0.mul(p.x_by_twist);
            t0.negate();
            t0.add(dc.c_l);
            t0.sub(dc.c_4c);

            auto t1 = dc.c_h;
            t1.mul(p.y_by_twist);

            g_rr_at_p.c0 = t0;
            g_rr_at_p.c1 = t1;

            f.square();
            f.mul(g_rr_at_p);

            if (bit)
            {
                auto const ac = q.addition_coefficients[add_idx];
                add_idx += 1;

                auto g_rq_at_p = F2::zero(context);

                auto t0 = ac.c_rz;
                t0.mul(p.y_by_twist);

                auto t = l1_coeff;
                t.mul(ac.c_l1);

                auto t1 = q.y_over_twist;
                t1.mul(ac.c_rz);
                t1.add(t);
                t1.negate();

                g_rq_at_p.c0 = t0;
                g_rq_at_p.c1 = t1;

                f.mul(g_rq_at_p);
            }
        }

        if (this->x_is_negative)
        {
            auto const ac = q.addition_coefficients[add_idx];

            auto g_rnegr_at_p = F2::zero(context);

            auto t0 = ac.c_rz;
            t0.mul(p.y_by_twist);

            auto t = l1_coeff;
            t.mul(ac.c_l1);

            auto t1 = q.y_over_twist;
            t1.mul(ac.c_rz);
            t1.add(t);
            t1.negate();

            g_rnegr_at_p.c0 = t0;
            g_rnegr_at_p.c1 = t1;

            f.mul(g_rnegr_at_p);
            f = f.inverse().value();
        }

        return f;
    }

    PrecomputedG1<F1, N> precompute_g1(CurvePoint<Fp<N>> const &g1_point) const
    {
        // not asserting normalization, it will be asserted in the loop
        auto x_twist = this->twist;
        x_twist.mul_by_fp(g1_point.x);

        auto y_twist = this->twist;
        y_twist.mul_by_fp(g1_point.y);

        return PrecomputedG1<F1, N>{
            g1_point.x,
            g1_point.y,
            x_twist,
            y_twist,
        };
    }

    template <class C>
    PrecomputedG2<F1, N> precompute_g2(CurvePoint<F1> const &g2_point, F1 const &twist_inv, C const &context) const
    {
        // not asserting normalization, it will be asserted in the loop
        // precompute addition and doubling coefficients
        auto x_over_twist = g2_point.x;
        x_over_twist.mul(twist_inv);

        auto y_over_twist = g2_point.y;
        y_over_twist.mul(twist_inv);

        auto g2_p = PrecomputedG2<F1, N>{
            g2_point.x,
            g2_point.y,
            x_over_twist,
            y_over_twist,
            std::vector<AteDoubleCoefficients<F1, N>>(),
            std::vector<AteAdditionCoefficients<F1, N>>(),
        };

        auto r = ExtendedCoordinates<F1, N>{
            g2_point.x,
            g2_point.y,
            F1::one(context),
            F1::one(context),
        };

        auto it = RevBitIterator(this->x);
        it.before(); // skip 1
        while (it.before())
        {
            auto const coeff = this->doubling_step(r);
            g2_p.double_coefficients.push_back(coeff);

            if (*it)
            {
                auto const coeff = this->addition_step(g2_point.x, g2_point.y, r);
                g2_p.addition_coefficients.push_back(coeff);
            }
        }

        if (this->x_is_negative)
        {
            auto const rz_inv = r.z.inverse().value();
            auto rz2_inv = rz_inv;
            rz2_inv.square();
            auto rz3_inv = rz_inv;
            rz3_inv.mul(rz2_inv);

            auto minus_r_affine_x = rz2_inv;
            minus_r_affine_x.mul(r.x);
            auto minus_r_affine_y = rz3_inv;
            minus_r_affine_y.mul(r.y);
            minus_r_affine_y.negate();

            auto const coeff = this->addition_step(
                minus_r_affine_x,
                minus_r_affine_y,
                r);

            g2_p.addition_coefficients.push_back(coeff);
        }

        return g2_p;
    }

    AteDoubleCoefficients<F1, N> doubling_step(ExtendedCoordinates<F1, N> &r) const
    {
        auto a = r.t;
        a.square();
        auto b = r.x;
        b.square();
        auto c = r.y;
        c.square();
        auto d = c;
        d.square();

        auto e = r.x;
        e.add(c);
        e.square();
        e.sub(b);
        e.sub(d);

        auto f = this->curve_twist.get_a();
        f.mul(a);
        f.add(b);
        f.add(b);
        f.add(b);

        auto g = f;
        g.square();

        auto d_eight = d;
        d_eight.mul2();
        d_eight.mul2();
        d_eight.mul2();

        auto t0 = e;
        t0.mul2();
        t0.mul2();

        auto x = g;
        x.sub(t0);

        auto y = e;
        y.mul2();
        y.sub(x);
        y.mul(f);
        y.sub(d_eight);

        auto h0 = r.z;
        h0.square();

        auto z = r.y;
        z.add(r.z);
        z.square();
        z.sub(c);
        z.sub(h0);

        auto t = z;
        t.square();

        auto c_h = z;
        c_h.add(r.t);
        c_h.square();
        c_h.sub(t);
        c_h.sub(a);

        auto c_4c = c;
        c_4c.mul2();
        c_4c.mul2();

        auto c_j = f;
        c_j.add(r.t);
        c_j.square();
        c_j.sub(g);
        c_j.sub(a);

        auto c_l = f;
        c_l.add(r.x);
        c_l.square();
        c_l.sub(g);
        c_l.sub(b);

        auto const coeff = AteDoubleCoefficients<F1, N>{c_h, c_4c, c_j, c_l};

        r.x = x;
        r.y = y;
        r.z = z;
        r.t = t;

        return coeff;
    }

    AteAdditionCoefficients<F1, N> addition_step(F1 const &x, F1 const &y, ExtendedCoordinates<F1, N> &r) const
    {
        auto a = y;
        a.square();
        auto b = r.t;
        b.mul(x);

        auto d = r.z;
        d.add(y);
        d.square();
        d.sub(a);
        d.sub(r.t);
        d.mul(r.t);

        auto h = b;
        h.sub(r.x);

        auto i = h;
        i.square();

        auto e = i;
        e.mul2();
        e.mul2();

        auto j = h;
        j.mul(e);

        auto v = r.x;
        v.mul(e);

        auto l1 = d;
        l1.sub(r.y);
        l1.sub(r.y);

        auto x0 = l1;
        x0.square();
        x0.sub(j);
        x0.sub(v);
        x0.sub(v);

        auto t0 = r.y;
        t0.mul2();
        t0.mul(j);

        auto y0 = v;
        y0.sub(x0);
        y0.mul(l1);
        y0.sub(t0);

        auto z = r.z;
        z.add(h);
        z.square();
        z.sub(r.t);
        z.sub(i);

        auto t = z;
        t.square();

        auto const coeff = AteAdditionCoefficients<F1, N>{l1, z};

        r.x = x0;
        r.y = y0;
        r.z = z;
        r.t = t;

        return coeff;
    }

    std::optional<F2> final_exponentiation(F2 const &f) const
    {
        auto const ovalue_inv = f.inverse();
        if (ovalue_inv)
        {
            return {};
        }
        auto const value_inv = ovalue_inv.value();
        auto const value_to_first_chunk = this->final_exponentiation_part_one(f, value_inv);
        auto const value_inv_to_first_chunk = this->final_exponentiation_part_one(value_inv, f);

        return this->final_exponentiation_part_two(value_to_first_chunk, value_inv_to_first_chunk);
    }

    virtual F2 final_exponentiation_part_one(F2 const &elt, F2 const &elt_inv) const = 0;

    F2 final_exponentiation_part_two(F2 const &elt, F2 const &elt_inv) const
    {
        auto elt_q = elt;
        elt_q.frobenius_map(1);

        auto w1_part = elt_q.cyclotomic_exp(this->exp_w1);
        if (this->exp_w0_is_negative)
        {
            auto w0_part = elt_inv.cyclotomic_exp(this->exp_w0);
            w1_part.mul(w0_part);
        }
        else
        {
            auto w0_part = elt.cyclotomic_exp(this->exp_w0);
            w1_part.mul(w0_part);
        }

        return w1_part;
    }
};

#endif