Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use starkware/crypto-cpp for starknet verify/pedersen #3

Merged
merged 10 commits into from
Jun 2, 2022
Merged

Conversation

utay
Copy link
Member

@utay utay commented May 23, 2022

Same idea as #2 but using https://github.com/starkware-libs/crypto-cpp which is audited and has existing JS bindings.

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

Use crypto-cpp for pedersen and verify functions that are called most of the time in our backend. sign is mostly called by client browsers and a k variable has to be computed which isn't trivial (https://github.com/starkware-libs/crypto-cpp/blob/master/src/starkware/crypto/ffi/js/crypto.js#L64-L65) so we decided to skip using it for now.

crypto-cpp is audited and hasn't changed since Dec 2021 so I copy/pasted the C++ code directly in this repo. I've made a few changes in CMakeLists.txt and removed presubmit.sh and Go bindings as we don't use them. To review this MR I suggest pulling the code and running git diff origin/master ':!src/crypto-cpp' to hide crypto-cpp additions.

The idea is to keep this library full JS compatible (for browsers especially) but set USE_STARKWARE_CRYPTO_CPP=true to make crypto functions use the C++ dynamic library and speed things up.

Results ⚡

(on my machine, without C++ sign)

Before:

$ yarn test
yarn run v1.22.0
$ jest ./src
[...]
Done in 16.45s.

After:

$ USE_STARKWARE_CRYPTO_CPP=true yarn test
yarn run v1.22.0
$ jest ./src
[...]
Done in 2.71s.

@utay utay self-assigned this May 23, 2022
src/starkware/signature.js Show resolved Hide resolved
src/index.ts Show resolved Hide resolved
@redox redox requested a review from piedup May 23, 2022 16:41
Copy link
Collaborator

@piedup piedup left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will I be able to install the lib on an M1?

src/starkware/signature.js Show resolved Hide resolved
src/index.ts Show resolved Hide resolved
@utay
Copy link
Member Author

utay commented Jun 2, 2022

Will I be able to install the lib on an M1?

@piedup As discussed we should make some tests about compiling the lib on M1/ARM, I'm not sure it will work as-is. starkware-libs/crypto-cpp#4 is open on starkware's repo for this and can certainly help

@utay utay merged commit 5b2859a into master Jun 2, 2022
@utay utay deleted the yu/crypto-cpp branch June 2, 2022 12:26
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