Diffstat (limited to 'frontend/gamma/js/Clipperz/Crypto/BigInt.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/gamma/js/Clipperz/Crypto/BigInt.js | 15 |
1 files changed, 6 insertions, 9 deletions
diff --git a/frontend/gamma/js/Clipperz/Crypto/BigInt.js b/frontend/gamma/js/Clipperz/Crypto/BigInt.js index d4d05d2..41483a3 100644 --- a/frontend/gamma/js/Clipperz/Crypto/BigInt.js +++ b/frontend/gamma/js/Clipperz/Crypto/BigInt.js | |||
@@ -1,216 +1,213 @@ | |||
1 | /* | 1 | /* |
2 | 2 | ||
3 | Copyright 2008-2011 Clipperz Srl | 3 | Copyright 2008-2011 Clipperz Srl |
4 | 4 | ||
5 | This file is part of Clipperz's Javascript Crypto Library. | 5 | This file is part of Clipperz Community Edition. |
6 | Javascript Crypto Library provides web developers with an extensive | 6 | Clipperz Community Edition is an online password manager. |
7 | and efficient set of cryptographic functions. The library aims to | ||
8 | obtain maximum execution speed while preserving modularity and | ||
9 | reusability. | ||
10 | For further information about its features and functionalities please | 7 | For further information about its features and functionalities please |
11 | refer to http://www.clipperz.com | 8 | refer to http://www.clipperz.com. |
12 | 9 | ||
13 | * Javascript Crypto Library is free software: you can redistribute | 10 | * Clipperz Community Edition is free software: you can redistribute |
14 | it and/or modify it under the terms of the GNU Affero General Public | 11 | it and/or modify it under the terms of the GNU Affero General Public |
15 | License as published by the Free Software Foundation, either version | 12 | License as published by the Free Software Foundation, either version |
16 | 3 of the License, or (at your option) any later version. | 13 | 3 of the License, or (at your option) any later version. |
17 | 14 | ||
18 | * Javascript Crypto Library is distributed in the hope that it will | 15 | * Clipperz Community Edition is distributed in the hope that it will |
19 | be useful, but WITHOUT ANY WARRANTY; without even the implied | 16 | be useful, but WITHOUT ANY WARRANTY; without even the implied |
20 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 17 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
21 | See the GNU Affero General Public License for more details. | 18 | See the GNU Affero General Public License for more details. |
22 | 19 | ||
23 | * You should have received a copy of the GNU Affero General Public | 20 | * You should have received a copy of the GNU Affero General Public |
24 | License along with Javascript Crypto Library. If not, see | 21 | License along with Clipperz Community Edition. If not, see |
25 | <http://www.gnu.org/licenses/>. | 22 | <http://www.gnu.org/licenses/>. |
26 | 23 | ||
27 | */ | 24 | */ |
28 | 25 | ||
29 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } | 26 | if (typeof(Clipperz) == 'undefined') { Clipperz = {}; } |
30 | if (typeof(Clipperz.Crypto) == 'undefined') { Clipperz.Crypto = {}; } | 27 | if (typeof(Clipperz.Crypto) == 'undefined') { Clipperz.Crypto = {}; } |
31 | 28 | ||
32 | //############################################################################# | 29 | //############################################################################# |
33 | //Downloaded on March 05, 2007 from http://www.leemon.com/crypto/BigInt.js | 30 | //Downloaded on March 05, 2007 from http://www.leemon.com/crypto/BigInt.js |
34 | //############################################################################# | 31 | //############################################################################# |
35 | 32 | ||
36 | 33 | ||
37 | //////////////////////////////////////////////////////////////////////////////////////// | 34 | //////////////////////////////////////////////////////////////////////////////////////// |
38 | // Big Integer Library v. 5.0 | 35 | // Big Integer Library v. 5.0 |
39 | // Created 2000, last modified 2006 | 36 | // Created 2000, last modified 2006 |
40 | // Leemon Baird | 37 | // Leemon Baird |
41 | // www.leemon.com | 38 | // www.leemon.com |
42 | // | 39 | // |
43 | // This file is public domain. You can use it for any purpose without restriction. | 40 | // This file is public domain. You can use it for any purpose without restriction. |
44 | // I do not guarantee that it is correct, so use it at your own risk. If you use | 41 | // I do not guarantee that it is correct, so use it at your own risk. If you use |
45 | // it for something interesting, I'd appreciate hearing about it. If you find | 42 | // it for something interesting, I'd appreciate hearing about it. If you find |
46 | // any bugs or make any improvements, I'd appreciate hearing about those too. | 43 | // any bugs or make any improvements, I'd appreciate hearing about those too. |
47 | // It would also be nice if my name and address were left in the comments. | 44 | // It would also be nice if my name and address were left in the comments. |
48 | // But none of that is required. | 45 | // But none of that is required. |
49 | // | 46 | // |
50 | // This code defines a bigInt library for arbitrary-precision integers. | 47 | // This code defines a bigInt library for arbitrary-precision integers. |
51 | // A bigInt is an array of integers storing the value in chunks of bpe bits, | 48 | // A bigInt is an array of integers storing the value in chunks of bpe bits, |
52 | // little endian (buff[0] is the least significant word). | 49 | // little endian (buff[0] is the least significant word). |
53 | // Negative bigInts are stored two's complement. | 50 | // Negative bigInts are stored two's complement. |
54 | // Some functions assume their parameters have at least one leading zero element. | 51 | // Some functions assume their parameters have at least one leading zero element. |
55 | // Functions with an underscore at the end of the name have unpredictable behavior in case of overflow, | 52 | // Functions with an underscore at the end of the name have unpredictable behavior in case of overflow, |
56 | // so the caller must make sure overflow won't happen. | 53 | // so the caller must make sure overflow won't happen. |
57 | // For each function where a parameter is modified, that same | 54 | // For each function where a parameter is modified, that same |
58 | // variable must not be used as another argument too. | 55 | // variable must not be used as another argument too. |
59 | // So, you cannot square x by doing multMod_(x,x,n). | 56 | // So, you cannot square x by doing multMod_(x,x,n). |
60 | // You must use squareMod_(x,n) instead, or do y=dup(x); multMod_(x,y,n). | 57 | // You must use squareMod_(x,n) instead, or do y=dup(x); multMod_(x,y,n). |
61 | // | 58 | // |
62 | // These functions are designed to avoid frequent dynamic memory allocation in the inner loop. | 59 | // These functions are designed to avoid frequent dynamic memory allocation in the inner loop. |
63 | // For most functions, if it needs a BigInt as a local variable it will actually use | 60 | // For most functions, if it needs a BigInt as a local variable it will actually use |
64 | // a global, and will only allocate to it when it's not the right size. This ensures | 61 | // a global, and will only allocate to it when it's not the right size. This ensures |
65 | // that when a function is called repeatedly with same-sized parameters, it only allocates | 62 | // that when a function is called repeatedly with same-sized parameters, it only allocates |
66 | // memory on the first call. | 63 | // memory on the first call. |
67 | // | 64 | // |
68 | // Note that for cryptographic purposes, the calls to Math.random() must | 65 | // Note that for cryptographic purposes, the calls to Math.random() must |
69 | // be replaced with calls to a better pseudorandom number generator. | 66 | // be replaced with calls to a better pseudorandom number generator. |
70 | // | 67 | // |
71 | // In the following, "bigInt" means a bigInt with at least one leading zero element, | 68 | // In the following, "bigInt" means a bigInt with at least one leading zero element, |
72 | // and "integer" means a nonnegative integer less than radix. In some cases, integer | 69 | // and "integer" means a nonnegative integer less than radix. In some cases, integer |
73 | // can be negative. Negative bigInts are 2s complement. | 70 | // can be negative. Negative bigInts are 2s complement. |
74 | // | 71 | // |
75 | // The following functions do not modify their inputs, but dynamically allocate memory every time they are called: | 72 | // The following functions do not modify their inputs, but dynamically allocate memory every time they are called: |
76 | // | 73 | // |
77 | // function bigInt2str(x,base) //convert a bigInt into a string in a given base, from base 2 up to base 95 | 74 | // function bigInt2str(x,base) //convert a bigInt into a string in a given base, from base 2 up to base 95 |
78 | // function dup(x) //returns a copy of bigInt x | 75 | // function dup(x) //returns a copy of bigInt x |
79 | // function findPrimes(n) //return array of all primes less than integer n | 76 | // function findPrimes(n) //return array of all primes less than integer n |
80 | // function int2bigInt(t,n,m) //convert integer t to a bigInt with at least n bits and m array elements | 77 | // function int2bigInt(t,n,m) //convert integer t to a bigInt with at least n bits and m array elements |
81 | // function int2bigInt(s,b,n,m) //convert string s in base b to a bigInt with at least n bits and m array elements | 78 | // function int2bigInt(s,b,n,m) //convert string s in base b to a bigInt with at least n bits and m array elements |
82 | // function trim(x,k) //return a copy of x with exactly k leading zero elements | 79 | // function trim(x,k) //return a copy of x with exactly k leading zero elements |
83 | // | 80 | // |
84 | // The following functions do not modify their inputs, so there is never a problem with the result being too big: | 81 | // The following functions do not modify their inputs, so there is never a problem with the result being too big: |
85 | // | 82 | // |
86 | // function bitSize(x) //returns how many bits long the bigInt x is, not counting leading zeros | 83 | // function bitSize(x) //returns how many bits long the bigInt x is, not counting leading zeros |
87 | // function equals(x,y) //is the bigInt x equal to the bigint y? | 84 | // function equals(x,y) //is the bigInt x equal to the bigint y? |
88 | // function equalsInt(x,y) //is bigint x equal to integer y? | 85 | // function equalsInt(x,y) //is bigint x equal to integer y? |
89 | // function greater(x,y) //is x>y? (x and y are nonnegative bigInts) | 86 | // function greater(x,y) //is x>y? (x and y are nonnegative bigInts) |
90 | // function greaterShift(x,y,shift)//is (x <<(shift*bpe)) > y? | 87 | // function greaterShift(x,y,shift)//is (x <<(shift*bpe)) > y? |
91 | // function isZero(x) //is the bigInt x equal to zero? | 88 | // function isZero(x) //is the bigInt x equal to zero? |
92 | // function millerRabin(x,b) //does one round of Miller-Rabin base integer b say that bigInt x is possibly prime (as opposed to definitely composite)? | 89 | // function millerRabin(x,b) //does one round of Miller-Rabin base integer b say that bigInt x is possibly prime (as opposed to definitely composite)? |
93 | // function modInt(x,n) //return x mod n for bigInt x and integer n. | 90 | // function modInt(x,n) //return x mod n for bigInt x and integer n. |
94 | // function negative(x) //is bigInt x negative? | 91 | // function negative(x) //is bigInt x negative? |
95 | // | 92 | // |
96 | // The following functions do not modify their inputs, but allocate memory and call functions with underscores | 93 | // The following functions do not modify their inputs, but allocate memory and call functions with underscores |
97 | // | 94 | // |
98 | // function add(x,y) //return (x+y) for bigInts x and y. | 95 | // function add(x,y) //return (x+y) for bigInts x and y. |
99 | // function addInt(x,n) //return (x+n) where x is a bigInt and n is an integer. | 96 | // function addInt(x,n) //return (x+n) where x is a bigInt and n is an integer. |
100 | // function expand(x,n) //return a copy of x with at least n elements, adding leading zeros if needed | 97 | // function expand(x,n) //return a copy of x with at least n elements, adding leading zeros if needed |
101 | // function inverseMod(x,n) //return (x**(-1) mod n) for bigInts x and n. If no inverse exists, it returns null | 98 | // function inverseMod(x,n) //return (x**(-1) mod n) for bigInts x and n. If no inverse exists, it returns null |
102 | // function mod(x,n) //return a new bigInt equal to (x mod n) for bigInts x and n. | 99 | // function mod(x,n) //return a new bigInt equal to (x mod n) for bigInts x and n. |
103 | // function mult(x,y) //return x*y for bigInts x and y. This is faster when y<x. | 100 | // function mult(x,y) //return x*y for bigInts x and y. This is faster when y<x. |
104 | // function multMod(x,y,n) //return (x*y mod n) for bigInts x,y,n. For greater speed, let y<x. | 101 | // function multMod(x,y,n) //return (x*y mod n) for bigInts x,y,n. For greater speed, let y<x. |
105 | // function powMod(x,y,n) //return (x**y mod n) where x,y,n are bigInts and ** is exponentiation. 0**0=1. Faster for odd n. | 102 | // function powMod(x,y,n) //return (x**y mod n) where x,y,n are bigInts and ** is exponentiation. 0**0=1. Faster for odd n. |
106 | // function randTruePrime(k) //return a new, random, k-bit, true prime using Maurer's algorithm. | 103 | // function randTruePrime(k) //return a new, random, k-bit, true prime using Maurer's algorithm. |
107 | // function sub(x,y) //return (x-y) for bigInts x and y. Negative answers will be 2s complement | 104 | // function sub(x,y) //return (x-y) for bigInts x and y. Negative answers will be 2s complement |
108 | // | 105 | // |
109 | // The following functions write a bigInt result to one of the parameters, but | 106 | // The following functions write a bigInt result to one of the parameters, but |
110 | // the result is never bigger than the original, so there can't be overflow problems: | 107 | // the result is never bigger than the original, so there can't be overflow problems: |
111 | // | 108 | // |
112 | // function divInt_(x,n) //do x=floor(x/n) for bigInt x and integer n, and return the remainder | 109 | // function divInt_(x,n) //do x=floor(x/n) for bigInt x and integer n, and return the remainder |
113 | // function GCD_(x,y) //set x to the greatest common divisor of bigInts x and y, (y is destroyed). | 110 | // function GCD_(x,y) //set x to the greatest common divisor of bigInts x and y, (y is destroyed). |
114 | // function halve_(x) //do x=floor(|x|/2)*sgn(x) for bigInt x in 2's complement | 111 | // function halve_(x) //do x=floor(|x|/2)*sgn(x) for bigInt x in 2's complement |
115 | // function mod_(x,n) //do x=x mod n for bigInts x and n. | 112 | // function mod_(x,n) //do x=x mod n for bigInts x and n. |
116 | // function rightShift_(x,n) //right shift bigInt x by n bits. 0 <= n < bpe. | 113 | // function rightShift_(x,n) //right shift bigInt x by n bits. 0 <= n < bpe. |
117 | // | 114 | // |
118 | // The following functions write a bigInt result to one of the parameters. The caller is responsible for | 115 | // The following functions write a bigInt result to one of the parameters. The caller is responsible for |
119 | // ensuring it is large enough to hold the result. | 116 | // ensuring it is large enough to hold the result. |
120 | // | 117 | // |
121 | // function addInt_(x,n) //do x=x+n where x is a bigInt and n is an integer | 118 | // function addInt_(x,n) //do x=x+n where x is a bigInt and n is an integer |
122 | // function add_(x,y) //do x=x+y for bigInts x and y | 119 | // function add_(x,y) //do x=x+y for bigInts x and y |
123 | // function addShift_(x,y,ys) //do x=x+(y<<(ys*bpe)) | 120 | // function addShift_(x,y,ys) //do x=x+(y<<(ys*bpe)) |
124 | // function copy_(x,y) //do x=y on bigInts x and y | 121 | // function copy_(x,y) //do x=y on bigInts x and y |
125 | // function copyInt_(x,n) //do x=n on bigInt x and integer n | 122 | // function copyInt_(x,n) //do x=n on bigInt x and integer n |
126 | // function carry_(x) //do carries and borrows so each element of the bigInt x fits in bpe bits. | 123 | // function carry_(x) //do carries and borrows so each element of the bigInt x fits in bpe bits. |
127 | // function divide_(x,y,q,r) //divide_ x by y giving quotient q and remainder r | 124 | // function divide_(x,y,q,r) //divide_ x by y giving quotient q and remainder r |
128 | // function eGCD_(x,y,d,a,b) //sets a,b,d to positive big integers such that d = GCD_(x,y) = a*x-b*y | 125 | // function eGCD_(x,y,d,a,b) //sets a,b,d to positive big integers such that d = GCD_(x,y) = a*x-b*y |
129 | // function inverseMod_(x,n) //do x=x**(-1) mod n, for bigInts x and n. Returns 1 (0) if inverse does (doesn't) exist | 126 | // function inverseMod_(x,n) //do x=x**(-1) mod n, for bigInts x and n. Returns 1 (0) if inverse does (doesn't) exist |
130 | // function inverseModInt_(x,n) //return x**(-1) mod n, for integers x and n. Return 0 if there is no inverse | 127 | // function inverseModInt_(x,n) //return x**(-1) mod n, for integers x and n. Return 0 if there is no inverse |
131 | // function leftShift_(x,n) //left shift bigInt x by n bits. n<bpe. | 128 | // function leftShift_(x,n) //left shift bigInt x by n bits. n<bpe. |
132 | // function linComb_(x,y,a,b) //do x=a*x+b*y for bigInts x and y and integers a and b | 129 | // function linComb_(x,y,a,b) //do x=a*x+b*y for bigInts x and y and integers a and b |
133 | // function linCombShift_(x,y,b,ys) //do x=x+b*(y<<(ys*bpe)) for bigInts x and y, and integers b and ys | 130 | // function linCombShift_(x,y,b,ys) //do x=x+b*(y<<(ys*bpe)) for bigInts x and y, and integers b and ys |
134 | // function mont_(x,y,n,np) //Montgomery multiplication (see comments where the function is defined) | 131 | // function mont_(x,y,n,np) //Montgomery multiplication (see comments where the function is defined) |
135 | // function mult_(x,y) //do x=x*y for bigInts x and y. | 132 | // function mult_(x,y) //do x=x*y for bigInts x and y. |
136 | // function multInt_(x,n) //do x=x*n where x is a bigInt and n is an integer. | 133 | // function multInt_(x,n) //do x=x*n where x is a bigInt and n is an integer. |
137 | // function multMod_(x,y,n) //do x=x*y mod n for bigInts x,y,n. | 134 | // function multMod_(x,y,n) //do x=x*y mod n for bigInts x,y,n. |
138 | // function powMod_(x,y,n) //do x=x**y mod n, where x,y,n are bigInts (n is odd) and ** is exponentiation. 0**0=1. | 135 | // function powMod_(x,y,n) //do x=x**y mod n, where x,y,n are bigInts (n is odd) and ** is exponentiation. 0**0=1. |
139 | // function randBigInt_(b,n,s) //do b = an n-bit random BigInt. if s=1, then nth bit (most significant bit) is set to 1. n>=1. | 136 | // function randBigInt_(b,n,s) //do b = an n-bit random BigInt. if s=1, then nth bit (most significant bit) is set to 1. n>=1. |
140 | // function randTruePrime_(ans,k) //do ans = a random k-bit true random prime (not just probable prime) with 1 in the msb. | 137 | // function randTruePrime_(ans,k) //do ans = a random k-bit true random prime (not just probable prime) with 1 in the msb. |
141 | // function squareMod_(x,n) //do x=x*x mod n for bigInts x,n | 138 | // function squareMod_(x,n) //do x=x*x mod n for bigInts x,n |
142 | // function sub_(x,y) //do x=x-y for bigInts x and y. Negative answers will be 2s complement. | 139 | // function sub_(x,y) //do x=x-y for bigInts x and y. Negative answers will be 2s complement. |
143 | // function subShift_(x,y,ys) //do x=x-(y<<(ys*bpe)). Negative answers will be 2s complement. | 140 | // function subShift_(x,y,ys) //do x=x-(y<<(ys*bpe)). Negative answers will be 2s complement. |
144 | // | 141 | // |
145 | // The following functions are based on algorithms from the _Handbook of Applied Cryptography_ | 142 | // The following functions are based on algorithms from the _Handbook of Applied Cryptography_ |
146 | // powMod_() = algorithm 14.94, Montgomery exponentiation | 143 | // powMod_() = algorithm 14.94, Montgomery exponentiation |
147 | // eGCD_,inverseMod_() = algorithm 14.61, Binary extended GCD_ | 144 | // eGCD_,inverseMod_() = algorithm 14.61, Binary extended GCD_ |
148 | // GCD_() = algorothm 14.57, Lehmer's algorithm | 145 | // GCD_() = algorothm 14.57, Lehmer's algorithm |
149 | // mont_() = algorithm 14.36, Montgomery multiplication | 146 | // mont_() = algorithm 14.36, Montgomery multiplication |
150 | // divide_() = algorithm 14.20 Multiple-precision division | 147 | // divide_() = algorithm 14.20 Multiple-precision division |
151 | // squareMod_() = algorithm 14.16 Multiple-precision squaring | 148 | // squareMod_() = algorithm 14.16 Multiple-precision squaring |
152 | // randTruePrime_() = algorithm 4.62, Maurer's algorithm | 149 | // randTruePrime_() = algorithm 4.62, Maurer's algorithm |
153 | // millerRabin() = algorithm 4.24, Miller-Rabin algorithm | 150 | // millerRabin() = algorithm 4.24, Miller-Rabin algorithm |
154 | // | 151 | // |
155 | // Profiling shows: | 152 | // Profiling shows: |
156 | // randTruePrime_() spends: | 153 | // randTruePrime_() spends: |
157 | // 10% of its time in calls to powMod_() | 154 | // 10% of its time in calls to powMod_() |
158 | // 85% of its time in calls to millerRabin() | 155 | // 85% of its time in calls to millerRabin() |
159 | // millerRabin() spends: | 156 | // millerRabin() spends: |
160 | // 99% of its time in calls to powMod_() (always with a base of 2) | 157 | // 99% of its time in calls to powMod_() (always with a base of 2) |
161 | // powMod_() spends: | 158 | // powMod_() spends: |
162 | // 94% of its time in calls to mont_() (almost always with x==y) | 159 | // 94% of its time in calls to mont_() (almost always with x==y) |
163 | // | 160 | // |
164 | // This suggests there are several ways to speed up this library slightly: | 161 | // This suggests there are several ways to speed up this library slightly: |
165 | // - convert powMod_ to use a Montgomery form of k-ary window (or maybe a Montgomery form of sliding window) | 162 | // - convert powMod_ to use a Montgomery form of k-ary window (or maybe a Montgomery form of sliding window) |
166 | // -- this should especially focus on being fast when raising 2 to a power mod n | 163 | // -- this should especially focus on being fast when raising 2 to a power mod n |
167 | // - convert randTruePrime_() to use a minimum r of 1/3 instead of 1/2 with the appropriate change to the test | 164 | // - convert randTruePrime_() to use a minimum r of 1/3 instead of 1/2 with the appropriate change to the test |
168 | // - tune the parameters in randTruePrime_(), including c, m, and recLimit | 165 | // - tune the parameters in randTruePrime_(), including c, m, and recLimit |
169 | // - speed up the single loop in mont_() that takes 95% of the runtime, perhaps by reducing checking | 166 | // - speed up the single loop in mont_() that takes 95% of the runtime, perhaps by reducing checking |
170 | // within the loop when all the parameters are the same length. | 167 | // within the loop when all the parameters are the same length. |
171 | // | 168 | // |
172 | // There are several ideas that look like they wouldn't help much at all: | 169 | // There are several ideas that look like they wouldn't help much at all: |
173 | // - replacing trial division in randTruePrime_() with a sieve (that speeds up something taking almost no time anyway) | 170 | // - replacing trial division in randTruePrime_() with a sieve (that speeds up something taking almost no time anyway) |
174 | // - increase bpe from 15 to 30 (that would help if we had a 32*32->64 multiplier, but not with JavaScript's 32*32->32) | 171 | // - increase bpe from 15 to 30 (that would help if we had a 32*32->64 multiplier, but not with JavaScript's 32*32->32) |
175 | // - speeding up mont_(x,y,n,np) when x==y by doing a non-modular, non-Montgomery square | 172 | // - speeding up mont_(x,y,n,np) when x==y by doing a non-modular, non-Montgomery square |
176 | // followed by a Montgomery reduction. The intermediate answer will be twice as long as x, so that | 173 | // followed by a Montgomery reduction. The intermediate answer will be twice as long as x, so that |
177 | // method would be slower. This is unfortunate because the code currently spends almost all of its time | 174 | // method would be slower. This is unfortunate because the code currently spends almost all of its time |
178 | // doing mont_(x,x,...), both for randTruePrime_() and powMod_(). A faster method for Montgomery squaring | 175 | // doing mont_(x,x,...), both for randTruePrime_() and powMod_(). A faster method for Montgomery squaring |
179 | // would have a large impact on the speed of randTruePrime_() and powMod_(). HAC has a couple of poorly-worded | 176 | // would have a large impact on the speed of randTruePrime_() and powMod_(). HAC has a couple of poorly-worded |
180 | // sentences that seem to imply it's faster to do a non-modular square followed by a single | 177 | // sentences that seem to imply it's faster to do a non-modular square followed by a single |
181 | // Montgomery reduction, but that's obviously wrong. | 178 | // Montgomery reduction, but that's obviously wrong. |
182 | //////////////////////////////////////////////////////////////////////////////////////// | 179 | //////////////////////////////////////////////////////////////////////////////////////// |
183 | 180 | ||
184 | //globals | 181 | //globals |
185 | bpe=0; //bits stored per array element | 182 | bpe=0; //bits stored per array element |
186 | mask=0; //AND this with an array element to chop it down to bpe bits | 183 | mask=0; //AND this with an array element to chop it down to bpe bits |
187 | radix=mask+1; //equals 2^bpe. A single 1 bit to the left of the last bit of mask. | 184 | radix=mask+1; //equals 2^bpe. A single 1 bit to the left of the last bit of mask. |
188 | 185 | ||
189 | //the digits for converting to different bases | 186 | //the digits for converting to different bases |
190 | digitsStr='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=!@#$%^&*()[]{}|;:,.<>/?`~ \\\'\"+-'; | 187 | digitsStr='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=!@#$%^&*()[]{}|;:,.<>/?`~ \\\'\"+-'; |
191 | 188 | ||
192 | //initialize the global variables | 189 | //initialize the global variables |
193 | for (bpe=0; (1<<(bpe+1)) > (1<<bpe); bpe++); //bpe=number of bits in the mantissa on this platform | 190 | for (bpe=0; (1<<(bpe+1)) > (1<<bpe); bpe++); //bpe=number of bits in the mantissa on this platform |
194 | bpe>>=1; //bpe=number of bits in one element of the array representing the bigInt | 191 | bpe>>=1; //bpe=number of bits in one element of the array representing the bigInt |
195 | mask=(1<<bpe)-1; //AND the mask with an integer to get its bpe least significant bits | 192 | mask=(1<<bpe)-1; //AND the mask with an integer to get its bpe least significant bits |
196 | radix=mask+1; //2^bpe. a single 1 bit to the left of the first bit of mask | 193 | radix=mask+1; //2^bpe. a single 1 bit to the left of the first bit of mask |
197 | one=int2bigInt(1,1,1); //constant used in powMod_() | 194 | one=int2bigInt(1,1,1); //constant used in powMod_() |
198 | 195 | ||
199 | //the following global variables are scratchpad memory to | 196 | //the following global variables are scratchpad memory to |
200 | //reduce dynamic memory allocation in the inner loop | 197 | //reduce dynamic memory allocation in the inner loop |
201 | t=new Array(0); | 198 | t=new Array(0); |
202 | ss=t; //used in mult_() | 199 | ss=t; //used in mult_() |
203 | s0=t; //used in multMod_(), squareMod_() | 200 | s0=t; //used in multMod_(), squareMod_() |
204 | s1=t; //used in powMod_(), multMod_(), squareMod_() | 201 | s1=t; //used in powMod_(), multMod_(), squareMod_() |
205 | s2=t; //used in powMod_(), multMod_() | 202 | s2=t; //used in powMod_(), multMod_() |
206 | s3=t; //used in powMod_() | 203 | s3=t; //used in powMod_() |
207 | s4=t; s5=t; //used in mod_() | 204 | s4=t; s5=t; //used in mod_() |
208 | s6=t; //used in bigInt2str() | 205 | s6=t; //used in bigInt2str() |
209 | s7=t; //used in powMod_() | 206 | s7=t; //used in powMod_() |
210 | T=t; //used in GCD_() | 207 | T=t; //used in GCD_() |
211 | sa=t; //used in mont_() | 208 | sa=t; //used in mont_() |
212 | mr_x1=t; mr_r=t; mr_a=t; //used in millerRabin() | 209 | mr_x1=t; mr_r=t; mr_a=t; //used in millerRabin() |
213 | eg_v=t; eg_u=t; eg_A=t; eg_B=t; eg_C=t; eg_D=t; //used in eGCD_(), inverseMod_() | 210 | eg_v=t; eg_u=t; eg_A=t; eg_B=t; eg_C=t; eg_D=t; //used in eGCD_(), inverseMod_() |
214 | md_q1=t; md_q2=t; md_q3=t; md_r=t; md_r1=t; md_r2=t; md_tt=t; //used in mod_() | 211 | md_q1=t; md_q2=t; md_q3=t; md_r=t; md_r1=t; md_r2=t; md_tt=t; //used in mod_() |
215 | 212 | ||
216 | primes=t; pows=t; s_i=t; s_i2=t; s_R=t; s_rm=t; s_q=t; s_n1=t; | 213 | primes=t; pows=t; s_i=t; s_i2=t; s_R=t; s_rm=t; s_q=t; s_n1=t; |