jedisct1 / libaegis

Portable C implementations of the AEGIS family of high-performance authenticated encryption algorithms.
https://datatracker.ietf.org/doc/draft-irtf-cfrg-aegis-aead/
MIT License
60 stars 14 forks source link

Fix CPU architecture guards for x86 and x64 on MSVC #8

Closed mfrischknecht closed 6 months ago

mfrischknecht commented 6 months ago

I noticed that rust-aegis runs very slow when I build it with the MSVC toolchain on Windows. From what my online searches turned up, it looks like MSVC uses _M_IX86 and _M_AMD64 for CPU architecture guards instead of __i386__ and __x86_64__ ^1.

This causes libaegis never to select any hardware accelerated implementations when built this way. I have created a simple test program that demonstrates the effect:

$ git clone --recursive https://github.com/mfrischknecht/rust-aegis-msvc-aesni-test.git
$ cd ./rust-aegis-msvc-aesni-test
$ git checkout without-fix
$ cargo run --release --quiet
CPU Features:

get_cpu_features(): 0
has_neon():         0
has_armcrypto():    0
has_avx():          0
has_avx2():         0
has_avx512f():      0
has_aesni():        0
has_vaes():         0

Encrypted: 100 MiB
Elapsed: 5.4909146s
Throughput: 18.211902257594755 MiB/s

$ git checkout with-fix
$ cargo run --release --quiet
CPU Features:

get_cpu_features(): 0
has_neon():         0
has_armcrypto():    0
has_avx():          1
has_avx2():         1
has_avx512f():      0
has_aesni():        1
has_vaes():         0

Encrypted: 100 MiB
Elapsed: 0.0278724s
Throughput: 3.5036900302808514 GiB/s

https://sourceforge.net/p/predef/wiki/Architectures/ http://web.archive.org/web/20240211072249/https://sourceforge.net/p/predef/wiki/Architectures/ https://archive.is/jRq9r

jedisct1 commented 6 months ago

Thank you!

The MSVC-specific macros were recognized for aarch64, but I didn't know that they were also needed for i386 and x86_64.

Thanks again!

jedisct1 commented 6 months ago

A new version of libaegis was tagged, and I'm going to update the Rust crate as well.

jedisct1 commented 6 months ago

Done!

mfrischknecht commented 6 months ago

Great, thank you very much!