From: lkcl Date: Sun, 24 Jan 2021 21:03:44 +0000 (+0000) Subject: (no commit message) X-Git-Tag: convert-csv-opcode-to-binary~349 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=a3400ab879ffebb7193cd739dbbabedab48c2a7d;p=libreriscv.git --- diff --git a/openpower/sv/bitmanip.mdwn b/openpower/sv/bitmanip.mdwn index 5cc43ba7b..156fb2b53 100644 --- a/openpower/sv/bitmanip.mdwn +++ b/openpower/sv/bitmanip.mdwn @@ -426,6 +426,56 @@ uint_xlen_t clmulr(uint_xlen_t RA, uint_xlen_t RB) return x; } ``` +# Galois Field Multiply + +``` +from functools import reduce + +# constants used in the multGF2 function +mask1 = mask2 = polyred = None + +def setGF2(degree, irPoly): + """Define parameters of binary finite field GF(2^m)/g(x) + - degree: extension degree of binary field + - irPoly: coefficients of irreducible polynomial g(x) + """ + def i2P(sInt): + """Convert an integer into a polynomial""" + return [(sInt >> i) & 1 + for i in reversed(range(sInt.bit_length()))] + + global mask1, mask2, polyred + mask1 = mask2 = 1 << degree + mask2 -= 1 + polyred = reduce(lambda x, y: (x << 1) + y, i2P(irPoly)[1:]) + +def multGF2(p1, p2): + """Multiply two polynomials in GF(2^m)/g(x)""" + p = 0 + while p2: + if p2 & 1: + p ^= p1 + p1 <<= 1 + if p1 & mask1: + p1 ^= polyred + p2 >>= 1 + return p & mask2 + +if __name__ == "__main__": + + # Define binary field GF(2^3)/x^3 + x + 1 + setGF2(3, 0b1011) + + # Evaluate the product (x^2 + x + 1)(x^2 + 1) + print("{:02x}".format(multGF2(0b111, 0b101))) + + # Define binary field GF(2^8)/x^8 + x^4 + x^3 + x + 1 + # (used in the Advanced Encryption Standard-AES) + setGF2(8, 0b100011011) + + # Evaluate the product (x^7)(x^7 + x + 1) + print("{:02x}".format(multGF2(0b10000000, 0b10000011))) +``` # crc