//globals
bpe=0; //bits stored per array element
mask=0; //AND this with an array element to chop it down to bpe bits
//initialize the global variables for (bpe=0; (1<<(bpe+1)) > (1<<bpe); bpe++); //bpe=number of bits in the mantissa on this platform
bpe>>=1; //bpe=number of bits in one element of the array representing the bigInt
mask=(1<<bpe)-1; //AND the mask with an integer to get its bpe least significant bits
//the following global variables are scratchpad memory to //reduce dynamic memory allocation in the inner loop
sa = new Array(0); //used in mont_()
//do x=y on bigInts x and y. x must be an array at least as big as y (not counting the leading zeros in y). function copy_(x,y) { var i; var k=x.length<y.length ? x.length : y.length; for (i=0;i<k;i++)
x[i]=y[i]; for (i=k;i<x.length;i++)
x[i]=0;
}
//do x=y on bigInt x and integer y. function copyInt_(x,n) { var i,c; for (c=n,i=0;i<x.length;i++) {
x[i]=c & mask;
c>>=bpe;
}
}
//is x > y? (x and y both nonnegative) function greater(x,y) { var i; var k=(x.length<y.length) ? x.length : y.length;
for (i=x.length;i<y.length;i++) if (y[i]) return 0; //y has more digits
for (i=y.length;i<x.length;i++) if (x[i]) return 1; //x has more digits
for (i=k-1;i>=0;i--) if (x[i]>y[i]) return 1; elseif (x[i]<y[i]) return 0; return 0;
}
//do x=x*y*Ri mod n for bigInts x,y,n, // where Ri = 2**(-kn*bpe) mod n, and kn is the // number of elements in the n array, not // counting leading zeros. //x must be large enough to hold the answer. //It's OK if x and y are the same variable. //must have: // x,y < n // n is odd // np = -(n^(-1)) mod radix function mont_(x,y,n,np) { var i,j,c,ui,t; var kn=n.length; var ky=y.length;
if (sa.length!=kn)
sa=new Array(kn);
for (;kn>0 && n[kn-1]==0;kn--); //ignore leading zeros of n for (;ky>0 && y[ky-1]==0;ky--); //ignore leading zeros of y
copyInt_(sa,0);
//the following loop consumes 95% of the runtime for randTruePrime_() and powMod_() for large keys for (i=0; i<kn; i++) {
t=sa[0]+x[i]*y[0];
ui=((t & mask) * np) & mask; //the inner "& mask" is needed on Macintosh MSIE, but not windows MSIE
c=(t+ui*n[0]) >> bpe;
t=x[i];
//do sa=(sa+x[i]*y+ui*n)/b where b=2**bpe for (j=1;j<ky;j++) {
c+=sa[j]+t*y[j]+ui*n[j];
sa[j-1]=c & mask;
c>>=bpe;
} for (;j<kn;j++) {
c+=sa[j]+ui*n[j];
sa[j-1]=c & mask;
c>>=bpe;
}
sa[j-1]=c & mask;
}
if (!greater(n,sa))
sub_(sa,n);
copy_(x,sa);
}
mont_(x, x, n, np);
var passed = expected.length == x.length; for (var i = 0; i < expected.length; i++) { if (passed)
passed = expected[i] == x[i];
}
assertEq(passed, true);
Messung V0.5
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.