marvinroger / nanocurrency-js

🔗 A toolkit for the Nano cryptocurrency, allowing you to derive keys, generate seeds, hashes, signatures, proofs of work and blocks.
https://marvinroger.github.io/nanocurrency-js
141 stars 31 forks source link

Slow performance with derivePublicKey(secretKey) #75

Open Joohansson opened 4 years ago

Joohansson commented 4 years ago

I'm building a reactJS app for finding vanity addresses using this library and web workers to get multithreading. It all works great and the following code does the address creation and matching. However I have problem with performance. Without the derivePublicKey below I can do 150,000 loops per second. With the derivePublicKey the performance drops to 600/s. Another strange thing is that there is no difference using 2 threads or 12 when doing that. Just using "currentAddressesCount += 1" boost the loop count to 120 million per second so nothing wrong with the multithreading implementation. Perhaps there is nothing wrong with your library but maybe you know what's wrong?

function search() {
  currentAddressesCount += 1
  const array = new Uint8Array(32)
  // eslint-disable-next-line no-restricted-globals
  self.crypto.getRandomValues(array)
  const seed = array.reduce((hex, idx) => hex + (`0${idx.toString(16)}`).slice(-2), '')
  const secretKey = nano.deriveSecretKey(seed, 0)
  const publicKey = nano.derivePublicKey(secretKey)
  const address = nano.deriveAddress(publicKey, {useNanoPrefix: true})

  if (isMatch(address)) {
    postMessage(address)
  }
}
Joohansson commented 4 years ago

Testing this tool https://github.com/vitorcremonez/nano-vanity I have now verified that running nanocurrency@1.7.4 with await nanocurrency.init() indeed result in 10,000 keys/s per thread compared to 300/s per thread with nanocurrency@2.3.0 which has no init() function. Easy to test by doing npm install and jumping between the two versions. So something has changed with this library. I hope it can be solved somehow.

Also tested with my own multithreading script and getting 70-120k per sec using 12 threads, with 1.7.4.

Joohansson commented 4 years ago

Ok, looks like the issue has been tracked down. https://github.com/marvinroger/nanocurrency-js/issues/24

You never did the high performance deriveAddressFromSeed function, right? I'm currently in need of high performance key derivation when multi-creating wallets, mass extracting keys/addresses from one seed, searching for a particular address in a seed and vanity address searches. Currently solved by using both the 1.7.4 and 2.3.0 in parallel.

bbaraban commented 3 years ago

I would also like the function! @marvinroger

marvinroger commented 3 years ago

I'm currently working on it, I'll add new optimized WASM functions