Skip to content

Instantly share code, notes, and snippets.

@ildyria
Created April 10, 2017 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ildyria/20e3b71675da198b51ea41357605c297 to your computer and use it in GitHub Desktop.
Save ildyria/20e3b71675da198b51ea41357605c297 to your computer and use it in GitHub Desktop.
quick verification of equivalence between Galois and Fibo representation
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# LICENCE MIT
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to
# do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
def lxor(a, b):
if a == '0' and b == '0': return '0'
if a == '1' and b == '0': return '1'
if a == '0' and b == '1': return '1'
if a == '1' and b == '1': return '0'
return '?'
def bit_to_num(a):
if a == '0': return 0
if a == '1': return 1
if a == '?': raise ValueError('Error: Converting unknown value to number')
def gen_bit(a):
if a == 1: return '1'
return '0'
def getbit(state, index):
return state[index]
def setbit(state, index, value):
state[index] = value
def print_state(state):
print(''.join(state))
def multiplexer(state):
getindex = bit_to_num(state[28])
getindex += 2 * bit_to_num(state[25])
getindex += 4 * bit_to_num(state[22])
getindex += 8 * bit_to_num(state[19])
return getindex + 1
def iterate(state):
bit = state.pop()
# x^30 + x^17 + x^16 + x^13 + x^11 + x^7 + x^5 + x^3 + x^2 + x + 1
indexes = [1, 2, 3]
for i in indexes:
b = getbit(state, i)
setbit(state, i, lxor(bit, b))
state.insert(0, bit)
return bit
def iterate2(state):
bit = state.pop()
ret = bit
indexes = [0, 1, 2]
for i in indexes:
b = getbit(state, i)
bit = lxor(bit, b)
state.insert(0, bit)
return ret
def roundf(state,iteratef):
return iteratef(state)
def gen(iteratef):
lfsr_state = ['1','0','0','0','0']
print_state(lfsr_state)
output=[]
for x in range(15):
b = roundf(lfsr_state,iteratef)
if x % 5 == 0:
print()
print(b)
print("GALOIS")
gen(iterate)
print("FIBO")
gen(iterate2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment