コード例 #1
0
ファイル: main.py プロジェクト: esterp/xkcd-hashing
def run_worker(do_submit=True, time_limit=None):
	best = float('inf')
	guess = random.getrandbits(RANDOM_BIT_LEN)
	t = time.time()
	i = 0
	while True:
		if gmpy is not None:
			encoded = gmpy.digits(guess, 62).encode('ascii')
			digest = gmpy.mpz(skein.skein1024(encoded).digest()[::-1] + b'\0', 256)
			diff = gmpy.hamdist(digest, TARGET)
		else: # fallback implementation
			encoded = hex(guess)[2:].encode('ascii')
			digest = int(skein.skein1024(encoded).hexdigest(), 16)
			diff = bin(digest ^ TARGET).count('1')
		if diff < best:
			best = diff
			if do_submit:
				submit(guess)
			print('Found new best input with diff [%.3d]: \"%s\"' %
				(diff, guess))
		i += 1
		if time_limit and time.time() - t > time_limit:
			break
		guess += 1
	return i
コード例 #2
0
ファイル: 20120621a.py プロジェクト: BIGtigr/xgcode
def get_transition_matrix(
        selection, mutation, recombination,
        nchromosomes, npositions):
    """
    This factors into the product of two transition matrices.
    The first transition matrix accounts for selection and recombination.
    The second transition matrix independently accounts for mutation.
    @param selection: a fitness ratio
    @param mutation: a state change probability
    @param recombination: a linkage phase change probability
    @param nchromosomes: number of chromosomes in the population
    @param npositions: number of positions per chromosome
    @return: a numpy array
    """
    nstates = 1 << (nchromosomes * npositions)
    # define the mutation transition matrix
    P_mutation = np.zeros((nstates, nstates))
    for source in range(nstates):
        for sink in range(nstates):
            ndiff = gmpy.hamdist(source, sink)
            nsame = nchromosomes * npositions - ndiff
            P_mutation[source, sink] = (mutation**ndiff)*((1-mutation)**nsame)
    """
    print 'mutation transition matrix row sums:'
    for value in np.sum(P_mutation, axis=1):
        print value
    print
    """
    # init the unnormalized selection and recombination transition matrix
    M = np.zeros((nstates, nstates))
    for source_index in range(nstates):
        K_source = popgenmarkov.int_to_bin2d(
                source_index, nchromosomes, npositions)
        chromosome_distn = popgenmarkov.get_chromosome_distn(
                selection, recombination, K_source)
        for x in product(range(1<<npositions), repeat=nchromosomes):
            weight = 1
            for index in x:
                weight *= chromosome_distn[index]
            sink_index = 0
            for i, index in enumerate(x):
                sink_index <<= npositions
                sink_index |= index
            M[source_index, sink_index] = weight
    M_row_sums = np.sum(M, axis=1)
    P_selection_recombination = (M.T / M_row_sums).T
    """
    print 'selection-recombination matrix row sums:'
    for value in np.sum(P_selection_recombination, axis=1):
        print value
    print
    """
    # define the state transition probability matrix
    P = np.dot(P_selection_recombination, P_mutation)
    return P
コード例 #3
0
ファイル: popgenmarkov.py プロジェクト: BIGtigr/xgcode
def get_mutation_transition_matrix(mutation, nchromosomes, npositions):
    nstates = 1 << (nchromosomes * npositions)
    # map from ndiff to probability
    ndiff_to_p = np.zeros(nchromosomes * npositions + 1)
    for ndiff in range(nchromosomes * npositions + 1):
        nsame = nchromosomes * npositions - ndiff
        ndiff_to_p[ndiff] = (mutation**ndiff)*((1-mutation)**nsame)
    # define the mutation transition matrix
    P = np.zeros((nstates, nstates))
    for source in range(nstates):
        for sink in range(nstates):
            P[source, sink] = ndiff_to_p[gmpy.hamdist(source, sink)]
    return P
コード例 #4
0
 def get_rate_matrix(self, X=None):
     if X is not None:
         self._check_params(X)
     d = self.d
     n = self.get_nstates()
     Q = np.zeros((n, n))
     for a in range(n):
         for b in range(n):
             dist = gmpy.hamdist(a, b)
             if dist == 0:
                 Q[a, b] = -d
             elif dist == 1:
                 Q[a, b] = 1
     return Q
コード例 #5
0
ファイル: hash.py プロジェクト: perryh/xkcd-hash
    def test(self, b):
        x = skein.skein1024(b)
        hex_x = x.hexdigest()
        int_x = int(hex_x, 16)
        dist = hamdist(int_x, int_goal)

        if dist < self.best:
            self.best = dist

            if dist < best_global.value:  # XXX: Race
                best_global.value = dist
                print("{} bits - {} - hashes to {}".format(dist, b, hex_x))

        if hex_x == goal:
            print("omg")
            print(b)
            print(repr(b))
            quit()
コード例 #6
0
ファイル: xkcd-hashing.py プロジェクト: yasyf/xkcd-hashing
def run_worker(do_submit=True, time_limit=None):
	best = float('inf')
	guess = random.getrandbits(RANDOM_BIT_LEN)
	t = time.time()
	i = 0
	while True:
		encoded = gmpy.digits(guess, 62).encode('ascii')
		digest = gmpy.mpz(skein.skein1024(encoded).digest()[::-1] + b'\0', 256)
		diff = gmpy.hamdist(digest, TARGET)
		if diff < best:
			best = diff
			if do_submit:
				submit(guess)
			print('Found new best input with diff [%.3d]: \"%s\"' %
				(diff, guess))
		i += 1
		if time_limit and time.time() - t > time_limit:
			break
		guess += 1
	return i
コード例 #7
0
ファイル: pgmfancy.py プロジェクト: argriffing/xgcode
def get_mutation_transition_matrix_s(
        ci_to_short, short_to_count, sorted_chrom_lists,
        mutation, nchromosomes, npositions):
    nstates = len(sorted_chrom_lists)
    # map from ndiff to probability
    ndiff_to_p = np.zeros(nchromosomes * npositions + 1)
    for ndiff in range(nchromosomes * npositions + 1):
        nsame = nchromosomes * npositions - ndiff
        ndiff_to_p[ndiff] = (mutation**ndiff)*((1-mutation)**nsame)
    # define the mutation transition matrix
    P = np.zeros((nstates, nstates))
    for parent_chroms in sorted_chrom_lists:
        parent_ci = chroms_to_index(sorted(parent_chroms), npositions)
        parent_short = ci_to_short[parent_ci]
        for child_chroms in product(range(1<<npositions), repeat=nchromosomes):
            child_ci = chroms_to_index(sorted(child_chroms), npositions)
            child_short = ci_to_short[child_ci]
            child_index = chroms_to_index(child_chroms, npositions)
            diff = gmpy.hamdist(parent_ci, child_index)
            P[parent_short, child_short] += ndiff_to_p[diff]
    return P
コード例 #8
0
ファイル: pgmfancy.py プロジェクト: BIGtigr/xgcode
def get_mutation_transition_matrix_s(ci_to_short, short_to_count,
                                     sorted_chrom_lists, mutation,
                                     nchromosomes, npositions):
    nstates = len(sorted_chrom_lists)
    # map from ndiff to probability
    ndiff_to_p = np.zeros(nchromosomes * npositions + 1)
    for ndiff in range(nchromosomes * npositions + 1):
        nsame = nchromosomes * npositions - ndiff
        ndiff_to_p[ndiff] = (mutation**ndiff) * ((1 - mutation)**nsame)
    # define the mutation transition matrix
    P = np.zeros((nstates, nstates))
    for parent_chroms in sorted_chrom_lists:
        parent_ci = chroms_to_index(sorted(parent_chroms), npositions)
        parent_short = ci_to_short[parent_ci]
        for child_chroms in product(range(1 << npositions),
                                    repeat=nchromosomes):
            child_ci = chroms_to_index(sorted(child_chroms), npositions)
            child_short = ci_to_short[child_ci]
            child_index = chroms_to_index(child_chroms, npositions)
            diff = gmpy.hamdist(parent_ci, child_index)
            P[parent_short, child_short] += ndiff_to_p[diff]
    return P
コード例 #9
0
def hamdist(x, y):
  return gmpy.hamdist(x, y)/64.0  
コード例 #10
0
     assert gmpy.getbit(n, i) == getbit(n, i)
 except AssertionError:
     print 'getbit fail %d, %d' % (n, i)
     raise
 try:
     assert gmpy.setbit(n, i) == setbit(n, i)
 except AssertionError:
     print 'setbit fail %d', n
     raise
 try:
     assert gmpy.lowbits(n, i + 1) == lowbits(n, i + 1)
 except AssertionError:
     print 'lowbits fail %d', n
     raise
 try:
     assert gmpy.hamdist(n, i) == hamdist(n, i)
 except AssertionError:
     print 'hamdist fail %d', n
     raise
 try:
     assert abs(flipbit(n, i) - n) == 2**i
 except AssertionError:
     print 'flipbit fail %d', n
     raise
 try:
     assert gmpy.scan0(n, i) == scan0(n, i)
 except AssertionError:
     print 'scan0 fail %d', n
     raise
 try:
     assert gmpy.scan1(n, i + 1) == scan1(n, i + 1)
コード例 #11
0
def hamdist(x, y):
  return gmpy.hamdist(x, y)/64.0