Skip to content
← Writing

Computing the Optimal Ate Pairing over the BLS12-381 Curve

A computation-first walkthrough of the optimal Ate pairing on BLS12-381: the extension-field tower, line equations, final exponentiation and cyclotomic tricks behind our zkASM implementation.

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

The post BLS12-381 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 BLS12-381 curve. This post will reuse some parts with an emphasy on the computational aspect of the optimal Ate pairing over such 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. Constructing Elliptic Curves with Prescribed Embedding Degrees.
  3. BLS12-381: New zk-SNARK Elliptic Curve Construction.

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 full list of consulted resources can be found at the end of this post.

The implementation in zkASM has been done in this PR.

The Curve

The BLS12-381 is the elliptic curve EE of the form:

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

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

p = 4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787

The number of points #E(Fp)=p+1−t\#E(\mathbb{F}_p) = p+1-t can be factorized as follows:

3 * 11^2 * 10177^2 * 859267^2 * 52437899^2 * 52435875175126190479447740508185965837690552500527637822603658699938581184513

where t = x + 1 = -15132376222941642751 is known as the trace of Frobenius and x is the BLS-family parameter that generates this specific curve.

Therefore, its 255255-bit prime factor:

r = 52435875175126190479447740508185965837690552500527637822603658699938581184513

is the only one with sufficiently size for cryptographic purposes.

The point P∈E(Fp)P \in E(\mathbb{F}_p) with coordinates:

x = 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507
y = 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569

is a point of order rr, i.e. a generator of the subgroup of size rr of the curve.

The embedding degree kk of this curve is 1212.

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 BLS12-381, we have β=−1\beta = -1 and ξ=1+u\xi = 1+u and therefore:

