aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / curve25519-dalek / docs / ifma-notes.md
| 1 | An AVX512-IFMA implementation of the vectorized point operation |
| 2 | strategy. |
| 3 | |
| 4 | # IFMA instructions |
| 5 | |
| 6 | AVX512-IFMA is an extension to AVX-512 consisting of two instructions: |
| 7 | |
| 8 | * `vpmadd52luq`: packed multiply of unsigned 52-bit integers and add |
| 9 | the low 52 product bits to 64-bit accumulators; |
| 10 | * `vpmadd52huq`: packed multiply of unsigned 52-bit integers and add |
| 11 | the high 52 product bits to 64-bit accumulators; |
| 12 | |
| 13 | These operate on 64-bit lanes of their source vectors, taking the low |
| 14 | 52 bits of each lane of each source vector, computing the 104-bit |
| 15 | products of each pair, and then adding either the high or low 52 bits |
| 16 | of the 104-bit products to the 64-bit lanes of the destination vector. |
| 17 | The multiplication is performed internally by reusing circuitry for |
| 18 | floating-point arithmetic. Although these instructions are part of |
| 19 | AVX512, the AVX512VL (vector length) extension (present whenever IFMA |
| 20 | is) allows using them with 512, 256, or 128-bit operands. |
| 21 | |
| 22 | This provides a major advantage to vectorized integer operations: |
| 23 | previously, vector operations could only use a \\(32 \times 32 |
| 24 | \rightarrow 64\\)-bit multiplier, while serial code could use a |
| 25 | \\(64\times 64 \rightarrow 128\\)-bit multiplier. |
| 26 | |
| 27 | ## IFMA for big-integer multiplications |
| 28 | |
| 29 | A detailed example of the intended use of the IFMA instructions can be |
| 30 | found in a 2016 paper by Gueron and Krasnov, [_Accelerating Big |
| 31 | Integer Arithmetic Using Intel IFMA Extensions_][2016_gueron_krasnov]. |
| 32 | The basic idea is that multiplication of large integers (such as 1024, |
| 33 | 2048, or more bits) can be performed as follows. |
| 34 | |
| 35 | First, convert a “packed” 64-bit representation |
| 36 | \\[ |
| 37 | \begin{aligned} |
| 38 | x &= x'_0 + x'_1 2^{64} + x'_2 2^{128} + \cdots \\\\ |
| 39 | y &= y'_0 + y'_1 2^{64} + y'_2 2^{128} + \cdots |
| 40 | \end{aligned} |
| 41 | \\] |
| 42 | into a “redundant” 52-bit representation |
| 43 | \\[ |
| 44 | \begin{aligned} |
| 45 | x &= x_0 + x_1 2^{52} + x_2 2^{104} + \cdots \\\\ |
| 46 | y &= y_0 + y_1 2^{52} + y_2 2^{104} + \cdots |
| 47 | \end{aligned} |
| 48 | \\] |
| 49 | with each \\(x_i, y_j\\) in a 64-bit lane. |
| 50 | |
| 51 | Writing the product as \\(z = z_0 + z_1 2^{52} + z_2 2^{104} + \cdots\\), |
| 52 | the “schoolbook” multiplication strategy gives |
| 53 | \\[ |
| 54 | \begin{aligned} |
| 55 | &z_0 &&=& x_0 & y_0 & & & & & & & & \\\\ |
| 56 | &z_1 &&=& x_1 & y_0 &+ x_0 & y_1 & & & & & & \\\\ |
| 57 | &z_2 &&=& x_2 & y_0 &+ x_1 & y_1 &+ x_0 & y_2 & & & & \\\\ |
| 58 | &z_3 &&=& x_3 & y_0 &+ x_2 & y_1 &+ x_1 & y_2 &+ x_0 & y_3 & & \\\\ |
| 59 | &z_4 &&=& \vdots\\;&\\;\vdots &+ x_3 & y_1 &+ x_2 & y_2 &+ x_1 & y_3 &+ \cdots& \\\\ |
| 60 | &z_5 &&=& & & \vdots\\;&\\;\vdots &+ x_3 & y_2 &+ x_2 & y_3 &+ \cdots& \\\\ |
| 61 | &z_6 &&=& & & & & \vdots\\;&\\;\vdots &+ x_3 & y_3 &+ \cdots& \\\\ |
| 62 | &z_7 &&=& & & & & & & \vdots\\;&\\;\vdots &+ \cdots& \\\\ |
| 63 | &\vdots&&=& & & & & & & & & \ddots& \\\\ |
| 64 | \end{aligned} |
| 65 | \\] |
| 66 | Notice that the product coefficient \\(z_k\\), representing the value |
| 67 | \\(z_k 2^{52k}\\), is the sum of all product terms |
| 68 | \\( |
| 69 | (x_i 2^{52 i}) (y_j 2^{52 j}) |
| 70 | \\) |
| 71 | with \\(k = i + j\\). |
| 72 | Write the IFMA operators \\(\mathrm{lo}(a,b)\\), denoting the low |
| 73 | \\(52\\) bits of \\(ab\\), and |
| 74 | \\(\mathrm{hi}(a,b)\\), denoting the high \\(52\\) bits of |
| 75 | \\(ab\\). |
| 76 | Now we can rewrite the product terms as |
| 77 | \\[ |
| 78 | \begin{aligned} |
| 79 | (x_i 2^{52 i}) (y_j 2^{52 j}) |
| 80 | &= |
| 81 | 2^{52 (i+j)}( |
| 82 | \mathrm{lo}(x_i, y_j) + |
| 83 | \mathrm{hi}(x_i, y_j) 2^{52} |
| 84 | ) |
| 85 | \\\\ |
| 86 | &= |
| 87 | \mathrm{lo}(x_i, y_j) 2^{52 (i+j)} + |
| 88 | \mathrm{hi}(x_i, y_j) 2^{52 (i+j+1)}. |
| 89 | \end{aligned} |
| 90 | \\] |
| 91 | This means that the low half of \\(x_i y_j\\) can be accumulated onto |
| 92 | the product limb \\(z_{i+j}\\) and the high half can be directly |
| 93 | accumulated onto the next-higher product limb \\(z_{i+j+1}\\) with no |
| 94 | additional operations. This allows rewriting the schoolbook |
| 95 | multiplication into the form |
| 96 | \\[ |
| 97 | \begin{aligned} |
| 98 | &z_0 &&=& \mathrm{lo}(x_0,&y_0) & & & & & & & & & & \\\\ |
| 99 | &z_1 &&=& \mathrm{lo}(x_1,&y_0) &+\mathrm{hi}(x_0,&y_0) &+\mathrm{lo}(x_0,&y_1) & & & & & & \\\\ |
| 100 | &z_2 &&=& \mathrm{lo}(x_2,&y_0) &+\mathrm{hi}(x_1,&y_0) &+\mathrm{lo}(x_1,&y_1) &+\mathrm{hi}(x_0,&y_1) &+\mathrm{lo}(x_0,&y_2) & & \\\\ |
| 101 | &z_3 &&=& \mathrm{lo}(x_3,&y_0) &+\mathrm{hi}(x_2,&y_0) &+\mathrm{lo}(x_2,&y_1) &+\mathrm{hi}(x_1,&y_1) &+\mathrm{lo}(x_1,&y_2) &+ \cdots& \\\\ |
| 102 | &z_4 &&=& \vdots\\;&\\;\vdots &+\mathrm{hi}(x_3,&y_0) &+\mathrm{lo}(x_3,&y_1) &+\mathrm{hi}(x_2,&y_1) &+\mathrm{lo}(x_2,&y_2) &+ \cdots& \\\\ |
| 103 | &z_5 &&=& & & \vdots\\;&\\;\vdots & \vdots\\;&\\;\vdots &+\mathrm{hi}(x_3,&y_1) &+\mathrm{lo}(x_3,&y_2) &+ \cdots& \\\\ |
| 104 | &z_6 &&=& & & & & & & \vdots\\;&\\;\vdots & \vdots\\;&\\;\vdots &+ \cdots& \\\\ |
| 105 | &\vdots&&=& & & & & & & & & & & \ddots& \\\\ |
| 106 | \end{aligned} |
| 107 | \\] |
| 108 | Gueron and Krasnov implement multiplication by constructing vectors |
| 109 | out of the columns of this diagram, so that the source operands for |
| 110 | the IFMA instructions are of the form \\((x_0, x_1, x_2, \ldots)\\) |
| 111 | and \\((y_i, y_i, y_i, \ldots)\\). |
| 112 | After performing the multiplication, |
| 113 | the product terms \\(z_i\\) are then repacked into a 64-bit representation. |
| 114 | |
| 115 | ## An alternative strategy |
| 116 | |
| 117 | The strategy described above is aimed at big-integer multiplications, |
| 118 | such as 1024, 2048, or 4096 bits, which would be used for applications |
| 119 | like RSA. However, elliptic curve cryptography uses much smaller field |
| 120 | sizes, such as 256 or 384 bits, so a different strategy is needed. |
| 121 | |
| 122 | The parallel Edwards formulas provide parallelism at the level of the |
| 123 | formulas for curve operations. This means that instead of scanning |
| 124 | through the terms of the source operands and parallelizing *within* a |
| 125 | field element (as described above), we can arrange the computation in |
| 126 | product-scanning form and parallelize *across* field elements (as |
| 127 | described below). |
| 128 | |
| 129 | The parallel Edwards |
| 130 | formulas provide 4-way parallelism, so they can be implemented using |
| 131 | 256-bit vectors using a single 64-bit lane for each element, or using |
| 132 | 512-bit vectors using two 64-bit lanes. |
| 133 | The only available CPU supporting IFMA (the |
| 134 | i3-8121U) executes 512-bit IFMA instructions at half rate compared to |
| 135 | 256-bit instructions, so for now there's no throughput advantage to |
| 136 | using 512-bit IFMA instructions, and this implementation uses 256-bit |
| 137 | vectors. |
| 138 | |
| 139 | To extend this to 512-bit vectors, it's only only necessary to achieve |
| 140 | 2-way parallelism, and it's possible (with a small amount of overhead) |
| 141 | to create a hybrid strategy that operates entirely within 128-bit |
| 142 | lanes. This means that cross-lane operations can use the faster |
| 143 | `vpshufd` (1c latency) instead of a general shuffle instruction (3c |
| 144 | latency). |
| 145 | |
| 146 | # Choice of radix |
| 147 | |
| 148 | The inputs to IFMA instructions are 52 bits wide, so the radix \\(r\\) |
| 149 | used to represent a multiprecision integer must be \\( r \leq 52 \\). |
| 150 | The obvious choice is the "native" radix \\(r = 52\\). |
| 151 | |
| 152 | As described above, this choice |
| 153 | has the advantage that for \\(x_i, y_j \in [0,2^{52})\\), the product term |
| 154 | \\[ |
| 155 | \begin{aligned} |
| 156 | (x_i 2^{52 i}) (y_j 2^{52 j}) |
| 157 | &= |
| 158 | 2^{52 (i+j)}( |
| 159 | \mathrm{lo}(x_i, y_j) + |
| 160 | \mathrm{hi}(x_i, y_j) 2^{52} |
| 161 | ) |
| 162 | \\\\ |
| 163 | &= |
| 164 | \mathrm{lo}(x_i, y_j) 2^{52 (i+j)} + |
| 165 | \mathrm{hi}(x_i, y_j) 2^{52 (i+j+1)}, |
| 166 | \end{aligned} |
| 167 | \\] |
| 168 | so that the low and high halves of the product can be directly accumulated |
| 169 | onto the product limbs. |
| 170 | In contrast, when using a smaller radix \\(r = 52 - k\\), |
| 171 | the product term has the form |
| 172 | \\[ |
| 173 | \begin{aligned} |
| 174 | (x_i 2^{r i}) (y_j 2^{r j}) |
| 175 | &= |
| 176 | 2^{r (i+j)}( |
| 177 | \mathrm{lo}(x_i, y_j) + |
| 178 | \mathrm{hi}(x_i, y_j) 2^{52} |
| 179 | ) |
| 180 | \\\\ |
| 181 | &= |
| 182 | \mathrm{lo}(x_i, y_j) 2^{r (i+j)} + |
| 183 | ( |
| 184 | \mathrm{hi}(x_i, y_j) 2^k |
| 185 | ) |
| 186 | 2^{r (i+j+1)}. |
| 187 | \end{aligned} |
| 188 | \\] |
| 189 | What's happening is that the product \\(x_i y_j\\) of size \\(2r\\) |
| 190 | bits is split not at \\(r\\) but at \\(52\\), so \\(k\\) product bits |
| 191 | are placed into the low half instead of the high half. This means |
| 192 | that the high half of the product cannot be directly accumulated onto |
| 193 | \\(z_{i+j+1}\\), but must first be multiplied by \\(2^k\\) (i.e., left |
| 194 | shifted by \\(k\\)). In addition, the low half of the product is |
| 195 | \\(52\\) bits large instead of \\(r\\) bits. |
| 196 | |
| 197 | ## Handling offset product terms |
| 198 | |
| 199 | [Drucker and Gueron][2018_drucker_gueron] analyze the choice of radix |
| 200 | in the context of big-integer squaring, outlining three ways to handle |
| 201 | the offset product terms, before concluding that all of them are |
| 202 | suboptimal: |
| 203 | |
| 204 | 1. Shift the results after accumulation; |
| 205 | 2. Shift the input operands before multiplication; |
| 206 | 3. Split the MAC operation, accumulating into a zeroed register, |
| 207 | shifting the result, and then adding. |
| 208 | |
| 209 | The first option is rejected because it could double-shift some |
| 210 | previously accumulated terms, the second doesn't work because the |
| 211 | inputs could become larger than \\(52\\) bits, and the third requires |
| 212 | additional instructions to handle the shifting and adding. |
| 213 | |
| 214 | Based on an analysis of total number of instructions, they suggest an |
| 215 | addition to the instruction set, which they call `FMSA` (fused |
| 216 | multiply-shift-add). This would shift the result according to an 8-bit |
| 217 | immediate value before accumulating it into the destination register. |
| 218 | |
| 219 | However, this change to the instruction set doesn't seem to be |
| 220 | necessary. Instead, the product terms can be grouped according to |
| 221 | their coefficients, accumulated together, then shifted once before |
| 222 | adding them to the final sum. This uses an extra register, shift, and |
| 223 | add, but only once per product term (accumulation target), not once |
| 224 | per source term (as in the Drucker-Gueron paper). |
| 225 | |
| 226 | Moreover, because IFMA instructions execute only on two ports |
| 227 | (presumably 0 and 1), while adds and shifts can execute on three ports |
| 228 | (0, 1, and 5), the adds and shifts can execute independently of the |
| 229 | IFMA operations, as long as there is not too much pressure on port 5. |
| 230 | This means that, although the total number of instructions increases, |
| 231 | the shifts and adds do not necessarily increase the execution time, as |
| 232 | long as throughput is limited by IFMA operations. |
| 233 | |
| 234 | Finally, because IFMA instructions have 4 cycle latency and 0.5/1 |
| 235 | cycle throughput (for 256/512 bit vectors), maximizing IFMA throughput |
| 236 | requires either 8 (for 256) or 4 (for 512) independent operations. So |
| 237 | accumulating groups of terms independently before adding them at the |
| 238 | end may be necessary anyways, in order to prevent long chains of |
| 239 | dependent instructions. |
| 240 | |
| 241 | ## Advantages of a smaller radix |
| 242 | |
| 243 | Using a smaller radix has other advantages. Although radix \\(52\\) |
| 244 | is an unsaturated representation from the point of view of the |
| 245 | \\(64\\)-bit accumulators (because up to 4096 product terms can be |
| 246 | accumulated without carries), it's a saturated representation from the |
| 247 | point of view of the multiplier (since \\(52\\)-bit values are the |
| 248 | maximum input size). |
| 249 | |
| 250 | Because the inputs to a multiplication must have all of their limbs |
| 251 | bounded by \\(2^{52}\\), limbs in excess of \\(2^{52}\\) must be |
| 252 | reduced before they can be used as an input. The |
| 253 | [Gueron-Krasnov][2016_gueron_krasnov] paper suggests normalizing |
| 254 | values using a standard, sequential carry chain: for each limb, add |
| 255 | the carryin from reducing the previous limb, compute the carryout and |
| 256 | reduce the current limb, then move to the next limb. |
| 257 | |
| 258 | However, when using a smaller radix, such as \\(51\\), each limb can |
| 259 | store a carry bit and still be used as the input to a multiplication. |
| 260 | This means that the inputs do not need to be normalized, and instead |
| 261 | of using a sequential carry chain, we can compute all carryouts in |
| 262 | parallel, reduce all limbs in parallel, and then add the carryins in |
| 263 | parallel (possibly growing the limb values by one bit). |
| 264 | |
| 265 | Because the output of this partial reduction is an acceptable |
| 266 | multiplication input, we can "close the loop" using partial reductions |
| 267 | and never have to normalize to a canonical representation through the |
| 268 | entire computation, in contrast to the Gueron-Krasnov approach, which |
| 269 | converts back to a packed representation after every operation. (This |
| 270 | idea seems to trace back to at least as early as [this 1999 |
| 271 | paper][1999_walter]). |
| 272 | |
| 273 | Using \\(r = 51\\) is enough to keep a carry bit in each limb and |
| 274 | avoid normalizations. What about an even smaller radix? One reason |
| 275 | to choose a smaller radix would be to align the limb boundaries with |
| 276 | an inline reduction (for instance, choosing \\(r = 43\\) for the |
| 277 | Mersenne field \\(p = 2^{127} - 1\\)), but for \\(p = 2^{255 - 19}\\), |
| 278 | \\(r = 51 = 255/5\\) is the natural choice. |
| 279 | |
| 280 | # Multiplication |
| 281 | |
| 282 | The inputs to a multiplication are two field elements |
| 283 | \\[ |
| 284 | \begin{aligned} |
| 285 | x &= x_0 + x_1 2^{51} + x_2 2^{102} + x_3 2^{153} + x_4 2^{204} \\\\ |
| 286 | y &= y_0 + y_1 2^{51} + y_2 2^{102} + y_3 2^{153} + y_4 2^{204}, |
| 287 | \end{aligned} |
| 288 | \\] |
| 289 | with limbs in range \\([0,2^{52})\\). |
| 290 | |
| 291 | Writing the product terms as |
| 292 | \\[ |
| 293 | \begin{aligned} |
| 294 | z &= z_0 + z_1 2^{51} + z_2 2^{102} + z_3 2^{153} + z_4 2^{204} \\\\ |
| 295 | &+ z_5 2^{255} + z_6 2^{306} + z_7 2^{357} + z_8 2^{408} + z_9 2^{459}, |
| 296 | \end{aligned} |
| 297 | \\] |
| 298 | a schoolbook multiplication in product scanning form takes the form |
| 299 | \\[ |
| 300 | \begin{aligned} |
| 301 | z_0 &= x_0 y_0 \\\\ |
| 302 | z_1 &= x_1 y_0 + x_0 y_1 \\\\ |
| 303 | z_2 &= x_2 y_0 + x_1 y_1 + x_0 y_2 \\\\ |
| 304 | z_3 &= x_3 y_0 + x_2 y_1 + x_1 y_2 + x_0 y_3 \\\\ |
| 305 | z_4 &= x_4 y_0 + x_3 y_1 + x_2 y_2 + x_1 y_3 + x_0 y_4 \\\\ |
| 306 | z_5 &= x_4 y_1 + x_3 y_2 + x_2 y_3 + x_1 y_4 \\\\ |
| 307 | z_6 &= x_4 y_2 + x_3 y_3 + x_2 y_4 \\\\ |
| 308 | z_7 &= x_4 y_3 + x_3 y_4 \\\\ |
| 309 | z_8 &= x_4 y_4 \\\\ |
| 310 | z_9 &= 0 \\\\ |
| 311 | \end{aligned} |
| 312 | \\] |
| 313 | Each term \\(x_i y_j\\) can be written in terms of IFMA operations as |
| 314 | \\[ |
| 315 | x_i y_j = \mathrm{lo}(x_i,y_j) + 2\mathrm{hi}(x_i,y_j)2^{51}. |
| 316 | \\] |
| 317 | Substituting this equation into the schoolbook multiplication, then |
| 318 | moving terms to eliminate the \\(2^{51}\\) factors gives |
| 319 | \\[ |
| 320 | \begin{aligned} |
| 321 | z_0 &= \mathrm{lo}(x_0, y_0) \\\\ |
| 322 | &+ \qquad 0 \\\\ |
| 323 | z_1 &= \mathrm{lo}(x_1, y_0) + \mathrm{lo}(x_0, y_1) \\\\ |
| 324 | &+ \qquad 2( \mathrm{hi}(x_0, y_0) )\\\\ |
| 325 | z_2 &= \mathrm{lo}(x_2, y_0) + \mathrm{lo}(x_1, y_1) + \mathrm{lo}(x_0, y_2) \\\\ |
| 326 | &+ \qquad 2( \mathrm{hi}(x_1, y_0) + \mathrm{hi}(x_0, y_1) )\\\\ |
| 327 | z_3 &= \mathrm{lo}(x_3, y_0) + \mathrm{lo}(x_2, y_1) + \mathrm{lo}(x_1, y_2) + \mathrm{lo}(x_0, y_3) \\\\ |
| 328 | &+ \qquad 2( \mathrm{hi}(x_2, y_0) + \mathrm{hi}(x_1, y_1) + \mathrm{hi}(x_0, y_2) )\\\\ |
| 329 | z_4 &= \mathrm{lo}(x_4, y_0) + \mathrm{lo}(x_3, y_1) + \mathrm{lo}(x_2, y_2) + \mathrm{lo}(x_1, y_3) + \mathrm{lo}(x_0, y_4) \\\\ |
| 330 | &+ \qquad 2( \mathrm{hi}(x_3, y_0) + \mathrm{hi}(x_2, y_1) + \mathrm{hi}(x_1, y_2) + \mathrm{hi}(x_0, y_3) )\\\\ |
| 331 | z_5 &= \mathrm{lo}(x_4, y_1) + \mathrm{lo}(x_3, y_2) + \mathrm{lo}(x_2, y_3) + \mathrm{lo}(x_1, y_4) \\\\ |
| 332 | &+ \qquad 2( \mathrm{hi}(x_4, y_0) + \mathrm{hi}(x_3, y_1) + \mathrm{hi}(x_2, y_2) + \mathrm{hi}(x_1, y_3) + \mathrm{hi}(x_0, y_4) )\\\\ |
| 333 | z_6 &= \mathrm{lo}(x_4, y_2) + \mathrm{lo}(x_3, y_3) + \mathrm{lo}(x_2, y_4) \\\\ |
| 334 | &+ \qquad 2( \mathrm{hi}(x_4, y_1) + \mathrm{hi}(x_3, y_2) + \mathrm{hi}(x_2, y_3) + \mathrm{hi}(x_1, y_4) )\\\\ |
| 335 | z_7 &= \mathrm{lo}(x_4, y_3) + \mathrm{lo}(x_3, y_4) \\\\ |
| 336 | &+ \qquad 2( \mathrm{hi}(x_4, y_2) + \mathrm{hi}(x_3, y_3) + \mathrm{hi}(x_2, y_4) )\\\\ |
| 337 | z_8 &= \mathrm{lo}(x_4, y_4) \\\\ |
| 338 | &+ \qquad 2( \mathrm{hi}(x_4, y_3) + \mathrm{hi}(x_3, y_4) )\\\\ |
| 339 | z_9 &= 0 \\\\ |
| 340 | &+ \qquad 2( \mathrm{hi}(x_4, y_4) )\\\\ |
| 341 | \end{aligned} |
| 342 | \\] |
| 343 | As noted above, our strategy will be to multiply and accumulate the |
| 344 | terms with coefficient \\(2\\) separately from those with coefficient |
| 345 | \\(1\\), before combining them at the end. This can alternately be |
| 346 | thought of as accumulating product terms into a *doubly-redundant* |
| 347 | representation, with two limbs for each digit, before collapsing |
| 348 | the doubly-redundant representation by shifts and adds. |
| 349 | |
| 350 | This computation requires 25 `vpmadd52luq` and 25 `vpmadd52huq` |
| 351 | operations. For 256-bit vectors, IFMA operations execute on an |
| 352 | i3-8121U with latency 4 cycles, throughput 0.5 cycles, so executing 50 |
| 353 | instructions requires 25 cycles' worth of throughput. Accumulating |
| 354 | terms with coefficient \\(1\\) and \\(2\\) separately means that the |
| 355 | longest dependency chain has length 5, so the critical path has length |
| 356 | 20 cycles and the bottleneck is throughput. |
| 357 | |
| 358 | # Reduction modulo \\(p\\) |
| 359 | |
| 360 | The next question is how to handle the reduction modulo \\(p\\). |
| 361 | Because \\(p = 2^{255} - 19\\), \\(2^{255} = 19 \pmod p\\), so we can |
| 362 | alternately write |
| 363 | \\[ |
| 364 | \begin{aligned} |
| 365 | z &= z_0 + z_1 2^{51} + z_2 2^{102} + z_3 2^{153} + z_4 2^{204} \\\\ |
| 366 | &+ z_5 2^{255} + z_6 2^{306} + z_7 2^{357} + z_8 2^{408} + z_9 2^{459} |
| 367 | \end{aligned} |
| 368 | \\] |
| 369 | as |
| 370 | \\[ |
| 371 | \begin{aligned} |
| 372 | z &= (z_0 + 19z_5) + (z_1 + 19z_6) 2^{51} + (z_2 + 19z_7) 2^{102} + (z_3 + 19z_8) 2^{153} + (z_4 + 19z_9) 2^{204}. |
| 373 | \end{aligned} |
| 374 | \\] |
| 375 | When using a \\(64 \times 64 \rightarrow 128\\)-bit multiplier, this |
| 376 | can be handled (as in [Ed25519][ed25519_paper]) by premultiplying |
| 377 | source terms by \\(19\\). Since \\(\lg(19) < 4.25\\), this increases |
| 378 | their size by less than \\(4.25\\) bits, and the rest of the |
| 379 | multiplication can be shown to work out. |
| 380 | |
| 381 | Here, we have at most \\(1\\) bit of headroom. In order to allow |
| 382 | premultiplication, we would need to use radix \\(2^{47}\\), which |
| 383 | would require six limbs instead of five. Instead, we compute the high |
| 384 | terms \\(z_5, \ldots, z_9\\), each using two chains of IFMA |
| 385 | operations, then multiply by \\(19\\) and combine with the lower terms |
| 386 | \\(z_0, \ldots, z_4\\). There are two ways to perform the |
| 387 | multiplication by \\(19\\): using more IFMA operations, or using the |
| 388 | `vpmullq` instruction, which computes the low \\(64\\) bits of a \\(64 |
| 389 | \times 64\\)-bit product. However, `vpmullq` has 15c/1.5c |
| 390 | latency/throughput, in contrast to the 4c/0.5c latency/throughput of |
| 391 | IFMA operations, so it seems like a worse choice. |
| 392 | |
| 393 | The high terms \\(z_5, \ldots, z_9\\) are sums of \\(52\\)-bit terms, |
| 394 | so they are larger than \\(52\\) bits. Write these terms in radix \\(52\\) as |
| 395 | \\[ |
| 396 | z_{5+i} = z_{5+i}' + z_{5+i}'' 2^{52}, \qquad z_{5+i}' < 2^{52}. |
| 397 | \\] |
| 398 | Then the contribution of \\(z_{5+i}\\), taken modulo \\(p\\), is |
| 399 | \\[ |
| 400 | \begin{aligned} |
| 401 | z_{5+i} 2^{255} 2^{51 i} |
| 402 | &= |
| 403 | 19 (z_{5+i}' + z_{5+i}'' 2^{52}) 2^{51 i} |
| 404 | \\\\ |
| 405 | &= |
| 406 | 19 z_{5+i}' 2^{51 i} + 2 \cdot 19 z_{5+i}'' 2^{51 (i+1)} |
| 407 | \\\\ |
| 408 | \end{aligned} |
| 409 | \\] |
| 410 | The products \\(19 z_{5+i}', 19 z_{5+i}''\\) can be written in terms of IFMA operations as |
| 411 | \\[ |
| 412 | \begin{aligned} |
| 413 | 19 z_{5+i}' &= \mathrm{lo}(19, z_{5+i}') + 2 \mathrm{hi}(19, z_{5+i}') 2^{51}, \\\\ |
| 414 | 19 z_{5+i}'' &= \mathrm{lo}(19, z_{5+i}'') + 2 \mathrm{hi}(19, z_{5+i}'') 2^{51}. \\\\ |
| 415 | \end{aligned} |
| 416 | \\] |
| 417 | Because \\(z_{5+i} < 2^{64}\\), \\(z_{5+i}'' < 2^{12} \\), so \\(19 |
| 418 | z_{5+i}'' < 2^{17} < 2^{52} \\) and \\(\mathrm{hi}(19, z_{5+i}'') = 0\\). |
| 419 | Because IFMA operations ignore the high bits of their source |
| 420 | operands, we do not need to compute \\(z\_{5+i}'\\) explicitly: |
| 421 | the high bits will be ignored. |
| 422 | Combining these observations, we can write |
| 423 | \\[ |
| 424 | \begin{aligned} |
| 425 | z_{5+i} 2^{255} 2^{51 i} |
| 426 | &= |
| 427 | 19 z_{5+i}' 2^{51 i} + 2 \cdot 19 z_{5+i}'' 2^{51 (i+1)} |
| 428 | \\\\ |
| 429 | &= |
| 430 | \mathrm{lo}(19, z_{5+i}) 2^{51 i} |
| 431 | \+ 2 \mathrm{hi}(19, z_{5+i}) 2^{51 (i+1)} |
| 432 | \+ 2 \mathrm{lo}(19, z_{5+i}/2^{52}) 2^{51 (i+1)}. |
| 433 | \end{aligned} |
| 434 | \\] |
| 435 | |
| 436 | For \\(i = 0,1,2,3\\), this allows reducing \\(z_{5+i}\\) onto |
| 437 | \\(z_{i}, z_{i+1}\\), and if the low terms are computed using a |
| 438 | doubly-redundant representation, no additional shifts are needed to |
| 439 | handle the \\(2\\) coefficients. For \\(i = 4\\), there's a |
| 440 | complication: the contribution becomes |
| 441 | \\[ |
| 442 | \begin{aligned} |
| 443 | z_{9} 2^{255} 2^{204} |
| 444 | &= |
| 445 | \mathrm{lo}(19, z_{9}) 2^{204} |
| 446 | \+ 2 \mathrm{hi}(19, z_{9}) 2^{255} |
| 447 | \+ 2 \mathrm{lo}(19, z_{9}/2^{52}) 2^{255} |
| 448 | \\\\ |
| 449 | &= |
| 450 | \mathrm{lo}(19, z_{9}) 2^{204} |
| 451 | \+ 2 \mathrm{hi}(19, z_{9}) 19 |
| 452 | \+ 2 \mathrm{lo}(19, z_{9}/2^{52}) 19 |
| 453 | \\\\ |
| 454 | &= |
| 455 | \mathrm{lo}(19, z_{9}) 2^{204} |
| 456 | \+ 2 |
| 457 | \mathrm{lo}(19, \mathrm{hi}(19, z_{9}) + \mathrm{lo}(19, z_{9}/2^{52})). |
| 458 | \\\\ |
| 459 | \end{aligned} |
| 460 | \\] |
| 461 | |
| 462 | It would be possible to cut the number of multiplications from 3 to 2 |
| 463 | by carrying the high part of each \\(z_i\\) onto \\(z_{i+1}\\). This |
| 464 | would eliminate 5 multiplications, clearing 2.5 cycles of port |
| 465 | pressure, at the cost of 5 additions, adding 1.66 cycles of port |
| 466 | pressure. But doing this would create a dependency between terms |
| 467 | (e.g., \\(z_{5}\\) must be computed before the reduction of |
| 468 | \\(z_{6}\\) can begin), whereas with the approach above, all |
| 469 | contributions to all terms are computed independently, to maximize ILP |
| 470 | and flexibility for the processor to schedule instructions. |
| 471 | |
| 472 | This strategy performs 16 IFMA operations, adding two IFMA operations |
| 473 | to each of the \\(2\\)-coefficient terms and one to each of the |
| 474 | \\(1\\)-coefficient terms. Considering the multiplication and |
| 475 | reduction together, we use 66 IFMA operations, requiring 33 cycles' |
| 476 | throughput, while the longest chain of IFMA operations is in the |
| 477 | reduction of \\(z_5\\) onto \\(z_1\\), of length 7 (so 28 cycles, plus |
| 478 | 2 cycles to combine the two parts of \\(z_5\\), and the bottleneck is |
| 479 | again throughput. |
| 480 | |
| 481 | Once this is done, we have computed the product terms |
| 482 | \\[ |
| 483 | z = z_0 + z_1 2^{51} + z_2 2^{102} + z_3 2^{153} + z_4 2^{204}, |
| 484 | \\] |
| 485 | without reducing the \\(z_i\\) to fit in \\(52\\) bits. Because the |
| 486 | overall flow of operations alternates multiplications and additions or |
| 487 | subtractions, we would have to perform a reduction after an addition |
| 488 | but before the next multiplication anyways, so there's no benefit to |
| 489 | fully reducing the limbs at the end of a multiplication. Instead, we |
| 490 | leave them unreduced, and track the reduction state using the type |
| 491 | system to ensure that unreduced limbs are not accidentally used as an |
| 492 | input to a multiplication. |
| 493 | |
| 494 | # Squaring |
| 495 | |
| 496 | Squaring operates similarly to multiplication, but with the |
| 497 | possibility to combine identical terms. |
| 498 | As before, we write the input as |
| 499 | \\[ |
| 500 | \begin{aligned} |
| 501 | x &= x_0 + x_1 2^{51} + x_2 2^{102} + x_3 2^{153} + x_4 2^{204} |
| 502 | \end{aligned} |
| 503 | \\] |
| 504 | with limbs in range \\([0,2^{52})\\). |
| 505 | Writing the product terms as |
| 506 | \\[ |
| 507 | \begin{aligned} |
| 508 | z &= z_0 + z_1 2^{51} + z_2 2^{102} + z_3 2^{153} + z_4 2^{204} \\\\ |
| 509 | &+ z_5 2^{255} + z_6 2^{306} + z_7 2^{357} + z_8 2^{408} + z_9 2^{459}, |
| 510 | \end{aligned} |
| 511 | \\] |
| 512 | a schoolbook squaring in product scanning form takes the form |
| 513 | \\[ |
| 514 | \begin{aligned} |
| 515 | z_0 &= x_0 x_0 \\\\ |
| 516 | z_1 &= 2 x_1 x_0 \\\\ |
| 517 | z_2 &= 2 x_2 x_0 + x_1 x_1 \\\\ |
| 518 | z_3 &= 2 x_3 x_0 + 2 x_2 x_1 \\\\ |
| 519 | z_4 &= 2 x_4 x_0 + 2 x_3 x_1 + x_2 x_2 \\\\ |
| 520 | z_5 &= 2 x_4 x_1 + 2 x_3 x_2 \\\\ |
| 521 | z_6 &= 2 x_4 x_2 + x_3 x_3 \\\\ |
| 522 | z_7 &= 2 x_4 x_3 \\\\ |
| 523 | z_8 &= x_4 x_4 \\\\ |
| 524 | z_9 &= 0 \\\\ |
| 525 | \end{aligned} |
| 526 | \\] |
| 527 | As before, we write \\(x_i x_j\\) as |
| 528 | \\[ |
| 529 | x_i x_j = \mathrm{lo}(x_i,x_j) + 2\mathrm{hi}(x_i,x_j)2^{51}, |
| 530 | \\] |
| 531 | and substitute to obtain |
| 532 | \\[ |
| 533 | \begin{aligned} |
| 534 | z_0 &= \mathrm{lo}(x_0, x_0) + 0 \\\\ |
| 535 | z_1 &= 2 \mathrm{lo}(x_1, x_0) + 2 \mathrm{hi}(x_0, x_0) \\\\ |
| 536 | z_2 &= 2 \mathrm{lo}(x_2, x_0) + \mathrm{lo}(x_1, x_1) + 4 \mathrm{hi}(x_1, x_0) \\\\ |
| 537 | z_3 &= 2 \mathrm{lo}(x_3, x_0) + 2 \mathrm{lo}(x_2, x_1) + 4 \mathrm{hi}(x_2, x_0) + 2 \mathrm{hi}(x_1, x_1) \\\\ |
| 538 | z_4 &= 2 \mathrm{lo}(x_4, x_0) + 2 \mathrm{lo}(x_3, x_1) + \mathrm{lo}(x_2, x_2) + 4 \mathrm{hi}(x_3, x_0) + 4 \mathrm{hi}(x_2, x_1) \\\\ |
| 539 | z_5 &= 2 \mathrm{lo}(x_4, x_1) + 2 \mathrm{lo}(x_3, x_2) + 4 \mathrm{hi}(x_4, x_0) + 4 \mathrm{hi}(x_3, x_1) + 2 \mathrm{hi}(x_2, x_2) \\\\ |
| 540 | z_6 &= 2 \mathrm{lo}(x_4, x_2) + \mathrm{lo}(x_3, x_3) + 4 \mathrm{hi}(x_4, x_1) + 4 \mathrm{hi}(x_3, x_2) \\\\ |
| 541 | z_7 &= 2 \mathrm{lo}(x_4, x_3) + 4 \mathrm{hi}(x_4, x_2) + 2 \mathrm{hi}(x_3, x_3) \\\\ |
| 542 | z_8 &= \mathrm{lo}(x_4, x_4) + 4 \mathrm{hi}(x_4, x_3) \\\\ |
| 543 | z_9 &= 0 + 2 \mathrm{hi}(x_4, x_4) \\\\ |
| 544 | \end{aligned} |
| 545 | \\] |
| 546 | To implement these, we group terms by their coefficient, computing |
| 547 | those with coefficient \\(2\\) on set of IFMA chains, and on another |
| 548 | set of chains, we begin with coefficient-\\(4\\) terms, then shift |
| 549 | left before continuing with the coefficient-\\(1\\) terms. |
| 550 | The reduction strategy is the same as for multiplication. |
| 551 | |
| 552 | # Future improvements |
| 553 | |
| 554 | LLVM won't use blend operations on [256-bit vectors yet][llvm_blend], |
| 555 | so there's a bunch of blend instructions that could be omitted. |
| 556 | |
| 557 | Although the multiplications and squarings are much faster, there's no |
| 558 | speedup to the additions and subtractions, so there are diminishing |
| 559 | returns. In fact, the complications in the doubling formulas mean |
| 560 | that doubling is actually slower than readdition. This also suggests |
| 561 | that moving to 512-bit vectors won't be much help for a strategy aimed |
| 562 | at parallelism within a group operation, so to extract performance |
| 563 | gains from 512-bit vectors it will probably be necessary to create a |
| 564 | parallel-friendly multiscalar multiplication algorithm. This could |
| 565 | also help with reducing shuffle pressure. |
| 566 | |
| 567 | The squaring implementation could probably be optimized, but without |
| 568 | `perf` support on Cannonlake it's difficult to make actual |
| 569 | measurements. |
| 570 | |
| 571 | Another improvement would be to implement vectorized square root |
| 572 | computations, which would allow creating an iterator adaptor for point |
| 573 | decompression that bunched decompression operations and executed them |
| 574 | in parallel. This would accelerate batch verification. |
| 575 | |
| 576 | [2016_gueron_krasnov]: https://ieeexplore.ieee.org/document/7563269 |
| 577 | [2018_drucker_gueron]: https://eprint.iacr.org/2018/335 |
| 578 | [1999_walter]: https://pdfs.semanticscholar.org/0e6a/3e8f30b63b556679f5dff2cbfdfe9523f4fa.pdf |
| 579 | [ed25519_paper]: https://ed25519.cr.yp.to/ed25519-20110926.pdf |
| 580 | [llvm_blend]: https://bugs.llvm.org/show_bug.cgi?id=38343 |