ccxt-look 1.81.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (264) hide show
  1. package/.cache/eslintcache +1 -0
  2. package/.dockerignore +6 -0
  3. package/.eslintignore +1 -0
  4. package/.gitattributes +5 -0
  5. package/.readthedocs.yaml +16 -0
  6. package/CONTRIBUTING.md +1049 -0
  7. package/LICENSE.txt +21 -0
  8. package/README.md +537 -0
  9. package/SECURITY.md +5 -0
  10. package/build/cleanup-old-tags.js +94 -0
  11. package/build/countries.js +256 -0
  12. package/build/export-exchanges.js +520 -0
  13. package/build/fs.js +51 -0
  14. package/build/transpile.js +1772 -0
  15. package/build/vss.js +78 -0
  16. package/ccxt.browser.js +7 -0
  17. package/ccxt.d.ts +692 -0
  18. package/ccxt.js +171 -0
  19. package/cleanup.sh +2 -0
  20. package/composer-install.sh +20 -0
  21. package/dist/ccxt.browser.js +208383 -0
  22. package/gource.sh +3 -0
  23. package/index.html +7 -0
  24. package/js/.eslintrc +87 -0
  25. package/js/aax.js +2686 -0
  26. package/js/ascendex.js +2584 -0
  27. package/js/base/.eslintrc.js +43 -0
  28. package/js/base/Exchange.js +2371 -0
  29. package/js/base/Precise.js +283 -0
  30. package/js/base/errorHierarchy.js +47 -0
  31. package/js/base/errors.js +55 -0
  32. package/js/base/functions/crypto.js +158 -0
  33. package/js/base/functions/encode.js +118 -0
  34. package/js/base/functions/generic.js +270 -0
  35. package/js/base/functions/misc.js +138 -0
  36. package/js/base/functions/number.js +329 -0
  37. package/js/base/functions/platform.js +38 -0
  38. package/js/base/functions/string.js +21 -0
  39. package/js/base/functions/throttle.js +79 -0
  40. package/js/base/functions/time.js +210 -0
  41. package/js/base/functions/type.js +66 -0
  42. package/js/base/functions.js +28 -0
  43. package/js/bequant.js +32 -0
  44. package/js/bibox.js +1407 -0
  45. package/js/bigone.js +1366 -0
  46. package/js/binance.js +5652 -0
  47. package/js/binancecoinm.js +46 -0
  48. package/js/binanceus.js +46 -0
  49. package/js/binanceusdm.js +49 -0
  50. package/js/bit2c.js +535 -0
  51. package/js/bitbank.js +842 -0
  52. package/js/bitbay.js +16 -0
  53. package/js/bitbns.js +1073 -0
  54. package/js/bitcoincom.js +15 -0
  55. package/js/bitfinex.js +1433 -0
  56. package/js/bitfinex2.js +2025 -0
  57. package/js/bitflyer.js +840 -0
  58. package/js/bitforex.js +614 -0
  59. package/js/bitget.js +2397 -0
  60. package/js/bithumb.js +980 -0
  61. package/js/bitmart.js +2516 -0
  62. package/js/bitmex.js +1809 -0
  63. package/js/bitopro.js +1443 -0
  64. package/js/bitpanda.js +1782 -0
  65. package/js/bitrue.js +1747 -0
  66. package/js/bitso.js +1062 -0
  67. package/js/bitstamp.js +1757 -0
  68. package/js/bitstamp1.js +343 -0
  69. package/js/bittrex.js +1876 -0
  70. package/js/bitvavo.js +1579 -0
  71. package/js/bkex.js +1233 -0
  72. package/js/bl3p.js +346 -0
  73. package/js/blockchaincom.js +969 -0
  74. package/js/btcalpha.js +680 -0
  75. package/js/btcbox.js +477 -0
  76. package/js/btcmarkets.js +1022 -0
  77. package/js/btctradeua.js +466 -0
  78. package/js/btcturk.js +734 -0
  79. package/js/buda.js +946 -0
  80. package/js/bw.js +1265 -0
  81. package/js/bybit.js +3372 -0
  82. package/js/bytetrade.js +1336 -0
  83. package/js/cdax.js +1646 -0
  84. package/js/cex.js +1410 -0
  85. package/js/coinbase.js +1342 -0
  86. package/js/coinbaseprime.js +31 -0
  87. package/js/coinbasepro.js +1466 -0
  88. package/js/coincheck.js +755 -0
  89. package/js/coinex.js +3400 -0
  90. package/js/coinfalcon.js +880 -0
  91. package/js/coinmate.js +794 -0
  92. package/js/coinone.js +816 -0
  93. package/js/coinspot.js +345 -0
  94. package/js/crex24.js +1636 -0
  95. package/js/cryptocom.js +1832 -0
  96. package/js/currencycom.js +1748 -0
  97. package/js/delta.js +1547 -0
  98. package/js/deribit.js +2148 -0
  99. package/js/digifinex.js +1585 -0
  100. package/js/eqonex.js +1660 -0
  101. package/js/exmo.js +1670 -0
  102. package/js/fairdesk.js +1231 -0
  103. package/js/flowbtc.js +35 -0
  104. package/js/fmfwio.js +34 -0
  105. package/js/ftx.js +2751 -0
  106. package/js/ftxus.js +38 -0
  107. package/js/gateio.js +4174 -0
  108. package/js/gemini.js +1397 -0
  109. package/js/hitbtc.js +1343 -0
  110. package/js/hitbtc3.js +2329 -0
  111. package/js/hollaex.js +1486 -0
  112. package/js/huobi.js +5706 -0
  113. package/js/huobijp.js +1710 -0
  114. package/js/huobipro.js +18 -0
  115. package/js/idex.js +1439 -0
  116. package/js/independentreserve.js +649 -0
  117. package/js/indodax.js +742 -0
  118. package/js/itbit.js +722 -0
  119. package/js/kraken.js +2179 -0
  120. package/js/kucoin.js +2571 -0
  121. package/js/kucoinfutures.js +1771 -0
  122. package/js/kuna.js +809 -0
  123. package/js/latoken.js +1445 -0
  124. package/js/lbank.js +760 -0
  125. package/js/liquid.js +1432 -0
  126. package/js/luno.js +873 -0
  127. package/js/lykke.js +1147 -0
  128. package/js/mercado.js +771 -0
  129. package/js/mexc.js +3151 -0
  130. package/js/ndax.js +2233 -0
  131. package/js/novadax.js +1318 -0
  132. package/js/oceanex.js +816 -0
  133. package/js/okcoin.js +3841 -0
  134. package/js/okex.js +16 -0
  135. package/js/okex5.js +16 -0
  136. package/js/okx.js +4795 -0
  137. package/js/paymium.js +498 -0
  138. package/js/phemex.js +2957 -0
  139. package/js/poloniex.js +1674 -0
  140. package/js/probit.js +1346 -0
  141. package/js/qtrade.js +1588 -0
  142. package/js/ripio.js +1061 -0
  143. package/js/static_dependencies/BN/bn.js +3526 -0
  144. package/js/static_dependencies/README.md +1 -0
  145. package/js/static_dependencies/crypto-js/crypto-js.js +5988 -0
  146. package/js/static_dependencies/elliptic/lib/elliptic/curve/base.js +375 -0
  147. package/js/static_dependencies/elliptic/lib/elliptic/curve/edwards.js +433 -0
  148. package/js/static_dependencies/elliptic/lib/elliptic/curve/index.js +8 -0
  149. package/js/static_dependencies/elliptic/lib/elliptic/curve/mont.js +180 -0
  150. package/js/static_dependencies/elliptic/lib/elliptic/curve/short.js +938 -0
  151. package/js/static_dependencies/elliptic/lib/elliptic/curves.js +204 -0
  152. package/js/static_dependencies/elliptic/lib/elliptic/ec/index.js +240 -0
  153. package/js/static_dependencies/elliptic/lib/elliptic/ec/key.js +119 -0
  154. package/js/static_dependencies/elliptic/lib/elliptic/ec/signature.js +24 -0
  155. package/js/static_dependencies/elliptic/lib/elliptic/eddsa/index.js +145 -0
  156. package/js/static_dependencies/elliptic/lib/elliptic/eddsa/key.js +100 -0
  157. package/js/static_dependencies/elliptic/lib/elliptic/eddsa/signature.js +65 -0
  158. package/js/static_dependencies/elliptic/lib/elliptic/precomputed/secp256k1.js +780 -0
  159. package/js/static_dependencies/elliptic/lib/elliptic/utils.js +214 -0
  160. package/js/static_dependencies/elliptic/lib/elliptic.js +22 -0
  161. package/js/static_dependencies/elliptic/lib/hmac-drbg/hmac-drbg.js +114 -0
  162. package/js/static_dependencies/fetch-ponyfill/fetch-node.js +39 -0
  163. package/js/static_dependencies/node-fetch/index.js +1564 -0
  164. package/js/static_dependencies/node-rsa/NodeRSA.js +223 -0
  165. package/js/static_dependencies/node-rsa/asn1/ber/errors.js +13 -0
  166. package/js/static_dependencies/node-rsa/asn1/ber/index.js +21 -0
  167. package/js/static_dependencies/node-rsa/asn1/ber/reader.js +262 -0
  168. package/js/static_dependencies/node-rsa/asn1/ber/types.js +36 -0
  169. package/js/static_dependencies/node-rsa/asn1/index.js +17 -0
  170. package/js/static_dependencies/node-rsa/encryptEngines/js.js +34 -0
  171. package/js/static_dependencies/node-rsa/formats/components.js +71 -0
  172. package/js/static_dependencies/node-rsa/formats/formats.js +31 -0
  173. package/js/static_dependencies/node-rsa/formats/pkcs1.js +148 -0
  174. package/js/static_dependencies/node-rsa/formats/pkcs8.js +187 -0
  175. package/js/static_dependencies/node-rsa/libs/jsbn.js +1252 -0
  176. package/js/static_dependencies/node-rsa/libs/rsa.js +147 -0
  177. package/js/static_dependencies/node-rsa/schemes/pkcs1.js +176 -0
  178. package/js/static_dependencies/node-rsa/schemes/schemes.js +21 -0
  179. package/js/static_dependencies/node-rsa/utils.js +98 -0
  180. package/js/static_dependencies/qs/formats.js +18 -0
  181. package/js/static_dependencies/qs/index.js +11 -0
  182. package/js/static_dependencies/qs/parse.js +242 -0
  183. package/js/static_dependencies/qs/stringify.js +269 -0
  184. package/js/static_dependencies/qs/utils.js +230 -0
  185. package/js/stex.js +1925 -0
  186. package/js/test/.eslintrc.js +42 -0
  187. package/js/test/Exchange/test.balance.js +61 -0
  188. package/js/test/Exchange/test.borrowRate.js +32 -0
  189. package/js/test/Exchange/test.currency.js +52 -0
  190. package/js/test/Exchange/test.fetchBalance.js +23 -0
  191. package/js/test/Exchange/test.fetchBorrowInterest.js +59 -0
  192. package/js/test/Exchange/test.fetchBorrowRate.js +32 -0
  193. package/js/test/Exchange/test.fetchBorrowRates.js +28 -0
  194. package/js/test/Exchange/test.fetchClosedOrders.js +32 -0
  195. package/js/test/Exchange/test.fetchCurrencies.js +35 -0
  196. package/js/test/Exchange/test.fetchDeposits.js +31 -0
  197. package/js/test/Exchange/test.fetchFundingFees.js +19 -0
  198. package/js/test/Exchange/test.fetchFundingRateHistory.js +40 -0
  199. package/js/test/Exchange/test.fetchL2OrderBook.js +23 -0
  200. package/js/test/Exchange/test.fetchLedger.js +42 -0
  201. package/js/test/Exchange/test.fetchLeverageTiers.js +33 -0
  202. package/js/test/Exchange/test.fetchMarketLeverageTiers.js +22 -0
  203. package/js/test/Exchange/test.fetchMarkets.js +33 -0
  204. package/js/test/Exchange/test.fetchMyTrades.js +42 -0
  205. package/js/test/Exchange/test.fetchOHLCV.js +46 -0
  206. package/js/test/Exchange/test.fetchOpenOrders.js +36 -0
  207. package/js/test/Exchange/test.fetchOrderBook.js +25 -0
  208. package/js/test/Exchange/test.fetchOrderBooks.js +35 -0
  209. package/js/test/Exchange/test.fetchOrders.js +41 -0
  210. package/js/test/Exchange/test.fetchPositions.js +47 -0
  211. package/js/test/Exchange/test.fetchStatus.js +35 -0
  212. package/js/test/Exchange/test.fetchTicker.js +38 -0
  213. package/js/test/Exchange/test.fetchTickers.js +49 -0
  214. package/js/test/Exchange/test.fetchTrades.js +39 -0
  215. package/js/test/Exchange/test.fetchTradingFee.js +18 -0
  216. package/js/test/Exchange/test.fetchTradingFees.js +22 -0
  217. package/js/test/Exchange/test.fetchTransactions.js +31 -0
  218. package/js/test/Exchange/test.fetchWithdrawals.js +31 -0
  219. package/js/test/Exchange/test.ledgerItem.js +46 -0
  220. package/js/test/Exchange/test.leverageTier.js +33 -0
  221. package/js/test/Exchange/test.loadMarkets.js +35 -0
  222. package/js/test/Exchange/test.market.js +129 -0
  223. package/js/test/Exchange/test.ohlcv.js +33 -0
  224. package/js/test/Exchange/test.order.js +62 -0
  225. package/js/test/Exchange/test.orderbook.js +61 -0
  226. package/js/test/Exchange/test.position.js +21 -0
  227. package/js/test/Exchange/test.throttle.js +94 -0
  228. package/js/test/Exchange/test.ticker.js +95 -0
  229. package/js/test/Exchange/test.trade.js +68 -0
  230. package/js/test/Exchange/test.tradingFee.js +34 -0
  231. package/js/test/Exchange/test.transaction.js +35 -0
  232. package/js/test/base/.eslintrc +38 -0
  233. package/js/test/base/functions/test.crypto.js +110 -0
  234. package/js/test/base/functions/test.datetime.js +62 -0
  235. package/js/test/base/functions/test.generic.js +152 -0
  236. package/js/test/base/functions/test.number.js +362 -0
  237. package/js/test/base/functions/test.time.js +56 -0
  238. package/js/test/base/functions/test.type.js +53 -0
  239. package/js/test/base/test.base.js +193 -0
  240. package/js/test/errors/test.InsufficientFunds.js +86 -0
  241. package/js/test/errors/test.InvalidNonce.js +64 -0
  242. package/js/test/errors/test.InvalidOrder.js +35 -0
  243. package/js/test/errors/test.OrderNotFound.js +39 -0
  244. package/js/test/test.js +426 -0
  245. package/js/test/test.timeout_hang.js +12 -0
  246. package/js/therock.js +1431 -0
  247. package/js/tidebit.js +632 -0
  248. package/js/tidex.js +939 -0
  249. package/js/timex.js +1283 -0
  250. package/js/upbit.js +1622 -0
  251. package/js/vcc.js +1353 -0
  252. package/js/wavesexchange.js +2185 -0
  253. package/js/wazirx.js +732 -0
  254. package/js/whitebit.js +1352 -0
  255. package/js/woo.js +1577 -0
  256. package/js/xena.js +1948 -0
  257. package/js/yobit.js +1129 -0
  258. package/js/zaif.js +647 -0
  259. package/js/zb.js +4088 -0
  260. package/js/zipmex.js +40 -0
  261. package/js/zonda.js +1497 -0
  262. package/multilang.sh +159 -0
  263. package/package.json +591 -0
  264. package/postinstall.js +103 -0
