Skip to content
← Writing

Computing the Optimal Ate Pairing over the BN254 Curve

The optimal Ate pairing on BN254, step by step: twist, line equations, the Miller loop's last two lines, final exponentiation and Frobenius shortcuts, as implemented in the Polygon zkEVM.

· 27 min read · Pairings, Elliptic Curves, Polygon zkEVM

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 EE of the form:

y2=x3+3y^2 = x^3 + 3

defined over a finite field Fp\mathbb{F}_p, where the prime pp is the following 254254-bit number:

p = 21888242871839275222246405745257275088696311157297823662689037894645226208583

The number of points #E(Fp)=p+1−t\#E(\mathbb{F}_p) = p+1-t happens to also be a prime rr of 254254 bits:

r = 21888242871839275222246405745257275088548364400416034343698204186575808495617

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

The point (1,2)∈E(Fp)(1,2) \in E(\mathbb{F}_p) is a point of order rr, i.e. a generator of the group of points of the curve. In fact, all points in E(Fp)\{O}E(\mathbb{F}_p)\backslash\{\mathcal{O}\} are generators, as Lagrange’s Theorem shows.

The embedding degree kk of this curve is 1212.

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

Tower of Extension Fields

To represent Fp12\mathbb{F}_{p^{12}} we use the following tower of extension fields: fields:

Fp2=Fp[u]/(u2−β),Fp6=Fp2[v]/(v3−ξ),Fp12=Fp6[w]/(w2−v),\begin{aligned} \mathbb{F}_{p^2} &= \mathbb{F}_p[u]/(u^2 - \beta), \\ \mathbb{F}_{p^6} &= \mathbb{F}_{p^2}[v]/(v^3 - \xi), \\ \mathbb{F}_{p^{12}} &= \mathbb{F}_{p^6}[w]/(w^2 - v), \end{aligned}

where β\beta is not a quadratic residue in Fp\mathbb{F}_p and ξ\xi is neither a quadratic residue or a cubic residue in Fp2\mathbb{F}_{p^2}.

In the particular case of the BN254, we have β=−1\beta = -1 and ξ=9+u\xi = 9+u and therefore:

Fp2=Fp[u]/(u2+1),Fp6=Fp2[v]/(v3−(9+u)),Fp12=Fp6[w]/(w2−v),\begin{aligned} \mathbb{F}_{p^2} &= \mathbb{F}_p[u]/(u^2 + 1), \\ \mathbb{F}_{p^6} &= \mathbb{F}_{p^2}[v]/(v^3 - (9+u)), \\ \mathbb{F}_{p^{12}} &= \mathbb{F}_{p^6}[w]/(w^2 - v), \end{aligned}

from which we can extract the identities u2=−1u^2 = -1, w2=vw^2 = v, v3=9+uv^3 = 9+u and consequently the idenity w6=9+uw^6 = 9+u.

Using the latter identity, we can also write:

Fp12=Fp2[w]/(w6−(9+u))\mathbb{F}_{p^{12}} = \mathbb{F}_{p^2}[w]/(w^6 - (9+u))

These two representations of Fp12\mathbb{F}_{p^{12}} will allow us to apply some optimizations:

  1. Writing any element f∈Fp12f \in \mathbb{F}_{p^{12}} as f=g+hwf = g + h w, with g,h∈Fp6g,h \in \mathbb{F}_{p^{6}} implies 1 fp6=fˉ=g−hwf^{p^6} = \bar{f} = g - h w. Thus, computing the p6p^6-th power of any element in Fp12\mathbb{F}_{p^{12}} is computationally “free”.
  2. Now, if we write g=g0+g1v+g2v2g = g_0 + g_1v + g_2v^2 and h=h0+h1v+h2v2h = h_0 + h_1v + h_2v^2 where g0g_0,g1g_1,g2g_2,h0h_0,h1h_1,h2∈Fp2h_2 \in \mathbb{F}_{p^2} we have: f=g0+h0w+g1w2+h1w3+g2w4+h2w5,f = g_0 + h_0w + g_1w^2 + h_1w^3 + g_2w^4 + h_2w^5, which will allow us to compute fp,fp2f^p,f^{p^2} and fp3f^{p^3} (needed for the final exponentiation) very efficiently.

Twist

BN curves admits a sextic twist E′E' defined over Fp2\mathbb{F}_{p^2} of the form:

y′2=x′3+3/(9+u)y'^2 = x'^3 + 3/(9+u)

or more specifically:

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

This means that pairing computations can be restricted to points P∈E(Fp)P \in E(\mathbb{F}_p) and Q∈E′(Fp2)Q \in E'(\mathbb{F}_{p^2}) and avoid computations over Fp12\mathbb{F}_{p^{12}} entirely.

The number of points of E′E' is:

#E′(Fp2)=(p+1−t)(p−1+t)=r⋅c\#E'(\mathbb{F}_{p^2}) = (p+1-t)(p-1+t) = r \cdot c

which concrete to:

#E' = 479095176016622842441988045216678740799252316531100822436447802254070093686356349204969212544220033486413271283566945264650845755880805213916963058350733
c = 21888242871839275222246405745257275088844257914179612981679871602714643921549

The mapping

Ψ(x′,y′)=(w2x′,w3y′)\Psi(x',y') = (w^2x',w^3y')

