test lfsr17
This commit is contained in:
		
							
								
								
									
										29
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								main.py
									
									
									
									
									
								
							@@ -1,4 +1,6 @@
 | 
				
			|||||||
#!/usr/bin/env python3
 | 
					#!/usr/bin/env python3
 | 
				
			||||||
 | 
					from random import randint
 | 
				
			||||||
 | 
					from math import log2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class lfsr(object):
 | 
					class lfsr(object):
 | 
				
			||||||
    def __init__(self, state, taps):
 | 
					    def __init__(self, state, taps):
 | 
				
			||||||
@@ -28,12 +30,23 @@ class lfsr(object):
 | 
				
			|||||||
        return output
 | 
					        return output
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
key=[0]*16
 | 
					def test_lfsr17():
 | 
				
			||||||
key.append(1)
 | 
					    key = [randint(0, 1) for _ in range(16)] # first 16 bits
 | 
				
			||||||
taps = [16-0, 16-14]
 | 
					    key.append(1)  # prevent initial state from being {0}^17
 | 
				
			||||||
 | 
					    taps = [16-0, 16-14]
 | 
				
			||||||
 | 
					    lfsr17 = lfsr(key, taps)
 | 
				
			||||||
 | 
					    states = [lfsr17.state]
 | 
				
			||||||
 | 
					    for _ in range(2**17-2):
 | 
				
			||||||
 | 
					        lfsr17.shift()
 | 
				
			||||||
 | 
					        states.append(lfsr17.state)
 | 
				
			||||||
 | 
					    sorted_states = sorted(states, key=lambda x: tuple(x))
 | 
				
			||||||
 | 
					    for i in range(2**17-2):
 | 
				
			||||||
 | 
					        if sorted_states[i] == sorted_states[i+1]:  # compare each state with the next state in the sorted list, if 2 states are identical they should be next to each other
 | 
				
			||||||
 | 
					            print(f'state {sorted_states[i]} appears at least 2 times')
 | 
				
			||||||
 | 
					            return False
 | 
				
			||||||
 | 
					    n_state = len(sorted_states)
 | 
				
			||||||
 | 
					    n_state_log = log2(n_state+1)
 | 
				
			||||||
 | 
					    print(f'all {n_state} = 2^({n_state_log})-1 states are different')
 | 
				
			||||||
 | 
					    return True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
lfsr17 = lfsr(key, taps)
 | 
					test_lfsr17()
 | 
				
			||||||
 | 
					 | 
				
			||||||
# Generate 100 bits
 | 
					 | 
				
			||||||
for _ in range(100):
 | 
					 | 
				
			||||||
    print(lfsr17.shift(), end=' ')
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user