Skip to content

Instantly share code, notes, and snippets.

@yaronvel
Created February 13, 2017 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaronvel/32bdc5435e43d89fe05f24db52c83742 to your computer and use it in GitHub Desktop.
Save yaronvel/32bdc5435e43d89fe05f24db52c83742 to your computer and use it in GitHub Desktop.
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