Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/curve25519-dalek/docs/parallel-formulas.md # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / curve25519-dalek / docs / parallel-formulas.md` 333 lines · 16384 bytes Raw Blame History Rendered 1 Vectorized implementations of field and point operations, using a 2 modification of the 4-way parallel formulas of Hisil, Wong, Carter, 3 and Dawson. 4 5 These notes explain the parallel formulas and our strategy for using 6 them with SIMD operations. There are two backend implementations: one 7 using AVX2, and the other using AVX512-IFMA. 8 9 # Overview 10 11 The 2008 paper [_Twisted Edwards Curves Revisited_][hwcd08] by Hisil, 12 Wong, Carter, and Dawson (HWCD) introduced the “extended coordinates” 13 and mixed-model representations which are used by most Edwards curve 14 implementations. 15 16 However, they also describe 4-way parallel formulas for point addition 17 and doubling: a unified addition algorithm taking an effective 18 \\(2\mathbf M + 1\mathbf D\\), a doubling algorithm taking an 19 effective \\(1\mathbf M + 1\mathbf S\\), and a dedicated (i.e., for 20 distinct points) addition algorithm taking an effective \\(2 \mathbf M 21 \\). They compare these formulas with a 2-way parallel variant of the 22 Montgomery ladder. 23 24 Unlike their serial formulas, which are used widely, their parallel 25 formulas do not seem to have been implemented in software before. The 26 2-way parallel Montgomery ladder was used in 2015 by Tung Chou's 27 `sandy2x` implementation. Curiously, however, although the [`sandy2x` 28 paper][sandy2x] also implements Edwards arithmetic, and cites HWCD08, 29 it doesn't mention their parallel Edwards formulas. 30 A 2015 paper by Hernández and López describes an AVX2 implementation 31 of X25519. Neither the paper nor the code are publicly available, but 32 it apparently gives only a [slight speedup][avx2trac], suggesting that 33 it uses a 4-way parallel Montgomery ladder rather than parallel 34 Edwards formulas. 35 36 The reason may be that HWCD08 describe their formulas as operating on 37 four independent processors, which would make a software 38 implementation impractical: all of the operations are too low-latency 39 to effectively synchronize. But a closer inspection reveals that the 40 (more expensive) multiplication and squaring steps are uniform, while 41 the instruction divergence occurs in the (much cheaper) addition and 42 subtraction steps. This means that a SIMD implementation can perform 43 the expensive steps uniformly, and handle divergence in the 44 inexpensive steps using masking. 45 46 These notes describe modifications to the original parallel formulas 47 to allow a SIMD implementation, and this module contains 48 implementations of the modified formulas targeting either AVX2 or 49 AVX512-IFMA. 50 51 # Parallel formulas in HWCD'08 52 53 The doubling formula is presented in the HWCD paper as follows: 54 55 | Cost | Processor 1 | Processor 2 | Processor 3 | Processor 4 | 56 |------------------|--------------------------------|--------------------------------|--------------------------------|--------------------------------| 57 | | idle | idle | idle | \\( R\_1 \gets X\_1 + Y\_1 \\) | 58 | \\(1\mathbf S\\) | \\( R\_2 \gets X\_1\^2 \\) | \\( R\_3 \gets Y\_1\^2 \\) | \\( R\_4 \gets Z\_1\^2 \\) | \\( R\_5 \gets R\_1\^2 \\) | 59 | | \\( R\_6 \gets R\_2 + R\_3 \\) | \\( R\_7 \gets R\_2 - R\_3 \\) | \\( R\_4 \gets 2 R\_4 \\) | idle | 60 | | idle | \\( R\_1 \gets R\_4 + R\_7 \\) | idle | \\( R\_2 \gets R\_6 - R\_5 \\) | 61 | \\(1\mathbf M\\) | \\( X\_3 \gets R\_1 R\_2 \\) | \\( Y\_3 \gets R\_6 R\_7 \\) | \\( T\_3 \gets R\_2 R\_6 \\) | \\( Z\_3 \gets R\_1 R\_7 \\) | 62 63 and the unified addition algorithm is presented as follows: 64 65 | Cost | Processor 1 | Processor 2 | Processor 3 | Processor 4 | 66 |------------------|--------------------------------|--------------------------------|--------------------------------|--------------------------------| 67 | | \\( R\_1 \gets Y\_1 - X\_1 \\) | \\( R\_2 \gets Y\_2 - X\_2 \\) | \\( R\_3 \gets Y\_1 + X\_1 \\) | \\( R\_4 \gets Y\_2 + X\_2 \\) | 68 | \\(1\mathbf M\\) | \\( R\_5 \gets R\_1 R\_2 \\) | \\( R\_6 \gets R\_3 R\_4 \\) | \\( R\_7 \gets T\_1 T\_2 \\) | \\( R\_8 \gets Z\_1 Z\_2 \\) | 69 | \\(1\mathbf D\\) | idle | idle | \\( R\_7 \gets k R\_7 \\) | \\( R\_8 \gets 2 R\_8 \\) | 70 | | \\( R\_1 \gets R\_6 - R\_5 \\) | \\( R\_2 \gets R\_8 - R\_7 \\) | \\( R\_3 \gets R\_8 + R\_7 \\) | \\( R\_4 \gets R\_6 + R\_5 \\) | 71 | \\(1\mathbf M\\) | \\( X\_3 \gets R\_1 R\_2 \\) | \\( Y\_3 \gets R\_3 R\_4 \\) | \\( T\_3 \gets R\_1 R\_4 \\) | \\( Z\_3 \gets R\_2 R\_3 \\) | 72 73 Here \\(\mathbf M\\) and \\(\mathbf S\\) represent the cost of 74 multiplication and squaring of generic field elements, \\(\mathbf D\\) 75 represents the cost of multiplication by a curve constant (in this 76 case \\( k = 2d \\)). 77 78 Notice that the \\(1\mathbf M\\) and \\(1\mathbf S\\) steps are 79 uniform. The non-uniform steps are all inexpensive additions or 80 subtractions, with the exception of the multiplication by the curve 81 constant \\(k = 2d\\): 82 $$ 83 R\_7 \gets 2 d R\_7. 84 $$ 85 86 HWCD suggest parallelising this step by breaking \\(k = 2d\\) into four 87 parts as \\(k = k_0 + 2\^n k_1 + 2\^{2n} k_2 + 2\^{3n} k_3 \\) and 88 computing \\(k_i R_7 \\) in parallel. This is quite awkward, but if 89 the curve constant is a ratio \\( d = d\_1/d\_2 \\), then projective 90 coordinates allow us to instead compute 91 $$ 92 (R\_5, R\_6, R\_7, R\_8) \gets (d\_2 R\_5, d\_2 R\_6, 2d\_1 R\_7, d\_2 R\_8). 93 $$ 94 This can be performed as a uniform multiplication by a vector of 95 constants, and if \\(d\_1, d\_2\\) are small, it is relatively 96 inexpensive. (This trick was suggested by Mike Hamburg). 97 In the Curve25519 case, we have 98 $$ 99 d = \frac{d\_1}{d\_2} = \frac{-121665}{121666}; 100 $$ 101 Since \\(2 \cdot 121666 < 2\^{18}\\), all the constants above fit (up 102 to sign) in 32 bits, so this can be done in parallel as four 103 multiplications by small constants \\( (121666, 121666, 2\cdot 121665, 104 2\cdot 121666) \\), followed by a negation to compute \\( - 2\cdot 121665\\). 105 106 # Modified parallel formulas 107 108 Using the modifications sketched above, we can write SIMD-friendly 109 versions of the parallel formulas as follows. To avoid confusion with 110 the original formulas, temporary variables are named \\(S\\) instead 111 of \\(R\\) and are in static single-assignment form. 112 113 ## Addition 114 115 To add points 116 \\(P_1 = (X_1 : Y_1 : Z_1 : T_1) \\) 117 and 118 \\(P_2 = (X_2 : Y_2 : Z_2 : T_2 ) \\), 119 we compute 120 $$ 121 \begin{aligned} 122 (S\_0 &&,&& S\_1 &&,&& S\_2 &&,&& S\_3 ) 123 &\gets 124 (Y\_1 - X\_1&&,&& Y\_1 + X\_1&&,&& Y\_2 - X\_2&&,&& Y\_2 + X\_2) 125 \\\\ 126 (S\_4 &&,&& S\_5 &&,&& S\_6 &&,&& S\_7 ) 127 &\gets 128 (S\_0 \cdot S\_2&&,&& S\_1 \cdot S\_3&&,&& Z\_1 \cdot Z\_2&&,&& T\_1 \cdot T\_2) 129 \\\\ 130 (S\_8 &&,&& S\_9 &&,&& S\_{10} &&,&& S\_{11} ) 131 &\gets 132 (d\_2 \cdot S\_4 &&,&& d\_2 \cdot S\_5 &&,&& 2 d\_2 \cdot S\_6 &&,&& 2 d\_1 \cdot S\_7 ) 133 \\\\ 134 (S\_{12} &&,&& S\_{13} &&,&& S\_{14} &&,&& S\_{15}) 135 &\gets 136 (S\_9 - S\_8&&,&& S\_9 + S\_8&&,&& S\_{10} - S\_{11}&&,&& S\_{10} + S\_{11}) 137 \\\\ 138 (X\_3&&,&& Y\_3&&,&& Z\_3&&,&& T\_3) 139 &\gets 140 (S\_{12} \cdot S\_{14}&&,&& S\_{15} \cdot S\_{13}&&,&& S\_{15} \cdot S\_{14}&&,&& S\_{12} \cdot S\_{13}) 141 \end{aligned} 142 $$ 143 to obtain \\( P\_3 = (X\_3 : Y\_3 : Z\_3 : T\_3) = P\_1 + P\_2 \\). 144 This costs \\( 2\mathbf M + 1 \mathbf D\\). 145 146 ## Readdition 147 148 If the point \\( P\_2 = (X\_2 : Y\_2 : Z\_2 : T\_2) \\) is fixed, we 149 can cache the multiplication of the curve constants by computing 150 $$ 151 \begin{aligned} 152 (S\_2\' &&,&& S\_3\' &&,&& Z\_2\' &&,&& T\_2\' ) 153 &\gets 154 (d\_2 \cdot (Y\_2 - X\_2)&&,&& d\_2 \cdot (Y\_1 + X\_1)&&,&& 2d\_2 \cdot Z\_2 &&,&& 2d\_1 \cdot T\_2). 155 \end{aligned} 156 $$ 157 This costs \\( 1\mathbf D\\); with \\( (S\_2\', S\_3\', Z\_2\', T\_2\')\\) 158 in hand, the addition formulas above become 159 $$ 160 \begin{aligned} 161 (S\_0 &&,&& S\_1 &&,&& Z\_1 &&,&& T\_1 ) 162 &\gets 163 (Y\_1 - X\_1&&,&& Y\_1 + X\_1&&,&& Z\_1 &&,&& T\_1) 164 \\\\ 165 (S\_8 &&,&& S\_9 &&,&& S\_{10} &&,&& S\_{11} ) 166 &\gets 167 (S\_0 \cdot S\_2\' &&,&& S\_1 \cdot S\_3\'&&,&& Z\_1 \cdot Z\_2\' &&,&& T\_1 \cdot T\_2\') 168 \\\\ 169 (S\_{12} &&,&& S\_{13} &&,&& S\_{14} &&,&& S\_{15}) 170 &\gets 171 (S\_9 - S\_8&&,&& S\_9 + S\_8&&,&& S\_{10} - S\_{11}&&,&& S\_{10} + S\_{11}) 172 \\\\ 173 (X\_3&&,&& Y\_3&&,&& Z\_3&&,&& T\_3) 174 &\gets 175 (S\_{12} \cdot S\_{14}&&,&& S\_{15} \cdot S\_{13}&&,&& S\_{15} \cdot S\_{14}&&,&& S\_{12} \cdot S\_{13}) 176 \end{aligned} 177 $$ 178 which costs only \\( 2\mathbf M \\). This precomputation is 179 essentially similar to the precomputation that HWCD suggest for their 180 serial formulas. Because the cost of precomputation and then 181 readdition is the same as addition, it's sufficient to only 182 implement caching and readdition. 183 184 ## Doubling 185 186 The non-uniform portions of the (re)addition formulas have a fairly 187 regular structure. Unfortunately, this is not the case for the 188 doubling formulas, which are much less nice. 189 190 To double a point \\( P = (X\_1 : Y\_1 : Z\_1 : T\_1) \\), we compute 191 $$ 192 \begin{aligned} 193 (X\_1 &&,&& Y\_1 &&,&& Z\_1 &&,&& S\_0) 194 &\gets 195 (X\_1 &&,&& Y\_1 &&,&& Z\_1 &&,&& X\_1 + Y\_1) 196 \\\\ 197 (S\_1 &&,&& S\_2 &&,&& S\_3 &&,&& S\_4 ) 198 &\gets 199 (X\_1\^2 &&,&& Y\_1\^2&&,&& Z\_1\^2 &&,&& S\_0\^2) 200 \\\\ 201 (S\_5 &&,&& S\_6 &&,&& S\_8 &&,&& S\_9 ) 202 &\gets 203 (S\_1 + S\_2 &&,&& S\_1 - S\_2 &&,&& S\_1 + 2S\_3 - S\_2 &&,&& S\_1 + S\_2 - S\_4) 204 \\\\ 205 (X\_3 &&,&& Y\_3 &&,&& Z\_3 &&,&& T\_3 ) 206 &\gets 207 (S\_8 \cdot S\_9 &&,&& S\_5 \cdot S\_6 &&,&& S\_8 \cdot S\_6 &&,&& S\_5 \cdot S\_9) 208 \end{aligned} 209 $$ 210 to obtain \\( P\_3 = (X\_3 : Y\_3 : Z\_3 : T\_3) = \[2\]P\_1 \\). 211 212 The intermediate step between the squaring and multiplication requires 213 a long chain of additions. For the IFMA-based implementation, this is not a problem; for the AVX2-based implementation, it is, but with some care and finesse, it's possible to arrange the computation without requiring an intermediate reduction. 214 215 # Implementation 216 217 These formulas aren't specific to a particular representation of field 218 element vectors, whose optimum choice is determined by the details of 219 the instruction set. However, it's not possible to perfectly separate 220 the implementation of the field element vectors from the 221 implementation of the point operations. Instead, the [`avx2`] and 222 [`ifma`] backends provide `ExtendedPoint` and `CachedPoint` types, and 223 the [`scalar_mul`] code uses one of the backend types by a type alias. 224 225 # Comparison to non-vectorized formulas 226 227 In theory, the parallel Edwards formulas seem to allow a \\(4\\)-way 228 speedup from parallelism. However, an actual vectorized 229 implementation has several slowdowns that cut into this speedup. 230 231 First, the parallel formulas can only use the available vector 232 multiplier. For AVX2, this is a \\( 32 \times 32 \rightarrow 64 233 \\)-bit integer multiplier, so the speedup from vectorization must 234 overcome the disadvantage of losing the \\( 64 \times 64 \rightarrow 235 128\\)-bit (serial) integer multiplier. The effect of this slowdown 236 is microarchitecture-dependent, since it requires accounting for the 237 total number of multiplications and additions and their relative 238 costs. IFMA allows using a \\( 52 \times 52 \rightarrow 104 \\)-bit 239 multiplier, but the high and low halves need to be computed 240 separately, and the reduction requires extra work because it's not 241 possible to pre-multiply by \\(19\\). 242 243 Second, the parallel doubling formulas incur both a theoretical and 244 practical slowdown. The parallel formulas described above work on the 245 \\( \mathbb P\^3 \\) “extended” coordinates. The \\( \mathbb P\^2 \\) 246 model introduced earlier by [Bernstein, Birkner, Joye, Lange, and 247 Peters][bbjlp08] allows slightly faster doublings, so HWCD suggest 248 mixing coordinate systems while performing scalar multiplication 249 (attributing the idea to [a 1998 paper][cmo98] by Cohen, Miyagi, and 250 Ono). The \\( T \\) coordinate is not required for doublings, so when 251 doublings are followed by doublings, its computation can be skipped. 252 More details on this approach and the different coordinate systems can 253 be found in the [`curve_models` module documentation][curve_models]. 254 255 Unfortunately, this optimization is not compatible with the parallel 256 formulas, which cannot save time by skipping a single variable, so the 257 parallel doubling formulas do slightly more work when counting the 258 total number of field multiplications and squarings. 259 260 In addition, the parallel doubling formulas have a less regular 261 pattern of additions and subtractions than the parallel addition 262 formulas, so the vectorization overhead is proportionately greater. 263 Both the parallel addition and parallel doubling formulas also require 264 some shuffling to rearrange data within the vectors, which places more 265 pressure on the shuffle unit than is desirable. 266 267 This means that the speedup from using a vectorized implementation of 268 parallel Edwards formulas is likely to be greatest in applications 269 that do fewer doublings and more additions (like a large multiscalar 270 multiplication) rather than applications that do fewer additions and 271 more doublings (like a double-base scalar multiplication). 272 273 Third, Amdahl's law says that the speedup is limited to the portion 274 which can be parallelized. Normally, the field multiplications 275 dominate the cost of point operations, but with the IFMA backend, the 276 multiplications are so fast that the non-parallel additions end up as 277 a significant portion of the total time. 278 279 Fourth, current Intel CPUs perform thermal throttling when using wide 280 vector instructions. A detailed description can be found in §15.26 of 281 [the Intel Optimization Manual][intel], but using wide vector 282 instructions prevents the core from operating at higher frequencies. 283 The core can return to the higher-frequency state after 2 284 milliseconds, but this timer is reset every time high-power 285 instructions are used. 286 287 Any speedup from vectorization therefore has to be weighed against a 288 slowdown for the next few million instructions. For a mixed workload, 289 where point operations are interspersed with other tasks, this can 290 reduce overall performance. This implementation is therefore probably 291 not suitable for basic applications, like signatures, but is 292 worthwhile for complex applications, like zero-knowledge proofs, which 293 do sustained work. 294 295 # Future work 296 297 There are several directions for future improvement: 298 299 * Using the vectorized field arithmetic code to parallelize across 300 point operations rather than within a single point operation. This 301 is less flexible, but would give a speedup both from allowing use of 302 the faster mixed-model arithmetic and from reducing shuffle 303 pressure. One approach in this direction would be to implement 304 batched scalar-point operations using vectors of points (AoSoA 305 layout). This less generally useful but would give a speedup for 306 Bulletproofs. 307 308 * Extending the IFMA implementation to use the full width of AVX512, 309 either handling the extra parallelism internally to a single point 310 operation (by using a 2-way parallel implementation of field 311 arithmetic instead of a wordsliced one), or externally, 312 parallelizing across point operations. Internal parallelism would 313 be preferable but might require too much shuffle pressure. For now, 314 the only available CPU which runs IFMA operations executes them at 315 256-bits wide anyways, so this isn't yet important. 316 317 * Generalizing the implementation to NEON instructions. The current 318 point arithmetic code is written in terms of field element vectors, 319 which are in turn implemented using platform SIMD vectors. It 320 should be possible to write an alternate implementation of the 321 `FieldElement2625x4` using NEON without changing the point 322 arithmetic. NEON has 128-bit vectors rather than 256-bit vectors, 323 but this may still be worthwhile compared to a serial 324 implementation. 325 326 327 [sandy2x]: https://eprint.iacr.org/2015/943.pdf 328 [avx2trac]: https://trac.torproject.org/projects/tor/ticket/8897#comment:28 329 [hwcd08]: https://www.iacr.org/archive/asiacrypt2008/`53500329`/`53500329`.pdf 330 [curve_models]: https://docs.rs/curve25519-dalek/latest/curve25519-dalek/backend/serial/curve_models/index.html 331 [bbjlp08]: https://eprint.iacr.org/2008/013 332 [cmo98]: https://link.springer.com/content/pdf/10.1007%2F3-540-49649-1_6.pdf 333 [intel]: https://software.intel.com/sites/default/files/managed/9e/bc/64-ia-32-architectures-optimization-manual.pdf