#!/usr/bin/python3 # # Author: Joao H de A Franco (jhafranco@acm.org) # # Description: Binary finite field multiplication in Python 3 # # Date: 2012-02-16 # # License: Attribution-NonCommercial-ShareAlike 3.0 Unported # (CC BY-NC-SA 3.0) #=========================================================== from functools import reduce import time # 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) """ global mask1, mask2, polyred mask1 = mask2 = 1 << degree mask2 -= 1 if sum(irPoly) <= len(irPoly): polyred = reduce(lambda x, y: (x << 1) + y, irPoly[1:]) else: polyred = poly2Int(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 #============================================================================= # Auxiliary formatting functions #============================================================================= def int2Poly(bInt): """Convert a "big" integer into a "high-degree" polynomial""" exp = 0 poly = [] while bInt: if bInt & 1: poly.append(exp) exp += 1 bInt >>= 1 return poly[::-1] def poly2Int(hdPoly): """Convert a "high-degree" polynomial into a "big" integer""" bigInt = 0 for exp in hdPoly: bigInt += 1 << exp return bigInt def i2P(sInt): """Convert a "small" integer into a "low-degree" polynomial""" return [(sInt >> i) & 1 for i in reversed(range(sInt.bit_length()))] def p2I(ldPoly): """Convert a "low-degree" polynomial into a "small" integer""" return reduce(lambda x, y: (x << 1) + y, ldPoly) def ldMultGF2(p1, p2): """Multiply two "low-degree" polynomials in GF(2^n)/g(x)""" return multGF2(p2I(p1), p2I(p2)) def hdMultGF2(p1, p2): """Multiply two "high-degree" polynomials in GF(2^n)/g(x)""" return multGF2(poly2Int(p1), poly2Int(p2)) if __name__ == "__main__": start_time = time.time() start_clock = time.clock() # Check if (x + 1)(x^2 + 1) == x^2 # assert ldMultGF2([1, 1], [1, 0, 1]) == p2I([1, 0, 0]) # Define binary field GF(2^571)/x^64 + /x^64 + x^4 + x^3 + x + 1 setGF2(64, [64,4,3,1,0]) a = poly2Int([131+68+65+9+6+3+0]) print(a) if a & mask1: a ^= polyred print(a) # Calculate the product of two polynomials in GF(2^64)/x^63 + x^2 + x + 1 * x^5 + x^3 print(int2Poly(hdMultGF2([63,2,1,0], [5,3]))) print ((time.clock() - start_clock)*1000, "ms - CPU time") print ((time.time() - start_time)*1000, "ms - total time")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question