# What do these two lines in electrum-words.cpp do?

Analyzing the seed logic here;

Can someone please explain this expression:

``````w= w + word_list_length * (((word_list_length - w) + w) % word_list_length) +
word_list_length * word_list_length * (((word_list_length - w) + w) % word_list_length);
``````

Once I understand, I would like to PR a comment for it. Are such PR's welcome?

## 1 Answer

Each 3 words gets converted to 4 bytes of the secret key.

This particular line is calculating those 4 bytes (the unsigned integer at `w`) in the loop which is taking 3 words at a time. It's essentially the reverse of the 4 bytes to 3 words in the function `bytes_to_words`.

• Oh I understand the concept (the "what"), but I'm looking for guidance on the logic (the "how"). (I am open to draw it out and understand it by analyzing, but tackling all such snippets in this manner adds up quickly and is highly inefficient for diving into the project.) – hokkjoy Nov 14 '18 at 23:13
• But the line is a simple expression using the indexes of the three words in the word list. Apologies, but I'm not sure what the confusion is with this. – jtgrassie Nov 15 '18 at 3:47
• For example, what does `(((word_list_length - w) + w) % word_list_length)` represent? If you'd assign it to a variable with meaningful name, what would that name be? Why do we subtract the first word's index from the total of indexes, then add the second word's index (and then run modulo on it)? – hokkjoy Dec 7 '18 at 19:54
• It's not important to assign to "meaningful name". What is important is that the math is reversible (i.e. you can take a set of words to produce a key and given a key produce the same words) and deterministic (the same set of words always produces the same key and vice versa). There is no need for a PR to change variable names for such simple to understand expressions / math. – jtgrassie Dec 8 '18 at 21:35
• I wrote my initial question "just to save time". Since then I went through the calculation manually with several example values in order to find a pattern. Result: I am still unable to extract the original reasoning behind the math and why it was chosen exactly the way it was. Your comment makes me think that I am lacking mental capacity, which may be true. So I'll just shut up until it develops. – hokkjoy Dec 10 '18 at 14:24