www.pudn.com > RSA-MD2-MD5-source_code.rar > digit.c, change:1994-03-26,size:2690b

```/* DIGIT.C - digit arithmetic routines
*/

/* Copyright (C) RSA Laboratories, a division of RSA Data Security,
*/

#include "global.h"
#include "rsaref.h"
#include "nn.h"
#include "digit.h"

/* Computes a = b * c, where b and c are digits.

Lengths: a.
*/
void NN_DigitMult (a, b, c)
NN_DIGIT a, b, c;
{
NN_DIGIT t, u;
NN_HALF_DIGIT bHigh, bLow, cHigh, cLow;

bHigh = (NN_HALF_DIGIT)HIGH_HALF (b);
bLow = (NN_HALF_DIGIT)LOW_HALF (b);
cHigh = (NN_HALF_DIGIT)HIGH_HALF (c);
cLow = (NN_HALF_DIGIT)LOW_HALF (c);

a = (NN_DIGIT)bLow * (NN_DIGIT)cLow;
t = (NN_DIGIT)bLow * (NN_DIGIT)cHigh;
u = (NN_DIGIT)bHigh * (NN_DIGIT)cLow;
a = (NN_DIGIT)bHigh * (NN_DIGIT)cHigh;

if ((t += u) < u)
a += TO_HIGH_HALF (1);
u = TO_HIGH_HALF (t);

if ((a += u) < u)
a++;
a += HIGH_HALF (t);
}

/* Sets a = b / c, where a and c are digits.

Lengths: b.
Assumes b < c and HIGH_HALF (c) > 0. For efficiency, c should be
normalized.
*/
void NN_DigitDiv (a, b, c)
NN_DIGIT *a, b, c;
{
NN_DIGIT t, u, v;
NN_HALF_DIGIT aHigh, aLow, cHigh, cLow;

cHigh = (NN_HALF_DIGIT)HIGH_HALF (c);
cLow = (NN_HALF_DIGIT)LOW_HALF (c);

t = b;
t = b;

/* Underestimate high half of quotient and subtract.
*/
if (cHigh == MAX_NN_HALF_DIGIT)
aHigh = (NN_HALF_DIGIT)HIGH_HALF (t);
else
aHigh = (NN_HALF_DIGIT)(t / (cHigh + 1));
u = (NN_DIGIT)aHigh * (NN_DIGIT)cLow;
v = (NN_DIGIT)aHigh * (NN_DIGIT)cHigh;
if ((t -= TO_HIGH_HALF (u)) > (MAX_NN_DIGIT - TO_HIGH_HALF (u)))
t--;
t -= HIGH_HALF (u);
t -= v;

/* Correct estimate.
*/
while ((t > cHigh) ||
((t == cHigh) && (t >= TO_HIGH_HALF (cLow)))) {
if ((t -= TO_HIGH_HALF (cLow)) > MAX_NN_DIGIT - TO_HIGH_HALF (cLow))
t--;
t -= cHigh;
aHigh++;
}

/* Underestimate low half of quotient and subtract.
*/
if (cHigh == MAX_NN_HALF_DIGIT)
aLow = (NN_HALF_DIGIT)LOW_HALF (t);
else
aLow =
(NN_HALF_DIGIT)((TO_HIGH_HALF (t) + HIGH_HALF (t)) / (cHigh + 1));
u = (NN_DIGIT)aLow * (NN_DIGIT)cLow;
v = (NN_DIGIT)aLow * (NN_DIGIT)cHigh;
if ((t -= u) > (MAX_NN_DIGIT - u))
t--;
if ((t -= TO_HIGH_HALF (v)) > (MAX_NN_DIGIT - TO_HIGH_HALF (v)))
t--;
t -= HIGH_HALF (v);

/* Correct estimate.
*/
while ((t > 0) || ((t == 0) && t >= c)) {
if ((t -= c) > (MAX_NN_DIGIT - c))
t--;
aLow++;
}

*a = TO_HIGH_HALF (aHigh) + aLow;
}
```