sm4: gcm purego optimize NewGCM

This commit is contained in:
Sun Yimin 2024-10-30 08:31:12 +08:00 committed by GitHub
parent ac075d803c
commit 559da498c9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 11 deletions

View File

@ -207,25 +207,29 @@ sm4InitEncLoop:
MOVQ $7, AX
initLoop:
// B0 * B2, Karatsuba Approach
MOVOU B2, T0
MOVOU B2, T1
MOVOU B3, T2
PCLMULQDQ $0x00, B0, T0
PCLMULQDQ $0x11, B0, T1
PCLMULQDQ $0x00, B1, T2
PCLMULQDQ $0x00, B0, T0 // B0[0] * B2[0]
PCLMULQDQ $0x11, B0, T1 // B0[1] * B2[1]
PCLMULQDQ $0x00, B1, T2 // (B0[0] + B0[1]) * (B2[0] + B2[1])
PXOR T0, T2
PXOR T1, T2
PXOR T0, T2 // (B0[0] + B0[1]) * (B2[0] + B2[1]) - B0[0] * B2[0]
PXOR T1, T2 // B0[0] * B2[1] + B0[1] * B2[0]
MOVOU T2, B4
PSLLDQ $8, B4
PSRLDQ $8, T2
PXOR B4, T0
PXOR T2, T1
PXOR T2, T1 // [T1, T0] = B0 * B2
// Fast reduction
// 1st reduction
MOVOU POLY, B2
PCLMULQDQ $0x01, T0, B2
PCLMULQDQ $0x01, T0, B2 // B2 = T0[0] * POLY[1]
PSHUFD $78, T0, T0
PXOR B2, T0
// 2nd reduction
MOVOU POLY, B2
PCLMULQDQ $0x01, T0, B2
PSHUFD $78, T0, T0

View File

@ -30,11 +30,16 @@ func (c *sm4CipherAsm) NewGCM(nonceSize, tagSize int) (cipher.AEAD, error) {
binary.BigEndian.Uint64(key[:8]),
binary.BigEndian.Uint64(key[8:]),
}
g.productTable[reverseBits(1)] = x
g.productTable[8] = x // reverseBits(1) = 8
for i := 2; i < 16; i += 2 {
g.productTable[reverseBits(i)] = gcmDouble(&g.productTable[reverseBits(i/2)])
g.productTable[reverseBits(i+1)] = gcmAdd(&g.productTable[reverseBits(i)], &x)
for j := 4; j > 0; j /= 2 {
g.productTable[j] = gcmDouble(&g.productTable[j*2])
}
for j := 2; j < 16; j *= 2 {
for k := 1; k < j; k++ {
g.productTable[j+k] = gcmAdd(&g.productTable[j], &g.productTable[k])
}
}
return g, nil