Try   HackMD

Computing the Optimal Ate Pairing over the BN254 Curve

The post BN254 For The Rest Of Us constitutes the base point for this article, so I highly recommend reading it to get a solid background of the BN254 curve. This post will reuse some parts with an emphasy on the computational aspect of the optimal Ate pairing over the BN254 curve.

For those with a poor background on Pairing-Based Cryptography (PBC) but with a decent background on Elliptic-Curve Cryptoraphy (ECC) I recommend reading the following resources in order:

  1. Pairings For Beginners.
  2. Pairing-Friendly Elliptic Curves of Prime Order.
  3. High-Speed Software Implementation of the Optimal Ate Pairing over Barreto–Naehrig Curves.

Only after (and not before) you feel sufficiently comfortable with both PBC and ECC come back to this post and start to make sense out of it.

The implementation in zkASM can be found here.

The Curve

The BN254 is the elliptic curve

E of the form:
y2=x3+3

defined over a finite field
Fp
, where the prime
p
is the following
254
-bit number:

p = 21888242871839275222246405745257275088696311157297823662689037894645226208583

The number of points

#E(Fp)=p+1t happens to also be a prime
r
of
254
bits:

r = 21888242871839275222246405745257275088548364400416034343698204186575808495617

where t = 147946756881789318990833708069417712967 is known as the trace of Frobenius.

The point

(1,2)E(Fp) is a point of order
r
, i.e. a generator of the group of points of the curve. In fact, all points in
E(Fp){O}
are generators, as Lagrange's Theorem shows.

The embedding degree

k of this curve is
12
.

For more information, refer to BN254 For The Rest Of Us or the EIPs 196 and 197.

Tower of Extension Fields

To represent

Fp12 we use the following tower of extension fields:
fields:
Fp2=Fp[u]/(u2β),Fp6=Fp2[v]/(v3ξ),Fp12=Fp6[w]/(w2v),

where
β
is not a quadratic residue in
Fp
and
ξ
is neither a quadratic residue or a cubic residue in
Fp2
.

In the particular case of the BN254, we have

β=1 and
ξ=9+u
and therefore:
Fp2=Fp[u]/(u2+1),Fp6=Fp2[v]/(v3(9+u)),Fp12=Fp6[w]/(w2v),

from which we can extract the identities

u2=1,
w2=v
,
v3=9+u
and consequently the idenity
w6=9+u
.

Using the latter identity, we can also write:

Fp12=Fp2[w]/(w6(9+u))

These two representations of

Fp12 will allow us to apply some optimizations:

  1. Writing any element
    fFp12
    as
    f=g+hw
    , with
    g,hFp6
    implies [1]
    fp6=f¯=ghw
    . Thus, computing the
    p6
    -th power of any element in
    Fp12
    is computationally "free".
  2. Now, if we write
    g=g0+g1v+g2v2
    and
    h=h0+h1v+h2v2
    where
    g0
    ,
    g1
    ,
    g2
    ,
    h0
    ,
    h1
    ,
    h2Fp2
    we have:
    f=g0+h0w+g1w2+h1w3+g2w4+h2w5,

    which will allow us to compute
    fp,fp2
    and
    fp3
    (needed for the final exponentiation) very efficiently.

Twist

BN curves admits a sextic twist

E defined over
Fp2
of the form:
y2=x3+3/(9+u)

or more specifically:

(y')² = (x')³ + (19485874751759354771024239261021720505790618469301721065564631296452457478373 + 266929791119991161246907387137283842545076965332900288569378510910307636690·u)

This means that pairing computations can be restricted to points

PE(Fp) and
QE(Fp2)
and avoid computations over
Fp12
entirely.

The number of points of

E is:
#E(Fp2)=(p+1t)(p1+t)=rc

which concrete to:

#E' = 479095176016622842441988045216678740799252316531100822436447802254070093686356349204969212544220033486413271283566945264650845755880805213916963058350733
c = 21888242871839275222246405745257275088844257914179612981679871602714643921549

The mapping

Ψ(x,y)=(w2x,w3y)
defines a group homomorphism from
E(Fp2)
to
E(Fp12)
and we are going to use it to send points from the twisted curve
E
to the curve
E
(but not the other way around).

The Pairing

The optimal Ate pairing

e:G1×G2GT over the BN254 curve is defined by:
e(P,Q)=(f6x+2,Q(P)[6x+2]Ψ(Q),πp(Ψ(Q))(P)[6x+2]Ψ(Q)+πp(Ψ(Q)),πp2(Ψ(Q))(P))p121r

where:

  • G1=E(Fp)[r] is the only subgroup of the
    r
    -torsion of
    E
    over
    Fp
    .

  • G2=E(Fp2)[r] is the only subgroup of the
    r
    -torsion of
    E
    over
    Fp2
    .

  • GT=μr is the set of
    r
    -th roots of unity over
    Fp12
    .

  • x = 4965661367192848881 and therefore 6x+2 = 29793968203157093288, which is a number of

    65 bits that can be expressed in the base
    {1,0,1}
    as follows:

    ​​​​2^3 + 2^5 - 2^7 + 2^10 - 2^11 + 2^14 + 2^17 + 2^18 - 2^20 + 2^23 - 2^25 + 2^30 + 2^31 + 2^32 - 2^35 + 2^38 - 2^44 + 2^47 + 2^48 - 2^51 + 2^55 + 2^56 - 2^58 + 2^61 + 2^63 + 2^64
    ​​​​[0, 0, 0, 1, 0, 1, 0, -1, 0, 0, 1, -1, 0, 0, 1, 0, 0, 1, 1, 0, -1, 0, 0, 1, 0, -1, 0, 0, 0, 0, 1, 1, 1, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 1, 0, 0, -1, 0, 0, 0, 1, 1, 0, -1, 0, 0, 1, 0, 1, 1]
    ​​​​0001010-1001-100100110-10010-1000011100-100100000-1001100-1000110-1001011
    

    which has a Hamming weight of 26.

    We prefer base

    {1,0,1} over base
    {0,1}
    because 6x+2 expressed in the latter is:

    ​​​​2^3 + 2^5 + 2^7 + 2^8 + 2^9 + 2^11 + 2^12 + 2^13 + 2^17 + 2^18 + 2^20 + 2^21 + 2^22 + 2^25 + 2^26 + 2^27 + 2^28 + 2^29 + 2^31 + 2^32 + 2^35 + 2^36 + 2^37 + 2^44 + 2^45 + 2^46 + 2^48 + 2^51 + 2^52 + 2^53 + 2^54 + 2^56 + 2^58 + 2^59 + 2^60 + 2^63 + 2^64 
    ​​​​[0,0,0,1,0,1,0,1,1,1,0,1,1,1,0,0,0,1,1,0,1,1,1,0,0,1,1,1,1,1,0,1,1,0,0,1,1,1,0,0,0,0,0,0,1,1,1,0,1,0,0,1,1,1,1,0,1,0,1,1,1,0,0,1,1]
    ​​​​00010101110111000110111001111101100111000000111010011110101110011
    

    which has a Hamming weigth of 37.

    I moved from base

    {0,1} to base
    {1,0,1}
    by using the identity[2]
    2n+12m=2n+2n1+...+2m,
    that holds for any
    n,mN
    such that
    nm
    . In words, it is unpacking groups of
    1
    's of any size to a single
    1
    and
    1
    in the binary decomposition.

  • fi,Q, for any
    iN
    and
    QG2
    , is a
    Fp12
    rational function on
    E
    that is computed using Miller's "double-and-add"-style algorithm:
    fi+j,Q=fi,Qfj,Q[i]Ψ(Q),[j]Ψ(Q)v[i+j]Ψ(Q),fi+1,Q=fm,Q[i]Ψ(Q),Ψ(Q)v[i+1]Ψ(Q),

    starting with
    f1,Q=1
    .

  • P1,P2 denotes the equation of a line passing through
    P1
    and
    P2
    while
    vP
    denotes the equation of a vertical line passing through
    P
    . However, due to the final exponentiation, the value of
    e(P,Q)
    remains unchanged if we omit the division by the vertical lines
    , so we will not care about such computation.

  • πpi(x,y)=(xpi,ypi) is known as the Frobenius operator.

Therefore, what we want to compute is the following:

bound = [0, 0, 0, 1, 0, 1, 0, -1, 0, 0, 1, -1, 0, 0, 1, 0, 0, 1, 1, 0, -1, 0, 0, 1, 0, -1, 0, 0, 0, 0, 1, 1, 1, 0, 0, -1, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 1, 0, 0, -1, 0, 0, 0, 1, 1, 0, -1, 0, 0, 1, 0, 1, 1] 1101111010011011000001001001000111011101011100001011001111110111111100000111110100101101000001011101000011111001111110101000110 # This is 6x+2 in base {-1,0,1}, which has a Hamming weight of 26 def e(P,Q): assert(subgroup_check_G1(P)) assert(subgroup_check_G2(Q)) if ((P == 0) or (Q == 0)) return 1 # Here, 0 is the identity element of E R = Q f = 1 for i in range(len(bound)-2,-1,-1): # len(bound)=65 f = f * f * line(twist(R),twist(R),P) R = 2 * R if bound[i] == 1: f = f * line(untwist(R),untwist(Q),P) R = R + Q elif bound[i] == -1: f = f * line(untwist(R),untwist(-Q),P) R = R - Q Qp = Frobenius1(Q); # Q' f = f * line(twist(R), twist(Qp), P); R = R + Qp; Qpp = -Frobenius1(Qp); # Q'' f = f * line(twist(R), twist(Qpp), P); return final_exponentiation(f)

In what follows we explain how to compute each of the components of the previous pseudocode snippet.

Subgroup Checks

To summary BN254 For The Rest Of Us, we have:

  • PG1=E(Fp)[r] if and only if
    PE(Fp)
    . That is, if we write
    P=(x,y)
    then we simply need to ensure that the equation
    y2=x3+3
    holds over
    Fp
    .

    Costs:

    2s+1m+1a

  • A point

    Q=(x,y)E(Fp2) belongs to
    G2=E(Fp2)[r]
    if and only if
    ψ(Q)=[6x2]Q
    , where
    ψ:E(Fp2)E(Fp2)
    is defined as:
    ψ(x,y)=(γ1,2x¯,γ1,3y¯),

    where
    γ1,2=(9+u)p13,γ1,3=(9+u)p12
    .

    This was proven in Proposition 3 of 2022/352 and it is clearly much faster than the naive check

    [r]Q=O, since the constants
    γ1,2,γ1,3
    can be precomputed.

    TODO:
    A more efficient approach appears in 2022/348, which ensures that

    QE(Fp2) belongs to
    G2
    if and only if:
    [x+1]Q+ψ([x]Q)+ψ2([x]Q)=ψ3([2x]Q)

Costs:

2c~+2m~+cost of [6x2]P

Line Equations

Different Points

Given two points

Q1=(x1,y1) and
Q2=(x2,y2)
in the twisted curve
E(Fp2)
such that
x1y1
and
x2y2
and a point
P=(x,y)
in the curve
E(Fp)
, the evaluation at
P
of the line passing through
Ψ(Q1)
and
Ψ(Q2)
is given by:
Ψ(Q1),Ψ(Q2)(P)=w2(x2x1)y+w3(y1y2)x+w5(x1y2x2y1)

Costs:

3a~+2m~+4m

The Same Points

Given a point

Q=(x,y) in the twisted curve
E(Fp2)
and a point
P=(x,y)
in the curve
E(Fp)
, the evaluation at
P
of the line passing through
Ψ(Q)
is given by:
Ψ(Q),Ψ(Q)(P)=(3x32y2)(9+u)+w3(2yy)+w4(3xx2)

Costs:

1a~+2s~+2m~+10m

The Last two Lines

Now we explain some tricks for the computation of the lines:

[6x+2]Ψ(Q),πp(Ψ(Q))(P),[6x+2]Ψ(Q)+πp(Ψ(Q)),πp2(Ψ(Q))(P).

  • For the first line notice that if

    Q=(x,y)E(Fp2), then:
    πp(Ψ(Q))=((w2x)p,(w3y)p)=(w2(γ1,2xp),w3(γ1,3yp))=Ψ(γ1,2xp,γ1,3yp)=Ψ(γ1,2x¯,γ1,3y¯),

    where
    γ1,2=(9+u)p13,γ1,3=(9+u)p12
    are precomputed.

    The conjugate of an element in

    f=a+bu in
    Fp2
    is
    f¯=abu
    .

    Moreover, due to

    Ψ being an homomorphism we have that
    [n]Ψ(Q)=Ψ([n]Q)
    for all
    nFr
    . Consequently, we compute
    Ψ([6x+2]Q),Ψ(Q)(P)
    as in the previous section, with
    Q=(γ1,2x¯,γ1,3y¯)=(x1,y1)
    (it's a good exercise to check that
    Q
    belongs to
    E
    ).

    Costs:

    2c~+2m~Q+diff

    I don't include the cost of computing

    [6x+2]Q, since it is obtained in the Miller loop.

  • For the second line we similarly have:

    πp2(Ψ(Q))=πp(πp(Ψ(Q)))=πp(Ψ(x1,y1))=Ψ(γ1,2x1¯,γ1,3y1¯)
    Again, due to the homomorphic property of
    Ψ
    , we compute
    Ψ([6x+2]Q+Q),Ψ(Q)(P)
    as in the previous section, with
    Q=(γ1,2x1¯,γ1,3y1¯)
    (again, check that
    Q
    belongs to
    E
    ).

    Costs:

    1Eadd+2c~+2m~+2mQ+diff

Hence, in both cases, the Frobenius operator comes for free.

Final Exponentiation

In the final exponentiation, we raise an element

fFp12 to the power
(p121)/r
.

First, divide the exponent as follows:

p121r=(p61)(p2+1)easy partp4p2+1rhard part

The Easy Part

Let's start with the easy part of the easy part, when we first attempt to compute

fp61. But since,
fp6=f¯
, we have
fp61=f¯f1
, just one conjugate, one inversion and one multiplication in
Fp12
.

The conjugate of an element in

f=a+bw in
Fp12
is
f¯=abw
. Hence, if we write
f=g0+h0w+g1w2+h1w3+g2w4+h2w5
we have:
f¯=g0h0w+g1w2h1w3+g2w4h2w5.

To finish the easy part, we first compute

(f(p61))p2 which is the Frobenius operator applied to
fp61
and then multiply the result by
fp61
to finally obtain
m:=f(p61)(p2+1)Fp12
.

Importantly, after the computation of the easy part, the resulting field element becomes a member of the cyclotomic subgroup

