1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/*
* gcd.c
*
* This file contains the kernel functions
*
* long int gcd(long int a, long int b);
* long int euclidean_algorithm(long int m, long int n, long int *a, long int *b);
* long int Zq_inverse(long int p, long int q);
*
* gcd() returns the greatest common divisor of two long integers,
* at least one of which is nonzero.
*
* euclidean_algorithm() returns the greatest common divisor of two long
* integers m and n, and also finds long integers a and b such that
* am + bn = gcd(m,n). The integers m and n may be negative, but cannot
* both be zero.
*
* Zq_inverse() returns the inverse of p in the ring Z/q. Assumes p and q
* are relatively prime integers satisfying 0 < p < q.
*
* 97/3/31 gcd() modified to accept negative integers.
*/
#include "kernel.h"
long int gcd(
long int a,
long int b)
{
a = ABS(a);
b = ABS(b);
if (a == 0)
{
if (b == 0)
uFatalError("gcd", "gcd.c");
else
return b;
}
while (TRUE)
{
if ((b = b%a) == 0)
return a;
if ((a = a%b) == 0)
return b;
}
}
long int euclidean_algorithm(
long int m,
long int n,
long int *a,
long int *b)
{
/*
* Given two long integers m and n, use the Euclidean algorithm to
* find integers a and b such that a*m + b*n = g.c.d.(m,n).
*
* Recall the Euclidean algorithm is to keep subtracting the
* smaller of {m, n} from the larger until one of them reaches
* zero. At that point the other will equal the g.c.d.
*
* As the algorithm progresses, we'll use the coefficients
* mm, mn, nm, and nn to express the current values of m and n
* in terms of the original values:
*
* current m = mm*(original m) + mn*(original n)
* current n = nm*(original m) + nn*(original n)
*/
long int mm,
mn,
nm,
nn,
quotient;
/*
* Begin with a quick error check.
*/
if (m == 0 && n == 0)
uFatalError("euclidean_algorithm", "gcd.c");
/*
* Initially we have
*
* current m = 1 (original m) + 0 (original n)
* current n = 0 (original m) + 1 (original n)
*/
mm = nn = 1;
mn = nm = 0;
/*
* It will be convenient to work with nonnegative m and n.
*/
if (m < 0)
{
m = - m;
mm = -1;
}
if (n < 0)
{
n = - n;
nn = -1;
}
while (TRUE)
{
/*
* If m is zero, then n is the g.c.d. and we're done.
*/
if (m == 0)
{
*a = nm;
*b = nn;
return n;
}
/*
* Let n = n % m, and adjust the coefficients nm and nn accordingly.
*/
quotient = n / m;
nm -= quotient * mm;
nn -= quotient * mn;
n -= quotient * m;
/*
* If n is zero, then m is the g.c.d. and we're done.
*/
if (n == 0)
{
*a = mm;
*b = mn;
return m;
}
/*
* Let m = m % n, and adjust the coefficients mm and mn accordingly.
*/
quotient = m / n;
mm -= quotient * nm;
mn -= quotient * nn;
m -= quotient * n;
}
/*
* We never reach this point.
*/
}
long int Zq_inverse(
long int p,
long int q)
{
long int a,
b,
g;
/*
* Make sure 0 < p < q.
*/
if (p <= 0 || p >= q)
uFatalError("Zq_inverse", "gcd.c");
/*
* Find a and b such that ap + bq = gcd(p,q) = 1.
*/
g = euclidean_algorithm(p, q, &a, &b);
/*
* Check that p and q are relatively prime.
*/
if (g != 1)
uFatalError("Zq_inverse", "gcd.c");
/*
* ap + bq = 1
* => ap = 1 (mod q)
* => The inverse of p in Z/q is a.
*
* Normalize a to the range (0, q).
*
* [My guess is that a must always fall in the range -q < a < q,
* in which case the follwing code would simplify to
*
* if (a < 0)
* a += q;
*
* but I haven't worked out a proof.]
*/
while (a < 0)
a += q;
while (a > q)
a -= q;
return a;
}
|