From f154ef9b3500d7186849cc9653b11a27cf83efff Mon Sep 17 00:00:00 2001 From: Alexander Rosenberg Date: Mon, 7 Oct 2024 01:59:12 -0700 Subject: [PATCH] Fix the modular-multiplicative-inverse --- hill.lisp | 7 +++++-- shift-affine.lisp | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/hill.lisp b/hill.lisp index 347b21a..10f0746 100644 --- a/hill.lisp +++ b/hill.lisp @@ -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 integer i such that (N * i) mod M = 1." (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)) (unless (= 1 gcd) (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)) ;; 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: diff --git a/shift-affine.lisp b/shift-affine.lisp index 13549e8..fde6706 100644 --- a/shift-affine.lisp +++ b/shift-affine.lisp @@ -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 integer i such that (N * i) mod M = 1." (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)) (unless (= 1 gcd) (error "N and M must be coprime. N: ~d, M: ~d" n m))