Diffstat (limited to 'frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/FiniteField.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/FiniteField.js | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/FiniteField.js b/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/FiniteField.js index 650b479..de1e6a8 100644 --- a/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/FiniteField.js +++ b/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/FiniteField.js | |||
@@ -1,214 +1,212 @@ | |||
1 | /* | 1 | /* |
2 | 2 | ||
3 | Copyright 2008-2011 Clipperz Srl | 3 | Copyright 2008-2013 Clipperz Srl |
4 | 4 | ||
5 | This file is part of Clipperz Community Edition. | 5 | This file is part of Clipperz, the online password manager. |
6 | Clipperz Community Edition is an online password manager. | ||
7 | For further information about its features and functionalities please | 6 | For further information about its features and functionalities please |
8 | refer to http://www.clipperz.com. | 7 | refer to http://www.clipperz.com. |
9 | 8 | ||
10 | * Clipperz Community Edition is free software: you can redistribute | 9 | * Clipperz is free software: you can redistribute it and/or modify it |
11 | it and/or modify it under the terms of the GNU Affero General Public | 10 | under the terms of the GNU Affero General Public License as published |
12 | License as published by the Free Software Foundation, either version | 11 | by the Free Software Foundation, either version 3 of the License, or |
13 | 3 of the License, or (at your option) any later version. | 12 | (at your option) any later version. |
14 | 13 | ||
15 | * Clipperz Community Edition is distributed in the hope that it will | 14 | * Clipperz is distributed in the hope that it will be useful, but |
16 | be useful, but WITHOUT ANY WARRANTY; without even the implied | 15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. | 17 | See the GNU Affero General Public License for more details. |
19 | 18 | ||
20 | * You should have received a copy of the GNU Affero General Public | 19 | * You should have received a copy of the GNU Affero General Public |
21 | License along with Clipperz Community Edition. If not, see | 20 | License along with Clipperz. If not, see http://www.gnu.org/licenses/. |
22 | <http://www.gnu.org/licenses/>. | ||
23 | 21 | ||
24 | */ | 22 | */ |
25 | 23 | ||
26 | try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) { | 24 | try { if (typeof(Clipperz.ByteArray) == 'undefined') { throw ""; }} catch (e) { |
27 | throw "Clipperz.Crypto.ECC depends on Clipperz.ByteArray!"; | 25 | throw "Clipperz.Crypto.ECC depends on Clipperz.ByteArray!"; |
28 | } | 26 | } |
29 | if (typeof(Clipperz.Crypto.ECC) == 'undefined') { Clipperz.Crypto.ECC = {}; } | 27 | if (typeof(Clipperz.Crypto.ECC) == 'undefined') { Clipperz.Crypto.ECC = {}; } |
30 | if (typeof(Clipperz.Crypto.ECC.BinaryField) == 'undefined') { Clipperz.Crypto.ECC.BinaryField = {}; } | 28 | if (typeof(Clipperz.Crypto.ECC.BinaryField) == 'undefined') { Clipperz.Crypto.ECC.BinaryField = {}; } |
31 | 29 | ||
32 | Clipperz.Crypto.ECC.BinaryField.FiniteField = function(args) { | 30 | Clipperz.Crypto.ECC.BinaryField.FiniteField = function(args) { |
33 | args = args || {}; | 31 | args = args || {}; |
34 | this._modulus = args.modulus; | 32 | this._modulus = args.modulus; |
35 | 33 | ||
36 | return this; | 34 | return this; |
37 | } | 35 | } |
38 | 36 | ||
39 | Clipperz.Crypto.ECC.BinaryField.FiniteField.prototype = MochiKit.Base.update(null, { | 37 | Clipperz.Crypto.ECC.BinaryField.FiniteField.prototype = MochiKit.Base.update(null, { |
40 | 38 | ||
41 | 'asString': function() { | 39 | 'asString': function() { |
42 | return "Clipperz.Crypto.ECC.BinaryField.FiniteField (" + this.modulus().asString() + ")"; | 40 | return "Clipperz.Crypto.ECC.BinaryField.FiniteField (" + this.modulus().asString() + ")"; |
43 | }, | 41 | }, |
44 | 42 | ||
45 | //----------------------------------------------------------------------------- | 43 | //----------------------------------------------------------------------------- |
46 | 44 | ||
47 | 'modulus': function() { | 45 | 'modulus': function() { |
48 | return this._modulus; | 46 | return this._modulus; |
49 | }, | 47 | }, |
50 | 48 | ||
51 | //----------------------------------------------------------------------------- | 49 | //----------------------------------------------------------------------------- |
52 | 50 | ||
53 | '_module': function(aValue) { | 51 | '_module': function(aValue) { |
54 | varresult; | 52 | varresult; |
55 | var modulusComparison; | 53 | var modulusComparison; |
56 | //console.log(">>> binaryField.finiteField.(standard)module"); | 54 | //console.log(">>> binaryField.finiteField.(standard)module"); |
57 | 55 | ||
58 | modulusComparison = Clipperz.Crypto.ECC.BinaryField.Value._compare(aValue, this.modulus()._value); | 56 | modulusComparison = Clipperz.Crypto.ECC.BinaryField.Value._compare(aValue, this.modulus()._value); |
59 | 57 | ||
60 | if (modulusComparison < 0) { | 58 | if (modulusComparison < 0) { |
61 | result = aValue; | 59 | result = aValue; |
62 | } else if (modulusComparison == 0) { | 60 | } else if (modulusComparison == 0) { |
63 | result = [0]; | 61 | result = [0]; |
64 | } else { | 62 | } else { |
65 | var modulusBitSize; | 63 | var modulusBitSize; |
66 | var resultBitSize; | 64 | var resultBitSize; |
67 | 65 | ||
68 | result = aValue; | 66 | result = aValue; |
69 | 67 | ||
70 | modulusBitSize = this.modulus().bitSize(); | 68 | modulusBitSize = this.modulus().bitSize(); |
71 | resultBitSize = Clipperz.Crypto.ECC.BinaryField.Value._bitSize(result); | 69 | resultBitSize = Clipperz.Crypto.ECC.BinaryField.Value._bitSize(result); |
72 | while (resultBitSize >= modulusBitSize) { | 70 | while (resultBitSize >= modulusBitSize) { |
73 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor(result, Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft(this.modulus()._value, resultBitSize - modulusBitSize)); | 71 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor(result, Clipperz.Crypto.ECC.BinaryField.Value._shiftLeft(this.modulus()._value, resultBitSize - modulusBitSize)); |
74 | resultBitSize = Clipperz.Crypto.ECC.BinaryField.Value._bitSize(result); | 72 | resultBitSize = Clipperz.Crypto.ECC.BinaryField.Value._bitSize(result); |
75 | } | 73 | } |
76 | } | 74 | } |
77 | //console.log("<<< binaryField.finiteField.(standard)module"); | 75 | //console.log("<<< binaryField.finiteField.(standard)module"); |
78 | 76 | ||
79 | return result; | 77 | return result; |
80 | }, | 78 | }, |
81 | 79 | ||
82 | 'module': function(aValue) { | 80 | 'module': function(aValue) { |
83 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._module(aValue._value.slice(0))); | 81 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._module(aValue._value.slice(0))); |
84 | }, | 82 | }, |
85 | 83 | ||
86 | //----------------------------------------------------------------------------- | 84 | //----------------------------------------------------------------------------- |
87 | 85 | ||
88 | '_add': function(a, b) { | 86 | '_add': function(a, b) { |
89 | return Clipperz.Crypto.ECC.BinaryField.Value._xor(a, b); | 87 | return Clipperz.Crypto.ECC.BinaryField.Value._xor(a, b); |
90 | }, | 88 | }, |
91 | 89 | ||
92 | '_overwriteAdd': function(a, b) { | 90 | '_overwriteAdd': function(a, b) { |
93 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor(a, b); | 91 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor(a, b); |
94 | }, | 92 | }, |
95 | 93 | ||
96 | 'add': function(a, b) { | 94 | 'add': function(a, b) { |
97 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._add(a._value, b._value)); | 95 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._add(a._value, b._value)); |
98 | }, | 96 | }, |
99 | 97 | ||
100 | //----------------------------------------------------------------------------- | 98 | //----------------------------------------------------------------------------- |
101 | 99 | ||
102 | 'negate': function(aValue) { | 100 | 'negate': function(aValue) { |
103 | return aValue.clone(); | 101 | return aValue.clone(); |
104 | }, | 102 | }, |
105 | 103 | ||
106 | //----------------------------------------------------------------------------- | 104 | //----------------------------------------------------------------------------- |
107 | 105 | ||
108 | '_multiply': function(a, b) { | 106 | '_multiply': function(a, b) { |
109 | var result; | 107 | var result; |
110 | var valueToXor; | 108 | var valueToXor; |
111 | var i,c; | 109 | var i,c; |
112 | 110 | ||
113 | result = [0]; | 111 | result = [0]; |
114 | valueToXor = b; | 112 | valueToXor = b; |
115 | c = Clipperz.Crypto.ECC.BinaryField.Value._bitSize(a); | 113 | c = Clipperz.Crypto.ECC.BinaryField.Value._bitSize(a); |
116 | for (i=0; i<c; i++) { | 114 | for (i=0; i<c; i++) { |
117 | if (Clipperz.Crypto.ECC.BinaryField.Value._isBitSet(a, i) === true) { | 115 | if (Clipperz.Crypto.ECC.BinaryField.Value._isBitSet(a, i) === true) { |
118 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor(result, valueToXor); | 116 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor(result, valueToXor); |
119 | } | 117 | } |
120 | valueToXor = Clipperz.Crypto.ECC.BinaryField.Value._overwriteShiftLeft(valueToXor, 1); | 118 | valueToXor = Clipperz.Crypto.ECC.BinaryField.Value._overwriteShiftLeft(valueToXor, 1); |
121 | } | 119 | } |
122 | result = this._module(result); | 120 | result = this._module(result); |
123 | 121 | ||
124 | return result; | 122 | return result; |
125 | }, | 123 | }, |
126 | 124 | ||
127 | 'multiply': function(a, b) { | 125 | 'multiply': function(a, b) { |
128 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._multiply(a._value, b._value)); | 126 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._multiply(a._value, b._value)); |
129 | }, | 127 | }, |
130 | 128 | ||
131 | //----------------------------------------------------------------------------- | 129 | //----------------------------------------------------------------------------- |
132 | 130 | ||
133 | '_fastMultiply': function(a, b) { | 131 | '_fastMultiply': function(a, b) { |
134 | var result; | 132 | var result; |
135 | var B; | 133 | var B; |
136 | var i,c; | 134 | var i,c; |
137 | 135 | ||
138 | result = [0]; | 136 | result = [0]; |
139 | B = b.slice(0); //Is this array copy avoidable? | 137 | B = b.slice(0); //Is this array copy avoidable? |
140 | c = 32; | 138 | c = 32; |
141 | for (i=0; i<c; i++) { | 139 | for (i=0; i<c; i++) { |
142 | var ii, cc; | 140 | var ii, cc; |
143 | 141 | ||
144 | cc = a.length; | 142 | cc = a.length; |
145 | for (ii=0; ii<cc; ii++) { | 143 | for (ii=0; ii<cc; ii++) { |
146 | if (((a[ii] >>> i) & 0x01) == 1) { | 144 | if (((a[ii] >>> i) & 0x01) == 1) { |
147 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor(result, B, ii); | 145 | Clipperz.Crypto.ECC.BinaryField.Value._overwriteXor(result, B, ii); |
148 | } | 146 | } |
149 | } | 147 | } |
150 | 148 | ||
151 | if (i < (c-1)) { | 149 | if (i < (c-1)) { |
152 | B = Clipperz.Crypto.ECC.BinaryField.Value._overwriteShiftLeft(B, 1); | 150 | B = Clipperz.Crypto.ECC.BinaryField.Value._overwriteShiftLeft(B, 1); |
153 | } | 151 | } |
154 | } | 152 | } |
155 | result = this._module(result); | 153 | result = this._module(result); |
156 | 154 | ||
157 | return result; | 155 | return result; |
158 | }, | 156 | }, |
159 | 157 | ||
160 | 'fastMultiply': function(a, b) { | 158 | 'fastMultiply': function(a, b) { |
161 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._fastMultiply(a._value, b._value)); | 159 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._fastMultiply(a._value, b._value)); |
162 | }, | 160 | }, |
163 | 161 | ||
164 | //----------------------------------------------------------------------------- | 162 | //----------------------------------------------------------------------------- |
165 | // | 163 | // |
166 | //Guide to Elliptic Curve Cryptography | 164 | //Guide to Elliptic Curve Cryptography |
167 | //Darrel Hankerson, Alfred Menezes, Scott Vanstone | 165 | //Darrel Hankerson, Alfred Menezes, Scott Vanstone |
168 | //- Pag: 49, Alorithm 2.34 | 166 | //- Pag: 49, Alorithm 2.34 |
169 | // | 167 | // |
170 | //----------------------------------------------------------------------------- | 168 | //----------------------------------------------------------------------------- |
171 | 169 | ||
172 | '_square': function(aValue) { | 170 | '_square': function(aValue) { |
173 | var result; | 171 | var result; |
174 | var value; | 172 | var value; |
175 | var c,i; | 173 | var c,i; |
176 | var precomputedValues; | 174 | var precomputedValues; |
177 | 175 | ||
178 | value = aValue; | 176 | value = aValue; |
179 | result = new Array(value.length * 2); | 177 | result = new Array(value.length * 2); |
180 | precomputedValues = Clipperz.Crypto.ECC.BinaryField.FiniteField.squarePrecomputedBytes; | 178 | precomputedValues = Clipperz.Crypto.ECC.BinaryField.FiniteField.squarePrecomputedBytes; |
181 | 179 | ||
182 | c = value.length; | 180 | c = value.length; |
183 | for (i=0; i<c; i++) { | 181 | for (i=0; i<c; i++) { |
184 | result[i*2] = precomputedValues[(value[i] & 0x000000ff)]; | 182 | result[i*2] = precomputedValues[(value[i] & 0x000000ff)]; |
185 | result[i*2] |= ((precomputedValues[(value[i] & 0x0000ff00) >>> 8]) << 16); | 183 | result[i*2] |= ((precomputedValues[(value[i] & 0x0000ff00) >>> 8]) << 16); |
186 | 184 | ||
187 | result[i*2 + 1] = precomputedValues[(value[i] & 0x00ff0000) >>> 16]; | 185 | result[i*2 + 1] = precomputedValues[(value[i] & 0x00ff0000) >>> 16]; |
188 | result[i*2 + 1] |= ((precomputedValues[(value[i] & 0xff000000) >>> 24]) << 16); | 186 | result[i*2 + 1] |= ((precomputedValues[(value[i] & 0xff000000) >>> 24]) << 16); |
189 | } | 187 | } |
190 | 188 | ||
191 | return this._module(result); | 189 | return this._module(result); |
192 | }, | 190 | }, |
193 | 191 | ||
194 | 'square': function(aValue) { | 192 | 'square': function(aValue) { |
195 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._square(aValue._value)); | 193 | return new Clipperz.Crypto.ECC.BinaryField.Value(this._square(aValue._value)); |
196 | }, | 194 | }, |
197 | 195 | ||
198 | //----------------------------------------------------------------------------- | 196 | //----------------------------------------------------------------------------- |
199 | 197 | ||
200 | '_inverse': function(aValue) { | 198 | '_inverse': function(aValue) { |
201 | varresult; | 199 | varresult; |
202 | var b, c; | 200 | var b, c; |
203 | var u, v; | 201 | var u, v; |
204 | 202 | ||
205 | // b = Clipperz.Crypto.ECC.BinaryField.Value.I._value; | 203 | // b = Clipperz.Crypto.ECC.BinaryField.Value.I._value; |
206 | b = [1]; | 204 | b = [1]; |
207 | // c = Clipperz.Crypto.ECC.BinaryField.Value.O._value; | 205 | // c = Clipperz.Crypto.ECC.BinaryField.Value.O._value; |
208 | c = [0]; | 206 | c = [0]; |
209 | u = this._module(aValue); | 207 | u = this._module(aValue); |
210 | v = this.modulus()._value.slice(0); | 208 | v = this.modulus()._value.slice(0); |
211 | 209 | ||
212 | while (Clipperz.Crypto.ECC.BinaryField.Value._bitSize(u) > 1) { | 210 | while (Clipperz.Crypto.ECC.BinaryField.Value._bitSize(u) > 1) { |
213 | varbitDifferenceSize; | 211 | varbitDifferenceSize; |
214 | 212 | ||