def gen_m_sequence(taps, init=0x2): degree = utils.get_highest_set_bit(taps) mask = (2 ** (1+degree)) - 1 bins = init done = 0 output = [] while not done: output.append(1 if bins & 1 else 0) bins = ((bins >> 1) + ((utils.count_bits(taps & bins) % 2) << (degree-1))) & mask if bins == init or bins == 0: done = 1 return numpy.array(output)
def get_primitive_polys_gf2(deg, max_num=None, random=True): # From http://www.seanerikoconnor.freeservers.com/Mathematics/AbstractAlgebra/PrimitivePolynomials/theory.html # Step 1: Generate a polynomial r = (2 ** deg) - 1 ret = [] for poly in xrange(1 << deg, 1 << (deg + 1)): # Step 2 (not needed for GF2) # Step 3: check f(a)!=0 for 1 <= a <= p-1 if (utils.count_bits(poly) % 2) == 0: continue # Step 4: if not berlekamp_factorisation_check(poly): continue # Step 5: x**r == a(mod f(x), p) for some integer a (gf(2) means a == 1) if utils.gf2_mod(1 << r, poly) != 1: continue # Step 6: skip # Step 7: p_k = list(set(utils.find_prime_factors(r))) passed = True for p in p_k: if utils.gf2_mod(1 << (r/p), poly) == 1: passed = False if not passed: continue #print poly ret.append(poly) print "Found poly = " + bin(poly) if max_num and len(ret) >= max_num: return ret return ret