Created
February 13, 2017 14:33
-
-
Save yaronvel/32bdc5435e43d89fe05f24db52c83742 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function verifyAgt( uint rootHash, | |
uint rootMin, | |
uint rootMax, | |
uint leaf32BytesHash, | |
uint leafNonce, | |
BlockHeader leafHeader, | |
uint branchIndex, | |
uint[] countersBranch, | |
uint[] hashesBranch ) constant internal returns(bool) { | |
uint leafCounter = leafNonce + (leafHeader.timestamp << 64); | |
uint currentHash = leaf32BytesHash & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; | |
uint leftCounter; | |
uint leftHash; | |
uint rightCounter; | |
uint rightHash; | |
uint min = leafCounter; | |
uint max = leafCounter; | |
for( uint i = 0 ; i < countersBranch.length ; i++ ) { | |
if( branchIndex & 0x1 > 0 ) { | |
leftCounter = countersBranch[i] & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; | |
leftHash = hashesBranch[i]; | |
rightCounter = max; | |
rightHash = currentHash; | |
} | |
else { | |
leftCounter = min; | |
leftHash = currentHash; | |
rightCounter = countersBranch[i] >> 128; | |
rightHash = hashesBranch[i]; | |
} | |
currentHash = uint(sha3(leftCounter,leftHash,rightCounter,rightHash)) & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; | |
min = leftCounter; | |
max = rightCounter; | |
if( min >= max ) return false; // TODO what if not a power of 2? | |
} | |
if( min != rootMin ) return false; | |
if( max != rootMax ) return false; | |
if( currentHash != rootHash ) return false; | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment