Search code examples
pythonaesfinite-field

pyfinite gives wrong result for multiplication in field GF(2^8)


I am using pyfinte to calculate multiplication for AES over the field it uses which is F(2^8) but when I do as following:

from pyfinite import ffield

a = 0xbf
b = 0x03
F = ffield.FField(8)
c = F.Multiply(a, b)
print(hex(c))

the actual result is 0xdc but it should be 0xda here is how I solve it by hand:

0xbf*0x03 = 0xbf * 0x02 + 0xbf * 0x01 = 0b10111111 * 0x02 + 0xbf = 0b01111110 + 0x1B + 0xbf = 0b11011010 = 0xda

here is better format of my hand soloving:

enter image description here

so where is the wrong? is it on my solving by hand? or in pyfinite? how to solve this?


Solution

  • I'm not intimately familiar with AES, but from the link you provided it appears that the generator polynomial for the field is 0x11b. By default, pyfinite uses a different generator, which will produce different multiplication results:

    >>> f = ffield.FField(8)
    >>> hex(f.generator)
    '0x11d'
    

    If you specify the generator when you create the field object, you get the result you're expecting:

    >>> f = ffield.FField(8, gen=0x11b, useLUT=0)
    >>> hex(f.Multiply(0xbf, 0x03))
    '0xda'