有没有办法在Cython代码中使用AES-NI指令?
我找到的最接近的是有人如何访问SIMD指令: https://groups.google.com/forum/#!msg/cython-users/nTnyI7A6sMc/a6_GnOOsLuQJ
Python线程中关于AES-NI的问题未得到回答: Python support for AES-NI
有没有办法在Cython代码中使用AES-NI指令?
我找到的最接近的是有人如何访问SIMD指令: https://groups.google.com/forum/#!msg/cython-users/nTnyI7A6sMc/a6_GnOOsLuQJ
Python线程中关于AES-NI的问题未得到回答: Python support for AES-NI
cdef extern from "emmintrin.h": # I'm going off the microsoft documentation for where the headers are
# define the datatype as an opaque type
ctypedef struct __m128i:
pass
__m128i _mm_set_epi32 (int i3, int i2, int i1, int i0)
cdef extern from "wmmintrin.h":
__m128i _mm_aesdec_si128(__m128i v,__m128i rkey)
# then in some Cython function
def f():
cdef __m128i v = _mm_set_epi32(1,2,3,4)
cdef __m128i key = _mm_set_epi32(5,6,7,8)
cdef __m128i result = _mm_aesdec_si128(v,key)
如何将此应用于 bytes
数组?首先,您需要获取 bytes 数组的 char*
。然后,使用 range
进行迭代(注意不要越界)即可。
# assuming you already have an __m128i key
cdef __m128i v
cdef char* array = python_bytes_array # auto conversion
cdef int i, j
# you NEED to ensure that the byte array has a length divisible by
# 16, otherwise you'll probably get a segmentation fault.
for i in range(0,len(python_bytes_array),16):
# go over in chunks of 16
v = _mm_set_epi8(array[i+15],array[i+14],array[i+13],
# etc... fill in the rest
array[i+1], array[i])
cdef __m128 result = _mm_aesdec_si128(v,key)
# write back to the same place?
for j in range(16):
array[i+j] = _mm_extract_epi8(result,j)
_mm_extract_epi8
16 次看起来很糟糕。如果你幸运的话,它可能会编译成单个非对齐存储,但如果不行,它将是一些令人讨厌的膨胀代码。 - Peter Cordesx
。我不知道当初为什么要加上它,但我怀疑那是个错误。没有 x
对我来说编译(并且有意义)。 - DavidW