• About
  • FAQ
  • Earn Bitcoin while Surfing the net
  • Buy & Sell Crypto on Paxful
Newsletter
Approx Foundation
  • Home
    • Home – Layout 1
  • Bitcoin
  • Ethereum
  • Regulation
  • Market
  • Blockchain
  • Business
  • Guide
  • Contact Us
No Result
View All Result
  • Home
    • Home – Layout 1
  • Bitcoin
  • Ethereum
  • Regulation
  • Market
  • Blockchain
  • Business
  • Guide
  • Contact Us
No Result
View All Result
Approx Foundation
No Result
View All Result
Home Bitcoin

python – Does Bitcoin check for high “s” value in a signature using integer or floating point arithmetic?

Moussa by Moussa
January 27, 2025
in Bitcoin
0
peer discovery – how to obtain the IP addresses of nodes for mining pools?
189
SHARES
1.5k
VIEWS
Share on FacebookShare on Twitter


In the code libraries in Programming Bitcoin by Jimmy Song in the method PrivateKey.sign, and in the Python package ecdsa.util.sigencode_der_canonize, which both support Python 3, the checks for high “s” value in a signature (r, s) use floating point arithmetic :

if s > N / 2:
    s = N - s

if s > order / 2:
    s = order - s

respectively, where N and “order” are the order of the elliptic curve secp256k1, and “/” is the Python 3 “true division” operator producing a floating point type irrespective of the type of the operands (eg. as described in [1] p146). The Python ecdsa package is not specific to Bitcoin but in its documentation (eg. here) it does mention this canonicalization is “most commonly used in bitcoin”.

This causes some high “s” values to not be detected by these libraries and thus the canonicalization is not performed and the signature they produce is then considered invalid by Bitcoin nodes. Specifically using N // 2 as the exact value of one less than the “mid point” of odd prime N (where “//” is the Python 3 “floor division” operator which discards the remainder and performs exact integer arithmetic for integer operands of arbitrary size (eg. [1] p135)) we have (using Python interpreter v3.8.10) :

N = order of secp256k1 generator point G =
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141

N // 2 = 
7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0        (exact)

>>> N / 2
5.78960446186581e+76            (approximate)

>>> s = (N // 2) + 2**127
>>> s > N / 2
False

>>> s = (N // 2) + 2**128
>>> s > N / 2
True

so all the high “s” values in at least the range [N // 2 + 1, N // 2 + 2**127] are not correctly detected.

The exact value of N / 2 should be N // 2 + 0.5, but we see it is considerably larger than that due to the floating point error:

>>> N/2 > N//2 + 2**127
True
>>> N/2 > N//2 + 2**128
False

Does Bitcoin work this way also and that is the reason for floating point usage in these libraries, to be compatible with Bitcoin? It would seem to me better to use exact integer arithmetic, even if there is not much probability that “s” could land in the range [N // 2 + 1, N // 2 + 2**127], it would seem to be better form.

[1] Mark Lutz (2013), Learning Python 5th Edition, O’Reilly



Source link

Related articles

Interactive Brokers Rolls out Ether Staking With Zerohash – Bitcoin News

Interactive Brokers Rolls out Ether Staking With Zerohash – Bitcoin News

October 13, 2026

Aptos Makes Encrypted Mempool Optional on Mainnet

October 13, 2026
Share76Tweet47

Related Posts

Interactive Brokers Rolls out Ether Staking With Zerohash – Bitcoin News

Interactive Brokers Rolls out Ether Staking With Zerohash – Bitcoin News

by Moussa
October 13, 2026
0

Key TakeawaysInteractive Brokers is taking another step into crypto, teaming up with Zerohash to let eligible clients stake ether and...

Aptos Makes Encrypted Mempool Optional on Mainnet

by Moussa
October 13, 2026
0

Aptos crypto has activated its optional Encrypted Mempool on mainnet, letting compatible wallets and apps conceal pending transaction instructions, assets...

How The Bitcoin-AI Convergence Impacts The Fight For Freedom

How The Bitcoin-AI Convergence Impacts The Fight For Freedom

by Moussa
October 13, 2026
0

Is the biggest AI risk really the end of humanity? Alex Gladstein says no: the real danger is AI centralized...

STRC Nearly Hits $100 After Bitcoin Giant Strategy Breaks Buyback Promise

STRC Nearly Hits $100 After Bitcoin Giant Strategy Breaks Buyback Promise

by Moussa
October 13, 2026
0

Key TakeawaysStrategy repurchased $368.7 million of STRC from Sept. 28 to Oct. 11 while buying just 584 BTC.STRC closed at...

Is Bitcoin Dominance Back? Altcoin Social Media Interest Falls to 2-Year Low

BTC USD Survives an $820M Liquidation Bloodbath: Can Bulls Handle CPI Next?

by Moussa
October 13, 2026
0

BTC USD dropped to around $81,000 on Monday, October 12, as a wave of forced selling hit leveraged traders, before...

Load More

youssufi.com

sephina.com

[vc_row full_width="stretch_row" parallax="content-moving" vc_row_background="" background_repeat="no-repeat" background_position="center center" footer_scheme="dark" css=".vc_custom_1517813231908{padding-top: 60px !important;padding-bottom: 30px !important;background-color: #191818 !important;background-position: center;background-repeat: no-repeat !important;background-size: cover !important;}" footer_widget_title_color="#fcbf46" footer_button_bg="#fcb11e"][vc_column width="1/4"]

We bring you the latest in Crypto News

[/vc_column][vc_column width="1/4"][vc_wp_categories]
[/vc_column][vc_column width="1/4"][vc_wp_tagcloud taxonomy="post_tag"][/vc_column][vc_column width="1/4"]

Newsletter

[vc_raw_html]JTNDcCUzRSUzQ2RpdiUyMGNsYXNzJTNEJTIydG5wJTIwdG5wLXN1YnNjcmlwdGlvbiUyMiUzRSUwQSUzQ2Zvcm0lMjBtZXRob2QlM0QlMjJwb3N0JTIyJTIwYWN0aW9uJTNEJTIyaHR0cHMlM0ElMkYlMkZhcHByb3gub3JnJTJGJTNGbmElM0RzJTIyJTNFJTBBJTBBJTNDaW5wdXQlMjB0eXBlJTNEJTIyaGlkZGVuJTIyJTIwbmFtZSUzRCUyMm5sYW5nJTIyJTIwdmFsdWUlM0QlMjIlMjIlM0UlM0NkaXYlMjBjbGFzcyUzRCUyMnRucC1maWVsZCUyMHRucC1maWVsZC1maXJzdG5hbWUlMjIlM0UlM0NsYWJlbCUyMGZvciUzRCUyMnRucC0xJTIyJTNFRmlyc3QlMjBuYW1lJTIwb3IlMjBmdWxsJTIwbmFtZSUzQyUyRmxhYmVsJTNFJTBBJTNDaW5wdXQlMjBjbGFzcyUzRCUyMnRucC1uYW1lJTIyJTIwdHlwZSUzRCUyMnRleHQlMjIlMjBuYW1lJTNEJTIybm4lMjIlMjBpZCUzRCUyMnRucC0xJTIyJTIwdmFsdWUlM0QlMjIlMjIlM0UlM0MlMkZkaXYlM0UlMEElM0NkaXYlMjBjbGFzcyUzRCUyMnRucC1maWVsZCUyMHRucC1maWVsZC1lbWFpbCUyMiUzRSUzQ2xhYmVsJTIwZm9yJTNEJTIydG5wLTIlMjIlM0VFbWFpbCUzQyUyRmxhYmVsJTNFJTBBJTNDaW5wdXQlMjBjbGFzcyUzRCUyMnRucC1lbWFpbCUyMiUyMHR5cGUlM0QlMjJlbWFpbCUyMiUyMG5hbWUlM0QlMjJuZSUyMiUyMGlkJTNEJTIydG5wLTIlMjIlMjB2YWx1ZSUzRCUyMiUyMiUyMHJlcXVpcmVkJTNFJTNDJTJGZGl2JTNFJTBBJTNDZGl2JTIwY2xhc3MlM0QlMjJ0bnAtZmllbGQlMjB0bnAtcHJpdmFjeS1maWVsZCUyMiUzRSUzQ2xhYmVsJTNFJTNDaW5wdXQlMjB0eXBlJTNEJTIyY2hlY2tib3glMjIlMjBuYW1lJTNEJTIybnklMjIlMjByZXF1aXJlZCUyMGNsYXNzJTNEJTIydG5wLXByaXZhY3klMjIlM0UlQzIlQTBCeSUyMGNvbnRpbnVpbmclMkMlMjB5b3UlMjBhY2NlcHQlMjB0aGUlMjBwcml2YWN5JTIwcG9saWN5JTNDJTJGbGFiZWwlM0UlM0MlMkZkaXYlM0UlM0NkaXYlMjBjbGFzcyUzRCUyMnRucC1maWVsZCUyMHRucC1maWVsZC1idXR0b24lMjIlM0UlM0NpbnB1dCUyMGNsYXNzJTNEJTIydG5wLXN1Ym1pdCUyMiUyMHR5cGUlM0QlMjJzdWJtaXQlMjIlMjB2YWx1ZSUzRCUyMlN1YnNjcmliZSUyMiUyMCUzRSUwQSUzQyUyRmRpdiUzRSUwQSUzQyUyRmZvcm0lM0UlMEElM0MlMkZkaXYlM0UlM0NiciUyRiUzRSUzQyUyRnAlM0U=[/vc_raw_html][/vc_column][/vc_row]
No Result
View All Result
  • Contact Us
  • Homepages
  • Business
  • Guide

© 2024 APPROX FOUNDATION - The Crypto Currency News