Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

[web3.js] Replace tweetnacl impl#27435

Merged
steveluscher merged 9 commits intosolana-labs:masterfrom
steveluscher:ed25519-replacement
Aug 28, 2022
Merged

[web3.js] Replace tweetnacl impl#27435
steveluscher merged 9 commits intosolana-labs:masterfrom
steveluscher:ed25519-replacement

Conversation

@steveluscher
Copy link
Copy Markdown
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-foundation/solana-web3.js#1103
Fixes #26933.

@codecov
Copy link
Copy Markdown

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
Copy Markdown

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

web3.js: Investigate replacement for tweetnacl

2 participants