Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

op_hash160 misimplemented #14

Open
Csi18nAlistairMann opened this issue Dec 17, 2018 · 8 comments
Open

op_hash160 misimplemented #14

Csi18nAlistairMann opened this issue Dec 17, 2018 · 8 comments

Comments

@Csi18nAlistairMann
Copy link

Consider the script 74686973697361736563726574 op_hash160.
Answer expected: 0x835ca4a6a1c2baaaf63bf33c777aff2fc681b017
Answer returned: 0xa69008fd5d7297bed5b71fea578a770e150b2f92

Expected behaviour confirmed at https://www.indicrypto.com/bitcointools/tool/pubkey-to-hash, https://bitcoinprices.org/public-key-to-hash/ and by running through the current master at https://github.com/kallewoof/btcdeb.

The error derives from mishandling the preimages as ascii instead of binary.
In PHP the hash() function returns ascii formed as hexadecimal. Thus:

php > echo hash('ripemd160', hash('sha256', '74686973697361736563726574'));
a69008fd5d7297bed5b71fea578a770e150b2f92

A correct implementation converts the preimages to binary first:

function hexStringToByteString($hexString){
  $len=strlen($hexString);
  $byteString="";
  for ($i=0;$i<$len;$i=$i+2){
    $charnum=hexdec(substr($hexString,$i,2));
    $byteString.=chr($charnum);
  }
  return $byteString;
}

function op_hashop160($preimage) {
  $preimage1 = hexStringToByteString($preimage);    
  $hash1 = hash('sha256', $preimage1);    
  $preimage2 = hexStringToByteString($hash1);    
  $hash2 = hash('ripemd160', $preimage2);
  return $hash2;
}

php > echo op_hashop160('74686973697361736563726574');
835ca4a6a1c2baaaf63bf33c777aff2fc681b017

Thanks!

@ghost
Copy link

ghost commented Aug 2, 2019

except fix

@bjdweck
Copy link

bjdweck commented Aug 23, 2019

OP_SHA256 seems to suffer from a similar issue...

@jadwahab
Copy link

looks like all hash op-codes aren't working...

@Frank-Buss
Copy link

I can confirm this bug. "033a38474ff6bbbb7a3e84b8db49a1400d2cb63281636119aa85973b65d29e2592" should result in "f204e24e51ed8b17873e6e54148f70ec29ab5afc", but this program calculates "0x033A38474FF6BBBB7A3E84B8DB49A1400D2CB63281636119AA85973B65D29E2592".

Looks like this project is dead, or the maintainer. Is this fixed in one of the 48 forks?

@bitjson
Copy link

bitjson commented May 14, 2022

You may find Bitauth IDE useful for debugging scripts. It's open source here: https://github.com/bitauth/bitauth-ide

@DariusParvin
Copy link

Just wanted to add that I've run into the same bugs with OP_HASH160 and OP_HASH256

@johnzweng
Copy link

Looks like this project is dead, or the maintainer. Is this fixed in one of the 48 forks?

Seems like https://vlad15june.github.io/bitcoinIDE/build/editor.html has fixed it. :)

@0xkzam
Copy link

0xkzam commented Apr 23, 2023

@johnzweng

Looks like this project is dead, or the maintainer. Is this fixed in one of the 48 forks?

Seems like https://vlad15june.github.io/bitcoinIDE/build/editor.html has fixed it. :)

Ya. It seems to be fixed but might have broken other op_codes in the process.
try this one

2 2 OP_GREATERTHAN
0 OP_EQUALVERIFY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants