Introducing a new way to review your PRs.

Read the announcement

mratsim / constantine

Constantine: modular, high-performance, zero-dependency cryptography stack for verifiable computation, proof systems and blockchain protocols.

View on GitHub

19 documents · Updated Nov 13, 2026

docs / pairing-engine

Pairing engine

The pairing engine maps one point from G1 and one point from G2 to a value in the target group GT. It builds this value with line evaluations and a Miller loop, then applies final exponentiation so higher-level systems can use the result for signature and proof checks.

Pairing model

Group roles

The pairing is a bilinear map from G1 × G2 to GT. G1 and G2 contain elliptic-curve points. GT contains the multiplicative result in an extension field. Bilinearity lets a protocol turn a relation between curve points into a relation in GT.

For the main pairing families, G1 uses the base field and G2 uses a quadratic extension with a curve twist. BLS12 and BN curves use a degree-12 target field. BW6-761 uses a degree-6 target field and has a separate curve-specific path. See Extension fields and tower construction for the field relationship and Named curves and group operations for the source groups.

References: constantine/…/pairings_generic.nim• L19-25 constantine/…/pairings_bls12.nim• L155-190 constantine/…/pairings_bn.nim• L164-205 constantine/…/pairings_bw6_761.nim• L149-161

Pairing interfaces

The pairing interface writes one result to GT. The pairing_check interface combines two point pairs and returns whether their product is the identity in GT. Protocol code uses the check form when it can place a negated point in one pair and test the full relation against one.

The low-level surface also exposes the Miller loop and the two final-exponentiation stages as separate operations. This surface has no stability guarantee. Higher-level protocol APIs must apply their own input checks and protocol rules before they use it. The parent page, Elliptic curves and pairings, describes the wider mathematical API.

References: constantine/…/pairings_generic.nim• L27-123 constantine/lowlevel_pairing_curves.nim• L21-32 constantine/lowlevel_pairing_curves.nim• L44-79

Pairing execution path

Miller loop

The Miller loop builds the point-dependent part of a pairing. It walks a curve parameter through repeated doublings and additions in G2. Each step creates a line function, evaluates it at the G1 point, and multiplies the result into an accumulator in the target field.

The general loop uses a signed digit form of the curve parameter. Named curves can replace it with a fixed addition chain. The same loop shape can process many point pairs at once, so the target-field accumulator holds the product of their partial pairing values.

This diagram shows how source points become the target-field value that enters final exponentiation.

flowchart LR
    P[G1 point] --> E[Line evaluation]
    Q[G2 point] --> U[G2 doubling or addition]
    U --> L[Line function]
    L --> E
    E --> A[Miller accumulator]
    A --> F[Pre-final target value]

References: constantine/…/miller_loops.nim• L29-90 constantine/…/miller_loops.nim• L286-321 constantine/…/bls12_381_pairings.nim• L42-82

Line functions

Line functions connect the group operation in G2 to the field operation in GT. A doubling line or addition line updates the current projective point and produces a compact line value. The engine evaluates that value with the G1 point and places it in the target field through the curve twist.

The line value is sparse because the twist maps it into a field representation with many zero coordinates. Sparse multiplication keeps the Miller loop focused on the non-zero part. Non-zero scale factors from the line representation do not affect the final pairing after the final exponentiation.

References: constantine/…/lines_eval.nim• L72-100 constantine/…/lines_eval.nim• L130-142 constantine/…/lines_eval.nim• L335-358 constantine/…/lines_eval.nim• L1347-1400

Curve-family paths

The generic pairing entry point selects a path from the named curve family. BLS12 curves use an optimal Ate loop and a BLS12 hard final exponentiation. BN curves use an optimal Ate loop with a BN correction and a BN hard final exponentiation.

The family path also handles the sign of its curve parameter. A negative BN parameter requires conjugation and point adjustment before the BN correction. Named constants provide the parameter, its sign, and the curve-specific exponentiation chain. These are curve rules, not choices for protocol callers.

BW6-761 is defined separately with an Fp6 target and its own parameters. Its current reference pairing entry reports that the Miller loop is not working, so it is not a ready substitute for the BN or BLS12 generic path.

References: constantine/…/pairings_generic.nim• L19-25 constantine/…/pairings_bn.nim• L50-67 constantine/…/pairings_bn.nim• L164-193 constantine/…/pairings_bls12.nim• L54-64 constantine/…/pairings_bw6_761.nim• L149-161

Final exponentiation

Easy exponentiation

The Miller result is not yet the unique GT value required by a protocol. The easy part removes the field cofactor and maps the result into the cyclotomic subgroup. It uses inversion, conjugation, and Frobenius maps instead of a large general exponentiation.

For a degree-12 target, the easy exponent is (p^6 - 1)(p^2 + 1). For a degree-6 target, it is (p^3 - 1)(p + 1). The result is unitary, so conjugation gives its inverse and the engine can use fast cyclotomic squaring in the next stage.

References: constantine/…/cyclotomic_subgroups.nim• L38-66 constantine/…/cyclotomic_subgroups.nim• L118-196 constantine/…/cyclotomic_subgroups.nim• L198-216

Hard exponentiation

The hard part maps the cyclotomic value into the subgroup of order r, which is the target group GT. BLS12 and BN paths use chains based on the curve parameter. These chains combine cyclotomic squaring, curve-parameter powers, Frobenius maps, and multiplication. A full generic exponentiation remains available as a slow reference for checks.

The complete single-pair path is ordered as follows:

  1. Run the curve-family Miller loop.
  2. Run the easy final exponentiation.
  3. Run the family-specific hard final exponentiation.
  4. Use the GT value or test it against one.

References: constantine/…/pairings_bls12.nim• L80-95 constantine/…/pairings_bls12.nim• L100-164 constantine/…/pairings_bn.nim• L92-112 constantine/…/pairings_bn.nim• L112-176 constantine/…/pairings_generic.nim• L111-123

Target-group membership

The engine separates cyclotomic membership from target-group membership. isInCyclotomicSubgroup checks the result after the easy stage. isInPairingSubgroup checks the stronger order-r condition after the hard stage. The latter checks use curve-specific relations and assume the input is already in the cyclotomic subgroup.

This split gives a useful failure boundary. Failure after the easy stage points to the field or Miller result. Success at that stage followed by failure at the target-group stage points to the hard chain or its curve parameters.

References: constantine/…/cyclotomic_subgroups.nim• L414-434 constantine/…/bls12_381_pairings.nim• L124-135 constantine/…/bn254_snarks_pairings.nim• L103-119

Multi-pairing flow

Product of pairings

The multi-pairing path combines several point pairs during the Miller loop and performs one final exponentiation for the whole product. It computes e(P0, Q0) * e(P1, Q1) * ... in GT. This fits protocol equations that already require a product of pairings to equal one.

The loop shares the walk through the curve parameter across all pairs. At each step it evaluates one line per pair and merges the line products with sparse target-field operations. Final exponentiation waits until all pair contributions are present.

References: constantine/…/pairings_bls12.nim• L166-190 constantine/…/pairings_bn.nim• L178-205 constantine/…/miller_loops.nim• L286-321

Miller accumulator

MillerAccumulator provides a streaming boundary for multi-pairings. A caller adds affine point pairs. The accumulator runs a batched Miller loop when its local buffer reaches its limit, then multiplies that partial result into its stored value. handover finishes pending local work before a merge, and finish returns the product before final exponentiation.

This boundary supports serial batches and parallel protocol work. Workers can build local accumulators, reduce them before transfer, and merge their partial target-field values. The protocol then performs one final exponentiation on the merged result.

References: constantine/…/miller_accumulators.nim• L24-62 constantine/…/miller_accumulators.nim• L64-104 constantine/…/miller_accumulators.nim• L106-150

Protocol boundary

BLS verification

BLS verification uses a product pairing check. It hashes a message to a curve point, pairs the public key with that point, and pairs the signature with the negated generator. A valid signature makes the product equal to one. Aggregate and batch verification use the Miller accumulator so many checks share the final exponentiation.

The BLS layer expects public keys and signatures to pass non-infinity and subgroup checks before they reach the pairing engine. The pairing engine supplies the group relation. The BLS layer supplies the signature rules and input policy.

References: constantine/…/bls_signatures.nim• L79-102 constantine/…/bls_signatures.nim• L214-242 constantine/…/bls_signatures.nim• L494-528

KZG verification

KZG verification also reduces to pairing products. The commitment, proof, evaluation value, and setup points form point pairs whose product must equal one. The commitment API can therefore use the same pairing check and multi-pairing flow without handling line evaluation or final exponentiation itself.

References: constantine/…/kzg.nim• L260-286 constantine/…/kzg.nim• L370-390 constantine/…/kzg_multiproofs.nim• L185-205 constantine/…/kzg_parallel.nim• L210-230

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep each named curve's Ate parameter, sign, target field, and final-exponent constants aligned. The Miller loop and hard exponentiation use the same curve data.
  • Keep the line layout and twist choice aligned with the target-field tower. A change in one can change both line evaluation and sparse multiplication.
  • Preserve the contract that MillerAccumulator.finish returns a pre-final-exponentiation value.
  • Recheck higher-level pairing equations when generator signs or G1 and G2 placement change. These choices are part of the protocol relation.
  • Keep subgroup-check assumptions at protocol and serialization boundaries. The low-level pairing API does not validate every point for its caller.
  • Add a separate generic dispatch path when a new pairing family is introduced. The current generic entry point supports BN and BLS12 families.

Important failure modes

TriggerConsequenceGuard
G1 or G2 input is the infinity pointAn accumulator update rejects the pair or a direct protocol check failsValidate non-infinity inputs before pairing or accumulation
Point is on the curve but outside the required subgroupA pairing result can satisfy an unintended relationPerform named-group checks during input decoding or before protocol verification
Miller output skips the easy final-exponentiation stageThe hard stage receives a value outside its cyclotomic-subgroup assumptionUse the complete easy-then-hard sequence
Pairing products use the wrong generator sign or swap G1 and G2A valid proof or signature is reported as invalidFollow the pairing equation owned by the BLS or KZG API
Unsupported family is sent to the generic pairing entry pointCompilation fails, or a separate family path remains unavailableConfirm family dispatch and implementation status before selecting a curve
Variable-time GT exponentiation receives secret dataTiming or power observations can reveal scalar informationUse constant-time gtExp for secret scalars and reserve gtExp_vartime for public values

Key files

FileWhy to read it
constantine/lowlevel_pairing_curves.nimDefines the low-level pairing surface and the stages exposed to primitive users
constantine/math/pairings/pairings_generic.nimConnects generic pairing, pairing checks, Miller loops, and final exponentiation
constantine/math/pairings/miller_loops.nimDescribes the shared single-pair and multi-pair Miller-loop flow
constantine/math/pairings/lines_eval.nimExplains line values, twist placement, and sparse target-field multiplication
constantine/math/pairings/cyclotomic_subgroups.nimDefines easy final exponentiation and cyclotomic-group operations
constantine/math/pairings/pairings_bls12.nimShows the BLS12 loop and hard final-exponentiation path
constantine/math/pairings/pairings_bn.nimShows the BN correction, loop, and hard final-exponentiation path
constantine/math/pairings/miller_accumulators.nimShows streaming, merging, and finalization for multi-pairings
constantine/named/zoo_pairings.nimDispatches curve-specific pairing parameters and addition chains
constantine/named/constants/bls12_381_pairings.nimIllustrates named Miller parameters, chains, and target-group checks
constantine/signatures/bls_signatures.nimShows how BLS verification turns pairing products into a protocol result
constantine/commitments/kzg.nimShows how KZG verification consumes the pairing-check boundary