Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Switch to rust-secp256k1 from libsecp256k1 #8089

Closed
tomusdrw opened this issue Feb 9, 2021 · 13 comments · Fixed by #10798
Closed

Switch to rust-secp256k1 from libsecp256k1 #8089

tomusdrw opened this issue Feb 9, 2021 · 13 comments · Fixed by #10798
Assignees
Labels
I4-annoyance The client behaves within expectations, however this “expected behaviour” itself is at issue. U2-some_time_soon Issue is worth doing soon. Z1-easy Can be fixed primarily by duplicating and adapting code by an intermediate coder

Comments

@tomusdrw
Copy link
Contributor

tomusdrw commented Feb 9, 2021

It seems that currently we use libsecp256k1 in sp_core::ecdsa implementation. As I've been told, the main reason for that was because rust-secp256k1 wouldn't compile for WASM/no_std.

It seems it's not the case any more and rust-secp256k1 is more performant, has been audited and is more commonly used, hence I see no reasons for not switching to this.

@tomusdrw tomusdrw added I4-annoyance The client behaves within expectations, however this “expected behaviour” itself is at issue. U2-some_time_soon Issue is worth doing soon. Z1-easy Can be fixed primarily by duplicating and adapting code by an intermediate coder labels Feb 9, 2021
bkchr added a commit that referenced this issue Feb 10, 2021
@tomusdrw
Copy link
Contributor Author

It seems that rust-secp256k1 might not have very good performance in intepreted WASM mode, so I think we should carefully benchmark this in both wasmtime and wasmi.

@bkchr
Copy link
Member

bkchr commented Feb 12, 2021

We don't use the crate anywhere natively in WASM. We always call into native for this.

@tarcieri
Copy link

If you're considering alternatives to libsecp256k1, perhaps consider k256:

https://github.com/RustCrypto/elliptic-curves/tree/master/k256

It's an optimized pure Rust library with performance closer to the bitcoin-core C library:

image

Also even better performance on WASM.

The k256 crate implements complete Weierstrass formulas, and also supports lazy normalization and endomorphism optimizations (which are now free of patents). You can read more about it here:

https://iqlusion.blog/k256-crate-pure-rust-projective-secp256k1-library

@jakehemmerle
Copy link
Contributor

I see @bkchr's PR solving this (at least partially) but it was closed and the branch was deleted. Was this because it needs an audit or another high-overhead reason, or was it just stale and incomplete?

@stale
Copy link

stale bot commented Jul 7, 2021

Hey, is anyone still working on this? Due to the inactivity this issue has been automatically marked as stale. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the A5-stale Pull request did not receive any updates in a long time. No review needed at this stage. Close it. label Jul 7, 2021
@jordy25519
Copy link
Contributor

It seems libsecp256k1 doesn't implement RFC6979 which causes some issues for signature verification by ethereum libraries

@stale stale bot removed the A5-stale Pull request did not receive any updates in a long time. No review needed at this stage. Close it. label Sep 3, 2021
@athei
Copy link
Member

athei commented Sep 16, 2021

We don't use the crate anywhere natively in WASM. We always call into native for this.

Except when compiling, with full-crypto, right? :

"libsecp256k1",

@davxy
Copy link
Member

davxy commented Feb 2, 2022

If you're considering alternatives to libsecp256k1, perhaps consider k256:

https://github.com/RustCrypto/elliptic-curves/tree/master/k256

It's an optimized pure Rust library with performance closer to the bitcoin-core C library:

I've done some benchmarking using the latest releases of all the libraries that we are considering for secp256k1 ecdsa signature.

The results (here) are still aligned with the ones provided by tarcieri not so long ago.

So currently secp256k1 is faster by a factor of 3 compared to (compelling) the k256 pure Rust implementation.

@tarcieri are there any plans to reduce this gap?

I wonder if secp256k1 C implementation is faster because of some low level code trick or if the difference is more of algorithmical nature.

I see there is a (now closed) issue meant to address this here.

@davxy davxy closed this as completed Feb 2, 2022
@davxy davxy reopened this Feb 2, 2022
@tarcieri
Copy link

tarcieri commented Feb 2, 2022

There are a few low hanging fruit optimizations left to pursue, like using wNAF form for verification, or precomputing basepoint tables. We have made some recent progress towards the latter.

@Srikarrao1
Copy link

Srikarrao1 commented Nov 4, 2022

What if I use both "libsecp256k1", "secp256k1" in , and we're still unable to achieve ECDSA state

@bkchr
Copy link
Member

bkchr commented Nov 4, 2022

@Srikarrao1 I don't get your question?

@Srikarrao1
Copy link

I want to acheive functionality of ECDSA crate by integrating functions such as Signer, Verifier and Signature traits in substrate based primitives, appreciate your response!

@bkchr
Copy link
Member

bkchr commented Nov 4, 2022

Still don't get it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
I4-annoyance The client behaves within expectations, however this “expected behaviour” itself is at issue. U2-some_time_soon Issue is worth doing soon. Z1-easy Can be fixed primarily by duplicating and adapting code by an intermediate coder
Projects
None yet
8 participants