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 / avx2-notes.md
| 1 | An AVX2 implementation of the vectorized point operation strategy. |
| 2 | |
| 3 | # Field element representation |
| 4 | |
| 5 | Our strategy is to implement 4-wide multiplication and squaring by |
| 6 | wordslicing, using one 64-bit AVX2 lane for each field element. Field |
| 7 | elements 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 |
| 10 | the parallel 32-bit AVX2 representation and the serial 64-bit |
| 11 | representation (which uses radix \\(2^{51}\\)) amounts to regrouping |
| 12 | digits. |
| 13 | |
| 14 | The field element representation is oriented around the AVX2 |
| 15 | `vpmuludq` instruction, which multiplies the low 32 bits of each |
| 16 | 64-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 | |
| 25 | To unpack 32-bit values into 64-bit lanes for use in multiplication |
| 26 | it would be convenient to use the `vpunpck[lh]dq` instructions, |
| 27 | which unpack and interleave the low and high 32-bit lanes of two |
| 28 | source vectors. |
| 29 | However, the AVX2 versions of these instructions are designed to |
| 30 | operate only within 128-bit lanes of the 256-bit vectors, so that |
| 31 | interleaving the low lanes of `(a0 b0 c0 d0 a1 b1 c1 d1)` with zero |
| 32 | gives `(a0 00 b0 00 a1 00 b1 00)`. Instead, we pre-shuffle the data |
| 33 | layout 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 | |
| 41 | The 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 |
| 43 | the 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 | |
| 53 | Since this breaks cleanly into two 128-bit lanes, it may be possible |
| 54 | to adapt it to 128-bit vector instructions such as NEON without too |
| 55 | much difficulty. |
| 56 | |
| 57 | # Avoiding Overflow in Doubling |
| 58 | |
| 59 | To analyze the size of the field element coefficients during the |
| 60 | computations, we can parameterize the bounds on the limbs of each |
| 61 | field element by \\( b \in \mathbb R \\) representing the excess bits |
| 62 | above that limb's radix, so that each limb is bounded by either |
| 63 | \\(2\^{25+b} \\) or \\( 2\^{26+b} \\), as appropriate. |
| 64 | |
| 65 | The multiplication routine requires that its inputs are bounded with |
| 66 | \\( b < 1.75 \\), in order to fit a multiplication by \\( 19 \\) |
| 67 | into 32 bits. Since \\( \lg 19 < 4.25 \\), \\( 19x < 2\^{32} \\) |
| 68 | when \\( x < 2\^{27.75} = 2\^{26 + 1.75} \\). However, this is only |
| 69 | required for one of the inputs; the other can grow up to \\( b < 2.5 |
| 70 | \\). |
| 71 | |
| 72 | In addition, the multiplication and squaring routines do not |
| 73 | canonically reduce their outputs, but can leave some small uncarried |
| 74 | excesses, so that their reduced outputs are bounded with |
| 75 | \\( b < 0.007 \\). |
| 76 | |
| 77 | The 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 | |
| 86 | Computing \\( (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 | $$ |
| 99 | results 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 |
| 101 | are then |
| 102 | $$ |
| 103 | \begin{aligned} |
| 104 | X\_3 &\gets S\_8 S\_9 \leftrightarrow (2.33, 2.01) \\\\ |
| 105 | Y\_3 &\gets S\_5 S\_6 \leftrightarrow (1.01, 1.60) \\\\ |
| 106 | Z\_3 &\gets S\_8 S\_6 \leftrightarrow (2.33, 1.60) \\\\ |
| 107 | T\_3 &\gets S\_5 S\_9 \leftrightarrow (1.01, 2.01) |
| 108 | \end{aligned} |
| 109 | $$ |
| 110 | which are too large: it's not possible to arrange the multiplicands so |
| 111 | that one vector has \\(b < 2.5\\) and the other has \\( b < 1.75 \\). |
| 112 | However, if we flip the sign of \\( S\_4 = S\_0\^2 \\) during |
| 113 | squaring, so that we output \\(S\_4' = -S\_4 \pmod p\\), then we can |
| 114 | compute |
| 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 | $$ |
| 127 | resulting 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 |
| 129 | are then |
| 130 | $$ |
| 131 | \begin{aligned} |
| 132 | X\_3 &\gets S\_8 S\_9 \leftrightarrow (2.33, 1.60) \\\\ |
| 133 | Y\_3 &\gets S\_5 S\_6 \leftrightarrow (1.01, 1.60) \\\\ |
| 134 | Z\_3 &\gets S\_8 S\_6 \leftrightarrow (2.33, 1.60) \\\\ |
| 135 | T\_3 &\gets S\_5 S\_9 \leftrightarrow (1.01, 1.60) |
| 136 | \end{aligned} |
| 137 | $$ |
| 138 | whose right-hand sides are all bounded with \\( b < 1.75 \\) and |
| 139 | whose left-hand sides are all bounded with \\( b < 2.5 \\), |
| 140 | so that we can avoid any intermediate reductions. |