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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
| /*** bigint.c ***/
/* This is a very crude large integer package. The purpose is to teach, not to
be efficient. See freelip or miracl for very efficient code or check out the
commercial packages like Macsyma, Maple or the other similar products. The basis
of these routines come from J.W. Crenshaw and several articles in his "Programmers
Toolbox" in Embedded Systems Programming magazine from Dec. 1996 thru Sept. 1997
*/
#include <stdio.h>
#include "bigint.h"
/* clear all bits in a large integer storage block. */
void int_null( BIGINT *a)
{
INDEX i;
INTLOOP (i) a->hw[i] = 0;
}
/* copy one BIGINT block to another */
void int_copy(BIGINT *a, BIGINT *b)
{
INDEX i;
INTLOOP (i) b->hw[i] = a->hw[i];
}
/* for use in the far distant future, convert a packed field to a large
integer. Does a simple expansion. The large integer is 4 times bigger
to accomodate multiplication (once!).
*/
void field_to_int( FIELD2N *a, BIGINT *b)
{
INDEX i, j;
int_null( b);
for (i=NUMWORD; i>=0; i--)
{
j = INTMAX - ((NUMWORD - i)<<1);
b->hw[j] = a->e[i] & LOMASK;
j--;
b->hw[j] = (a->e[i] & HIMASK) >> HALFSIZE;
}
}
/* Pack a BIGINT variable back into a FIELD2N size one. */
void int_to_field( BIGINT *a, FIELD2N *b)
{
INDEX i, j;
SUMLOOP(i)
{
j = (i + MAXLONG) << 1;
b->e[i] = a->hw[j+1] | (a->hw[j] << HALFSIZE);
}
}
/* Negate a BIGINT in place. Each half word is complemented, then we add 1 */
void int_neg( BIGINT *a)
{
INDEX i;
INTLOOP(i) a->hw[i] = ~a->hw[i] & LOMASK;
INTLOOP(i)
{
a->hw[i]++;
if (a->hw[i] & LOMASK) break;
a->hw[i] &= LOMASK;
}
}
/* add two BIGINTS to get a third. c = a + b
Unlike the polynomial or ONB math, c can be one of a or b
*/
void int_add( BIGINT *a, BIGINT *b, BIGINT *c)
{
INDEX i;
ELEMENT ec;
ec = 0;
INTLOOP (i)
{
/* add previous carry bit to each term */
ec = a->hw[i] + b->hw[i] + (ec >> HALFSIZE);
c->hw[i] = ec & LOMASK;
}
}
/* subtract two BIGINTS, c = a - b == a + (-b).
as in addition, c can point to a or b and still works
*/
void int_sub( BIGINT *a, BIGINT *b, BIGINT *c)
{
BIGINT negb;
int_copy( b, &negb);
int_neg( &negb);
int_add( a, &negb, c);
}
/* multiply two BIGINTs to get a third.
Do NOT attempt to do 2 multiplies in a row without a division in between.
You may get an overflow and there is no provision in this code to return
an error condition for that. See more advanced packages for correct way
to do this. c can *not* be one of a or b, it must be a separate storage
location.
*/
void int_mul( BIGINT *a, BIGINT *b, BIGINT *c)
{
ELEMENT ea, eb, mul;
INDEX i, j, k;
BIGINT sum;
int_null(c);
for ( i = INTMAX; i > INTMAX/2; i--)
{
ea = a->hw[i];
int_null( &sum);
for ( j = INTMAX; j > INTMAX/2; j--)
{
eb = b->hw[j];
k = i + j - INTMAX;
mul = ea * eb + sum.hw[k];
sum.hw[k] = mul & LOMASK;
sum.hw[k-1] = mul >> HALFSIZE;
}
int_add( &sum, c, c);
}
}
/* unsigned divide. Input full sized numerator (top),
half sized denominator (bottom).
Output half sized quotient and half sized remainder.
Exceptionally crude but works ok for basics, error
conditions return zero results.
*/
void int_div( BIGINT *top, BIGINT *bottom, BIGINT *quotient, BIGINT *remainder)
{
BIGINT d, e;
ELEMENT mask;
INDEX l, m, n, i, j;
/* first step, initialize counters to most significant
bit position in top and bottom.
*/
int_copy( top, &d);
int_copy( bottom, &e);
l = (INTMAX + 1) * HALFSIZE;
for( i=0; i<=INTMAX; i++)
{
if (!d.hw[i]) l -= HALFSIZE;
else break;
}
mask = 1L << (HALFSIZE-1);
for ( j=0; j<HALFSIZE; j++)
{
if ( !(d.hw[i] & mask))
{
l--;
mask >>= 1;
}
else break;
}
/* same thing for bottom, compute msb position */
m = (INTMAX + 1) * HALFSIZE;
for( i=0; i<=INTMAX; i++)
{
if (!e.hw[i]) m -= HALFSIZE;
else break;
}
mask = 1L << (HALFSIZE-1);
for ( j=0; j<HALFSIZE; j++)
{
if ( !(e.hw[i] & mask))
{
m--;
mask >>= 1;
}
else break;
}
/* check for error inputs, does not check for zero, so is
actually incorrect.
*/
if (!m) /* x/1 = x */
{
int_copy( top, quotient);
int_null( remainder);
}
if (!l | (l<m)) /* 1/x = 0 */
{
int_null( quotient);
int_copy( bottom, remainder);
}
/* next step, shift bottom over to align msb with top msb */
n = l - m;
i = n;
while ( i > HALFSIZE )
{
for (j=0; j<INTMAX; j++) e.hw[j] = e.hw[j+1];
i -= HALFSIZE;
e.hw[INTMAX] = 0;
}
mask = 0;
while ( i > 0 )
{
INTLOOP (j)
{
e.hw[j] = (e.hw[j] << 1) | mask;
mask = e.hw[j] & CARRY ? 1 : 0;
e.hw[j] &= LOMASK;
}
i--;
}
/* main division loop. check to see if we can subtract shifted bottom
from what's left on top. If we can, set that bit in quotient and do
subtract. if we can't, just shift bottom right and repeat until only
remainder is left.
*/
int_null( quotient);
while ( n>=0)
{
i = INTMAX - l/HALFSIZE;
j = INTMAX - n/HALFSIZE;
while ( (d.hw[i] == e.hw[i]) && ( i<INTMAX) ) i++;
if ( d.hw[i] >= e.hw[i] )
{
int_sub( &d, &e, &d);
mask = 1L << ( n%HALFSIZE );
quotient->hw[j] |= mask;
}
INTLOOP(j)
{
if (j) mask = ( e.hw[j-1] & 1) ? CARRY : 0;
else mask = 0;
e.hw[j] = (e.hw[j] | mask) >> 1;
}
n--;
l--;
}
int_copy ( &d, remainder);
}
/* Convert ascii string of decimal digits into BIGINT binary.
Ignores out of range characters. This is very crude, 'a' = '1',
so watch out for input errors!
*/
void ascii_to_bigint( char *instring, BIGINT *outhex)
{
ELEMENT ch;
BIGINT ten, digit, temp;
INDEX i=0;
int_null( &ten); /* create decimal multiplier */
ten.hw[INTMAX] = 0xA;
int_null( &digit);
int_null( outhex);
while (ch = *instring++)
{
digit.hw[INTMAX] = ch & 0xF;
int_mul( outhex, &ten, &temp);
if (digit.hw[INTMAX] > 9) continue;
int_add( &temp, &digit, outhex);
}
}
/* Convert binary BIGINT to ascii string. Assumes destination has
enough characters to hold result. This is 4*HALFSIZE*MAXLONG bits
= Log(2)*4*HALFSIZE*MAXLONG = 1.20412*HALFSIZE*MAXLONG characters
or about 5/4*HALFSIZE*MAXLONG chars. Works backwards and blank
fills destination string.
*/
void bigint_to_ascii( BIGINT *inhex, char *outstring)
{
BIGINT top, ten, quotient, remainder;
ELEMENT check;
INDEX i;
int_copy( inhex, &top);
int_null( &ten); /* create constant 10 */
ten.hw[INTMAX] = 0xA;
for (i=0; i<MAXSTRING; i++) *outstring++ = ' '; /* blank fill and null string */
outstring--;
*outstring-- = 0;
check = 1;
while (check)
{
int_div( &top, &ten, "ient, &remainder);
*outstring-- = remainder.hw[INTMAX] | '0';
check = 0;
INTLOOP(i) check |= quotient.hw[i];
int_copy( "ient, &top);
}
} |
Partager