Line data Source code
1 : // Copyright (c) 2013-2014 The Bitcoin developers 2 : // Copyright (c) 2017-2021 The PIVX Core developers 3 : // Distributed under the MIT software license, see the accompanying 4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 : 6 : #include "hash.h" 7 : #include "crypto/common.h" 8 : #include "crypto/hmac_sha512.h" 9 : #include "crypto/scrypt.h" 10 : 11 61400887 : inline uint32_t ROTL32(uint32_t x, int8_t r) 12 : { 13 61400887 : return (x << r) | (x >> (32 - r)); 14 : } 15 : 16 7671986 : unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash) 17 : { 18 : // The following is MurmurHash3 (x86_32), see http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp 19 7671986 : uint32_t h1 = nHashSeed; 20 7671986 : const uint32_t c1 = 0xcc9e2d51; 21 7671986 : const uint32_t c2 = 0x1b873593; 22 : 23 7671986 : const int nblocks = vDataToHash.size() / 4; 24 : 25 : //---------- 26 : // body 27 7671986 : const uint8_t* blocks = vDataToHash.data(); 28 : 29 69065081 : for (int i = 0; i < nblocks; ++i) { 30 61393185 : uint32_t k1 = ReadLE32(blocks + i*4); 31 : 32 61393185 : k1 *= c1; 33 61393185 : k1 = ROTL32(k1, 15); 34 61393185 : k1 *= c2; 35 : 36 61393185 : h1 ^= k1; 37 61393185 : h1 = ROTL32(h1, 13); 38 61393185 : h1 = h1 * 5 + 0xe6546b64; 39 : } 40 : 41 : //---------- 42 : // tail 43 7671986 : const uint8_t* tail = vDataToHash.data() + nblocks * 4; 44 : 45 7671986 : uint32_t k1 = 0; 46 : 47 7671986 : switch (vDataToHash.size() & 3) { 48 16 : case 3: 49 16 : k1 ^= tail[2] << 16; 50 1889 : case 2: 51 1889 : k1 ^= tail[1] << 8; 52 7708 : case 1: 53 7708 : k1 ^= tail[0]; 54 7708 : k1 *= c1; 55 7708 : k1 = ROTL32(k1, 15); 56 7708 : k1 *= c2; 57 7708 : h1 ^= k1; 58 : } 59 : 60 : //---------- 61 : // finalization 62 7671986 : h1 ^= vDataToHash.size(); 63 7671986 : h1 ^= h1 >> 16; 64 7671986 : h1 *= 0x85ebca6b; 65 7671986 : h1 ^= h1 >> 13; 66 7671986 : h1 *= 0xc2b2ae35; 67 7671986 : h1 ^= h1 >> 16; 68 : 69 7671986 : return h1; 70 : } 71 : 72 102613 : void BIP32Hash(const ChainCode chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]) 73 : { 74 102613 : unsigned char num[4]; 75 102613 : num[0] = (nChild >> 24) & 0xFF; 76 102613 : num[1] = (nChild >> 16) & 0xFF; 77 102613 : num[2] = (nChild >> 8) & 0xFF; 78 102613 : num[3] = (nChild >> 0) & 0xFF; 79 205226 : CHMAC_SHA512(chainCode.begin(), chainCode.size()).Write(&header, 1).Write(data, 32).Write(num, 4).Finalize(output); 80 102613 : } 81 : 82 2 : void scrypt_hash(const char* pass, unsigned int pLen, const char* salt, unsigned int sLen, char* output, unsigned int N, unsigned int r, unsigned int p, unsigned int dkLen) 83 : { 84 2 : scrypt(pass, pLen, salt, sLen, output, N, r, p, dkLen); 85 2 : }