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

feat: Replace @stablelib/ with noble-crypto #280

Merged
merged 29 commits into from
Apr 19, 2023

Conversation

ukstv
Copy link
Contributor

@ukstv ukstv commented Apr 16, 2023

This PR replaces @StableLib dependencies with noble-crypto wherever it makes sense, i.e. only @stablelib/xchacha20poly1305 is still used.

Code difference:

  1. Jest now treats test files as ES modules
  2. @stablelib/sha256 -> @noble/hashes
  3. js-sha3 -> @noble/hashes
  4. @stablelib/random -> @noble/hashes as @noble/hashes/utils
  5. elliptic, @stablelib/ed25519, @stablelib/x25519 -> @noble/curves
  6. Inhouse Ripemd160 implementation -> @noble/hashes
  7. jwe-vectors.js turned into TS jwe-vectors.ts

Important note regarding different signatures previously created by elliptic package you could see in the test cases. Apparently, elliptic created signature with high value "s". @noble/curves signature gets lower "s" value, being the same fully valid signature.

Another issue: for EdDSASigner a private key is expected to contain 64 bytes. Really, private key is only 32 bytes. The second 32 bytes are a public key. Maybe at some point, a constraint should be relieved.

One benefit apart relying on a solid cryptography libraries, is speed. Tests in the upstream take 22s on my machine. After changes in PR, the same test suite takes 4s.

Re: @scure/base for text encoding/decoding: I am conflicted. uint8arrays and multiformats are quite often used alongside did-jwt, so adding @scure/base would add to the end application size, rather than reduce it. And, @paulmillr names you use like base64url and base64 are quite misleading if compared to multiformats analogs because of padding.

@ukstv ukstv marked this pull request as ready for review April 16, 2023 20:01
@ukstv ukstv mentioned this pull request Apr 16, 2023
Copy link
Contributor

@oed oed left a comment

Choose a reason for hiding this comment

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

That sounds like pretty significant performance gains!

Is impact on package size also positive?

@ukstv
Copy link
Contributor Author

ukstv commented Apr 17, 2023

Is impact on package size also positive?

The best way to gauge size is a bundle prepared by webpack. It contains all the dependencies included as far as I can see. According to the bundle size, current upstream is 236K, changes in the PR reduce it to 133K. Looks like ~40% reduction.

Copy link

@kdenhartog kdenhartog left a comment

Choose a reason for hiding this comment

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

I can give a solid +1 for these noble libraries. They're well tested and audited crypto libraries that weren't around when a lot of this was originally implemented. Noting I've not reviewed the code throughly just providing approval on behalf of the dependency changes. It's worth noting that this change bumps a lot of unrelated packages as well because of the usage of ^. To minimize the number of updates to the yarn.lock file it's probably worth version locking those to see only the affected changes from replacing the dependencies.

@paulmillr
Copy link
Contributor

paulmillr commented Apr 17, 2023

Re: @scure/base for text encoding/decoding: I am conflicted. uint8arrays and multiformats are quite often used alongside did-jwt

multiformat libraries are using unaudited garbage dependencies

so adding @scure/base would add to the end application size, rather than reduce it.

true, so what's needed is switching multiformats to noble - unfortunately they don't seem to care much, i've tried an issue one time and they mentioned it ain't really needed: multiformats/js-sha3#48 but open to pull requests

And, @paulmillr names you use like base64url and base64 are quite misleading if compared to multiformats analogs because of padding.

padding is easily adjustable in code. we have an open issue for the native features paulmillr/scure-base#4

@ukstv
Copy link
Contributor Author

ukstv commented Apr 18, 2023

To minimize the number of updates to the yarn.lock file it's probably worth version locking those to see only the affected changes from replacing the dependencies.

Okay, I am going to try minimising changes to yarn.lock.

@ukstv
Copy link
Contributor Author

ukstv commented Apr 18, 2023

padding is easily adjustable in code. we have an open issue for the native features

@paulmillr I am aware I can create proper base64url using combinators provided in @scure/base/utils. This seems far from optimal to me. Are you open to rename exported names according to multibase table ? As far as I know, the table is the most comprehensive list of various encoding combinations. It gives you no surprises with regards to padding and casing.

Anyway, encoding/decoding I believe is a slightly separate topic. Let's do with crypto libs first.

@paulmillr
Copy link
Contributor

Are you open to rename

if it ain't breaking backwards compat, sure thing. breaking will need a new breaking release, not optimal rn

@nklomp
Copy link
Member

nklomp commented Apr 18, 2023

Very nice work!

I haven't done a proper review, but one thing I did notice is that the e256kSigner test is producing different signatures for the same inputs/keys. What is the reason for those?

@ukstv
Copy link
Contributor Author

ukstv commented Apr 18, 2023

@nklomp Copied from the PR description :)

Important note regarding different signatures previously created by elliptic package you could see in the test cases. Apparently, elliptic created signature with high value "s". @noble/curves signature gets lower "s" value, being the same fully valid signature.

If you look closer to the changed signatures in the tests, you'll see that they begin the same. The second part encoding "s" is different.

@nklomp
Copy link
Member

nklomp commented Apr 18, 2023

Hehehe, thanks. Guess I was too excited about these changes, to read that ;)

@paulmillr
Copy link
Contributor

There is a lowS: false option that will bring the old behavior if you need it.

However, the old behavior (your current) is not safe and introduces signature malleability.

@ukstv
Copy link
Contributor Author

ukstv commented Apr 18, 2023

@kdenhartog Now yarn.lock should be fine. One could easily see scope of the changes there.

@ukstv
Copy link
Contributor Author

ukstv commented Apr 19, 2023

@mirceanis Hey, is there anything I can do to move the PR forward?

The gains are pretty significant:

  • proper audited dependencies for cryptography,
  • improved performance (5x reduction of test time on Apple M1),
  • reduced bundle size by ~40%.

@mirceanis
Copy link
Member

@mirceanis Hey, is there anything I can do to move the PR forward?

I'm reviewing it now. This looks great so far 🚀

Copy link
Member

@mirceanis mirceanis left a comment

Choose a reason for hiding this comment

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

Looks wonderful, thank you!

I'll mark this as a BREAKING CHANGE since the weierstrass signer outputs are changing and it's likely going to cause test failures for dependents.

@mirceanis mirceanis merged commit 0f6221a into decentralized-identity:master Apr 19, 2023
uport-automation-bot pushed a commit that referenced this pull request Apr 19, 2023
# [7.0.0](6.11.6...7.0.0) (2023-04-19)

### Features

* **deps:** replace @stablelib/ with noble-crypto ([#280](#280)) ([0f6221a](0f6221a)), closes [#270](#270)

### BREAKING CHANGES

* **deps:** `ES256*` signers are now enforcing canonical signatures (s-value less than or equal to half the curve order). This will likely break some expectations for dependents that were using the previous versions.
@uport-automation-bot
Copy link
Collaborator

🎉 This PR is included in version 7.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@ukstv
Copy link
Contributor Author

ukstv commented Apr 19, 2023

Yahoo!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants