首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cython中的AES-NI本质?

Cython中的AES-NI本质?
EN

Stack Overflow用户
提问于 2016-07-10 19:43:51
回答 1查看 650关注 0票数 3

在Cython代码中有使用AES-NI指令的方法吗?

我能找到的最接近的是有人如何访问SIMD指令:GnOOsLuQJ

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-13 15:38:21

您应该能够像定义Cython中的普通C函数一样定义本质。有点像

代码语言:javascript
复制
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来迭代它(小心不要跑出终点)。

代码语言:javascript
复制
# 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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38295835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档