Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
我想在CUDA中编写SHA1-Function,但是当我执行该函数时,我从该函数中得到了错误的结果。当我在CPU上运行相同的功能时,可以获得正确的结果。我的SHA函数如下所示:

__device__ void  SHA1_CUDA(uint8_t input_string[], int slen, uint32_t Hash_ptr[])
{
    printf("Input string is %s, input len is %d\n", input_string, slen);
    uint32_t K[80];
    uint32_t A,B,C,D,E,TEMP;
    int r,k,ln,t,l,i,j;

    Hash_ptr[0]=0x67452301;
    Hash_ptr[1]=0xefcdab89;
    Hash_ptr[2]=0x98badcfe;
    Hash_ptr[3]=0x10325476;
    Hash_ptr[4]=0xc3d2e1f0;

    ln=slen;
    r = (int)((ln+1)/64);

    if (((ln+1) % 64) > 56)
        {
        r=r+1;
        }

    // initialize Constants

    for(t=0; t<80; t++)
        {
            if (t<20)
                {
                    K[t] = 0x5a827999;
                }

            if ((t>19)&(t<40))
                {
                    K[t] = 0x6ED9EBA1;
                }
            if ((t>39)&(t<60))
                {
                    K[t] = 0x8F1BBCDC;
                }
            if (t>59)
                {
                    K[t] = 0xca62c1d6;
                }
        }

    for(l=0; l <= r; l++)
    {
        uint32_t W[80]={0};
        //Initialize Text
        for (i=0; i<16; i++)
            {
            for(j=0; j<4; j++)
                {
                    if (4*i+j <= ln)
                    {
                        k = input_string[64*l+4*i+j];
                    }
                    else
                    {
                        k =0;
                    }

                    if (k<0)
                    {
                        k = k +256;
                    }

                    if (4*i+j == ln)
                        {
                            k = 0x80;
                        }

    //              W[i]= W[i] + k*(uint32_t)pow(256,(double)3-j);
                    W[i]= W[i] + k*expo_d[3-j];
                }
            }
        if ((W[14]==0)&(W[15]==0))
        {
            W[15]=8*slen;
        }

    // Hash Cycle


        for (t = 16; t <80; t++)
            {
                W[t] = Rol(W[t-3]^W[t-8]^W[t-14]^W[t-16],1);
            }

        A = Hash_ptr[0];
        B = Hash_ptr[1];
        C = Hash_ptr[2];
        D = Hash_ptr[3];
        E = Hash_ptr[4];


        for(t = 0; t < 80; t++)
        {
            TEMP = (Rol(A,5) + f(B,C,D,t) + E + W[t] + K[t]);
            E = D;
            D = C;
            C = Rol(B,30);
            B = A;
            A = TEMP;
        }

        Hash_ptr[0] = Hash_ptr[0] + A;
        Hash_ptr[1] = Hash_ptr[1] + B;
        Hash_ptr[2] = Hash_ptr[2] + C;
        Hash_ptr[3] = Hash_ptr[3] + D;
        Hash_ptr[4] = Hash_ptr[4] + E;

        ln = ln - 64;
    }

}


(主机功能是类似的,仅使用__host__而不是__device__)。
我的内核功能是

__global__ void test_sha(uint8_t pw[], int* pw_len, uint32_t H[])
{
    SHA1_CUDA(pw, *pw_len, H);
}


我这样称呼它

printf("\nTesting SHA\n");
    uint32_t * H_h = (uint32_t*)malloc(sizeof(uint32_t)*5);
    memset(H_h, 0, sizeof(uint32_t) * 5);
    uint32_t * H_d;
    cudaMalloc(&H_d, sizeof(uint32_t)*5);
    cudaMemcpy(H_d, H_h, 5*sizeof(uint32_t), cudaMemcpyHostToDevice);
    test_sha<<<1, 1>>>(Pass_d, Pass_len_d, H_d);
    cudaMemcpy(H_h, H_d, 5*sizeof(uint32_t), cudaMemcpyDeviceToHost);
    cudaFree(H_d);
    for(int i = 0; i < 5; i++)
        printf("%x ", H_h[i]);
    printf("\n\n");
    printf("Comparing to CPU:  \n");
    SHA1_CUDA_h(Pass_h, Pass_len, H_h);
    for(int i = 0; i < 5; i++)
        printf("%x ", H_h[i]);
    printf("\n\n");
    free(H_h);


因此,我在SHA函数中的printf函数告诉我一切都已正确传输,但是我得到了错误的结果...
我的错误在哪里?

最佳答案

问题解决了,我在函数中使用的ROL函数Rol_CUDA返回了错误的值,因此除我之外没有人可以解决问题。
对于每个想使用此功能的人:在pastebin的第51行中,应该有一个32-y,而不是-y。通过此更正,一切正常。

关于c++ - CUDA SHA计算失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24138161/

10-11 18:10