Skip to content

Conversation

@utay
Copy link
Contributor

@utay utay commented May 1, 2022

Problem

sign/verify/pedersen functions are CPU intensive and the current implementation uses https://github.com/indutny/elliptic which is very slow in JS.

Solution

I took a look at what we can do to improve this and there seem to be several options:

  1. Use Starkware's crypto-cpp repo with their crypto.js JS bindings
  2. Use https://github.com/xJonathanLEI/starknet-rs/tree/master/starknet-crypto which is a pure Rust implementation of starknet crypto operations and write our own bindings
  3. Rewrite src/starkware/ in Rust with https://github.com/RustCrypto/traits/tree/master/elliptic-curve and write our own bindings

I chose option 2. as the repo is seriously maintained, has better performance and portability than starknet-cpp, and was way easier than rewriting our own stark curve.

Results ⚡

  • sign took 200ms before vs 1ms now -> 200x faster
  • verify took 1000ms before vs 3ms now -> 333x faster
  • Test suite passed successfully in 30s before vs 2s now -> 15x faster

More serious benchmark can be done but overall it's way faster!

TODO

  • Clean the code and figure out a better way to switch between the JS and the Rust implementations. An env variable? I think the JS one is still great for ease of use (no need to compile Rust) and browser support
  • Add CI steps to build and ship the napi addon (dynamic library) in the JS lib

@utay
Copy link
Contributor Author

utay commented May 23, 2022

Closing in favor of #3

@utay utay closed this May 23, 2022
@utay utay deleted the yu/starknet-crypto-rust branch May 23, 2022 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants