Author

Topic: Hashing code explanation? (Read 585 times)

newbie
Activity: 54
Merit: 0
May 13, 2016, 02:18:10 PM
#5
Hash(pbegin, pend) computes the hash between [pbegin, pend) and returns the value
but scrypt(pbegin, presult) hashes starting from pbegin and returns the result in presult. The length of the data is probably hard coded.


Thanks for the explanation!
sr. member
Activity: 467
Merit: 267
August 30, 2015, 09:39:39 AM
#4
Hash(pbegin, pend) computes the hash between [pbegin, pend) and returns the value
but scrypt(pbegin, presult) hashes starting from pbegin and returns the result in presult. The length of the data is probably hard coded.
member
Activity: 80
Merit: 10
August 30, 2015, 08:10:13 AM
#3
Thanks, also what does this mean and what are the difference , second one is scrypt, what thash is ?? why second one is BEGIN-BEGIN not BEGIN-END ?

Code:
uint256 GetPoWHash() const
    {
return Hash(BEGIN(nVersion), END(nNonce));
    }

Code:
uint256 GetPoWHash() const
    {
       uint256 thash;
       scrypt_1024_1_1_256(BEGIN(nVersion), BEGIN(thash));
       return thash;

    }
sr. member
Activity: 467
Merit: 267
August 28, 2015, 09:41:51 PM
#2
pbegin is a pointer to the beginning of the data, pend is a pointer to the element *just after* the end of the array.
Therefore, *pbegin is the first element but *pend is invalid since it is past your array.
However, defining pend this way simplifies many formulas. For instance, (pend-pbegin) is the number of elements of your array. (pend-pbegin)*sizeof(T) is the size of your array in bytes. sizeof(T) == sizeof(&pbegin[0]) but sizeof(T) is more readable.

if (pend==pbegin), the array is empty. The code handles this boundary case specially by passing pblank instead of pbegin, possibly because sph_shabal256(p) evaluates *p which in this case is *pend and then could crash.
However, it is a bug of sph_shabal256 to do so. The caller passes a dummy array of one byte to make it happy.






member
Activity: 80
Merit: 10
August 28, 2015, 09:22:45 PM
#1
Can anyone please explane the code below with comments ?

what are `pbegin` & `pend` ?

what is ( i believe it is data, where it comes from ? ): `(pbegin == pend ? pblank : static_cast(&pbegin[0]))`

What is (i believe it is the size of data 80 ?) : `(pend - pbegin) * sizeof(pbegin[0])`

what is : `pblank[1]`

    template
    inline uint256 HashShabal(const T1 pbegin, const T1 pend)
    {   
        sph_shabal256_context ctx_shabal;
        static unsigned char pblank[1];
        uint256 hash[1];
        sph_shabal256_init(&ctx_shabal);
        // ZSHABAL;
        sph_shabal256 (&ctx_shabal, (pbegin == pend ? pblank : static_cast(&pbegin[0])), (pend - pbegin) * sizeof(pbegin[0]));
        sph_shabal256_close(&ctx_shabal, static_cast(&hash[0]));
   
        return hash[0];
    }
Jump to: