libmp: initial attempt at constant time code, faster reductions for special primes (for ecc)
introduce MPtimesafe flag to request time invariant computation disables normalization so significant digits are not leaked.front
parent
b677ab0c59
commit
38e1e5272f
|
@ -22,7 +22,10 @@ struct mpint
|
||||||
|
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
MPstatic= 0x01,
|
MPstatic= 0x01, /* static constant */
|
||||||
|
MPnorm= 0x02, /* normalization status */
|
||||||
|
MPtimesafe= 0x04, /* request time invariant computation */
|
||||||
|
|
||||||
Dbytes= sizeof(mpdigit), /* bytes per digit */
|
Dbytes= sizeof(mpdigit), /* bytes per digit */
|
||||||
Dbits= Dbytes*8 /* bits per digit */
|
Dbits= Dbytes*8 /* bits per digit */
|
||||||
};
|
};
|
||||||
|
@ -32,7 +35,7 @@ void mpsetminbits(int n); /* newly created mpint's get at least n bits */
|
||||||
mpint* mpnew(int n); /* create a new mpint with at least n bits */
|
mpint* mpnew(int n); /* create a new mpint with at least n bits */
|
||||||
void mpfree(mpint *b);
|
void mpfree(mpint *b);
|
||||||
void mpbits(mpint *b, int n); /* ensure that b has at least n bits */
|
void mpbits(mpint *b, int n); /* ensure that b has at least n bits */
|
||||||
void mpnorm(mpint *b); /* dump leading zeros */
|
mpint* mpnorm(mpint *b); /* dump leading zeros */
|
||||||
mpint* mpcopy(mpint *b);
|
mpint* mpcopy(mpint *b);
|
||||||
void mpassign(mpint *old, mpint *new);
|
void mpassign(mpint *old, mpint *new);
|
||||||
|
|
||||||
|
@ -47,8 +50,10 @@ int mpfmt(Fmt*);
|
||||||
char* mptoa(mpint*, int, char*, int);
|
char* mptoa(mpint*, int, char*, int);
|
||||||
mpint* letomp(uchar*, uint, mpint*); /* byte array, little-endian */
|
mpint* letomp(uchar*, uint, mpint*); /* byte array, little-endian */
|
||||||
int mptole(mpint*, uchar*, uint, uchar**);
|
int mptole(mpint*, uchar*, uint, uchar**);
|
||||||
|
void mptolel(mpint *b, uchar *p, int n);
|
||||||
mpint* betomp(uchar*, uint, mpint*); /* byte array, big-endian */
|
mpint* betomp(uchar*, uint, mpint*); /* byte array, big-endian */
|
||||||
int mptobe(mpint*, uchar*, uint, uchar**);
|
int mptobe(mpint*, uchar*, uint, uchar**);
|
||||||
|
void mptober(mpint *b, uchar *p, int n);
|
||||||
uint mptoui(mpint*); /* unsigned int */
|
uint mptoui(mpint*); /* unsigned int */
|
||||||
mpint* uitomp(uint, mpint*);
|
mpint* uitomp(uint, mpint*);
|
||||||
int mptoi(mpint*); /* int */
|
int mptoi(mpint*); /* int */
|
||||||
|
@ -71,12 +76,20 @@ void mpmul(mpint *b1, mpint *b2, mpint *prod); /* prod = b1*b2 */
|
||||||
void mpexp(mpint *b, mpint *e, mpint *m, mpint *res); /* res = b**e mod m */
|
void mpexp(mpint *b, mpint *e, mpint *m, mpint *res); /* res = b**e mod m */
|
||||||
void mpmod(mpint *b, mpint *m, mpint *remainder); /* remainder = b mod m */
|
void mpmod(mpint *b, mpint *m, mpint *remainder); /* remainder = b mod m */
|
||||||
|
|
||||||
|
/* modular arithmetic, time invariant when 0≤b1≤m-1 and 0≤b2≤m-1 */
|
||||||
|
void mpmodadd(mpint *b1, mpint *b2, mpint *m, mpint *sum); /* sum = b1+b2 % m */
|
||||||
|
void mpmodsub(mpint *b1, mpint *b2, mpint *m, mpint *diff); /* diff = b1-b2 % m */
|
||||||
|
void mpmodmul(mpint *b1, mpint *b2, mpint *m, mpint *prod); /* prod = b1*b2 % m */
|
||||||
|
|
||||||
/* quotient = dividend/divisor, remainder = dividend % divisor */
|
/* quotient = dividend/divisor, remainder = dividend % divisor */
|
||||||
void mpdiv(mpint *dividend, mpint *divisor, mpint *quotient, mpint *remainder);
|
void mpdiv(mpint *dividend, mpint *divisor, mpint *quotient, mpint *remainder);
|
||||||
|
|
||||||
/* return neg, 0, pos as b1-b2 is neg, 0, pos */
|
/* return neg, 0, pos as b1-b2 is neg, 0, pos */
|
||||||
int mpcmp(mpint *b1, mpint *b2);
|
int mpcmp(mpint *b1, mpint *b2);
|
||||||
|
|
||||||
|
/* res = s != 0 ? b1 : b2 */
|
||||||
|
void mpsel(int s, mpint *b1, mpint *b2, mpint *res);
|
||||||
|
|
||||||
/* extended gcd return d, x, and y, s.t. d = gcd(a,b) and ax+by = d */
|
/* extended gcd return d, x, and y, s.t. d = gcd(a,b) and ax+by = d */
|
||||||
void mpextendedgcd(mpint *a, mpint *b, mpint *d, mpint *x, mpint *y);
|
void mpextendedgcd(mpint *a, mpint *b, mpint *d, mpint *x, mpint *y);
|
||||||
|
|
||||||
|
@ -106,12 +119,14 @@ void mpvecdigmuladd(mpdigit *b, int n, mpdigit m, mpdigit *p);
|
||||||
/* prereq: p has room for n+1 digits */
|
/* prereq: p has room for n+1 digits */
|
||||||
int mpvecdigmulsub(mpdigit *b, int n, mpdigit m, mpdigit *p);
|
int mpvecdigmulsub(mpdigit *b, int n, mpdigit m, mpdigit *p);
|
||||||
|
|
||||||
/* p[0:alen*blen-1] = a[0:alen-1] * b[0:blen-1] */
|
/* p[0:alen+blen-1] = a[0:alen-1] * b[0:blen-1] */
|
||||||
/* prereq: alen >= blen, p has room for m*n digits */
|
/* prereq: alen >= blen, p has room for m*n digits */
|
||||||
void mpvecmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p);
|
void mpvecmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p);
|
||||||
|
void mpvectsmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p);
|
||||||
|
|
||||||
/* sign of a - b or zero if the same */
|
/* sign of a - b or zero if the same */
|
||||||
int mpveccmp(mpdigit *a, int alen, mpdigit *b, int blen);
|
int mpveccmp(mpdigit *a, int alen, mpdigit *b, int blen);
|
||||||
|
int mpvectscmp(mpdigit *a, int alen, mpdigit *b, int blen);
|
||||||
|
|
||||||
/* divide the 2 digit dividend by the one digit divisor and stick in quotient */
|
/* divide the 2 digit dividend by the one digit divisor and stick in quotient */
|
||||||
/* we assume that the result is one digit - overflow is all 1's */
|
/* we assume that the result is one digit - overflow is all 1's */
|
||||||
|
|
80
sys/man/2/mp
80
sys/man/2/mp
|
@ -1,6 +1,6 @@
|
||||||
.TH MP 2
|
.TH MP 2
|
||||||
.SH NAME
|
.SH NAME
|
||||||
mpsetminbits, mpnew, mpfree, mpbits, mpnorm, mpcopy, mpassign, mprand, mpnrand, strtomp, mpfmt,mptoa, betomp, mptobe, letomp, mptole, mptoui, uitomp, mptoi, itomp, uvtomp, mptouv, vtomp, mptov, mpdigdiv, mpadd, mpsub, mpleft, mpright, mpmul, mpexp, mpmod, mpdiv, mpcmp, mpextendedgcd, mpinvert, mpsignif, mplowbits0, mpvecdigmuladd, mpvecdigmulsub, mpvecadd, mpvecsub, mpveccmp, mpvecmul, mpmagcmp, mpmagadd, mpmagsub, crtpre, crtin, crtout, crtprefree, crtresfree \- extended precision arithmetic
|
mpsetminbits, mpnew, mpfree, mpbits, mpnorm, mpcopy, mpassign, mprand, mpnrand, strtomp, mpfmt,mptoa, betomp, mptobe, mptober, letomp, mptole, mptolel, mptoui, uitomp, mptoi, itomp, uvtomp, mptouv, vtomp, mptov, mpdigdiv, mpadd, mpsub, mpleft, mpright, mpmul, mpexp, mpmod, mpmodadd, mpmodsub, mpmodmul, mpdiv, mpcmp, mpsel, mpextendedgcd, mpinvert, mpsignif, mplowbits0, mpvecdigmuladd, mpvecdigmulsub, mpvecadd, mpvecsub, mpveccmp, mpvecmul, mpmagcmp, mpmagadd, mpmagsub, crtpre, crtin, crtout, crtprefree, crtresfree \- extended precision arithmetic
|
||||||
.SH SYNOPSIS
|
.SH SYNOPSIS
|
||||||
.B #include <u.h>
|
.B #include <u.h>
|
||||||
.br
|
.br
|
||||||
|
@ -22,7 +22,7 @@ void mpsetminbits(int n)
|
||||||
void mpbits(mpint *b, int n)
|
void mpbits(mpint *b, int n)
|
||||||
.PP
|
.PP
|
||||||
.B
|
.B
|
||||||
void mpnorm(mpint *b)
|
mpint* mpnorm(mpint *b)
|
||||||
.PP
|
.PP
|
||||||
.B
|
.B
|
||||||
mpint* mpcopy(mpint *b)
|
mpint* mpcopy(mpint *b)
|
||||||
|
@ -52,12 +52,18 @@ mpint* betomp(uchar *buf, uint blen, mpint *b)
|
||||||
int mptobe(mpint *b, uchar *buf, uint blen, uchar **bufp)
|
int mptobe(mpint *b, uchar *buf, uint blen, uchar **bufp)
|
||||||
.PP
|
.PP
|
||||||
.B
|
.B
|
||||||
|
void mptober(mpint *b, uchar *buf, int blen)
|
||||||
|
.PP
|
||||||
|
.B
|
||||||
mpint* letomp(uchar *buf, uint blen, mpint *b)
|
mpint* letomp(uchar *buf, uint blen, mpint *b)
|
||||||
.PP
|
.PP
|
||||||
.B
|
.B
|
||||||
int mptole(mpint *b, uchar *buf, uint blen, uchar **bufp)
|
int mptole(mpint *b, uchar *buf, uint blen, uchar **bufp)
|
||||||
.PP
|
.PP
|
||||||
.B
|
.B
|
||||||
|
void mptolel(mpint *b, uchar *buf, int blen)
|
||||||
|
.PP
|
||||||
|
.B
|
||||||
uint mptoui(mpint*)
|
uint mptoui(mpint*)
|
||||||
.PP
|
.PP
|
||||||
.B
|
.B
|
||||||
|
@ -115,12 +121,24 @@ void mpdiv(mpint *dividend, mpint *divisor, mpint *quotient,
|
||||||
mpint *remainder)
|
mpint *remainder)
|
||||||
.PP
|
.PP
|
||||||
.B
|
.B
|
||||||
|
void mpmodadd(mpint *b1, mpint *b2, mpint *m, mpint *sum)
|
||||||
|
.PP
|
||||||
|
.B
|
||||||
|
void mpmodsub(mpint *b1, mpint *b2, mpint *m, mpint *diff)
|
||||||
|
.PP
|
||||||
|
.B
|
||||||
|
void mpmodmul(mpint *b1, mpint *b2, mpint *m, mpint *prod)
|
||||||
|
.PP
|
||||||
|
.B
|
||||||
int mpcmp(mpint *b1, mpint *b2)
|
int mpcmp(mpint *b1, mpint *b2)
|
||||||
.PP
|
.PP
|
||||||
.B
|
.B
|
||||||
int mpmagcmp(mpint *b1, mpint *b2)
|
int mpmagcmp(mpint *b1, mpint *b2)
|
||||||
.PP
|
.PP
|
||||||
.B
|
.B
|
||||||
|
void mpsel(int s, mpint *b1, mpint *b2, mpint *res)
|
||||||
|
.PP
|
||||||
|
.B
|
||||||
void mpextendedgcd(mpint *a, mpint *b, mpint *d, mpint *x,
|
void mpextendedgcd(mpint *a, mpint *b, mpint *d, mpint *x,
|
||||||
.br
|
.br
|
||||||
.B
|
.B
|
||||||
|
@ -383,6 +401,24 @@ deposited in the location pointed to by
|
||||||
Sign is ignored in these conversions, i.e., the byte
|
Sign is ignored in these conversions, i.e., the byte
|
||||||
array version is always positive.
|
array version is always positive.
|
||||||
.PP
|
.PP
|
||||||
|
.I Mptober
|
||||||
|
and
|
||||||
|
.I mptolel
|
||||||
|
fill
|
||||||
|
.I blen
|
||||||
|
lower bytes of an
|
||||||
|
.I mpint
|
||||||
|
into a fixed length byte array.
|
||||||
|
.I Mptober
|
||||||
|
fills the bytes right adjusted in big endian order so that the least
|
||||||
|
significant byte is at
|
||||||
|
.I buf[blen-1]
|
||||||
|
while
|
||||||
|
.I mptolel
|
||||||
|
fills in little endian order; left adjusted; so that the least
|
||||||
|
significat byte is filled into
|
||||||
|
.IR buf[0] .
|
||||||
|
.PP
|
||||||
.IR Betomp ,
|
.IR Betomp ,
|
||||||
and
|
and
|
||||||
.I letomp
|
.I letomp
|
||||||
|
@ -486,6 +522,31 @@ is less than, equal to, or greater than
|
||||||
the same as
|
the same as
|
||||||
.I mpcmp
|
.I mpcmp
|
||||||
but ignores the sign and just compares magnitudes.
|
but ignores the sign and just compares magnitudes.
|
||||||
|
.TP
|
||||||
|
.I mpsel
|
||||||
|
assigns
|
||||||
|
.I b1
|
||||||
|
to
|
||||||
|
.I res
|
||||||
|
when
|
||||||
|
.I s
|
||||||
|
is not zero, otherwise
|
||||||
|
.I b2
|
||||||
|
is assigned to
|
||||||
|
.IR res .
|
||||||
|
.PD
|
||||||
|
.PP
|
||||||
|
Modular arithmetic:
|
||||||
|
.TF mpmodmul_
|
||||||
|
.TP
|
||||||
|
.I mpmodadd
|
||||||
|
.BR "sum = b1+b2 mod m" .
|
||||||
|
.TP
|
||||||
|
.I mpmodsub
|
||||||
|
.BR "diff = b1-b2 mod m" .
|
||||||
|
.TP
|
||||||
|
.I mpmodmul
|
||||||
|
.BR "prod = b1*b2 mod m" .
|
||||||
.PD
|
.PD
|
||||||
.PP
|
.PP
|
||||||
.I Mpextendedgcd
|
.I Mpextendedgcd
|
||||||
|
@ -564,8 +625,8 @@ We assume p has room for n+1 digits. It returns +1 is the result is positive an
|
||||||
-1 if negative.
|
-1 if negative.
|
||||||
.TP
|
.TP
|
||||||
.I mpvecmul
|
.I mpvecmul
|
||||||
.BR "p[0:alen*blen] = a[0:alen-1] * b[0:blen-1]" .
|
.BR "p[0:alen+blen] = a[0:alen-1] * b[0:blen-1]" .
|
||||||
We assume that p has room for alen*blen+1 digits.
|
We assume that p has room for alen+blen+1 digits.
|
||||||
.TP
|
.TP
|
||||||
.I mpveccmp
|
.I mpveccmp
|
||||||
This returns -1, 0, or +1 as a - b is negative, 0, or positive.
|
This returns -1, 0, or +1 as a - b is negative, 0, or positive.
|
||||||
|
@ -576,6 +637,17 @@ This returns -1, 0, or +1 as a - b is negative, 0, or positive.
|
||||||
and
|
and
|
||||||
.I mpzero
|
.I mpzero
|
||||||
are the constants 2, 1 and 0. These cannot be freed.
|
are the constants 2, 1 and 0. These cannot be freed.
|
||||||
|
.SS "Time invariant computation"
|
||||||
|
.PP
|
||||||
|
In the field of cryptography, it is sometimes neccesary to implement
|
||||||
|
algorithms such that the runtime of the algorithm is not depdenent on
|
||||||
|
the input data. This library provides partial support for time
|
||||||
|
invariant computation with the
|
||||||
|
.I MPtimesafe
|
||||||
|
flag that can be set on input or destination operands to request timing
|
||||||
|
safe operation. The result of a timing safe operation will also have the
|
||||||
|
.I MPtimesafe
|
||||||
|
flag set and is not normalized.
|
||||||
.SS "Chinese remainder theorem
|
.SS "Chinese remainder theorem
|
||||||
.PP
|
.PP
|
||||||
When computing in a non-prime modulus,
|
When computing in a non-prime modulus,
|
||||||
|
|
|
@ -13,19 +13,12 @@ betomp(uchar *p, uint n, mpint *b)
|
||||||
b = mpnew(0);
|
b = mpnew(0);
|
||||||
setmalloctag(b, getcallerpc(&p));
|
setmalloctag(b, getcallerpc(&p));
|
||||||
}
|
}
|
||||||
|
|
||||||
// dump leading zeros
|
|
||||||
while(*p == 0 && n > 1){
|
|
||||||
p++;
|
|
||||||
n--;
|
|
||||||
}
|
|
||||||
|
|
||||||
// get the space
|
|
||||||
mpbits(b, n*8);
|
mpbits(b, n*8);
|
||||||
b->top = DIGITS(n*8);
|
|
||||||
m = b->top-1;
|
|
||||||
|
|
||||||
// first digit might not be Dbytes long
|
m = DIGITS(n*8);
|
||||||
|
b->top = m--;
|
||||||
|
b->sign = 1;
|
||||||
|
|
||||||
s = ((n-1)*8)%Dbits;
|
s = ((n-1)*8)%Dbits;
|
||||||
x = 0;
|
x = 0;
|
||||||
for(; n > 0; n--){
|
for(; n > 0; n--){
|
||||||
|
@ -37,6 +30,5 @@ betomp(uchar *p, uint n, mpint *b)
|
||||||
x = 0;
|
x = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return mpnorm(b);
|
||||||
return b;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,8 +9,10 @@ letomp(uchar *s, uint n, mpint *b)
|
||||||
int i=0, m = 0;
|
int i=0, m = 0;
|
||||||
mpdigit x=0;
|
mpdigit x=0;
|
||||||
|
|
||||||
if(b == nil)
|
if(b == nil){
|
||||||
b = mpnew(0);
|
b = mpnew(0);
|
||||||
|
setmalloctag(b, getcallerpc(&s));
|
||||||
|
}
|
||||||
mpbits(b, 8*n);
|
mpbits(b, 8*n);
|
||||||
for(; n > 0; n--){
|
for(; n > 0; n--){
|
||||||
x |= ((mpdigit)(*s++)) << i;
|
x |= ((mpdigit)(*s++)) << i;
|
||||||
|
@ -24,5 +26,6 @@ letomp(uchar *s, uint n, mpint *b)
|
||||||
if(i > 0)
|
if(i > 0)
|
||||||
b->p[m++] = x;
|
b->p[m++] = x;
|
||||||
b->top = m;
|
b->top = m;
|
||||||
return b;
|
b->sign = 1;
|
||||||
|
return mpnorm(b);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,12 +6,15 @@ FILES=\
|
||||||
mpfmt\
|
mpfmt\
|
||||||
strtomp\
|
strtomp\
|
||||||
mptobe\
|
mptobe\
|
||||||
|
mptober\
|
||||||
mptole\
|
mptole\
|
||||||
|
mptolel\
|
||||||
betomp\
|
betomp\
|
||||||
letomp\
|
letomp\
|
||||||
mpadd\
|
mpadd\
|
||||||
mpsub\
|
mpsub\
|
||||||
mpcmp\
|
mpcmp\
|
||||||
|
mpsel\
|
||||||
mpfactorial\
|
mpfactorial\
|
||||||
mpmul\
|
mpmul\
|
||||||
mpleft\
|
mpleft\
|
||||||
|
@ -20,10 +23,12 @@ FILES=\
|
||||||
mpvecsub\
|
mpvecsub\
|
||||||
mpvecdigmuladd\
|
mpvecdigmuladd\
|
||||||
mpveccmp\
|
mpveccmp\
|
||||||
|
mpvectscmp\
|
||||||
mpdigdiv\
|
mpdigdiv\
|
||||||
mpdiv\
|
mpdiv\
|
||||||
mpexp\
|
mpexp\
|
||||||
mpmod\
|
mpmod\
|
||||||
|
mpmodop\
|
||||||
mpextendedgcd\
|
mpextendedgcd\
|
||||||
mpinvert\
|
mpinvert\
|
||||||
mprand\
|
mprand\
|
||||||
|
|
|
@ -9,6 +9,8 @@ mpmagadd(mpint *b1, mpint *b2, mpint *sum)
|
||||||
int m, n;
|
int m, n;
|
||||||
mpint *t;
|
mpint *t;
|
||||||
|
|
||||||
|
sum->flags |= (b1->flags | b2->flags) & MPtimesafe;
|
||||||
|
|
||||||
// get the sizes right
|
// get the sizes right
|
||||||
if(b2->top > b1->top){
|
if(b2->top > b1->top){
|
||||||
t = b1;
|
t = b1;
|
||||||
|
@ -41,6 +43,7 @@ mpadd(mpint *b1, mpint *b2, mpint *sum)
|
||||||
int sign;
|
int sign;
|
||||||
|
|
||||||
if(b1->sign != b2->sign){
|
if(b1->sign != b2->sign){
|
||||||
|
assert(((b1->flags | b2->flags | sum->flags) & MPtimesafe) == 0);
|
||||||
if(b1->sign < 0)
|
if(b1->sign < 0)
|
||||||
mpmagsub(b2, b1, sum);
|
mpmagsub(b2, b1, sum);
|
||||||
else
|
else
|
||||||
|
|
|
@ -5,33 +5,27 @@
|
||||||
static mpdigit _mptwodata[1] = { 2 };
|
static mpdigit _mptwodata[1] = { 2 };
|
||||||
static mpint _mptwo =
|
static mpint _mptwo =
|
||||||
{
|
{
|
||||||
1,
|
1, 1, 1,
|
||||||
1,
|
|
||||||
1,
|
|
||||||
_mptwodata,
|
_mptwodata,
|
||||||
MPstatic
|
MPstatic|MPnorm
|
||||||
};
|
};
|
||||||
mpint *mptwo = &_mptwo;
|
mpint *mptwo = &_mptwo;
|
||||||
|
|
||||||
static mpdigit _mponedata[1] = { 1 };
|
static mpdigit _mponedata[1] = { 1 };
|
||||||
static mpint _mpone =
|
static mpint _mpone =
|
||||||
{
|
{
|
||||||
1,
|
1, 1, 1,
|
||||||
1,
|
|
||||||
1,
|
|
||||||
_mponedata,
|
_mponedata,
|
||||||
MPstatic
|
MPstatic|MPnorm
|
||||||
};
|
};
|
||||||
mpint *mpone = &_mpone;
|
mpint *mpone = &_mpone;
|
||||||
|
|
||||||
static mpdigit _mpzerodata[1] = { 0 };
|
static mpdigit _mpzerodata[1] = { 0 };
|
||||||
static mpint _mpzero =
|
static mpint _mpzero =
|
||||||
{
|
{
|
||||||
1,
|
1, 1, 0,
|
||||||
1,
|
|
||||||
0,
|
|
||||||
_mpzerodata,
|
_mpzerodata,
|
||||||
MPstatic
|
MPstatic|MPnorm
|
||||||
};
|
};
|
||||||
mpint *mpzero = &_mpzero;
|
mpint *mpzero = &_mpzero;
|
||||||
|
|
||||||
|
@ -57,18 +51,17 @@ mpnew(int n)
|
||||||
if(n < 0)
|
if(n < 0)
|
||||||
sysfatal("mpsetminbits: n < 0");
|
sysfatal("mpsetminbits: n < 0");
|
||||||
|
|
||||||
b = mallocz(sizeof(mpint), 1);
|
|
||||||
setmalloctag(b, getcallerpc(&n));
|
|
||||||
if(b == nil)
|
|
||||||
sysfatal("mpnew: %r");
|
|
||||||
n = DIGITS(n);
|
n = DIGITS(n);
|
||||||
if(n < mpmindigits)
|
if(n < mpmindigits)
|
||||||
n = mpmindigits;
|
n = mpmindigits;
|
||||||
b->p = (mpdigit*)mallocz(n*Dbytes, 1);
|
b = mallocz(sizeof(mpint) + n*Dbytes, 1);
|
||||||
if(b->p == nil)
|
if(b == nil)
|
||||||
sysfatal("mpnew: %r");
|
sysfatal("mpnew: %r");
|
||||||
|
setmalloctag(b, getcallerpc(&n));
|
||||||
|
b->p = (mpdigit*)&b[1];
|
||||||
b->size = n;
|
b->size = n;
|
||||||
b->sign = 1;
|
b->sign = 1;
|
||||||
|
b->flags = MPnorm;
|
||||||
|
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
@ -83,16 +76,23 @@ mpbits(mpint *b, int m)
|
||||||
if(b->size >= n){
|
if(b->size >= n){
|
||||||
if(b->top >= n)
|
if(b->top >= n)
|
||||||
return;
|
return;
|
||||||
memset(&b->p[b->top], 0, Dbytes*(n - b->top));
|
} else {
|
||||||
b->top = n;
|
if(b->p == (mpdigit*)&b[1]){
|
||||||
return;
|
b->p = (mpdigit*)mallocz(n*Dbytes, 0);
|
||||||
|
if(b->p == nil)
|
||||||
|
sysfatal("mpbits: %r");
|
||||||
|
memmove(b->p, &b[1], Dbytes*b->top);
|
||||||
|
memset(&b[1], 0, Dbytes*b->size);
|
||||||
|
} else {
|
||||||
|
b->p = (mpdigit*)realloc(b->p, n*Dbytes);
|
||||||
|
if(b->p == nil)
|
||||||
|
sysfatal("mpbits: %r");
|
||||||
|
}
|
||||||
|
b->size = n;
|
||||||
}
|
}
|
||||||
b->p = (mpdigit*)realloc(b->p, n*Dbytes);
|
|
||||||
if(b->p == nil)
|
|
||||||
sysfatal("mpbits: %r");
|
|
||||||
memset(&b->p[b->top], 0, Dbytes*(n - b->top));
|
memset(&b->p[b->top], 0, Dbytes*(n - b->top));
|
||||||
b->size = n;
|
|
||||||
b->top = n;
|
b->top = n;
|
||||||
|
b->flags &= ~MPnorm;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -102,22 +102,30 @@ mpfree(mpint *b)
|
||||||
return;
|
return;
|
||||||
if(b->flags & MPstatic)
|
if(b->flags & MPstatic)
|
||||||
sysfatal("freeing mp constant");
|
sysfatal("freeing mp constant");
|
||||||
memset(b->p, 0, b->size*Dbytes); // information hiding
|
memset(b->p, 0, b->size*Dbytes);
|
||||||
free(b->p);
|
if(b->p != (mpdigit*)&b[1])
|
||||||
|
free(b->p);
|
||||||
free(b);
|
free(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
mpint*
|
||||||
mpnorm(mpint *b)
|
mpnorm(mpint *b)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
if(b->flags & MPtimesafe){
|
||||||
|
assert(b->sign == 1);
|
||||||
|
b->flags &= ~MPnorm;
|
||||||
|
return b;
|
||||||
|
}
|
||||||
for(i = b->top-1; i >= 0; i--)
|
for(i = b->top-1; i >= 0; i--)
|
||||||
if(b->p[i] != 0)
|
if(b->p[i] != 0)
|
||||||
break;
|
break;
|
||||||
b->top = i+1;
|
b->top = i+1;
|
||||||
if(b->top == 0)
|
if(b->top == 0)
|
||||||
b->sign = 1;
|
b->sign = 1;
|
||||||
|
b->flags |= MPnorm;
|
||||||
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
mpint*
|
mpint*
|
||||||
|
@ -126,8 +134,10 @@ mpcopy(mpint *old)
|
||||||
mpint *new;
|
mpint *new;
|
||||||
|
|
||||||
new = mpnew(Dbits*old->size);
|
new = mpnew(Dbits*old->size);
|
||||||
new->top = old->top;
|
setmalloctag(new, getcallerpc(&old));
|
||||||
new->sign = old->sign;
|
new->sign = old->sign;
|
||||||
|
new->top = old->top;
|
||||||
|
new->flags = old->flags & ~MPstatic;
|
||||||
memmove(new->p, old->p, Dbytes*old->top);
|
memmove(new->p, old->p, Dbytes*old->top);
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
|
@ -135,9 +145,14 @@ mpcopy(mpint *old)
|
||||||
void
|
void
|
||||||
mpassign(mpint *old, mpint *new)
|
mpassign(mpint *old, mpint *new)
|
||||||
{
|
{
|
||||||
|
if(new == nil || old == new)
|
||||||
|
return;
|
||||||
|
new->top = 0;
|
||||||
mpbits(new, Dbits*old->top);
|
mpbits(new, Dbits*old->top);
|
||||||
new->sign = old->sign;
|
new->sign = old->sign;
|
||||||
new->top = old->top;
|
new->top = old->top;
|
||||||
|
new->flags &= ~MPnorm;
|
||||||
|
new->flags |= old->flags & ~MPstatic;
|
||||||
memmove(new->p, old->p, Dbytes*old->top);
|
memmove(new->p, old->p, Dbytes*old->top);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,6 +182,7 @@ mplowbits0(mpint *n)
|
||||||
int k, bit, digit;
|
int k, bit, digit;
|
||||||
mpdigit d;
|
mpdigit d;
|
||||||
|
|
||||||
|
assert(n->flags & MPnorm);
|
||||||
if(n->top==0)
|
if(n->top==0)
|
||||||
return 0;
|
return 0;
|
||||||
k = 0;
|
k = 0;
|
||||||
|
@ -187,4 +203,3 @@ mplowbits0(mpint *n)
|
||||||
}
|
}
|
||||||
return k;
|
return k;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,10 +8,14 @@ mpmagcmp(mpint *b1, mpint *b2)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = b1->top - b2->top;
|
i = b1->flags | b2->flags;
|
||||||
if(i)
|
if(i & MPtimesafe)
|
||||||
return i;
|
return mpvectscmp(b1->p, b1->top, b2->p, b2->top);
|
||||||
|
if(i & MPnorm){
|
||||||
|
i = b1->top - b2->top;
|
||||||
|
if(i)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
return mpveccmp(b1->p, b1->top, b2->p, b2->top);
|
return mpveccmp(b1->p, b1->top, b2->p, b2->top);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,10 +23,8 @@ mpmagcmp(mpint *b1, mpint *b2)
|
||||||
int
|
int
|
||||||
mpcmp(mpint *b1, mpint *b2)
|
mpcmp(mpint *b1, mpint *b2)
|
||||||
{
|
{
|
||||||
if(b1->sign != b2->sign)
|
int sign;
|
||||||
return b1->sign - b2->sign;
|
|
||||||
if(b1->sign < 0)
|
sign = (b1->sign - b2->sign) >> 1; // -1, 0, 1
|
||||||
return mpmagcmp(b2, b1);
|
return sign | (sign&1)-1 & mpmagcmp(b1, b2)*b1->sign;
|
||||||
else
|
|
||||||
return mpmagcmp(b1, b2);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,10 +13,29 @@ mpdiv(mpint *dividend, mpint *divisor, mpint *quotient, mpint *remainder)
|
||||||
mpdigit qd, *up, *vp, *qp;
|
mpdigit qd, *up, *vp, *qp;
|
||||||
mpint *u, *v, *t;
|
mpint *u, *v, *t;
|
||||||
|
|
||||||
|
assert(quotient != remainder);
|
||||||
|
assert(divisor->flags & MPnorm);
|
||||||
|
|
||||||
// divide bv zero
|
// divide bv zero
|
||||||
if(divisor->top == 0)
|
if(divisor->top == 0)
|
||||||
abort();
|
abort();
|
||||||
|
|
||||||
|
// division by one or small powers of two
|
||||||
|
if(divisor->top == 1 && (divisor->p[0] & divisor->p[0]-1) == 0){
|
||||||
|
vlong r = (vlong)dividend->sign * (dividend->p[0] & divisor->p[0]-1);
|
||||||
|
if(quotient != nil){
|
||||||
|
for(s = 0; ((divisor->p[0] >> s) & 1) == 0; s++)
|
||||||
|
;
|
||||||
|
mpright(dividend, s, quotient);
|
||||||
|
}
|
||||||
|
if(remainder != nil){
|
||||||
|
remainder->flags |= dividend->flags & MPtimesafe;
|
||||||
|
vtomp(r, remainder);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
assert((dividend->flags & MPtimesafe) == 0);
|
||||||
|
|
||||||
// quick check
|
// quick check
|
||||||
if(mpmagcmp(dividend, divisor) < 0){
|
if(mpmagcmp(dividend, divisor) < 0){
|
||||||
if(remainder != nil)
|
if(remainder != nil)
|
||||||
|
@ -95,12 +114,14 @@ mpdiv(mpint *dividend, mpint *divisor, mpint *quotient, mpint *remainder)
|
||||||
*up-- = 0;
|
*up-- = 0;
|
||||||
}
|
}
|
||||||
if(qp != nil){
|
if(qp != nil){
|
||||||
|
assert((quotient->flags & MPtimesafe) == 0);
|
||||||
mpnorm(quotient);
|
mpnorm(quotient);
|
||||||
if(dividend->sign != divisor->sign)
|
if(dividend->sign != divisor->sign)
|
||||||
quotient->sign = -1;
|
quotient->sign = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(remainder != nil){
|
if(remainder != nil){
|
||||||
|
assert((remainder->flags & MPtimesafe) == 0);
|
||||||
mpright(u, s, remainder); // u is the remainder shifted
|
mpright(u, s, remainder); // u is the remainder shifted
|
||||||
remainder->sign = dividend->sign;
|
remainder->sign = dividend->sign;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,9 @@ mpeuclid(mpint *a, mpint *b, mpint *d, mpint *x, mpint *y)
|
||||||
{
|
{
|
||||||
mpint *tmp, *x0, *x1, *x2, *y0, *y1, *y2, *q, *r;
|
mpint *tmp, *x0, *x1, *x2, *y0, *y1, *y2, *q, *r;
|
||||||
|
|
||||||
|
assert((a->flags&b->flags) & MPnorm);
|
||||||
|
assert(((a->flags|b->flags|d->flags|x->flags|y->flags) & MPtimesafe) == 0);
|
||||||
|
|
||||||
if(a->sign<0 || b->sign<0)
|
if(a->sign<0 || b->sign<0)
|
||||||
sysfatal("mpeuclid: negative arg");
|
sysfatal("mpeuclid: negative arg");
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,10 @@ mpexp(mpint *b, mpint *e, mpint *m, mpint *res)
|
||||||
mpdigit d, bit;
|
mpdigit d, bit;
|
||||||
int i, j;
|
int i, j;
|
||||||
|
|
||||||
|
assert(m->flags & MPnorm);
|
||||||
|
assert((e->flags & MPtimesafe) == 0);
|
||||||
|
res->flags |= b->flags & MPtimesafe;
|
||||||
|
|
||||||
i = mpcmp(e,mpzero);
|
i = mpcmp(e,mpzero);
|
||||||
if(i==0){
|
if(i==0){
|
||||||
mpassign(mpone, res);
|
mpassign(mpone, res);
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
// extended binary gcd
|
// extended binary gcd
|
||||||
//
|
//
|
||||||
// For a anv b it solves, v = gcd(a,b) and finds x and y s.t.
|
// For a and b it solves, v = gcd(a,b) and finds x and y s.t.
|
||||||
// ax + by = v
|
// ax + by = v
|
||||||
//
|
//
|
||||||
// Handbook of Applied Cryptography, Menezes et al, 1997, pg 608.
|
// Handbook of Applied Cryptography, Menezes et al, 1997, pg 608.
|
||||||
|
@ -15,6 +15,9 @@ mpextendedgcd(mpint *a, mpint *b, mpint *v, mpint *x, mpint *y)
|
||||||
mpint *u, *A, *B, *C, *D;
|
mpint *u, *A, *B, *C, *D;
|
||||||
int g;
|
int g;
|
||||||
|
|
||||||
|
assert((a->flags&b->flags) & MPnorm);
|
||||||
|
assert(((a->flags|b->flags|v->flags|x->flags|y->flags) & MPtimesafe) == 0);
|
||||||
|
|
||||||
if(a->sign < 0 || b->sign < 0){
|
if(a->sign < 0 || b->sign < 0){
|
||||||
mpassign(mpzero, v);
|
mpassign(mpzero, v);
|
||||||
mpassign(mpzero, y);
|
mpassign(mpzero, y);
|
||||||
|
|
|
@ -102,6 +102,7 @@ to10(mpint *b, char *buf, int len)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
d = mpcopy(b);
|
d = mpcopy(b);
|
||||||
|
mpnorm(d);
|
||||||
r = mpnew(0);
|
r = mpnew(0);
|
||||||
billion = uitomp(1000000000, nil);
|
billion = uitomp(1000000000, nil);
|
||||||
out = buf+len;
|
out = buf+len;
|
||||||
|
@ -128,15 +129,20 @@ int
|
||||||
mpfmt(Fmt *fmt)
|
mpfmt(Fmt *fmt)
|
||||||
{
|
{
|
||||||
mpint *b;
|
mpint *b;
|
||||||
char *p;
|
char *p, f;
|
||||||
|
|
||||||
b = va_arg(fmt->args, mpint*);
|
b = va_arg(fmt->args, mpint*);
|
||||||
if(b == nil)
|
if(b == nil)
|
||||||
return fmtstrcpy(fmt, "*");
|
return fmtstrcpy(fmt, "*");
|
||||||
|
|
||||||
|
f = b->flags;
|
||||||
|
b->flags &= ~MPtimesafe;
|
||||||
|
|
||||||
p = mptoa(b, fmt->prec, nil, 0);
|
p = mptoa(b, fmt->prec, nil, 0);
|
||||||
fmt->flags &= ~FmtPrec;
|
fmt->flags &= ~FmtPrec;
|
||||||
|
|
||||||
|
b->flags = f;
|
||||||
|
|
||||||
if(p == nil)
|
if(p == nil)
|
||||||
return fmtstrcpy(fmt, "*");
|
return fmtstrcpy(fmt, "*");
|
||||||
else{
|
else{
|
||||||
|
|
|
@ -15,8 +15,8 @@ mpleft(mpint *b, int shift, mpint *res)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// a negative left shift is a right shift
|
// a zero or negative left shift is a right shift
|
||||||
if(shift < 0){
|
if(shift <= 0){
|
||||||
mpright(b, -shift, res);
|
mpright(b, -shift, res);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,6 @@ mpleft(mpint *b, int shift, mpint *res)
|
||||||
for(i = 0; i < d; i++)
|
for(i = 0; i < d; i++)
|
||||||
res->p[i] = 0;
|
res->p[i] = 0;
|
||||||
|
|
||||||
// normalize
|
res->flags |= b->flags & MPtimesafe;
|
||||||
while(res->top > 0 && res->p[res->top-1] == 0)
|
mpnorm(res);
|
||||||
res->top--;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,14 +2,100 @@
|
||||||
#include <mp.h>
|
#include <mp.h>
|
||||||
#include "dat.h"
|
#include "dat.h"
|
||||||
|
|
||||||
// remainder = b mod m
|
|
||||||
//
|
|
||||||
// knuth, vol 2, pp 398-400
|
|
||||||
|
|
||||||
void
|
void
|
||||||
mpmod(mpint *b, mpint *m, mpint *remainder)
|
mpmod(mpint *x, mpint *n, mpint *r)
|
||||||
{
|
{
|
||||||
mpdiv(b, m, nil, remainder);
|
static int busy;
|
||||||
if(remainder->sign < 0)
|
static mpint *p, *m, *c, *v;
|
||||||
mpadd(m, remainder, remainder);
|
mpdigit q[32], t[64], d;
|
||||||
|
int sign, k, s, qn, tn;
|
||||||
|
|
||||||
|
sign = x->sign;
|
||||||
|
|
||||||
|
assert(n->flags & MPnorm);
|
||||||
|
if(n->top < 2 || n->top > nelem(q) || (x->top-n->top) > nelem(q))
|
||||||
|
goto hard;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* check if n = 2**k - c where c has few power of two factors
|
||||||
|
* above the lowest digit.
|
||||||
|
*/
|
||||||
|
for(k = n->top-1; k > 0; k--){
|
||||||
|
d = n->p[k] >> 1;
|
||||||
|
if((d+1 & d) != 0)
|
||||||
|
goto hard;
|
||||||
|
}
|
||||||
|
|
||||||
|
d = n->p[n->top-1];
|
||||||
|
for(s = 0; (d & (mpdigit)1<<Dbits-1) == 0; s++)
|
||||||
|
d <<= 1;
|
||||||
|
|
||||||
|
/* lo(x) = x[0:k-1], hi(x) = x[k:xn-1] */
|
||||||
|
k = n->top;
|
||||||
|
|
||||||
|
while(_tas(&busy))
|
||||||
|
;
|
||||||
|
|
||||||
|
if(p == nil || mpmagcmp(n, p) != 0){
|
||||||
|
if(m == nil){
|
||||||
|
m = mpnew(0);
|
||||||
|
c = mpnew(0);
|
||||||
|
p = mpnew(0);
|
||||||
|
}
|
||||||
|
mpassign(n, p);
|
||||||
|
|
||||||
|
mpleft(n, s, m);
|
||||||
|
mpleft(mpone, k*Dbits, c);
|
||||||
|
mpsub(c, m, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
mpleft(x, s, r);
|
||||||
|
if(r->top <= k){
|
||||||
|
mpbits(r, (k+1)*Dbits);
|
||||||
|
r->top = k+1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* q = hi(r) */
|
||||||
|
qn = r->top - k;
|
||||||
|
memmove(q, r->p+k, qn*Dbytes);
|
||||||
|
|
||||||
|
/* r = lo(r) */
|
||||||
|
r->top = k;
|
||||||
|
|
||||||
|
do {
|
||||||
|
/* t = q*c */
|
||||||
|
tn = qn + c->top;
|
||||||
|
memset(t, 0, tn*Dbytes);
|
||||||
|
mpvecmul(q, qn, c->p, c->top, t);
|
||||||
|
|
||||||
|
/* q = hi(t) */
|
||||||
|
qn = tn - k;
|
||||||
|
if(qn <= 0) qn = 0;
|
||||||
|
else memmove(q, t+k, qn*Dbytes);
|
||||||
|
|
||||||
|
/* r += lo(t) */
|
||||||
|
if(tn > k)
|
||||||
|
tn = k;
|
||||||
|
mpvecadd(r->p, k, t, tn, r->p);
|
||||||
|
|
||||||
|
/* if(r >= m) r -= m */
|
||||||
|
mpvecsub(r->p, k+1, m->p, k, t), d = t[k];
|
||||||
|
for(tn = 0; tn < k; tn++)
|
||||||
|
r->p[tn] = (r->p[tn] & d) | (t[tn] & ~d);
|
||||||
|
} while(qn > 0);
|
||||||
|
|
||||||
|
busy = 0;
|
||||||
|
|
||||||
|
if(s != 0)
|
||||||
|
mpright(r, s, r);
|
||||||
|
else
|
||||||
|
mpnorm(r);
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
hard:
|
||||||
|
mpdiv(x, n, nil, r);
|
||||||
|
|
||||||
|
done:
|
||||||
|
if(sign < 0)
|
||||||
|
mpmagsub(n, r, r);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,96 @@
|
||||||
|
#include <u.h>
|
||||||
|
#include <libc.h>
|
||||||
|
#include <mp.h>
|
||||||
|
|
||||||
|
/* operands need to have m->top+1 digits of space and satisfy 0 ≤ a ≤ m-1 */
|
||||||
|
static mpint*
|
||||||
|
modarg(mpint *a, mpint *m)
|
||||||
|
{
|
||||||
|
if(a->size <= m->top || a->sign < 0 || mpmagcmp(a, m) >= 0){
|
||||||
|
a = mpcopy(a);
|
||||||
|
mpmod(a, m, a);
|
||||||
|
mpbits(a, Dbits*(m->top+1));
|
||||||
|
a->top = m->top;
|
||||||
|
} else if(a->top < m->top){
|
||||||
|
memset(&a->p[a->top], 0, (m->top - a->top)*Dbytes);
|
||||||
|
}
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
mpmodadd(mpint *b1, mpint *b2, mpint *m, mpint *sum)
|
||||||
|
{
|
||||||
|
mpint *a, *b;
|
||||||
|
mpdigit d;
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
a = modarg(b1, m);
|
||||||
|
b = modarg(b2, m);
|
||||||
|
|
||||||
|
sum->flags |= (a->flags | b->flags) & MPtimesafe;
|
||||||
|
mpbits(sum, Dbits*2*(m->top+1));
|
||||||
|
|
||||||
|
mpvecadd(a->p, m->top, b->p, m->top, sum->p);
|
||||||
|
mpvecsub(sum->p, m->top+1, m->p, m->top, sum->p+m->top+1);
|
||||||
|
|
||||||
|
d = sum->p[2*m->top+1];
|
||||||
|
for(i = 0, j = m->top+1; i < m->top; i++, j++)
|
||||||
|
sum->p[i] = (sum->p[i] & d) | (sum->p[j] & ~d);
|
||||||
|
|
||||||
|
sum->top = m->top;
|
||||||
|
sum->sign = 1;
|
||||||
|
mpnorm(sum);
|
||||||
|
|
||||||
|
if(a != b1)
|
||||||
|
mpfree(a);
|
||||||
|
if(b != b2)
|
||||||
|
mpfree(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
mpmodsub(mpint *b1, mpint *b2, mpint *m, mpint *diff)
|
||||||
|
{
|
||||||
|
mpint *a, *b;
|
||||||
|
mpdigit d;
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
a = modarg(b1, m);
|
||||||
|
b = modarg(b2, m);
|
||||||
|
|
||||||
|
diff->flags |= (a->flags | b->flags) & MPtimesafe;
|
||||||
|
mpbits(diff, Dbits*2*(m->top+1));
|
||||||
|
|
||||||
|
a->p[m->top] = 0;
|
||||||
|
mpvecsub(a->p, m->top+1, b->p, m->top, diff->p);
|
||||||
|
mpvecadd(diff->p, m->top, m->p, m->top, diff->p+m->top+1);
|
||||||
|
|
||||||
|
d = ~diff->p[m->top];
|
||||||
|
for(i = 0, j = m->top+1; i < m->top; i++, j++)
|
||||||
|
diff->p[i] = (diff->p[i] & d) | (diff->p[j] & ~d);
|
||||||
|
|
||||||
|
diff->top = m->top;
|
||||||
|
diff->sign = 1;
|
||||||
|
mpnorm(diff);
|
||||||
|
|
||||||
|
if(a != b1)
|
||||||
|
mpfree(a);
|
||||||
|
if(b != b2)
|
||||||
|
mpfree(b);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
mpmodmul(mpint *b1, mpint *b2, mpint *m, mpint *prod)
|
||||||
|
{
|
||||||
|
mpint *a, *b;
|
||||||
|
|
||||||
|
a = modarg(b1, m);
|
||||||
|
b = modarg(b2, m);
|
||||||
|
|
||||||
|
mpmul(a, b, prod);
|
||||||
|
mpmod(prod, m, prod);
|
||||||
|
|
||||||
|
if(a != b1)
|
||||||
|
mpfree(a);
|
||||||
|
if(b != b2)
|
||||||
|
mpfree(b);
|
||||||
|
}
|
|
@ -113,10 +113,6 @@ mpvecmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p)
|
||||||
a = b;
|
a = b;
|
||||||
b = t;
|
b = t;
|
||||||
}
|
}
|
||||||
if(blen == 0){
|
|
||||||
memset(p, 0, Dbytes*(alen+blen));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(alen >= KARATSUBAMIN && blen > 1){
|
if(alen >= KARATSUBAMIN && blen > 1){
|
||||||
// O(n^1.585)
|
// O(n^1.585)
|
||||||
|
@ -131,25 +127,49 @@ mpvecmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
mpvectsmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
mpdigit *t;
|
||||||
|
|
||||||
|
if(alen < blen){
|
||||||
|
i = alen;
|
||||||
|
alen = blen;
|
||||||
|
blen = i;
|
||||||
|
t = a;
|
||||||
|
a = b;
|
||||||
|
b = t;
|
||||||
|
}
|
||||||
|
if(blen == 0)
|
||||||
|
return;
|
||||||
|
for(i = 0; i < blen; i++)
|
||||||
|
mpvecdigmuladd(a, alen, b[i], &p[i]);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
mpmul(mpint *b1, mpint *b2, mpint *prod)
|
mpmul(mpint *b1, mpint *b2, mpint *prod)
|
||||||
{
|
{
|
||||||
mpint *oprod;
|
mpint *oprod;
|
||||||
|
|
||||||
oprod = nil;
|
oprod = prod;
|
||||||
if(prod == b1 || prod == b2){
|
if(prod == b1 || prod == b2){
|
||||||
oprod = prod;
|
|
||||||
prod = mpnew(0);
|
prod = mpnew(0);
|
||||||
|
prod->flags = oprod->flags;
|
||||||
}
|
}
|
||||||
|
prod->flags |= (b1->flags | b2->flags) & MPtimesafe;
|
||||||
|
|
||||||
prod->top = 0;
|
prod->top = 0;
|
||||||
mpbits(prod, (b1->top+b2->top+1)*Dbits);
|
mpbits(prod, (b1->top+b2->top+1)*Dbits);
|
||||||
mpvecmul(b1->p, b1->top, b2->p, b2->top, prod->p);
|
if(prod->flags & MPtimesafe)
|
||||||
|
mpvectsmul(b1->p, b1->top, b2->p, b2->top, prod->p);
|
||||||
|
else
|
||||||
|
mpvecmul(b1->p, b1->top, b2->p, b2->top, prod->p);
|
||||||
prod->top = b1->top+b2->top+1;
|
prod->top = b1->top+b2->top+1;
|
||||||
prod->sign = b1->sign*b2->sign;
|
prod->sign = b1->sign*b2->sign;
|
||||||
mpnorm(prod);
|
mpnorm(prod);
|
||||||
|
|
||||||
if(oprod != nil){
|
if(oprod != prod){
|
||||||
mpassign(prod, oprod);
|
mpassign(prod, oprod);
|
||||||
mpfree(prod);
|
mpfree(prod);
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,10 @@ mpnrand(mpint *n, void (*gen)(uchar*, int), mpint *b)
|
||||||
mpleft(mpone, bits, m);
|
mpleft(mpone, bits, m);
|
||||||
mpsub(m, mpone, m);
|
mpsub(m, mpone, m);
|
||||||
|
|
||||||
if(b == nil)
|
if(b == nil){
|
||||||
b = mpnew(bits);
|
b = mpnew(bits);
|
||||||
|
setmalloctag(b, getcallerpc(&n));
|
||||||
|
}
|
||||||
|
|
||||||
/* m = m - (m % n) */
|
/* m = m - (m % n) */
|
||||||
mpmod(m, n, b);
|
mpmod(m, n, b);
|
||||||
|
|
|
@ -6,37 +6,32 @@
|
||||||
mpint*
|
mpint*
|
||||||
mprand(int bits, void (*gen)(uchar*, int), mpint *b)
|
mprand(int bits, void (*gen)(uchar*, int), mpint *b)
|
||||||
{
|
{
|
||||||
int n, m;
|
|
||||||
mpdigit mask;
|
mpdigit mask;
|
||||||
|
int n, m;
|
||||||
uchar *p;
|
uchar *p;
|
||||||
|
|
||||||
n = DIGITS(bits);
|
n = DIGITS(bits);
|
||||||
if(b == nil)
|
if(b == nil){
|
||||||
b = mpnew(bits);
|
b = mpnew(bits);
|
||||||
else
|
setmalloctag(b, getcallerpc(&bits));
|
||||||
|
}else
|
||||||
mpbits(b, bits);
|
mpbits(b, bits);
|
||||||
|
|
||||||
p = malloc(n*Dbytes);
|
p = malloc(n*Dbytes);
|
||||||
if(p == nil)
|
if(p == nil)
|
||||||
return nil;
|
sysfatal("mprand: %r");
|
||||||
(*gen)(p, n*Dbytes);
|
(*gen)(p, n*Dbytes);
|
||||||
betomp(p, n*Dbytes, b);
|
betomp(p, n*Dbytes, b);
|
||||||
free(p);
|
free(p);
|
||||||
|
|
||||||
// make sure we don't give too many bits
|
// make sure we don't give too many bits
|
||||||
m = bits%Dbits;
|
m = bits%Dbits;
|
||||||
n--;
|
if(m == 0)
|
||||||
if(m > 0){
|
return b;
|
||||||
mask = 1;
|
|
||||||
mask <<= m;
|
|
||||||
mask--;
|
|
||||||
b->p[n] &= mask;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(; n >= 0; n--)
|
mask = 1;
|
||||||
if(b->p[n] != 0)
|
mask <<= m;
|
||||||
break;
|
mask--;
|
||||||
b->top = n+1;
|
b->p[n-1] &= mask;
|
||||||
b->sign = 1;
|
return mpnorm(b);
|
||||||
return b;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,12 +23,16 @@ mpright(mpint *b, int shift, mpint *res)
|
||||||
|
|
||||||
if(res != b)
|
if(res != b)
|
||||||
mpbits(res, b->top*Dbits - shift);
|
mpbits(res, b->top*Dbits - shift);
|
||||||
|
else if(shift == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
d = shift/Dbits;
|
d = shift/Dbits;
|
||||||
r = shift - d*Dbits;
|
r = shift - d*Dbits;
|
||||||
l = Dbits - r;
|
l = Dbits - r;
|
||||||
|
|
||||||
// shift all the bits out == zero
|
// shift all the bits out == zero
|
||||||
if(d>=b->top){
|
if(d>=b->top){
|
||||||
|
res->sign = 1;
|
||||||
res->top = 0;
|
res->top = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -46,9 +50,8 @@ mpright(mpint *b, int shift, mpint *res)
|
||||||
}
|
}
|
||||||
res->p[i++] = last>>r;
|
res->p[i++] = last>>r;
|
||||||
}
|
}
|
||||||
while(i > 0 && res->p[i-1] == 0)
|
|
||||||
i--;
|
|
||||||
res->top = i;
|
res->top = i;
|
||||||
if(i==0)
|
res->flags |= b->flags & MPtimesafe;
|
||||||
res->sign = 1;
|
mpnorm(res);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
#include "os.h"
|
||||||
|
#include <mp.h>
|
||||||
|
#include "dat.h"
|
||||||
|
|
||||||
|
// res = s != 0 ? b1 : b2
|
||||||
|
void
|
||||||
|
mpsel(int s, mpint *b1, mpint *b2, mpint *res)
|
||||||
|
{
|
||||||
|
mpdigit d;
|
||||||
|
int n, m, i;
|
||||||
|
|
||||||
|
res->flags |= (b1->flags | b2->flags) & MPtimesafe;
|
||||||
|
if((res->flags & MPtimesafe) == 0){
|
||||||
|
mpassign(s ? b1 : b2, res);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
res->flags &= ~MPnorm;
|
||||||
|
|
||||||
|
n = b1->top;
|
||||||
|
m = b2->top;
|
||||||
|
mpbits(res, Dbits*(n >= m ? n : m));
|
||||||
|
res->top = n >= m ? n : m;
|
||||||
|
|
||||||
|
s = (-s^s|s)>>(sizeof(s)*8-1);
|
||||||
|
res->sign = (b1->sign & s) | (b2->sign & ~s);
|
||||||
|
|
||||||
|
d = -((mpdigit)s & 1);
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while(i < n && i < m){
|
||||||
|
res->p[i] = (b1->p[i] & d) | (b2->p[i] & ~d);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while(i < n){
|
||||||
|
res->p[i] = b1->p[i] & d;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while(i < m){
|
||||||
|
res->p[i] = b2->p[i] & ~d;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,12 +11,15 @@ mpmagsub(mpint *b1, mpint *b2, mpint *diff)
|
||||||
|
|
||||||
// get the sizes right
|
// get the sizes right
|
||||||
if(mpmagcmp(b1, b2) < 0){
|
if(mpmagcmp(b1, b2) < 0){
|
||||||
|
assert(((b1->flags | b2->flags | diff->flags) & MPtimesafe) == 0);
|
||||||
sign = -1;
|
sign = -1;
|
||||||
t = b1;
|
t = b1;
|
||||||
b1 = b2;
|
b1 = b2;
|
||||||
b2 = t;
|
b2 = t;
|
||||||
} else
|
} else {
|
||||||
|
diff->flags |= (b1->flags | b2->flags) & MPtimesafe;
|
||||||
sign = 1;
|
sign = 1;
|
||||||
|
}
|
||||||
n = b1->top;
|
n = b1->top;
|
||||||
m = b2->top;
|
m = b2->top;
|
||||||
if(m == 0){
|
if(m == 0){
|
||||||
|
@ -39,6 +42,7 @@ mpsub(mpint *b1, mpint *b2, mpint *diff)
|
||||||
int sign;
|
int sign;
|
||||||
|
|
||||||
if(b1->sign != b2->sign){
|
if(b1->sign != b2->sign){
|
||||||
|
assert(((b1->flags | b2->flags | diff->flags) & MPtimesafe) == 0);
|
||||||
sign = b1->sign;
|
sign = b1->sign;
|
||||||
mpmagadd(b1, b2, diff);
|
mpmagadd(b1, b2, diff);
|
||||||
diff->sign = sign;
|
diff->sign = sign;
|
||||||
|
|
|
@ -2,57 +2,31 @@
|
||||||
#include <mp.h>
|
#include <mp.h>
|
||||||
#include "dat.h"
|
#include "dat.h"
|
||||||
|
|
||||||
// convert an mpint into a big endian byte array (most significant byte first)
|
// convert an mpint into a big endian byte array (most significant byte first; left adjusted)
|
||||||
// return number of bytes converted
|
// return number of bytes converted
|
||||||
// if p == nil, allocate and result array
|
// if p == nil, allocate and result array
|
||||||
int
|
int
|
||||||
mptobe(mpint *b, uchar *p, uint n, uchar **pp)
|
mptobe(mpint *b, uchar *p, uint n, uchar **pp)
|
||||||
{
|
{
|
||||||
int i, j, suppress;
|
int m;
|
||||||
mpdigit x;
|
|
||||||
uchar *e, *s, c;
|
|
||||||
|
|
||||||
|
m = (mpsignif(b)+7)/8;
|
||||||
|
if(m == 0)
|
||||||
|
m++;
|
||||||
if(p == nil){
|
if(p == nil){
|
||||||
n = (b->top+1)*Dbytes;
|
n = m;
|
||||||
p = malloc(n);
|
p = malloc(n);
|
||||||
|
if(p == nil)
|
||||||
|
sysfatal("mptobe: %r");
|
||||||
setmalloctag(p, getcallerpc(&b));
|
setmalloctag(p, getcallerpc(&b));
|
||||||
|
} else {
|
||||||
|
if(n < m)
|
||||||
|
return -1;
|
||||||
|
if(n > m)
|
||||||
|
memset(p+m, 0, n-m);
|
||||||
}
|
}
|
||||||
if(p == nil)
|
|
||||||
return -1;
|
|
||||||
if(pp != nil)
|
if(pp != nil)
|
||||||
*pp = p;
|
*pp = p;
|
||||||
memset(p, 0, n);
|
mptober(b, p, m);
|
||||||
|
return m;
|
||||||
// special case 0
|
|
||||||
if(b->top == 0){
|
|
||||||
if(n < 1)
|
|
||||||
return -1;
|
|
||||||
else
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
s = p;
|
|
||||||
e = s+n;
|
|
||||||
suppress = 1;
|
|
||||||
for(i = b->top-1; i >= 0; i--){
|
|
||||||
x = b->p[i];
|
|
||||||
for(j = Dbits-8; j >= 0; j -= 8){
|
|
||||||
c = x>>j;
|
|
||||||
if(c == 0 && suppress)
|
|
||||||
continue;
|
|
||||||
if(p >= e)
|
|
||||||
return -1;
|
|
||||||
*p++ = c;
|
|
||||||
suppress = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// guarantee at least one byte
|
|
||||||
if(s == p){
|
|
||||||
if(p >= e)
|
|
||||||
return -1;
|
|
||||||
*p++ = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return p - s;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
#include "os.h"
|
||||||
|
#include <mp.h>
|
||||||
|
#include "dat.h"
|
||||||
|
|
||||||
|
void
|
||||||
|
mptober(mpint *b, uchar *p, int n)
|
||||||
|
{
|
||||||
|
int i, j, m;
|
||||||
|
mpdigit x;
|
||||||
|
|
||||||
|
memset(p, 0, n);
|
||||||
|
|
||||||
|
p += n;
|
||||||
|
m = b->top*Dbytes;
|
||||||
|
if(m < n)
|
||||||
|
n = m;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while(n >= Dbytes){
|
||||||
|
n -= Dbytes;
|
||||||
|
x = b->p[i++];
|
||||||
|
for(j = 0; j < Dbytes; j++){
|
||||||
|
*--p = x;
|
||||||
|
x >>= 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(n > 0){
|
||||||
|
x = b->p[i];
|
||||||
|
for(j = 0; j < n; j++){
|
||||||
|
*--p = x;
|
||||||
|
x >>= 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,17 +10,15 @@
|
||||||
mpint*
|
mpint*
|
||||||
itomp(int i, mpint *b)
|
itomp(int i, mpint *b)
|
||||||
{
|
{
|
||||||
if(b == nil)
|
if(b == nil){
|
||||||
b = mpnew(0);
|
b = mpnew(0);
|
||||||
mpassign(mpzero, b);
|
setmalloctag(b, getcallerpc(&i));
|
||||||
if(i != 0)
|
}
|
||||||
b->top = 1;
|
b->sign = (i >> (sizeof(i)*8 - 1)) | 1;
|
||||||
if(i < 0){
|
i *= b->sign;
|
||||||
b->sign = -1;
|
*b->p = i;
|
||||||
*b->p = -i;
|
b->top = 1;
|
||||||
} else
|
return mpnorm(b);
|
||||||
*b->p = i;
|
|
||||||
return b;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
|
@ -3,52 +3,26 @@
|
||||||
#include "dat.h"
|
#include "dat.h"
|
||||||
|
|
||||||
// convert an mpint into a little endian byte array (least significant byte first)
|
// convert an mpint into a little endian byte array (least significant byte first)
|
||||||
|
|
||||||
// return number of bytes converted
|
// return number of bytes converted
|
||||||
// if p == nil, allocate and result array
|
// if p == nil, allocate and result array
|
||||||
int
|
int
|
||||||
mptole(mpint *b, uchar *p, uint n, uchar **pp)
|
mptole(mpint *b, uchar *p, uint n, uchar **pp)
|
||||||
{
|
{
|
||||||
int i, j;
|
int m;
|
||||||
mpdigit x;
|
|
||||||
uchar *e, *s;
|
|
||||||
|
|
||||||
|
m = (mpsignif(b)+7)/8;
|
||||||
|
if(m == 0)
|
||||||
|
m++;
|
||||||
if(p == nil){
|
if(p == nil){
|
||||||
n = (b->top+1)*Dbytes;
|
n = m;
|
||||||
p = malloc(n);
|
p = malloc(n);
|
||||||
}
|
if(p == nil)
|
||||||
|
sysfatal("mptole: %r");
|
||||||
|
setmalloctag(p, getcallerpc(&b));
|
||||||
|
} else if(n < m)
|
||||||
|
return -1;
|
||||||
if(pp != nil)
|
if(pp != nil)
|
||||||
*pp = p;
|
*pp = p;
|
||||||
if(p == nil)
|
mptolel(b, p, n);
|
||||||
return -1;
|
return m;
|
||||||
memset(p, 0, n);
|
|
||||||
|
|
||||||
// special case 0
|
|
||||||
if(b->top == 0){
|
|
||||||
if(n < 1)
|
|
||||||
return -1;
|
|
||||||
else
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
s = p;
|
|
||||||
e = s+n;
|
|
||||||
for(i = 0; i < b->top-1; i++){
|
|
||||||
x = b->p[i];
|
|
||||||
for(j = 0; j < Dbytes; j++){
|
|
||||||
if(p >= e)
|
|
||||||
return -1;
|
|
||||||
*p++ = x;
|
|
||||||
x >>= 8;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
x = b->p[i];
|
|
||||||
while(x > 0){
|
|
||||||
if(p >= e)
|
|
||||||
return -1;
|
|
||||||
*p++ = x;
|
|
||||||
x >>= 8;
|
|
||||||
}
|
|
||||||
|
|
||||||
return p - s;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
#include "os.h"
|
||||||
|
#include <mp.h>
|
||||||
|
#include "dat.h"
|
||||||
|
|
||||||
|
void
|
||||||
|
mptolel(mpint *b, uchar *p, int n)
|
||||||
|
{
|
||||||
|
int i, j, m;
|
||||||
|
mpdigit x;
|
||||||
|
|
||||||
|
memset(p, 0, n);
|
||||||
|
|
||||||
|
m = b->top*Dbytes;
|
||||||
|
if(m < n)
|
||||||
|
n = m;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while(n >= Dbytes){
|
||||||
|
n -= Dbytes;
|
||||||
|
x = b->p[i++];
|
||||||
|
for(j = 0; j < Dbytes; j++){
|
||||||
|
*p++ = x;
|
||||||
|
x >>= 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(n > 0){
|
||||||
|
x = b->p[i];
|
||||||
|
for(j = 0; j < n; j++){
|
||||||
|
*p++ = x;
|
||||||
|
x >>= 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,13 +10,14 @@
|
||||||
mpint*
|
mpint*
|
||||||
uitomp(uint i, mpint *b)
|
uitomp(uint i, mpint *b)
|
||||||
{
|
{
|
||||||
if(b == nil)
|
if(b == nil){
|
||||||
b = mpnew(0);
|
b = mpnew(0);
|
||||||
mpassign(mpzero, b);
|
setmalloctag(b, getcallerpc(&i));
|
||||||
if(i != 0)
|
}
|
||||||
b->top = 1;
|
|
||||||
*b->p = i;
|
*b->p = i;
|
||||||
return b;
|
b->top = 1;
|
||||||
|
b->sign = 1;
|
||||||
|
return mpnorm(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint
|
uint
|
||||||
|
|
|
@ -13,19 +13,18 @@ uvtomp(uvlong v, mpint *b)
|
||||||
{
|
{
|
||||||
int s;
|
int s;
|
||||||
|
|
||||||
if(b == nil)
|
if(b == nil){
|
||||||
b = mpnew(VLDIGITS*sizeof(mpdigit));
|
b = mpnew(VLDIGITS*sizeof(mpdigit));
|
||||||
else
|
setmalloctag(b, getcallerpc(&v));
|
||||||
|
}else
|
||||||
mpbits(b, VLDIGITS*sizeof(mpdigit));
|
mpbits(b, VLDIGITS*sizeof(mpdigit));
|
||||||
mpassign(mpzero, b);
|
b->sign = 1;
|
||||||
if(v == 0)
|
for(s = 0; s < VLDIGITS; s++){
|
||||||
return b;
|
|
||||||
for(s = 0; s < VLDIGITS && v != 0; s++){
|
|
||||||
b->p[s] = v;
|
b->p[s] = v;
|
||||||
v >>= sizeof(mpdigit)*8;
|
v >>= sizeof(mpdigit)*8;
|
||||||
}
|
}
|
||||||
b->top = s;
|
b->top = s;
|
||||||
return b;
|
return mpnorm(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
uvlong
|
uvlong
|
||||||
|
@ -37,7 +36,6 @@ mptouv(mpint *b)
|
||||||
if(b->top == 0)
|
if(b->top == 0)
|
||||||
return 0LL;
|
return 0LL;
|
||||||
|
|
||||||
mpnorm(b);
|
|
||||||
if(b->top > VLDIGITS)
|
if(b->top > VLDIGITS)
|
||||||
return MAXVLONG;
|
return MAXVLONG;
|
||||||
|
|
||||||
|
|
|
@ -14,24 +14,19 @@ vtomp(vlong v, mpint *b)
|
||||||
int s;
|
int s;
|
||||||
uvlong uv;
|
uvlong uv;
|
||||||
|
|
||||||
if(b == nil)
|
if(b == nil){
|
||||||
b = mpnew(VLDIGITS*sizeof(mpdigit));
|
b = mpnew(VLDIGITS*sizeof(mpdigit));
|
||||||
else
|
setmalloctag(b, getcallerpc(&v));
|
||||||
|
}else
|
||||||
mpbits(b, VLDIGITS*sizeof(mpdigit));
|
mpbits(b, VLDIGITS*sizeof(mpdigit));
|
||||||
mpassign(mpzero, b);
|
b->sign = (v >> (sizeof(v)*8 - 1)) | 1;
|
||||||
if(v == 0)
|
uv = v * b->sign;
|
||||||
return b;
|
for(s = 0; s < VLDIGITS; s++){
|
||||||
if(v < 0){
|
|
||||||
b->sign = -1;
|
|
||||||
uv = -v;
|
|
||||||
} else
|
|
||||||
uv = v;
|
|
||||||
for(s = 0; s < VLDIGITS && uv != 0; s++){
|
|
||||||
b->p[s] = uv;
|
b->p[s] = uv;
|
||||||
uv >>= sizeof(mpdigit)*8;
|
uv >>= sizeof(mpdigit)*8;
|
||||||
}
|
}
|
||||||
b->top = s;
|
b->top = s;
|
||||||
return b;
|
return mpnorm(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
vlong
|
vlong
|
||||||
|
@ -43,7 +38,6 @@ mptov(mpint *b)
|
||||||
if(b->top == 0)
|
if(b->top == 0)
|
||||||
return 0LL;
|
return 0LL;
|
||||||
|
|
||||||
mpnorm(b);
|
|
||||||
if(b->top > VLDIGITS){
|
if(b->top > VLDIGITS){
|
||||||
if(b->sign > 0)
|
if(b->sign > 0)
|
||||||
return (vlong)MAXVLONG;
|
return (vlong)MAXVLONG;
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
#include "os.h"
|
||||||
|
#include <mp.h>
|
||||||
|
#include "dat.h"
|
||||||
|
|
||||||
|
int
|
||||||
|
mpvectscmp(mpdigit *a, int alen, mpdigit *b, int blen)
|
||||||
|
{
|
||||||
|
mpdigit x, y, z, v;
|
||||||
|
int m, p;
|
||||||
|
|
||||||
|
if(alen > blen){
|
||||||
|
v = 0;
|
||||||
|
while(alen > blen)
|
||||||
|
v |= a[--alen];
|
||||||
|
m = p = (-v^v|v)>>Dbits-1;
|
||||||
|
} else if(blen > alen){
|
||||||
|
v = 0;
|
||||||
|
while(blen > alen)
|
||||||
|
v |= b[--blen];
|
||||||
|
m = (-v^v|v)>>Dbits-1;
|
||||||
|
p = m^1;
|
||||||
|
} else
|
||||||
|
m = p = 0;
|
||||||
|
while(alen-- > 0){
|
||||||
|
x = a[alen];
|
||||||
|
y = b[alen];
|
||||||
|
z = x - y;
|
||||||
|
x = ~x;
|
||||||
|
v = ((-z^z|z)>>Dbits-1) & ~m;
|
||||||
|
p = ((~(x&y|x&z|y&z)>>Dbits-1) & v) | (p & ~v);
|
||||||
|
m |= v;
|
||||||
|
}
|
||||||
|
return (p-m) | m;
|
||||||
|
}
|
|
@ -50,7 +50,6 @@ from16(char *a, mpint *b)
|
||||||
int i;
|
int i;
|
||||||
mpdigit x;
|
mpdigit x;
|
||||||
|
|
||||||
b->top = 0;
|
|
||||||
for(p = a; *p; p++)
|
for(p = a; *p; p++)
|
||||||
if(tab.t16[*(uchar*)p] == INVAL)
|
if(tab.t16[*(uchar*)p] == INVAL)
|
||||||
break;
|
break;
|
||||||
|
@ -157,8 +156,10 @@ strtomp(char *a, char **pp, int base, mpint *b)
|
||||||
int sign;
|
int sign;
|
||||||
char *e;
|
char *e;
|
||||||
|
|
||||||
if(b == nil)
|
if(b == nil){
|
||||||
b = mpnew(0);
|
b = mpnew(0);
|
||||||
|
setmalloctag(b, getcallerpc(&a));
|
||||||
|
}
|
||||||
|
|
||||||
if(tab.inited == 0)
|
if(tab.inited == 0)
|
||||||
init();
|
init();
|
||||||
|
@ -196,10 +197,9 @@ strtomp(char *a, char **pp, int base, mpint *b)
|
||||||
if(e == a)
|
if(e == a)
|
||||||
return nil;
|
return nil;
|
||||||
|
|
||||||
mpnorm(b);
|
|
||||||
b->sign = sign;
|
|
||||||
if(pp != nil)
|
if(pp != nil)
|
||||||
*pp = e;
|
*pp = e;
|
||||||
|
|
||||||
return b;
|
b->sign = sign;
|
||||||
|
return mpnorm(b);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue