Diffstat (limited to 'frontend/gamma/js/Clipperz/Crypto/BigInt_scoped.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/gamma/js/Clipperz/Crypto/BigInt_scoped.js | 15 |
1 files changed, 6 insertions, 9 deletions
diff --git a/frontend/gamma/js/Clipperz/Crypto/BigInt_scoped.js b/frontend/gamma/js/Clipperz/Crypto/BigInt_scoped.js index e91e823..f91c7e9 100644 --- a/frontend/gamma/js/Clipperz/Crypto/BigInt_scoped.js +++ b/frontend/gamma/js/Clipperz/Crypto/BigInt_scoped.js | |||
@@ -1,152 +1,149 @@ | |||
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 | if (typeof(Leemon) == 'undefined') { Leemon = {}; } | 29 | if (typeof(Leemon) == 'undefined') { Leemon = {}; } |
33 | if (typeof(Baird.Crypto) == 'undefined') { Baird.Crypto = {}; } | 30 | if (typeof(Baird.Crypto) == 'undefined') { Baird.Crypto = {}; } |
34 | if (typeof(Baird.Crypto.BigInt) == 'undefined') { Baird.Crypto.BigInt = {}; } | 31 | if (typeof(Baird.Crypto.BigInt) == 'undefined') { Baird.Crypto.BigInt = {}; } |
35 | 32 | ||
36 | 33 | ||
37 | //############################################################################# | 34 | //############################################################################# |
38 | //Downloaded on March 05, 2007 from http://www.leemon.com/crypto/BigInt.js | 35 | //Downloaded on March 05, 2007 from http://www.leemon.com/crypto/BigInt.js |
39 | //############################################################################# | 36 | //############################################################################# |
40 | 37 | ||
41 | //////////////////////////////////////////////////////////////////////////////////////// | 38 | //////////////////////////////////////////////////////////////////////////////////////// |
42 | // Big Integer Library v. 5.0 | 39 | // Big Integer Library v. 5.0 |
43 | // Created 2000, last modified 2006 | 40 | // Created 2000, last modified 2006 |
44 | // Leemon Baird | 41 | // Leemon Baird |
45 | // www.leemon.com | 42 | // www.leemon.com |
46 | // | 43 | // |
47 | // This file is public domain. You can use it for any purpose without restriction. | 44 | // This file is public domain. You can use it for any purpose without restriction. |
48 | // I do not guarantee that it is correct, so use it at your own risk. If you use | 45 | // I do not guarantee that it is correct, so use it at your own risk. If you use |
49 | // it for something interesting, I'd appreciate hearing about it. If you find | 46 | // it for something interesting, I'd appreciate hearing about it. If you find |
50 | // any bugs or make any improvements, I'd appreciate hearing about those too. | 47 | // any bugs or make any improvements, I'd appreciate hearing about those too. |
51 | // It would also be nice if my name and address were left in the comments. | 48 | // It would also be nice if my name and address were left in the comments. |
52 | // But none of that is required. | 49 | // But none of that is required. |
53 | // | 50 | // |
54 | // This code defines a bigInt library for arbitrary-precision integers. | 51 | // This code defines a bigInt library for arbitrary-precision integers. |
55 | // A bigInt is an array of integers storing the value in chunks of bpe bits, | 52 | // A bigInt is an array of integers storing the value in chunks of bpe bits, |
56 | // little endian (buff[0] is the least significant word). | 53 | // little endian (buff[0] is the least significant word). |
57 | // Negative bigInts are stored two's complement. | 54 | // Negative bigInts are stored two's complement. |
58 | // Some functions assume their parameters have at least one leading zero element. | 55 | // Some functions assume their parameters have at least one leading zero element. |
59 | // Functions with an underscore at the end of the name have unpredictable behavior in case of overflow, | 56 | // Functions with an underscore at the end of the name have unpredictable behavior in case of overflow, |
60 | // so the caller must make sure overflow won't happen. | 57 | // so the caller must make sure overflow won't happen. |
61 | // For each function where a parameter is modified, that same | 58 | // For each function where a parameter is modified, that same |
62 | // variable must not be used as another argument too. | 59 | // variable must not be used as another argument too. |
63 | // So, you cannot square x by doing multMod_(x,x,n). | 60 | // So, you cannot square x by doing multMod_(x,x,n). |
64 | // You must use squareMod_(x,n) instead, or do y=dup(x); multMod_(x,y,n). | 61 | // You must use squareMod_(x,n) instead, or do y=dup(x); multMod_(x,y,n). |
65 | // | 62 | // |
66 | // These functions are designed to avoid frequent dynamic memory allocation in the inner loop. | 63 | // These functions are designed to avoid frequent dynamic memory allocation in the inner loop. |
67 | // For most functions, if it needs a BigInt as a local variable it will actually use | 64 | // For most functions, if it needs a BigInt as a local variable it will actually use |
68 | // a global, and will only allocate to it when it's not the right size. This ensures | 65 | // a global, and will only allocate to it when it's not the right size. This ensures |
69 | // that when a function is called repeatedly with same-sized parameters, it only allocates | 66 | // that when a function is called repeatedly with same-sized parameters, it only allocates |
70 | // memory on the first call. | 67 | // memory on the first call. |
71 | // | 68 | // |
72 | // Note that for cryptographic purposes, the calls to Math.random() must | 69 | // Note that for cryptographic purposes, the calls to Math.random() must |
73 | // be replaced with calls to a better pseudorandom number generator. | 70 | // be replaced with calls to a better pseudorandom number generator. |
74 | // | 71 | // |
75 | // In the following, "bigInt" means a bigInt with at least one leading zero element, | 72 | // In the following, "bigInt" means a bigInt with at least one leading zero element, |
76 | // and "integer" means a nonnegative integer less than radix. In some cases, integer | 73 | // and "integer" means a nonnegative integer less than radix. In some cases, integer |
77 | // can be negative. Negative bigInts are 2s complement. | 74 | // can be negative. Negative bigInts are 2s complement. |
78 | // | 75 | // |
79 | // The following functions do not modify their inputs, but dynamically allocate memory every time they are called: | 76 | // The following functions do not modify their inputs, but dynamically allocate memory every time they are called: |
80 | // | 77 | // |
81 | // function bigInt2str(x,base) //convert a bigInt into a string in a given base, from base 2 up to base 95 | 78 | // function bigInt2str(x,base) //convert a bigInt into a string in a given base, from base 2 up to base 95 |
82 | // function dup(x) //returns a copy of bigInt x | 79 | // function dup(x) //returns a copy of bigInt x |
83 | // function findPrimes(n) //return array of all primes less than integer n | 80 | // function findPrimes(n) //return array of all primes less than integer n |
84 | // function int2bigInt(t,n,m) //convert integer t to a bigInt with at least n bits and m array elements | 81 | // function int2bigInt(t,n,m) //convert integer t to a bigInt with at least n bits and m array elements |
85 | // function str2bigInt(s,b,n,m) //convert string s in base b to a bigInt with at least n bits and m array elements | 82 | // function str2bigInt(s,b,n,m) //convert string s in base b to a bigInt with at least n bits and m array elements |
86 | // function trim(x,k) //return a copy of x with exactly k leading zero elements | 83 | // function trim(x,k) //return a copy of x with exactly k leading zero elements |
87 | // | 84 | // |
88 | // The following functions do not modify their inputs, so there is never a problem with the result being too big: | 85 | // The following functions do not modify their inputs, so there is never a problem with the result being too big: |
89 | // | 86 | // |
90 | // function bitSize(x) //returns how many bits long the bigInt x is, not counting leading zeros | 87 | // function bitSize(x) //returns how many bits long the bigInt x is, not counting leading zeros |
91 | // function equals(x,y) //is the bigInt x equal to the bigint y? | 88 | // function equals(x,y) //is the bigInt x equal to the bigint y? |
92 | // function equalsInt(x,y) //is bigint x equal to integer y? | 89 | // function equalsInt(x,y) //is bigint x equal to integer y? |
93 | // function greater(x,y) //is x>y? (x and y are nonnegative bigInts) | 90 | // function greater(x,y) //is x>y? (x and y are nonnegative bigInts) |
94 | // function greaterShift(x,y,shift)//is (x <<(shift*bpe)) > y? | 91 | // function greaterShift(x,y,shift)//is (x <<(shift*bpe)) > y? |
95 | // function isZero(x) //is the bigInt x equal to zero? | 92 | // function isZero(x) //is the bigInt x equal to zero? |
96 | // 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 millerRabin(x,b) //does one round of Miller-Rabin base integer b say that bigInt x is possibly prime (as opposed to definitely composite)? |
97 | // function modInt(x,n) //return x mod n for bigInt x and integer n. | 94 | // function modInt(x,n) //return x mod n for bigInt x and integer n. |
98 | // function negative(x) //is bigInt x negative? | 95 | // function negative(x) //is bigInt x negative? |
99 | // | 96 | // |
100 | // The following functions do not modify their inputs, but allocate memory and call functions with underscores | 97 | // The following functions do not modify their inputs, but allocate memory and call functions with underscores |
101 | // | 98 | // |
102 | // function add(x,y) //return (x+y) for bigInts x and y. | 99 | // function add(x,y) //return (x+y) for bigInts x and y. |
103 | // function addInt(x,n) //return (x+n) where x is a bigInt and n is an integer. | 100 | // function addInt(x,n) //return (x+n) where x is a bigInt and n is an integer. |
104 | // function expand(x,n) //return a copy of x with at least n elements, adding leading zeros if needed | 101 | // function expand(x,n) //return a copy of x with at least n elements, adding leading zeros if needed |
105 | // function inverseMod(x,n) //return (x**(-1) mod n) for bigInts x and n. If no inverse exists, it returns null | 102 | // function inverseMod(x,n) //return (x**(-1) mod n) for bigInts x and n. If no inverse exists, it returns null |
106 | // function mod(x,n) //return a new bigInt equal to (x mod n) for bigInts x and n. | 103 | // function mod(x,n) //return a new bigInt equal to (x mod n) for bigInts x and n. |
107 | // function mult(x,y) //return x*y for bigInts x and y. This is faster when y<x. | 104 | // function mult(x,y) //return x*y for bigInts x and y. This is faster when y<x. |
108 | // function multMod(x,y,n) //return (x*y mod n) for bigInts x,y,n. For greater speed, let y<x. | 105 | // function multMod(x,y,n) //return (x*y mod n) for bigInts x,y,n. For greater speed, let y<x. |
109 | // 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 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. |
110 | // function randTruePrime(k) //return a new, random, k-bit, true prime using Maurer's algorithm. | 107 | // function randTruePrime(k) //return a new, random, k-bit, true prime using Maurer's algorithm. |
111 | // function sub(x,y) //return (x-y) for bigInts x and y. Negative answers will be 2s complement | 108 | // function sub(x,y) //return (x-y) for bigInts x and y. Negative answers will be 2s complement |
112 | // | 109 | // |
113 | // The following functions write a bigInt result to one of the parameters, but | 110 | // The following functions write a bigInt result to one of the parameters, but |
114 | // the result is never bigger than the original, so there can't be overflow problems: | 111 | // the result is never bigger than the original, so there can't be overflow problems: |
115 | // | 112 | // |
116 | // function divInt_(x,n) //do x=floor(x/n) for bigInt x and integer n, and return the remainder | 113 | // function divInt_(x,n) //do x=floor(x/n) for bigInt x and integer n, and return the remainder |
117 | // function GCD_(x,y) //set x to the greatest common divisor of bigInts x and y, (y is destroyed). | 114 | // function GCD_(x,y) //set x to the greatest common divisor of bigInts x and y, (y is destroyed). |
118 | // function halve_(x) //do x=floor(|x|/2)*sgn(x) for bigInt x in 2's complement | 115 | // function halve_(x) //do x=floor(|x|/2)*sgn(x) for bigInt x in 2's complement |
119 | // function mod_(x,n) //do x=x mod n for bigInts x and n. | 116 | // function mod_(x,n) //do x=x mod n for bigInts x and n. |
120 | // function rightShift_(x,n) //right shift bigInt x by n bits. 0 <= n < bpe. | 117 | // function rightShift_(x,n) //right shift bigInt x by n bits. 0 <= n < bpe. |
121 | // | 118 | // |
122 | // The following functions write a bigInt result to one of the parameters. The caller is responsible for | 119 | // The following functions write a bigInt result to one of the parameters. The caller is responsible for |
123 | // ensuring it is large enough to hold the result. | 120 | // ensuring it is large enough to hold the result. |
124 | // | 121 | // |
125 | // function addInt_(x,n) //do x=x+n where x is a bigInt and n is an integer | 122 | // function addInt_(x,n) //do x=x+n where x is a bigInt and n is an integer |
126 | // function add_(x,y) //do x=x+y for bigInts x and y | 123 | // function add_(x,y) //do x=x+y for bigInts x and y |
127 | // function addShift_(x,y,ys) //do x=x+(y<<(ys*bpe)) | 124 | // function addShift_(x,y,ys) //do x=x+(y<<(ys*bpe)) |
128 | // function copy_(x,y) //do x=y on bigInts x and y | 125 | // function copy_(x,y) //do x=y on bigInts x and y |
129 | // function copyInt_(x,n) //do x=n on bigInt x and integer n | 126 | // function copyInt_(x,n) //do x=n on bigInt x and integer n |
130 | // function carry_(x) //do carries and borrows so each element of the bigInt x fits in bpe bits. | 127 | // function carry_(x) //do carries and borrows so each element of the bigInt x fits in bpe bits. |
131 | // function divide_(x,y,q,r) //divide_ x by y giving quotient q and remainder r | 128 | // function divide_(x,y,q,r) //divide_ x by y giving quotient q and remainder r |
132 | // 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 eGCD_(x,y,d,a,b) //sets a,b,d to positive big integers such that d = GCD_(x,y) = a*x-b*y |
133 | // 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 inverseMod_(x,n) //do x=x**(-1) mod n, for bigInts x and n. Returns 1 (0) if inverse does (doesn't) exist |
134 | // function inverseModInt_(x,n) //return x**(-1) mod n, for integers x and n. Return 0 if there is no inverse | 131 | // function inverseModInt_(x,n) //return x**(-1) mod n, for integers x and n. Return 0 if there is no inverse |
135 | // function leftShift_(x,n) //left shift bigInt x by n bits. n<bpe. | 132 | // function leftShift_(x,n) //left shift bigInt x by n bits. n<bpe. |
136 | // function linComb_(x,y,a,b) //do x=a*x+b*y for bigInts x and y and integers a and b | 133 | // function linComb_(x,y,a,b) //do x=a*x+b*y for bigInts x and y and integers a and b |
137 | // 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 linCombShift_(x,y,b,ys) //do x=x+b*(y<<(ys*bpe)) for bigInts x and y, and integers b and ys |
138 | // function mont_(x,y,n,np) //Montgomery multiplication (see comments where the function is defined) | 135 | // function mont_(x,y,n,np) //Montgomery multiplication (see comments where the function is defined) |
139 | // function mult_(x,y) //do x=x*y for bigInts x and y. | 136 | // function mult_(x,y) //do x=x*y for bigInts x and y. |
140 | // function multInt_(x,n) //do x=x*n where x is a bigInt and n is an integer. | 137 | // function multInt_(x,n) //do x=x*n where x is a bigInt and n is an integer. |
141 | // function multMod_(x,y,n) //do x=x*y mod n for bigInts x,y,n. | 138 | // function multMod_(x,y,n) //do x=x*y mod n for bigInts x,y,n. |
142 | // 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 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. |
143 | // 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 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. |
144 | // function randTruePrime_(ans,k) //do ans = a random k-bit true random prime (not just probable prime) with 1 in the msb. | 141 | // function randTruePrime_(ans,k) //do ans = a random k-bit true random prime (not just probable prime) with 1 in the msb. |
145 | // function squareMod_(x,n) //do x=x*x mod n for bigInts x,n | 142 | // function squareMod_(x,n) //do x=x*x mod n for bigInts x,n |
146 | // function sub_(x,y) //do x=x-y for bigInts x and y. Negative answers will be 2s complement. | 143 | // function sub_(x,y) //do x=x-y for bigInts x and y. Negative answers will be 2s complement. |
147 | // function subShift_(x,y,ys) //do x=x-(y<<(ys*bpe)). Negative answers will be 2s complement. | 144 | // function subShift_(x,y,ys) //do x=x-(y<<(ys*bpe)). Negative answers will be 2s complement. |
148 | // | 145 | // |
149 | // The following functions are based on algorithms from the _Handbook of Applied Cryptography_ | 146 | // The following functions are based on algorithms from the _Handbook of Applied Cryptography_ |
150 | // powMod_() = algorithm 14.94, Montgomery exponentiation | 147 | // powMod_() = algorithm 14.94, Montgomery exponentiation |
151 | // eGCD_,inverseMod_() = algorithm 14.61, Binary extended GCD_ | 148 | // eGCD_,inverseMod_() = algorithm 14.61, Binary extended GCD_ |
152 | // GCD_() = algorothm 14.57, Lehmer's algorithm | 149 | // GCD_() = algorothm 14.57, Lehmer's algorithm |