jedisct1 / libsodium.js

libsodium compiled to Webassembly and pure JavaScript, with convenient wrappers.
Other
968 stars 138 forks source link

Question: State of SIMD? (Regarding Argon2) #340

Closed mwoelke closed 2 months ago

mwoelke commented 2 months ago

Hi there, neat library.

I was testing the performance of different Argon2 implementations for the web. Especially, I was testing the performance of libsodium.js against argon2-browser which utilizes the Argon2 reference implementation in WASM.

From my numbers, libsodium.js (pre-built browsers-sumo) is faster than argon2-browser when later is used without SIMD. With SIMD enabled, argon2-browser is about 35% faster.

The possibility of utilizing SIMD was already discussed in #133. According to caniuse SIMD is enabled in all major browsers since March 2023. I was therefore wondering about the current state of SIMD and libsodium.js.

Thanks in advance!

My numbers for reference (t=3):

memory (KiB) argon2-browser w/o simd (ms) argon2-browser with simd (ms) libsodium.js (ms)
1024 9 5 12
2048 17 9 12
4096 32 14 22
8192 66 31 44
16384 133 61 93
32768 272 138 191
65536 537 255 384
131072 1086 507 794
262144 2248 1053 1642
524288 4627 2197 3311
1048576 9942 4526 7065
jedisct1 commented 2 months ago

It's still too early to enable SIMD for WebAssembly. It's not even part of WebAssembly yet; it's an extension whose specification can still change, and often requires to be explicitly enabled.

Just like many other WebAssembly extensions we could take advantage of. And unfortunately, WebAssembly doesn't support run-time feature detection.

Just to enable SIMD, it means that we would have to ship at least 2 versions of the entire libsodium webassembly code. Unfortunately, a lot of people would find that unacceptable.

mwoelke commented 2 months ago

Thank you for your quick response!

I was under the impression that run-time feature detection is supported because of this PR https://github.com/jedisct1/libsodium.js/issues/340 for argon2-browser. Turns out this is however using some external dependency.

Anyway, I will close this issue. Thanks again for your time :)