defines a group homomorphism from E′(Fp2)E'(\mathbb{F}_{p^2}) to E(Fp12)E(\mathbb{F}_{p^{12}}) and we are going to use it to send points from the twisted curve E′E' to the curve EE (but not the other way around).

The Pairing

The optimal Ate pairing e:G1×G2→GTe : \mathbb{G}_1 \times \mathbb{G}_2 \to \mathbb{G}_T 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))p12−1re(P,Q) = \left( f_{6x+2,Q}(P) \cdot \ell_{[6x+2]\Psi(Q),\pi_p(\Psi(Q))}(P) \cdot \ell_{[6x+2]\Psi(Q)+\pi_p(\Psi(Q)),-\pi_p^2(\Psi(Q))}(P) \right)^{\frac{p^{12}-1}{r}}

where:

  • G1=E(Fp)[r]\mathbb{G}_1 = E(\mathbb{F}_p)[r] is the only subgroup of the rr-torsion of EE over Fp\mathbb{F}_p.

  • G2=E′(Fp2)[r]\mathbb{G}_2 = E'(\mathbb{F}_{p^2})[r] is the only subgroup of the rr-torsion of E′E' over Fp2\mathbb{F}_{p^2}.

  • GT=μr\mathbb{G}_T = \mu_r is the set of rr-th roots of unity over Fp12∗\mathbb{F}^*_{p^{12}}.

  • x = 4965661367192848881 and therefore 6x+2 = 29793968203157093288, which is a number of 6565 bits that can be expressed in the base {−1,0,1}\{-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}\{-1,0,1\} over base {0,1}\{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}\{0,1\} to base {−1,0,1}\{-1,0,1\} by using the identity2 2n+1−2m=2n+2n−1+...+2m,2^{n+1} - 2^m = 2^n + 2^{n-1} + ... + 2^m, that holds for any n,m∈Nn,m \in \mathbb{N} such that n≥mn \geq m. In words, it is unpacking groups of 11‘s of any size to a single 11 and −1-1 in the binary decomposition.

  • fi,Qf_{i,Q}, for any i∈Ni \in \mathbb{N} and Q∈G2Q \in \mathbb{G}_2, is a Fp12\mathbb{F}_{p^{12}} rational function on EE that is computed using Miller’s “double-and-add”-style algorithm: fi+j,Q=fi,Q⋅fj,Q⋅ℓ[i]Ψ(Q),[j]Ψ(Q)v[i+j]Ψ(Q),fi+1,Q=fm,Q⋅ℓ[i]Ψ(Q),Ψ(Q)v[i+1]Ψ(Q),\begin{aligned} f_{i+j,Q} = f_{i,Q} \cdot f_{j,Q} \cdot \frac{\ell_{[i]\Psi(Q),[j]\Psi(Q)}}{v_{[i+j]\Psi(Q)}}, \\ f_{i+1,Q} = f_{m,Q} \cdot \frac{\ell_{[i]\Psi(Q),\Psi(Q)}}{v_{[i+1]\Psi(Q)}}, \\ \end{aligned} starting with f1,Q=1f_{1,Q} = 1.
  • ℓP1,P2\ell_{P_1,P_2} denotes the equation of a line passing through P1P_1 and P2P_2 while vPv_{P} denotes the equation of a vertical line passing through PP. However, due to the final exponentiation, the value of e(P,Q)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)\pi_p^i(x,y) = (x^{p^i},y^{p^i}) 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(twist(R),twist(Q),P)
            R = R + Q
        elif bound[i] == -1:
            f = f * line(twist(R),twist(-Q),P)
            R = R - Q
            
    Qp = FrobEndo(Q); # Q'
    f = f * line(twist(R), twist(Qp), P);
    R = R + Qp;
    Qpp = -FrobEndo(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:

  • P∈G1=E(Fp)[r]P \in \mathbb{G}_1 = E(\mathbb{F}_p)[r] if and only if P∈E(Fp)P \in E(\mathbb{F}_p). That is, if we write P=(x,y)P = (x,y) then we simply need to ensure that the equation y2=x3+3y^2 = x^3 + 3 holds over Fp\mathbb{F}_p.

    Costs: 2s+1m+1a2s + 1m + 1a

  • A point Q=(x′,y′)∈E′(Fp2)Q = (x',y') \in E'(\mathbb{F}_{p^2}) belongs to G2=E′(Fp2)[r]\mathbb{G}_2 = E'(\mathbb{F}_{p^2})[r] if and only if:

    [x+1]Q+ψ([x]Q)+ψ2([x]Q)=ψ3([2x]Q)[x+1]Q + \psi([x]Q) + \psi^2([x]Q) = \psi^3([2x]Q)

    where ψ:E′(Fp2)→E′(Fp2)\psi : E'(\mathbb{F}_{p^2}) \to E'(\mathbb{F}_{p^2}) is defined as ψ(x′,y′)=(γ1,2xˉ′,γ1,3yˉ′)\psi(x',y') = ({\gamma_{1,2}}\bar{x}',\gamma_{1,3}\bar{y}'), where γ1,2=(9+u)p−13,γ1,3=(9+u)p−12\gamma_{1,2} = (9+u)^{\frac{p-1}{3}},\gamma_{1,3} = (9+u)^{\frac{p-1}{2}}.

    This was proven in 2022/348 and it is clearly much faster than the naive check [r]Q=O[r]Q = \mathcal{O}, since x≪rx \ll r.

Costs: 2c~+2m~+cost of [6x2]P2\tilde{c} + 2\tilde{m} + \text{cost of } [6x^2]P

Line Equations

Different Points

Given two points Q1=(x1′,y1′)Q_1 =(x_1',y_1') and Q2=(x2′,y2′)Q_2=(x_2',y_2') in the twisted curve E′(Fp2)E'(\mathbb{F}_{p^2}) such that x1′≠y1′x_1' \neq y_1' and x2′≠y2′x_2' \neq y_2' and a point P=(x,y)P=(x,y) in the curve E(Fp)E(\mathbb{F}_p), the evaluation at PP of the line passing through Ψ(Q1)\Psi(Q_1) and Ψ(Q2)\Psi(Q_2) is given by:

ℓΨ(Q1),Ψ(Q2)(P)=w2(x2′−x1′)y+w3(y1′−y2′)x+w5(x1′y2′−x2′y1′)\ell_{\Psi(Q_1),\Psi(Q_2)}(P) = w^2(x_2'-x_1')y + w^3(y_1'-y_2')x + w^5(x_1'y_2'-x_2'y_1')

Costs: 3a~+2m~+4m3\tilde{a} + 2\tilde{m} + 4m

The Same Points

Given a point Q=(x′,y′)Q =(x',y') in the twisted curve E′(Fp2)E'(\mathbb{F}_{p^2}) and a point P=(x,y)P=(x,y) in the curve E(Fp)E(\mathbb{F}_p), the evaluation at PP of the line passing through Ψ(Q)\Psi(Q) is given by:

ℓΨ(Q),Ψ(Q)(P)=(3x′3−2y′2)(9+u)+w3(2yy′)+w4(−3xx′2)\ell_{\Psi(Q),\Psi(Q)}(P) = (3x'^3-2y'^2)(9+u) + w^3(2yy') + w^4(-3xx'^2)

Costs: 1a~+2s~+2m~+10m1\tilde{a} + 2\tilde{s} + 2\tilde{m} + 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).\ell_{[6x+2]\Psi(Q),\pi_p(\Psi(Q))}(P), \quad \ell_{[6x+2]\Psi(Q)+\pi_p(\Psi(Q)),-\pi_p^2(\Psi(Q))}(P).
  • For the first line notice that if Q=(x′,y′)∈E′(Fp2)Q = (x',y') \in E'(\mathbb{F}_{p^2}), then: πp(Ψ(Q))=((w2x′)p,(w3y′)p)=(w2(γ1,2x′p),w3(γ1,3y′p))=Ψ(γ1,2x′p,γ1,3y′p)=Ψ(γ1,2xˉ′,γ1,3yˉ′),\pi_p(\Psi(Q)) = ((w^2x')^p,(w^3y')^p) = (w^2(\gamma_{1,2}x'^p),w^3(\gamma_{1,3}y'^p)) = \Psi(\gamma_{1,2}x'^p,\gamma_{1,3}y'^p) = \Psi({\gamma_{1,2}}\bar{x}',\gamma_{1,3}\bar{y}'), where γ1,2=(9+u)p−13,γ1,3=(9+u)p−12\gamma_{1,2} = (9+u)^{\frac{p-1}{3}},\gamma_{1,3} = (9+u)^{\frac{p-1}{2}} are precomputed.

The conjugate of an element in f=a+buf = a + bu in Fp2\mathbb{F}_{p^2} is fˉ=a−bu\bar{f} = a -bu.

Moreover, due to $\Psi$ being an homomorphism we have that $[n]\Psi(Q) = \Psi([n]Q)$ for all $n \in \mathbb{F}_r$. Consequently, we compute $\ell_{\Psi([6x+2]Q),\Psi(Q')}(P)$ as in the previous section, with $Q' = ({\gamma_{1,2}}\bar{x}',\gamma_{1,3}\bar{y}') = (x_1,y_1)$ (it's a good exercise to check that $Q'$ belongs to $E$).

**Costs:** $\underbrace{2\tilde{c} + 2\tilde{m}}_{Q'} + \ell_{\text{diff}}$

I don’t include the cost of computing [6x+2]Q[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ˉ)-\pi_p^2(\Psi(Q)) = -\pi_p(\pi_p(\Psi(Q))) = -\pi_p(\Psi(x_1,y_1)) = \Psi(\gamma_{1,2}\bar{x_1},-\gamma_{1,3}\bar{y_1})

    Again, due to the homomorphic property of Ψ\Psi, we compute ℓΨ([6x+2]Q+Q′),Ψ(Q′′)(P)\ell_{\Psi([6x+2]Q + Q'),\Psi(Q'')}(P) as in the previous section, with Q′′=(γ1,2x1ˉ,−γ1,3y1ˉ)Q'' = (\gamma_{1,2}\bar{x_1},-\gamma_{1,3}\bar{y_1}) (again, check that Q′′Q'' belongs to EE).

    Costs: 1Eadd′+2c~+2m~+2m⏟Q′′+ℓdiff1E'_{\text{add}} + \underbrace{2\tilde{c} + 2\tilde{m} + 2m}_{Q''} + \ell_{\text{diff}}

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

Final Exponentiation

In the final exponentiation, we raise an element f∈Fp12f \in \mathbb{F}_{p^{12}} to the power (p12−1)/r(p^{12}-1)/r.

First, divide the exponent as follows:

p12−1r=(p6−1)(p2+1)⏟easy partp4−p2+1r⏟hard part\frac{p^{12}-1}{r} = \underbrace{(p^6-1)(p^2+1)}_{\text{easy part}}\underbrace{\frac{p^4-p^2+1}{r}}_{\text{hard part}}

The Easy Part

Let’s start with the easy part of the easy part, when we first attempt to compute fp6−1f^{p^6 - 1}. But since, fp6=fˉf^{p^6} = \bar{f}, we have fp6−1=fˉ⋅f−1f^{p^6 - 1} = \bar{f} \cdot f^{-1}, just one conjugate, one inversion and one multiplication in Fp12\mathbb{F}_{p^{12}}.

The conjugate of an element in f=a+bwf = a + bw in Fp12\mathbb{F}_{p^{12}} is fˉ=a−bw\bar{f} = a -bw. Hence, if we write f=g0+h0w+g1w2+h1w3+g2w4+h2w5f = g_0 + h_0w + g_1w^2 + h_1w^3 + g_2w^4 + h_2w^5 we have:

fˉ=g0−h0w+g1w2−h1w3+g2w4−h2w5.\bar{f} = g_0 - h_0w + g_1w^2 - h_1w^3 + g_2w^4 - h_2w^5.

To finish the easy part, we first compute (f(p6−1))p2(f^{(p^6 - 1)})^{p^2} which is the Frobenius operator applied to fp6−1f^{p^6-1} and then multiply the result by fp6−1f^{p^6 - 1} to finally obtain m:=f(p6−1)(p2+1)∈Fp12m := f^{(p^6-1)(p^2+1)} \in \mathbb{F}_{p^{12}}.

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)={a∈Fp12∣aϕ12(p)=ap4−p2+1=ap6+1=1}\mathbb{G}_{\phi_{6}(p^2)} = \mathbb{G}_{\phi_{12}(p)} = \{a \in \mathbb{F}_{p^{12}} \mid a^{\phi_{12}(p)} = a^{p^4-p^2+1} = a^{p^6+1} = 1\}, which means that inversion from now on can be computed by simply conjugation. Moreover, when an element aa belongs to Gϕ6(p2)\mathbb{G}_{\phi_{6}(p^2)}, one can compute a2a^2 using fewer multiplications and therefore the overall costs of performing exponentiations (see this section).

Costs: 1C+1I+1M⏟fp6−1+5m~+1M\underbrace{1C + 1I + 1M}_{f^{p^6-1}} + 5\tilde{m} + 1M

The Hard Part

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

p4−p2+1r=λ0+λ1p+λ2p2+λ3p3,\frac{p^4-p^2+1}{r} = \lambda_0 + \lambda_1p + \lambda_2p^2 + \lambda_3p^3,

where:

  • λ0=−2−18x−30x2−36x3\lambda_0 = -2 - 18x - 30x^2 - 36x^3.
  • λ1=1−12x−18x2−36x3\lambda_1 = 1 - 12x - 18x^2 - 36x^3.
  • λ2=1+6x2\lambda_2 = 1 + 6x^2.
  • λ3=1\lambda_3 = 1.

So we just need to compute:

mλ0,mλ1p,mλ2p2,mλ3p3,m^{\lambda_0}, m^{\lambda_1p}, m^{\lambda_2p^2}, m^{\lambda_3p^3},

and then multiply them all.

In what follows, remember that x = 4965661367192848881, which is a number of 6363 bits that can be expressed in the base {−1,0,1}\{-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}\{-1,0,1\} over base {0,1}\{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)xm^x, (m^x)^x, (m^{x^2})^x.

  2. Then, compute mp,mp2,mp3,(mx)p,(mx2)p,(mx3)p,(mx2)p2m^p, m^{p^2}, m^{p^3}, (m^{x})^p, (m^{x^2})^p, (m^{x^3})^p, (m^{x^2})^{p^2} (use the Frobenius operator).

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

    y0=mp⋅mp2⋅mp3,y1=mˉ,y2=mx2p2,y3=mxp‾,y4=mx⋅mx2p‾,y5=mx2‾,y6=mx3⋅mx3p‾.\begin{aligned} y_0 &= m^p \cdot m^{p^2} \cdot m^{p^3}, \\ y_1 &= \bar{m}, \\ y_2 &= m^{x^2p^2}, \\ y_3 &= \overline{m^{xp}}, \\ y_4 &= \overline{m^{x} \cdot m^{x^2p}}, \\ y_5 &= \overline{m^{x^2}}, \\ y_6 &= \overline{m^{x^3} \cdot m^{x^3p}}. \end{aligned}

    (recall that at this point computing the conjugate is the same as computing the inverse)

  4. Finally, compute the product:

    y0⋅y12⋅y26⋅y312⋅y418⋅y530⋅y636y_0 \cdot y_1^2 \cdot y_2^6 \cdot y_3^{12} \cdot y_4^{18} \cdot y_5^{30} \cdot y_6^{36}
Expand for correctness
$$
\begin{aligned}
&y_0 \cdot y_1^2 \cdot y_2^6 \cdot y_3^{12} \cdot y_4^{18} \cdot y_5^{30} \cdot y_6^{36} = \\ 
&(m^p \cdot m^{p^2} \cdot m^{p^3}) \cdot \left(\frac{1}{m}\right)^2 \cdot (m^{x^2p^2})^6 \cdot \left(\frac{1}{m^{xp}}\right)^{12} \cdot \left(\frac{1}{m^{x}m^{x^2p}}\right)^{18} \cdot \left(\frac{1}{m^{x^2}}\right)^{30} \cdot \left(\frac{1}{m^{x^3}m^{x^3p}}\right)^{36} \\
&= \underbrace{\left(\frac{1}{m}\right)^2 \cdot \left(\frac{1}{m^{x}}\right)^{18}\left(\frac{1}{m^{x^2}}\right)^{30}\left(\frac{1}{m^{x^3}}\right)^{36}}_{m^{\lambda_0}}\cdot \underbrace{m^p \cdot \left(\frac{1}{m^{xp}}\right)^{12} \cdot \left(\frac{1}{m^{x^2p}}\right)^{18} \cdot \left(\frac{1}{m^{x^3p}}\right)^{36}}_{m^{\lambda_1p}} \cdot \underbrace{m^{p^2} \cdot (m^{x^2p^2})^6}_{m^{\lambda_2p^2}} \cdot \underbrace{m^{p^3}}_{m^{\lambda_3p^3}} \\
&= m^{\lambda_0} \cdot m^{\lambda_1p} \cdot m^{\lambda_2p^2} \cdot m^{\lambda_3p^3} = m^{\lambda_0 + \lambda_1p + \lambda_2p^2 + \lambda_3p^3} = m^{\frac{p^4-p^2+1}{r}}
\end{aligned}
$$
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](https://eprint.iacr.org/2008/490.pdf):
T0,1=y62⋅y4⋅y5,T1,1=T0,1⋅y3⋅y5,T0,2=T0,1⋅y2,T1,2=T1,12⋅T0,2,T1,3=T1,22,T1,4=T1,3⋅y0,T0,3=T1,3⋅y1,T0,4=T0,32⋅T1,4.\begin{aligned} T_{0,1} &= y_6^2\cdot y_4 \cdot y_5, \\ T_{1,1} &= T_{0,1} \cdot y_3 \cdot y_5, \\ T_{0,2} &= T_{0,1} \cdot y_2, \\ T_{1,2} &= T_{1,1}^2 \cdot T_{0,2}, \\ T_{1,3} &= T_{1,2}^2, \\ T_{1,4} &= T_{1,3} \cdot y_0, \\ T_{0,3} &= T_{1,3} \cdot y_1, \\ T_{0,4} &= T_{0,3}^2 \cdot T_{1,4}. \end{aligned}

which requires only 99 multiplications and 44 squarings.

Costs: (63⋅3)S+(x⋅3)M⏟Step 1+5⋅(6c~+5m~)+2⋅(5m~)⏟Step 2+4M+5C⏟Step 3+9M+4S⏟Step 4\underbrace{(63·3)S + (x·3)M}_{\text{Step 1}} + \underbrace{5 · (6\tilde{c} + 5\tilde{m}) + 2·(5\tilde{m})}_{\text{Step 2}} + \underbrace{4M + 5C}_{\text{Step 3}} + \underbrace{9M + 4S}_{\text{Step 4}}

Frobenius Operator

As we have shown, in the final exponentation we must compute the first, second and third Frobenius operator πp,πp2,πp3\pi_p,\pi_p^2,\pi_p^3 over an element in Fp12\mathbb{F}_{p^{12}}. Recalling that πpi(x)=xpi\pi_p^i(x) = x^{p^i}, a naive implementation would take O(log⁡p)=O(254)O(\log p) = O(254) Fp12\mathbb{F}_{p^{12}}-operations, but we will do it much better.

Recall that we can write any element f∈Fp12f \in \mathbb{F}_{p^{12}} as:

f=g+hw=g0+h0w+g1w2+h1w3+g2w4+h2w5f = g + h w = g_0 + h_0w + g_1w^2 + h_1w^3 + g_2w^4 + h_2w^5

where g=g0+g1v+g2v2g = g_0 + g_1v + g_2v^2 and h=h0+h1v+h2v2h = h_0 + h_1v + h_2v^2 with g,h∈Fp6g,h \in \mathbb{F}_{p^{6}} and gi,hi∈Fp2g_i,h_i \in \mathbb{F}_{p^2}.

Expand to see the proof

We will now show that raising an Fp2\mathbb{F}_{p^2}-element over any power of pp is almost cost free and therefore so is ff.

  • Take b=b0+b1u∈Fp2b = b_0 + b_1u \in \mathbb{F}_{p^2} with b0,b1∈Fpb_0,b_1 \in \mathbb{F}_{p}. Then, bp2i=b0+b1(u(p2)i)=b0+b1u=bb^{p^{2i}} = b_0 + b_1(u^{(p^{2})^i}) = b_0 + b_1u = b for Lagrange theorem. Similarly, bp2i−1=bp2ibp2⋅bp=bb⋅b−p=1b−p=bp=bˉb^{p^{2i-1}} = \frac{b^{p^{2i}}}{b^{p^2} \cdot b^p} = \frac{b}{b \cdot b^{-p}} = \frac{1}{b^{-p}} = b^p = \bar{b}.
  • Notice wp=(w6)p−16w=(9+u)p−16ww^p = (w^6)^{\frac{p-1}{6}}w = (9+u)^{\frac{p-1}{6}}w and therefore (wi)p=γ1,iwi(w^{i})^p = \gamma_{1,i}w^i, where γ1,i=(9+u)ip−16\gamma_{1,i} = (9+u)^{i\frac{p-1}{6}}.

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,\begin{aligned} f^p &= (g_0 + h_0w + g_1w^2 + h_1w^3 + g_2w^4 + h_2w^5)^p \\ &= \bar{g}_0 + \bar{h}_0w^p + \bar{g}_1w^{2p} + \bar{h}_1w^{3p} + \bar{g}_2w^{4p} + \bar{h}_2w^{5} \\ &= \bar{g}_0 + \bar{h}_0\gamma_{1,1}w + \bar{g}_1\gamma_{1,2}w^2 + \bar{h}_1\gamma_{1,3}w^3 + \bar{g}_2\gamma_{1,4}w^4 + \bar{h}_2\gamma_{1,5}w^5, \end{aligned}

which can be computed using a few multiplications and a few conjugations, assuming each γ1,i\gamma_{1,i} is pre-computed for i∈[5]i \in [5].

Similarly, notice wp2=(9+u)p2−16w=(γ1,1)1+pw=γ1,1⋅γˉ1,1ww^{p^2} = (9+u)^{\frac{p^2-1}{6}}w = (\gamma_{1,1})^{1+p}w = \gamma_{1,1} \cdot \bar{\gamma}_{1,1}w and therefore (wi)p2=γ2,iwi(w^{i})^{p^2} = \gamma_{2,i}w^i, where γ2,i=γ1,i⋅γˉ1,i\gamma_{2,i} = \gamma_{1,i} \cdot \bar{\gamma}_{1,i}.

Lastly, we can use the identity p3−1=p2(p−1)+(p+1)(p−1)p^3-1 = p^2(p-1) + (p+1)(p-1) to show that wp3=(9+u)p3−16w=(γ1,1)p2⋅γ2,1w=γ1,1⋅γ2,1ww^{p^3} = (9+u)^{\frac{p^3-1}{6}}w = (\gamma_{1,1})^{p^2} \cdot \gamma_{2,1}w = \gamma_{1,1} \cdot \gamma_{2,1}w and therefore (wi)p3=γ3,iwi(w^{i})^{p^3} = \gamma_{3,i}w^i, where γ3,i=γ1,i⋅γ2,i\gamma_{3,i} = \gamma_{1,i} \cdot \gamma_{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,\begin{aligned} f^{p} = \bar{g}_0 + \bar{h}_0\gamma_{1,1}w + \bar{g}_1\gamma_{1,2}w^2 + \bar{h}_1\gamma_{1,3}w^3 + \bar{g}_2\gamma_{1,4}w^4 + \bar{h}_2\gamma_{1,5}w^5, \\ f^{p^2} = g_0 + h_0\gamma_{2,1}w + g_1\gamma_{2,2}w^2 + h_1\gamma_{2,3}w^3 + g_2\gamma_{2,4}w^4 + h_2\gamma_{2,5}w^5, \\ f^{p^3} = \bar{g}_0 + \bar{h}_0\gamma_{3,1}w + \bar{g}_1\gamma_{3,2}w^2 + \bar{h}_1\gamma_{3,3}w^3 + \bar{g}_2\gamma_{3,4}w^4 + \bar{h}_2\gamma_{3,5}w^5, \end{aligned}

with γ1,i=(9+u)ip−16\gamma_{1,i} = (9+u)^{i\frac{p-1}{6}}, γ2,i=(9+u)ip2−16=γ1,i⋅γˉ1,i\gamma_{2,i} = (9+u)^{i\frac{p^2-1}{6}} = \gamma_{1,i} \cdot \bar{\gamma}_{1,i} and γ3,i=(9+u)ip3−16=γ1,i⋅γ2,i\gamma_{3,i} = (9+u)^{i\frac{p^3-1}{6}} = \gamma_{1,i} \cdot \gamma_{2,i}. Hence, assuming the precomputation of γ1,i,γ2,i,γ3,i\gamma_{1,i},\gamma_{2,i},\gamma_{3,i} for i∈[5]i \in [5], we can compute fp,fp2f^p,f^{p^2} or fp3f^{p^3} almost for free.

Costs: 6c~+5m~⏟fp,5m~⏟fp2,6c~+5m~⏟fp3\underbrace{6\tilde{c} + 5\tilde{m}}_{f^p}, \quad \underbrace{5\tilde{m}}_{f^{p^2}}, \quad \underbrace{6\tilde{c} + 5\tilde{m}}_{f^{p^3}}

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]/(w2−v).\begin{aligned} \mathbb{F}_{p^2} &= \mathbb{F}_p[u]/(u^2 + 1), \\ \mathbb{F}_{p^6} &= \mathbb{F}_{p^2}[v]/(v^3 - (9+u)), \\ \mathbb{F}_{p^{12}} &= \mathbb{F}_{p^6}[w]/(w^2 - v). \end{aligned}

and also:

Fp12=Fp2[w]/(w6−(9+u))\mathbb{F}_{p^{12}} = \mathbb{F}_{p^2}[w]/(w^6 - (9+u))

For this optimitzation, we will further need the following Fp12\mathbb{F}_{p^{12}} representation:

Fp4=Fp2[w3]/((w3)2−(9+u)),Fp12=Fp4[w]/(w3−w3),\begin{aligned} \mathbb{F}_{p^{4}} &= \mathbb{F}_{p^2}[w^3]/((w^3)^2 - (9+u)), \\ \mathbb{F}_{p^{12}} &= \mathbb{F}_{p^4}[w]/(w^3 - w^3), \end{aligned}

so that for f=(g0+g1w3)+(g2+g3w3)w+(g4+g5w3)w2∈Cf = (g_0 + g_1 w^3) + (g_2 + g_3 w^3) w + (g_4 + g_5 w^3) w^2 \in C, we can equivalently write f=g0+g2w+g4w2+g1w3+g3w4+g5w5∈Bf = g_0 + g_2 w + g_4 w^2 + g_1 w^3 + g_3 w^4 + g_5 w^5 \in B.

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)\mathbb{G}_{\phi_{6}(p^2)}.

Compression and Decompression

If f=g0+g2w+g4w2+g1w3+g3w4+g5w5f = g_0 + g_2w + g_4w^2 + g_1w^3 + g_3w^4 + g_5w^5 then the compression function C\mathcal{C} is:

C(f)=[g2,g3,g4,g5].\mathcal{C}(f) = [g_2,g_3,g_4,g_5].

and the decompression function D\mathcal{D} is:

D([g~2,g~3,g~4,g~5])=g~0+g~2w+g~4w2+g~1w3+g~3w4+g~5w5,\mathcal{D}([\tilde{g}_2,\tilde{g}_3,\tilde{g}_4,\tilde{g}_5]) = \tilde{g}_0 + \tilde{g}_2w + \tilde{g}_4w^2 + \tilde{g}_1w^3 + \tilde{g}_3w^4 + \tilde{g}_5w^5,

where:

{g~1=g~52(9+u)+3g~42−2g~34g~2,g~0=(2g~12+g~2g~5−3g~3g~4)(9+u)+1,if g~2≠0g~1=2g~4g~5g~3,g~0=(2g~12−3g~3g~4)(9+u)+1,if g~2=0\begin{cases} \tilde{g}_1 = \frac{\tilde{g}_5^2(9+u) + 3\tilde{g}_4^2 - 2\tilde{g}_3}{4\tilde{g}_2}, \quad \tilde{g}_0 = (2\tilde{g}_1^2 + \tilde{g}_2\tilde{g}_5 - 3\tilde{g}_3\tilde{g}_4)(9+u) + 1, & \text{if } \tilde{g}_2 \neq 0 \\ \tilde{g}_1 = \frac{2\tilde{g}_4\tilde{g}_5}{\tilde{g}_3}, \qquad\qquad\quad \tilde{g}_0 = (2\tilde{g}_1^2 - 3\tilde{g}_3\tilde{g}_4)(9+u) + 1, & \text{if } \tilde{g}_2 = 0 \end{cases}

Compressed Squaring

Now, on input f∈Gϕ6(p2)f \in \mathbb{G}_{\phi_{6}(p^2)} we will be able to compute its square on compressed form. That is, C(f2)=[h2,h3,h4,h5]\mathcal{C}(f^2) = [h_2, h_3, h_4, h_5], where h=f2=h0+h2w+h4w2+h1w3+h3w4+h5w5h = f^2 = h_0 + h_2w + h_4w^2 + h_1w^3 + h_3w^4 + h_5w^5:

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=gi⋅gj.\begin{aligned} h_2 &= 2(g_2 + 3(9+u)B_{4,5}), \\ h_3 &= 3(A_{4,5} - (10 + u)B_{4,5}) - 2g_3, \\ h_4 &= 3(A_{2,3} - (10+u)B_{2,3}) - 2g_4, \\ h_5 &= 2(g_5 + 3B_{2,3}), \\ A_{i,j} &= (g_i + g_j)(g_i + (9+u)g_j), \\ B_{i,j} &= g_i \cdot g_j. \end{aligned}

Exponentiation in the Cyclotomic Subgroup

Given f∈Gϕ6(p2)⊂Fp12∗f \in \mathbb{G}_{\phi_{6}(p^2)} \subset \mathbb{F}_{p^{12}}^* and e≥1e \geq 1, the idea is to compute fef^e using the squaring formula in compressed form.

Represent ee in binary as e=eℓ−1eℓ−2…e1e0e = e_{\ell-1}e_{\ell-2}\dots e_1e_0 with eℓ−1=1e_{\ell-1}=1 and define He={i:1≤i≤ℓ−1 and ei=1}H_e = \{i : 1 \leq i \leq \ell - 1 \text{ and } e_i = 1\}. Then:

fe=∏i=0ℓ−1fei⋅2i=fe0∏i∈HeD(C(f)2i).f^e = \prod_{i=0}^{\ell - 1} f^{e_i \cdot 2^i} = f^{e_0} \prod_{i \in H_e} \mathcal{D}(\mathcal{C}(f)^{2^i}).

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)\mathcal{C}(f) rather than the standard ff 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]/(w2−v).\begin{aligned} \mathbb{F}_{p^2} &= \mathbb{F}_p[u]/(u^2 + 1), \\ \mathbb{F}_{p^6} &= \mathbb{F}_{p^2}[v]/(v^3 - (9+u)), \\ A = \mathbb{F}_{p^{12}} &= \mathbb{F}_{p^6}[w]/(w^2 - v). \end{aligned}

We use two additional representations of Fp12\mathbb{F}_{p^{12}}.

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

B=Fp12=Fp2[w]/(w6−(9+u)).B = \mathbb{F}_{p^{12}} = \mathbb{F}_{p^2}[w]/(w^6 - (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]/(w3−w3).\begin{aligned} \mathbb{F}_{p^{4}} &= \mathbb{F}_{p^2}[w^3]/((w^3)^2 - (9+u)), \\ C = \mathbb{F}_{p^{12}} &= \mathbb{F}_{p^4}[w]/(w^3 - w^3). \end{aligned}

Isomorphism between A and B

We start with A=Fp6[w]/(w2−v)A = \mathbb{F}_{p^6}[w]/(w^2 - v) and B=Fp2[w]/(w6−(9+u))B = \mathbb{F}_{p^2}[w]/(w^6 - (9+u)), where we notice that if we take a=a1+a2⋅w∈Aa = a_1 + a_2 \cdot w \in A, we can use the identity v=w2v = w^2 to rewrite it as:

a=(a1,1+a1,2v+a1,3v2)+(a2,1+a2,2v+a2,3v2)⋅w==a1,1+a2,1⋅w+a1,2⋅w2+a2,2⋅w3+a1,3⋅w4+a2,3⋅w5∈B\begin{aligned} a &= (a_{1,1} + a_{1,2}v + a_{1,3}v^2) + (a_{2,1} + a_{2,2}v + a_{2,3}v^2) \cdot w = \\[0.1cm] &= a_{1,1} + a_{2,1} \cdot w + a_{1,2} \cdot w^2 + a_{2,2} \cdot w^3 + a_{1,3} \cdot w^4 + a_{2,3} \cdot w^5 \in B \end{aligned}

This relationship induces the following field isomorphism χAB:A→B\chi_{AB} : A \to B and its inverse χAB−1:B→A\chi_{AB}^{-1} : B \to A:

χ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),χAB−1(b1,b2,b3,b4,b5,b6)=(b1,b3,b5,b2,b4,b6).\begin{aligned} \chi_{AB}(a_{1,1},a_{1,2},a_{1,3},a_{2,1},a_{2,2},a_{2,3}) &= (a_{1,1},a_{2,1},a_{1,2},a_{2,2},a_{1,3},a_{2,3}), \\[0.1cm] \chi_{AB}^{-1}(b_1,b_2,b_3,b_4,b_5,b_6) &= (b_1,b_3,b_5,b_2,b_4,b_6). \end{aligned}

Exercise: Check that χAB\chi_{AB} is a field isomorphism between AA and BB and that for all a∈Aa \in A and b∈Bb \in B it is true that χAB−1(χAB(a))=a\chi_{AB}^{-1}(\chi_{AB}(a)) = a and χAB(χAB−1(b))=b\chi_{AB}(\chi_{AB}^{-1}(b)) = b.

Isomorphism between B and C

We continue with B=Fp2[w]/(w6−(9+u))B = \mathbb{F}_{p^2}[w]/(w^6 - (9+u)) and C=Fp4[w]/(w3−w3)C = \mathbb{F}_{p^4}[w]/(w^3 - w^3), for which we notice that c=(c1+c2⋅w3)+(c3+c4⋅w3)w+(c5+c6⋅w3)⋅w2∈Cc = (c_1 + c_2 \cdot w^3) + (c_3 + c_4 \cdot w^3)w + (c_5 + c_6 \cdot w^3) \cdot w^2 \in C can be equivalently written as c1+c3⋅w+c5⋅w2+c2⋅w3+c4⋅w4+c6⋅w5∈Bc_1 + c_3 \cdot w + c_5 \cdot w^2 + c_2 \cdot w^3 + c_4 \cdot w^4 + c_6 \cdot w^5 \in B.

This relationship induces the following field isomorphism χBC:B→C\chi_{BC} : B \to C and its inverse χBC−1:C→B\chi_{BC}^{-1} : C \to B:

χBC(b1,b2,b3,b4,b5,b6)=(b1,b4,b2,b5,b3,b6),χBC−1(c1,c2,c3,c4,c5,c6)=(c1,c3,c5,c2,c4,c6).\begin{aligned} \chi_{BC}(b_1,b_2,b_3,b_4,b_5,b_6) &= (b_1,b_4,b_2,b_5,b_3,b_6), \\ \chi_{BC}^{-1}(c_1,c_2,c_3,c_4,c_5,c_6) &= (c_1,c_3,c_5,c_2,c_4,c_6). \end{aligned}

Exercise: Check that χBC\chi_{BC} is a field isomorphism between BB and CC and that for all b∈Bb \in B and c∈Cc \in C it is true that χBC−1(χBC(b))=b\chi_{BC}^{-1}(\chi_{BC}(b)) = b and χBC(χBC−1(c))=c\chi_{BC}(\chi_{BC}^{-1}(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\mathbb{G}_{\phi_{6}(p^2)} \subset \mathbb{F}_{p^{12}}. 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

Footnotes

  1. if you don’t know why, make sure to review Galois theory. Start by noticing that Galois group Gal(Fp12/Fp6)\text{Gal}(\mathbb{F}_{p^{12}}/ \mathbb{F}_{p^{6}}) is a cyclic group of order 22 generated by the Frobenius endomorphism F(X)=Xp6F(X) = X^{p^6}. ↩

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

Break my circuit

You are the prover. The verifier doesn't run the program; it only checks the constraints. Each circuit here is missing one, which is the most common bug in real ZK systems. Find a witness that fools it.

circuit.circom

Public (verifier sees)

Witness (you choose)

Constraints · everything mod 97