Fp2=Fp[u]/(u2+1),Fp6=Fp2[v]/(v3−(1+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 - (1+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=1+uv^3 = 1+u and consequently the idenity w6=1+uw^6 = 1+u.

Using the latter identity, we can also write:

Fp12=Fp2[w]/(w6−(1+u))\mathbb{F}_{p^{12}} = \mathbb{F}_{p^2}[w]/(w^6 - (1+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 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 fpf^p and fp2f^{p^2} (needed for the final exponentiation) very efficiently.

Twist

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

y′2=x′3+4⋅(1+u).y'^2 = x'^3 + 4·(1+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 point P∈E′(Fp2)P \in E'(\mathbb{F}_{p^2}) with coordinates:

x = 352701069587466618187139116011060144890029952792775240219908644239793785735715026873347600343865175952761926303160 + 3059144344244213709971259814753781636986470325476647558659373206291635324768958432433509563104347017837885763365758·u
y = 1985150602287291935568054521177171638300868978215655730859378665066344726373823718423869104263333984641494340347905 + 927553665492332455747201965776037880757740193453592970025027978793976877002675564980949289727957565575433344219582·u

is a point of order rr, i.e. a generator of the subgroup of size rr of the curve.

The following mapping, known as the untwist:

φ(x′,y′)=(x′/w2,y′/w3)\varphi(x',y') = (x'/w^2,y'/w^3)

with:

1/w^2 = (2001204777610833696708894912867952078278441409969503942666029068062015825245418932221343814564507832018947136279894 + 2001204777610833696708894912867952078278441409969503942666029068062015825245418932221343814564507832018947136279893·u)·w^4
1/w^3 = (2001204777610833696708894912867952078278441409969503942666029068062015825245418932221343814564507832018947136279894 + 2001204777610833696708894912867952078278441409969503942666029068062015825245418932221343814564507832018947136279893·u)·w^3

defines a group isomorphism from E′(Fp2)[r]E'(\mathbb{F}_{p^2})[r] to E(Fp12)[r]∩Ker(πp−[p])E(\mathbb{F}_{p^{12}})[r] \cap \text{Ker}(\pi_p - [p]) and we are going to use it to convert points from the twisted curve E′E' to the curve EE only whenever required.

The inverse mapping, the twist, is defined as:

φ−1(x′,y′)=(x′w2,y′w3).\varphi^{-1}(x',y') = (x'w^2,y'w^3).

The Pairing

The optimal Ate pairing e:G1×G2→GTe : \mathbb{G}_1 \times \mathbb{G}_2 \to \mathbb{G}_T over the BLS12-381 curve is defined by:

e(P,Q)=f∣x∣,Q(P)‾p12−1re(P,Q) = \overline{f_{|x|,Q}(P)}^{\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(Fp12)[r]∩Ker(πp−[p])\mathbb{G}_2 = E(\mathbb{F}_{p^{12}})[r] \cap\text{Ker}(\pi_p - [p]), represented for point arithmetic efficiency by E′(Fp2)[r]E'(\mathbb{F}_{p^2})[r]. The former is the trace zero subgroup of the rr-torsion of EE over Fp12\mathbb{F}_{p^{12}}, whereas the latter 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 = -15132376222941642752, which is a number of 6464 bits that can be expressed in binary as:

    2^63 + 2^62 + 2^60 + 2^57 + 2^48 + 2^16
    [1,1,0,1,0,0,1,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,0,0,0,0,0,0,0,0]
    0b1101001000000001000000000000000000000000000000010000000000000000

    and has a Hamming weight of 6.

  • 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]\varphi(Q),[j]\varphi(Q)}}{v_{[i+j]\varphi(Q)}}, \\ f_{i+1,Q} = f_{m,Q} \cdot \frac{\ell_{[i]\varphi(Q),\varphi(Q)}}{v_{[i+1]\varphi(Q)}}, \\ \end{aligned}

    starting with f1,Q=1f_{1,Q} = 1.

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.

Therefore, what we want to compute is the following:

def e(P,Q):
    assert(subgroup_check_G1(P))
    assert(subgroup_check_G2(Q))
    if ((P == 0) or (Q == 0)) return 1 # Here, 0 represents the 
                                       # point at infinity
    R = Q
    f = 1
    for i in range(len(x)-2,-1,-1):
        f = f * f * line(untwist(R),untwist(R),P)
        R = 2 * R
        if x[i] == 1:
            f = f * line(untwist(R),untwist(Q),P)
            R = R + Q

    return final_exponentiation(conjugate(f))

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

Subgroup Checks

To summary 2019/814, we have:

  • A point P∈E(Fp)P \in E(\mathbb{F}_{p}) belongs to G1=E(Fp)[r]\mathbb{G}_1 = E(\mathbb{F}_{p})[r] if and only if:

    [x2−13]([2]σ(P)−P−σ2(P))=σ2(P)\left[\frac{x^2-1}{3}\right]([2]\sigma(P) - P - \sigma^2(P)) = \sigma^2(P)

    where σ:E(Fp)→E(Fp)\sigma : E(\mathbb{F}_{p}) \to E(\mathbb{F}_{p}) is the well-known endomorphism1 defined by (a,b)→(γ⋅a,b)(a,b) \to (\gamma·a, b), where γ∈Fp∗\gamma \in \mathbb{F}_p^* is of order 33.

    𝛄 = 4002409555221667392624310435006688643935503118305586438271171395842971157480381377015405980053539358417135540939436

    This is clearly much cheaper than the naive scalar multiplication by rr, since its main cost is the scalar multiplication by (x2−1)/3(x^2-1)/3, which is half the size of rr and has low Hamming weight.

  • A point Q∈E′(Fp2)Q \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]ψ3(Q)+Q=ψ2(Q)[x]\psi^3(Q) + Q = \psi^2(Q)

    where ψ:E′(Fp2)→E′(Fp2)\psi : E'(\mathbb{F}_{p^2}) \to E'(\mathbb{F}_{p^2}) is the untwist-Frobenius-twist endomorphism, i.e., ψ:=φ−1πpφ\psi := \varphi^{-1} \pi_p \varphi.

    Again, this is much cheaper than the naive scalar multiplication by rr, since its main cost is the scalar multiplication by xx, which is roughly a fourth the size of rr.

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)\varphi(Q_1) and φ(Q2)\varphi(Q_2) is given by:

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

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)\varphi(Q) is given by:

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

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}}.

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).

The Hard Part

Following Section 5 of this 2020/875, we can write:

p4−p2+1r=(∣x∣+1)2⋅(p−∣x∣)⋅(x2+p2−1)3+1.\frac{p^4-p^2+1}{r} = \frac{(|x|+1)^2\cdot(p-|x|)\cdot(x^2+p^2-1)}{3}+1.

To compute it, we proceed as follows:

def hard_part(m):
    x = abs(x)
    
    # 1] m^{(x+1)/3}
    y1 = m**((x+1)/3)
    
    # 2] m^{(x+1)^2/3}
    y2 = y1**(x+1)
    
    # 3.1] m^{(x+1)^2/3*-x}
    y31 = conjugate(y2)**x
    
    # 3.2] m^{(x+1)^2/3*p}
    y32 = Frobenius1(y2)
    
    # 4] m^{(x+1)^2/3*(p-x)}
    y4 = y31*y32
    
    # 5.1] m^{(x+1)^2/3*(p-x)*x^2}
    y51 = (y4**x)**x
    
    # 5.2] m^{(x+1)^2/3*(p-x)*p^2}
    y52 = Frobenius2(y4)
    
    # 5.3] m^{(x+1)^2/3*(p-x)*-1}
    y53 = conjugate(y4)
    
    # 6] m^{(x+1)^2/3*(p-x)*(x^2+p^2)}
    y6 = y51*y52
    
    # 7] m^{(x+1)^2/3*(p-x)*(x^2+p^2-1)}
    y7 = y6*y53
    
    # 8] m^{(x+1)^2/3*(p-x)*(x^2+p^2-1)+1}
    y8 = y7*m

    return y8

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

Frobenius Operator

As we have shown, in the final exponentation we must compute the first and second Frobenius operator πp,πp2\pi_p,\pi_p^2 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(\log p) 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}.

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,\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, \end{aligned}

with γ1,i=(1+u)ip−16\gamma_{1,i} = (1+u)^{i\frac{p-1}{6}} and γ2,i=(1+u)ip2−16=γ1,i⋅γˉ1,i\gamma_{2,i} = (1+u)^{i\frac{p^2-1}{6}} = \gamma_{1,i} \cdot \bar{\gamma}_{1,i}.

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 raising 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 from 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=(1+u)p−16ww^p = (w^6)^{\frac{p-1}{6}}w = (1+u)^{\frac{p-1}{6}}w and therefore (wi)p=γ1,iwi(w^{i})^p = \gamma_{1,i}w^i, where γ1,i=(1+u)ip−16\gamma_{1,i} = (1+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=(1+u)p2−16w=(γ1,1)1+pw=γ1,1⋅γˉ1,1ww^{p^2} = (1+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}.

Hence, assuming the precomputation of γ1,i,γ2,i\gamma_{1,i},\gamma_{2,i} for i∈[5]i \in [5], we can compute fpf^p and fp2f^{p^2} almost for free.

Speeding up Exponentiations in the Cyclotomic Subgroup

I follow 2010/542 closely for this section.

Recall that:

Fp2=Fp[u]/(u2+1),Fp6=Fp2[v]/(v3−(1+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 - (1+u)), \\ \mathbb{F}_{p^{12}} &= \mathbb{F}_{p^6}[w]/(w^2 - v). \end{aligned}

and also:

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

For this optimitzation, it will be useful to introduce Fp4\mathbb{F}_{p^{4}} and represent Fp12\mathbb{F}_{p^{12}} as a degree-3 extension of Fp4\mathbb{F}_{p^{4}}:

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

where V=w3V = w^3; so that an element ff in Fp12\mathbb{F}_{p^{12}} can be written as f=(g0+g1V)+(g2+g3V)w+(g4+g5V)w2f = (g_0 + g_1 V) + (g_2 + g_3 V) w + (g_4 + g_5 V) w^2. We can equivalently write f=g0+g2w+g4w2+g1w3+g3w4+g5w5f = g_0 + g_2 w + g_4 w^2 + g_1 w^3 + g_3 w^4 + g_5 w^5.

These optimizations are due to “compression” and “decompression” techniques, 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+g5w5∈Gϕ6(p2)f = g_0 + g_2w + g_4w^2 + g_1w^3 + g_3w^4 + g_5w^5 \in \mathbb{G}_{\phi_{6}(p^2)} 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(1+u)+3g~42−2g~34g~2,g~0=(2g~12+g~2g~5−3g~3g~4)(1+u)+1,if g~2≠0g~1=2g~4g~5g~3,g~0=(2g~12−3g~3g~4)(1+u)+1,if g~2=0\begin{cases} \tilde{g}_1 = \frac{\tilde{g}_5^2(1+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)(1+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)(1+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(1+u)B4,5),h3=3(A4,5−(2+u)B4,5)−2g3,h4=3(A2,3−(2+u)B2,3)−2g4,h5=2(g5+3B2,3),Ai,j=(gi+gj)(gi+(1+u)gj),Bi,j=gi⋅gj.\begin{aligned} h_2 &= 2(g_2 + 3(1+u)B_{4,5}), \\ h_3 &= 3(A_{4,5} - (2 + u)B_{4,5}) - 2g_3, \\ h_4 &= 3(A_{2,3} - (2+u)B_{2,3}) - 2g_4, \\ h_5 &= 2(g_5 + 3B_{2,3}), \\ A_{i,j} &= (g_i + g_j)(g_i + (1+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=∏i=0ℓ−1(f2i)ei=fe0∏i∈HeD(C(f)2i).f^e = \prod_{i=0}^{\ell - 1} f^{e_i \cdot 2^i} = \prod_{i = 0}^{\ell - 1} (f^{2^i})^{e_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;

Resources

Footnotes

  1. The endomorphism σ\sigma acts as a multiplication map [λ]P[\lambda]P, where λ=t2−1\lambda = t^2 - 1. ↩

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