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 / avx2-notes.md

140 lines · 4985 bytes Raw Blame History Rendered
1An AVX2 implementation of the vectorized point operation strategy.
2
3# Field element representation
4
5Our strategy is to implement 4-wide multiplication and squaring by
6wordslicing, using one 64-bit AVX2 lane for each field element. Field
7elements are represented in the usual way as 10 `u32` limbs in radix
8\\(25.5\\) (i.e., alternating between \\(2\^{26}\\) for even limbs and
9\\(2\^{25}\\) for odd limbs). This has the effect that passing between
10the parallel 32-bit AVX2 representation and the serial 64-bit
11representation (which uses radix \\(2^{51}\\)) amounts to regrouping
12digits.
13
14The field element representation is oriented around the AVX2
15`vpmuludq` instruction, which multiplies the low 32 bits of each
1664-bit lane of each operand to produce a 64-bit result.
17
18```text,no_run
19(a1 ?? b1 ?? c1 ?? d1 ??)
20(a2 ?? b2 ?? c2 ?? d2 ??)
21
22(a1*a2 b1*b2 c1*c2 d1*d2)
23```
24
25To unpack 32-bit values into 64-bit lanes for use in multiplication
26it would be convenient to use the `vpunpck[lh]dq` instructions,
27which unpack and interleave the low and high 32-bit lanes of two
28source vectors.
29However, the AVX2 versions of these instructions are designed to
30operate only within 128-bit lanes of the 256-bit vectors, so that
31interleaving the low lanes of `(a0 b0 c0 d0 a1 b1 c1 d1)` with zero
32gives `(a0 00 b0 00 a1 00 b1 00)`. Instead, we pre-shuffle the data
33layout as `(a0 b0 a1 b1 c0 d0 c1 d1)` so that we can unpack the
34"low" and "high" parts as
35
36```text,no_run
37(a0 00 b0 00 c0 00 d0 00)
38(a1 00 b1 00 c1 00 d1 00)
39```
40
41The data layout for a vector of four field elements \\( (a,b,c,d)
42\\) with limbs \\( a_0, a_1, \ldots, a_9 \\) is as `[u32x8; 5]` in
43the form
44
45```text,no_run
46(a0 b0 a1 b1 c0 d0 c1 d1)
47(a2 b2 a3 b3 c2 d2 c3 d3)
48(a4 b4 a5 b5 c4 d4 c5 d5)
49(a6 b6 a7 b7 c6 d6 c7 d7)
50(a8 b8 a9 b9 c8 d8 c9 d9)
51```
52
53Since this breaks cleanly into two 128-bit lanes, it may be possible
54to adapt it to 128-bit vector instructions such as NEON without too
55much difficulty.
56
57# Avoiding Overflow in Doubling
58
59To analyze the size of the field element coefficients during the
60computations, we can parameterize the bounds on the limbs of each
61field element by \\( b \in \mathbb R \\) representing the excess bits
62above that limb's radix, so that each limb is bounded by either
63\\(2\^{25+b} \\) or \\( 2\^{26+b} \\), as appropriate.
64
65The multiplication routine requires that its inputs are bounded with
66\\( b < 1.75 \\), in order to fit a multiplication by \\( 19 \\)
67into 32 bits. Since \\( \lg 19 < 4.25 \\), \\( 19x < 2\^{32} \\)
68when \\( x < 2\^{27.75} = 2\^{26 + 1.75} \\). However, this is only
69required for one of the inputs; the other can grow up to \\( b < 2.5
70\\).
71
72In addition, the multiplication and squaring routines do not
73canonically reduce their outputs, but can leave some small uncarried
74excesses, so that their reduced outputs are bounded with
75\\( b < 0.007 \\).
76
77The non-parallel portion of the doubling formulas is
78$$
79\begin{aligned}
80(S\_5 &&,&& S\_6 &&,&& S\_8 &&,&& S\_9 )
81&\gets
82(S\_1 + S\_2 &&,&& S\_1 - S\_2 &&,&& S\_1 + 2S\_3 - S\_2 &&,&& S\_1 + S\_2 - S\_4)
83\end{aligned}
84$$
85
86Computing \\( (S\_5, S\_6, S\_8, S\_9 ) \\) as
87$$
88\begin{matrix}
89 & S\_1 & S\_1 & S\_1 & S\_1 \\\\
90+& S\_2 & & & S\_2 \\\\
91+& & & S\_3 & \\\\
92+& & & S\_3 & \\\\
93+& & 2p & 2p & 2p \\\\
94-& & S\_2 & S\_2 & \\\\
95-& & & & S\_4 \\\\
96=& S\_5 & S\_6 & S\_8 & S\_9
97\end{matrix}
98$$
99results in bit-excesses \\( < (1.01, 1.60, 2.33, 2.01)\\) for
100\\( (S\_5, S\_6, S\_8, S\_9 ) \\). The products we want to compute
101are then
102$$
103\begin{aligned}
104X\_3 &\gets S\_8 S\_9 \leftrightarrow (2.33, 2.01) \\\\
105Y\_3 &\gets S\_5 S\_6 \leftrightarrow (1.01, 1.60) \\\\
106Z\_3 &\gets S\_8 S\_6 \leftrightarrow (2.33, 1.60) \\\\
107T\_3 &\gets S\_5 S\_9 \leftrightarrow (1.01, 2.01)
108\end{aligned}
109$$
110which are too large: it's not possible to arrange the multiplicands so
111that one vector has \\(b < 2.5\\) and the other has \\( b < 1.75 \\).
112However, if we flip the sign of \\( S\_4 = S\_0\^2 \\) during
113squaring, so that we output \\(S\_4' = -S\_4 \pmod p\\), then we can
114compute
115$$
116\begin{matrix}
117 & S\_1 & S\_1 & S\_1 & S\_1 \\\\
118+& S\_2 & & & S\_2 \\\\
119+& & & S\_3 & \\\\
120+& & & S\_3 & \\\\
121+& & & & S\_4' \\\\
122+& & 2p & 2p & \\\\
123-& & S\_2 & S\_2 & \\\\
124=& S\_5 & S\_6 & S\_8 & S\_9
125\end{matrix}
126$$
127resulting in bit-excesses \\( < (1.01, 1.60, 2.33, 1.60)\\) for
128\\( (S\_5, S\_6, S\_8, S\_9 ) \\). The products we want to compute
129are then
130$$
131\begin{aligned}
132X\_3 &\gets S\_8 S\_9 \leftrightarrow (2.33, 1.60) \\\\
133Y\_3 &\gets S\_5 S\_6 \leftrightarrow (1.01, 1.60) \\\\
134Z\_3 &\gets S\_8 S\_6 \leftrightarrow (2.33, 1.60) \\\\
135T\_3 &\gets S\_5 S\_9 \leftrightarrow (1.01, 1.60)
136\end{aligned}
137$$
138whose right-hand sides are all bounded with \\( b < 1.75 \\) and
139whose left-hand sides are all bounded with \\( b < 2.5 \\),
140so that we can avoid any intermediate reductions.