Bitcoin Forum
April 25, 2024, 09:02:59 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: How to convert High-S value to Low-S value?  (Read 177 times)
Fellane (OP)
Copper Member
Newbie
*
Offline Offline

Activity: 23
Merit: 32


View Profile WWW
August 06, 2020, 03:55:25 PM
Merited by suchmoon (4), o_e_l_e_o (2), ABCbits (1), Heisenberg_Hunter (1)
 #1

I created a bitcoin transaction but I'm getting an error when trying to broadcast it:

mandatory-script-verify-flag-failed (Non-canonical signature: S value is unnecessarily high) (code 16)

I googled it and seems the problem is that S value in ScriptSig is High. I didn't find a working method to convert my High-S value to Low-s value. Could someone help me with that? Any help would be appreciated and well rewarded Cheesy. Maybe there is some python script that can convert High-S value to Low.

1714078979
Hero Member
*
Offline Offline

Posts: 1714078979

View Profile Personal Message (Offline)

Ignore
1714078979
Reply with quote  #2

1714078979
Report to moderator
In order to achieve higher forum ranks, you need both activity points and merit points.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714078979
Hero Member
*
Offline Offline

Posts: 1714078979

View Profile Personal Message (Offline)

Ignore
1714078979
Reply with quote  #2

1714078979
Report to moderator
1714078979
Hero Member
*
Offline Offline

Posts: 1714078979

View Profile Personal Message (Offline)

Ignore
1714078979
Reply with quote  #2

1714078979
Report to moderator
AdolfinWolf
Legendary
*
Offline Offline

Activity: 1946
Merit: 1427


View Profile
August 06, 2020, 04:15:54 PM
Last edit: August 06, 2020, 09:10:47 PM by AdolfinWolf
 #2

It seems like the most-used solution is to check if s > N/2, and if so, subtract N by S, so it becomes N-s

This library seems to support it, but i don't use python so don't quote me on that.
https://github.com/warner/python-ecdsa/search?q=sigencode_der_canonize&type=
Code: (src/ecdsa/util.py)
def sigencode_der_canonize(r, s, order):
    if s > order / 2:
        s = order - s

o_e_l_e_o
In memoriam
Legendary
*
Offline Offline

Activity: 2268
Merit: 18507


View Profile
August 06, 2020, 07:26:21 PM
Merited by pooya87 (1), ABCbits (1), AdolfinWolf (1), Heisenberg_Hunter (1)
 #3

divide s by N-s ?
Not quite. You simply make s equal to N-s.

If s is greater than N/2, then you would replace s with -s. Since we are using modular arthimetic, -s becomes N-s.

Here's the relevant text from BIP62:

The value S in signatures must be between 0x1 and 0x7FFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF 5D576E73 57A4501D DFE92F46 681B20A0 (inclusive). If S is too high, simply replace it by S' = 0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141 - S.
AdolfinWolf
Legendary
*
Offline Offline

Activity: 1946
Merit: 1427


View Profile
August 06, 2020, 09:08:01 PM
 #4

divide s by N-s ?
Not quite. You simply make s equal to N-s.

If s is greater than N/2, then you would replace s with -s. Since we are using modular arthimetic, -s becomes N-s.

Here's the relevant text from BIP62:

The value S in signatures must be between 0x1 and 0x7FFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF 5D576E73 57A4501D DFE92F46 681B20A0 (inclusive). If S is too high, simply replace it by S' = 0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141 - S.
Sorry, you're right. Subtract is what it should've been :O, Not sure where i came up with divide (and s by N-s.. :O), Terrible!  Roll Eyes

Fellane (OP)
Copper Member
Newbie
*
Offline Offline

Activity: 23
Merit: 32


View Profile WWW
August 06, 2020, 09:47:39 PM
 #5

divide s by N-s ?
Not quite. You simply make s equal to N-s.

If s is greater than N/2, then you would replace s with -s. Since we are using modular arthimetic, -s becomes N-s.

Here's the relevant text from BIP62:

The value S in signatures must be between 0x1 and 0x7FFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF 5D576E73 57A4501D DFE92F46 681B20A0 (inclusive). If S is too high, simply replace it by S' = 0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141 - S.

How do I replace it? I don’t get it. Is there step by step tutorial? My S value doesn’t look like anything like what your just sent.

achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3374
Merit: 6535


Just writing some code


View Profile WWW
August 06, 2020, 10:46:26 PM
Merited by ABCbits (1)
 #6

How do I replace it? I don’t get it. Is there step by step tutorial? My S value doesn’t look like anything like what your just sent.
How are you making the signature? What software are you using?



Here is a signature with high S:
Code:
30440220534d4f17b88e62b097ba36720ecee6d3389d1192f2018546aa5d23bf2f234a27022100871b3da3d77564f2589fedfce761a42a912b186d78a4da291a2eaaacbf5eb31201
The S value is:
Code:
00871b3da3d77564f2589fedfce761a42a912b186d78a4da291a2eaaacbf5eb312

In python:
Code:
>>> hex(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - 0x00871b3da3d77564f2589fedfce761a42a912b186d78a4da291a2eaaacbf5eb312)
'0x78e4c25c288a9b0da7601203189e5bd42983c47936a3c612a5a3b3e010d78e2f'
The new signature with low S is
Code:
30440220534d4f17b88e62b097ba36720ecee6d3389d1192f2018546aa5d23bf2f234a27022078e4c25c288a9b0da7601203189e5bd42983c47936a3c612a5a3b3e010d78e2f01

If you are doing this by hand, don't forget to change the length before the S value.

pooya87
Legendary
*
Offline Offline

Activity: 3430
Merit: 10499



View Profile
August 07, 2020, 03:33:43 AM
Last edit: August 07, 2020, 04:30:05 AM by pooya87
Merited by ABCbits (1), Heisenberg_Hunter (1)
 #7

How do I replace it? I don’t get it. Is there step by step tutorial? My S value doesn’t look like anything like what your just sent.

with palpable integers:
Code:
N = 23
s = 18 -> -s = -18
-18 ≡ 5 (mod 23)

If you are doing this by hand, don't forget to change the length before the S value.
things like this should never be done by hand Smiley
you missed the sequence length in your first (high s) signature which should have been 0x45 and also it is easy to miss the data push size and scriptsig length that come before all this which should also be reduced by one (or more) each. the new s may also be smaller than 32 bytes and need another size (eg 0x19) or need the positive sign byte (0x00) if its highest bit is set.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
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!