summaryrefslogtreecommitdiffstats
path: root/modular.scm
diff options
context:
space:
mode:
Diffstat (limited to 'modular.scm')
-rw-r--r--modular.scm44
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)))