Bitcoin Forum
May 12, 2024, 05:37:01 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Which of the two is correct order for signing the transaction?  (Read 94 times)
webtricks (OP)
Legendary
*
Offline Offline

Activity: 1918
Merit: 1728


View Profile
December 22, 2020, 05:45:04 AM
 #1

Consider the following unsigned raw transaction (just an example):

Code:
0100000001a2a3b7744d9021efaa94a08a54c385373fa9381203dabf317d9a83efecb76470000000001976a9143104a52d281de138aa47e944d1acc3288b9598c588acfdffffff0140420f00000000001976a914e84d6c709afa87d7f93315af93f0804cd08cd34b88ac00000000

The double SHA-256 hash of the above is (A):
Code:
ae6d2bc786fcafbcd39696276bb6539564eefdbc2f54099da4a273db5c467e8b

Hash with reverse bytes (B):
Code:
8b7e465cdb73a2a49d09542fbcfdee649553b66b279696d3bcaffc86c72b6dae

My question is: which of the above hash is signed for creating DER-encoded signature - A or B?
1715535421
Hero Member
*
Offline Offline

Posts: 1715535421

View Profile Personal Message (Offline)

Ignore
1715535421
Reply with quote  #2

1715535421
Report to moderator
1715535421
Hero Member
*
Offline Offline

Posts: 1715535421

View Profile Personal Message (Offline)

Ignore
1715535421
Reply with quote  #2

1715535421
Report to moderator
"There should not be any signed int. If you've found a signed int somewhere, please tell me (within the next 25 years please) and I'll change it to unsigned int." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715535421
Hero Member
*
Offline Offline

Posts: 1715535421

View Profile Personal Message (Offline)

Ignore
1715535421
Reply with quote  #2

1715535421
Report to moderator
pooya87
Legendary
*
Offline Offline

Activity: 3444
Merit: 10558



View Profile
December 22, 2020, 08:32:52 AM
Merited by webtricks (2)
 #2

My question is: which of the above hash is signed for creating DER-encoded signature - A or B?
The first one, meaning the actual 32 bytes that you get from your SHA256 function as the result of hashing the input [twice]. The second one is more like a "string" that bitcoin uses to show hashes and has no usages.

P.S. There is a mistake in what you are hashing, it appears that you have replaced the only input's signature with the pubkey script that is being spent (which is the correct step for spending P2PKH outputs) but you haven't added the SIGHASHTYPE flag at the end.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
webtricks (OP)
Legendary
*
Offline Offline

Activity: 1918
Merit: 1728


View Profile
December 22, 2020, 08:55:25 AM
 #3

My question is: which of the above hash is signed for creating DER-encoded signature - A or B?
The first one, meaning the actual 32 bytes that you get from your SHA256 function as the result of hashing the input [twice]. The second one is more like a "string" that bitcoin uses to show hashes and has no usages.

Okay thanks for the information. I read the other way around somewhere and got confused. I was wondering if bytes are reversed then why my application is working perfectly.

P.S. There is a mistake in what you are hashing, it appears that you have replaced the only input's signature with the pubkey script that is being spent (which is the correct step for spending P2PKH outputs) but you haven't added the SIGHASHTYPE flag at the end.

Yeah! I know. I just randomly picked input and addresses from the recent block, pasted those in Coinbin online tool and created the transaction. That's why I wrote - 'just an example'. In my real application, I do account for additional 4-bytes SIGHASHTYPE code.
NotATether
Legendary
*
Offline Offline

Activity: 1596
Merit: 6737


bitcoincleanup.com / bitmixlist.org


View Profile WWW
December 22, 2020, 09:10:50 AM
 #4

AFAIK, programs don't have to worry about which direction they write fields in DER-encoded files, because A would be the representation in little endian on everything running an x86 processor. B would be the typical way you send byte arrays across the network because the network order is big endian so in other words the hash that was created needs to be "flipped over" while going across the network and then flipped back when it reaches the other host. So all clients only ever see A.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
pooya87
Legendary
*
Offline Offline

Activity: 3444
Merit: 10558



View Profile
December 22, 2020, 09:24:09 AM
 #5

AFAIK, programs don't have to worry about which direction they write fields in DER-encoded files, because A would be the representation in little endian on everything running an x86 processor. B would be the typical way you send byte arrays across the network because the network order is big endian so in other words the hash that was created needs to be "flipped over" while going across the network and then flipped back when it reaches the other host. So all clients only ever see A.
Hashes are also sent as is over the network without any kind of flipping. You can read more about it on P2P networking part of dev docs. For example if your hash is 123 you send it as 123 and use it everywhere as 123 but show it to user in UI as 321.
The only other place when endianness is important is when converting a hash to an integer such as converting block header hashes to a BigInt to compare with difficulty target and you use little endian conversion there.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
NotATether
Legendary
*
Offline Offline

Activity: 1596
Merit: 6737


bitcoincleanup.com / bitmixlist.org


View Profile WWW
December 22, 2020, 09:38:26 AM
 #6

Hashes are also sent as is over the network without any kind of flipping. You can read more about it on P2P networking part of dev docs. For example if your hash is 123 you send it as 123 and use it everywhere as 123 but show it to user in UI as 321.

What I don't understand is why does the UI reverse the hash when it displays it to users if it uses the original version everywhere else? Wouldn't this be misleading?  Huh

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
pooya87
Legendary
*
Offline Offline

Activity: 3444
Merit: 10558



View Profile
December 22, 2020, 09:52:16 AM
Merited by webtricks (1), Heisenberg_Hunter (1)
 #7

What I don't understand is why does the UI reverse the hash when it displays it to users if it uses the original version everywhere else? Wouldn't this be misleading?  Huh
Because the original design by Satoshi was like that and although it is not in the protocol (as consensus rules or anything like that) it became kind of an standard.

I believe the reason for this behavior lies in the class used to hold the hash data, it is a UInt256 (as I said above when we convert hash to integer we use little-endian notation) so hashes are treated as integers already and are reversed when represented.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
webtricks (OP)
Legendary
*
Offline Offline

Activity: 1918
Merit: 1728


View Profile
December 22, 2020, 10:11:37 AM
Merited by NotATether (1)
 #8

AFAIK, programs don't have to worry about which direction they write fields in DER-encoded files, because A would be the representation in little endian on everything running an x86 processor. B would be the typical way you send byte arrays across the network because the network order is big endian so in other words the hash that was created needs to be "flipped over" while going across the network and then flipped back when it reaches the other host. So all clients only ever see A.

That I know but I wasn't asking about DER-encoding. My query was for ECDSA signature.

Here's the steps for calculating ECDSA signature (wikipedia):


As you can see, z is taken as the Ln leftmost bits of 'e' where  Ln is the bit length of the group order n. So, 'e' i.e. hash of message to be signed has to be in the specific order.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!