Diffstat (limited to 'frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Curve.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Curve.js | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Curve.js b/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Curve.js index 01127c3..c39a075 100644 --- a/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Curve.js +++ b/frontend/beta/js/Clipperz/Crypto/ECC/BinaryField/Curve.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.Curve = function(args) { | 30 | Clipperz.Crypto.ECC.BinaryField.Curve = function(args) { |
33 | args = args || {}; | 31 | args = args || {}; |
34 | 32 | ||
35 | this._modulus = args.modulus; | 33 | this._modulus = args.modulus; |
36 | 34 | ||
37 | this._a = args.a; | 35 | this._a = args.a; |
38 | this._b = args.b; | 36 | this._b = args.b; |
39 | this._G = args.G; | 37 | this._G = args.G; |
40 | this._r = args.r; | 38 | this._r = args.r; |
41 | this._h = args.h; | 39 | this._h = args.h; |
42 | 40 | ||
43 | this._finiteField = null; | 41 | this._finiteField = null; |
44 | 42 | ||
45 | return this; | 43 | return this; |
46 | } | 44 | } |
47 | 45 | ||
48 | Clipperz.Crypto.ECC.BinaryField.Curve.prototype = MochiKit.Base.update(null, { | 46 | Clipperz.Crypto.ECC.BinaryField.Curve.prototype = MochiKit.Base.update(null, { |
49 | 47 | ||
50 | 'asString': function() { | 48 | 'asString': function() { |
51 | return "Clipperz.Crypto.ECC.BinaryField.Curve"; | 49 | return "Clipperz.Crypto.ECC.BinaryField.Curve"; |
52 | }, | 50 | }, |
53 | 51 | ||
54 | //----------------------------------------------------------------------------- | 52 | //----------------------------------------------------------------------------- |
55 | 53 | ||
56 | 'modulus': function() { | 54 | 'modulus': function() { |
57 | return this._modulus; | 55 | return this._modulus; |
58 | }, | 56 | }, |
59 | 57 | ||
60 | 'a': function() { | 58 | 'a': function() { |
61 | return this._a; | 59 | return this._a; |
62 | }, | 60 | }, |
63 | 61 | ||
64 | 'b': function() { | 62 | 'b': function() { |
65 | return this._b; | 63 | return this._b; |
66 | }, | 64 | }, |
67 | 65 | ||
68 | 'G': function() { | 66 | 'G': function() { |
69 | return this._G; | 67 | return this._G; |
70 | }, | 68 | }, |
71 | 69 | ||
72 | 'r': function() { | 70 | 'r': function() { |
73 | return this._r; | 71 | return this._r; |
74 | }, | 72 | }, |
75 | 73 | ||
76 | 'h': function() { | 74 | 'h': function() { |
77 | return this._h; | 75 | return this._h; |
78 | }, | 76 | }, |
79 | 77 | ||
80 | //----------------------------------------------------------------------------- | 78 | //----------------------------------------------------------------------------- |
81 | 79 | ||
82 | 'finiteField': function() { | 80 | 'finiteField': function() { |
83 | if (this._finiteField == null) { | 81 | if (this._finiteField == null) { |
84 | this._finiteField = new Clipperz.Crypto.ECC.BinaryField.FiniteField({modulus:this.modulus()}) | 82 | this._finiteField = new Clipperz.Crypto.ECC.BinaryField.FiniteField({modulus:this.modulus()}) |
85 | } | 83 | } |
86 | 84 | ||
87 | return this._finiteField; | 85 | return this._finiteField; |
88 | }, | 86 | }, |
89 | 87 | ||
90 | //----------------------------------------------------------------------------- | 88 | //----------------------------------------------------------------------------- |
91 | 89 | ||
92 | 'negate': function(aPointA) { | 90 | 'negate': function(aPointA) { |
93 | var result; | 91 | var result; |
94 | 92 | ||
95 | result = new Clipperz.Crypto.ECC.Point({x:aPointA.x(), y:this.finiteField().add(aPointA.y(), aPointA.x())}) | 93 | result = new Clipperz.Crypto.ECC.Point({x:aPointA.x(), y:this.finiteField().add(aPointA.y(), aPointA.x())}) |
96 | 94 | ||
97 | return result; | 95 | return result; |
98 | }, | 96 | }, |
99 | 97 | ||
100 | //----------------------------------------------------------------------------- | 98 | //----------------------------------------------------------------------------- |
101 | 99 | ||
102 | 'add': function(aPointA, aPointB) { | 100 | 'add': function(aPointA, aPointB) { |
103 | var result; | 101 | var result; |
104 | 102 | ||
105 | //console.log(">>> ECC.BinaryField.Curve.add"); | 103 | //console.log(">>> ECC.BinaryField.Curve.add"); |
106 | if (aPointA.isZero()) { | 104 | if (aPointA.isZero()) { |
107 | //console.log("--- pointA == zero"); | 105 | //console.log("--- pointA == zero"); |
108 | result = aPointB; | 106 | result = aPointB; |
109 | } else if (aPointB.isZero()) { | 107 | } else if (aPointB.isZero()) { |
110 | //console.log("--- pointB == zero"); | 108 | //console.log("--- pointB == zero"); |
111 | result = aPointA; | 109 | result = aPointA; |
112 | } else if ((aPointA.x().compare(aPointB.x()) == 0) && ((aPointA.y().compare(aPointB.y()) != 0) || aPointB.x().isZero())) { | 110 | } else if ((aPointA.x().compare(aPointB.x()) == 0) && ((aPointA.y().compare(aPointB.y()) != 0) || aPointB.x().isZero())) { |
113 | //console.log("compare A.x - B.x: ", aPointA.x().compare(aPointB.x())); | 111 | //console.log("compare A.x - B.x: ", aPointA.x().compare(aPointB.x())); |
114 | //console.log("compare A.y - B.y: ", (aPointA.y().compare(aPointB.y()) != 0)); | 112 | //console.log("compare A.y - B.y: ", (aPointA.y().compare(aPointB.y()) != 0)); |
115 | //console.log("compare B.x.isZero(): ", aPointB.x().isZero()); | 113 | //console.log("compare B.x.isZero(): ", aPointB.x().isZero()); |
116 | 114 | ||
117 | //console.log("--- result = zero"); | 115 | //console.log("--- result = zero"); |
118 | result = new Clipperz.Crypto.ECC.BinaryField.Point({x:Clipperz.Crypto.ECC.BinaryField.Value.O, y:Clipperz.Crypto.ECC.BinaryField.Value.O}); | 116 | result = new Clipperz.Crypto.ECC.BinaryField.Point({x:Clipperz.Crypto.ECC.BinaryField.Value.O, y:Clipperz.Crypto.ECC.BinaryField.Value.O}); |
119 | } else { | 117 | } else { |
120 | //console.log("--- result = ELSE"); | 118 | //console.log("--- result = ELSE"); |
121 | varf2m; | 119 | varf2m; |
122 | var x, y; | 120 | var x, y; |
123 | var lambda; | 121 | var lambda; |
124 | var aX, aY, bX, bY; | 122 | var aX, aY, bX, bY; |
125 | 123 | ||
126 | aX = aPointA.x()._value; | 124 | aX = aPointA.x()._value; |
127 | aY = aPointA.y()._value; | 125 | aY = aPointA.y()._value; |
128 | bX = aPointB.x()._value; | 126 | bX = aPointB.x()._value; |
129 | bY = aPointB.y()._value; | 127 | bY = aPointB.y()._value; |
130 | 128 | ||
131 | f2m = this.finiteField(); | 129 | f2m = this.finiteField(); |
132 | 130 | ||
133 | if (aPointA.x().compare(aPointB.x()) != 0) { | 131 | if (aPointA.x().compare(aPointB.x()) != 0) { |
134 | //console.log(" a.x != b.x"); | 132 | //console.log(" a.x != b.x"); |
135 | lambda =f2m._fastMultiply( | 133 | lambda =f2m._fastMultiply( |
136 | f2m._add(aY, bY), | 134 | f2m._add(aY, bY), |
137 | f2m._inverse(f2m._add(aX, bX)) | 135 | f2m._inverse(f2m._add(aX, bX)) |
138 | ); | 136 | ); |
139 | x = f2m._add(this.a()._value, f2m._square(lambda)); | 137 | x = f2m._add(this.a()._value, f2m._square(lambda)); |
140 | f2m._overwriteAdd(x, lambda); | 138 | f2m._overwriteAdd(x, lambda); |
141 | f2m._overwriteAdd(x, aX); | 139 | f2m._overwriteAdd(x, aX); |
142 | f2m._overwriteAdd(x, bX); | 140 | f2m._overwriteAdd(x, bX); |
143 | } else { | 141 | } else { |
144 | //console.log(" a.x == b.x"); | 142 | //console.log(" a.x == b.x"); |
145 | lambda = f2m._add(bX, f2m._fastMultiply(bY, f2m._inverse(bX))); | 143 | lambda = f2m._add(bX, f2m._fastMultiply(bY, f2m._inverse(bX))); |
146 | //console.log(" lambda: " + lambda.asString(16)); | 144 | //console.log(" lambda: " + lambda.asString(16)); |
147 | x = f2m._add(this.a()._value, f2m._square(lambda)); | 145 | x = f2m._add(this.a()._value, f2m._square(lambda)); |
148 | //console.log(" x (step 1): " + x.asString(16)); | 146 | //console.log(" x (step 1): " + x.asString(16)); |
149 | f2m._overwriteAdd(x, lambda); | 147 | f2m._overwriteAdd(x, lambda); |
150 | //console.log(" x (step 2): " + x.asString(16)); | 148 | //console.log(" x (step 2): " + x.asString(16)); |
151 | } | 149 | } |
152 | 150 | ||
153 | y = f2m._fastMultiply(f2m._add(bX, x), lambda); | 151 | y = f2m._fastMultiply(f2m._add(bX, x), lambda); |
154 | //console.log(" y (step 1): " + y.asString(16)); | 152 | //console.log(" y (step 1): " + y.asString(16)); |
155 | f2m._overwriteAdd(y, x); | 153 | f2m._overwriteAdd(y, x); |
156 | //console.log(" y (step 2): " + y.asString(16)); | 154 | //console.log(" y (step 2): " + y.asString(16)); |
157 | f2m._overwriteAdd(y, bY); | 155 | f2m._overwriteAdd(y, bY); |
158 | //console.log(" y (step 3): " + y.asString(16)); | 156 | //console.log(" y (step 3): " + y.asString(16)); |
159 | 157 | ||
160 | result = new Clipperz.Crypto.ECC.BinaryField.Point({x:new Clipperz.Crypto.ECC.BinaryField.Value(x), y:new Clipperz.Crypto.ECC.BinaryField.Value(y)}) | 158 | result = new Clipperz.Crypto.ECC.BinaryField.Point({x:new Clipperz.Crypto.ECC.BinaryField.Value(x), y:new Clipperz.Crypto.ECC.BinaryField.Value(y)}) |
161 | } | 159 | } |
162 | //console.log("<<< ECC.BinaryField.Curve.add"); | 160 | //console.log("<<< ECC.BinaryField.Curve.add"); |
163 | 161 | ||
164 | return result; | 162 | return result; |
165 | }, | 163 | }, |
166 | 164 | ||
167 | //----------------------------------------------------------------------------- | 165 | //----------------------------------------------------------------------------- |
168 | 166 | ||
169 | 'overwriteAdd': function(aPointA, aPointB) { | 167 | 'overwriteAdd': function(aPointA, aPointB) { |
170 | if (aPointA.isZero()) { | 168 | if (aPointA.isZero()) { |
171 | // result = aPointB; | 169 | // result = aPointB; |
172 | aPointA._x._value = aPointB._x._value; | 170 | aPointA._x._value = aPointB._x._value; |
173 | aPointA._y._value = aPointB._y._value; | 171 | aPointA._y._value = aPointB._y._value; |
174 | } else if (aPointB.isZero()) { | 172 | } else if (aPointB.isZero()) { |
175 | // result = aPointA; | 173 | // result = aPointA; |
176 | } else if ((aPointA.x().compare(aPointB.x()) == 0) && ((aPointA.y().compare(aPointB.y()) != 0) || aPointB.x().isZero())) { | 174 | } else if ((aPointA.x().compare(aPointB.x()) == 0) && ((aPointA.y().compare(aPointB.y()) != 0) || aPointB.x().isZero())) { |
177 | // result = new Clipperz.Crypto.ECC.BinaryField.Point({x:Clipperz.Crypto.ECC.BinaryField.Value.O, y:Clipperz.Crypto.ECC.BinaryField.Value.O}); | 175 | // result = new Clipperz.Crypto.ECC.BinaryField.Point({x:Clipperz.Crypto.ECC.BinaryField.Value.O, y:Clipperz.Crypto.ECC.BinaryField.Value.O}); |
178 | aPointA._x = Clipperz.Crypto.ECC.BinaryField.Value.O; | 176 | aPointA._x = Clipperz.Crypto.ECC.BinaryField.Value.O; |
179 | aPointA._y = Clipperz.Crypto.ECC.BinaryField.Value.O; | 177 | aPointA._y = Clipperz.Crypto.ECC.BinaryField.Value.O; |
180 | } else { | 178 | } else { |
181 | varf2m; | 179 | varf2m; |
182 | var x, y; | 180 | var x, y; |
183 | var lambda; | 181 | var lambda; |
184 | var aX, aY, bX, bY; | 182 | var aX, aY, bX, bY; |
185 | 183 | ||
186 | aX = aPointA.x()._value; | 184 | aX = aPointA.x()._value; |
187 | aY = aPointA.y()._value; | 185 | aY = aPointA.y()._value; |
188 | bX = aPointB.x()._value; | 186 | bX = aPointB.x()._value; |
189 | bY = aPointB.y()._value; | 187 | bY = aPointB.y()._value; |
190 | 188 | ||
191 | f2m = this.finiteField(); | 189 | f2m = this.finiteField(); |
192 | 190 | ||
193 | if (aPointA.x().compare(aPointB.x()) != 0) { | 191 | if (aPointA.x().compare(aPointB.x()) != 0) { |
194 | //console.log(" a.x != b.x"); | 192 | //console.log(" a.x != b.x"); |
195 | lambda =f2m._fastMultiply( | 193 | lambda =f2m._fastMultiply( |
196 | f2m._add(aY, bY), | 194 | f2m._add(aY, bY), |
197 | f2m._inverse(f2m._add(aX, bX)) | 195 | f2m._inverse(f2m._add(aX, bX)) |
198 | ); | 196 | ); |
199 | x = f2m._add(this.a()._value, f2m._square(lambda)); | 197 | x = f2m._add(this.a()._value, f2m._square(lambda)); |
200 | f2m._overwriteAdd(x, lambda); | 198 | f2m._overwriteAdd(x, lambda); |
201 | f2m._overwriteAdd(x, aX); | 199 | f2m._overwriteAdd(x, aX); |
202 | f2m._overwriteAdd(x, bX); | 200 | f2m._overwriteAdd(x, bX); |
203 | } else { | 201 | } else { |
204 | //console.log(" a.x == b.x"); | 202 | //console.log(" a.x == b.x"); |
205 | lambda = f2m._add(bX, f2m._fastMultiply(bY, f2m._inverse(bX))); | 203 | lambda = f2m._add(bX, f2m._fastMultiply(bY, f2m._inverse(bX))); |
206 | //console.log(" lambda: " + lambda.asString(16)); | 204 | //console.log(" lambda: " + lambda.asString(16)); |
207 | x = f2m._add(this.a()._value, f2m._square(lambda)); | 205 | x = f2m._add(this.a()._value, f2m._square(lambda)); |
208 | //console.log(" x (step 1): " + x.asString(16)); | 206 | //console.log(" x (step 1): " + x.asString(16)); |
209 | f2m._overwriteAdd(x, lambda); | 207 | f2m._overwriteAdd(x, lambda); |
210 | //console.log(" x (step 2): " + x.asString(16)); | 208 | //console.log(" x (step 2): " + x.asString(16)); |
211 | } | 209 | } |
212 | 210 | ||
213 | y = f2m._fastMultiply(f2m._add(bX, x), lambda); | 211 | y = f2m._fastMultiply(f2m._add(bX, x), lambda); |
214 | //console.log(" y (step 1): " + y.asString(16)); | 212 | //console.log(" y (step 1): " + y.asString(16)); |