Skip to content

Commit 8552465

Browse files
authored
Compression doc (#1014)
* compression doc * minor * fix typo * fix typo * basis conversion * typo * Compression * Fix latex render and add code impl * Improved documentation * Fix typo
1 parent 0017138 commit 8552465

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

‎book/src/SUMMARY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- [Inlines](./how/optimizations/inlines.md)
2929
- [Small value optimizations](./how/optimizations/small-value.md)
3030
- [EQ optimizations](./how/optimizations/eq.md)
31+
- [Torus compression](./how/optimizations/compression.md)
3132
- [Appendix](./how/appendix/appendix.md)
3233
- [Terminology and nomenclature](./how/appendix/terminology.md)
3334
- [Multilinear extensions](./how/appendix/multilinear-extensions.md)
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# Torus-based compression
2+
3+
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 $\mathbb{F}_{q^{12}}$ over the base prime field $\mathbb{F}_q$) 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.
4+
5+
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.
6+
7+
# Methodology
8+
The pairing output has the form $f^{\frac{q^{12}-1}{r}}$, where $f \in \mathbb{F}_{q^{12}}$ is the output from the Miller loop, and $r$ is an integer such that the pairing inputs are $r$-torsion points on the BN254 curve defined over some finite extension of $\mathbb{F}_q$ - in other words, the $r^{\text{th}}$ power vanishes. We can write
9+
10+
$$
11+
f^{\frac{q^{12}-1}{r}} = \Psi_6(q^2)\frac{\Phi_6(q^2)}{r},
12+
$$
13+
where
14+
15+
$$
16+
\Phi_6(x) = x^2 - x + 1
17+
$$
18+
is the $6^{\text{th}}$ cyclotomic polynomial and
19+
20+
$$
21+
\Psi_6(x) = \frac{x^6 - 1}{\Phi_6(x)} = (x^3 + 1)(x + 1).
22+
$$
23+
24+
Let $\xi \in \mathbb{F}_{q^2}$ be a sextic non-residue and identify
25+
$$\mathbb{F}_{q^6} = \mathbb{F}_{q^2}(\xi^{\frac{1}{3}}) = \mathbb{F}_{q^2}(\tau)$$
26+
and
27+
$$\mathbb{F}_{q^{12}} = \mathbb{F}_{q^6}(\xi^{\frac{1}{2}}) = \mathbb{F}_{q^6}(\sigma),$$
28+
where $\tau = \xi^{\frac{1}{3}}$ and $\sigma = \xi^{\frac{1}{2}}$. Through this notation, we emphasise that the sets $\{1, \tau\}$ and $\{1, \sigma\}$ form $\mathbb{F}_{q^2}$-linear and $\mathbb{F}_{q^6}$-linear bases of the fields $\mathbb{F}_{q^6}$ and $\mathbb{F}_{q^{12}}$ viewed as vector spaces, respectively.
29+
30+
<!-- For example, each element $a \in \mathbb{F}_{q^{12}}$ is uniquely represented as $a = a_0 + a_1\sigma$ where the coefficients $a_i$ are in $\mathbb{F}_{q^6}$, and multiplication is implemented -->
31+
32+
It turns out that for each element $f \in \mathbb{F}_{q^{12}}$, the power $f^{\Psi_6(q^2)}$ can be written as
33+
34+
$$
35+
f^{\Psi_6(q^2)} = \frac{b + \sigma}{b - \sigma},
36+
$$
37+
where $b = c_0 + c_1\tau + c_2\tau^2 \in \mathbb{F}_6$, $c_i \in \mathbb{F}_2$, and we can recover $c_2$ from $c_0$ and $c_1$ alone.
38+
39+
Hence we can represent $f^{\Psi_6(q^2)}$ using the pair $(c_0, c_1)$ achieving a compression ratio of three, where the compression takes two steps in which we compress to the field $\mathbb{F}_{q^6}$ and $\mathbb{F}_{q^2}$, respectively.
40+
41+
## Compression to $\mathbb{F}_{q^6}$
42+
43+
We can compute $f^{\Psi_6(q^2)}$ as
44+
45+
$$
46+
f^{\Psi_6(q^2)} = f^{(q^6 - 1)(q^2 + 1)} = (f^{q^6 - 1})^{q^2 + 1}.
47+
$$
48+
49+
Write $f = a_0 + a_1\sigma$, where $a_i \in \mathbb{F}_{q^6}$, we have
50+
51+
$$
52+
f^{q^6 - 1} = \frac{(a_0 + a_1\sigma)^{q^6}}{a_0 + a_1\sigma} = \frac{(a_0 - a_1\sigma)}{a_0 + a_1\sigma} = \frac{\tilde{a} - \sigma}{\tilde{a} + \sigma},
53+
$$
54+
55+
where $\tilde{a} = \frac{a_0}{a_1}$ and the second equality follows since the $q^2$-power map generates the Galois group of the quadratic extension $\mathbb{F}_{q^{2}}(\sigma)/\mathbb{F}_{q^2}$ inside $\mathbb{F}_{q^{12}}$, so in particular $\sigma^{q^6} = -\sigma$. Hence
56+
57+
$$
58+
(f^{q^6 - 1})^{q^2 + 1} = \frac{\tilde{a} - \sigma}{\tilde{a} + \sigma}(\frac{\tilde{a} - \sigma}{\tilde{a} + \sigma})^{q^2} = \frac{\tilde{a} - \sigma}{\tilde{a}^{q^2} + \sigma}\cdot\frac{\tilde{a} + \sigma}{\tilde{a}^{q^2} - \sigma} = \frac{\tilde{a} - \sigma}{\tilde{a} + \sigma}\cdot\frac{-\tilde{a}^{q^2} - \sigma}{-\tilde{a}^{q^2} + \sigma},
59+
$$
60+
which simplifies to
61+
$$
62+
(f^{q^6 - 1})^{q^2 + 1} = \frac{\tilde{\beta} - \sigma}{\tilde{\beta} + \sigma},
63+
$$
64+
where
65+
$$
66+
\tilde{\beta} = \frac{-\tilde{a}^{q+1} + \xi}{-\tilde{a}^q + \tilde{a}} \in \mathbb{F}_{q^6}.
67+
$$
68+
69+
70+
## Compression to two elements in $\mathbb{F}_{q^2}$
71+
72+
We can write $\tilde{\beta} = c_0 + c_1\tau + c_2\tau^2$, where recall $\tau = \xi^{\frac{1}{3}}$, then we have
73+
74+
$$
75+
c_2 = \frac{3c_0^2 + \xi}{3c_1\xi},
76+
$$
77+
so we can drop $c_2$ to only use $c_0$ and $c_1$ to represent $\tilde{\beta}$.
78+
79+
## Compression and decompression
80+
For compressing a pairing value $a^{\frac{q^{12} - 1}{r}}$, first compute $f = a^{\Phi_6(q^2)}$, then compress $f^{\Psi_6(q^2)}$ to two $\mathbb{F}_{q^2}$ elements as in the previous section.
81+
82+
For decompression, first compute $\tilde{\beta} \in \mathbb{F}_{q^6}$ from two coefficients $c_0$ and $c_1$ in $\mathbb{F}_{q^2}$, where $\tilde{\beta} = c_0 + c_1\tau + c_2\tau^2$ as in the previous section. Then, compute
83+
$$
84+
a^{\frac{q^{12} - 1}{r}} = \frac{\tilde{\beta} - \sigma}{\tilde{\beta} + \sigma}
85+
$$to recover the original pairing value.
86+
87+
# Implementation Detail
88+
89+
## Basis choice
90+
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 $\mathbb{F}_{q^{12}}$ is represented as an extension $\mathbb{F}_{q^2}(\tau, \sigma^{\frac{1}{3}})$, where $\{1, \sigma\}$ is an $\mathbb{F}_{q^6}$-basis over the sub-extension $\mathbb{F}_{q^2}(\tau) = \mathbb{F}_{q^6}$. (Recall that $\tau = \xi^{\frac{1}{3}}$ and $\sigma = \xi^{\frac{1}{2}}$, where $\xi \in \mathbb{F}_{q^2}$ is a sextic non-residue.)
91+
92+
The $q^2$-power map no longer maps $\sigma^{\frac{1}{3}}$ to $-\sigma^{\frac{1}{3}}$, so naively applying the formula in the previous section on the generator $\sigma^{\frac{1}{3}}$ will cause problems. Fortunately, we can fix this easily by doing a change of basis before compression and after decompression.
93+
94+
Using the identity $\tau = \sigma^{\frac{2}{3}}$, we have
95+
96+
$$
97+
a + b\sigma = a + (b\sigma^{\frac{2}{3}})\sigma^{\frac{1}{3}} = a + (b\tau)\sigma^{\frac{1}{3}}
98+
$$
99+
100+
and hence
101+
102+
$$
103+
a + b\sigma^{\frac{1}{3}} = a + (b\tau^{-1})\sigma,
104+
$$
105+
106+
where $a$, $b$, and $\tau$ all are elements of $\mathbb{F}_{q^6} = \mathbb{F}_{q^2}(\tau)$. To convert between elements in $\mathbb{F}_{q^{12}}$ written in $\mathbb{F}_{q^6}$-bases $\{1, \sigma\}$ and $\{1, \sigma^{\frac{1}{3}}\}$, it suffices to do a multiplication or division by $\tau$, and we shall see this can be implemented entirely using arithmetics in $\mathbb{F}_{q^2}$.
107+
108+
Indeed, write $b = c_0 + c_1\tau + c_2\tau^2$, where $c_i \in \mathbb{F}_{q^2}$, we have
109+
110+
$$
111+
b\tau = c_2\xi + c_0\tau + c_1\tau^2,
112+
$$
113+
114+
and
115+
116+
$$
117+
b\tau^{-1} = c_1 + c_2\tau + c_0\xi^{-1}\tau^2.
118+
$$
119+
The conversion formulae are provided in the following code snippet.
120+
121+
```rust
122+
#[inline]
123+
pub fn fq12_to_compressible_fq12(value: Fq12) -> CompressibleFq12 {
124+
// Divide by the generator of Fq6
125+
let new_c1 = Fq6 {
126+
c0: value.c1.c1,
127+
c1: value.c1.c2,
128+
c2: value.c1.c0 * Fq6Config::NONRESIDUE.inverse().unwrap(),
129+
};
130+
131+
CompressibleFq12 {
132+
c0: value.c0,
133+
c1: new_c1,
134+
}
135+
}
136+
137+
#[inline]
138+
pub fn compressible_fq12_to_fq12(value: CompressibleFq12) -> Fq12 {
139+
// Multiply by the generator of Fq6
140+
let new_c1 = Fq6 {
141+
c0: value.c1.c2 * Fq6Config::NONRESIDUE,
142+
c1: value.c1.c0,
143+
c2: value.c1.c1,
144+
};
145+
146+
Fq12 {
147+
c0: value.c0,
148+
c1: new_c1,
149+
}
150+
}
151+
```

‎book/src/how/optimizations/optimizations.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ This section describes notable optimizations implemented in the Jolt codebase.
77
- [Inlines](./inlines.md)
88
- [Small value optimizations](./small-value.md)
99
- [EQ optimizations](./eq.md)
10+
- [Torus compression](./compression.md)

0 commit comments

Comments
 (0)