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

[web3.js] Replace tweetnacl impl #27435

Merged
merged 9 commits into from
Aug 28, 2022
Merged

[web3.js] Replace tweetnacl impl #27435

merged 9 commits into from
Aug 28, 2022

Conversation

steveluscher
Copy link
Contributor

@steveluscher steveluscher commented Aug 26, 2022

Problem

This PR aims to replace the ed25519 keygen, signing, and verifying algorithms with smaller ones having fewer dependencies.

Summary of Changes

  • Replaced tweetnacl with @noble/ed25519

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));
Before After
https://gist.github.com/steveluscher/3cc75a21d94be656071a5983acd4fae9 https://gist.github.com/steveluscher/a8722c7ad7726d897b625e971492a086

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

Dependency tree

Replaced tweetnacl for @noble/ed25519, dropping about 4K gzipped.

Before After
image image

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

Performance

Consider this keypair generation code:

// perf.js
const {Keypair} = require('@solana/web3.js/lib/index.cjs');

for (let ii = 0; ii < 10000; ii++) {
  Keypair.generate();
}

Take 10 trials:

 % time node perf.js

Results

image

No change.


Consider this transaction signing code:

const {
  Keypair,
  PublicKey,
  Transaction,
  TransactionInstruction,
} = require('@solana/web3.js/lib/index.cjs');

const keypair = Keypair.generate();
const tx = new Transaction({
  feePayer: keypair.publicKey,
  recentBlockhash: '3CYc7JXH2iEgiyiSFakXMuBJ43ss8ciJzErUfD7jBCuk',
}).add(
  new TransactionInstruction({
    data: Buffer.from('hello world'),
    keys: [],
    programId: new PublicKey('MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr'),
  }),
);

for (let ii = 0; ii < 1000; ii++) {
  tx.sign(keypair);
}

Take 10 trials:

 % time node perf.js

Results:

image

~6% slower.

Compatibility notes

Together, this, #27390, and #27127 make @solana/web3.js incompatible with React Native because of their use of:

  • BigInt
  • Exponentiation with BigInt
  • bigint literals

Because this team is small, we're likely to recommend that folks upgrade their React Native apps to 0.70 and convert them to run using the Hermes JavaScript engine, which in that version supports all of the above. Support for big integers is critical to most crypto apps, and React Native has now made Hermes the default engine. We would be best advised to skate where the puck is going.

Addresses solana-labs/solana-web3.js#1103
Fixes #26933.

@codecov
Copy link

codecov bot commented Aug 27, 2022

Codecov Report

Merging #27435 (81a016d) into master (e779032) will decrease coverage by 0.2%.
The diff coverage is n/a.

@@            Coverage Diff            @@
##           master   solana-labs/solana#27435     +/-   ##
=========================================
- Coverage    76.9%    76.6%   -0.3%     
=========================================
  Files          48       52      +4     
  Lines        2505     2648    +143     
  Branches      355      363      +8     
=========================================
+ Hits         1927     2030    +103     
- Misses        448      484     +36     
- Partials      130      134      +4     

@steveluscher steveluscher merged commit 60f3dc5 into solana-labs:master Aug 28, 2022
@steveluscher steveluscher deleted the ed25519-replacement branch August 28, 2022 19:11
@paulmillr
Copy link

@steveluscher it seems like the performance bottleneck is somewhere outside of ed25519, since tweetnacl should be 4x slower.

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.

web3.js: Investigate replacement for tweetnacl
2 participants