core-rs icon indicating copy to clipboard operation
core-rs copied to clipboard

Argon2 native CPU module selection

Open riptl opened this issue 7 years ago • 2 comments

Like in https://github.com/nimiq-network/core/pull/440 of core-js, I think it's useful to have a native module selection for packaged builds, and I'd like to take on this.

My idea is to statically link the different versions of libargon2 into the same binary, giving the function names a suffix of the target via a C macro. We'd then have argon2d_hash_raw_avx2, argon2d_hash_raw_sse2 and so on built from the same source.

Like in https://github.com/nimiq-network/core/pull/456, a $PACKAGING environment variable should be introduced, that will trigger a native build if it's false instead of the avx2, sse2… versions.

On startup, Rust then would set up function pointers to the native module and use rust-cupid to initialize them with the correct implementations.

I also filed https://github.com/nimiq/core-rs/pull/1 to reduce the number of functions so the module is easier to work with.

The alternative would be to dynamically load the implementations like the .node libraries in Node.js but that'd be more difficult and inconvenient, in my opinion.

riptl avatar Dec 14 '18 10:12 riptl

If we use function multiversioning instead, no changes are required in Rust.

  • https://gcc.gnu.org/wiki/FunctionMultiVersioning
  • https://llvm.org/devmtg/2014-10/Slides/Christopher-Function%20Multiversioning%20Talk.pdf

riptl avatar Dec 15 '18 05:12 riptl

Function multi versioning looks like a nice way to tackle that.

paberr avatar Jan 15 '19 12:01 paberr