[GH-ISSUE #1749] Introduce AVX-512's ternary logic instruction #1157

Closed
opened 2026-10-14 03:37:10 +07:00 by bsakaguchi · 16 comments
Owner

Originally created by @lei-april on GitHub (Sep 11, 2015).
Original GitHub issue: https://github.com/openwall/john/issues/1749

Originally assigned to: @lei-april on GitHub.

Theoretically, all 3-argument operations like SHA1_I() can be implemented with just one ternary logic instruction.

However, one thing that bothers me a bit is that simd-intrinsics.c might eventually be bloated with things like #if ... #else ... #endif, which I (we) wish to appear only in pseudo-intrinsics.h.

Originally created by @lei-april on GitHub (Sep 11, 2015). Original GitHub issue: https://github.com/openwall/john/issues/1749 Originally assigned to: @lei-april on GitHub. Theoretically, all 3-argument operations like `SHA1_I()` can be implemented with just one ternary logic instruction. However, one thing that bothers me a bit is that simd-intrinsics.c might eventually be bloated with things like `#if ... #else ... #endif`, which I (we) wish to appear only in pseudo-intrinsics.h.
bsakaguchi 2026-10-14 03:37:10 +07:00
Author
Owner

@magnumripper commented on GitHub (Sep 11, 2015):

I see what you mean but I think it's the right thing for now. Compare with HAVE_ANDNOT or whatever it was we just introduced. We need to do it that way unless we want to move things like MD4_F into pseudo-intrinsics.h (and that would not make sense).

If you come up with a better idea I'm all ears though!

<!-- gh-comment-id:139655164 --> @magnumripper commented on GitHub (Sep 11, 2015): I see what you mean but I think it's the right thing for now. Compare with HAVE_ANDNOT or whatever it was we just introduced. We need to do it that way unless we want to move things like MD4_F into pseudo-intrinsics.h (and that would not make sense). If you come up with a better idea I'm all ears though!
Author
Owner

@magnumripper commented on GitHub (Sep 12, 2015):

So what is this, bitselect() on acid? Oh, it's like LOP3.LUT... So can we implement any MD4/5/SHA function using a single instruction?

Note to self: http://wm.ite.pl/articles/avx512-ternary-functions.html

<!-- gh-comment-id:139692770 --> @magnumripper commented on GitHub (Sep 12, 2015): So what is this, bitselect() on acid? Oh, it's like LOP3.LUT... So can we implement _any_ MD4/5/SHA function using a single instruction? Note to self: http://wm.ite.pl/articles/avx512-ternary-functions.html
Author
Owner

@magnumripper commented on GitHub (Sep 12, 2015):

I can't find it in https://software.intel.com/sites/landingpage/IntrinsicsGuide

<!-- gh-comment-id:139693302 --> @magnumripper commented on GitHub (Sep 12, 2015): I can't find it in https://software.intel.com/sites/landingpage/IntrinsicsGuide
Author
Owner

@magnumripper commented on GitHub (Sep 12, 2015):

https://software.intel.com/en-us/node/523807

<!-- gh-comment-id:139693473 --> @magnumripper commented on GitHub (Sep 12, 2015): https://software.intel.com/en-us/node/523807
Author
Owner

@lei-april commented on GitHub (Sep 12, 2015):

So can we implement any MD4/5/SHA function using a single instruction?

Not necessarily. But at least they can be compacted to use much fewer instructions.

<!-- gh-comment-id:139697797 --> @lei-april commented on GitHub (Sep 12, 2015): > So can we implement any MD4/5/SHA function using a single instruction? Not necessarily. But at least they can be compacted to use much fewer instructions.
Author
Owner

@magnumripper commented on GitHub (Sep 12, 2015):

But if the function is F(x, y, z) it should always be implementable with a single instruction, no? You'd just need a trivial tool to find out the truth table.

<!-- gh-comment-id:139698949 --> @magnumripper commented on GitHub (Sep 12, 2015): But if the function is `F(x, y, z)` it should always be implementable with a single instruction, no? You'd just need a trivial tool to find out the truth table.
Author
Owner

@magnumripper commented on GitHub (Sep 12, 2015):

OK, not if there are bit shifts within the function of course. But as long as there's only and, or, not and so on, in any combination.

<!-- gh-comment-id:139699092 --> @magnumripper commented on GitHub (Sep 12, 2015): OK, not if there are bit shifts within the function of course. But as long as there's only and, or, not and so on, in any combination.
Author
Owner

@lei-april commented on GitHub (Sep 12, 2015):

I have a question regarding to the code: what's the difference between MD5_H() and MD5_H2()? It seems to me they are doing the same thing, i.e. x^y^z.

Then why are the operands written in different orders?

#define MD5_H(x,y,z)                            \
    tmp[i] = vxor((x[i]),(y[i]));               \
    tmp[i] = vxor((tmp[i]),(z[i]));

#define MD5_H2(x,y,z)                           \
    tmp[i] = vxor((y[i]),(z[i]));               \
    tmp[i] = vxor((tmp[i]),(x[i]));
<!-- gh-comment-id:139740609 --> @lei-april commented on GitHub (Sep 12, 2015): I have a question regarding to the code: what's the difference between `MD5_H()` and `MD5_H2()`? It seems to me they are doing the same thing, i.e. `x^y^z`. Then why are the operands written in different orders? ``` c #define MD5_H(x,y,z) \ tmp[i] = vxor((x[i]),(y[i])); \ tmp[i] = vxor((tmp[i]),(z[i])); #define MD5_H2(x,y,z) \ tmp[i] = vxor((y[i]),(z[i])); \ tmp[i] = vxor((tmp[i]),(x[i])); ```
Author
Owner

@magnumripper commented on GitHub (Sep 12, 2015):

They are originally like this

#define H(x, y, z)          (x ^ y ^ z)
(...)
    H(a, b, c, d, k)
    H(d, a, b, c, k)
    (...)

If you expand that, you'll see that we do a ^ b twice (order doesn't matter for x ^ y ^ z). So with this optimization we store the result from the first and re-use it. Obviously we need to have a spare register at hand for it to be any net gain.

