# Calculate RSA private exponent when given public exponent and the modulus factors using extended euclid

I was reading this answer that shows how to calculate the inverse of public exponent, i got to the point where we apply the euclidean algorithm and finish substituting to get

(40−2×17)−1×(17−2×(40−2×17))=1

it states "this is a linear combination of 17 and 40, after simplifying you get:"

(−7)×17+3×40=1

which gives us the d = -7

but i'm stuck as to how we simplified down to

(−7)×17+3×40=1

from:

(40−2×17)−1×(17−2×(40−2×17))=1

i would be grateful if someone could explain it to me

Try just expanding the parentheses. Once they're all gone, collect all the multiples of $40$ and $17$ together:

$(40 - 2 \times 17) - 1 \times (17 - 2 \times (40 - 2 \times 17)) = 1$

$1\times40 - 2 \times 17 - 1 \times 17 + 2 \times (40 - 2 \times 17) = 1$

$1\times40 - 2 \times 17 - 1 \times 17 + 2 \times 40 - 4 \times 17 = 1$

$(1 + 2)\times40 - 2 \times 17 - 1 \times 17 - 4 \times 17 = 1$

$(1 + 2)\times40 - (2 + 1 + 4) \times 17 = 1$

$3\times40 - 7 \times 17 = 1$, or

$(-7) \times 17 + 3\times 40 = 1$