2

If I'm calculating $a^{-1} \equiv 1 \pmod n$ where $a$ is negative. Do I simply add $kn$ to $a'$ until $0\lt a' \lt n$?

I'm currently using the extended euclidean algorithm to calculate my modular multiplicative inverses since I already have to make sure that $a$ and $n$ are coprime. From what little number theory I understand $a'=a+kn$ is going to give me the same result as $a \pmod n$. So that should mean that $a' \equiv 1 \pmod n$ is the same as $a \equiv 1 \pmod n$

I've confirmed this with a few values below but don't know if I'm understanding this properly.

$a=-36 \;\; a'=14$

$9 = -36^{-1} \pmod{25}$

$9 = 14^{-1} \pmod {25}$

$a=-11\;\; a'=15$

$7 = -11^{-1} \pmod{26}$

$7 = 15^{-1} \pmod{26}$

Here's a link to my python code. https://paste.debian.net/1117624/

133794m3r
  • 155
  • 1
  • 8
  • 2
    You can do it whichever way you want! For example, I think it is easier to invert $-1$ than $n-1$. And if $n$ is odd, $-2$ is easier than $n-2$. Euclid's algorithm doesn't care either. More generally, if $b$ and $a$ are inverses modulo $n$, so are $-b$ and $-a$. You can negate the answer later. – Jyrki Lahtonen Nov 23 '19 at 20:58
  • Well when using his algorithm you have to make sure that you're still using the correct value and convert it to positive for it not to not work for me. – 133794m3r Nov 23 '19 at 21:00
  • 1
    It may be to your advantage to pick whichever of $a$ and $a'$ has a smaller absolute value. That should speed up Euclid in average. – Jyrki Lahtonen Nov 23 '19 at 21:01
  • Depending on what you are doing with the end result you may never need to convert it to positive. Of course, if this is for a computer program, then you need to worry about the remainder operation being defined to give negative answers given a negative input. I created an interesting bug when I thought that Borland Pascal and Intel Pentium will co-operate and always give me remainders in the range $0\ldots n-1$ :-) – Jyrki Lahtonen Nov 23 '19 at 21:05
  • 1
  • I'm using the extended euclidean algorithm to make sure that $a$ and $n$ are coprime and then have it return the gcd, x and y. Then I'm simply doing $x \pmod n$. – 133794m3r Nov 23 '19 at 21:13
  • 1
    Since you mention the Hill cipher, you may want to look at this also. I won't post any answers for I am generally opposed to reproducing earlier material, will gladly explain it though. – Jyrki Lahtonen Nov 23 '19 at 21:19
  • That is precisely the value of my own determinant except in reverse. Mine was -11 and I was trying to get and finally arrived at 15. I'm going to use my knowledge to generate challenges for the students to complete as part of a competition to give them applications for this stuff. Here's my current lab for RSA(not complete yet though) https://drive.google.com/file/d/1o8IvNpFDEq126xQmxhxtnS2sICHSy1-t/view?usp=sharing – 133794m3r Nov 23 '19 at 21:22

2 Answers2

2

Hint: $ $ like sums & products, inverses too are preserved on replacing argument(s) by a congruent one

Congruence Inverse Law $\ \color{#c00}{\bar a\equiv a}\,\Rightarrow\,{\bar a}^{-1}\equiv a^{-1}\,$ if $\ a\,$ is invertible, i.e. $\, ab\equiv 1\,$ for some $b$.

Proof $\ $ Notice $\,\ \color{c00}ab\equiv 1\ \Rightarrow\ \color{#c00}{\bar a} b\equiv \color{#c00}ab\equiv 1\,$ by applying the Congruence Product Rule. Therefore we conclude that $\, {\bar a}^{-1}\!\equiv b\equiv a^{-1}\,$ by Uniqueness of Inverses.

Bill Dubuque
  • 272,048
  • I've yet to have any formal math beyond Trig/Stats so I'm going at this from a self-learning way based upon solving problems in projects I'm working on. I'm guessing that means that I was correct in my assumptions. Right now I'm writing a lab for solving Hill Ciphers and the determinate can be negative so I need to make sure that I'm showing them how to properly solve it. But also taking into account that even trig/precalc is an optional class. – 133794m3r Nov 23 '19 at 20:53
  • @133794m3r Yes, because - as we see in the proof above - if we unwind the definition of an inverse it is about congruence of certain products, and products are preserved by replacing arguments by congruent arguments (by said Congruence Product Ruke). Same for additive inverses and sums (but not for exponents - see the linked congruence rules). – Bill Dubuque Nov 23 '19 at 21:00
  • @133794m3r You might find it instructive to do the same proof for additive inverses, i.e. $,\bar a\equiv a,\Rightarrow, -\bar a\equiv -a,\ $ and a disproof for exponents $,\bar a\equiv a,\not \Rightarrow, b^{\large \bar a}\equiv b^{\large a}\ $ These are basic facts one should master to be proficient at modular arithmetic. – Bill Dubuque Nov 23 '19 at 21:10
  • The school's letout for Thanksgiving since Thursday so I couldn't talk to the one Math Professor who's currently working towards his doctorate and thus has this kind of math fresh in his head so I relented and asked you guys. I'm planning on spending my thanksgiving/christmas breaks from school to learn more math since I'll finally have a break from my Nursing classes, the courses I'm teaching, and the course curriculum I'm writing for the cybersecurity program. – 133794m3r Nov 23 '19 at 21:16
  • @133794m3r Seems like a good plan. I updated my answer to be consistent with your notation. Maybe it will be clearer now. – Bill Dubuque Nov 23 '19 at 21:25
1

Yes of course since

$$a\equiv a+ka =a' \mod k$$

we have that $a$ and $a'$ have the same unique modular inverse $a^{-1}$ when it exists, indeed

$$a^{-1}\cdot a\equiv 1\mod k \iff a^{-1}\cdot a+a^{-1}\cdot ka\equiv 1\mod k$$

$$\iff a^{-1}\cdot (a+ka)\equiv 1\mod k\iff a^{-1}\cdot a'\equiv 1\mod k$$

user
  • 154,566
  • Seeing it written like that makes it make a lot more sense to me. I feel like I should probably try to take more math classes soon. Since I'm coming at this from a "solve this problem" way I'm learning the math piecemeal in fashion. I'm writing labs for the students to teach them about cryptography and realized that negative determinants are a thing in the real-world when calculating the modular multiplicative inverse of the matrix. This is for the classic Hill Cipher btw. – 133794m3r Nov 23 '19 at 20:59
  • 1
    @133794m3r The above argument is incorrect (or incomplete) without invoking the uniqueness of inverses. It essentially repeats the proof of the Congruence Product Rule in this special case. Generally it is much better to reuse basic theorems than to repeat their proofs in special cases (such is the power of abstraction). – Bill Dubuque Nov 23 '19 at 21:02
  • @BillDubuque Yes I'm assuming the uniqueness of the inverse, I had to mention it. – user Nov 23 '19 at 21:07
  • @user Alas, your edit still fails to reach the sought conclusion. – Bill Dubuque Nov 23 '19 at 21:16