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

fix: replace js-sha3 with @noble/hashes/sha3 #27630

Merged
merged 1 commit into from
Sep 7, 2022
Merged

fix: replace js-sha3 with @noble/hashes/sha3 #27630

merged 1 commit into from
Sep 7, 2022

Conversation

steveluscher
Copy link
Contributor

@steveluscher steveluscher commented Sep 6, 2022

Problem

This PR aims to replace the keccak_256 hashing algorithm with a smaller one.

Summary of Changes

  • Replace js-sha3 with @noble/hashes/sha3

Bundle Size

Used package-build-stats to analyze the size change locally.

const {getPackageStats} = require('package-build-stats');
getPackageStats('~/web3.js').then(s => console.log(s));

tl;dr a ~3% reduction in bundle size after gzip.

Dependency tree

Replaced js-sha3 for @noble/hashes/sha3.

Before After
image image

Produced using https://npmgraph.js.org/

Performance

Consider this keypair generation code:

// perf.js
const {Secp256k1Program, Keypair} = require('./lib/index.cjs');

const key = Keypair.generate().secretKey;

for (let ii = 0; ii < 1000000; ii++) {
  Secp256k1Program.publicKeyToEthAddress(key);
}

Take 10 trials:

 % time node perf.js

Results

image

~180% slower than the existing library.

Compatibility notes

Both of these libraries have no dependencies, thought @noble/hashes introduces a dependency on BigInt. We wrote more about this here: https://twitter.com/steveluscher/status/1565232091260157952

Fixes #27629

@steveluscher steveluscher added the javascript Pull requests that update Javascript code label Sep 7, 2022
@solana-labs solana-labs deleted a comment from codecov bot Sep 7, 2022
@codecov
Copy link

codecov bot commented Sep 7, 2022

Codecov Report

Merging #27630 (001ce2c) into master (e779032) will increase coverage by 0.1%.
The diff coverage is n/a.

@@            Coverage Diff            @@
##           master   #27630     +/-   ##
=========================================
+ Coverage    76.9%    77.0%   +0.1%     
=========================================
  Files          48       55      +7     
  Lines        2505     2877    +372     
  Branches      355      401     +46     
=========================================
+ Hits         1927     2217    +290     
- Misses        448      519     +71     
- Partials      130      141     +11     

@steveluscher steveluscher merged commit 6bc04b5 into solana-labs:master Sep 7, 2022
@steveluscher steveluscher deleted the keccak-noble-hashes branch September 7, 2022 05:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
javascript Pull requests that update Javascript code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[web3.js] Replace js-sha3 dependency for keccak-ing
1 participant