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 / extension-fields-and-towers

Extension fields and tower construction

Extension fields build larger finite fields from a curve's base field. Pairing-friendly curves use these fields for G2 coordinates and for the target group GT. The repository keeps the general tower rules in low-level field modules and supplies each curve's non-residues, embedding degree, and related constants through named configuration. See finite-field arithmetic for base operations, big integer and modular arithmetic for the number model, named curves and groups for curve selection, and the pairing engine for the pairing flow.

Tower model

Extension field layers

An extension field is a field whose elements have coordinates in a smaller field. The tower types use quadratic or cubic layers, so the degree of the complete field is the product of the degrees in its layers. Each layer uses a non-residue to ensure that its defining relation produces the intended field.

The supported named layers are:

TypeTower shapeRole
Fp2Quadratic extension of FpCommon coordinate field for G2 and the first pairing layer
Fp4Quadratic extension of Fp2Intermediate layer for some degree-12 towers
Fp6Cubic extension of Fp2Intermediate layer for many degree-12 towers and target fields of degree-6 curves
Fp12Cubic extension of Fp4Standard degree-12 target-field shape
AnyFp12Either quadratic over Fp6 or cubic over Fp4Common view for code that must accept both degree-12 shapes

The base field is Fp, the prime field attached to the curve. The scalar field Fr is a separate field and is not used as the base of these extension towers. This distinction matters because curve points and pairing values use different algebraic fields.

References: constantine/…/towers.nim• L33-65 constantine/…/properties_fields.nim• L41-58

Curve-specific tower choices

The tower shape is general, but its non-residues are curve-specific. A curve configuration supplies a base-field non-residue and an Fp2 non-residue. These values select the layer relations and also support twist arithmetic for the curve's G2 group.

The named configuration records the properties that connect a curve to its field tower:

PropertyMeaning
Base non-residueDefines the first extension relation or another curve-specific non-residue role
Fp2 non-residueDefines a later extension relation or a twist constant
Embedding degreeSelects the field degree used by GT
Sextic twistSelects the twist convention used by pairing-friendly curves

For example, BLS12-381 and BN254 use degree-12 pairing fields. BW6-761 uses a degree-6 pairing field. The low-level tower code stays shared while named constants provide the values that make each configuration valid.

References: constantine/…/config_fields_and_curves.nim• L97-133 constantine/…/config_fields_and_curves.nim• L246-287 constantine/…/properties_curves.nim• L76-107

Field operations

Core field behavior

Extension elements support the same field-level behavior as their base elements. The general layer handles creation of zero and one, comparison, addition, subtraction, negation, doubling, multiplication, squaring, and inversion by applying the matching algebra at each coordinate and using the layer's non-residue relation where needed.

These operations provide the common input to elliptic-curve formulas, hash-to-curve maps, and pairing calculations. The public low-level extension API also exposes conditional moves and conditional arithmetic so cryptographic code can keep secret-dependent choices independent of ordinary control flow.

References: constantine/…/towers.nim• L87-199 constantine/…/towers.nim• L231-279 constantine/lowlevel_extension_fields.nim• L59-93

Frobenius maps

The Frobenius map raises an element of Fp^k to its p-th power without a general exponentiation loop. Repeating the map with k computes the p^k power. This map is important in pairing work because it gives fast field endomorphisms and supports subgroup checks and twist-related maps.

The general field layer applies the map to the nested coordinates. Curve-specific coefficient tables supply the corrections required by each tower. The test suite checks the map against repeated exponentiation by the base-field modulus for degrees 2, 4, 6, and 12.

References: constantine/…/frobenius.nim• L39-99 constantine/…/zoo_frobenius.nim• L20-35 tests/…/t_fp_tower_frobenius_template.nim• L58-121

Roots and exponentiation

The extension-field layer provides square testing and square roots for Fp2, while the exponentiation layer provides variable-time exponentiation for a public exponent. These operations support point recovery and field-based curve maps as well as general field use.

Square roots are deterministic when a root exists. The Fp2 path depends on curve-specific square-root constants and on the non-residue that defines Fp2. Callers must not use the variable-time exponentiation path with a secret exponent.

References: constantine/…/square_root_fp2.nim• L19-43 constantine/…/square_root_fp2.nim• L99-145 constantine/…/exponentiations.nim• L104-162

Pairing field relationship

Target field selection

Pairing-friendly curves map two source groups into a multiplicative target group called GT. GT is a subgroup of the field Fp^k, where k is the curve's embedding degree. Named curve properties select that degree and map it to the matching tower type.

For the degree-12 curves in the repository, pairing code works with Fp12 or the tower-independent AnyFp12 view. Pairing-specific code then uses the tower operations for Miller-loop values, cyclotomic operations, Frobenius maps, and final subgroup checks. The extension-field page therefore describes the field layer below the pairing engine, not the pairing algorithm itself.

References: constantine/…/properties_curves.nim• L98-116 constantine/…/bls12_381_pairings.nim• L42-46 constantine/…/bls12_381_pairings.nim• L84-135

Source-field boundary

G1 normally uses Fp, while G2 often uses Fp2 for curves with a sextic twist. Pairing code combines these source points with values in the target extension field. The curve configuration must keep the source-field choice, twist choice, non-residues, and target-field degree consistent.

This boundary explains why a change to a field setting can affect more than field arithmetic. It can change G2 coordinates, twist maps, serialization, Frobenius constants, and pairing subgroup checks. Read named curves and groups with this page when tracing a curve-specific change.

References: constantine/…/config_fields_and_curves.nim• L97-114 constantine/…/config_fields_and_curves.nim• L269-287 constantine/…/frobenius.nim• L101-116

Element representation

Coordinate serialization

An extension element is represented as nested coordinates. Fp2 has two base-field coordinates. Higher fields nest those coordinates according to their tower shape. The I/O layer converts the full coordinate tree to and from hexadecimal values while preserving the selected coordinate order.

The representation is part of the boundary between field arithmetic and encoded curve data. A reader who works on codecs or test vectors must first identify the curve's tower shape before interpreting a flat list of coordinates.

References: constantine/…/io_extfields.nim• L31-53 constantine/…/io_extfields.nim• L55-112

Module boundaries

The low-level extension API is a bridge between generic field algebra and named curve data. constantine/math/extension_fields.nim gathers tower, square-root, and exponentiation features. constantine/lowlevel_extension_fields.nim re-exports the named types and common operations for low-level consumers. Named algebra and property modules bind a static curve name to its modulus and extension settings.

The low-level API does not provide a stable protocol contract. Protocol code should use the higher-level interfaces that define its own safety and input rules. Use the low-level modules to understand the field behavior or to support a higher-level operation.

References: constantine/…/extension_fields.nim• L9-16 constantine/lowlevel_extension_fields.nim• L16-52 constantine/…/algebras.nim• L9-10

Change checklist

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

  • A change to a curve modulus or either non-residue must be checked against derived field constants and the curve's Frobenius coefficient tables.
  • A change to an embedding degree must be traced to the GT type, pairing-specific field operations, and subgroup checks.
  • A change to the tower shape must be traced to nested coordinate order, hexadecimal I/O, curve codecs, and any code using AnyFp12.
  • A change to the sextic twist must be traced to G2 field selection and the untwist-Frobenius-twist maps.
  • A new named pairing curve needs matching pairing and Frobenius constants before generic tower code can produce valid pairing results.

Important failure modes

TriggerConsequenceGuard
A curve uses the wrong base or Fp2 non-residueMultiplication and square roots describe a different field. Curve and pairing values no longer match.Keep the non-residues in the named curve configuration and validate them with tower and curve tests.
Frobenius coefficients do not match the selected towerFrobenius results differ from a^p. Pairing subgroup checks can reject valid values or accept invalid ones.Check Frobenius against exponentiation for every configured tower degree.
A caller mixes Fr with an Fp extensionScalars are treated as coordinates in the wrong field. The resulting curve or pairing computation is invalid.Use Fp[Curve] for extension towers and Fr[Curve] for scalar arithmetic.
A producer and consumer assume different degree-12 tower shapesCoordinate parsing or pairing values are read in the wrong nesting order.Use the curve's declared tower type and the shared extension-field I/O rules.
Secret data reaches variable-time extension exponentiationTiming or memory behavior can reveal exponent bits.Use a constant-time operation for secret exponents and reserve pow_vartime for public values.

Key files

FileWhy to read it
constantine/math/extension_fields/towers.nimDefines the shared quadratic and cubic layers and the named Fp2 through Fp12 tower types.
constantine/math/extension_fields.nimShows the extension-field feature surface assembled for consumers.
constantine/named/config_fields_and_curves.nimShows how pairing-friendly curves declare non-residues, embedding degree, and twist choice.
constantine/named/properties_curves.nimShows how curve names expose tower and target-field properties.
constantine/math/endomorphisms/frobenius.nimShows the relationship between generic tower structure and curve-specific Frobenius data.
constantine/math/io/io_extfields.nimShows how nested extension coordinates cross the encoded-data boundary.
constantine/lowlevel_extension_fields.nimShows the low-level API boundary and the operations exported to consumers.
constantine/named/constants/bls12_381_pairings.nimIllustrates how a degree-12 target field is used by a concrete pairing configuration.
tests/math_extension_fields/t_fp_tower_template.nimSummarizes the algebraic guarantees checked across configured tower degrees.
tests/math_extension_fields/t_fp_tower_frobenius_template.nimSummarizes the equivalence checks for Frobenius maps and field powers.