Skip to content

Instantly share code, notes, and snippets.

@alejolp
Created June 28, 2010 18:41
Show Gist options
  • Save alejolp/456200 to your computer and use it in GitHub Desktop.
Save alejolp/456200 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Implementacion para entender cómo es el algoritmo de los Multiplicative
Scramblers.
Referencias:
- http://en.wikipedia.org/wiki/Scrambler#Multiplicative_.28self-synchronizing.29_scramblers
- http://books.google.com.ar/books?id=-ZAccwyQeXMC&pg=PA385&lpg=PA385&dq=%22multiplicative+scrambler%22+example+simple&source=bl&ots=OWO4F6c6E1&sig=8BSR2FGegYThmekEUpoerdjuie4&hl=es&ei=lTIlTNbXBMKB8gb59YnKDw&sa=X&oi=book_result&ct=result&resnum=3&ved=0CCEQ6AEwAg#v=onepage&q&f=false
Gracias Miguel por el link del libro.
"""
class MultScrambler(object):
def __init__(self, terms):
self.terms = terms
self.size = max(self.terms)
self.shiftreg = [0] * self.size
def process(self, scramble, bit):
if bit not in [0, 1]:
raise Exception('bit no es un bit')
tmp = 0
for t in self.terms:
tmp = tmp ^ self.shiftreg[t-1]
tmp = tmp ^ bit
self.shiftreg.pop(-1)
if scramble:
self.shiftreg.insert(0, tmp)
else:
self.shiftreg.insert(0, bit)
return tmp
def scramble(self, bit):
return self.process(True, bit)
def descramble(self, bit):
return self.process(False, bit)
if __name__ == '__main__':
ms = MultScrambler([3, 4])
bits = [1,0,1,1,0,0,0,1,0,1,1,0]
result = []
print "Original: \t", bits
for b in bits:
result.append(ms.scramble(b))
print "Scrambled: \t", result
# Para poder obtener la secuencia original, el estado inicial del registro
# de desplazamiento tiene que ser el mismo.
ms = MultScrambler([3, 4])
bits = result
result = []
for b in bits:
result.append(ms.descramble(b))
print "Descrambled: \t", result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment