# Example: Implementing a pairing-based signature scheme

Cryptimeleon Math is a library supplying the mathematical basics for cryptography.

In this notebook, we’ll take a look at how to implement a pairing-based scheme.

The (multi-message) Pointcheval-Sanders signature scheme is a very useful digital signature scheme for advanced cryptographic constructions because of its elegant and simple algebraic structure. We’ll use it as an example for implementing a cryptographic scheme.

We’ll work alongside the scheme’s definition in the paper:

… and show how to implement it.

Note: You can also check this page out in an interactive Jupyter notebook by clicking the badge below:

## Setting up the bilinear group

First, we need to set up the bilinear group setting required for the scheme. We need to know the type of pairing and the desired security parameter. In this case we want a type 3 pairing and 100 bit security.

%maven org.cryptimeleon:math:3.0.1
import org.cryptimeleon.math.structures.groups.elliptic.*;
import org.cryptimeleon.math.structures.groups.elliptic.type3.bn.BarretoNaehrigBilinearGroup;
import org.cryptimeleon.math.structures.groups.*;
import org.cryptimeleon.math.structures.groups.mappings.*;
import org.cryptimeleon.math.structures.rings.zn.*;

// Choose number of messages r
var r = 3;

// BN pairing is type 3 and we specify a 100 bit security parameter
BilinearGroup bilinearGroup = new BarretoNaehrigBilinearGroup(100);

// Let's collect the values for our pp
Group groupG1 = bilinearGroup.getG1();
Group groupG2 = bilinearGroup.getG2();
Group groupGT = bilinearGroup.getGT();
BilinearMap e = bilinearGroup.getBilinearMap();
BigInteger p = groupG1.size();
Zn zp = bilinearGroup.getZn();
System.out.println("Generated bilinear group of order " + p);
Generated bilinear group of order 66696243400694322499906033083377966773014133293855982130239936888504801018589797

## Generating a key pair

For a key pair, we need to generate random exponents $$x$$ and $$y_i$$ as the secret key. Because it’s a group of order $$p$$, we interpret the exponents as elements of $$\mathbb{Z}_p$$.

// Generate secret key

var x = zp.getUniformlyRandomElement();
var y = zp.getUniformlyRandomElements(r); //computes a vector of r random numbers y_0, ..., y_(r-1)

System.out.println("x = " + x);
System.out.println("y = " + y);
x = 29595934960003020444107534039537102483550993225474520393316645701745378759233847
y = [51846473421590272311811580401837390549611745304688048326757539608512745926166609, 40744123197122423835896290149525754957962305608723555152477514071868400120915516, 14118433781163385433907583197138836400965709724838452006859106497983035966695790]

Then we can compute the corresponding public key easily and run precomputation on it to speed up later verifications:

// Generate public key

var tildeg = groupG2.getUniformlyRandomElement();
var tildeX = tildeg.pow(x).precomputePow(); // this computes X = tildeg^x as above and runs precomputations to speed up later pow() calls on tildeX
var tildeY = tildeg.pow(y).precomputePow(); // because y is a vector, this yields a vector of values tildeg.pow(y_0), tildeg.pow(y_1), ...
System.out.println("tildeg = " + tildeg);
System.out.println("tildeX = " + tildeX);
System.out.println("tildeY = " + tildeY);
tildeg = ([66566665320523086311834270182424013291533607057269930597322004707754858852991768, 66417315842984586624869491175841637964526368869195458410626598319010930898491454],[47975601810232346435154561082965619008302490527753986506429374975258343095700078, 46181774961763282130431993239454355617906329822567175337660632156941709902812974])
tildeX = ([10213786910752021025982376628731003821568873369121198122262709825862584469690478, 46020894631100939002434761012088917032359697581950969085448336866970548207571542],[27965971510496021847992517628567839732273444766337047173963114788458586326605781, 29120622231750253033314881138479103885818245578727351244532368889398295837752957])
tildeY = [([49375743799900755311097738391372857656788888126865649044643649257703850322668252, 37823181782501983524313488843344248100475931890231305778015949618153834267236410],[16695430345700177019986557639775938098524401685016029767744548022428905004444430, 48971556488066141662768415711822079733454941848085850355447475911164643635799990]), ([20416906519755103709032961173584310844609826712312371149364049542875299385814567, 17121972027132155327344887147214287887548217858852686014359052573773859835391189],[38982153705178966571975191407279945619604225931352043947622766710361017353573045, 54708150629572498480014597924628824244276161208930394125520108948573979389499859]), ([60755641489301040670670233143808477666105922545143268053947293518373890773088770, 46295344852704424377095182925033242319147674527951407853083444586762099257735259],[52207316683747270030218291458095217670150635401678282648187799309197624806782325, 15522797480203736280932157345691664662096498562438520372149377420131722492320384])]

## Computing a signature

Computing a signature works as you’d expect now with what we’ve already seen. Messages for Pointcheval-Sanders lie in $$\mathbb{Z}_p$$, but we can use a hash function $$\mathcal{H}:\{0,1\}\rightarrow \mathbb{Z}_p$$ to sign arbitrary strings.

import org.cryptimeleon.math.structures.rings.cartesian.RingElementVector;

// Preparing messages ("Hello PS sigs", 42, 0, 0, ...)
var m = new RingElementVector(
bilinearGroup.getHashIntoZGroupExponent().hash("Hello PS sigs"),
);

// Computing signature
var sigma1 = groupG1.getUniformlyRandomNonNeutral().compute(); // h
var sigma2 = sigma1.pow(x.add(y.innerProduct(m))).compute(); // h^{x + sum(y_i*m_i)}
// The compute() call is optional but will cause sigma1 and sigma2 to be computed concurrently in the background.
System.out.println("sigma1 = " + sigma1);
System.out.println("sigma2 = " + sigma2);
sigma1 = (30813667478663470838255044866161582757476755771178391733522201843082578722518389,51354455544643278444174827360110250738214088064862493697057526715617676068545644)
sigma2 = (43621328238404016248364605427528770413434141917542387917760779987402894740316616,26608370877135380121335308396159046924766666271164693360705469066169584170996850)

## Verifying a signature

For this verification, we need to emply the pairing e.

!sigma1.isNeutralElement()
&& e.apply(sigma1, tildeX.op(tildeY.innerProduct(m))).equals(e.apply(sigma2, tildeg))
true

If this pairing computation seems slow, check out the mclwrap addon for a faster bilinear group.