0

I am simulating a mini AES encryption/decryption algorithm using C++.

For this I need to multiply two 4-bit numbers while treating them as polynomials.

It goes through some stages that are converting them to polynomials, multiplying the two polynomials, then doing the polynomial reduction to lower power if needed using a predefined irreducible polynomial, and it finally converts them back to 4-bit format.

For instance, multiplying 1011 ⊗ 0111 is analogous to x3+x+1 ⊗ x2+x+1 The answer is, if x5+x4+1 has a power of 5 then you need to reduce it by dividing on the predefined polynomial x4+x+1. The answer will be x2 that is 0100.

Many thanks in advance!

Eduard
  • 3,395
  • 8
  • 37
  • 62
Pts
  • 1
  • 1

2 Answers2

1

You could for example do this

unsigned int multiply_poly(unsigned int a, unsigned int b)
{
    unsigned int ret = 0;
    while(a)
    {
        if(a & 1)
        {
            ret ^= b;
        }
        a >>= 1;
        b <<= 1;
    }
    return ret;
}

Explanation: You do basically written multiplication. You shift the a to the right and always look at the last bit. If it is 0, you add nothing if it is 1 you xor with b. Since xor is not exactly addition on integers, this is not just a*b. You can think about why the addition of two polynomials can be done by xor. Since we shift b to the left, it always is multiplied with the current monom from a.

Evg
  • 25,259
  • 5
  • 41
  • 83
n314159
  • 4,990
  • 1
  • 5
  • 20
  • This answer is missing the reduction that is use x^4 = x+1. At each step look at bit 5. If set 1 then clear it and x-or the ret with 0x03 – kelalaka Nov 30 '19 at 18:32
  • 1
    I have code for that, but as @efekctive pointed out, this seems to be homework and I won't do the whole homework for OP. – n314159 Nov 30 '19 at 18:39
0

Two 4-bit numbers? Generally bit manipulations functions (e.g., first bit, last bit, bit count) are slow and inefficient unless CPU has special functions that implement them (e.g., SSE4 has routine for bitcount). Common efficient solution is to create a helper array that solves the problem for bytes (256 long array with solution for each byte).

In your case I'd store the two 4-bit polynomials inside a single unsigned char and use std::array<unsigned char,256> with pre-computed answers. So the whole computation is replaced with a single load operation (hopefully from L3 cache or whatever most efficient memory your platform has).

You only need to compute the answers once to initialize the array. Even if the computation function is slow and inefficient it won't be an issue.

ALX23z
  • 4,456
  • 1
  • 11
  • 18