def fromcomplete(self, sequence): '''Find the seed when given the whole sequence.''' # Let's use lits #lastk = [deque()]*1024 lastk = [[]]*1024 k = len(sequence) while len(self.p) > 0: i = self.p.pop() srandom(i) for x in xrange(k): r = random() if r == sequence[x]: lastk[i].append(r) else: # We got the wrong value, pointless to go on break if lastk[i] == sequence: return i # Happens if the seed is larger than 2**10-1 or we have a bad sequence. return None
def fromcomplete(self, sequence): '''Find the seed when given the whole sequence.''' # Let's use lits #lastk = [deque()]*1024 lastk = [[]] * 1024 k = len(sequence) while len(self.p) > 0: i = self.p.pop() srandom(i) for x in xrange(k): r = random() if r == sequence[x]: lastk[i].append(r) else: # We got the wrong value, pointless to go on break if lastk[i] == sequence: return i # Happens if the seed is larger than 2**10-1 or we have a bad sequence. return None
def findseed(self, sequence, m=1): '''Finds the seed given a sequcence that does not have to be complete from the seed. Let S_0 be the initial sequence of length k with s as seed, and S_n be the sequence with radom variables s_{n-k},..,s_k. Let S_1 be the observerd sequnce, also of length k. In each iteration, this program will generate all sequences S_i of s_{last},...,s{last+k+m}. Thus m is in a wayan estimation of C. One of those performance-tuning thingies. The program makes infinitely many iterations until it finds it unless the debug value is set. ''' # Giveup option for testing purposes giveup = 50 k = len(sequence) lastk = [deque([]) for i in self.p] # Expand all the deques by k elements from p[i] for each i. # => generate initial sequences. # 1024*k operations → O(k) for i in self.p: srandom(i) #lastk[i] = deque([random() for _ in range(k)]) for _ in xrange(k): lastk[i].append(random()) # If we recieved a sequence dervied directly from the seed if list(lastk[i]) == sequence: return i #while True: for _ in xrange(giveup): # This has to be a little more sophisticated than a crude # bruteforce attack. # # This program has the rather curious property that it has # an essentially unknown running boundary. It is # completely dependent upon the number of calls made to # the PRNG before the sequence given to us appeared (until # we find the sequence, we stay inside the while loop. Let # C denote this value. Let m be our best estimation of C. # # For every value i, we have started with the intial sequnce # of length k. In each iteration we will check east of the # m+k sequential ques for a match. for i in self.p: for _ in range(m+k): srandom(lastk[i][-1]) v = random() lastk[i].popleft() lastk[i].append(v) if list(lastk[i]) == sequence: return i # If we haven't found anything yet, we stay in the while loop. This program # may never halt, if we are given a bad sequence (that originated from some # other PRNG or perhaps not from a PRNG at all). # Giving up return -1
#coding: utf-8 from stattests import StatTests, FipsTests from seedfind import Seedfinder from avrlibcrandom import random, srandom f = open('samples.txt').read().split('\n') seed = int(f[0]) seed = 526 srandom(seed) seq = [random() for _ in range(1000)][990:] print "Ég sendi inn þessa runu", seq s = Seedfinder() print "Ég fann", s.findseed(seq, 0), "en þú gafst mér", seed
def findseed(self, sequence, m=1): '''Finds the seed given a sequcence that does not have to be complete from the seed. Let S_0 be the initial sequence of length k with s as seed, and S_n be the sequence with radom variables s_{n-k},..,s_k. Let S_1 be the observerd sequnce, also of length k. In each iteration, this program will generate all sequences S_i of s_{last},...,s{last+k+m}. Thus m is in a wayan estimation of C. One of those performance-tuning thingies. The program makes infinitely many iterations until it finds it unless the debug value is set. ''' # Giveup option for testing purposes giveup = 50 k = len(sequence) lastk = [deque([]) for i in self.p] # Expand all the deques by k elements from p[i] for each i. # => generate initial sequences. # 1024*k operations → O(k) for i in self.p: srandom(i) #lastk[i] = deque([random() for _ in range(k)]) for _ in xrange(k): lastk[i].append(random()) # If we recieved a sequence dervied directly from the seed if list(lastk[i]) == sequence: return i #while True: for _ in xrange(giveup): # This has to be a little more sophisticated than a crude # bruteforce attack. # # This program has the rather curious property that it has # an essentially unknown running boundary. It is # completely dependent upon the number of calls made to # the PRNG before the sequence given to us appeared (until # we find the sequence, we stay inside the while loop. Let # C denote this value. Let m be our best estimation of C. # # For every value i, we have started with the intial sequnce # of length k. In each iteration we will check east of the # m+k sequential ques for a match. for i in self.p: for _ in range(m + k): srandom(lastk[i][-1]) v = random() lastk[i].popleft() lastk[i].append(v) if list(lastk[i]) == sequence: return i # If we haven't found anything yet, we stay in the while loop. This program # may never halt, if we are given a bad sequence (that originated from some # other PRNG or perhaps not from a PRNG at all). # Giving up return -1