Skip to content

I am a newbie in bitcoin learning,would you please help me ?It couldn't make success.thanks. #23283

@jiebanghan

Description

@jiebanghan

Hi , I am a newbie in bitcoin learning. I am just learning the source of bitcoin. Now my aim is to change the hash algorithm in order to learn the knowledge.The version is bitcoin-0.20.
I want to change the hash to x13, so I add the x13 files in ~/src/crypto/x13hash$ ls

aes_helper.c  bmw.c	  echo.c   groestl.c  hamsi_helper.c  keccak.c	shavite.c  skein.c  sph_blake.h  sph_cubehash.h  sph_fugue.h	sph_hamsi.h  sph_keccak.h  sph_shavite.h  sph_skein.h  sph_types.h
blake.c       cubehash.c  fugue.c  hamsi.c    jh.c	      luffa.c	simd.c	   sm3.c    sph_bmw.h	 sph_echo.h	 sph_groestl.h	sph_jh.h     sph_luffa.h   sph_simd.h	  sph_sm3.h

modify the Makefile.am in src

crypto_libbitcoin_crypto_base_a_CPPFLAGS = $(AM_CPPFLAGS)
crypto_libbitcoin_crypto_base_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
crypto_libbitcoin_crypto_base_a_SOURCES = \
	#crypto/siphash.cpp \
	#crypto/siphash.h \
	crypto/aes.cpp \
	crypto/aes.h \
	crypto/chacha_poly_aead.h \
	crypto/chacha_poly_aead.cpp \
	crypto/chacha20.h \
	crypto/chacha20.cpp \
	crypto/common.h \
	crypto/hkdf_sha256_32.cpp \
	crypto/hkdf_sha256_32.h \
	crypto/hmac_sha256.cpp \
	crypto/hmac_sha256.h \
	crypto/hmac_sha512.cpp \
	crypto/hmac_sha512.h \
	crypto/poly1305.h \
	crypto/poly1305.cpp \
	crypto/ripemd160.cpp \
	crypto/ripemd160.h \
	crypto/sha1.cpp \
	crypto/sha1.h \
	crypto/sha256.cpp \
	crypto/sha256.h \
	crypto/sha512.cpp \
	crypto/sha512.h \
	crypto/siphash.cpp \
	crypto/siphash.h \
	crypto/x13hash/aes_helper.c \
	crypto/x13hash/blake.c \
	crypto/x13hash/bmw.c \
	crypto/x13hash/cubehash.c \
	crypto/x13hash/echo.c \
	crypto/x13hash/fugue.c \
	crypto/x13hash/groestl.c \
	crypto/x13hash/hamsi_helper.c \
	crypto/x13hash/hamsi.c \
	crypto/x13hash/jh.c \
	crypto/x13hash/keccak.c \
	crypto/x13hash/luffa.c \
	crypto/x13hash/shavite.c \
	crypto/x13hash/simd.c \
	crypto/x13hash/skein.c \
	crypto/x13hash/sm3.c \
	crypto/x13hash/sph_blake.h \
	crypto/x13hash/sph_bmw.h \
	crypto/x13hash/sph_cubehash.h \
	crypto/x13hash/sph_echo.h \
	crypto/x13hash/sph_fugue.h \
	crypto/x13hash/sph_groestl.h 
	crypto/x13hash/sph_hamsi.h \
	crypto/x13hash/sph_jh.h \
	crypto/x13hash/sph_keccak.h \
	crypto/x13hash/sph_luffa.h \
	crypto/x13hash/sph_shavite.h \
	crypto/x13hash/sph_simd.h \
	crypto/x13hash/sph_skein.h \
	crypto/x13hash/sph_sm3.h \
	crypto/x13hash/sph_types.h
	

in ./primitives/block.cpp add below:

uint256 CBlockHeader::GetNewPoWHash() const 
 {
     return HashX13sm3(BEGIN(nVersion), END(nNonce));
 }

./primitives/block.h:

#define BEGIN(a)            ((char*)&(a))
#define END(a)              ((char*)&((&(a))[1]))
#define UBEGIN(a)           ((unsigned char*)&(a))
#define UEND(a)             ((unsigned char*)&((&(a))[1]))
#define ARRAYLEN(array)     (sizeof(array)/sizeof((array)[0]))
uint256 GetNewPoWHash() const; 

