Bitcoin Forum
June 15, 2024, 07:25:46 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Warning: One or more bitcointalk.org users have reported that they strongly believe that the creator of this topic is a scammer. (Login to see the detailed trust ratings.) While the bitcointalk.org administration does not verify such claims, you should proceed with extreme caution.
Pages: [1]
  Print  
Author Topic: Hashing code explanation?  (Read 569 times)
XaiZou (OP)
Member
**
Offline Offline

Activity: 80
Merit: 10


View Profile
August 29, 2015, 02:22:45 AM
 #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<const void*>(&pbegin[0]))`

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

what is : `pblank[1]`

    template<typename T1>
    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<const void*>(&pbegin[0])), (pend - pbegin) * sizeof(pbegin[0]));
        sph_shabal256_close(&ctx_shabal, static_cast<void*>(&hash[0]));
   
        return hash[0];
    }
hhanh00
Sr. Member
****
Offline Offline

Activity: 467
Merit: 266


View Profile
August 29, 2015, 02:41:51 AM
 #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.







XaiZou (OP)
Member
**
Offline Offline

Activity: 80
Merit: 10


View Profile
August 30, 2015, 01:10:13 PM
 #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;

    }
hhanh00
Sr. Member
****
Offline Offline

Activity: 467
Merit: 266


View Profile
August 30, 2015, 02:39:39 PM
 #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.

wanghaoqd
Newbie
*
Offline Offline

Activity: 54
Merit: 0



View Profile
May 13, 2016, 07: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!
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!