diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2017-02-20 00:05:38 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2017-02-20 00:05:38 -0800 |
commit | 64f037d91e0c9296dcaef9a0ff3eb33b19a2ed34 (patch) | |
tree | 1b23b8e8005328194e2fb4bf653806c85050933f /modular.scm | |
parent | 5bea21e81ed516440e34e480f2c33ca41aa8c597 (diff) | |
download | slib-64f037d91e0c9296dcaef9a0ff3eb33b19a2ed34.tar.gz slib-64f037d91e0c9296dcaef9a0ff3eb33b19a2ed34.zip |
Import Upstream version 3a5upstream/3a5
Diffstat (limited to 'modular.scm')
-rw-r--r-- | modular.scm | 44 |
1 files changed, 23 insertions, 21 deletions
diff --git a/modular.scm b/modular.scm index e77ced4..78c78bb 100644 --- a/modular.scm +++ b/modular.scm @@ -185,32 +185,34 @@ (lambda (m a b) (define a0 a) (define p 0) - (cond ((zero? m) (* a b)) ((negative? m) ;; Need algorighm to work with symmetric representation. - (modular:normalize m (* a b))) + (modular:normalize m (* (modular:normalize m a) + (modular:normalize m b)))) (else - (cond - ((< a modular:r)) - ((< b modular:r) (set! a b) (set! b a0) (set! a0 a)) - (else - (set! a0 (modulo a modular:r)) - (let ((a1 (quotient a modular:r)) - (qh (quotient m modular:r)) - (rh (modulo m modular:r))) - (cond ((>= a1 modular:r) - (set! a1 (- a1 modular:r)) - (set! p (modulo (- (* modular:r (modulo b qh)) - (* (quotient b qh) rh)) m)))) - (cond ((not (zero? a1)) - (let ((q (quotient m a1))) - (set! p (- p (* (quotient b q) (modulo m a1)))) - (set! p (modulo (+ (if (positive? p) (- p m) p) - (* a1 (modulo b q))) m))))) - (set! p (modulo (- (* modular:r (modulo p qh)) - (* (quotient p qh) rh)) m))))) + (set! a (modulo a m)) + (set! b (modulo b m)) + (set! a0 a) + (cond ((< a modular:r)) + ((< b modular:r) (set! a b) (set! b a0) (set! a0 a)) + (else + (set! a0 (modulo a modular:r)) + (let ((a1 (quotient a modular:r)) + (qh (quotient m modular:r)) + (rh (modulo m modular:r))) + (cond ((>= a1 modular:r) + (set! a1 (- a1 modular:r)) + (set! p (modulo (- (* modular:r (modulo b qh)) + (* (quotient b qh) rh)) m)))) + (cond ((not (zero? a1)) + (let ((q (quotient m a1))) + (set! p (- p (* (quotient b q) (modulo m a1)))) + (set! p (modulo (+ (if (positive? p) (- p m) p) + (* a1 (modulo b q))) m))))) + (set! p (modulo (- (* modular:r (modulo p qh)) + (* (quotient p qh) rh)) m))))) (if (zero? a0) p (let ((q (quotient m a0))) |