<!-- gh-comment-id:139744420 --> @magnumripper commented on GitHub (Sep 12, 2015): They are originally like this ``` #define H(x, y, z) (x ^ y ^ z) (...) H(a, b, c, d, k) H(d, a, b, c, k) (...) ``` If you expand that, you'll see that we do `a ^ b` twice (order doesn't matter for `x ^ y ^ z`). So with this optimization we store the result from the first and re-use it. Obviously we need to have a spare register at hand for it to be any net gain.
Author
Owner

@magnumripper commented on GitHub (Sep 12, 2015):

BTW for plain scalar C, we just do it like this. It merely hints to the optimizer that it should do the same thing (and a good optimizer might see it anyway):

#define H(x, y, z)          ((x ^ y) ^ z)
#define H2(x, y, z)         (x ^ (y ^ z))
(...)
    H(a, b, c, d, k)
    H2(d, a, b, c, k)
    (...)
<!-- gh-comment-id:139744676 --> @magnumripper commented on GitHub (Sep 12, 2015): BTW for plain scalar C, we just do it like this. It merely hints to the optimizer that it should do the same thing (and a good optimizer might see it anyway): ``` #define H(x, y, z) ((x ^ y) ^ z) #define H2(x, y, z) (x ^ (y ^ z)) (...) H(a, b, c, d, k) H2(d, a, b, c, k) (...) ```
Author
Owner

@lei-april commented on GitHub (Sep 13, 2015):

I'm using this command: ./configure CC="gcc-5 -mavx512f" --host=x86_64-linux, but it still got configured as AVX2. OTOH, using ./configure CC="icc -xCOMMON-AVX512" --host=x86_64-linux, it's configured correctly as AVX512F.

Any hint about this?

<!-- gh-comment-id:139843350 --> @lei-april commented on GitHub (Sep 13, 2015): I'm using this command: `./configure CC="gcc-5 -mavx512f" --host=x86_64-linux`, but it still got configured as AVX2. OTOH, using `./configure CC="icc -xCOMMON-AVX512" --host=x86_64-linux`, it's configured correctly as AVX512F. Any hint about this?
Author
Owner

@magnumripper commented on GitHub (Sep 13, 2015):

When I tried a while ago, gcc could produce the assembler but as(1) didn't understand the new instructions. Maybe this is the case here for you?

<!-- gh-comment-id:139857831 --> @magnumripper commented on GitHub (Sep 13, 2015): When I tried a while ago, gcc could produce the assembler but as(1) didn't understand the new instructions. Maybe this is the case here for you?
Author
Owner

@lei-april commented on GitHub (Sep 14, 2015):

Oh, I figured it out. When compiling some non-AVX512 test code in ./configure, gcc generated code that can execute natively. Although I used --host=x86_64-linux, it's still not configured as cross-compiling.

OTOH, when compiling the same test code, icc emitted some AVX512 instructions in the generated code, which cannot run natively. So ./configure correctly recognized it as cross-compiling.

Is there a way that I can force ./configure to cross-compile?

<!-- gh-comment-id:139948460 --> @lei-april commented on GitHub (Sep 14, 2015): Oh, I figured it out. When compiling some non-AVX512 test code in ./configure, gcc generated code that can execute natively. Although I used `--host=x86_64-linux`, it's still not configured as cross-compiling. OTOH, when compiling the same test code, icc emitted some AVX512 instructions in the generated code, which cannot run natively. So ./configure correctly recognized it as cross-compiling. Is there a way that I can force ./configure to cross-compile?
Author
Owner

@magnumripper commented on GitHub (Sep 14, 2015):

--disable-native-tests should fix that

<!-- gh-comment-id:139988182 --> @magnumripper commented on GitHub (Sep 14, 2015): --disable-native-tests should fix that
Author
Owner

@lei-april commented on GitHub (Sep 14, 2015):

--disable-native-tests should fix that

Oh, thanks! That's exactly what I'm looking for.

<!-- gh-comment-id:139995280 --> @lei-april commented on GitHub (Sep 14, 2015): > --disable-native-tests should fix that Oh, thanks! That's exactly what I'm looking for.
Author
Owner

@magnumripper commented on GitHub (Sep 14, 2015):

Done in #1762. Good stuff!

<!-- gh-comment-id:140143834 --> @magnumripper commented on GitHub (Sep 14, 2015): Done in #1762. Good stuff!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openwall/john#1157
No description provided.