@@ -0,0 +1,938 @@
1
+ 'use strict';
2
+
3
+ var curve = require('../curve');
4
+ var elliptic = require('../../elliptic');
5
+ var BN = require('../../../../BN/bn');
6
+ var inherits = elliptic.inherits;
7
+ var Base = curve.base;
8
+
9
+ var assert = elliptic.utils.assert;
10
+
11
+ function ShortCurve(conf) {
12
+ Base.call(this, 'short', conf);
13
+
14
+ this.a = new BN(conf.a, 16).toRed(this.red);
15
+ this.b = new BN(conf.b, 16).toRed(this.red);
16
+ this.tinv = this.two.redInvm();
17
+
18
+ this.zeroA = this.a.fromRed().cmpn(0) === 0;
19
+ this.threeA = this.a.fromRed().sub(this.p).cmpn(-3) === 0;
20
+
21
+ // If the curve is endomorphic, precalculate beta and lambda
22
+ this.endo = this._getEndomorphism(conf);
23
+ this._endoWnafT1 = new Array(4);
24
+ this._endoWnafT2 = new Array(4);
25
+ }
26
+ inherits(ShortCurve, Base);
27
+ module.exports = ShortCurve;
28
+
29
+ ShortCurve.prototype._getEndomorphism = function _getEndomorphism(conf) {
30
+ // No efficient endomorphism
31
+ if (!this.zeroA || !this.g || !this.n || this.p.modn(3) !== 1)
32
+ return;
33
+
34
+ // Compute beta and lambda, that lambda * P = (beta * Px; Py)
35
+ var beta;
36
+ var lambda;
37
+ if (conf.beta) {
38
+ beta = new BN(conf.beta, 16).toRed(this.red);
39
+ } else {
40
+ var betas = this._getEndoRoots(this.p);
41
+ // Choose the smallest beta
42
+ beta = betas[0].cmp(betas[1]) < 0 ? betas[0] : betas[1];
43
+ beta = beta.toRed(this.red);
44
+ }
45
+ if (conf.lambda) {
46
+ lambda = new BN(conf.lambda, 16);
47
+ } else {
48
+ // Choose the lambda that is matching selected beta
49
+ var lambdas = this._getEndoRoots(this.n);
50
+ if (this.g.mul(lambdas[0]).x.cmp(this.g.x.redMul(beta)) === 0) {
51
+ lambda = lambdas[0];
52
+ } else {
53
+ lambda = lambdas[1];
54
+ assert(this.g.mul(lambda).x.cmp(this.g.x.redMul(beta)) === 0);
55
+ }
56
+ }
57
+
58
+ // Get basis vectors, used for balanced length-two representation
59
+ var basis;
60
+ if (conf.basis) {
61
+ basis = conf.basis.map(function(vec) {
62
+ return {
63
+ a: new BN(vec.a, 16),
64
+ b: new BN(vec.b, 16)
65
+ };
66
+ });
67
+ } else {
68
+ basis = this._getEndoBasis(lambda);
69
+ }
70
+
71
+ return {
72
+ beta: beta,
73
+ lambda: lambda,
74
+ basis: basis
75
+ };
76
+ };
77
+
78
+ ShortCurve.prototype._getEndoRoots = function _getEndoRoots(num) {
79
+ // Find roots of for x^2 + x + 1 in F
80
+ // Root = (-1 +- Sqrt(-3)) / 2
81
+ //
82
+ var red = num === this.p ? this.red : BN.mont(num);
83
+ var tinv = new BN(2).toRed(red).redInvm();
84
+ var ntinv = tinv.redNeg();
85
+
86
+ var s = new BN(3).toRed(red).redNeg().redSqrt().redMul(tinv);
87
+
88
+ var l1 = ntinv.redAdd(s).fromRed();
89
+ var l2 = ntinv.redSub(s).fromRed();
90
+ return [ l1, l2 ];
91
+ };
92
+
93
+ ShortCurve.prototype._getEndoBasis = function _getEndoBasis(lambda) {
94
+ // aprxSqrt >= sqrt(this.n)
95
+ var aprxSqrt = this.n.ushrn(Math.floor(this.n.bitLength() / 2));
96
+
97
+ // 3.74
98
+ // Run EGCD, until r(L + 1) < aprxSqrt
99
+ var u = lambda;
100
+ var v = this.n.clone();
101
+ var x1 = new BN(1);
102
+ var y1 = new BN(0);
103
+ var x2 = new BN(0);
104
+ var y2 = new BN(1);
105
+
106
+ // NOTE: all vectors are roots of: a + b * lambda = 0 (mod n)
107
+ var a0;
108
+ var b0;
109
+ // First vector
110
+ var a1;
111
+ var b1;
112
+ // Second vector
113
+ var a2;
114
+ var b2;
115
+
116
+ var prevR;
117
+ var i = 0;
118
+ var r;
119
+ var x;
120
+ while (u.cmpn(0) !== 0) {
121
+ var q = v.div(u);
122
+ r = v.sub(q.mul(u));
123
+ x = x2.sub(q.mul(x1));
124
+ var y = y2.sub(q.mul(y1));
125
+
126
+ if (!a1 && r.cmp(aprxSqrt) < 0) {
127
+ a0 = prevR.neg();
128
+ b0 = x1;
129
+ a1 = r.neg();
130
+ b1 = x;
131
+ } else if (a1 && ++i === 2) {
132
+ break;
133
+ }
134
+ prevR = r;
135
+
136
+ v = u;
137
+ u = r;
138
+ x2 = x1;
139
+ x1 = x;
140
+ y2 = y1;
141
+ y1 = y;
142
+ }
143
+ a2 = r.neg();
144
+ b2 = x;
145
+
146
+ var len1 = a1.sqr().add(b1.sqr());
147
+ var len2 = a2.sqr().add(b2.sqr());
148
+ if (len2.cmp(len1) >= 0) {
149
+ a2 = a0;
150
+ b2 = b0;
151
+ }
152
+
153
+ // Normalize signs
154
+ if (a1.negative) {
155
+ a1 = a1.neg();
156
+ b1 = b1.neg();
157
+ }
158
+ if (a2.negative) {
159
+ a2 = a2.neg();
160
+ b2 = b2.neg();
161
+ }
162
+
163
+ return [
164
+ { a: a1, b: b1 },
165
+ { a: a2, b: b2 }
166
+ ];
167
+ };
168
+
169
+ ShortCurve.prototype._endoSplit = function _endoSplit(k) {
170
+ var basis = this.endo.basis;
171
+ var v1 = basis[0];
172
+ var v2 = basis[1];
173
+
174
+ var c1 = v2.b.mul(k).divRound(this.n);
175
+ var c2 = v1.b.neg().mul(k).divRound(this.n);
176
+
177
+ var p1 = c1.mul(v1.a);
178
+ var p2 = c2.mul(v2.a);
179
+ var q1 = c1.mul(v1.b);
180
+ var q2 = c2.mul(v2.b);
181
+
182
+ // Calculate answer
183
+ var k1 = k.sub(p1).sub(p2);
184
+ var k2 = q1.add(q2).neg();
185
+ return { k1: k1, k2: k2 };
186
+ };
187
+
188
+ ShortCurve.prototype.pointFromX = function pointFromX(x, odd) {
189
+ x = new BN(x, 16);
190
+ if (!x.red)
191
+ x = x.toRed(this.red);
192
+
193
+ var y2 = x.redSqr().redMul(x).redIAdd(x.redMul(this.a)).redIAdd(this.b);
194
+ var y = y2.redSqrt();
195
+ if (y.redSqr().redSub(y2).cmp(this.zero) !== 0)
196
+ throw new Error('invalid point');
197
+
198
+ // XXX Is there any way to tell if the number is odd without converting it
199
+ // to non-red form?
200
+ var isOdd = y.fromRed().isOdd();
201
+ if (odd && !isOdd || !odd && isOdd)
202
+ y = y.redNeg();
203
+
204
+ return this.point(x, y);
205
+ };
206
+
207
+ ShortCurve.prototype.validate = function validate(point) {
208
+ if (point.inf)
209
+ return true;
210
+
211
+ var x = point.x;
212
+ var y = point.y;
213
+
214
+ var ax = this.a.redMul(x);
215
+ var rhs = x.redSqr().redMul(x).redIAdd(ax).redIAdd(this.b);
216
+ return y.redSqr().redISub(rhs).cmpn(0) === 0;
217
+ };
218
+
219
+ ShortCurve.prototype._endoWnafMulAdd =
220
+ function _endoWnafMulAdd(points, coeffs, jacobianResult) {
221
+ var npoints = this._endoWnafT1;
222
+ var ncoeffs = this._endoWnafT2;
223
+ for (var i = 0; i < points.length; i++) {
224
+ var split = this._endoSplit(coeffs[i]);
225
+ var p = points[i];
226
+ var beta = p._getBeta();
227
+
228
+ if (split.k1.negative) {
229
+ split.k1.ineg();
230
+ p = p.neg(true);
231
+ }
232
+ if (split.k2.negative) {
233
+ split.k2.ineg();
234
+ beta = beta.neg(true);
235
+ }
236
+
237
+ npoints[i * 2] = p;
238
+ npoints[i * 2 + 1] = beta;
239
+ ncoeffs[i * 2] = split.k1;
240
+ ncoeffs[i * 2 + 1] = split.k2;
241
+ }
242
+ var res = this._wnafMulAdd(1, npoints, ncoeffs, i * 2, jacobianResult);
243
+
244
+ // Clean-up references to points and coefficients
245
+ for (var j = 0; j < i * 2; j++) {
246
+ npoints[j] = null;
247
+ ncoeffs[j] = null;
248
+ }
249
+ return res;
250
+ };
251
+
252
+ function Point(curve, x, y, isRed) {
253
+ Base.BasePoint.call(this, curve, 'affine');
254
+ if (x === null && y === null) {
255
+ this.x = null;
256
+ this.y = null;
257
+ this.inf = true;
258
+ } else {
259
+ this.x = new BN(x, 16);
260
+ this.y = new BN(y, 16);
261
+ // Force redgomery representation when loading from JSON
262
+ if (isRed) {
263
+ this.x.forceRed(this.curve.red);
264
+ this.y.forceRed(this.curve.red);
265
+ }
266
+ if (!this.x.red)
267
+ this.x = this.x.toRed(this.curve.red);
268
+ if (!this.y.red)
269
+ this.y = this.y.toRed(this.curve.red);
270
+ this.inf = false;
271
+ }
272
+ }
273
+ inherits(Point, Base.BasePoint);
274
+
275
+ ShortCurve.prototype.point = function point(x, y, isRed) {
276
+ return new Point(this, x, y, isRed);
277
+ };
278
+
279
+ ShortCurve.prototype.pointFromJSON = function pointFromJSON(obj, red) {
280
+ return Point.fromJSON(this, obj, red);
281
+ };
282
+
283
+ Point.prototype._getBeta = function _getBeta() {
284
+ if (!this.curve.endo)
285
+ return;
286
+
287
+ var pre = this.precomputed;
288
+ if (pre && pre.beta)
289
+ return pre.beta;
290
+
291
+ var beta = this.curve.point(this.x.redMul(this.curve.endo.beta), this.y);
292
+ if (pre) {
293
+ var curve = this.curve;
294
+ var endoMul = function(p) {
295
+ return curve.point(p.x.redMul(curve.endo.beta), p.y);
296
+ };
297
+ pre.beta = beta;
298
+ beta.precomputed = {
299
+ beta: null,
300
+ naf: pre.naf && {
301
+ wnd: pre.naf.wnd,
302
+ points: pre.naf.points.map(endoMul)
303
+ },
304
+ doubles: pre.doubles && {
305
+ step: pre.doubles.step,
306
+ points: pre.doubles.points.map(endoMul)
307
+ }
308
+ };
309
+ }
310
+ return beta;
311
+ };
312
+
313
+ Point.prototype.toJSON = function toJSON() {
314
+ if (!this.precomputed)
315
+ return [ this.x, this.y ];
316
+
317
+ return [ this.x, this.y, this.precomputed && {
318
+ doubles: this.precomputed.doubles && {
319
+ step: this.precomputed.doubles.step,
320
+ points: this.precomputed.doubles.points.slice(1)
321
+ },
322
+ naf: this.precomputed.naf && {
323
+ wnd: this.precomputed.naf.wnd,
324
+ points: this.precomputed.naf.points.slice(1)
325
+ }
326
+ } ];
327
+ };
328
+
329
+ Point.fromJSON = function fromJSON(curve, obj, red) {
330
+ if (typeof obj === 'string')
331
+ obj = JSON.parse(obj);
332
+ var res = curve.point(obj[0], obj[1], red);
333
+ if (!obj[2])
334
+ return res;
335
+
336
+ function obj2point(obj) {
337
+ return curve.point(obj[0], obj[1], red);
338
+ }
339
+
340
+ var pre = obj[2];
341
+ res.precomputed = {
342
+ beta: null,
343
+ doubles: pre.doubles && {
344
+ step: pre.doubles.step,
345
+ points: [ res ].concat(pre.doubles.points.map(obj2point))
346
+ },
347
+ naf: pre.naf && {
348
+ wnd: pre.naf.wnd,
349
+ points: [ res ].concat(pre.naf.points.map(obj2point))
350
+ }
351
+ };
352
+ return res;
353
+ };
354
+
355
+ Point.prototype.inspect = function inspect() {
356
+ if (this.isInfinity())
357
+ return '<EC Point Infinity>';
358
+ return '<EC Point x: ' + this.x.fromRed().toString(16, 2) +
359
+ ' y: ' + this.y.fromRed().toString(16, 2) + '>';
360
+ };
361
+
362
+ Point.prototype.isInfinity = function isInfinity() {
363
+ return this.inf;
364
+ };
365
+
366
+ Point.prototype.add = function add(p) {
367
+ // O + P = P
368
+ if (this.inf)
369
+ return p;
370
+
371
+ // P + O = P
372
+ if (p.inf)
373
+ return this;
374
+
375
+ // P + P = 2P
376
+ if (this.eq(p))
377
+ return this.dbl();
378
+
379
+ // P + (-P) = O
380
+ if (this.neg().eq(p))
381
+ return this.curve.point(null, null);
382
+
383
+ // P + Q = O
384
+ if (this.x.cmp(p.x) === 0)
385
+ return this.curve.point(null, null);
386
+
387
+ var c = this.y.redSub(p.y);
388
+ if (c.cmpn(0) !== 0)
389
+ c = c.redMul(this.x.redSub(p.x).redInvm());
390
+ var nx = c.redSqr().redISub(this.x).redISub(p.x);
391
+ var ny = c.redMul(this.x.redSub(nx)).redISub(this.y);
392
+ return this.curve.point(nx, ny);
393
+ };
394
+
395
+ Point.prototype.dbl = function dbl() {
396
+ if (this.inf)
397
+ return this;
398
+
399
+ // 2P = O
400
+ var ys1 = this.y.redAdd(this.y);
401
+ if (ys1.cmpn(0) === 0)
402
+ return this.curve.point(null, null);
403
+
404
+ var a = this.curve.a;
405
+
406
+ var x2 = this.x.redSqr();
407
+ var dyinv = ys1.redInvm();
408
+ var c = x2.redAdd(x2).redIAdd(x2).redIAdd(a).redMul(dyinv);
409
+
410
+ var nx = c.redSqr().redISub(this.x.redAdd(this.x));
411
+ var ny = c.redMul(this.x.redSub(nx)).redISub(this.y);
412
+ return this.curve.point(nx, ny);
413
+ };
414
+
415
+ Point.prototype.getX = function getX() {
416
+ return this.x.fromRed();
417
+ };
418
+
419
+ Point.prototype.getY = function getY() {
420
+ return this.y.fromRed();
421
+ };
422
+
423
+ Point.prototype.mul = function mul(k) {
424
+ k = new BN(k, 16);
425
+
426
+ if (this._hasDoubles(k))
427
+ return this.curve._fixedNafMul(this, k);
428
+ else if (this.curve.endo)
429
+ return this.curve._endoWnafMulAdd([ this ], [ k ]);
430
+ else
431
+ return this.curve._wnafMul(this, k);
432
+ };
433
+
434
+ Point.prototype.mulAdd = function mulAdd(k1, p2, k2) {
435
+ var points = [ this, p2 ];
436
+ var coeffs = [ k1, k2 ];
437
+ if (this.curve.endo)
438
+ return this.curve._endoWnafMulAdd(points, coeffs);
439
+ else
440
+ return this.curve._wnafMulAdd(1, points, coeffs, 2);
441
+ };
442
+
443
+ Point.prototype.jmulAdd = function jmulAdd(k1, p2, k2) {
444
+ var points = [ this, p2 ];
445
+ var coeffs = [ k1, k2 ];
446
+ if (this.curve.endo)
447
+ return this.curve._endoWnafMulAdd(points, coeffs, true);
448
+ else
449
+ return this.curve._wnafMulAdd(1, points, coeffs, 2, true);
450
+ };
451
+
452
+ Point.prototype.eq = function eq(p) {
453
+ return this === p ||
454
+ this.inf === p.inf &&
455
+ (this.inf || this.x.cmp(p.x) === 0 && this.y.cmp(p.y) === 0);
456
+ };
457
+
458
+ Point.prototype.neg = function neg(_precompute) {
459
+ if (this.inf)
460
+ return this;
461
+
462
+ var res = this.curve.point(this.x, this.y.redNeg());
463
+ if (_precompute && this.precomputed) {
464
+ var pre = this.precomputed;
465
+ var negate = function(p) {
466
+ return p.neg();
467
+ };
468
+ res.precomputed = {
469
+ naf: pre.naf && {
470
+ wnd: pre.naf.wnd,
471
+ points: pre.naf.points.map(negate)
472
+ },
473
+ doubles: pre.doubles && {
474
+ step: pre.doubles.step,
475
+ points: pre.doubles.points.map(negate)
476
+ }
477
+ };
478
+ }
479
+ return res;
480
+ };
481
+
482
+ Point.prototype.toJ = function toJ() {
483
+ if (this.inf)
484
+ return this.curve.jpoint(null, null, null);
485
+
486
+ var res = this.curve.jpoint(this.x, this.y, this.curve.one);
487
+ return res;
488
+ };
489
+
490
+ function JPoint(curve, x, y, z) {
491
+ Base.BasePoint.call(this, curve, 'jacobian');
492
+ if (x === null && y === null && z === null) {
493
+ this.x = this.curve.one;
494
+ this.y = this.curve.one;
495
+ this.z = new BN(0);
496
+ } else {
497
+ this.x = new BN(x, 16);
498
+ this.y = new BN(y, 16);
499
+ this.z = new BN(z, 16);
500
+ }
501
+ if (!this.x.red)
502
+ this.x = this.x.toRed(this.curve.red);
503
+ if (!this.y.red)
504
+ this.y = this.y.toRed(this.curve.red);
505
+ if (!this.z.red)
506
+ this.z = this.z.toRed(this.curve.red);
507
+
508
+ this.zOne = this.z === this.curve.one;
509
+ }
510
+ inherits(JPoint, Base.BasePoint);
511
+
512
+ ShortCurve.prototype.jpoint = function jpoint(x, y, z) {
513
+ return new JPoint(this, x, y, z);
514
+ };
515
+
516
+ JPoint.prototype.toP = function toP() {
517
+ if (this.isInfinity())
518
+ return this.curve.point(null, null);
519
+
520
+ var zinv = this.z.redInvm();
521
+ var zinv2 = zinv.redSqr();
522
+ var ax = this.x.redMul(zinv2);
523
+ var ay = this.y.redMul(zinv2).redMul(zinv);
524
+
525
+ return this.curve.point(ax, ay);
526
+ };
527
+
528
+ JPoint.prototype.neg = function neg() {
529
+ return this.curve.jpoint(this.x, this.y.redNeg(), this.z);
530
+ };
531
+
532
+ JPoint.prototype.add = function add(p) {
533
+ // O + P = P
534
+ if (this.isInfinity())
535
+ return p;
536
+
537
+ // P + O = P
538
+ if (p.isInfinity())
539
+ return this;
540
+
541
+ // 12M + 4S + 7A
542
+ var pz2 = p.z.redSqr();
543
+ var z2 = this.z.redSqr();
544
+ var u1 = this.x.redMul(pz2);
545
+ var u2 = p.x.redMul(z2);
546
+ var s1 = this.y.redMul(pz2.redMul(p.z));
547
+ var s2 = p.y.redMul(z2.redMul(this.z));
548
+
549
+ var h = u1.redSub(u2);
550
+ var r = s1.redSub(s2);
551
+ if (h.cmpn(0) === 0) {
552
+ if (r.cmpn(0) !== 0)
553
+ return this.curve.jpoint(null, null, null);
554
+ else
555
+ return this.dbl();
556
+ }
557
+
558
+ var h2 = h.redSqr();
559
+ var h3 = h2.redMul(h);
560
+ var v = u1.redMul(h2);
561
+
562
+ var nx = r.redSqr().redIAdd(h3).redISub(v).redISub(v);
563
+ var ny = r.redMul(v.redISub(nx)).redISub(s1.redMul(h3));
564
+ var nz = this.z.redMul(p.z).redMul(h);
565
+
566
+ return this.curve.jpoint(nx, ny, nz);
567
+ };
568
+
569
+ JPoint.prototype.mixedAdd = function mixedAdd(p) {
570
+ // O + P = P
571
+ if (this.isInfinity())
572
+ return p.toJ();
573
+
574
+ // P + O = P
575
+ if (p.isInfinity())
576
+ return this;
577
+
578
+ // 8M + 3S + 7A
579
+ var z2 = this.z.redSqr();
580
+ var u1 = this.x;
581
+ var u2 = p.x.redMul(z2);
582
+ var s1 = this.y;
583
+ var s2 = p.y.redMul(z2).redMul(this.z);
584
+
585
+ var h = u1.redSub(u2);
586
+ var r = s1.redSub(s2);
587
+ if (h.cmpn(0) === 0) {
588
+ if (r.cmpn(0) !== 0)
589
+ return this.curve.jpoint(null, null, null);
590
+ else
591
+ return this.dbl();
592
+ }
593
+
594
+ var h2 = h.redSqr();
595
+ var h3 = h2.redMul(h);
596
+ var v = u1.redMul(h2);
597
+
598
+ var nx = r.redSqr().redIAdd(h3).redISub(v).redISub(v);
599
+ var ny = r.redMul(v.redISub(nx)).redISub(s1.redMul(h3));
600
+ var nz = this.z.redMul(h);
601
+
602
+ return this.curve.jpoint(nx, ny, nz);
603
+ };
604
+
605
+ JPoint.prototype.dblp = function dblp(pow) {
606
+ if (pow === 0)
607
+ return this;
608
+ if (this.isInfinity())
609
+ return this;
610
+ if (!pow)
611
+ return this.dbl();
612
+
613
+ if (this.curve.zeroA || this.curve.threeA) {
614
+ var r = this;
615
+ for (var i = 0; i < pow; i++)
616
+ r = r.dbl();
617
+ return r;
618
+ }
619
+
620
+ // 1M + 2S + 1A + N * (4S + 5M + 8A)
621
+ // N = 1 => 6M + 6S + 9A
622
+ var a = this.curve.a;
623
+ var tinv = this.curve.tinv;
624
+
625
+ var jx = this.x;
626
+ var jy = this.y;
627
+ var jz = this.z;
628
+ var jz4 = jz.redSqr().redSqr();
629
+
630
+ // Reuse results
631
+ var jyd = jy.redAdd(jy);
632
+ for (var i = 0; i < pow; i++) {
633
+ var jx2 = jx.redSqr();
634
+ var jyd2 = jyd.redSqr();
635
+ var jyd4 = jyd2.redSqr();
636
+ var c = jx2.redAdd(jx2).redIAdd(jx2).redIAdd(a.redMul(jz4));
637
+
638
+ var t1 = jx.redMul(jyd2);
639
+ var nx = c.redSqr().redISub(t1.redAdd(t1));
640
+ var t2 = t1.redISub(nx);
641
+ var dny = c.redMul(t2);
642
+ dny = dny.redIAdd(dny).redISub(jyd4);
643
+ var nz = jyd.redMul(jz);
644
+ if (i + 1 < pow)
645
+ jz4 = jz4.redMul(jyd4);
646
+
647
+ jx = nx;
648
+ jz = nz;
649
+ jyd = dny;
650
+ }
651
+
652
+ return this.curve.jpoint(jx, jyd.redMul(tinv), jz);
653
+ };
654
+
655
+ JPoint.prototype.dbl = function dbl() {
656
+ if (this.isInfinity())
657
+ return this;
658
+
659
+ if (this.curve.zeroA)
660
+ return this._zeroDbl();
661
+ else if (this.curve.threeA)
662
+ return this._threeDbl();
663
+ else
664
+ return this._dbl();
665
+ };
666
+
667
+ JPoint.prototype._zeroDbl = function _zeroDbl() {
668
+ var nx;
669
+ var ny;
670
+ var nz;
671
+ // Z = 1
672
+ if (this.zOne) {
673
+ // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html
674
+ // #doubling-mdbl-2007-bl
675
+ // 1M + 5S + 14A
676
+
677
+ // XX = X1^2
678
+ var xx = this.x.redSqr();
679
+ // YY = Y1^2
680
+ var yy = this.y.redSqr();
681
+ // YYYY = YY^2
682
+ var yyyy = yy.redSqr();
683
+ // S = 2 * ((X1 + YY)^2 - XX - YYYY)
684
+ var s = this.x.redAdd(yy).redSqr().redISub(xx).redISub(yyyy);
685
+ s = s.redIAdd(s);
686
+ // M = 3 * XX + a; a = 0
687
+ var m = xx.redAdd(xx).redIAdd(xx);
688
+ // T = M ^ 2 - 2*S
689
+ var t = m.redSqr().redISub(s).redISub(s);
690
+
691
+ // 8 * YYYY
692
+ var yyyy8 = yyyy.redIAdd(yyyy);
693
+ yyyy8 = yyyy8.redIAdd(yyyy8);
694
+ yyyy8 = yyyy8.redIAdd(yyyy8);
695
+
696
+ // X3 = T
697
+ nx = t;
698
+ // Y3 = M * (S - T) - 8 * YYYY
699
+ ny = m.redMul(s.redISub(t)).redISub(yyyy8);
700
+ // Z3 = 2*Y1
701
+ nz = this.y.redAdd(this.y);
702
+ } else {
703
+ // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html
704
+ // #doubling-dbl-2009-l
705
+ // 2M + 5S + 13A
706
+
707
+ // A = X1^2
708
+ var a = this.x.redSqr();
709
+ // B = Y1^2
710
+ var b = this.y.redSqr();
711
+ // C = B^2
712
+ var c = b.redSqr();
713
+ // D = 2 * ((X1 + B)^2 - A - C)
714
+ var d = this.x.redAdd(b).redSqr().redISub(a).redISub(c);
715
+ d = d.redIAdd(d);
716
+ // E = 3 * A
717
+ var e = a.redAdd(a).redIAdd(a);
718
+ // F = E^2
719
+ var f = e.redSqr();
720
+
721
+ // 8 * C
722
+ var c8 = c.redIAdd(c);
723
+ c8 = c8.redIAdd(c8);
724
+ c8 = c8.redIAdd(c8);
725
+
726
+ // X3 = F - 2 * D
727
+ nx = f.redISub(d).redISub(d);
728
+ // Y3 = E * (D - X3) - 8 * C
729
+ ny = e.redMul(d.redISub(nx)).redISub(c8);
730
+ // Z3 = 2 * Y1 * Z1
731
+ nz = this.y.redMul(this.z);
732
+ nz = nz.redIAdd(nz);
733
+ }
734
+
735
+ return this.curve.jpoint(nx, ny, nz);
736
+ };
737
+
738
+ JPoint.prototype._threeDbl = function _threeDbl() {
739
+ var nx;
740
+ var ny;
741
+ var nz;
742
+ // Z = 1
743
+ if (this.zOne) {
744
+ // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html
745
+ // #doubling-mdbl-2007-bl
746
+ // 1M + 5S + 15A
747
+
748
+ // XX = X1^2
749
+ var xx = this.x.redSqr();
750
+ // YY = Y1^2
751
+ var yy = this.y.redSqr();
752
+ // YYYY = YY^2
753
+ var yyyy = yy.redSqr();
754
+ // S = 2 * ((X1 + YY)^2 - XX - YYYY)
755
+ var s = this.x.redAdd(yy).redSqr().redISub(xx).redISub(yyyy);
756
+ s = s.redIAdd(s);
757
+ // M = 3 * XX + a
758
+ var m = xx.redAdd(xx).redIAdd(xx).redIAdd(this.curve.a);
759
+ // T = M^2 - 2 * S
760
+ var t = m.redSqr().redISub(s).redISub(s);
761
+ // X3 = T
762
+ nx = t;
763
+ // Y3 = M * (S - T) - 8 * YYYY
764
+ var yyyy8 = yyyy.redIAdd(yyyy);
765
+ yyyy8 = yyyy8.redIAdd(yyyy8);
766
+ yyyy8 = yyyy8.redIAdd(yyyy8);
767
+ ny = m.redMul(s.redISub(t)).redISub(yyyy8);
768
+ // Z3 = 2 * Y1
769
+ nz = this.y.redAdd(this.y);
770
+ } else {
771
+ // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
772
+ // 3M + 5S
773
+
774
+ // delta = Z1^2
775
+ var delta = this.z.redSqr();
776
+ // gamma = Y1^2
777
+ var gamma = this.y.redSqr();
778
+ // beta = X1 * gamma
779
+ var beta = this.x.redMul(gamma);
780
+ // alpha = 3 * (X1 - delta) * (X1 + delta)
781
+ var alpha = this.x.redSub(delta).redMul(this.x.redAdd(delta));
782
+ alpha = alpha.redAdd(alpha).redIAdd(alpha);
783
+ // X3 = alpha^2 - 8 * beta
784
+ var beta4 = beta.redIAdd(beta);
785
+ beta4 = beta4.redIAdd(beta4);
786
+ var beta8 = beta4.redAdd(beta4);
787
+ nx = alpha.redSqr().redISub(beta8);
788
+ // Z3 = (Y1 + Z1)^2 - gamma - delta
789
+ nz = this.y.redAdd(this.z).redSqr().redISub(gamma).redISub(delta);
790
+ // Y3 = alpha * (4 * beta - X3) - 8 * gamma^2
791
+ var ggamma8 = gamma.redSqr();
792
+ ggamma8 = ggamma8.redIAdd(ggamma8);
793
+ ggamma8 = ggamma8.redIAdd(ggamma8);
794
+ ggamma8 = ggamma8.redIAdd(ggamma8);
795
+ ny = alpha.redMul(beta4.redISub(nx)).redISub(ggamma8);
796
+ }
797
+
798
+ return this.curve.jpoint(nx, ny, nz);
799
+ };
800
+
801
+ JPoint.prototype._dbl = function _dbl() {
802
+ var a = this.curve.a;
803
+
804
+ // 4M + 6S + 10A
805
+ var jx = this.x;
806
+ var jy = this.y;
807
+ var jz = this.z;
808
+ var jz4 = jz.redSqr().redSqr();
809
+
810
+ var jx2 = jx.redSqr();
811
+ var jy2 = jy.redSqr();
812
+
813
+ var c = jx2.redAdd(jx2).redIAdd(jx2).redIAdd(a.redMul(jz4));
814
+
815
+ var jxd4 = jx.redAdd(jx);
816
+ jxd4 = jxd4.redIAdd(jxd4);
817
+ var t1 = jxd4.redMul(jy2);
818
+ var nx = c.redSqr().redISub(t1.redAdd(t1));
819
+ var t2 = t1.redISub(nx);
820
+
821
+ var jyd8 = jy2.redSqr();
822
+ jyd8 = jyd8.redIAdd(jyd8);
823
+ jyd8 = jyd8.redIAdd(jyd8);
824
+ jyd8 = jyd8.redIAdd(jyd8);
825
+ var ny = c.redMul(t2).redISub(jyd8);
826
+ var nz = jy.redAdd(jy).redMul(jz);
827
+
828
+ return this.curve.jpoint(nx, ny, nz);
829
+ };
830
+
831
+ JPoint.prototype.trpl = function trpl() {
832
+ if (!this.curve.zeroA)
833
+ return this.dbl().add(this);
834
+
835
+ // hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#tripling-tpl-2007-bl
836
+ // 5M + 10S + ...
837
+
838
+ // XX = X1^2
839
+ var xx = this.x.redSqr();
840
+ // YY = Y1^2
841
+ var yy = this.y.redSqr();
842
+ // ZZ = Z1^2
843
+ var zz = this.z.redSqr();
844
+ // YYYY = YY^2
845
+ var yyyy = yy.redSqr();
846
+ // M = 3 * XX + a * ZZ2; a = 0
847
+ var m = xx.redAdd(xx).redIAdd(xx);
848
+ // MM = M^2
849
+ var mm = m.redSqr();
850
+ // E = 6 * ((X1 + YY)^2 - XX - YYYY) - MM
851
+ var e = this.x.redAdd(yy).redSqr().redISub(xx).redISub(yyyy);
852
+ e = e.redIAdd(e);
853
+ e = e.redAdd(e).redIAdd(e);
854
+ e = e.redISub(mm);
855
+ // EE = E^2
856
+ var ee = e.redSqr();
857
+ // T = 16*YYYY
858
+ var t = yyyy.redIAdd(yyyy);
859
+ t = t.redIAdd(t);
860
+ t = t.redIAdd(t);
861
+ t = t.redIAdd(t);
862
+ // U = (M + E)^2 - MM - EE - T
863
+ var u = m.redIAdd(e).redSqr().redISub(mm).redISub(ee).redISub(t);
864
+ // X3 = 4 * (X1 * EE - 4 * YY * U)
865
+ var yyu4 = yy.redMul(u);
866
+ yyu4 = yyu4.redIAdd(yyu4);
867
+ yyu4 = yyu4.redIAdd(yyu4);
868
+ var nx = this.x.redMul(ee).redISub(yyu4);
869
+ nx = nx.redIAdd(nx);
870
+ nx = nx.redIAdd(nx);
871
+ // Y3 = 8 * Y1 * (U * (T - U) - E * EE)
872
+ var ny = this.y.redMul(u.redMul(t.redISub(u)).redISub(e.redMul(ee)));
873
+ ny = ny.redIAdd(ny);
874
+ ny = ny.redIAdd(ny);
875
+ ny = ny.redIAdd(ny);
876
+ // Z3 = (Z1 + E)^2 - ZZ - EE
877
+ var nz = this.z.redAdd(e).redSqr().redISub(zz).redISub(ee);
878
+
879
+ return this.curve.jpoint(nx, ny, nz);
880
+ };
881
+
882
+ JPoint.prototype.mul = function mul(k, kbase) {
883
+ k = new BN(k, kbase);
884
+
885
+ return this.curve._wnafMul(this, k);
886
+ };
887
+
888
+ JPoint.prototype.eq = function eq(p) {
889
+ if (p.type === 'affine')
890
+ return this.eq(p.toJ());
891
+
892
+ if (this === p)
893
+ return true;
894
+
895
+ // x1 * z2^2 == x2 * z1^2
896
+ var z2 = this.z.redSqr();
897
+ var pz2 = p.z.redSqr();
898
+ if (this.x.redMul(pz2).redISub(p.x.redMul(z2)).cmpn(0) !== 0)
899
+ return false;
900
+
901
+ // y1 * z2^3 == y2 * z1^3
902
+ var z3 = z2.redMul(this.z);
903
+ var pz3 = pz2.redMul(p.z);
904
+ return this.y.redMul(pz3).redISub(p.y.redMul(z3)).cmpn(0) === 0;
905
+ };
906
+
907
+ JPoint.prototype.eqXToP = function eqXToP(x) {
908
+ var zs = this.z.redSqr();
909
+ var rx = x.toRed(this.curve.red).redMul(zs);
910
+ if (this.x.cmp(rx) === 0)
911
+ return true;
912
+
913
+ var xc = x.clone();
914
+ var t = this.curve.redN.redMul(zs);
915
+ for (;;) {
916
+ xc.iadd(this.curve.n);
917
+ if (xc.cmp(this.curve.p) >= 0)
918
+ return false;
919
+
920
+ rx.redIAdd(t);
921
+ if (this.x.cmp(rx) === 0)
922
+ return true;
923
+ }
924
+ return false;
925
+ };
926
+
927
+ JPoint.prototype.inspect = function inspect() {
928
+ if (this.isInfinity())
929
+ return '<EC JPoint Infinity>';
930
+ return '<EC JPoint x: ' + this.x.toString(16, 2) +
931
+ ' y: ' + this.y.toString(16, 2) +
932
+ ' z: ' + this.z.toString(16, 2) + '>';
933
+ };
934
+
935
+ JPoint.prototype.isInfinity = function isInfinity() {
936
+ // XXX This code assumes that zero is always zero in red
937
+ return this.z.cmpn(0) === 0;
938
+ };