2017-08-29 15:25:27 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# adc/xor routine from:
|
|
|
|
# https://stackoverflow.com/questions/17411712/finding-seeds-for-a-5-byte-prng
|
|
|
|
|
2017-08-29 16:03:02 +00:00
|
|
|
# Pretty good choices for 8 bit seeds
|
2017-08-29 15:25:27 +00:00
|
|
|
adcxor_8bit = [
|
2017-08-29 16:03:02 +00:00
|
|
|
(0x03, 0x045),
|
|
|
|
(0x03, 0x073),
|
|
|
|
(0x03, 0x085),
|
|
|
|
(0x03, 0x0b3),
|
|
|
|
(0x0d, 0x04d),
|
|
|
|
(0x0d, 0x08d),
|
|
|
|
(0x23, 0x033),
|
|
|
|
(0x23, 0x0f3),
|
|
|
|
(0x3b, 0x03f),
|
|
|
|
(0x3b, 0x0ff),
|
|
|
|
(0x45, 0x07f),
|
|
|
|
(0x45, 0x0bf),
|
|
|
|
(0x5d, 0x073),
|
|
|
|
(0x5d, 0x0b3),
|
|
|
|
(0x73, 0x00d),
|
|
|
|
(0x73, 0x0cd),
|
|
|
|
(0x7d, 0x005),
|
|
|
|
(0x7d, 0x033),
|
|
|
|
(0x7d, 0x0c5),
|
|
|
|
(0x7d, 0x0f3),
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_prng8(bits, adc, xor):
|
2017-08-29 15:25:27 +00:00
|
|
|
order = []
|
|
|
|
r = 0
|
2017-08-29 16:03:02 +00:00
|
|
|
for i in range(2**bits):
|
|
|
|
r=(((r>>(bits-1)) & 1)+r+r+adc)^xor
|
|
|
|
r&=(1<<(bits-1)) | ((1<<(bits-1))-1)
|
2017-08-29 15:25:27 +00:00
|
|
|
order.append(r)
|
|
|
|
return order
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
gen_16_bit = False
|
2017-08-29 16:03:02 +00:00
|
|
|
summary = True
|
|
|
|
show = True
|
2017-08-29 15:25:27 +00:00
|
|
|
|
|
|
|
if gen_16_bit:
|
|
|
|
random_bits = 16
|
|
|
|
else:
|
|
|
|
random_bits = 8
|
|
|
|
|
|
|
|
valid_seeds = []
|
2017-08-29 16:03:02 +00:00
|
|
|
mc = 0
|
2017-08-29 15:25:27 +00:00
|
|
|
for BITS in range(2, random_bits + 1):
|
|
|
|
|
2017-08-29 16:03:02 +00:00
|
|
|
adc = 1 # Only odd add values produce useful results
|
2017-08-29 15:25:27 +00:00
|
|
|
while True:
|
2017-08-29 16:03:02 +00:00
|
|
|
xor = 1 # Only odd add values produce useful results
|
2017-08-29 15:25:27 +00:00
|
|
|
while True:
|
|
|
|
r = 0
|
2017-08-29 16:03:02 +00:00
|
|
|
c = ~0
|
2017-08-29 15:25:27 +00:00
|
|
|
values = []
|
|
|
|
while True:
|
2017-08-29 16:03:02 +00:00
|
|
|
r=(((r>>(BITS-1)) & 1)+r+r+adc)^xor
|
2017-08-29 15:25:27 +00:00
|
|
|
r&=(1<<(BITS-1)) | ((1<<(BITS-1))-1)
|
|
|
|
c += 1
|
|
|
|
values.append(r)
|
|
|
|
if r == 0:
|
|
|
|
break
|
2017-08-29 16:03:02 +00:00
|
|
|
if c >= mc:
|
|
|
|
mc = c
|
|
|
|
valid_seeds.append((BITS, adc, xor))
|
|
|
|
if not summary:
|
|
|
|
print("BITS: %d adc: %04x, xor: %04x" % (BITS, adc, xor))
|
|
|
|
xor += 2
|
|
|
|
xor &= (1<<(BITS-1)) | ((1<<(BITS-1))-1)
|
|
|
|
if xor == 1:
|
2017-08-29 15:25:27 +00:00
|
|
|
break
|
2017-08-29 16:03:02 +00:00
|
|
|
adc += 2
|
|
|
|
adc &= ((1<<(BITS-1))-1) # * Do not check complements
|
|
|
|
if adc == 1:
|
2017-08-29 15:25:27 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
if summary:
|
2017-08-29 16:03:02 +00:00
|
|
|
for bits, adc, xor in valid_seeds:
|
|
|
|
print("BITS: %d adc(adc): %04x, xor(eor): %04x" % (bits, adc, xor))
|
|
|
|
if show:
|
|
|
|
values = get_prng8(bits, adc, xor)
|
|
|
|
unique_check = set(values)
|
|
|
|
if len(values) == len(unique_check):
|
|
|
|
print("%d: %s" % (len(values), str(values)))
|
|
|
|
else:
|
|
|
|
print("Failed unique check")
|