and in hash.h

#ifndef BITCOIN_HASH_H
#define BITCOIN_HASH_H

#include <crypto/common.h>
#include <crypto/ripemd160.h>
#include <crypto/sha256.h>
#include <prevector.h>
#include <serialize.h>
#include <uint256.h>
#include <version.h>

#include <vector>

#include "crypto/x13hash/sph_blake.h"
#include "crypto/x13hash/sph_bmw.h"
#include "crypto/x13hash/sph_groestl.h"
#include "crypto/x13hash/sph_jh.h"
#include "crypto/x13hash/sph_keccak.h"
#include "crypto/x13hash/sph_skein.h"
#include "crypto/x13hash/sph_luffa.h"
#include "crypto/x13hash/sph_cubehash.h"
#include "crypto/x13hash/sph_shavite.h"
#include "crypto/x13hash/sph_simd.h"
#include "crypto/x13hash/sph_echo.h"
#include "crypto/x13hash/sph_hamsi.h"
#include "crypto/x13hash/sph_fugue.h"
#include "crypto/x13hash/sph_sm3.h"

#ifdef GLOBALDEFINED
#define GLOBAL
#else
#define GLOBAL extern
#endif

GLOBAL sph_blake512_context     z_blake;
GLOBAL sph_bmw512_context       z_bmw;
GLOBAL sph_groestl512_context   z_groestl;
GLOBAL sph_jh512_context        z_jh;
GLOBAL sph_keccak512_context    z_keccak;
GLOBAL sph_skein512_context     z_skein;
GLOBAL sph_luffa512_context     z_luffa;
GLOBAL sph_cubehash512_context  z_cubehash;
GLOBAL sph_shavite512_context   z_shavite;
GLOBAL sph_simd512_context      z_simd;
GLOBAL sph_echo512_context      z_echo;
GLOBAL sph_hamsi512_context     z_hamsi;
GLOBAL sph_fugue512_context     z_fugue;
#define fillz() do { \
    sph_blake512_init(&z_blake); \
    sph_bmw512_init(&z_bmw); \
    sph_groestl512_init(&z_groestl); \
    sph_jh512_init(&z_jh); \
    sph_keccak512_init(&z_keccak); \
    sph_skein512_init(&z_skein); \
    sph_luffa512_init(&z_luffa); \
    sph_cubehash512_init(&z_cubehash); \
    sph_shavite512_init(&z_shavite); \
    sph_simd512_init(&z_simd); \
    sph_echo512_init(&z_echo); \
    sph_hamsi512_init(&z_hamsi); \
    sph_fugue512_init(&z_fugue); \
} while (0) 
typedef uint256 ChainCode;
template<typename T1>
inline uint256 HashX13(const T1 pbegin, const T1 pend)
{
    sph_blake512_context     ctx_blake;
    sph_bmw512_context       ctx_bmw;
    sph_groestl512_context   ctx_groestl;
    sph_jh512_context        ctx_jh;
    sph_keccak512_context    ctx_keccak;
    sph_skein512_context     ctx_skein;
    sph_luffa512_context     ctx_luffa;
    sph_cubehash512_context  ctx_cubehash;
    sph_shavite512_context   ctx_shavite;
    sph_simd512_context      ctx_simd;
    sph_echo512_context      ctx_echo;
    sph_hamsi512_context      ctx_hamsi;
    sph_fugue512_context      ctx_fugue;
    static unsigned char pblank[1];

#ifndef QT_NO_DEBUG
    //std::string strhash;
    //strhash = "";
#endif

    uint256 hash[34];

    sph_blake512_init(&ctx_blake);
    sph_blake512 (&ctx_blake, (pbegin == pend ? pblank : static_cast<const void*>(&pbegin[0])), (pend - pbegin) * sizeof(pbegin[0]));
    sph_blake512_close(&ctx_blake, static_cast<void*>(&hash[0]));

    sph_bmw512_init(&ctx_bmw);
    sph_bmw512 (&ctx_bmw, static_cast<const void*>(&hash[0]), 64);
    sph_bmw512_close(&ctx_bmw, static_cast<void*>(&hash[2]));

    sph_groestl512_init(&ctx_groestl);
    sph_groestl512 (&ctx_groestl, static_cast<const void*>(&hash[2]), 64);
    sph_groestl512_close(&ctx_groestl, static_cast<void*>(&hash[4]));

    sph_skein512_init(&ctx_skein);
    sph_skein512 (&ctx_skein, static_cast<const void*>(&hash[4]), 64);
    sph_skein512_close(&ctx_skein, static_cast<void*>(&hash[6]));

    sph_jh512_init(&ctx_jh);
    sph_jh512 (&ctx_jh, static_cast<const void*>(&hash[6]), 64);
    sph_jh512_close(&ctx_jh, static_cast<void*>(&hash[8]));

    sph_keccak512_init(&ctx_keccak);
    sph_keccak512 (&ctx_keccak, static_cast<const void*>(&hash[8]), 64);
    sph_keccak512_close(&ctx_keccak, static_cast<void*>(&hash[10]));

    sph_luffa512_init(&ctx_luffa);
    sph_luffa512 (&ctx_luffa, static_cast<void*>(&hash[10]), 64);
    sph_luffa512_close(&ctx_luffa, static_cast<void*>(&hash[12]));

    sph_cubehash512_init(&ctx_cubehash);
    sph_cubehash512 (&ctx_cubehash, static_cast<const void*>(&hash[12]), 64);
    sph_cubehash512_close(&ctx_cubehash, static_cast<void*>(&hash[14]));

    sph_shavite512_init(&ctx_shavite);
    sph_shavite512(&ctx_shavite, static_cast<const void*>(&hash[14]), 64);
    sph_shavite512_close(&ctx_shavite, static_cast<void*>(&hash[16]));

    sph_simd512_init(&ctx_simd);
    sph_simd512 (&ctx_simd, static_cast<const void*>(&hash[16]), 64);
    sph_simd512_close(&ctx_simd, static_cast<void*>(&hash[18]));

    sph_echo512_init(&ctx_echo);
    sph_echo512 (&ctx_echo, static_cast<const void*>(&hash[18]), 64);
    sph_echo512_close(&ctx_echo, static_cast<void*>(&hash[20]));

    sph_hamsi512_init(&ctx_hamsi);
    sph_hamsi512 (&ctx_hamsi, static_cast<const void*>(&hash[20]), 64);
    sph_hamsi512_close(&ctx_hamsi, static_cast<void*>(&hash[22]));

    sph_fugue512_init(&ctx_fugue);
    sph_fugue512 (&ctx_fugue, static_cast<const void*>(&hash[22]), 64);
    sph_fugue512_close(&ctx_fugue, static_cast<void*>(&hash[24]));


    return hash[24];
}


template<typename T1>
inline uint256 HashX13sm3(const T1 pbegin, const T1 pend)
{
    sph_blake512_context     ctx_blake;
    sph_bmw512_context       ctx_bmw;
    sph_groestl512_context   ctx_groestl;
    sph_jh512_context        ctx_jh;
    sph_keccak512_context    ctx_keccak;
    sph_skein512_context     ctx_skein;
    sm3_ctx_t                ctx_sm3;
    sph_cubehash512_context  ctx_cubehash;
    sph_shavite512_context   ctx_shavite;
    sph_simd512_context      ctx_simd;
    sph_echo512_context      ctx_echo;
    sph_hamsi512_context      ctx_hamsi;
    sph_fugue512_context      ctx_fugue;
    static unsigned char pblank[1];

#ifndef QT_NO_DEBUG
    //std::string strhash;
    //strhash = "";
#endif

    uint256 hash[34];

    sph_blake512_init(&ctx_blake);
    sph_blake512 (&ctx_blake, (pbegin == pend ? pblank : static_cast<const void*>(&pbegin[0])), (pend - pbegin) * sizeof(pbegin[0]));
    sph_blake512_close(&ctx_blake, static_cast<void*>(&hash[0]));

    sph_bmw512_init(&ctx_bmw);
    sph_bmw512 (&ctx_bmw, static_cast<const void*>(&hash[0]), 64);
    sph_bmw512_close(&ctx_bmw, static_cast<void*>(&hash[2]));

    sph_groestl512_init(&ctx_groestl);
    sph_groestl512 (&ctx_groestl, static_cast<const void*>(&hash[2]), 64);
    sph_groestl512_close(&ctx_groestl, static_cast<void*>(&hash[4]));

    sph_skein512_init(&ctx_skein);
    sph_skein512 (&ctx_skein, static_cast<const void*>(&hash[4]), 64);
    sph_skein512_close(&ctx_skein, static_cast<void*>(&hash[6]));

    sph_jh512_init(&ctx_jh);
    sph_jh512 (&ctx_jh, static_cast<const void*>(&hash[6]), 64);
    sph_jh512_close(&ctx_jh, static_cast<void*>(&hash[8]));

    sph_keccak512_init(&ctx_keccak);
    sph_keccak512 (&ctx_keccak, static_cast<const void*>(&hash[8]), 64);
    sph_keccak512_close(&ctx_keccak, static_cast<void*>(&hash[10]));

    hash[12].SetNull(); //sm3 is 256bit, just in case
    hash[13].SetNull();
    sm3_init(&ctx_sm3);
    sph_sm3(&ctx_sm3, static_cast<const void*>(&hash[10]), 64);
    sph_sm3_close(&ctx_sm3, static_cast<void*>(&hash[12]));

    sph_cubehash512_init(&ctx_cubehash);
    sph_cubehash512 (&ctx_cubehash, static_cast<const void*>(&hash[12]), 64);
    sph_cubehash512_close(&ctx_cubehash, static_cast<void*>(&hash[14]));

    sph_shavite512_init(&ctx_shavite);
    sph_shavite512(&ctx_shavite, static_cast<const void*>(&hash[14]), 64);
    sph_shavite512_close(&ctx_shavite, static_cast<void*>(&hash[16]));

    sph_simd512_init(&ctx_simd);
    sph_simd512 (&ctx_simd, static_cast<const void*>(&hash[16]), 64);
    sph_simd512_close(&ctx_simd, static_cast<void*>(&hash[18]));

    sph_echo512_init(&ctx_echo);
    sph_echo512 (&ctx_echo, static_cast<const void*>(&hash[18]), 64);
    sph_echo512_close(&ctx_echo, static_cast<void*>(&hash[20]));

    sph_hamsi512_init(&ctx_hamsi);
    sph_hamsi512 (&ctx_hamsi, static_cast<const void*>(&hash[20]), 64);
    sph_hamsi512_close(&ctx_hamsi, static_cast<void*>(&hash[22]));

    sph_fugue512_init(&ctx_fugue);
    sph_fugue512 (&ctx_fugue, static_cast<const void*>(&hash[22]), 64);
    sph_fugue512_close(&ctx_fugue, static_cast<void*>(&hash[24]));


    return hash[24];
} 

But the result is below:
gilsun91@DESKTOP-EEM7CKJ:~$ make
Making all in src
make[1]: Entering directory '/home/gilsun91/bitcoin-0.20/src'
Makefile:18525: warning: overriding recipe for target 'libbitcoin_util_a-clientversion.o'
Makefile:10842: warning: ignoring old recipe for target 'libbitcoin_util_a-clientversion.o'
make[2]: Entering directory '/home/gilsun91/bitcoin-0.20/src'
Makefile:18525: warning: overriding recipe for target 'libbitcoin_util_a-clientversion.o'
Makefile:10842: warning: ignoring old recipe for target 'libbitcoin_util_a-clientversion.o'
libbitcoin_common.a
libbitcoin_consensus.a
libbitcoin_util.a
crypto/libbitcoin_crypto_base.a crypto/libbitcoin_crypto_sse41.a crypto/libbitcoin_crypto_avx2.a crypto/libbitcoin_crypto_shani.a

leveldb/libleveldb.a crc32c/libcrc32c.a crc32c/libcrc32c_sse42.a

leveldb/libmemenv.a
secp256k1/libsecp256k1.la
univalue/libunivalue.la
/bin/bash: line 1: libbitcoin_common.a: command not found
make[2]: *** [Makefile:18525: libbitcoin_util_a-clientversion.o] Error 127
make[2]: Leaving directory '/home/gilsun91/bitcoin-0.20/src'
make[1]: *** [Makefile:16859: all-recursive] Error 1
make[1]: Leaving directory '/home/gilsun91/bitcoin-0.20/src'
make: *** [Makefile:781: all-recursive] Error 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions