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

Replace heavy crypto packages for lighter noble implementations via upgrading ethereumjs-util to latest (now called @ethereumjs/util) #260

Merged
merged 21 commits into from
Aug 17, 2022

Conversation

adonesky1
Copy link
Contributor

@adonesky1 adonesky1 commented Jul 27, 2022

Removes secp256k1 and keccak via as dependencies by upgrading from [email protected] to its latest (beta) version @ethereumjs/[email protected] which uses noble implementations for crypto. Replaces keccak hashing with keccak256 from ethereum-cryptography/keccak

To achieve this I've copied functions from ethereumjs-abi (rawEncode and solidityPack) that we use to pack the values before hashing them with the newly added keccak256 implementation from ethereum-cryptography/keccak, and removed the ethereumjs-abi dependency. I've extracted these functions and more or less preserved their implementations (solidityPack, rawEncode) minus the references to ethereumjs/[email protected] methods, which, if left intact would keep secp256k1 and keccak in our dependency tree.

Next steps:

@adonesky1 adonesky1 force-pushed the upgrade-ethereumjs-util branch 4 times, most recently from 1709cf6 to 546c992 Compare August 4, 2022 20:31
src/sign-typed-data.ts Outdated Show resolved Hide resolved
src/sign-typed-data.ts Outdated Show resolved Hide resolved
kumavis
kumavis previously requested changes Aug 4, 2022
Copy link
Member

@kumavis kumavis left a comment

Choose a reason for hiding this comment

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

as possible try to remove toBuffer calls where we know the input type

@adonesky1
Copy link
Contributor Author

adonesky1 commented Aug 4, 2022

@kumavis this currently appears to be increasing the size of the package... 🤦

@adonesky1
Copy link
Contributor Author

adonesky1 commented Aug 5, 2022

@kumavis this currently appears to be increasing the size of the package... 🤦

This is just true of the dist/ folder, not the overall impact on the consumer and its dependency tree, so yeah its gonna be bigger with all those added utility functions

I'm trying to evaluate the impact upstream using yarn why looking at Disk size with transitive dependencies and Disk size with unique dependencies but not sure how valuable these are of indicators.

@wbt
Copy link
Contributor

wbt commented Aug 5, 2022

Updating ethereumjs-util is also useful for the reasons noted here around getting consistent types from bn.js after a recent breaking-change update adopted elsewhere.

@paulmillr
Copy link

Why are you keeping bn.js if all the internal methods of new @ethereumjs libraries switched to native bigints? For keccak256, you can use ethereum-cryptography/keccak256 since you're already using it.

@adonesky1
Copy link
Contributor Author

adonesky1 commented Aug 10, 2022

Why are you keeping bn.js if all the internal methods of new @ethereumjs libraries switched to native bigints

I'm using bn.js here to replicate the functions from ethereumjs-abi (rawEncode and solidityPack) that we use to pack the values before hashing them with the keccak256 implementation from ethereum-cryptography/keccak. I've extracted these functions and more or less preserved their implementations (solidityPack, rawEncode) - including use of bn.js - minus the references to old ethereumjs/[email protected] methods, which would keep secp256k1 and keccak in our dependency tree. cc @kumavis

@paulmillr
Copy link

@adonesky1 understood, so it's basically copy-pasting code from other lib. I've noticed about 5 lines that use BN. Think it would be really easy to replace it with native BigInt. In the current case you're adding 3547 lines of unnecessary code.

@adonesky1 adonesky1 force-pushed the upgrade-ethereumjs-util branch from cb9490a to 1ed5ceb Compare August 11, 2022 20:25
@adonesky1
Copy link
Contributor Author

I've noticed about 5 lines that use BN. Think it would be really easy to replace it with native BigInt. In the current case you're adding 3547 lines of unnecessary code.

Yeah looking into this now. Will try to make it work in a subsequent PR.

@adonesky1 adonesky1 marked this pull request as ready for review August 11, 2022 22:26
@adonesky1 adonesky1 requested a review from a team as a code owner August 11, 2022 22:26
@adonesky1 adonesky1 requested review from kumavis and Gudahtt August 11, 2022 22:35
@adonesky1 adonesky1 changed the title upgrade ethereumjs-util to latest (@ethereumjs/util) and adapt accordingly Replace heavy crypto packages for lighter noble implementations via upgrading ethereumjs-util to latest (now called @ethereumjs/util) Aug 11, 2022
@adonesky1 adonesky1 changed the title Replace heavy crypto packages for lighter noble implementations via upgrading ethereumjs-util to latest (now called @ethereumjs/util) Replace heavy crypto packages for lighter noble implementations via upgrading ethereumjs-util to latest (now called @ethereumjs/util) Aug 11, 2022
@adonesky1 adonesky1 dismissed kumavis’s stale review August 15, 2022 13:58

addressed feedback

@adonesky1 adonesky1 force-pushed the upgrade-ethereumjs-util branch from f767905 to 0c7cac4 Compare August 15, 2022 20:39
@adonesky1 adonesky1 force-pushed the upgrade-ethereumjs-util branch from 5094d73 to b85ac7e Compare August 16, 2022 19:30
@adonesky1 adonesky1 merged commit 9f01c9d into main Aug 17, 2022
@adonesky1 adonesky1 deleted the upgrade-ethereumjs-util branch August 17, 2022 17:26
@paulmillr paulmillr mentioned this pull request Sep 13, 2022
@adonesky1 adonesky1 mentioned this pull request Sep 21, 2022
@legobeat legobeat mentioned this pull request Sep 9, 2023
1 task
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.

4 participants