Giter Club home page Giter Club logo

Comments (9)

HelloZeroNet avatar HelloZeroNet commented on August 16, 2024

I made some modification to enable libsecp256k1 support in SignMessage function (PR:#12), but the early benchmarks are confusing on Win10, Python 3.7.1 64bit:

  • OpenSSL 1.0.2p compiled by indyproject: 3.22s
  • secp256k1 from coincurve pip (600k): 3.27s
  • secp256k1 extracted from secp256k1_vc120 nupkg (86k): 3.4s

Any idea why is it slower than using openssl and what the reason of the size difference in the dll?

Benchmark code:

from bitcoin.wallet import CBitcoinSecret, P2PKHBitcoinAddress
from bitcoin.signmessage import BitcoinMessage, VerifyMessage, SignMessage

address = "1N2XWu5soeppX2qUjvrf81rpdbShKJrjTr"
message = BitcoinMessage("hello")
key = CBitcoinSecret("5JsunC55XGVqFQj5kPGK4MWgTL26jKbnPhjnmchSNPo75XXCwtk")
signature = "HGbib2kv9gm9IJjDt1FXbXFczZi35u0rZR3iPUIt5GglDDCeIQ7v8eYXVNIaLoJRI4URGZrhwmsYQ9aVtRTnTfQ="  

use_libsecp256k1_for_signing(False)
signed = SignMessage(key, message)
s = time.time()
for i in range(1000):
    signed = SignMessage(key, message)

print("Signmessage (openssl) x 1000: %.2fs" % (time.time() - s))

use_libsecp256k1_for_signing(True)
signed = SignMessage(key, message)
s = time.time()
for i in range(1000):
    signed = SignMessage(key, message)

print("Signmessage (secp256k1) x 1000: %.2fs" % (time.time() - s)) 

from python-bitcointx.

dgpv avatar dgpv commented on August 16, 2024

reason of the size difference in the dll

debug/release build of the dll ?

On linux, when I increased number of iterations to 10000, there's about 5% difference

Signmessage (openssl) x 10000: 20.29s
Signmessage (secp256k1) x 10000: 21.37s

libsecp256k1 is faster for signature validation (optimized for it, to make tx validation fast), but may not necessary be faster for signature creation.

The difference may also be caused by the difference in python code paths, but I do not see any places that could be suspect, and do not feel like investigating/profiling it further.

I'm OK with just using openssl for sign_compact, because it is used only in sign_message(), and libsecp256k1 signing was added for RFC6979 deterministic signature generation for transactions, not for speed.

from python-bitcointx.

HelloZeroNet avatar HelloZeroNet commented on August 16, 2024

I did the same test using coincurve and the signature speed is more than 10 times faster:

  • Verify x1000: 0.140s
  • Sign x1000: 0.227s

The code I used: https://gist.github.com/HelloZeroNet/c82198335b430b51d86f9d6484fd91c2

Do you have and idea why is the significant difference?

from python-bitcointx.

dgpv avatar dgpv commented on August 16, 2024

probably because coincurve do much more in C code, just calling secp256k1_ecdsa_recoverable_signature() and then secp256k1_ecdsa_recoverable_signature_serialize_compact()

versus python-bitcoinlib's version that do cec_key.recover() in python, for example

from python-bitcointx.

dgpv avatar dgpv commented on August 16, 2024

signing messages in the same way as in coincurve is now implemented in branch https://github.com/Simplexum/python-bitcointx/tree/sign_libsecp256k1_default

(pull request #14, I will merge it into master later, it needs more testing before this)

But now the speed for message signing is comparable with coincurve.

Signmessage (secp256k1) x 1000: 0.07s

from python-bitcointx.

dgpv avatar dgpv commented on August 16, 2024

When secp256k1_ecdsa_verify() is used in CECKey.verify(), the speedup is roughly 4x versus openssl's ECDSA_verify()

from python-bitcointx.

HelloZeroNet avatar HelloZeroNet commented on August 16, 2024

I can confirm the performance is close to coincurve now:

Verifymessage (secp256k) x 1000: 0.17s 
Verifymessage (openssl) x 1000: 0.93s 
Verifymessage (coincurve) x 1000: 0.16s

Do you think if adding recover_compact_with_openssl function and make coincurve dll/so optional could be possible?

from python-bitcointx.

dgpv avatar dgpv commented on August 16, 2024

The changes in #14 do not use coincurve - just libsecp256k1 directly through ctypes. There's no need for additional dependency.

recover_compact_with_openssl() is an old, slow function that uses openssl - I just thought that the code might still be useful somehow, so did not delete it right away. Might do in the future, though. The main recover_compact() uses direct calls to libsecp256k1.

The changes make libsecp256k1 a hard dependency - bitcointx.core.key module won't load without libsecp256k1 present. This is the change that this issue was created for, in the first palce.

from python-bitcointx.

dgpv avatar dgpv commented on August 16, 2024

Pull request #14 merged.

libsecp256k1 is now required dependency, and is used for signing and verifying.

from python-bitcointx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.