paulmillr / noble-hashes

Audited & minimal JS implementation of hash functions, MACs and KDFs.
https://paulmillr.com/noble
MIT License
573 stars 46 forks source link

Reduce Bundle Size #30

Closed dawsbot closed 2 years ago

dawsbot commented 2 years ago

Hey Paul, love what you're doing here. I was hoping to switch from sha3 to this in-order to save on bundle size.

Unfortunately the keccak_256 import from noble appears to be 2x the size of the sha3 import from the sha3 library. Before I go into the code to investigate, do you know why this might be?

import { keccak_256 } from '@noble/hashes/sha3';

versus

import { Keccak } from 'sha3';
paulmillr commented 2 years ago

Hi. I'm not really sure, everything is as minimal as possible.

paulmillr commented 2 years ago

@dawsbot can you provide both files (bundled noble/sha3 and bundled sha3) as a gist?

dawsbot commented 2 years ago

@paulmillr good question, I'm not sure which bundle system to use to test this in isolation. I tested this over in essential-eth by replacing the use of sha3 then rerun the whole build process.

What bundling system did you use to get "SHA256-only is 240 lines / 3KB gzipped ?" noted in the readme?

paulmillr commented 2 years ago

Rollup - see this file https://github.com/paulmillr/noble-hashes/blob/main/rollup.js, you can comment-out unused code

dawsbot commented 2 years ago

I need more help to do this.

I tested exporting only sha3 via "export * from 'sha3'" using your rollup config and found it was about 700 bytes. When looking in, I noticed that it had requires to files which were not in the rollup. I know I don't understand rollup well and have never used it for a project so I'll have to stop here and ask advice.

Any tips?

The sha3 repo has the code public, it's very small: https://github.com/phusion/node-sha3/tree/main/src

paulmillr commented 2 years ago

you should do npn run build && npm run build:release. Also do not use the star import, use whatever was specified in this rollup config file. Paste the result file into a gist.

dawsbot commented 2 years ago

Hey Paul, I don't want to spend the time to take care of this. Feel free to close this if you don't want to take it on.

paulmillr commented 2 years ago

You haven't provided any build artifacts, so I assume something wasn't configured in a proper way.