projects
/
u
/
mdw
/
putty
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Make mkfiles.pl less tied to PuTTY in particular, by inventing new
[u/mdw/putty]
/
sshbn.c
diff --git
a/sshbn.c
b/sshbn.c
index
a51c3a5
..
7fe0309
100644
(file)
--- a/
sshbn.c
+++ b/
sshbn.c
@@
-15,6
+15,10
@@
typedef unsigned long long BignumDblInt;
#define BIGNUM_TOP_BIT 0x80000000UL
#define BIGNUM_INT_BITS 32
#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
#define BIGNUM_TOP_BIT 0x80000000UL
#define BIGNUM_INT_BITS 32
#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
+#define DIVMOD_WORD(q, r, hi, lo, w) \
+ __asm__("div %2" : \
+ "=d" (r), "=a" (q) : \
+ "r" (w), "d" (hi), "a" (lo))
#else
typedef unsigned short BignumInt;
typedef unsigned long BignumDblInt;
#else
typedef unsigned short BignumInt;
typedef unsigned long BignumDblInt;
@@
-22,6
+26,11
@@
typedef unsigned long BignumDblInt;
#define BIGNUM_TOP_BIT 0x8000U
#define BIGNUM_INT_BITS 16
#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
#define BIGNUM_TOP_BIT 0x8000U
#define BIGNUM_INT_BITS 16
#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
+#define DIVMOD_WORD(q, r, hi, lo, w) do { \
+ BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
+ q = n / w; \
+ r = n % w; \
+} while (0)
#endif
#define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
#endif
#define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
@@
-124,7
+133,7
@@
static void internal_add_shifted(BignumInt *number,
int bshift = shift % BIGNUM_INT_BITS;
BignumDblInt addend;
int bshift = shift % BIGNUM_INT_BITS;
BignumDblInt addend;
- addend = n << bshift;
+ addend =
(BignumDblInt)
n << bshift;
while (addend) {
addend += number[word];
while (addend) {
addend += number[word];
@@
-175,13
+184,11
@@
static void internal_mod(BignumInt *a, int alen,
ai1 = a[i + 1];
/* Find q = h:a[i] / m0 */
ai1 = a[i + 1];
/* Find q = h:a[i] / m0 */
- t = ((BignumDblInt) h << BIGNUM_INT_BITS) + a[i];
- q = t / m0;
- r = t % m0;
+ DIVMOD_WORD(q, r, h, a[i], m0);
/* Refine our estimate of q by looking at
h:a[i]:a[i+1] / m0:m1 */
/* Refine our estimate of q by looking at
h:a[i]:a[i+1] / m0:m1 */
- t =
(BignumDblInt) m1 * (BignumDblInt) q
;
+ t =
MUL_WORD(m1, q)
;
if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
q--;
t -= m1;
if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
q--;
t -= m1;
@@
-193,7
+200,7
@@
static void internal_mod(BignumInt *a, int alen,
/* Subtract q * m from a[i...] */
c = 0;
for (k = mlen - 1; k >= 0; k--) {
/* Subtract q * m from a[i...] */
c = 0;
for (k = mlen - 1; k >= 0; k--) {
- t =
(BignumDblInt) q * (BignumDblInt) m[k]
;
+ t =
MUL_WORD(q, m[k])
;
t += c;
c = t >> BIGNUM_INT_BITS;
if ((BignumInt) t > a[i + k])
t += c;
c = t >> BIGNUM_INT_BITS;
if ((BignumInt) t > a[i + k])
@@
-722,6
+729,7
@@
Bignum bigmuladd(Bignum a, Bignum b, Bignum addend)
}
ret[0] = maxspot;
}
ret[0] = maxspot;
+ sfree(workspace);
return ret;
}
return ret;
}
@@
-807,7
+815,7
@@
unsigned short bignum_mod_short(Bignum number, unsigned short modulus)
r = 0;
mod = modulus;
for (i = number[0]; i > 0; i--)
r = 0;
mod = modulus;
for (i = number[0]; i > 0; i--)
- r = (r *
65536 + number[i]
) % mod;
+ r = (r *
(BIGNUM_TOP_BIT % mod) * 2 + number[i] % mod
) % mod;
return (unsigned short) r;
}
return (unsigned short) r;
}
@@
-901,6
+909,7
@@
Bignum modinv(Bignum number, Bignum modulus)
x = bigmuladd(q, xp, t);
sign = -sign;
freebn(t);
x = bigmuladd(q, xp, t);
sign = -sign;
freebn(t);
+ freebn(q);
}
freebn(b);
}
freebn(b);
@@
-1002,5
+1011,6
@@
char *bignum_decimal(Bignum x)
/*
* Done.
*/
/*
* Done.
*/
+ sfree(workspace);
return ret;
}
return ret;
}