interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / curve25519-dalek / docs / ifma-notes.md

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