[Cryptography] Exotic Operations in Primitive Construction

Ard Biesheuvel ard.biesheuvel at gmail.com
Sat Oct 10 04:41:16 EDT 2020


Op za 10 okt. 2020 om 07:05 schreef Dan McDonald <danmcd at kebe.com>:
>
> I tried it, and I think you've some bugs.  Starting with an extra close-paren on each expression.  Losing those made it compile.
>
> Now granted, your choice of parentheses generally was good, but your constant expressions were to me unreadable.
>
> I've attached a program which contains a test harness, the original function (with one close paren moved from each operation), and an IMHO more readable version of the original.  It compiles well on MacOS, and on illumos (OmniOSce), and probably will work with Linux or *BSD just fine as well.  Correcting the buggy code is an exercise left to the reader, however.
>
> Sample output showing it's incorrect:
>
> nowhere(/tmp)[0]% ./r32-32 g
> Original(0x2112) = 0x40800000
> Readable(0x2112) = 0x40800000
> nowhere(/tmp)[0]%
>
> I believe the reverse32 of 0x2112 is 0x48840000.
>

Your masks are incorrect.

const uint32_t mask[] = {
  0x55555555,
  0x33333333,
  0x0f0f0f0f,
  0x00ff00ff,
  0x0000ffff,
};

for (i = 0, j = 1; i < 5; i++, j <<= 1)
    bits = ((bits & mask[i]) << j) |
           ((bits & ~mask[i]) >> j);


More information about the cryptography mailing list