| 1 |
//go:generate go run src.go -out ../amd64.s -stubs ../stubs_amd64.go -pkg common |
| 2 |
|
| 3 |
// AVX2 optimized version of polynomial operations. See the comments on the |
| 4 |
// generic implementation for the details of the maths involved. |
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
. "github.com/mmcloughlin/avo/build" // nolint:golint,stylecheck |
| 9 |
. "github.com/mmcloughlin/avo/operand" // nolint:golint,stylecheck |
| 10 |
. "github.com/mmcloughlin/avo/reg" // nolint:golint,stylecheck |
| 11 |
|
| 12 |
"github.com/cloudflare/circl/pke/kyber/internal/common/params" |
| 13 |
) |
| 14 |
|
| 15 |
// XXX align Poly on 16 bytes such that we can use aligned moves |
| 16 |
// XXX ensure Zetas and ZetasAVX2 are 16 byte aligned |
| 17 |
|
| 18 |
// Barrett reduces the int16x16 where q must contain {q, q, …}; |
| 19 |
// num must contain {20159, 20159, …} the numerator in the approximation |
| 20 |
// 20159/2²⁶ of 1/q and t is a temporary register that will be clobbered. |
| 21 |
func barrettReduceX16(x, q, num, t Op) { |
| 22 |
// Recall that the Barrett reduction of x is given by |
| 23 |
// |
| 24 |
// x - int16((int32(x)*20159)>>26)*q |
| 25 |
|
| 26 |
VPMULHW(num, x, t) // t := (int32(x) * 20159) >> 16 |
| 27 |
VPSRAW(U8(10), t, t) // t = int16(t)>>10 so that t = (int32(x)*20159) >> 26 |
| 28 |
VPMULLW(q, t, t) // t *= q |
| 29 |
VPSUBW(t, x, x) // x -= t |
| 30 |
} |
| 31 |
|
| 32 |
func broadcastImm16(c int16, out Op) { |
| 33 |
tmp1 := GP32() |
| 34 |
tmp2 := XMM() |
| 35 |
MOVL(U32(uint32(int32(c))), tmp1) |
| 36 |
VMOVD(tmp1, tmp2) |
| 37 |
VPBROADCASTW(tmp2, out) |
| 38 |
} |
| 39 |
|
| 40 |
func addAVX2() { |
| 41 |
TEXT("addAVX2", NOSPLIT, "func(p, a, b *[256]int16)") |
| 42 |
Pragma("noescape") |
| 43 |
|
| 44 |
pPtr := Load(Param("p"), GP64()) |
| 45 |
aPtr := Load(Param("a"), GP64()) |
| 46 |
bPtr := Load(Param("b"), GP64()) |
| 47 |
|
| 48 |
var a [8]VecVirtual |
| 49 |
var b [8]VecVirtual |
| 50 |
for i := 0; i < 8; i++ { |
| 51 |
a[i] = YMM() |
| 52 |
b[i] = YMM() |
| 53 |
} |
| 54 |
|
| 55 |
for j := 0; j < 2; j++ { |
| 56 |
for i := 0; i < 8; i++ { |
| 57 |
VMOVDQU(Mem{Base: aPtr, Disp: 32 * (8*j + i)}, a[i]) |
| 58 |
} |
| 59 |
for i := 0; i < 8; i++ { |
| 60 |
VMOVDQU(Mem{Base: bPtr, Disp: 32 * (8*j + i)}, b[i]) |
| 61 |
} |
| 62 |
for i := 0; i < 8; i++ { |
| 63 |
VPADDW(a[i], b[i], b[i]) |
| 64 |
} |
| 65 |
for i := 0; i < 8; i++ { |
| 66 |
VMOVDQU(b[i], Mem{Base: pPtr, Disp: 32 * (8*j + i)}) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
RET() |
| 71 |
} |
| 72 |
|
| 73 |
func subAVX2() { |
| 74 |
TEXT("subAVX2", NOSPLIT, "func(p, a, b *[256]int16)") |
| 75 |
Pragma("noescape") |
| 76 |
|
| 77 |
pPtr := Load(Param("p"), GP64()) |
| 78 |
aPtr := Load(Param("a"), GP64()) |
| 79 |
bPtr := Load(Param("b"), GP64()) |
| 80 |
|
| 81 |
var a [8]VecVirtual |
| 82 |
var b [8]VecVirtual |
| 83 |
for i := 0; i < 8; i++ { |
| 84 |
a[i] = YMM() |
| 85 |
b[i] = YMM() |
| 86 |
} |
| 87 |
|
| 88 |
for j := 0; j < 2; j++ { |
| 89 |
for i := 0; i < 8; i++ { |
| 90 |
VMOVDQU(Mem{Base: aPtr, Disp: 32 * (8*j + i)}, a[i]) |
| 91 |
} |
| 92 |
for i := 0; i < 8; i++ { |
| 93 |
VMOVDQU(Mem{Base: bPtr, Disp: 32 * (8*j + i)}, b[i]) |
| 94 |
} |
| 95 |
for i := 0; i < 8; i++ { |
| 96 |
VPSUBW(b[i], a[i], b[i]) |
| 97 |
} |
| 98 |
for i := 0; i < 8; i++ { |
| 99 |
VMOVDQU(b[i], Mem{Base: pPtr, Disp: 32 * (8*j + i)}) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
RET() |
| 104 |
} |
| 105 |
|
| 106 |
// For each lane in a that has a 1 on ith bit of its index, swap it with |
| 107 |
// the corresponding lane in b where this 1 has been replaced by a 0. |
| 108 |
// |
| 109 |
// For instance, if i=2, then this will swap |
| 110 |
// |
| 111 |
// a[0b0100] ↔ b[0b0000] a[0b0101] ↔ b[0b0001] |
| 112 |
// a[0b0110] ↔ b[0b0010] a[0b0111] ↔ b[0b0011] |
| 113 |
// a[0b1100] ↔ b[0b1000] a[0b1101] ↔ b[0b1001] |
| 114 |
// a[0b1110] ↔ b[0b1010] a[0b1111] ↔ b[0b1011] |
| 115 |
// |
| 116 |
// and keep all other lanes in their place. If we index the lanes of a and |
| 117 |
// b consecutively (i.e. 0wxyz is wxyz of a and 1wxyz = wxyz of b), then |
| 118 |
// this corresponds to mapping lane vwxyz to xwvyz -- that is: flipping the |
| 119 |
// fourth bit with the ith bit (where we start from zeroth.) Hence the name. |
| 120 |
// |
| 121 |
// Why these permutations? There are two reasons: these are reasonable |
| 122 |
// easy to implement and they pull sequential butterflies in the NTT apart. |
| 123 |
// Recall, namely, that on the fifth layer of the NTT we're computing |
| 124 |
// butterflies between indices |
| 125 |
// |
| 126 |
// abcd0fgh abcd1fgh |
| 127 |
// |
| 128 |
// Applying bitflip with i=3 beforehand, the butterflies become |
| 129 |
// |
| 130 |
// abc0dfgh abc1dfgh |
| 131 |
// |
| 132 |
// which allows for 16 consecutive butterflies, which is convenient for AVX2. |
| 133 |
// What we'll actually end up doing is a bit different: we'll apply both |
| 134 |
// an i=3 and i=2 bitflip before then to also interleave the ζs correctly |
| 135 |
// for the fourth layer. |
| 136 |
// |
| 137 |
// See the diagram linked to in the documentation of nttAVX2(). |
| 138 |
func bitflip(i int, a, b, t Op) { |
| 139 |
switch i { |
| 140 |
case 3: |
| 141 |
VPERM2I128(U8(0x20), b, a, t) |
| 142 |
VPERM2I128(U8(0x31), b, a, b) |
| 143 |
VMOVDQA(t, a) |
| 144 |
case 2: |
| 145 |
VPUNPCKLQDQ(b, a, t) |
| 146 |
VPUNPCKHQDQ(b, a, b) |
| 147 |
VMOVDQA(t, a) |
| 148 |
case 1: |
| 149 |
VMOVSLDUP(b, t) |
| 150 |
VPBLENDD(U8(0xaa), t, a, t) |
| 151 |
VPSRLQ(U8(32), a, a) |
| 152 |
VPBLENDD(U8(0xaa), b, a, b) |
| 153 |
VMOVDQA(t, a) |
| 154 |
case 0: |
| 155 |
VPSLLD(U8(16), b, t) |
| 156 |
VPBLENDW(U8(0xaa), t, a, t) |
| 157 |
VPSRLD(U8(16), a, a) |
| 158 |
VPBLENDW(U8(0xaa), b, a, b) |
| 159 |
VMOVDQA(t, a) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
func invNttAVX2() { |
| 164 |
// This AVX2-optimized inverse NTT is close, but more disimilar from |
| 165 |
// the generic inverse NTT, then the AVX2-optimized forward NTT is |
| 166 |
// from the generic. |
| 167 |
// |
| 168 |
// 1. Just like in the AVX2-optimized forward NTT, we shuffle the |
| 169 |
// coefficients around to ensure we can do consecutive butterflies and |
| 170 |
// 2. we use the same preshuffled and duplicated ZetasAVX2 table. |
| 171 |
// 3. Barrett reductions are computed at different moments as it's very |
| 172 |
// efficient to do 16 at a time. |
| 173 |
|
| 174 |
// The butterflies and swaps are in the exact reverse order as those |
| 175 |
// of the AVX2-optimized forward NTT. See the comments on nttAVX2() |
| 176 |
// and bitflip() for documentation on the shufflings. |
| 177 |
|
| 178 |
// A diagram of the order of the butterflies and swaps can be found here: |
| 179 |
// |
| 180 |
// https://github.com/cloudflare/circl/wiki/images/kyber-invntt-avx2.svg |
| 181 |
// |
| 182 |
// The vertical lines with circles on the end represent butterflies. |
| 183 |
// The number in those butterflies refers to the index into the Zetas |
| 184 |
// array of which ζ is used. (Note that this array is different from |
| 185 |
// the ZetasAVX2 array, which contains the elements of Zetas many times |
| 186 |
// over in a way that is efficient for our implementation.) |
| 187 |
// |
| 188 |
// The green squares represent Barrett reductions. The green numbers after |
| 189 |
// the butterflies show the multiple of q that bounds the coefficient in |
| 190 |
// absolute value. (Recall that the lower coefficient is always bounded |
| 191 |
// by one when computing the inverse butterflies in the obvious way.) |
| 192 |
// The vertical lines with crosses on them represent a swap. |
| 193 |
|
| 194 |
TEXT("invNttAVX2", NOSPLIT, "func(p *[256]int16)") |
| 195 |
Pragma("noescape") |
| 196 |
pPtr := Load(Param("p"), GP64()) |
| 197 |
zetasPtr := GP64() |
| 198 |
LEAQ(NewDataAddr(Symbol{Name: "·ZetasAVX2"}, 0), zetasPtr) |
| 199 |
|
| 200 |
// Compute 4x16 Gentleman--Sande butterflies (a, b) ↦ (a + b, ζ(a - b)). |
| 201 |
// |
| 202 |
// There is a catch: the first two and the last two sets of butterflies |
| 203 |
// have to use the same sets of zetas, as we don't have enough registers |
| 204 |
// to keep everything around. t1 up to t4 are temporary registers that |
| 205 |
// will be clobbered. |
| 206 |
gsButterfly := func(a1, b1, a2, b2, zeta12l, zeta12h, |
| 207 |
a3, b3, a4, b4, zeta34l, zeta34h, t1, t2, t3, t4, q Op) { |
| 208 |
// In the generic implementation, a single butterfly is computed as |
| 209 |
// follows (unfolding the definition of montReduce and recalling |
| 210 |
// zeta stores -ζ.) |
| 211 |
// |
| 212 |
// t := b - a |
| 213 |
// a += b |
| 214 |
// m := int16(zeta * t * 62209) |
| 215 |
// b = int16(uint32(zeta * int32(t) - m * int32(Q)) >> 16) |
| 216 |
// |
| 217 |
// As ζt ≡ mq (mod 2¹⁶), see comments on montReduce(), we can |
| 218 |
// also compute b as |
| 219 |
// |
| 220 |
// b = (uint32(zeta * int32(t)) >> 16) - (uint32(m * int32(Q)) >> 16) |
| 221 |
// |
| 222 |
// m (x16) can be computed using a single VPMULLW with zeta * 62209 |
| 223 |
// as the second operand stored in a table. The two multiplications |
| 224 |
// and bitshifts for b can be performed using two VPMULHWs (again |
| 225 |
// for 16 at a time.) |
| 226 |
|
| 227 |
VPSUBW(a1, b1, t1) // t = b - a |
| 228 |
VPSUBW(a2, b2, t2) |
| 229 |
VPSUBW(a3, b3, t3) |
| 230 |
|
| 231 |
// We don't use t4 yet, so that zeta12l may be used as t4. |
| 232 |
|
| 233 |
VPADDW(a1, b1, a1) // a += b |
| 234 |
VPADDW(a2, b2, a2) |
| 235 |
VPADDW(a3, b3, a3) |
| 236 |
|
| 237 |
VPMULLW(t1, zeta12l, b1) // m = int16(zeta * t * 62209) |
| 238 |
VPMULLW(t2, zeta12l, b2) |
| 239 |
|
| 240 |
// At this point zeta12l (which might equal t4) is free. |
| 241 |
VPSUBW(a4, b4, t4) |
| 242 |
|
| 243 |
VPMULLW(t3, zeta34l, b3) |
| 244 |
|
| 245 |
VPADDW(a4, b4, a4) |
| 246 |
|
| 247 |
VPMULLW(t4, zeta34l, b4) |
| 248 |
|
| 249 |
VPMULHW(t1, zeta12h, t1) // uint32(zeta*int32(t)) >> 16 |
| 250 |
VPMULHW(t2, zeta12h, t2) |
| 251 |
VPMULHW(t3, zeta34h, t3) |
| 252 |
VPMULHW(t4, zeta34h, t4) |
| 253 |
|
| 254 |
VPMULHW(b1, q, b1) // uint32(m*int32(Q)) >> 16 |
| 255 |
VPMULHW(b2, q, b2) |
| 256 |
VPMULHW(b3, q, b3) |
| 257 |
VPMULHW(b4, q, b4) |
| 258 |
|
| 259 |
VPSUBW(b1, t1, b1) // Compute b |
| 260 |
VPSUBW(b2, t2, b2) |
| 261 |
VPSUBW(b3, t3, b3) |
| 262 |
VPSUBW(b4, t4, b4) |
| 263 |
} |
| 264 |
|
| 265 |
// Registers and constants |
| 266 |
var xs [8]VecVirtual |
| 267 |
zs := [4]VecVirtual{YMM(), YMM(), YMM(), YMM()} |
| 268 |
ts := [3]VecVirtual{YMM(), YMM(), YMM()} |
| 269 |
for i := 0; i < 8; i++ { |
| 270 |
xs[i] = YMM() |
| 271 |
} |
| 272 |
|
| 273 |
q := YMM() |
| 274 |
broadcastImm16(params.Q, q) |
| 275 |
|
| 276 |
// Layers 1 - 6 |
| 277 |
for offset := 0; offset < 2; offset++ { |
| 278 |
for i := 0; i < 8; i++ { |
| 279 |
VMOVDQU(Mem{Base: pPtr, Disp: 32 * (i + offset*8)}, xs[i]) |
| 280 |
} |
| 281 |
|
| 282 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (33 + offset*4)}, zs[0]) |
| 283 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (33 + offset*4 + 1)}, zs[1]) |
| 284 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (33 + offset*4 + 2)}, zs[2]) |
| 285 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (33 + offset*4 + 3)}, zs[3]) |
| 286 |
|
| 287 |
// Layer 1 (inverse of 7) |
| 288 |
|
| 289 |
gsButterfly( |
| 290 |
xs[0], xs[2], // a1, b1 |
| 291 |
xs[1], xs[3], // a2, b2 |
| 292 |
zs[0], zs[1], // zs12l, zs12h |
| 293 |
xs[4], xs[6], // a3, b3, |
| 294 |
xs[5], xs[7], // a4, b4, |
| 295 |
zs[2], zs[3], // zs34l, zs34h |
| 296 |
ts[0], ts[1], ts[2], zs[0], // t1, t2, t3, t4 |
| 297 |
q, // q |
| 298 |
) |
| 299 |
|
| 300 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (41 + offset*4)}, zs[0]) |
| 301 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (41 + offset*4 + 1)}, zs[1]) |
| 302 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (41 + offset*4 + 2)}, zs[2]) |
| 303 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (41 + offset*4 + 3)}, zs[3]) |
| 304 |
|
| 305 |
bitflip(0, xs[0], xs[1], ts[0]) |
| 306 |
bitflip(0, xs[2], xs[3], ts[0]) |
| 307 |
bitflip(0, xs[4], xs[5], ts[0]) |
| 308 |
bitflip(0, xs[6], xs[7], ts[0]) |
| 309 |
|
| 310 |
// Layer 2 (inverse of 6) |
| 311 |
|
| 312 |
gsButterfly( |
| 313 |
xs[0], xs[1], // a1, b1 |
| 314 |
xs[2], xs[3], // a2, b2 |
| 315 |
zs[0], zs[1], // zs12l, zs12h |
| 316 |
xs[4], xs[5], // a3, b3, |
| 317 |
xs[6], xs[7], // a4, b4, |
| 318 |
zs[2], zs[3], // zs34l, zs34h |
| 319 |
ts[0], ts[1], ts[2], zs[0], // t1, t2, t3, t4 |
| 320 |
q, // q |
| 321 |
) |
| 322 |
|
| 323 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (49 + offset*4)}, zs[0]) |
| 324 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (49 + offset*4 + 1)}, zs[1]) |
| 325 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (49 + offset*4 + 2)}, zs[2]) |
| 326 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (49 + offset*4 + 3)}, zs[3]) |
| 327 |
|
| 328 |
bitflip(1, xs[0], xs[2], ts[0]) |
| 329 |
bitflip(1, xs[1], xs[3], ts[0]) |
| 330 |
bitflip(1, xs[4], xs[6], ts[0]) |
| 331 |
bitflip(1, xs[5], xs[7], ts[0]) |
| 332 |
|
| 333 |
// Layer 3 (inverse of 5) |
| 334 |
|
| 335 |
gsButterfly( |
| 336 |
xs[0], xs[2], // a1, b1 |
| 337 |
xs[1], xs[3], // a2, b2 |
| 338 |
zs[0], zs[1], // zs12l, zs12h |
| 339 |
xs[4], xs[6], // a3, b3, |
| 340 |
xs[5], xs[7], // a4, b4, |
| 341 |
zs[2], zs[3], // zs34l, zs34h |
| 342 |
ts[0], ts[1], ts[2], zs[0], // t1, t2, t3, t4 |
| 343 |
q, // q |
| 344 |
) |
| 345 |
|
| 346 |
broadcastImm16(20159, ts[0]) |
| 347 |
barrettReduceX16(xs[0], q, ts[0], ts[1]) |
| 348 |
barrettReduceX16(xs[4], q, ts[0], ts[1]) |
| 349 |
|
| 350 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (57 + offset*4)}, zs[0]) |
| 351 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (57 + offset*4 + 1)}, zs[1]) |
| 352 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (57 + offset*4 + 2)}, zs[2]) |
| 353 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (57 + offset*4 + 3)}, zs[3]) |
| 354 |
|
| 355 |
bitflip(2, xs[0], xs[1], ts[0]) |
| 356 |
bitflip(2, xs[2], xs[3], ts[0]) |
| 357 |
bitflip(2, xs[4], xs[5], ts[0]) |
| 358 |
bitflip(2, xs[6], xs[7], ts[0]) |
| 359 |
|
| 360 |
// Layer 4 (inverse of 4) |
| 361 |
|
| 362 |
gsButterfly( |
| 363 |
xs[0], xs[1], // a1, b1 |
| 364 |
xs[2], xs[3], // a2, b2 |
| 365 |
zs[0], zs[1], // zs12l, zs12h |
| 366 |
xs[4], xs[5], // a3, b3, |
| 367 |
xs[6], xs[7], // a4, b4, |
| 368 |
zs[2], zs[3], // zs34l, zs34h |
| 369 |
ts[0], ts[1], ts[2], zs[0], // t1, t2, t3, t4 |
| 370 |
q, // q |
| 371 |
) |
| 372 |
|
| 373 |
VPBROADCASTW(Mem{Base: zetasPtr, Disp: 32*65 + 2*(4*offset)}, zs[0]) |
| 374 |
VPBROADCASTW(Mem{Base: zetasPtr, Disp: 32*65 + 2*(4*offset+1)}, zs[1]) |
| 375 |
VPBROADCASTW(Mem{Base: zetasPtr, Disp: 32*65 + 2*(4*offset+2)}, zs[2]) |
| 376 |
VPBROADCASTW(Mem{Base: zetasPtr, Disp: 32*65 + 2*(4*offset+3)}, zs[3]) |
| 377 |
|
| 378 |
bitflip(3, xs[0], xs[2], ts[0]) |
| 379 |
bitflip(3, xs[1], xs[3], ts[0]) |
| 380 |
bitflip(3, xs[4], xs[6], ts[0]) |
| 381 |
bitflip(3, xs[5], xs[7], ts[0]) |
| 382 |
|
| 383 |
// Layer 5 (inverse of 3) |
| 384 |
|
| 385 |
gsButterfly( |
| 386 |
xs[0], xs[2], // a1, b1 |
| 387 |
xs[1], xs[3], // a2, b2 |
| 388 |
zs[0], zs[1], // zs12l, zs12h |
| 389 |
xs[4], xs[6], // a3, b3, |
| 390 |
xs[5], xs[7], // a4, b4, |
| 391 |
zs[2], zs[3], // zs34l, zs34h |
| 392 |
ts[0], ts[1], ts[2], zs[0], // t1, t2, t3, t4 |
| 393 |
q, // q |
| 394 |
) |
| 395 |
|
| 396 |
broadcastImm16(20159, ts[0]) |
| 397 |
barrettReduceX16(xs[0], q, ts[0], ts[1]) |
| 398 |
barrettReduceX16(xs[4], q, ts[0], ts[1]) |
| 399 |
|
| 400 |
// Layer 6 (inverse of 2) |
| 401 |
|
| 402 |
VPBROADCASTW(Mem{Base: zetasPtr, Disp: 32*65 + 2*(8+2*offset)}, zs[0]) |
| 403 |
VPBROADCASTW(Mem{Base: zetasPtr, Disp: 32*65 + 2*(8+2*offset+1)}, zs[1]) |
| 404 |
|
| 405 |
gsButterfly( |
| 406 |
xs[0], xs[4], // a1, b1 |
| 407 |
xs[1], xs[5], // a2, b2 |
| 408 |
zs[0], zs[1], // zs12l, zs12h |
| 409 |
xs[2], xs[6], // a3, b3, |
| 410 |
xs[3], xs[7], // a4, b4, |
| 411 |
zs[0], zs[1], // zs34l, zs34h |
| 412 |
ts[0], ts[1], ts[2], zs[2], // t1, t2, t3, t4 |
| 413 |
q, // q |
| 414 |
) |
| 415 |
|
| 416 |
for i := 0; i < 8; i++ { |
| 417 |
VMOVDQU(xs[i], Mem{Base: pPtr, Disp: 32 * (i + offset*8)}) |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
// Layers 7 (inverse of 1) |
| 422 |
|
| 423 |
for offset := 0; offset < 2; offset++ { |
| 424 |
VPBROADCASTW(Mem{Base: zetasPtr, Disp: 32*65 + 2*(12)}, zs[0]) |
| 425 |
VPBROADCASTW(Mem{Base: zetasPtr, Disp: 32*65 + 2*(12+1)}, zs[1]) |
| 426 |
|
| 427 |
for i := 0; i < 4; i++ { |
| 428 |
VMOVDQU(Mem{Base: pPtr, Disp: 32 * (i + offset*4)}, xs[i]) |
| 429 |
} |
| 430 |
for i := 0; i < 4; i++ { |
| 431 |
VMOVDQU(Mem{Base: pPtr, Disp: 32 * (i + 8 + offset*4)}, xs[4+i]) |
| 432 |
} |
| 433 |
|
| 434 |
gsButterfly( |
| 435 |
xs[0], xs[4], // a1, b1 |
| 436 |
xs[1], xs[5], // a2, b2 |
| 437 |
zs[0], zs[1], // zs12l, zs12h |
| 438 |
xs[2], xs[6], // a3, b3, |
| 439 |
xs[3], xs[7], // a4, b4, |
| 440 |
zs[0], zs[1], // zs34l, zs34h |
| 441 |
zs[2], zs[3], ts[0], ts[1], // t1, t2, t3, t4 |
| 442 |
q, // q |
| 443 |
) |
| 444 |
|
| 445 |
// Finally, we set x = montgomeryReduce(x * 1441). Just like in the |
| 446 |
// the butterflies we compute the Montgomery reduction using |
| 447 |
// VPMULHWs and VPMULLWs by observing: |
| 448 |
// |
| 449 |
// m := int16(1441 * x * 62209) = int16(-10079 * x) |
| 450 |
// x' = int16(uint32(1441 * int32(x) - m * int32(Q)) >> 16) |
| 451 |
// = (uint32(1441 * int32(x)) >> 16) - (uint32(m * int32(Q)) >> 16) |
| 452 |
|
| 453 |
broadcastImm16(-10079, zs[0]) |
| 454 |
broadcastImm16(1441, zs[1]) |
| 455 |
|
| 456 |
for j := 0; j < 2; j++ { |
| 457 |
VPMULLW(xs[4*j+0], zs[0], zs[2]) // m = int16(-10079 * x) |
| 458 |
VPMULLW(xs[4*j+1], zs[0], zs[3]) |
| 459 |
VPMULLW(xs[4*j+2], zs[0], ts[0]) |
| 460 |
VPMULLW(xs[4*j+3], zs[0], ts[1]) |
| 461 |
|
| 462 |
VPMULHW(xs[4*j+0], zs[1], xs[4*j+0]) // uint32(1441*int32(x)) >> 16 |
| 463 |
VPMULHW(xs[4*j+1], zs[1], xs[4*j+1]) |
| 464 |
VPMULHW(xs[4*j+2], zs[1], xs[4*j+2]) |
| 465 |
VPMULHW(xs[4*j+3], zs[1], xs[4*j+3]) |
| 466 |
|
| 467 |
VPMULHW(zs[2], q, zs[2]) // uint32(m*int32(Q)) >> 16 |
| 468 |
VPMULHW(zs[3], q, zs[3]) |
| 469 |
VPMULHW(ts[0], q, ts[0]) |
| 470 |
VPMULHW(ts[1], q, ts[1]) |
| 471 |
|
| 472 |
VPSUBW(zs[2], xs[4*j+0], xs[4*j+0]) // computes t |
| 473 |
VPSUBW(zs[3], xs[4*j+1], xs[4*j+1]) |
| 474 |
VPSUBW(ts[0], xs[4*j+2], xs[4*j+2]) |
| 475 |
VPSUBW(ts[1], xs[4*j+3], xs[4*j+3]) |
| 476 |
} |
| 477 |
|
| 478 |
for i := 0; i < 4; i++ { |
| 479 |
VMOVDQU(xs[i], Mem{Base: pPtr, Disp: 32 * (i + offset*4)}) |
| 480 |
} |
| 481 |
for i := 0; i < 4; i++ { |
| 482 |
VMOVDQU(xs[4+i], Mem{Base: pPtr, Disp: 32 * (i + 8 + offset*4)}) |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
RET() |
| 487 |
} |
| 488 |
|
| 489 |
func nttAVX2() { |
| 490 |
// We perform almost the same operations as the generic implementation of NTT, |
| 491 |
// but use AVX2 to perform 64 butterflies at the same time. We can keep |
| 492 |
// 128 coefficients in registers at the same time. We do the first level |
| 493 |
// separately writing back to memory. Then we do levels 2 through 7 for |
| 494 |
// 128 coefficients in registers all at the same time. |
| 495 |
|
| 496 |
// As we can only perform butterflies of 8 consecutive coefficients, |
| 497 |
// we need to shuffle coefficients around. Similarly parallel |
| 498 |
// multiplication in the NTT-domain (as implemented by MulHat) requires |
| 499 |
// sequential coefficients to be pulled apart. Thus we will use a |
| 500 |
// different order of coefficients than the reference implementation. |
| 501 |
|
| 502 |
// A diagram of the order of butterflies and swaps can be found here: |
| 503 |
// |
| 504 |
// https://github.com/cloudflare/circl/wiki/images/kyber-ntt-avx2.svg |
| 505 |
// |
| 506 |
// The vertical lines with circles on the end represent butterflies. |
| 507 |
// The number in those butterflies refers to the index into the Zetas |
| 508 |
// array of which ζ is used. (Note that this array is different from |
| 509 |
// the ZetasAVX2 array, which contains the elements of Zetas many times |
| 510 |
// over in a way that is efficient for our implementation.) |
| 511 |
// |
| 512 |
// The vertical lines with crosses on them represent a swap. |
| 513 |
|
| 514 |
// Related reading: https://eprint.iacr.org/2018/039.pdf |
| 515 |
|
| 516 |
TEXT("nttAVX2", NOSPLIT, "func(p *[256]int16)") |
| 517 |
Pragma("noescape") |
| 518 |
pPtr := Load(Param("p"), GP64()) |
| 519 |
zetasPtr := GP64() |
| 520 |
LEAQ(NewDataAddr(Symbol{Name: "·ZetasAVX2"}, 0), zetasPtr) |
| 521 |
|
| 522 |
// Compute 4x16 Cooley--Tukey butterflies (a, b) ↦ (a + ζb, a - ζb). |
| 523 |
// |
| 524 |
// There is a catch: the first two and the last two sets of butterflies |
| 525 |
// have to use the same sets of zetas, as we don't have enough registers |
| 526 |
// to keep everything around. t1 up to t4 are temporary registers that |
| 527 |
// will be clobbered. |
| 528 |
ctButterfly := func(a1, b1, a2, b2, zeta12l, zeta12h, |
| 529 |
a3, b3, a4, b4, zeta34l, zeta34h, t1, t2, t3, t4, q Op) { |
| 530 |
// In the generic implementation, a single butterfly is computed as |
| 531 |
// follows (unfolding the definition of montReduce): |
| 532 |
// |
| 533 |
// m := int16(zeta * b * 62209) |
| 534 |
// t := int16(uint32(zeta * int32(b) - m * int32(Q)) >> 16) |
| 535 |
// b = a - t |
| 536 |
// a += t |
| 537 |
// |
| 538 |
// As ζb ≡ mq (mod 2¹⁶), see comments on montReduce(), we can |
| 539 |
// also compute t as |
| 540 |
// |
| 541 |
// t := (uint32(zeta * int32(b)) >> 16) - (uint32(m * int32(Q)) >> 16) |
| 542 |
// |
| 543 |
// m (x16) can be computed using a single VPMULLW with zeta * 62209 |
| 544 |
// as the second operand stored in a table. The two multiplications |
| 545 |
// and bitshifts for t can be performed using two VPMULHWs (again |
| 546 |
// for 16 at a time.) |
| 547 |
|
| 548 |
VPMULLW(b1, zeta12l, t1) // m = int16(zeta * b * 62209) |
| 549 |
VPMULLW(b2, zeta12l, t2) |
| 550 |
VPMULLW(b3, zeta34l, t3) |
| 551 |
VPMULLW(b4, zeta34l, t4) |
| 552 |
|
| 553 |
VPMULHW(b1, zeta12h, b1) // uint32(zeta*int32(b)) >> 16 |
| 554 |
VPMULHW(b2, zeta12h, b2) |
| 555 |
VPMULHW(b3, zeta34h, b3) |
| 556 |
VPMULHW(b4, zeta34h, b4) |
| 557 |
|
| 558 |
VPMULHW(t1, q, t1) // uint32(m*int32(Q)) >> 16 |
| 559 |
VPMULHW(t2, q, t2) |
| 560 |
VPMULHW(t3, q, t3) |
| 561 |
VPMULHW(t4, q, t4) |
| 562 |
|
| 563 |
VPSUBW(t1, b1, t1) // computes t |
| 564 |
VPSUBW(t2, b2, t2) |
| 565 |
VPSUBW(t3, b3, t3) |
| 566 |
VPSUBW(t4, b4, t4) |
| 567 |
|
| 568 |
VPSUBW(t1, a1, b1) // b = a - t |
| 569 |
VPSUBW(t2, a2, b2) |
| 570 |
VPSUBW(t3, a3, b3) |
| 571 |
VPSUBW(t4, a4, b4) |
| 572 |
|
| 573 |
VPADDW(t1, a1, a1) // a = a + t |
| 574 |
VPADDW(t2, a2, a2) |
| 575 |
VPADDW(t3, a3, a3) |
| 576 |
VPADDW(t4, a4, a4) |
| 577 |
} |
| 578 |
|
| 579 |
// First level: |
| 580 |
var xs [8]VecVirtual |
| 581 |
zs := [4]VecVirtual{YMM(), YMM(), YMM(), YMM()} |
| 582 |
ts := [3]VecVirtual{YMM(), YMM(), YMM()} |
| 583 |
for i := 0; i < 8; i++ { |
| 584 |
xs[i] = YMM() |
| 585 |
} |
| 586 |
|
| 587 |
q := YMM() |
| 588 |
broadcastImm16(params.Q, q) |
| 589 |
|
| 590 |
VPBROADCASTW(Mem{Base: zetasPtr}, zs[0]) |
| 591 |
VPBROADCASTW(Mem{Base: zetasPtr, Disp: 2}, zs[1]) |
| 592 |
|
| 593 |
for offset := 0; offset < 2; offset++ { |
| 594 |
for i := 0; i < 4; i++ { |
| 595 |
VMOVDQU(Mem{Base: pPtr, Disp: 32 * (i + offset*4)}, xs[i]) |
| 596 |
} |
| 597 |
for i := 0; i < 4; i++ { |
| 598 |
VMOVDQU(Mem{Base: pPtr, Disp: 32 * (i + 8 + offset*4)}, xs[4+i]) |
| 599 |
} |
| 600 |
|
| 601 |
ctButterfly( |
| 602 |
xs[0], xs[4], // a1, b1 |
| 603 |
xs[1], xs[5], // a2, b2 |
| 604 |
zs[0], zs[1], // zs12l, zs12h |
| 605 |
xs[2], xs[6], // a3, b3, |
| 606 |
xs[3], xs[7], // a4, b4, |
| 607 |
zs[0], zs[1], // zs34l, zs34h |
| 608 |
zs[2], zs[3], ts[0], ts[1], // t1, t2, t3, t4 |
| 609 |
q, // q |
| 610 |
) |
| 611 |
|
| 612 |
for i := 0; i < 4; i++ { |
| 613 |
VMOVDQU(xs[i], Mem{Base: pPtr, Disp: 32 * (i + offset*4)}) |
| 614 |
} |
| 615 |
for i := 0; i < 4; i++ { |
| 616 |
VMOVDQU(xs[4+i], Mem{Base: pPtr, Disp: 32 * (i + 8 + offset*4)}) |
| 617 |
} |
| 618 |
} |
| 619 |
|
| 620 |
// Layers 2 - 7 |
| 621 |
// |
| 622 |
// Layers 2 and 3 are straight forward. From layers 4 onwards, the |
| 623 |
// shuffling begins. |
| 624 |
for offset := 0; offset < 2; offset++ { |
| 625 |
VPBROADCASTW(Mem{Base: zetasPtr, Disp: 2 * (2 * (1 + offset))}, zs[0]) |
| 626 |
VPBROADCASTW(Mem{Base: zetasPtr, Disp: 2 * (2*(1+offset) + 1)}, zs[1]) |
| 627 |
|
| 628 |
for i := 0; i < 8; i++ { |
| 629 |
VMOVDQU(Mem{Base: pPtr, Disp: 32 * (i + offset*8)}, xs[i]) |
| 630 |
} |
| 631 |
|
| 632 |
// Layer 2 |
| 633 |
ctButterfly( |
| 634 |
xs[0], xs[4], // a1, b1 |
| 635 |
xs[1], xs[5], // a2, b2 |
| 636 |
zs[0], zs[1], // zs12l, zs12h |
| 637 |
xs[2], xs[6], // a3, b3, |
| 638 |
xs[3], xs[7], // a4, b4, |
| 639 |
zs[0], zs[1], // zs34l, zs34h |
| 640 |
zs[2], zs[3], ts[0], ts[1], // t1, t2, t3, t4 |
| 641 |
q, // q |
| 642 |
) |
| 643 |
|
| 644 |
VPBROADCASTW(Mem{Base: zetasPtr, Disp: 2 * (6 + 4*offset)}, zs[0]) |
| 645 |
VPBROADCASTW(Mem{Base: zetasPtr, Disp: 2 * ((6 + 4*offset) + 1)}, zs[1]) |
| 646 |
VPBROADCASTW(Mem{Base: zetasPtr, Disp: 2 * ((6 + 4*offset) + 2)}, zs[2]) |
| 647 |
VPBROADCASTW(Mem{Base: zetasPtr, Disp: 2 * ((6 + 4*offset) + 3)}, zs[3]) |
| 648 |
|
| 649 |
// Layer 3 |
| 650 |
ctButterfly( |
| 651 |
xs[0], xs[2], // a1, b1 |
| 652 |
xs[1], xs[3], // a2, b2 |
| 653 |
zs[0], zs[1], // zs12l, zs12h |
| 654 |
xs[4], xs[6], // a3, b3, |
| 655 |
xs[5], xs[7], // a4, b4, |
| 656 |
zs[2], zs[3], // zs34l, zs34h |
| 657 |
ts[0], ts[1], ts[2], zs[0], // t1, t2, t3, t4 |
| 658 |
q, // q |
| 659 |
) |
| 660 |
|
| 661 |
// Layer 4 |
| 662 |
// |
| 663 |
// On this layer, the butterflies are of length 16 and so would still |
| 664 |
// fit. However, the first set of butterflies uses Zetas[8], whereas |
| 665 |
// the second set uses Zetas[9]. On the next layer the butterflies |
| 666 |
// are each of length 8 and wouldn't fit. We solve both issues now |
| 667 |
// by swapping the second part of the first set with the first part |
| 668 |
// of the second set, etc. This is a bitflip() with i=4. |
| 669 |
|
| 670 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (1 + offset*4)}, zs[0]) |
| 671 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (1 + offset*4 + 1)}, zs[1]) |
| 672 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (1 + offset*4 + 2)}, zs[2]) |
| 673 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (1 + offset*4 + 3)}, zs[3]) |
| 674 |
|
| 675 |
bitflip(3, xs[0], xs[2], ts[0]) |
| 676 |
bitflip(3, xs[1], xs[3], ts[0]) |
| 677 |
bitflip(3, xs[4], xs[6], ts[0]) |
| 678 |
bitflip(3, xs[5], xs[7], ts[0]) |
| 679 |
|
| 680 |
ctButterfly( |
| 681 |
xs[0], xs[1], // a1, b1 |
| 682 |
xs[2], xs[3], // a2, b2 |
| 683 |
zs[0], zs[1], // zs12l, zs12h |
| 684 |
xs[4], xs[5], // a3, b3, |
| 685 |
xs[6], xs[7], // a4, b4, |
| 686 |
zs[2], zs[3], // zs34l, zs34h |
| 687 |
ts[0], ts[1], ts[2], zs[0], // t1, t2, t3, t4 |
| 688 |
q, // q |
| 689 |
) |
| 690 |
|
| 691 |
// Layer 5 |
| 692 |
// |
| 693 |
// On this layer, the butterflies are of length 8 and wouldn't fit |
| 694 |
// directly. However, because of the previous shuffling we have |
| 695 |
// sets of 16 consecutive butterflies. However, just like in the |
| 696 |
// previous layer, the ζs required by the two sets are different: |
| 697 |
// the first sets uses 16 & 17 whereas the second uses 18 - 19. |
| 698 |
// We solve the issue by swapping two pairs of quarters. At the same |
| 699 |
// time this swapping also ensures that the next layer has consecutive |
| 700 |
// butterflies. This is bitflip() with i=3. There is some freedom |
| 701 |
// which pairs to flip. We try to keep the permutations as local |
| 702 |
// as possible: there is only mixing between xs[0], xs[1], xs[2] |
| 703 |
// and xs[3]. As an added benefit this ensures that the final |
| 704 |
// complete permutation is convenient for multiplication. |
| 705 |
|
| 706 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (9 + offset*4)}, zs[0]) |
| 707 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (9 + offset*4 + 1)}, zs[1]) |
| 708 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (9 + offset*4 + 2)}, zs[2]) |
| 709 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (9 + offset*4 + 3)}, zs[3]) |
| 710 |
|
| 711 |
bitflip(2, xs[0], xs[1], ts[0]) |
| 712 |
bitflip(2, xs[2], xs[3], ts[0]) |
| 713 |
bitflip(2, xs[4], xs[5], ts[0]) |
| 714 |
bitflip(2, xs[6], xs[7], ts[0]) |
| 715 |
|
| 716 |
ctButterfly( |
| 717 |
xs[0], xs[2], // a1, b1 |
| 718 |
xs[1], xs[3], // a2, b2 |
| 719 |
zs[0], zs[1], // zs12l, zs12h |
| 720 |
xs[4], xs[6], // a3, b3, |
| 721 |
xs[5], xs[7], // a4, b4, |
| 722 |
zs[2], zs[3], // zs34l, zs34h |
| 723 |
ts[0], ts[1], ts[2], zs[0], // t1, t2, t3, t4 |
| 724 |
q, // q |
| 725 |
) |
| 726 |
|
| 727 |
// Layer 6 |
| 728 |
// |
| 729 |
// We continue on with the same principle. |
| 730 |
|
| 731 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (17 + offset*4)}, zs[0]) |
| 732 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (17 + offset*4 + 1)}, zs[1]) |
| 733 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (17 + offset*4 + 2)}, zs[2]) |
| 734 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (17 + offset*4 + 3)}, zs[3]) |
| 735 |
|
| 736 |
bitflip(1, xs[0], xs[2], ts[0]) |
| 737 |
bitflip(1, xs[1], xs[3], ts[0]) |
| 738 |
bitflip(1, xs[4], xs[6], ts[0]) |
| 739 |
bitflip(1, xs[5], xs[7], ts[0]) |
| 740 |
|
| 741 |
ctButterfly( |
| 742 |
xs[0], xs[1], // a1, b1 |
| 743 |
xs[2], xs[3], // a2, b2 |
| 744 |
zs[0], zs[1], // zs12l, zs12h |
| 745 |
xs[4], xs[5], // a3, b3, |
| 746 |
xs[6], xs[7], // a4, b4, |
| 747 |
zs[2], zs[3], // zs34l, zs34h |
| 748 |
ts[0], ts[1], ts[2], zs[0], // t1, t2, t3, t4 |
| 749 |
q, // q |
| 750 |
) |
| 751 |
|
| 752 |
// Layer 7 |
| 753 |
|
| 754 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (25 + offset*4)}, zs[0]) |
| 755 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (25 + offset*4 + 1)}, zs[1]) |
| 756 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (25 + offset*4 + 2)}, zs[2]) |
| 757 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (25 + offset*4 + 3)}, zs[3]) |
| 758 |
|
| 759 |
bitflip(0, xs[0], xs[1], ts[0]) |
| 760 |
bitflip(0, xs[2], xs[3], ts[0]) |
| 761 |
bitflip(0, xs[4], xs[5], ts[0]) |
| 762 |
bitflip(0, xs[6], xs[7], ts[0]) |
| 763 |
|
| 764 |
ctButterfly( |
| 765 |
xs[0], xs[2], // a1, b1 |
| 766 |
xs[1], xs[3], // a2, b2 |
| 767 |
zs[0], zs[1], // zs12l, zs12h |
| 768 |
xs[4], xs[6], // a3, b3, |
| 769 |
xs[5], xs[7], // a4, b4, |
| 770 |
zs[2], zs[3], // zs34l, zs34h |
| 771 |
ts[0], ts[1], ts[2], zs[0], // t1, t2, t3, t4 |
| 772 |
q, // q |
| 773 |
) |
| 774 |
|
| 775 |
for i := 0; i < 8; i++ { |
| 776 |
VMOVDQU(xs[i], Mem{Base: pPtr, Disp: 32 * (i + offset*8)}) |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
RET() |
| 781 |
} |
| 782 |
|
| 783 |
func mulHatAVX2() { |
| 784 |
TEXT("mulHatAVX2", NOSPLIT, "func(p, a, b *[256]int16)") |
| 785 |
Pragma("noescape") |
| 786 |
AllocLocal(8) // In case BP register is needed. |
| 787 |
|
| 788 |
pPtr := Load(Param("p"), GP64()) |
| 789 |
aPtr := Load(Param("a"), GP64()) |
| 790 |
bPtr := Load(Param("b"), GP64()) |
| 791 |
|
| 792 |
zetasPtr := GP64() |
| 793 |
LEAQ(NewDataAddr(Symbol{Name: "·ZetasAVX2"}, 0), zetasPtr) |
| 794 |
|
| 795 |
a := []Op{YMM(), YMM(), YMM(), YMM()} |
| 796 |
b := []Op{YMM(), YMM(), YMM(), YMM()} |
| 797 |
t := []Op{YMM(), YMM(), YMM(), YMM()} |
| 798 |
|
| 799 |
zl := YMM() |
| 800 |
zh := YMM() |
| 801 |
qinv := YMM() |
| 802 |
q := YMM() |
| 803 |
|
| 804 |
broadcastImm16(-3327, qinv) // = q⁻¹ (mod 2¹⁶) |
| 805 |
broadcastImm16(params.Q, q) |
| 806 |
|
| 807 |
for j := 0; j < 4; j++ { |
| 808 |
for i := 0; i < 4; i++ { |
| 809 |
VMOVDQU(Mem{Base: aPtr, Disp: 32 * (4*j + i)}, a[i]) |
| 810 |
} |
| 811 |
for i := 0; i < 4; i++ { |
| 812 |
VMOVDQU(Mem{Base: bPtr, Disp: 32 * (4*j + i)}, b[i]) |
| 813 |
} |
| 814 |
|
| 815 |
// Recall that quite conveniently for this computation (when j=0), |
| 816 |
// |
| 817 |
// a[0] contains a₀, a₄, ..., a₆₀, |
| 818 |
// a[1] contains a₁, a₅, ..., a₆₁, |
| 819 |
// a[2] contains a₂, a₆, ..., a₆₂ and |
| 820 |
// a[3] contains a₃, a₇, ..., a₆₃ |
| 821 |
// |
| 822 |
// and a similar thing for b, p and j>0. |
| 823 |
|
| 824 |
// We have to compute several products of the form t=montReduce(a*b). |
| 825 |
// From the discussion in AVX2-optimized NTT, recall that we can |
| 826 |
// compute this as follows. |
| 827 |
// |
| 828 |
// m := int16(a * b * 62209) = int16(a * b * -3227) |
| 829 |
// t := int16(uint32(int32(a) * int32(b) - m * int32(Q)) >> 16) |
| 830 |
// = (uint32(int32(a) * int32(b))>>16) - (uint32(m * int32(Q))>>16) |
| 831 |
|
| 832 |
// We start with the first four lines of |
| 833 |
// |
| 834 |
// p0 := montReduce(int32(a[i+1]) * int32(b[i+1])) |
| 835 |
// p2 := montReduce(int32(a[i]) * int32(b[i])) |
| 836 |
// p1 := montReduce(int32(a[i]) * int32(b[i+1])) |
| 837 |
// p1 += montReduce(int32(a[i+1]) * int32(b[i])) |
| 838 |
// p0 = montReduce(int32(p0) * zeta) + p2 |
| 839 |
VPMULLW(a[1], b[1], t[0]) |
| 840 |
VPMULLW(a[0], b[0], t[1]) |
| 841 |
VPMULLW(a[0], b[1], t[2]) |
| 842 |
VPMULLW(a[1], b[0], t[3]) |
| 843 |
|
| 844 |
VPMULLW(t[0], qinv, t[0]) |
| 845 |
VPMULLW(t[1], qinv, t[1]) |
| 846 |
VPMULLW(t[2], qinv, t[2]) |
| 847 |
VPMULLW(t[3], qinv, t[3]) |
| 848 |
|
| 849 |
// zl and zh are used as temporary registers here |
| 850 |
VPMULHW(a[1], b[1], zl) // will end up in b[0] |
| 851 |
VPMULHW(a[0], b[0], zh) // will end up in b[1] |
| 852 |
VPMULHW(a[0], b[1], a[0]) |
| 853 |
VPMULHW(a[1], b[0], a[1]) |
| 854 |
VMOVDQA(zl, b[0]) |
| 855 |
VMOVDQA(zh, b[1]) |
| 856 |
|
| 857 |
VPMULHW(t[0], q, t[0]) |
| 858 |
VPMULHW(t[1], q, t[1]) |
| 859 |
VPMULHW(t[2], q, t[2]) |
| 860 |
VPMULHW(t[3], q, t[3]) |
| 861 |
|
| 862 |
VPSUBW(t[0], b[0], b[0]) // a[i+1]*b[i+1] |
| 863 |
VPSUBW(t[1], b[1], b[1]) // a[i]*b[i] |
| 864 |
VPSUBW(t[2], a[0], a[0]) // a[i]*b[i+1] |
| 865 |
VPSUBW(t[3], a[1], a[1]) // a[i+1]*b[i] |
| 866 |
|
| 867 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (25 + j*2)}, zl) |
| 868 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (25 + j*2 + 1)}, zh) |
| 869 |
|
| 870 |
// Compute p0 = montReduce(int32(p0) * zeta) + p2 |
| 871 |
VPMULLW(b[0], zl, t[0]) |
| 872 |
VPMULHW(b[0], zh, b[0]) |
| 873 |
VPMULHW(t[0], q, t[0]) |
| 874 |
VPSUBW(t[0], b[0], b[0]) |
| 875 |
VPADDW(b[0], b[1], b[0]) |
| 876 |
|
| 877 |
// p1 |
| 878 |
VPADDW(a[0], a[1], b[1]) |
| 879 |
|
| 880 |
// Now the same but then for the next two |
| 881 |
VPMULLW(a[3], b[3], t[0]) |
| 882 |
VPMULLW(a[2], b[2], t[1]) |
| 883 |
VPMULLW(a[2], b[3], t[2]) |
| 884 |
VPMULLW(a[3], b[2], t[3]) |
| 885 |
|
| 886 |
VPMULLW(t[0], qinv, t[0]) |
| 887 |
VPMULLW(t[1], qinv, t[1]) |
| 888 |
VPMULLW(t[2], qinv, t[2]) |
| 889 |
VPMULLW(t[3], qinv, t[3]) |
| 890 |
|
| 891 |
// zl and zh are used as temporary registers here |
| 892 |
VPMULHW(a[3], b[3], zl) // will end up in b[2] |
| 893 |
VPMULHW(a[2], b[2], zh) // will end up in b[3] |
| 894 |
VPMULHW(a[2], b[3], a[2]) |
| 895 |
VPMULHW(a[3], b[2], a[3]) |
| 896 |
VMOVDQA(zl, b[2]) |
| 897 |
VMOVDQA(zh, b[3]) |
| 898 |
|
| 899 |
VPMULHW(t[0], q, t[0]) |
| 900 |
VPMULHW(t[1], q, t[1]) |
| 901 |
VPMULHW(t[2], q, t[2]) |
| 902 |
VPMULHW(t[3], q, t[3]) |
| 903 |
|
| 904 |
VPSUBW(t[0], b[2], b[2]) // a[i+1]*b[i+1] |
| 905 |
VPSUBW(t[1], b[3], b[3]) // a[i]*b[i] |
| 906 |
VPSUBW(t[2], a[2], a[2]) // a[i]*b[i+1] |
| 907 |
VPSUBW(t[3], a[3], a[3]) // a[i+1]*b[i] |
| 908 |
|
| 909 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (25 + j*2)}, zl) |
| 910 |
VMOVDQU(Mem{Base: zetasPtr, Disp: 32 * (25 + j*2 + 1)}, zh) |
| 911 |
|
| 912 |
// Compute p0 = p2 - montReduce(int32(p0) * zeta) |
| 913 |
VPMULLW(b[2], zl, t[0]) |
| 914 |
VPMULHW(b[2], zh, b[2]) |
| 915 |
VPMULHW(t[0], q, t[0]) |
| 916 |
VPSUBW(t[0], b[2], b[2]) |
| 917 |
VPSUBW(b[2], b[3], b[2]) |
| 918 |
|
| 919 |
// p1 |
| 920 |
VPADDW(a[2], a[3], b[3]) |
| 921 |
|
| 922 |
for i := 0; i < 4; i++ { |
| 923 |
VMOVDQU(b[i], Mem{Base: pPtr, Disp: 32 * (4*j + i)}) |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
RET() |
| 928 |
} |
| 929 |
|
| 930 |
func tangleAVX2() { |
| 931 |
TEXT("tangleAVX2", NOSPLIT, "func(p *[256]int16)") |
| 932 |
Pragma("noescape") |
| 933 |
pPtr := Load(Param("p"), GP64()) |
| 934 |
|
| 935 |
var xs [8]VecVirtual |
| 936 |
for i := 0; i < 8; i++ { |
| 937 |
xs[i] = YMM() |
| 938 |
} |
| 939 |
t := YMM() |
| 940 |
|
| 941 |
for offset := 0; offset < 2; offset++ { |
| 942 |
for i := 0; i < 8; i++ { |
| 943 |
VMOVDQU(Mem{Base: pPtr, Disp: 32 * (i + offset*8)}, xs[i]) |
| 944 |
} |
| 945 |
|
| 946 |
bitflip(3, xs[0], xs[2], t) |
| 947 |
bitflip(3, xs[1], xs[3], t) |
| 948 |
bitflip(3, xs[4], xs[6], t) |
| 949 |
bitflip(3, xs[5], xs[7], t) |
| 950 |
|
| 951 |
bitflip(2, xs[0], xs[1], t) |
| 952 |
bitflip(2, xs[2], xs[3], t) |
| 953 |
bitflip(2, xs[4], xs[5], t) |
| 954 |
bitflip(2, xs[6], xs[7], t) |
| 955 |
|
| 956 |
bitflip(1, xs[0], xs[2], t) |
| 957 |
bitflip(1, xs[1], xs[3], t) |
| 958 |
bitflip(1, xs[4], xs[6], t) |
| 959 |
bitflip(1, xs[5], xs[7], t) |
| 960 |
|
| 961 |
bitflip(0, xs[0], xs[1], t) |
| 962 |
bitflip(0, xs[2], xs[3], t) |
| 963 |
bitflip(0, xs[4], xs[5], t) |
| 964 |
bitflip(0, xs[6], xs[7], t) |
| 965 |
|
| 966 |
for i := 0; i < 8; i++ { |
| 967 |
VMOVDQU(xs[i], Mem{Base: pPtr, Disp: 32 * (i + offset*8)}) |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
RET() |
| 972 |
} |
| 973 |
|
| 974 |
func detangleAVX2() { |
| 975 |
TEXT("detangleAVX2", NOSPLIT, "func(p *[256]int16)") |
| 976 |
Pragma("noescape") |
| 977 |
pPtr := Load(Param("p"), GP64()) |
| 978 |
|
| 979 |
var xs [8]VecVirtual |
| 980 |
for i := 0; i < 8; i++ { |
| 981 |
xs[i] = YMM() |
| 982 |
} |
| 983 |
t := YMM() |
| 984 |
|
| 985 |
for offset := 0; offset < 2; offset++ { |
| 986 |
for i := 0; i < 8; i++ { |
| 987 |
VMOVDQU(Mem{Base: pPtr, Disp: 32 * (i + offset*8)}, xs[i]) |
| 988 |
} |
| 989 |
|
| 990 |
bitflip(0, xs[0], xs[1], t) |
| 991 |
bitflip(0, xs[2], xs[3], t) |
| 992 |
bitflip(0, xs[4], xs[5], t) |
| 993 |
bitflip(0, xs[6], xs[7], t) |
| 994 |
|
| 995 |
bitflip(1, xs[0], xs[2], t) |
| 996 |
bitflip(1, xs[1], xs[3], t) |
| 997 |
bitflip(1, xs[4], xs[6], t) |
| 998 |
bitflip(1, xs[5], xs[7], t) |
| 999 |
|
| 1000 |
bitflip(2, xs[0], xs[1], t) |
| 1001 |
bitflip(2, xs[2], xs[3], t) |
| 1002 |
bitflip(2, xs[4], xs[5], t) |
| 1003 |
bitflip(2, xs[6], xs[7], t) |
| 1004 |
|
| 1005 |
bitflip(3, xs[0], xs[2], t) |
| 1006 |
bitflip(3, xs[1], xs[3], t) |
| 1007 |
bitflip(3, xs[4], xs[6], t) |
| 1008 |
bitflip(3, xs[5], xs[7], t) |
| 1009 |
|
| 1010 |
for i := 0; i < 8; i++ { |
| 1011 |
VMOVDQU(xs[i], Mem{Base: pPtr, Disp: 32 * (i + offset*8)}) |
| 1012 |
} |
| 1013 |
} |
| 1014 |
|
| 1015 |
RET() |
| 1016 |
} |
| 1017 |
|
| 1018 |
func barrettReduceAVX2() { |
| 1019 |
TEXT("barrettReduceAVX2", NOSPLIT, "func(p *[256]int16)") |
| 1020 |
Pragma("noescape") |
| 1021 |
pPtr := Load(Param("p"), GP64()) |
| 1022 |
|
| 1023 |
xs := [4]Op{YMM(), YMM(), YMM(), YMM()} |
| 1024 |
ts := [4]Op{YMM(), YMM(), YMM(), YMM()} |
| 1025 |
num := YMM() |
| 1026 |
q := YMM() |
| 1027 |
|
| 1028 |
broadcastImm16(params.Q, q) |
| 1029 |
broadcastImm16(20159, num) |
| 1030 |
|
| 1031 |
for offset := 0; offset < 4; offset++ { |
| 1032 |
for i := 0; i < 4; i++ { |
| 1033 |
VMOVDQU(Mem{Base: pPtr, Disp: 32 * (i + offset*4)}, xs[i]) |
| 1034 |
} |
| 1035 |
|
| 1036 |
// Recall that the Barrett reduction of x is given by |
| 1037 |
// |
| 1038 |
// x - int16((int32(x)*20159)>>26)*q |
| 1039 |
|
| 1040 |
VPMULHW(num, xs[0], ts[0]) // t := (int32(x) * 20159) >> 16 |
| 1041 |
VPMULHW(num, xs[1], ts[1]) |
| 1042 |
VPMULHW(num, xs[2], ts[2]) |
| 1043 |
VPMULHW(num, xs[3], ts[3]) |
| 1044 |
|
| 1045 |
// t = int16(t)>>10 so that t = (int32(x)*20159) >> 26 |
| 1046 |
VPSRAW(U8(10), ts[0], ts[0]) |
| 1047 |
VPSRAW(U8(10), ts[1], ts[1]) |
| 1048 |
VPSRAW(U8(10), ts[2], ts[2]) |
| 1049 |
VPSRAW(U8(10), ts[3], ts[3]) |
| 1050 |
|
| 1051 |
VPMULLW(q, ts[0], ts[0]) // t *= q |
| 1052 |
VPMULLW(q, ts[1], ts[1]) |
| 1053 |
VPMULLW(q, ts[2], ts[2]) |
| 1054 |
VPMULLW(q, ts[3], ts[3]) |
| 1055 |
|
| 1056 |
VPSUBW(ts[0], xs[0], xs[0]) // x -= t |
| 1057 |
VPSUBW(ts[1], xs[1], xs[1]) |
| 1058 |
VPSUBW(ts[2], xs[2], xs[2]) |
| 1059 |
VPSUBW(ts[3], xs[3], xs[3]) |
| 1060 |
|
| 1061 |
for i := 0; i < 4; i++ { |
| 1062 |
VMOVDQU(xs[i], Mem{Base: pPtr, Disp: 32 * (i + offset*4)}) |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
RET() |
| 1067 |
} |
| 1068 |
|
| 1069 |
func normalizeAVX2() { |
| 1070 |
TEXT("normalizeAVX2", NOSPLIT, "func(p *[256]int16)") |
| 1071 |
Pragma("noescape") |
| 1072 |
pPtr := Load(Param("p"), GP64()) |
| 1073 |
|
| 1074 |
xs := [4]Op{YMM(), YMM(), YMM(), YMM()} |
| 1075 |
ts := [4]Op{YMM(), YMM(), YMM(), YMM()} |
| 1076 |
num := YMM() |
| 1077 |
q := YMM() |
| 1078 |
|
| 1079 |
broadcastImm16(params.Q, q) |
| 1080 |
broadcastImm16(20159, num) |
| 1081 |
|
| 1082 |
for offset := 0; offset < 4; offset++ { |
| 1083 |
for i := 0; i < 4; i++ { |
| 1084 |
VMOVDQU(Mem{Base: pPtr, Disp: 32 * (i + offset*4)}, xs[i]) |
| 1085 |
} |
| 1086 |
|
| 1087 |
// Just like the generic implementation, we do a Barrett reduction |
| 1088 |
// followed by a conditional subtraction. |
| 1089 |
|
| 1090 |
// Recall that the Barrett reduction of x is given by |
| 1091 |
// |
| 1092 |
// x - int16((int32(x)*20159)>>26)*q |
| 1093 |
|
| 1094 |
VPMULHW(num, xs[0], ts[0]) // t := (int32(x) * 20159) >> 16 |
| 1095 |
VPMULHW(num, xs[1], ts[1]) |
| 1096 |
VPMULHW(num, xs[2], ts[2]) |
| 1097 |
VPMULHW(num, xs[3], ts[3]) |
| 1098 |
|
| 1099 |
// t = int16(t)>>10 so that t = (int32(x)*20159) >> 26 |
| 1100 |
VPSRAW(U8(10), ts[0], ts[0]) |
| 1101 |
VPSRAW(U8(10), ts[1], ts[1]) |
| 1102 |
VPSRAW(U8(10), ts[2], ts[2]) |
| 1103 |
VPSRAW(U8(10), ts[3], ts[3]) |
| 1104 |
|
| 1105 |
VPMULLW(q, ts[0], ts[0]) // t *= q |
| 1106 |
VPMULLW(q, ts[1], ts[1]) |
| 1107 |
VPMULLW(q, ts[2], ts[2]) |
| 1108 |
VPMULLW(q, ts[3], ts[3]) |
| 1109 |
|
| 1110 |
VPSUBW(ts[0], xs[0], xs[0]) // x -= t |
| 1111 |
VPSUBW(ts[1], xs[1], xs[1]) |
| 1112 |
VPSUBW(ts[2], xs[2], xs[2]) |
| 1113 |
VPSUBW(ts[3], xs[3], xs[3]) |
| 1114 |
|
| 1115 |
// x is now Barrett reduced. Next we conditionally subtract q to |
| 1116 |
// normalize it. |
| 1117 |
// |
| 1118 |
// x -= Q |
| 1119 |
// x += (x >> 15) & Q |
| 1120 |
|
| 1121 |
VPSUBW(q, xs[0], xs[0]) // x -= q |
| 1122 |
VPSUBW(q, xs[1], xs[1]) |
| 1123 |
VPSUBW(q, xs[2], xs[2]) |
| 1124 |
VPSUBW(q, xs[3], xs[3]) |
| 1125 |
|
| 1126 |
VPSRAW(U8(15), xs[0], ts[0]) // t := x >> 15 |
| 1127 |
VPSRAW(U8(15), xs[1], ts[1]) |
| 1128 |
VPSRAW(U8(15), xs[2], ts[2]) |
| 1129 |
VPSRAW(U8(15), xs[3], ts[3]) |
| 1130 |
|
| 1131 |
VPAND(ts[0], q, ts[0]) // t &= q |
| 1132 |
VPAND(ts[1], q, ts[1]) |
| 1133 |
VPAND(ts[2], q, ts[2]) |
| 1134 |
VPAND(ts[3], q, ts[3]) |
| 1135 |
|
| 1136 |
VPADDW(xs[0], ts[0], xs[0]) // x += t |
| 1137 |
VPADDW(xs[1], ts[1], xs[1]) |
| 1138 |
VPADDW(xs[2], ts[2], xs[2]) |
| 1139 |
VPADDW(xs[3], ts[3], xs[3]) |
| 1140 |
|
| 1141 |
for i := 0; i < 4; i++ { |
| 1142 |
VMOVDQU(xs[i], Mem{Base: pPtr, Disp: 32 * (i + offset*4)}) |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
RET() |
| 1147 |
} |
| 1148 |
|
| 1149 |
func main() { |
| 1150 |
ConstraintExpr("amd64,!purego") |
| 1151 |
|
| 1152 |
addAVX2() |
| 1153 |
subAVX2() |
| 1154 |
nttAVX2() |
| 1155 |
invNttAVX2() |
| 1156 |
mulHatAVX2() |
| 1157 |
detangleAVX2() |
| 1158 |
tangleAVX2() |
| 1159 |
barrettReduceAVX2() |
| 1160 |
normalizeAVX2() |
| 1161 |
|
| 1162 |
Generate() |
| 1163 |
} |