# pairing

Optimal ate pairing over Barreto-Naehrig curves

https://github.com/adjoint-io/pairing#readme

Version on this page: | 0.1.4 |

LTS Haskell 14.27: | 0.4.1 |

Stackage Nightly 2019-08-07: | 0.4.1 |

Latest on Hackage: | 1.1.0 |

**Adjoint Inc ([email protected])**

`pairing-0.1.4@sha256:168fc9923bcf1d9b4c510c79bd6850b1d11436b692b2ab398d85da47f9f96b1d,3102`

#### Module documentation for 0.1.4

*(full list with versions)*:

Implementation of the Barreto-Naehrig (BN) curve construction from
[BCTV2015] to provide two cyclic groups
**G1** and **G2**, with an efficient bilinear pairing:

*e: G1 × G2 → GT*

# Pairing

Let G1, G2 and GT be abelian groups of prime order `q`

and let `g`

and `h`

elements of G1 and G2 respectively . A pairing is a non-degenerate bilinear map e: G1 × G2 → GT.

This bilinearity property is what makes pairings such a powerful primitive in cryptography. It satisfies:

- e(g1 + g2, h) = e(g1, h) e(g2, h)
- e(g, h1 + h2) = e(g, h1) e(g, h2)

The non-degeneracy property guarantees non-trivial pairings for non-trivial arguments. In other words, being non-degenerate means that:

- ∀ g ≠ 1, ∃ hi ∈ G2 such that e(g, hi) ≠ 1
- ∀ h ≠ 1, ∃ gi ∈ G1 such that e(gi, h) ≠ 1

An example of a pairing would be the scalar product on euclidean space <.> : Rn × Rn → R

## Example Usage

A simple example of calculating the optimal ate pairing given two points in G1 and G2.

```
import Protolude
import Pairing.Group
import Pairing.Pairing
import Pairing.Point
import Pairing.Fq (Fq(..))
import Pairing.Fq2 (Fq2(..))
e1 :: G1
e1 = Point
(Fq 1368015179489954701390400359078579693043519447331113978918064868415326638035)
(Fq 9918110051302171585080402603319702774565515993150576347155970296011118125764)
e2 :: G2
e2 = Point
(Fq2
(Fq 2725019753478801796453339367788033689375851816420509565303521482350756874229)
(Fq 7273165102799931111715871471550377909735733521218303035754523677688038059653)
)
(Fq2
(Fq 2512659008974376214222774206987427162027254181373325676825515531566330959255)
(Fq 957874124722006818841961785324909313781880061366718538693995380805373202866)
)
main :: IO ()
main = do
putText "Ate pairing:"
print (atePairing e1 e2)
let
lhs = reducedPairing (gMul e1 2) (gMul e2 3)
rhs = (reducedPairing e1 e2)^(2 * 3)
putText "Is bilinear:"
print (lhs == rhs)
```

## Pairings in cryptography

Pairings are used in encryption algorithms, such as identity-based encryption (IBE), attribute-based encryption (ABE), (inner-product) predicate encryption, short broadcast encryption and searchable encryption, among others. It allows strong encryption with small signature sizes.

## Admissible Pairings

A pairing `e`

is called admissible pairing if it is efficiently computable. The only admissible pairings that are suitable for cryptography are the Weil and Tate pairings on algebraic curves and their variants. Let `r`

be the order of a group and E[r] be the entire group of points of order `r`

on E(Fq). E[r] is called the r-torsion and is defined as E[r] = { P ∈ E(Fq) | rP = O }. Both Weil and Tate pairings require that `P`

and `Q`

come from disjoint cyclic subgroups of the same prime order `r`

. Lagrange’s theorem states that for any finite group `G`

, the order (number of elements) of every subgroup `H`

of `G`

divides the order of `G`

. Therefore, r | #E(Fq).

G1 and G2 are subgroups of a group defined in an elliptic curve over an extension of a finite field Fq, namely E(Fqk), where `q`

is the characteristic of the field and `k`

is a positive integer called embedding degree.

The embedding degree `k`

plays a crucial role in pairing cryptography:

- It’s the value that makes Fqk be the smallest extension of Fq such that E(Fqk) captures more points of order
`r`

. - It’s the minimal value that holds r | (qk - 1).
- It’s the smallest positive integer such that E[r] ⊂ E(Fqk)

