jedisct1 / wasm-crypto

A WebAssembly (via AssemblyScript) set of cryptographic primitives for building authentication and key exchange protocols.
MIT License
232 stars 22 forks source link

sha256Hash some cases wrong #42

Open suntzu93 opened 1 year ago

suntzu93 commented 1 year ago

hello,

Thank you for the great library for hashing on AssemblyScript. I'm trying to hash a bitcoin transaction from data and using sha256Hash , but some returned incorrect data. I have reviewed the input data and found no problem so I am thinking sha256hash has some problem. Can you check again ?

My code :

    let version: Uint8Array = Uint8Array.wrap(fundingTx.version.buffer);
    let inputVector: Uint8Array = Uint8Array.wrap(fundingTx.inputVector.buffer);
    let outputVector: Uint8Array = Uint8Array.wrap(fundingTx.outputVector.buffer);
    let locktime: Uint8Array = Uint8Array.wrap(fundingTx.locktime.buffer);

    let packed = new Uint8Array(version.length + inputVector.length + outputVector.length + locktime.length);
    packed.set(version,0);
    packed.set(inputVector, version.length);
    packed.set(outputVector, version.length + inputVector.length);
    packed.set(locktime, version.length + inputVector.length + outputVector.length);

    const hashData = sha256Hash(sha256Hash(packed));
    let fundingTxHash: string = bin2hex(hashData);

my data input is bytes , but I can display it as hex below :

Data 1 will return the correct data:

fundingTx.version = 0x02000000 fundingTx.inputVector = 0x017e51eabf289df407e4f5b31ad590749136ed60e92d64da0248cbf57a18883ce60100000000fdffffff fundingTx.outputVector = 0x0240420f000000000022002027075e11ac70099a08a0591e75ab790270fc5a57755b5c6ab4dac02b5bb545582cd8d011000000001600141614c1f63463e60cccc5498b81b07445795ef5c3 fundingTx.locktime = 0xf2e42400

respone : fundingTxHash = 6d3c65377150abd72ef21029771acdd70c839579bd24994d36114fd40db5a946

Data get from this eth transaction : https://goerli.etherscan.io/tx/0x04ae7cc750db0549ad8b2302a46e9eda33eae6f2d3b38bac5a1c1a9f13fa710d

Data 2 will return incorrect data:

fundingTx.version = 0x02000000 fundingTx.inputVector = 0x036af25a46e05249189ec79430ccf13acb47f9d0a89a9fcbacdde41f8b0195f9c800000000232200201ceb5ed9aebfe9f9c3d2954ae8dacdfd68138c135249566a53127ca64d4d9db7fdffffffef6611d6f0715eb783bc1f0b2a2c9654fc97fa84fc9c8e090674931b0030f8cf010000002322002053ee1f96b67fc80dee871942c5a793469749d9cd207966203aa8e066b173ec2bfdffffffe4167d4447b9fcf17a9cac242d1128ee7fcbd88b6ea473203667070d5a46787e01000000232200202897cfe01913f798e3fd0d0bb4bc5a7bd30a6d15732f40f1493b2d627fcd6c95fdffffff fundingTx.outputVector = 0x0260e3160000000000220020901f47553748a623736b7eb8fbef4760f2d512ee6e6080f7b6f56dfcd892d8fc49e815000000000017a914ef5311088575bceac84ad1dc7375f89f4dd89ea187 fundingTx.locktime = 0xf6e32400

response : fundingTxHash = b8aa9b3408bfedc1ac27fc91ae3e8b97774c32e14f13798685ec417990e8aa74 but correct is : 437db270c650c8b3fc88903626c8c32caa363917d676060486fa50f522eba74d

Data get from this transaction : https://goerli.etherscan.io/tx/0x0b7489d3e71e7051faaa1d9010ababbaa0ea613665d79272c638c31af7c8eed2

suntzu93 commented 1 year ago

I tried with this code https://github.com/ChainSafe/as-sha256/blob/master/assembly/index.ts and now everything is working perfectly, so I'm pretty sure there's something wrong with your code.