Gϕ6(p2)=Gϕ12(p)={aFp12aϕ12(p)=ap4p2+1=ap6+1=1}, which means that inversion from now on can be computed by simply conjugation. Moreover, when an element
a
belongs to
Gϕ6(p2)
, one can compute
a2
using fewer multiplications and therefore the overall costs of performing exponentiations (see this section).

Costs:

1C+1I+1Mfp61+5m~+1M

The Hard Part

Following Section 5 of this 2008/490, we can write:

p4p2+1r=λ0+λ1p+λ2p2+λ3p3,
where:

  • λ0=218x30x236x3
    .
  • λ1=112x18x236x3
    .
  • λ2=1+6x2
    .
  • λ3=1
    .

So we just need to compute:

mλ0,mλ1p,mλ2p2,mλ3p3,
and then multiply them all.

In what follows, remember that x = 4965661367192848881, which is a number of

63 bits that can be expressed in the base
{1,0,1}
as follows:

1 - 2^4 + 2^9 + 2^11 + 2^16 + 2^19 + 2^21 + 2^22 + 2^25 + 2^27 + 2^30 + 2^34 + 2^36 + 2^37 + 2^39 + 2^41 + 2^44 + 2^47 + 2^48 + 2^51 - 2^53 + 2^56 + 2^58 + 2^62
[1,0,0,0,-1,0,0,0,0,1,0,1,0,0,0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,0,1,0,0,0,1,0,1,1,0,1,0,1,0,0,1,0,0,1,1,0,0,1,0,-1,0,0,1,0,1,0,0,0,1]
1000-1000010100001001011001010010001011010100100110010-1001010001

which has a Hamming weight of 24.

We prefer base

{1,0,1} over base
{0,1}
because x expressed in the latter is:

1 + 2^4 + 2^5 + 2^6 + 2^7 + 2^8 + 2^11 + 2^16 + 2^19 + 2^21 + 2^22 + 2^25 + 2^27 + 2^30 + 2^34 + 2^36 + 2^37 + 2^39 + 2^41 + 2^44 + 2^47 + 2^48 + 2^51 + 2^53 + 2^54 + 2^55 + 2^58 + 2^62
[1,0,0,0,1,1,1,1,1,0,0,1,0,0,0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,0,1,0,0,0,1,0,1,1,0,1,0,1,0,0,1,0,0,1,1,0,0,1,0,1,1,1,0,0,1,0,0,0,1]
100011111001000010010110010100100010110101001001100101110010001

which has a Hamming weigth of 28.

We proceed as follows:

  1. We start by computing

    mx,(mx)x,(mx2)x.

  2. Then, compute

    mp,mp2,mp3,(mx)p,(mx2)p,(mx3)p,(mx2)p2 (use the Frobenius operator).

  3. Next, we group the previous elements as follows:

    y0=mpmp2mp3,y1=m¯,y2=mx2p2,y3=mxp,y4=mxmx2p,y5=mx2,y6=mx3mx3p.
    (recall that at this point computing the conjugate is the same as computing the inverse)

  4. Finally, compute the product:

    y0y12y26y312y418y530y636

    Expand for correctness

    y0y12y26y312y418y530y636=(mpmp2mp3)(1m)2(mx2p2)6(1mxp)12(1mxmx2p)18(1mx2)30(1mx3mx3p)36=(1m)2(1mx)18(1mx2)30(1mx3)36mλ0mp(1mxp)12(1mx2p)18(1mx3p)36mλ1pmp2(mx2p2)6mλ2p2mp3mλ3p3=mλ0mλ1pmλ2p2mλ3p3=mλ0+λ1p+λ2p2+λ3p3=mp4p2+1r

    To compute this product as efficient as possible, we use the vectorial addition chain technique as explained in the last part of Section 5 of 2008/490:

    T0,1=y62y4y5,T1,1=T0,1y3y5,T0,2=T0,1y2,T1,2=T1,12T0,2,T1,3=T1,22,T1,4=T1,3y0,T0,3=T1,3y1,T0,4=T0,32T1,4.
    which requires only
    9
    multiplications and
    4
    squarings.

Costs:

(63·3)S+(x·3)MStep 1+5·(6c~+5m~)+2·(5m~)Step 2+4M+5CStep 3+9M+4SStep 4

Frobenius Operator

As we have shown, in the final exponentation we must compute the first, second and third Frobenius operator

πp,πp2,πp3 over an element in
Fp12
. Recalling that
πpi(x)=xpi
, a naive implementation would take
O(logp)=O(254)
Fp12
-operations, but we will do it much better.

Recall that we can write any element

fFp12 as:
f=g+hw=g0+h0w+g1w2+h1w3+g2w4+h2w5

where
g=g0+g1v+g2v2
and
h=h0+h1v+h2v2
with
g,hFp6
and
gi,hiFp2
.

Expand to see the proof

We will now show that raising an

Fp2-element over any power of
p
is almost cost free and therefore so is
f
.

  • Take
    b=b0+b1uFp2
    with
    b0,b1Fp
    . Then,
    bp2i=b0+b1(u(p2)i)=b0+b1u=b
    for Lagrange theorem. Similarly,
    bp2i1=bp2ibp2bp=bbbp=1bp=bp=b¯
    .
  • Notice
    wp=(w6)p16w=(9+u)p16w
    and therefore
    (wi)p=γ1,iwi
    , where
    γ1,i=(9+u)ip16
    .

Using all the previous observations, we have:

fp=(g0+h0w+g1w2+h1w3+g2w4+h2w5)p=g¯0+h¯0wp+g¯1w2p+h¯1w3p+g¯2w4p+h¯2w5=g¯0+h¯0γ1,1w+g¯1γ1,2w2+h¯1γ1,3w3+g¯2γ1,4w4+h¯2γ1,5w5,
which can be computed using a few multiplications and a few conjugations, assuming each
γ1,i
is pre-computed for
i[5]
.

Similarly, notice

wp2=(9+u)p216w=(γ1,1)1+pw=γ1,1γ¯1,1w and therefore
(wi)p2=γ2,iwi
, where
γ2,i=γ1,iγ¯1,i
.

Lastly, we can use the identity

p31=p2(p1)+(p+1)(p1) to show that
wp3=(9+u)p316w=(γ1,1)p2γ2,1w=γ1,1γ2,1w
and therefore
(wi)p3=γ3,iwi
, where
γ3,i=γ1,iγ2,i
.

Then, we have:

fp=g¯0+h¯0γ1,1w+g¯1γ1,2w2+h¯1γ1,3w3+g¯2γ1,4w4+h¯2γ1,5w5,fp2=g0+h0γ2,1w+g1γ2,2w2+h1γ2,3w3+g2γ2,4w4+h2γ2,5w5,fp3=g¯0+h¯0γ3,1w+g¯1γ3,2w2+h¯1γ3,3w3+g¯2γ3,4w4+h¯2γ3,5w5,
with
γ1,i=(9+u)ip16
,
γ2,i=(9+u)ip216=γ1,iγ¯1,i
and
γ3,i=(9+u)ip316=γ1,iγ2,i
. Hence, assuming the precomputation of
γ1,i,γ2,i,γ3,i
for
i[5]
, we can compute
fp,fp2
or
fp3
almost for free.

Costs:

6c~+5m~fp,5m~fp2,6c~+5m~fp3

Speeding up Computations in the Cyclotomic Subgroup

I follow 2010/542 closely for this section.

Recall that:

Fp2=Fp[u]/(u2+1),Fp6=Fp2[v]/(v3(9+u)),Fp12=Fp6[w]/(w2v).
and also:
Fp12=Fp2[w]/(w6(9+u))

For this optimitzation, we will further need the following

Fp12 representation:
Fp4=Fp2[w3]/((w3)2(9+u)),Fp12=Fp4[w]/(w3w3),

so that for
f=(g0+g1w3)+(g2+g3w3)w+(g4+g5w3)w2C
, we can equivalently write
f=g0+g2w+g4w2+g1w3+g3w4+g5w5B
.

This optimization is due to "compression" and "decompression" technique, which exploits the algebraic relationships arising from elements belonging to the cyclotomic subgroup

Gϕ6(p2).

Compression and Decompression

If

f=g0+g2w+g4w2+g1w3+g3w4+g5w5 then the compression function
C
is:
C(f)=[g2,g3,g4,g5].

and the decompression function
D
is:
D([g~2,g~3,g~4,g~5])=g~0+g~2w+g~4w2+g~1w3+g~3w4+g~5w5,

where:
{g~1=g~52(9+u)+3g~422g~34g~2,g~0=(2g~12+g~2g~53g~3g~4)(9+u)+1,if g~20g~1=2g~4g~5g~3,g~0=(2g~123g~3g~4)(9+u)+1,if g~2=0

Compressed Squaring

Now, on input

fGϕ6(p2) we will be able to compute its square on compressed form. That is,
C(f2)=[h2,h3,h4,h5]
, where
h=f2=h0+h2w+h4w2+h1w3+h3w4+h5w5
:
h2=2(g2+3(9+u)B4,5),h3=3(A4,5(10+u)B4,5)2g3,h4=3(A2,3(10+u)B2,3)2g4,h5=2(g5+3B2,3),Ai,j=(gi+gj)(gi+(9+u)gj),Bi,j=gigj.

Exponentiation in the Cyclotomic Subgroup

Given

fGϕ6(p2)Fp12 and
e1
, the idea is to compute
fe
using the squaring formula in compressed form.

Represent

e in binary as
e=e1e2e1e0
with
e1=1
and define
He={i:1i1 and ei=1}
. Then:
fe=i=01fei2i=fe0iHeD(C(f)2i).

Therefore, we can use the square-and-multiply algorithm to compute such product. Since we are going to be squaring in compressed form, we start at

C(f) rather than the standard
f
in such algorithm.

def exp_cyclo(f,e): Cf = comp(f); result = 1; for i in range(len(e)): if (e[i] == 1) { result = decomp(Cf) * result; } Cf = square_comp(Cf); } return result;

Appendix A: Field Isomorphisms

Recall that:

Fp2=Fp[u]/(u2+1),Fp6=Fp2[v]/(v3(9+u)),A=Fp12=Fp6[w]/(w2v).

We use two additional representations of

Fp12.

The first one is used to efficiently compute the Frobenius operator and is as follows:

B=Fp12=Fp2[w]/(w6(9+u)).

The second one is used to speed up the squares and exponentations in the hard part of the final exponentiation:

Fp4=Fp2[w3]/((w3)2(9+u)),C=Fp12=Fp4[w]/(w3w3).

Isomorphism between A and B

We start with

A=Fp6[w]/(w2v) and
B=Fp2[w]/(w6(9+u))
, where we notice that if we take
a=a1+a2wA
, we can use the identity
v=w2
to rewrite it as:
a=(a1,1+a1,2v+a1,3v2)+(a2,1+a2,2v+a2,3v2)w==a1,1+a2,1w+a1,2w2+a2,2w3+a1,3w4+a2,3w5B

This relationship induces the following field isomorphism

χAB:AB and its inverse
χAB1:BA
:
χAB(a1,1,a1,2,a1,3,a2,1,a2,2,a2,3)=(a1,1,a2,1,a1,2,a2,2,a1,3,a2,3),χAB1(b1,b2,b3,b4,b5,b6)=(b1,b3,b5,b2,b4,b6).

Exercise: Check that

χAB is a field isomorphism between
A
and
B
and that for all
aA
and
bB
it is true that
χAB1(χAB(a))=a
and
χAB(χAB1(b))=b
.

Isomorphism between B and C

We continue with

B=Fp2[w]/(w6(9+u)) and
C=Fp4[w]/(w3w3)
, for which we notice that
c=(c1+c2w3)+(c3+c4w3)w+(c5+c6w3)w2C
can be equivalently written as
c1+c3w+c5w2+c2w3+c4w4+c6w5B
.

This relationship induces the following field isomorphism

χBC:BC and its inverse
χBC1:CB
:
χBC(b1,b2,b3,b4,b5,b6)=(b1,b4,b2,b5,b3,b6),χBC1(c1,c2,c3,c4,c5,c6)=(c1,c3,c5,c2,c4,c6).

Exercise: Check that

χBC is a field isomorphism between
B
and
C
and that for all
bB
and
cC
it is true that
χBC1(χBC(b))=b
and
χBC(χBC1(c))=c
.

Appendix B: Finite Field Arithmetic

Fp2 Arithmetic

Addition

# INPUT: (a = a1 + a2·u), (b = b1 + b2·u) ∈ Fp2, where ai,bi ∈ Fp
# OUTPUT: (c1 + c2·u) = a + b ∈ Fp
c1 = a1 + b1
c2 = a2 + b2

Subtraction

# INPUT: (a = a1 + a2·u), (b = b1 + b2·u) ∈ Fp2, where ai,bi ∈ Fp
# OUTPUT: (c1 + c2·u) = a - b ∈ Fp
c1 = a1 - b1
c2 = a2 - b2

Multiplication

# INPUT: (a = a1 + a2·u), (b = b1 + b2·u) ∈ Fp2, where ai,bi ∈ Fp
# OUTPUT: (c1 + c2·u) = a·b ∈ Fp
c1 = a1·b1 - a2·b2
c2 = a1·b2 + a2·b1

Scalar Multiplication

# INPUT: b ∈ Fp, (a = a1 + a2·u) ∈ Fp2, where ai ∈ Fp
# OUTPUT: (c1 + c2·u) = a·b ∈ Fp
c1 = b·a1
c2 = b·a2

Square

# INPUT: (a = a1 + a2·u) ∈ Fp2, where ai ∈ Fp
# OUTPUT: (c1 + c2·u) = a² ∈ Fp
c1 = (a1 - a2)·(a1 + a2)
c2 = 2·a1·a2

Inverse

# INPUT: (a = a1 + a2·u) ∈ Fp2, where ai ∈ Fp
# OUTPUT: (c1 + c2·u) = a⁻¹ ∈ Fp
c1 = a1·(a1² + a2²)⁻¹
c2 = -a2·(a1² + a2²)⁻¹

Fp4 Arithmetic

Square

# INPUT: (a = a1 + a2·V) ∈ Fp4, where ai ∈ Fp2
# OUTPUT: (c1 + c2·V) = a² ∈ Fp4
c1 = a2²·(9 + u) + a1²
c2 = (a1 + a2)² - a1² - a2²

Fp6 Arithmetic

Addition

# INPUT: (a = a1 + a2·v + a3·v²),(b = b1 + b2·v + b3·v²) ∈ Fp6, where ai,bi ∈ Fp2
# OUTPUT: (c1 + c2·v + c3·v²) = a + b ∈ Fp6
c1 = a1 + b1
c2 = a2 + b2
c3 = a3 + b3

Subtraction

# INPUT: (a = a1 + a2·v + a3·v²),(b = b1 + b2·v + b3·v²) ∈ Fp6, where ai,bi ∈ Fp2
# OUTPUT: (c1 + c2·v + c3·v²) = a - b ∈ Fp6
c1 = a1 - b1
c2 = a2 - b2
c3 = a3 - b3

Multiplication

# INPUT: (a = a1 + a2·v + a3·v²),(b = b1 + b2·v + b3·v²) ∈ Fp6, where ai,bi ∈ Fp2
# OUTPUT: (c1 + c2·v + c3·v²) = a·b ∈ Fp6
c1 = [(a2+a3)·(b2+b3) - a2·b2 - a3·b3]·(9+u) + a1·b1
c2 = (a1+a2)·(b1+b2) - a1·b1 - a2·b2 + (9+u)·(a3·b3)
c3 = (a1+a3)·(b1+b3) - a1·b1 + a2·b2 - a3·b3

Scalar Multiplication

# INPUT: b ∈ Fp, (a = a1 + a2·v + a3·v²) ∈ Fp6, where ai ∈ Fp2
# OUTPUT: (c1 + c2·v + c3·v²) = a·b ∈ Fp6
c1 = b·a1
c1 = b·a2
c1 = b·a3

The following three algorithms help to compute the line equations. See the sparse multiplications algorithms of next section for a full understanding.

Sparse Multiplication A

# INPUT: (a = a1 + a2·v + a3·v²),b = b2·v ∈ Fp6, where ai,b2 ∈ Fp2
# OUTPUT: (c1 + c2·v + c3·v²) = a·b ∈ Fp6
c1 = b2·a3·(9+u)
c2 = b2·a1
c3 = b2·a2

Sparse Multiplication B

# INPUT: (a = a1 + a2·v + a3·v²),(b = b2·v + b3·v²) ∈ Fp6, where ai,bi ∈ Fp2
# OUTPUT: (c1 + c2·v + c3·v²) = a·b ∈ Fp6
c1 = [(a2 + a3)·(b2 + b3) - a2·b2 - a3·b3]·(9+u)
c2 = a1·b2 + a3·b3·(9+u)
c3 = (a1 + a3)·b3 - a3·b3 + a2·b2

Sparse Multiplication C

# INPUT: (a = a1 + a2·v + a3·v²),(b = b1 + b3·v²) ∈ Fp6, where ai,bi ∈ Fp2
# OUTPUT: (c1 + c2·v + c3·v²) = a·b ∈ Fp6
c1 = a1·b1 + a2·b3·(9+u)
c2 = a2·b1 + a3·b3·(9+u)
c3 = (a1 + a3)·(b1 + b3) - a1·b1 - a3·b3

Square

# INPUT: (a = a1 + a2·v + a3·v²) ∈ Fp6, where ai ∈ Fp2
# OUTPUT: (c1 + c2·v + c3·v²) = a² ∈ Fp6
c1 = 2·a2·a3·(9 + u) + a1²
c2 = a3²·(9 + u) + 2·a1·a2
c3 = 2·a1·a2 - a3² + (a1 - a2 + a3)² + 2·a2·a3 - a1²

Inverse

# INPUT: (a = a1 + a2·v + a3·v²) ∈ Fp6, where ai ∈ Fp2
# OUTPUT: (c1 + c2·v + c3·v²) = a⁻¹ ∈ Fp6
c1mid = a1² - (9 + u)·(a2·a3)
c2mid = (9 + u)·a3² - (a1·a2)
c3mid = a2² - (a1·a3)
c1 = (a1² - (9 + u)·(a2·a3))·(a1·c1mid + xi·(a3·c2mid + a2·c3mid))⁻¹
c2 = ((9 + u)·a3² - (a1·a2))·(a1·c1mid + xi·(a3·c2mid + a2·c3mid))⁻¹
c3 = (a2²-a1·a3)·(a1·c1mid + xi·(a3·c2mid + a2·c3mid))⁻¹

Fp12 Arithmetic

Addition

# INPUT: (a = a1 + a2·w),(b = b1 + b2·w) ∈ Fp12, where ai,bi ∈ Fp6
# OUTPUT: (c1 + c2·w) = a + b ∈ Fp12
c1 = a1 + b1
c2 = a2 + b2

Subtraction

# INPUT: (a = a1 + a2·w),(b = b1 + b2·w) ∈ Fp12, where ai,bi ∈ Fp6
# OUTPUT: (c1 + c2·w) = a - b ∈ Fp12
c1 = a1 - b1
c2 = a2 - b2

Multiplication

# INPUT: (a = a1 + a2·w),(b = b1 + b2·w) ∈ Fp12, where ai,bi ∈ Fp6
# OUTPUT: (c1 + c2·w) = a·b ∈ Fp12
c1 = a1·b1 + a2·b2·v
c2 = (a1+a2)·(b1+b2) - a1·b1 - a2·b2

The following two algorithms are used to compute the line equations avoiding all multiplications by zero that arise from the sparisity of such equations.

Sparse Multiplication A

# INPUT: (a = a1 + a2·w),(b = b1 + b2·w) ∈ Fp12, where ai ∈ Fp6, b1 = b12·v and b2 = b22·v + b23·v², with b12,b22,b23 ∈ Fp2
# OUTPUT: (c1 + c2·w) = a·b ∈ Fp12
c1 = a1·b1 + a2·b2·v
c2 = (a1+a2)·[(b12+b22)·v + b23·v²] - a1·b1 - a2·b2

Sparse Multiplication B

# INPUT: (a = a1 + a2·w),(b = b1 + b2·w) ∈ Fp12, where ai ∈ Fp6, b1 = b11 + b13·v² and b2 = b22·v, with b11,b13,b22 ∈ Fp2
# OUTPUT: (c1 + c2·w) = a·b ∈ Fp12
c1 = a1·b1 + a2·b2·v
c2 = (a1+a2)·(b11 + b22·v + b13·v²) - a1·b1 - a2·b2

Square

# INPUT: (a = a1 + a2·w) ∈ Fp12, where ai ∈ Fp6
# OUTPUT: (c1 + c2·w) = a² ∈ Fp12
c1 = (a1-a2)·(a1-a2·v) + a1·a2 + a1·a2·v
c2 = 2·a1·a2

Inverse

# INPUT: (a = a1 + a2·w) ∈ Fp12, where ai ∈ Fp6
# OUTPUT: (c1 + c2·w) = a⁻¹ ∈ Fp12
c1 = a1·(a1² - a2²·v)⁻¹
c2 = -a2·(a1² - a2²·v)⁻¹

First Frobenius Operator

# INPUT: (a = a1 + a2·w) = ((a11 + a12v + a13v²) + (a21 + a22v + a23v²)) ∈ Fp12, where ai ∈ Fp6 and aij ∈ Fp2
# OUTPUT: (c1 + c2·w) = a^p ∈ Fp12
c1 = a̅11     + a̅12·γ12·v + a̅13·γ14·v²
c2 = a̅21·γ11 + a̅22·γ13·v + a̅23·γ15·v²

Second Frobenius Operator

# INPUT: (a = a1 + a2·w) = ((a11 + a12v + a13v²) + (a21 + a22v + a23v²)) ∈ Fp12, where ai ∈ Fp6 and aij ∈ Fp2
# OUTPUT: (c1 + c2·w) = a^p² ∈ Fp12
c1 = a11     + a12·γ22·v + a13·γ24·v²
c2 = a21·γ21 + a22·γ23·v + a23·γ25·v²

Third Frobenius Operator

# INPUT: (a = a1 + a2·w) = ((a11 + a12v + a13v²) + (a21 + a22v + a23v²)) ∈ Fp12, where ai ∈ Fp6 and aij ∈ Fp2
# OUTPUT: (c1 + c2·w) = a^p³ ∈ Fp12
c1 = a̅11     + a̅12·γ32·v + a̅13·γ34·v²
c2 = a̅21·γ31 + a̅22·γ33·v + a̅23·γ35·v²

The following two algorithms are used for computing squarings and exponentiations over the cyclotomic subgroup

Gϕ6(p2)Fp12. Luckily for us, this is the case after performing the easy part of the final exponentiation.

Fast Squaring

# INPUT: (a = a1 + a2·w) ∈ GΦ6(p²), where ai ∈ Fp6
# OUTPUT: ((c11 + c12·v + c13·v²) + (c21 + c22·v + c23·v²)·w) = a² ∈ GΦ6(p²)
[t11,t22] = (a11 + a22·V)²
[t23,t12] = (a21 + a13·V)²
[t13,aux] = (a12 + a23·V)²
t21 = aux·(9+u)
c11 = -2·a11 + 3·t11
c12 = -2·a12 + 3·t23
c13 = -2·a13 + 3·t13
c21 = 2·a21 + 3·t21
c22 = 2·a22 + 3·t22
c23 = 2·a23 + 3·t12

Fast Exponentatiation

# INPUT: e, (a = a1 + a2·w) ∈ GΦ6(p²), where e ∈ [0,p¹²-2] ai ∈ Fp6
# OUTPUT: (c = c1 + c2·w) = a^e ∈ GΦ6(p²)
c = a
for i in range(L-2,-1,-1): # L is log2(e)
    c = c² # Use previous algorithm
    if e[i] == 1:
        c = c·a

return c

  1. if you don't know why, make sure to review Galois theory. Start by noticing that Galois group

    Gal(Fp12/Fp6) is a cyclic group of order
    2
    generated by the Frobenius endomorphism
    F(X)=Xp6
    . ↩︎

  2. I recently discoverd that this is just a particular case of the more generic non-adjacent form (NAF) of a number. ↩︎