There are subtle but relevant differences in G1 and G2 subgroups depending on the type of pairing. Nowadays, all of the state-of-the-art implementations of pairings take place on ordinary curves and assume a type of pairing (Type 3) where G1 = E[r] ∩ Ker(π - [1]) and G2 = E[r] ∩ Ker(π - [q]) and there is no non-trivial map φ: G2 → G1.

## Tate Pairing

The Tate pairing is a map:

tr : E(Fqk)[r] × E(Fqk) / rE(Fqk) → F*qk / (F*qk)r

defined as:

tr(P, Q) = f(Q)

where P ∈ E(Fqk)[r], Q is any representative in a equivalence class in E(Fqk) / rE(Fqk) and F*qk / (F*qk)r is the set of equivalence classes of F*qk under the equivalence relation a ≡ b iff a / b ∈ (F*qk)r. The equivalence relation in the output of the Tate pairing is unfortunate. In cryptography, different parties must compute the same value under the bilinearity property.

The reduced Tate pairing solves this undesirable property by exponentiating elements in F*qk / (F*qk)r to the power of (qk - 1) / r. It maps all elements in an equivalence class to the same value. It is defined as:

Tr(P, Q) = tr(P, Q)#Fqk / r = fr,P(Q)(qk - 1) / r.

When we say Tate pairing, we normally mean the reduced Tate pairing.

## Pairing optimization

Tate pairings use Miller’s algorithm, which is essentially the double-and-add algorithm for elliptic curve point multiplication combined with evaluation of the functions used in the addition process. Miller’s algorithm remains the fastest algorithm for computing pairings to date.

Both G1 and G2 are elliptic curve groups. GT is a multiplicative subgroup of a finite field. The security an elliptic curve group offers per bit is considerably greater than the security a finite field does. In order to achieve security comparable to 128-bit security (AES-128), an elliptic curve of 256 bits will suffice, while we need a finite field of 3248 bits. The aim of a cryptographic protocol is to achieve the highest security degree with the smallest signature size, which normally leads to a more efficient computation. In pairing cryptography, significant improvements can be made by keeping all three group sizes the same. It is possible to find elliptic curves over a field Fq whose largest prime order subgroup `r`

has the same bit-size as the characteristic of the field `q`

. The ratio between the field size `q`

and the large prime group order `r`

is called the φ-value. It is an important value that indicates how much (ECDLP) security a curve offers for its field size. φ=1 is the optimal value. The Barreto-Naehrig (BN) family of curves all have φ=1 and k=12. They are perfectly suited to the 128-bit security level.

Most operations in pairings happen in the extension field Fqk. The larger k gets, the more complex Fqk becomes and the more computationally expensive the pairing becomes. The complexity of Miller’s algorithm heavily depends on the complexity of the associated Fqk-arithmetic. Therefore, the aim is to minimize the cost of arithmetic in Fqk.

It is possible to construct an extension of a field Fqk by successively towering up intermediate fields Fqa and Fqb such that k = a^i b^j, where a and b are usually 2 and 3. One of the reasons tower extensions work is that quadratic and cubic extensions (Fq2 and Fq3) offer methods of performing arithmetic more efficiently.

Miller’s algorithm in the Tate pairing iterates as far as the prime group order `r`

, which is a large number in cryptography. The ate pairing comes up as an optimization of the Tate pairing by shortening Miller’s loop. It achieves a much shorter loop of length T = t - 1 on an ordinary curve, where t is the trace of the Frobenius endomorphism. The ate pairing is defined as:

at(Q,P) = fr,Q(P)(qk - 1) / r

## Implementation

We have implemented the optimal Ate pairing over the BN128 curve, i.e. we define `q`

and `r`

as

- q = 36t4 + 36t3 + 24t2 + 6t + 1
- r = 36t4 + 36t3 + 18t2 + 6t + 1
- t = 4965661367192848881

The tower of finite fields we work with is defined as follows:

- Fq is the prime field with characteristic
`q`

- Fq2 := Fq[u]/u2 + 1
- Fq6 := Fq2[v]/v3 - (9 + u)
- Fq12 := Fq6[w]/w2 - v

The groups’ definitions are:

- G1 := E(Fq), with equation y2 = x3 + 3
- G2 := E’(Fq2), with equation y2 = x3 + 3 / (9 + u)
- GT := μr, i.e. the
`r`

-th roots of unity subgroup of the multiplicative group of Fq12

## License

```
Copyright (c) 2018-2019 Adjoint Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
```

## Changes

# Changelog for pairing

## 0.1

- Initial release.