[1] | 1 | head 1.4;
|
---|
| 2 | access;
|
---|
| 3 | symbols;
|
---|
| 4 | locks; strict;
|
---|
| 5 | comment @;;; @;
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 | 1.4
|
---|
| 9 | date 2009.01.22.04.03.50; author marek; state Exp;
|
---|
| 10 | branches;
|
---|
| 11 | next 1.3;
|
---|
| 12 |
|
---|
| 13 | 1.3
|
---|
| 14 | date 2009.01.19.09.26.32; author marek; state Exp;
|
---|
| 15 | branches;
|
---|
| 16 | next 1.2;
|
---|
| 17 |
|
---|
| 18 | 1.2
|
---|
| 19 | date 2009.01.19.07.39.06; author marek; state Exp;
|
---|
| 20 | branches;
|
---|
| 21 | next 1.1;
|
---|
| 22 |
|
---|
| 23 | 1.1
|
---|
| 24 | date 2009.01.19.06.44.13; author marek; state Exp;
|
---|
| 25 | branches;
|
---|
| 26 | next ;
|
---|
| 27 |
|
---|
| 28 |
|
---|
| 29 | desc
|
---|
| 30 | @@
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 | 1.4
|
---|
| 34 | log
|
---|
| 35 | @*** empty log message ***
|
---|
| 36 | @
|
---|
| 37 | text
|
---|
| 38 | @;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: Grobner; Base: 10 -*-
|
---|
| 39 | #|
|
---|
| 40 | $Id$
|
---|
| 41 | *--------------------------------------------------------------------------*
|
---|
| 42 | | Copyright (C) 1994, Marek Rychlik (e-mail: rychlik@@math.arizona.edu) |
|
---|
| 43 | | Department of Mathematics, University of Arizona, Tucson, AZ 85721 |
|
---|
| 44 | | |
|
---|
| 45 | | Everyone is permitted to copy, distribute and modify the code in this |
|
---|
| 46 | | directory, as long as this copyright note is preserved verbatim. |
|
---|
| 47 | *--------------------------------------------------------------------------*
|
---|
| 48 | |#
|
---|
| 49 |
|
---|
| 50 | (defpackage "MODULAR"
|
---|
| 51 | (:export modular-division make-modular-division)
|
---|
| 52 | (:use "XGCD" "COMMON-LISP"))
|
---|
| 53 |
|
---|
| 54 | (in-package "MODULAR")
|
---|
| 55 |
|
---|
| 56 | #+debug(proclaim '(optimize (speed 0) (debug 3)))
|
---|
| 57 | #-debug(proclaim '(optimize (speed 3) (debug 0)))
|
---|
| 58 |
|
---|
| 59 | (defun modular-inverse (x p)
|
---|
| 60 | "Find the inverse of X modulo prime P, using Euclid algorithm."
|
---|
| 61 | (multiple-value-bind (gcd u v)
|
---|
| 62 | (xgcd x p)
|
---|
| 63 | (declare (ignore gcd v))
|
---|
| 64 | (mod u p)))
|
---|
| 65 |
|
---|
| 66 | (defun modular-division (x y p)
|
---|
| 67 | "Divide X by Y modulo prime P."
|
---|
| 68 | (mod (* x (modular-inverse y p)) p))
|
---|
| 69 |
|
---|
| 70 | (defvar *inverse-by-lookup-limit* 100000
|
---|
| 71 | "If prime modulus is < this number then the division algorithm
|
---|
| 72 | will use a lookup table of inverses created at the time when field-modulo-prime is called.")
|
---|
| 73 |
|
---|
| 74 | (defun make-inverse-table (modulus &aux (table (list 0)))
|
---|
| 75 | "Make a vector of length MODULUS containing all inverses modulo MODULUS,
|
---|
| 76 | which should be a prime number. The inverse of 0 is 0."
|
---|
| 77 | (do ((x 1 (1+ x))) ((>= x modulus) (apply #'vector (nreverse table)))
|
---|
| 78 | (push (modular-inverse x modulus) table)))
|
---|
| 79 |
|
---|
| 80 | (defun make-modular-division (modulus)
|
---|
| 81 | "Return a function of two arguments which will perform division
|
---|
| 82 | modulo MODULUS. Currently, if MODULUS is < *INVERSE-BY-LOOKUP-LIMIT*
|
---|
| 83 | then the returned function does table lookup, otherwise it uses
|
---|
| 84 | the Euclid algorithm to find the inverse."
|
---|
| 85 | (cond
|
---|
| 86 | ((>= modulus *inverse-by-lookup-limit*)
|
---|
| 87 | #'(lambda (x y) (modular-division x y modulus)))
|
---|
| 88 | (t
|
---|
| 89 | (let ((table (make-inverse-table modulus)))
|
---|
| 90 | #'(lambda (x y)
|
---|
| 91 | (mod (* x (svref table y)) modulus))))))@
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 | 1.3
|
---|
| 95 | log
|
---|
| 96 | @*** empty log message ***
|
---|
| 97 | @
|
---|
| 98 | text
|
---|
| 99 | @d19 2
|
---|
| 100 | a20 2
|
---|
| 101 | ;;(proclaim '(optimize (speed 0) (debug 3)))
|
---|
| 102 | (proclaim '(optimize (speed 3) (debug 0)))
|
---|
| 103 | @
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | 1.2
|
---|
| 107 | log
|
---|
| 108 | @*** empty log message ***
|
---|
| 109 | @
|
---|
| 110 | text
|
---|
| 111 | @d19 2
|
---|
| 112 | a20 1
|
---|
| 113 | (proclaim '(optimize (speed 0) (debug 3)))
|
---|
| 114 | @
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 | 1.1
|
---|
| 118 | log
|
---|
| 119 | @Initial revision
|
---|
| 120 | @
|
---|
| 121 | text
|
---|
| 122 | @d3 1
|
---|
| 123 | a3 1
|
---|
| 124 | $Id: modular.lisp,v 1.6 1997/12/13 15:55:32 marek Exp $
|
---|
| 125 | d19 2
|
---|
| 126 | @
|
---|