from random import randint F = GF(2) PR = PolynomialRing(F, 'x') k = 31 P = PR.irreducible_element(k) m = 100 # Test the calculation for moving the left edge of the sliding window for _ in range(1): q = tuple(randint(0, 1) for _ in range(m)) for i in range(1, m+1): for j in range(i+1, m+1): assert (PR(q[:i]) % P) == (PR(q) % P) - (PR(f'x^{i}')*PR(q[i:]) % P) # Moving the right side of the window is as described in the Rabin Fingerprint article # Next step (TODO) # pattern = (1,0,1,1,1,1,0,1,1,0,1,0,1,0,1,1,1,1,1,0,0,1,0,0,0,1) # period = (1,0,1,1,1,1,0,1,1,0,1,0,1,0,1,1,1,1,1,0,0,1,0,0,0) # m = len(pattern) # s_period = tuple(list(period)+[0]*(m-len(period))) # pattern_1 = (1) # period_1 = (1) # s_period_1 = tuple(list(period_1)+[0]*(m-len(period_1))) # pattern_2 = (1,0) # period_2 = (1,0) # s_period_2 = tuple(list(period_2)+[0]*(m-len(period_2))) # pattern_4 = (1,0,1,1) # period_4 = (1,0,1) # s_period_4 = tuple(list(period_4)+[0]*(m-len(period_4))) # pattern_8 = (1,0,1,1,1,1,0,1) # period_8 = (1,0,1,1,1) # s_period_8 = tuple(list(period_8)+[0]*(m-len(period_8))) # pattern_16 = (1,0,1,1,1,1,0,1,1,0,1,0,1,0,1,1) # period_16 = (1,0,1,1,1,1,0,1,1,0,1,0) # s_period_16 = tuple(list(period_16)+[0]*(m-len(period_16))) # for i in range(len(pattern)-len(pattern_4)-len(period_4)): # assert PR(pattern[i+len(period_4):i+len(pattern_4)+len(period_4)]) == PR(pattern[i:i+len(pattern_4)] + pattern[i+len(pattern_4):i+len(pattern_4)+len(period_4)]) - PR(s_period_4) # assert PR(pattern[i+len(period_4):i+len(pattern_4)+len(period_4)]) == pattern[i-len(period_4):i+len(pattern_4)-len(period_4)]+pattern[i+len(pattern_4)-len(period_4):i+len(pattern_4)-len(period_4)] - PR(s_period_4) # Does the bits of a prime number correspond to the coefficients of an irreducible polynomial? k = 31 reducible = 0 irreducible = 0 for p in Primes()[200000000:200010000]:#[100000:100000]: # for p in range(2147483648,2147483648+10000): poly = PR(tuple(bin(p)[2:][::-1])) # print(f'{poly.degree()}') if poly.degree() > k: break if poly.degree() != k: continue irr = poly.is_irreducible() # print(f'The polynomial {poly}, corresponding to the prime {p}, being an irreducible polynomial is {irr}.') if irr: irreducible += 1 else: reducible += 1 print(f'{irreducible}/{reducible+irreducible} of the primes correspond to irreducible polynomials. That is {float(irreducible/(reducible+irreducible))*100}%') progress = 0 the_same = 0 different = 0 for p in Primes()[200000000:200010000]: progress += 1 print(f'\r{progress}/10000', end='') p_poly = PR(tuple(bin(p)[2:][::-1])) if not p_poly.is_irreducible(): continue # i = p + randint(1,2**29) i = p + randint(1,p-1) # print(f'{i}, {p}, {2*p}, {i%p}') i_poly = PR(tuple(bin(i)[2:][::-1])) i_mod_p = i % p i_mod_p_poly = PR(tuple(bin(i_mod_p)[2:][::-1])) # print(f'i_poly: {i_poly}') # print(f'p_poly: {p_poly}') # print(f'i_mod_p_poly: {i_mod_p_poly}') if i_mod_p_poly == i_poly % p_poly: the_same += 1 else: different += 1 # if not i_mod_p_poly == i_poly - (p_poly - PR(f'x^{p_poly.degree()}')): # if not i_mod_p_poly == i_poly % p_poly: # print() # print('ERROR') # print(i_mod_p_poly) # # print(i_poly - (p_poly - PR(f'x^{p_poly.degree()}'))) # print(i_poly % p_poly) # break # assert i_mod_p_poly == i_poly - (p_poly - PR(f'x^{p_poly.degree()}')) print() print(f'{the_same}/{the_same+different} are the same as if we did polynomial calculations. This is {float(the_same/(the_same+different))*100}%')