I'm coding AES Invert mixcolumns operation and I need to multiply numbers by 14 in GF(256). This is what I have come up with (p is the result and q the number to multiply by 14):
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
int main()
{
uint8_t p=1, q=0x02;
p = (q<<1) ^ (q<<2) ^ (q<<3);//*(x³+x²+x)
//modulo x⁸+x⁴+x³+x+1
if((q>>5)<5&&(q>>5)!=0&&(q>>5)!=3)
p^=0x1B;
if((q>>5)>1&&(q>>5)<6)
p^=0x36;
if((q>>5)>3)
p^=0x6C;
printf("\n\n\t%2.2X\n\n",p);
return 0;
}
It works but I think there is a simpler way to do this but I cannot seem to find it. My main issue here is that I make 6 comparisons.
My main issue here is that I make 6 comparisons.
Use look-up table to avoid all compares. q>>5
is only 3 bits.
static const uint8_t Table1B[8] = { 0, 0x1B, 0x1B, 0, 0x1B, 0, 0, 0 };
static const uint8_t Table36[8] = { 0, 0, 0x36, 0x36, 0x36, 0x36, 0, 0 };
static const uint8_t Table6C[8] = { 0, 0, 0, 0, 0x6C, 0x6C, 0x6C, 0x6C };
q >>= 5;
p ^= Table1B[q];
p ^= Table36[q];
p ^= Table6C[q];
Likely could simplify to 1 table: Table[0] = Table1B[0] ^ Table36[0] ^ Table6C[0]
, etc.
p ^= Table[q >> 5];