1

I need help finding the multiplicative inverse for a = 123, m = 256

I ran through it python script and it gave me 179

I want to do it by pen paper and see what the algorithm is doing.

so here is what I got so far

the euclidean algorithm

256 = 123 * 2 + 10
123 = 10 * 12 + 3
10 = 3 * 3 + 1 

it's equivelancy

1 = 10 - 3 * 3
3 = 123 - 10 * 12
10 = 256 - 123 * 2

it is when I need to back substitute where I get confuse, since I need to substitute 3 by its equivalency do I need to do it for both 3's

1 = 10 - (123 - 10 * 12) * (123 - 10 * 12)

A full step by step solution would be appreciated

Arthur
  • 13
  • "do I need to do it for both 3's" $;-;$ No, you only need it for the $3$ that comes from the previous step: $1 = (256 - 123 \cdot 2) - (123 - (256 - 123 \cdot 2) \cdot 12) \cdot 3 = \dots$ – dxiv Jan 27 '22 at 23:10
  • "do I need to do it for both 3's" No. The $256,123, 10, 3, 1$ are your remainder/divisors. The $2, 12, 3$ are you coefficient-quotients. We do our work of substituting the remainders with full equations. We leave the coefficient-quotients alone. That we happen to have a coincidence where a remainder/divisor is equal to a coefficient is just a coincidence. The threes serve different purposes. – fleablood Jan 27 '22 at 23:25
  • Search on "multiplicative inverse" at mathSE. For example, see this question. – user2661923 Jan 27 '22 at 23:45
  • I added an answer which may help you better understand the idea behinf back substitution (which makes the answer to your query clear). – Bill Dubuque Jan 28 '22 at 14:48

2 Answers2

-1

The divisors (the things you divide by) and the remainders (which become that things you divide by in the next step) are the "hard components". The quotient (how many times it goes into it) are your "soft coefficients". They serve different purposes. If I color them in different colors maybe you'll see why we don't need to substitute both $3$s. The "soft coefficients" will be in green. The "hard components" will be in red. And our original two components, $256$ and $123$ will be in blue. Our final result, $1$ will be in purple.

$\color{blue}{256} = \color{blue}{123} * \color{green}2 + \color{red}{10}$
$\color{blue}{123} = \color{red}{10} * \color{green}{12} + \color{red}3$
$\color{red}{10} = \color{red}3 * \color{green}3 + \color{purple}1$

So

$\color{purple}1 = \color{red}{10}-\color{red}3*\color{green}3$
$\color{red}3 = \color{blue}{123} - \color{red}{10}*\color{green}{12}$
$\color{red}{10}=\color{blue}{256} - \color{blue}{123} * \color{green}2$

Now we do the substitution:

$\color{purple}1 = \color{red}{10}-\color{red}3*\color{green}3$; and $\color{red}3 = \color{blue}{123} - \color{red}{10}*\color{green}{12}\implies $
$\color{purple}1 = \color{red}{10}-( \color{blue}{123} - \color{red}{10}*\color{green}{12})*\color{green}3$ which we simplify to
$\color{purple}1 = \color{red}{10}-( \color{blue}{123} - \color{red}{10}*\color{green}{12})*\color{green}3=\color{red}{10}(1-(-\color{green}{12}* \color{green}3)) - \color{blue}{123}*\color{green}3=\color{red}{10}*\color{green}{37}-\color{blue}{123}*\color{green}3$ or simply
$\color{purple}1 =\color{red}{10}*\color{green}{37}-\color{blue}{123}*\color{green}3$

and second substitution

$\color{purple}1 =\color{red}{10}*\color{green}{37}-\color{blue}{123}*\color{green}3$; and $\color{red}{10}=\color{blue}{256} - \color{blue}{123} * \color{green}2\implies$

$\color{purple}1 =(\color{blue}{256} - \color{blue}{123} * \color{green}2)*\color{green}{37}-\color{blue}{123}*\color{green}3$ which we simplify to
$\color{purple}1 =\color{blue}{256}*\color{green}{37} +\color{blue}{123}(- \color{green}2*\color{green}{37}-\color{green}3)=\color{blue}{256}*\color{green}{37} +\color{blue}{123}*\color{green}{(-77)}$ or simply
$\color{purple}1 =\color{blue}{256}*\color{green}{37} +\color{blue}{123}*\color{green}{(-77)}$

