Created
March 23, 2019 16:25
-
-
Save piotrek-k/fbcf3da7fdb3d3c813b99fdc7475a219 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Implementacja w oparciu o | |
https://en.wikipedia.org/wiki/Scrambler#Multiplicative_(self-synchronizing)_scramblers | |
""" | |
from operator import xor | |
class V34_Scrambler: | |
""" | |
Klasa skramblująca sekwencję bitów | |
""" | |
seq_state = [] # obiekt przechowujący aktualny stan sekwencji bitów | |
def __init__(self, input_seq, val1_index, val2_index): | |
""" | |
input_seq: [bool] | |
tablica wartości boolean | |
val1_index: int | |
index wartości użytej do generowania kolejnego bitu w sekwencji | |
val2_index: int | |
index wartości użytej do generowania kolejnego bitu w sekwencji | |
""" | |
# inicjalizacja zmiennych w klasie | |
self.input_seq = input_seq | |
self.val1_index = val1_index | |
self.val2_index = val2_index | |
self.seq_state = input_seq | |
# sprawdzienie poprawności danych | |
if (self.val1_index < 0 or self.val2_index < 0 or | |
self.val1_index > len(self.input_seq) or self.val2_index > len(self.input_seq)): | |
raise Exception('indexes should point to input_seq array values') | |
if len(self.input_seq) < 2: | |
raise Exception('length of input_seqence should be at least 2') | |
def next(self, input_bit) -> bool: | |
""" | |
Zapisuje podany bit w tablicy, zwraca następny "zeskramblowany" bit | |
input_bit: bool | |
bit wejściowy | |
""" | |
generated_bit = xor(self.seq_state[self.val1_index], self.seq_state[self.val2_index]) | |
output = xor(input_bit, generated_bit) | |
self.seq_state.insert(0, output) # dodaj element na początku listy | |
del self.seq_state[-1] # usuń ostatni element z listy | |
return output | |
input_sequence = [True, False, False, True, False] | |
scrambler = V34_Scrambler(input_sequence, 2, 4) | |
print(scrambler.next(True)) | |
print(scrambler.next(True)) | |
print(scrambler.next(True)) | |
print(scrambler.next(True)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment