Skip to content

Commit

Permalink
Fixed MTP TMTO following Alex's recommendation
Browse files Browse the repository at this point in the history
  • Loading branch information
insomp committed Jul 15, 2017
1 parent c4fb24d commit 0d1f112
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 38 deletions.
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3472,7 +3472,7 @@ bool CBlock::AddToBlockIndex(CValidationState &state, const CDiskBlockPos &pos)
pindexNew->nUndoPos = 0;
pindexNew->nStatus = BLOCK_VALID_TRANSACTIONS | BLOCK_HAVE_DATA;
int i = 0;
for(i = 0; i < 140; i++){
for(i = 0; i < 210; i++){
pindexNew->blockhashInBlockchain[i] = blockhashInBlockchain[i];
}
pindexNew->mtpMerkleRoot = mtpMerkleRoot;
Expand Down Expand Up @@ -6215,7 +6215,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock);
pblock->nNonce = 0;
pblock->mtpMerkleRoot = uint256(0);
memset(pblock->blockhashInBlockchain, 0, sizeof(block_with_offset)*140);
memset(pblock->blockhashInBlockchain, 0, sizeof(block_with_offset)*210);
pblock->vtx[0].vin[0].scriptSig = CScript() << OP_0 << OP_0;
pblocktemplate->vTxSigOps[0] = pblock->vtx[0].GetLegacySigOpCount();

Expand Down Expand Up @@ -6431,7 +6431,7 @@ CBlockHeader CBlockIndex::GetBlockHeader() const

if(CBlockHeader::CURRENT_VERSION == 3){
int i = 0;
for(i = 0; i < 140; i++){
for(i = 0; i <= 210; i++){
block.blockhashInBlockchain[i] = blockhashInBlockchain[i];
}
block.mtpMerkleRoot = mtpMerkleRoot ;
Expand Down
20 changes: 10 additions & 10 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ unsigned char GetNfactor(int64 nTimestamp);
extern int64 nTransactionFee;
extern int64 nMinimumInputValue;

extern bool mtp_verifier(uint256 hashTarget, uint256 mtpMerkleRoot, unsigned int nNonce,const block_with_offset blockhashInBlockchain[140], uint256 *yL, uint256 blockHeader);
extern bool mtp_verifier(uint256 hashTarget, uint256 mtpMerkleRoot, unsigned int nNonce,const block_with_offset blockhashInBlockchain[210], uint256 *yL, uint256 blockHeader);

// Minimum disk space required - used in CheckDiskSpace()
static const uint64 nMinDiskSpace = 52428800;
Expand Down Expand Up @@ -1343,7 +1343,7 @@ class CBlockHeader
unsigned int nBits;
unsigned int nNonce;
boost::shared_ptr<CAuxPow> auxpow;
block_with_offset blockhashInBlockchain[140];
block_with_offset blockhashInBlockchain[211];
uint256 mtpMerkleRoot;


Expand Down Expand Up @@ -1431,7 +1431,7 @@ class CBlockHeader
nNonce = 0;

if(CBlockHeader::CURRENT_VERSION == 3){
memset(blockhashInBlockchain,0, sizeof(block_with_offset)*140);
memset(blockhashInBlockchain,0, sizeof(block_with_offset)*210);
mtpMerkleRoot = 0;
}

Expand Down Expand Up @@ -1508,7 +1508,7 @@ class CBlock : public CBlockHeader

if(CBlockHeader::CURRENT_VERSION == 3){
int i = 0;
for(i = 0; i < 140; i++){
for(i = 0; i < 210; i++){
block.blockhashInBlockchain[i] = blockhashInBlockchain[i];
}
block.mtpMerkleRoot = mtpMerkleRoot ;
Expand Down Expand Up @@ -1816,7 +1816,7 @@ class CBlockIndex
unsigned int nTime;
unsigned int nBits;
unsigned int nNonce;
block_with_offset blockhashInBlockchain[140];
block_with_offset blockhashInBlockchain[210];
uint256 mtpMerkleRoot;


Expand All @@ -1840,7 +1840,7 @@ class CBlockIndex
nBits = 0;
nNonce = 0;

memset(blockhashInBlockchain, 0, sizeof(block_with_offset)*140);
memset(blockhashInBlockchain, 0, sizeof(block_with_offset)*210);
mtpMerkleRoot = 0;
}

Expand All @@ -1866,14 +1866,14 @@ class CBlockIndex

if(fTestNet && block.LastHeight + 1 >= HF_MTP_HEIGHT_TESTNET){
int i = 0;
for(i = 0; i < 140; i++){
for(i = 0; i < 210; i++){
blockhashInBlockchain[i] = block.blockhashInBlockchain[i];
}
mtpMerkleRoot = block.mtpMerkleRoot ;

}else if(!fTestNet && block.LastHeight + 1 >= HF_MTP_HEIGHT){
int i = 0;
for(i = 0; i < 140; i++){
for(i = 0; i < 210; i++){
blockhashInBlockchain[i] = block.blockhashInBlockchain[i];
}
mtpMerkleRoot = block.mtpMerkleRoot;
Expand Down Expand Up @@ -2073,14 +2073,14 @@ class CDiskBlockIndex : public CBlockIndex

if(fTestNet && block.LastHeight + 1 >= HF_MTP_HEIGHT_TESTNET){
int i = 0;
for(i = 0; i < 140; i++){
for(i = 0; i < 210; i++){
block.blockhashInBlockchain[i] = blockhashInBlockchain[i];
}
block.mtpMerkleRoot = mtpMerkleRoot;

}else if(!fTestNet && block.LastHeight + 1 >= HF_MTP_HEIGHT){
int i = 0;
for(i = 0; i < 140; i++){
for(i = 0; i < 210; i++){
block.blockhashInBlockchain[i] = blockhashInBlockchain[i];
}
block.mtpMerkleRoot = mtpMerkleRoot;
Expand Down
88 changes: 72 additions & 16 deletions src/mtp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,37 @@ bool mtp_prover(CBlock *pblock, argon2_instance_t *instance, uint256 hashTarget,
break;
}

// current block
copy_block(&pblock->blockhashInBlockchain[(j * 3) - 1].memory, &instance->memory[ij]);
pblock->blockhashInBlockchain[(j * 3) - 1].memory.prev_block = instance->memory[ij].prev_block;
pblock->blockhashInBlockchain[(j * 3) - 1].memory.ref_block = instance->memory[ij].ref_block;

block blockhash_current;
uint8_t blockhash_bytes_current[ARGON2_BLOCK_SIZE];
copy_block(&blockhash_current, &instance->memory[ij]);
store_block(&blockhash_bytes_current, &blockhash_current);

SHA256_CTX ctx_current;
SHA256_Init(&ctx_current);
SHA256_Update(&ctx_current, blockhash_bytes_current, ARGON2_BLOCK_SIZE);
uint256 t_current;
SHA256_Final((unsigned char*)&t_current, &ctx_current);

clear_internal_memory(blockhash_current.v, ARGON2_BLOCK_SIZE);
clear_internal_memory(blockhash_bytes_current, ARGON2_BLOCK_SIZE);
blockhash_current.prev_block = NULL;
blockhash_current.ref_block = NULL;
vector<ProofNode> newproof_current = mtree.proof(t_current);


char* buffer_current = serializeMTP(newproof_current);
memcpy(pblock->blockhashInBlockchain[(j * 3) - 1].proof, buffer_current, newproof_current.size() * SHA256_LENGTH * 3 + 1);
free(buffer_current);

// previous block
copy_block(&pblock->blockhashInBlockchain[(j * 2) - 1].memory, &instance->memory[instance->memory[ij].prev_block]);
pblock->blockhashInBlockchain[(j * 2) - 1].memory.prev_block = instance->memory[instance->memory[ij].prev_block].prev_block;
pblock->blockhashInBlockchain[(j * 2) - 1].memory.ref_block = instance->memory[instance->memory[ij].prev_block].ref_block;
copy_block(&pblock->blockhashInBlockchain[(j * 3) - 2].memory, &instance->memory[instance->memory[ij].prev_block]);
pblock->blockhashInBlockchain[(j * 3) - 2].memory.prev_block = instance->memory[instance->memory[ij].prev_block].prev_block;
pblock->blockhashInBlockchain[(j * 3) - 2].memory.ref_block = instance->memory[instance->memory[ij].prev_block].ref_block;

block blockhash_previous;
uint8_t blockhash_bytes_previous[ARGON2_BLOCK_SIZE];
Expand All @@ -346,14 +373,14 @@ bool mtp_prover(CBlock *pblock, argon2_instance_t *instance, uint256 hashTarget,


char* buffer = serializeMTP(newproof);
memcpy(pblock->blockhashInBlockchain[(j * 2) - 1].proof, buffer, newproof.size() * SHA256_LENGTH * 3 + 1);
memcpy(pblock->blockhashInBlockchain[(j * 3) - 2].proof, buffer, newproof.size() * SHA256_LENGTH * 3 + 1);
free(buffer);


// ref block
copy_block(&pblock->blockhashInBlockchain[(j * 2) - 2].memory, &instance->memory[instance->memory[ij].ref_block]);
pblock->blockhashInBlockchain[(j * 2) - 2].memory.prev_block = instance->memory[instance->memory[ij].ref_block].prev_block;
pblock->blockhashInBlockchain[(j * 2) - 2].memory.ref_block = instance->memory[instance->memory[ij].ref_block].ref_block;
copy_block(&pblock->blockhashInBlockchain[(j * 3) - 3].memory, &instance->memory[instance->memory[ij].ref_block]);
pblock->blockhashInBlockchain[(j * 3) - 3].memory.prev_block = instance->memory[instance->memory[ij].ref_block].prev_block;
pblock->blockhashInBlockchain[(j * 3) - 3].memory.ref_block = instance->memory[instance->memory[ij].ref_block].ref_block;


block blockhash_ref_block;
Expand All @@ -374,17 +401,17 @@ bool mtp_prover(CBlock *pblock, argon2_instance_t *instance, uint256 hashTarget,
blockhash_ref_block.ref_block = NULL;

char* buff = serializeMTP(newproof_ref);
memcpy(pblock->blockhashInBlockchain[(j * 2) - 2].proof, buff ,newproof_ref.size() * SHA256_LENGTH * 3 + 1);
memcpy(pblock->blockhashInBlockchain[(j * 3) - 3].proof, buff ,newproof_ref.size() * SHA256_LENGTH * 3 + 1);
free(buff);

block X_IJ;
__m128i state_test[64];
uint32_t block_header[4];
memset(state_test, 0, sizeof(state_test));
memcpy(state_test, &pblock->blockhashInBlockchain[(j * 2) - 1].memory.v, ARGON2_BLOCK_SIZE);
memcpy(state_test, &pblock->blockhashInBlockchain[(j * 3) - 2].memory.v, ARGON2_BLOCK_SIZE);
uint256 hash = pblock->GetHashMTP();
memcpy(block_header, &hash, sizeof(__m128i));
fill_block(state_test, &pblock->blockhashInBlockchain[(j * 2) - 2].memory, &X_IJ, 0, block_header);
fill_block(state_test, &pblock->blockhashInBlockchain[(j * 3) - 3].memory, &X_IJ, 0, block_header);
X_IJ.prev_block = instance->memory[ij].prev_block;
X_IJ.ref_block = instance->memory[ij].ref_block;
clear_internal_memory(state_test, sizeof(__m128i) * 64);
Expand Down Expand Up @@ -480,7 +507,7 @@ bool mtp_verifier(uint256 hashTarget, CBlock *pblock, uint256 *yL) {

int i = 0;
//printf("Step 8 : Verify all block\n");
for (i = 0; i < L * 2; ++i) {
for (i = 0; i < L * 3; ++i) {
block blockhash;
copy_block(&blockhash, &pblock->blockhashInBlockchain[i].memory);
uint8_t blockhash_bytes[ARGON2_BLOCK_SIZE];
Expand Down Expand Up @@ -517,10 +544,25 @@ bool mtp_verifier(uint256 hashTarget, CBlock *pblock, uint256 *yL) {
__m128i state_test[64];
uint32_t block_header[4];
memset(state_test, 0, sizeof(state_test));
memcpy(state_test, &pblock->blockhashInBlockchain[(j * 2) - 1].memory.v, ARGON2_BLOCK_SIZE);
memcpy(state_test, &pblock->blockhashInBlockchain[(j * 3) - 2].memory.v, ARGON2_BLOCK_SIZE);
uint256 hash = pblock->GetHashMTP();
memcpy(block_header, &hash, sizeof(__m128i));
fill_block(state_test, &pblock->blockhashInBlockchain[(j * 2) - 2].memory, &X_IJ, 0, block_header);
fill_block(state_test, &pblock->blockhashInBlockchain[(j * 3) - 3].memory, &X_IJ, 0, block_header);


// check X[I(j)] from above calculation is the same as block header proof
bool unmatch_block = false;
int countIndex;
for (countIndex = 0; countIndex < 128; countIndex++) {
if (X_IJ.v[countIndex] != pblock->blockhashInBlockchain[(j * 3) - 1].memory.v[countIndex]) {
unmatch_block = true;
break;
}
}
if (unmatch_block) {
return error("CheckProofOfWork() : proof of work failed - mtp verify a derived block is not the same");
}


//Y(j) = H(Y(j - 1), X[I(j)])
block blockhash_client_tmp;
Expand Down Expand Up @@ -549,7 +591,7 @@ bool mtp_verifier(uint256 hashTarget, CBlock *pblock, uint256 *yL) {
}


bool mtp_verifier(uint256 hashTarget, uint256 mtpMerkleRoot, unsigned int nNonce,const block_with_offset blockhashInBlockchain[140], uint256 *yL, uint256 blockHeader) {
bool mtp_verifier(uint256 hashTarget, uint256 mtpMerkleRoot, unsigned int nNonce,const block_with_offset blockhashInBlockchain[210], uint256 *yL, uint256 blockHeader) {

uint256 Y_CLIENT[L + 1];

Expand Down Expand Up @@ -602,9 +644,23 @@ bool mtp_verifier(uint256 hashTarget, uint256 mtpMerkleRoot, unsigned int nNonce
__m128i state_test[64];
uint32_t block_header[4];
memset(state_test, 0, sizeof(state_test));
memcpy(state_test, &blockhashInBlockchain[(j * 2) - 1].memory.v, ARGON2_BLOCK_SIZE);
memcpy(state_test, &blockhashInBlockchain[(j * 3) - 2].memory.v, ARGON2_BLOCK_SIZE);
memcpy(block_header, &blockHeader, sizeof(__m128i));
fill_block(state_test, &blockhashInBlockchain[(j * 2) - 2].memory, &X_IJ, 0, block_header);
fill_block(state_test, &blockhashInBlockchain[(j * 3) - 3].memory, &X_IJ, 0, block_header);

// check X[I(j)] from above calculation is the same as block header proof
bool unmatch_block = false;
int countIndex;
for (countIndex = 0; countIndex < 128; countIndex++) {
if (X_IJ.v[countIndex] != blockhashInBlockchain[(j * 3) - 1].memory.v[countIndex]) {
unmatch_block = true;
break;
}
}
if (unmatch_block) {
return error("CheckProofOfWork() : proof of work failed - mtp verify a derived block is not the same");
}


//Y(j) = H(Y(j - 1), X[I(j)])
block blockhash_client_tmp;
Expand Down
2 changes: 1 addition & 1 deletion src/mtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ bool mtp_hash(uint256* output, const char* input, uint256 hashTarget, CBlock *pb

bool mtp_verifier(uint256 hashTarget, CBlock *pblock, uint256 *yL);

bool mtp_verifier(uint256 hashTarget, uint256 mtpMerkleRoot, unsigned int nNonce,const block_with_offset blockhashInBlockchain[140], uint256 *yL);
bool mtp_verifier(uint256 hashTarget, uint256 mtpMerkleRoot, unsigned int nNonce,const block_with_offset blockhashInBlockchain[210], uint256 *yL);
12 changes: 6 additions & 6 deletions src/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ inline unsigned int GetSizeOfCompactSize(uint64 nSize)
}

// argon2 block with offset
inline unsigned int GetSerializeSize(const block_with_offset data[140], int, int=0){
inline unsigned int GetSerializeSize(const block_with_offset data[210], int, int=0){
//return sizeof(block_with_offset) * 140;
unsigned int sizeData = 0;
int i =0 , r = 0, k = 0;
for( r = 0; r < 140; r++){
for( r = 0; r < 210; r++){
sizeData += sizeof(uint64_t) * ARGON2_QWORDS_IN_BLOCK; // v
sizeData += sizeof(uint64_t) * 2; // ref and prev
//sizeData += GetSizeOfCompactSize(2882); // compact
Expand All @@ -153,11 +153,11 @@ inline unsigned int GetSerializeSize(const block_with_offset data[140], int, int



template<typename Stream> inline void Serialize(Stream& s, const block_with_offset a[140], int, int=0)
template<typename Stream> inline void Serialize(Stream& s, const block_with_offset a[210], int, int=0)
{
if(a != NULL){
int i =0 , r = 0, k = 0;
for( r = 0; r < 140; r++){
for( r = 0; r < 210; r++){
for(i = 0; i < ARGON2_QWORDS_IN_BLOCK; i++){
WRITEDATA(s, a[r].memory.v[i]);
}
Expand Down Expand Up @@ -189,11 +189,11 @@ template<typename Stream> inline void Serialize(Stream& s, const block_with_offs
}
}

template<typename Stream> inline void Unserialize(Stream& s, block_with_offset a[140], int, int=0)
template<typename Stream> inline void Unserialize(Stream& s, block_with_offset a[210], int, int=0)
{
if(a != NULL){
int i = 0, r = 0, k = 0;
for( r = 0; r < 140; r++){
for( r = 0; r < 210; r++){

for(i = 0; i < ARGON2_QWORDS_IN_BLOCK; i++){
READDATA(s, a[r].memory.v[i]);
Expand Down
4 changes: 2 additions & 2 deletions src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,13 @@ bool CBlockTreeDB::LoadBlockIndexGuts()
if(fTestNet && diskindex.nHeight >= HF_MTP_HEIGHT_TESTNET){
pindexNew->mtpMerkleRoot = diskindex.mtpMerkleRoot;
int i = 0;
for(i = 0; i < 140; i++){
for(i = 0; i < 210; i++){
pindexNew->blockhashInBlockchain[i] = diskindex.blockhashInBlockchain[i];
}
}else if(!fTestNet && diskindex.nHeight >= HF_MTP_HEIGHT){
pindexNew->mtpMerkleRoot = diskindex.mtpMerkleRoot;
int i = 0;
for(i = 0; i < 140; i++){
for(i = 0; i < 210; i++){
pindexNew->blockhashInBlockchain[i] = diskindex.blockhashInBlockchain[i];
}
}
Expand Down

0 comments on commit 0d1f112

Please sign in to comment.