paulmillr / noble-curves

Audited & minimal JS implementation of elliptic curve cryptography.
https://paulmillr.com/noble
MIT License
664 stars 62 forks source link

Improve `hexToBytes` performance #83

Closed arobsn closed 1 year ago

arobsn commented 1 year ago

What this does

Rewrites hexToBytes function using char code approach to gain ~6x in performance.

Benchmark

Benchmark results on a MacBook Air M1 and node v20:

 ✓ packages/crypto/src/coders/hex.bench.ts (6) 3583ms
   ✓ Decode hex to bytes (3) 3055ms
     name                                      hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · @noble/hashes implementation       25,861.49  0.0364  0.4523  0.0387  0.0381  0.0433  0.0540  0.3573  ±0.67%    12931  
   · @scure/base implementation          4,391.03  0.2167  0.6890  0.2277  0.2287  0.2810  0.5655  0.6118  ±0.66%     2196   slowest
   · @fleet-sdk/crypto implementation  173,810.56  0.0054  0.4406  0.0058  0.0057  0.0076  0.0093  0.0144  ±0.31%    86906   fastest

  @fleet-sdk/crypto implementation - packages/crypto/src/coders/hex.bench.ts > Decode hex to bytes
    6.72x faster than @noble/hashes implementation
    39.58x faster than @scure/base implementation

Benchmark code: https://github.com/fleet-sdk/fleet/blob/master/packages/crypto/src/coders/hex.bench.ts

paulmillr commented 1 year ago

This is awesome. Thanks @arobsn

paulmillr commented 1 year ago

Also landed in noble-hashes at https://github.com/paulmillr/noble-hashes/commit/84f9cf645a35a2a58e900ba75759e01d16533734

paulmillr commented 1 year ago

Benchmark added in https://github.com/paulmillr/noble-curves/blob/main/benchmark/utils.js

arobsn commented 1 year ago

Awesome, thanks for merging! I'm a big fan of your work :)