Title: Hashing code explanation? Post by: XaiZou on August 29, 2015, 02:22:45 AM 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]; } Title: Re: Hashing code explanation? Post by: hhanh00 on August 29, 2015, 02:41:51 AM 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. Title: Re: Hashing code explanation? Post by: XaiZou on August 30, 2015, 01:10:13 PM 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 Code: uint256 GetPoWHash() const Title: Re: Hashing code explanation? Post by: hhanh00 on August 30, 2015, 02:39:39 PM 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. Title: Re: Hashing code explanation? Post by: wanghaoqd on May 13, 2016, 07:18:10 PM 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! |