gpt4 book ai didi

python - Cython 中的 AES-NI 内在函数?

转载 作者:太空宇宙 更新时间:2023-11-04 03:10:10 26 4
gpt4 key购买 nike

有没有办法在 Cython 代码中使用 AES-NI 指令?

我能找到的最接近的是某人如何访问 SIMD 指令: https://groups.google.com/forum/#!msg/cython-users/nTnyI7A6sMc/a6_GnOOsLuQJ

未回答 Python 线程中的 AES-NI: Python support for AES-NI

最佳答案

您应该能够只定义内部函数,就好像它们是 Cython 中的普通 C 函数一样。有点像

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 数组”这个问题?首先,您获得字节数组的 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)

关于python - Cython 中的 AES-NI 内在函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38295835/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com