~mdw
/
catacomb
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
infra: Remove BRANCHES file.
[catacomb]
/
mpreduce.c
diff --git
a/mpreduce.c
b/mpreduce.c
index
7d31334
..
d99ff16
100644
(file)
--- a/
mpreduce.c
+++ b/
mpreduce.c
@@
-1,6
+1,6
@@
/* -*-c-*-
*
/* -*-c-*-
*
- * $Id
: mpreduce.c,v 1.2 2004/04/08 01:36:15 mdw Exp
$
+ * $Id$
*
* Efficient reduction modulo nice primes
*
*
* Efficient reduction modulo nice primes
*
@@
-47,12
+47,12
@@
DA_DECL(instr_v, mpreduce_instr);
* Arguments: @gfreduce *r@ = structure to fill in
* @mp *x@ = an integer
*
* Arguments: @gfreduce *r@ = structure to fill in
* @mp *x@ = an integer
*
- * Returns:
---
+ * Returns:
Zero if successful; nonzero on failure.
*
* Use: Initializes a context structure for reduction.
*/
*
* Use: Initializes a context structure for reduction.
*/
-
void
mpreduce_create(mpreduce *r, mp *p)
+
int
mpreduce_create(mpreduce *r, mp *p)
{
mpscan sc;
enum { Z = 0, Z1 = 2, X = 4, X0 = 6 };
{
mpscan sc;
enum { Z = 0, Z1 = 2, X = 4, X0 = 6 };
@@
-64,7
+64,8
@@
void mpreduce_create(mpreduce *r, mp *p)
/* --- Fill in the easy stuff --- */
/* --- Fill in the easy stuff --- */
- assert(MP_ISPOS(p));
+ if (!MP_POSP(p))
+ return (-1);
d = mp_bits(p);
r->lim = d/MPW_BITS;
r->s = d%MPW_BITS;
d = mp_bits(p);
r->lim = d/MPW_BITS;
r->s = d%MPW_BITS;
@@
-118,9
+119,9
@@
void mpreduce_create(mpreduce *r, mp *p)
}
}
if ((DA(&iv)[DA_LEN(&iv) - 1].op & ~1u) == MPRI_SUB) {
}
}
if ((DA(&iv)[DA_LEN(&iv) - 1].op & ~1u) == MPRI_SUB) {
- fprintf(stderr,
-
"mpreduce can't handle moduli of the form 2^m + x\n"
);
-
abort(
);
+ mp_drop(r->p);
+
DA_DESTROY(&iv
);
+
return (-1
);
}
#undef INSTR
}
#undef INSTR
@@
-149,7
+150,8
@@
void mpreduce_create(mpreduce *r, mp *p)
r->iv[i + r->in].argy = b;
}
}
r->iv[i + r->in].argy = b;
}
}
- DA_DESTROY(&iv);
+ DA_DESTROY(&iv);
+ return (0);
}
/* --- @mpreduce_destroy@ --- *
}
/* --- @mpreduce_destroy@ --- *
@@
-253,7
+255,7
@@
mp *mpreduce_do(mpreduce *r, mp *d, mp *x)
/* --- If source is negative, divide --- */
/* --- If source is negative, divide --- */
- if (MP_
ISNEG
(x)) {
+ if (MP_
NEGP
(x)) {
mp_div(0, &d, x, r->p);
return (d);
}
mp_div(0, &d, x, r->p);
return (d);
}
@@
-335,12
+337,18
@@
mp *mpreduce_exp(mpreduce *mr, mp *d, mp *a, mp *e)
mp *spare = (e->f & MP_BURN) ? MP_NEWSEC : MP_NEW;
MP_SHRINK(e);
mp *spare = (e->f & MP_BURN) ? MP_NEWSEC : MP_NEW;
MP_SHRINK(e);
- if (!MP_LEN(e))
+ MP_COPY(a);
+ if (MP_ZEROP(e))
;
;
- else if (MP_LEN(e) < EXP_THRESH)
- EXP_SIMPLE(x, a, e);
- else
- EXP_WINDOW(x, a, e);
+ else {
+ if (MP_NEGP(e))
+ a = mp_modinv(a, a, mr->p);
+ if (MP_LEN(e) < EXP_THRESH)
+ EXP_SIMPLE(x, a, e);
+ else
+ EXP_WINDOW(x, a, e);
+ }
+ mp_drop(a);
mp_drop(d);
mp_drop(spare);
return (x);
mp_drop(d);
mp_drop(spare);
return (x);