Fix the modular-multiplicative-inverse

This commit is contained in:
Alexander Rosenberg 2024-10-07 01:59:12 -07:00
parent 1f86243917
commit f154ef9b35
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730
2 changed files with 9 additions and 3 deletions

View File

@ -145,7 +145,10 @@ return a cons of (x . y) such char x * N1 + y * N2 = GCD."
"Find the modular multiplicative inverse of N with respect to M. That is, some "Find the modular multiplicative inverse of N with respect to M. That is, some
integer i such that (N * i) mod M = 1." integer i such that (N * i) mod M = 1."
(destructuring-bind (gcd (x . y)) (destructuring-bind (gcd (x . y))
(multiple-value-list (extended-gcd n m)) (multiple-value-list (extended-gcd (if (minusp n)
(+ 26 n)
n)
m))
(declare (ignorable y)) (declare (ignorable y))
(unless (= 1 gcd) (unless (= 1 gcd)
(error "N and M must be coprime. N: ~d, M: ~d" n m)) (error "N and M must be coprime. N: ~d, M: ~d" n m))
@ -428,5 +431,5 @@ be the same value originally used to encrypt the text."
'string)) 'string))
;; Local Variables: ;; Local Variables:
;; jinx-local-words: "adjugate cofactor cofactors det plaintext prandom unnessesary xorshift" ;; jinx-local-words: "adjugate cofactor cofactors coprime det plaintext prandom unnessesary xorshift"
;; End: ;; End:

View File

@ -61,7 +61,10 @@ return a cons of (x . y) such char x * N1 + y * N2 = GCD."
"Find the modular multiplicative inverse of N with respect to M. That is, some "Find the modular multiplicative inverse of N with respect to M. That is, some
integer i such that (N * i) mod M = 1." integer i such that (N * i) mod M = 1."
(destructuring-bind (gcd (x . y)) (destructuring-bind (gcd (x . y))
(multiple-value-list (extended-gcd n m)) (multiple-value-list (extended-gcd (if (minusp n)
(+ 26 n)
n)
m))
(declare (ignorable y)) (declare (ignorable y))
(unless (= 1 gcd) (unless (= 1 gcd)
(error "N and M must be coprime. N: ~d, M: ~d" n m)) (error "N and M must be coprime. N: ~d, M: ~d" n m))