Torus-based compression
We implement a torus-based compression method to compress an output of the pairing computation for the BN254 curve (viewed as an element in a degree 12 extension over the base prime field ) to two elements in a degree 2 sub-extension over the same prime field, thus achieving a threefold compression ratio with no information loss. In other words, the decompressed value recovers exactly the pairing value computed without compression.
Recall that the pairing computation follows two steps - the Miller loop and the final exponentiation. The compression method requires only making changes to the final exponentiation step. The compression overhead turns out to be insignificant for applications in Jolt.
Methodology
The pairing output has the form , where is the output from the Miller loop, and is an integer such that the pairing inputs are -torsion points on the BN254 curve defined over some finite extension of - in other words, the power vanishes. We can write
where
is the cyclotomic polynomial and
(In practice, a common optimization is that instead of exponentiating by , one raises to a multiple of and an integer coprime to the order of the multiplicative group of the target field. This of course is a 1-to-1 map of the underlying field and has the advantage that one may use linear-algebraic techniques to compute exponentiation by the multiple more efficiently (e.g. see how this is applied to the BN254 curve in Faster Hashing to ). This optimization does not affect the compression method, so we consider only the case of raising to a -power for the rest of the discussion.)
Let be a sextic non-residue and identify and where and . Through this notation, we emphasise that the sets and form -linear and -linear bases of the fields and viewed as vector spaces, respectively.
It turns out that for each element , the power can be written as
where , , and we can recover from and alone.
Hence we can represent using the pair achieving a compression ratio of three, where the compression takes two steps in which we compress to the field and , respectively.
Compression to
We can compute as
Write , where , we have
where and the second equality follows since the -power map generates the Galois group of the quadratic extension inside , so in particular . Hence
which simplifies to where
Compression to two elements in
We can write , where recall , then we have
so we can drop to only use and to represent .
Compression and decompression
For compressing a pairing value , first compute , then compress to two elements as in the previous section.
For decompression, first compute from two coefficients and in , where as in the previous section. Then, compute
to recover the original pairing value.
Implementation Detail
Basis choice
Different choices of basis vectors for an extension field affect the complexity of field operations within the extension field. In Arkworks, for field arithmetic optimization, the field is represented as an extension , where is an -basis over the sub-extension . (Recall that and , where is a sextic non-residue.)
The -power map no longer maps to , so naively applying the formula in the previous section on the generator will cause problems. Fortunately, we can fix this easily by doing a change of basis before compression and after decompression.
Using the identity , we have
and hence
where , , and all are elements of . To convert between elements in written in -bases and , it suffices to do a multiplication or division by , and we shall see this can be implemented entirely using arithmetics in .
Indeed, write , where , we have
and
The conversion formulae are provided in the following code snippet.
#![allow(unused)] fn main() { #[inline] pub fn fq12_to_compressible_fq12(value: Fq12) -> CompressibleFq12 { // Divide by the generator of Fq6 let new_c1 = Fq6 { c0: value.c1.c1, c1: value.c1.c2, c2: value.c1.c0 * Fq6Config::NONRESIDUE.inverse().unwrap(), }; CompressibleFq12 { c0: value.c0, c1: new_c1, } } #[inline] pub fn compressible_fq12_to_fq12(value: CompressibleFq12) -> Fq12 { // Multiply by the generator of Fq6 let new_c1 = Fq6 { c0: value.c1.c2 * Fq6Config::NONRESIDUE, c1: value.c1.c0, c2: value.c1.c1, }; Fq12 { c0: value.c0, c1: new_c1, } } }
Switching order of exponentiation
In the case of compression, one needs to first raise to a power of before then raising to a power of (recall that the compression method amounts to using two elements in to represent a -power). However, the final exponentiation is significantly faster if we switch the order and first raise to the power of instead, as in the uncompressed case. This is because elements of -powers have the special properties that they are in a cyclotomic subgroup, so optimized squaring and inversion are much cheaper. Fortunately, the overhead in practice is insignificant for multi-pairing computation, which is the use case for Dory, because the overhead in the final exponentiation step is amortized across the multi-Miller loop computations.