Thus we have the multiplicative inverse is $-77\pmod{256}\equiv 256-77\pmod{256}\equiv 179\pmod{256}$

We could have done another step:

$\color{purple}1 =\color{blue}{256}*\color{green}{37} +\color{blue}{123}*\color{green}{(-77)}=$
$\color{blue}{256}*\color{green}{37} +\color{blue}{123}*(\color{blue}{256}+\color{green}{(-77)}-\color{blue}{123}*\color{blue}{256}=$
$\color{blue}{256}(\color{green}{37}-\color{blue}{256}) + \color{blue}{123}*(\color{blue}{256}+\color{green}{(-77)}=$
$\color{blue}{256}*\color{green}{(-219)} + \color{blue}{123}*\color{green}{179}$

fleablood
  • 124,253
-1

$\require{enclose}$

Below is an intuitive explanation of how back-substitution works in the extended Euclidean algorithm. In the top line of the diagram below we compute your $\,\gcd(256,123)$ using the common remainder based version of Euclidean algorithm $\,\gcd(a,b) = \gcd(b,\:\!a\bmod b)\ $ if $\, a\ge b$.

enter image description here

The second row in the diagram shows the remainder (mod) reductions used in each step, e.g. the first is one is $\,256\bmod 123 = 10,\,$ via $\,256 - 2\cdot 123 = 10,\,$ and the second one is $\, 123-12\cdot 10 = 3.\,$ The algorithm terminates when we reach $\,10\bmod \color{#c00}3 = 1$ via $\,10-3\cdot\color{#c00} 3 = 1,\,$ a Bezout equation for $\,\gcd(10,\color{#c00}3)=1$. Next we reverse the Euclidean steps to convert this to a Bezout equation for $\,\gcd(256,123) = 1$. The first back substitution step ${\textstyle \enclose{circle}{1\:\!}}$ reverses the reduction step $\,\gcd(123,10)\to \gcd(10,3)$, which used $\,123-12\cdot 10 = 3$ to replace $123$ by $3$. Reversing, we use the equation to replace $\,3\,$ by $\,123\,$ by substituting $\,\color{#c00}3 = 123-12\cdot 10\,$ into $\,10-3\cdot\color{#c00}{3}=1\,$ to get $\, -3\cdot 123 + 37\cdot 10 = 1,\,$ our Bezout equation for $\,\gcd(123,10)=1.\,$ Back step ${\textstyle \enclose{circle}{2\:\!}}$ proceeds similarly, we reverse the first step $\,\gcd(256,123) = \gcd(123,10)\,$ using $\, 256-2\cdot 123 = \color{#0a0}{10}\,$ by substituting for $\color{#0a0}{10}$ in our Bezout equation for $\,\gcd(123,\color{#0a0}{10}),\,$ viz $\,-3\cdot 123 + 37\cdot\color{#0a0}{10} = 1,\,$ yielding $\,37\cdot 256 - 77\cdot 123 = 1,\,$ the sought Bezout equation for $\,\gcd(256,123) = 1$.

Remark $ $ Worth strong emphasis: this back-substitution method is notoriously error prone. To remedy this we can instead use the forward version described here and its links, or its fractional form. For example see answers here or here.

Bill Dubuque
  • 272,048
  • Please strive not to add more (dupe) answers to dupes of FAQs, cf. recent site policy announcement. – Cornman Jan 28 '22 at 22:38
  • 1
    @Conman Parroting my comment on your answer is not at all constructive. Nor does it make any sense since you have not supplied any dupe targets. – Bill Dubuque Jan 29 '22 at 09:26
  • The content of this question is exactly the same where you commented on my given answer, and I think you know that. – Cornman Jan 30 '22 at 03:22
  • @Cornman Please read more carefully. My comment that you parroted concerns duplication. No one has shown this question to be a dupe, so that comment does not apply here. I don't agree that the "content is exactly the same". This question specifically concerns what should be substituted for in back substitution in a specific method of modular inversion. But the other question asks about modular inversion methods in general (most of which don't even use back-substitution). – Bill Dubuque Jan 30 '22 at 08:23