permutations with words at set positions

155 views Asked by At

I am trying to write a script that creates all permutations of words, with a few in set positions. I'm basically trying to recover my 12 word mnemonic seed, in which i have a few extra letters but I know the position of a few for example, I know that word 1 is wild and word 5 is script and the last word is hurt. and I have 15 words that I want to try permutations on, so that I get

wild permword1 permword2 permword3 script permword6 permword7 permword8 permword9 permword10 permword11 hurt

` I have basic knowledge of python. I only know how to just print all the permutations of the words, however that takes longer and also prints some that wouldn't even be a possibility because one of the permutations has word 5 at a different position. I have my list of words, I'm just trying to recover my mnemonic seed in a much more efficient way, because I'm ending up creating so many permutations that my laptop runs out of space. I'm not looking to be reccomended btcrecover as I have a few issues with it.

Ideally I'd get a list like,

wild permword1 permword2 permword3 script permword6 permword7 permword8 permword9 permword10 permword11 hurt

wild permword1 permword2 permword3 script permword6 permword7 permword8 permword9 permword10 permword12 hurt

wild permword1 permword2 permword3 script permword6 permword7 permword8 permword9 permword10 permword13 hurt

I appreciate in advance!

just regular permutations, but i'm unsure of how to split ther words so that word 5 is always script.

1

There are 1 answers

7
mafe On

Generators

I guess that the reason your computer is running out of "space" is that you keep all created permutations in memory at once. This is a perfect use case for generators in python. You can think of generators as rules to create some value given an index value, instead of creating everything at once. See for example here: Understanding generators in Python

Set values in permutations

As to the words at set positions, you can just create a list for each permutation and then insert your pre-set words.

Solution

I think the following code solves your problem. In the last line, I print the permutation, you can replace this with whatever you want to do with each permutation.

import itertools

permwords = [f"permword{ind+1}" for ind in range(0,15)]

def produce_partially_set(worlist):
    worlist.insert(0, "wild")
    worlist.insert(4, "script")
    worlist.append("hurt")
    return worlist

indices_perm = itertools.permutations(range(0,12)) # 12 unknown words in "solution"
while True:
    indices = next(indices_perm)
    permutation = [permwords[x] for x in indices]
    mneumonic_seed = produce_partially_set(permutation)
    print(mneumonic_seed)

PS: the code will throw a "StopIteration", once it iterated through all possible permutations.