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.
- package/.cache/eslintcache +1 -0
- package/.dockerignore +6 -0
- package/.eslintignore +1 -0
- package/.gitattributes +5 -0
- package/.readthedocs.yaml +16 -0
- package/CONTRIBUTING.md +1049 -0
- package/LICENSE.txt +21 -0
- package/README.md +537 -0
- package/SECURITY.md +5 -0
- package/build/cleanup-old-tags.js +94 -0
- package/build/countries.js +256 -0
- package/build/export-exchanges.js +520 -0
- package/build/fs.js +51 -0
- package/build/transpile.js +1772 -0
- package/build/vss.js +78 -0
- package/ccxt.browser.js +7 -0
- package/ccxt.d.ts +692 -0
- package/ccxt.js +171 -0
- package/cleanup.sh +2 -0
- package/composer-install.sh +20 -0
- package/dist/ccxt.browser.js +208383 -0
- package/gource.sh +3 -0
- package/index.html +7 -0
- package/js/.eslintrc +87 -0
- package/js/aax.js +2686 -0
- package/js/ascendex.js +2584 -0
- package/js/base/.eslintrc.js +43 -0
- package/js/base/Exchange.js +2371 -0
- package/js/base/Precise.js +283 -0
- package/js/base/errorHierarchy.js +47 -0
- package/js/base/errors.js +55 -0
- package/js/base/functions/crypto.js +158 -0
- package/js/base/functions/encode.js +118 -0
- package/js/base/functions/generic.js +270 -0
- package/js/base/functions/misc.js +138 -0
- package/js/base/functions/number.js +329 -0
- package/js/base/functions/platform.js +38 -0
- package/js/base/functions/string.js +21 -0
- package/js/base/functions/throttle.js +79 -0
- package/js/base/functions/time.js +210 -0
- package/js/base/functions/type.js +66 -0
- package/js/base/functions.js +28 -0
- package/js/bequant.js +32 -0
- package/js/bibox.js +1407 -0
- package/js/bigone.js +1366 -0
- package/js/binance.js +5652 -0
- package/js/binancecoinm.js +46 -0
- package/js/binanceus.js +46 -0
- package/js/binanceusdm.js +49 -0
- package/js/bit2c.js +535 -0
- package/js/bitbank.js +842 -0
- package/js/bitbay.js +16 -0
- package/js/bitbns.js +1073 -0
- package/js/bitcoincom.js +15 -0
- package/js/bitfinex.js +1433 -0
- package/js/bitfinex2.js +2025 -0
- package/js/bitflyer.js +840 -0
- package/js/bitforex.js +614 -0
- package/js/bitget.js +2397 -0
- package/js/bithumb.js +980 -0
- package/js/bitmart.js +2516 -0
- package/js/bitmex.js +1809 -0
- package/js/bitopro.js +1443 -0
- package/js/bitpanda.js +1782 -0
- package/js/bitrue.js +1747 -0
- package/js/bitso.js +1062 -0
- package/js/bitstamp.js +1757 -0
- package/js/bitstamp1.js +343 -0
- package/js/bittrex.js +1876 -0
- package/js/bitvavo.js +1579 -0
- package/js/bkex.js +1233 -0
- package/js/bl3p.js +346 -0
- package/js/blockchaincom.js +969 -0
- package/js/btcalpha.js +680 -0
- package/js/btcbox.js +477 -0
- package/js/btcmarkets.js +1022 -0
- package/js/btctradeua.js +466 -0
- package/js/btcturk.js +734 -0
- package/js/buda.js +946 -0
- package/js/bw.js +1265 -0
- package/js/bybit.js +3372 -0
- package/js/bytetrade.js +1336 -0
- package/js/cdax.js +1646 -0
- package/js/cex.js +1410 -0
- package/js/coinbase.js +1342 -0
- package/js/coinbaseprime.js +31 -0
- package/js/coinbasepro.js +1466 -0
- package/js/coincheck.js +755 -0
- package/js/coinex.js +3400 -0
- package/js/coinfalcon.js +880 -0
- package/js/coinmate.js +794 -0
- package/js/coinone.js +816 -0
- package/js/coinspot.js +345 -0
- package/js/crex24.js +1636 -0
- package/js/cryptocom.js +1832 -0
- package/js/currencycom.js +1748 -0
- package/js/delta.js +1547 -0
- package/js/deribit.js +2148 -0
- package/js/digifinex.js +1585 -0
- package/js/eqonex.js +1660 -0
- package/js/exmo.js +1670 -0
- package/js/fairdesk.js +1231 -0
- package/js/flowbtc.js +35 -0
- package/js/fmfwio.js +34 -0
- package/js/ftx.js +2751 -0
- package/js/ftxus.js +38 -0
- package/js/gateio.js +4174 -0
- package/js/gemini.js +1397 -0
- package/js/hitbtc.js +1343 -0
- package/js/hitbtc3.js +2329 -0
- package/js/hollaex.js +1486 -0
- package/js/huobi.js +5706 -0
- package/js/huobijp.js +1710 -0
- package/js/huobipro.js +18 -0
- package/js/idex.js +1439 -0
- package/js/independentreserve.js +649 -0
- package/js/indodax.js +742 -0
- package/js/itbit.js +722 -0
- package/js/kraken.js +2179 -0
- package/js/kucoin.js +2571 -0
- package/js/kucoinfutures.js +1771 -0
- package/js/kuna.js +809 -0
- package/js/latoken.js +1445 -0
- package/js/lbank.js +760 -0
- package/js/liquid.js +1432 -0
- package/js/luno.js +873 -0
- package/js/lykke.js +1147 -0
- package/js/mercado.js +771 -0
- package/js/mexc.js +3151 -0
- package/js/ndax.js +2233 -0
- package/js/novadax.js +1318 -0
- package/js/oceanex.js +816 -0
- package/js/okcoin.js +3841 -0
- package/js/okex.js +16 -0
- package/js/okex5.js +16 -0
- package/js/okx.js +4795 -0
- package/js/paymium.js +498 -0
- package/js/phemex.js +2957 -0
- package/js/poloniex.js +1674 -0
- package/js/probit.js +1346 -0
- package/js/qtrade.js +1588 -0
- package/js/ripio.js +1061 -0
- package/js/static_dependencies/BN/bn.js +3526 -0
- package/js/static_dependencies/README.md +1 -0
- package/js/static_dependencies/crypto-js/crypto-js.js +5988 -0
- package/js/static_dependencies/elliptic/lib/elliptic/curve/base.js +375 -0
- package/js/static_dependencies/elliptic/lib/elliptic/curve/edwards.js +433 -0
- package/js/static_dependencies/elliptic/lib/elliptic/curve/index.js +8 -0
- package/js/static_dependencies/elliptic/lib/elliptic/curve/mont.js +180 -0
- package/js/static_dependencies/elliptic/lib/elliptic/curve/short.js +938 -0
- package/js/static_dependencies/elliptic/lib/elliptic/curves.js +204 -0
- package/js/static_dependencies/elliptic/lib/elliptic/ec/index.js +240 -0
- package/js/static_dependencies/elliptic/lib/elliptic/ec/key.js +119 -0
- package/js/static_dependencies/elliptic/lib/elliptic/ec/signature.js +24 -0
- package/js/static_dependencies/elliptic/lib/elliptic/eddsa/index.js +145 -0
- package/js/static_dependencies/elliptic/lib/elliptic/eddsa/key.js +100 -0
- package/js/static_dependencies/elliptic/lib/elliptic/eddsa/signature.js +65 -0
- package/js/static_dependencies/elliptic/lib/elliptic/precomputed/secp256k1.js +780 -0
- package/js/static_dependencies/elliptic/lib/elliptic/utils.js +214 -0
- package/js/static_dependencies/elliptic/lib/elliptic.js +22 -0
- package/js/static_dependencies/elliptic/lib/hmac-drbg/hmac-drbg.js +114 -0
- package/js/static_dependencies/fetch-ponyfill/fetch-node.js +39 -0
- package/js/static_dependencies/node-fetch/index.js +1564 -0
- package/js/static_dependencies/node-rsa/NodeRSA.js +223 -0
- package/js/static_dependencies/node-rsa/asn1/ber/errors.js +13 -0
- package/js/static_dependencies/node-rsa/asn1/ber/index.js +21 -0
- package/js/static_dependencies/node-rsa/asn1/ber/reader.js +262 -0
- package/js/static_dependencies/node-rsa/asn1/ber/types.js +36 -0
- package/js/static_dependencies/node-rsa/asn1/index.js +17 -0
- package/js/static_dependencies/node-rsa/encryptEngines/js.js +34 -0
- package/js/static_dependencies/node-rsa/formats/components.js +71 -0
- package/js/static_dependencies/node-rsa/formats/formats.js +31 -0
- package/js/static_dependencies/node-rsa/formats/pkcs1.js +148 -0
- package/js/static_dependencies/node-rsa/formats/pkcs8.js +187 -0
- package/js/static_dependencies/node-rsa/libs/jsbn.js +1252 -0
- package/js/static_dependencies/node-rsa/libs/rsa.js +147 -0
- package/js/static_dependencies/node-rsa/schemes/pkcs1.js +176 -0
- package/js/static_dependencies/node-rsa/schemes/schemes.js +21 -0
- package/js/static_dependencies/node-rsa/utils.js +98 -0
- package/js/static_dependencies/qs/formats.js +18 -0
- package/js/static_dependencies/qs/index.js +11 -0
- package/js/static_dependencies/qs/parse.js +242 -0
- package/js/static_dependencies/qs/stringify.js +269 -0
- package/js/static_dependencies/qs/utils.js +230 -0
- package/js/stex.js +1925 -0
- package/js/test/.eslintrc.js +42 -0
- package/js/test/Exchange/test.balance.js +61 -0
- package/js/test/Exchange/test.borrowRate.js +32 -0
- package/js/test/Exchange/test.currency.js +52 -0
- package/js/test/Exchange/test.fetchBalance.js +23 -0
- package/js/test/Exchange/test.fetchBorrowInterest.js +59 -0
- package/js/test/Exchange/test.fetchBorrowRate.js +32 -0
- package/js/test/Exchange/test.fetchBorrowRates.js +28 -0
- package/js/test/Exchange/test.fetchClosedOrders.js +32 -0
- package/js/test/Exchange/test.fetchCurrencies.js +35 -0
- package/js/test/Exchange/test.fetchDeposits.js +31 -0
- package/js/test/Exchange/test.fetchFundingFees.js +19 -0
- package/js/test/Exchange/test.fetchFundingRateHistory.js +40 -0
- package/js/test/Exchange/test.fetchL2OrderBook.js +23 -0
- package/js/test/Exchange/test.fetchLedger.js +42 -0
- package/js/test/Exchange/test.fetchLeverageTiers.js +33 -0
- package/js/test/Exchange/test.fetchMarketLeverageTiers.js +22 -0
- package/js/test/Exchange/test.fetchMarkets.js +33 -0
- package/js/test/Exchange/test.fetchMyTrades.js +42 -0
- package/js/test/Exchange/test.fetchOHLCV.js +46 -0
- package/js/test/Exchange/test.fetchOpenOrders.js +36 -0
- package/js/test/Exchange/test.fetchOrderBook.js +25 -0
- package/js/test/Exchange/test.fetchOrderBooks.js +35 -0
- package/js/test/Exchange/test.fetchOrders.js +41 -0
- package/js/test/Exchange/test.fetchPositions.js +47 -0
- package/js/test/Exchange/test.fetchStatus.js +35 -0
- package/js/test/Exchange/test.fetchTicker.js +38 -0
- package/js/test/Exchange/test.fetchTickers.js +49 -0
- package/js/test/Exchange/test.fetchTrades.js +39 -0
- package/js/test/Exchange/test.fetchTradingFee.js +18 -0
- package/js/test/Exchange/test.fetchTradingFees.js +22 -0
- package/js/test/Exchange/test.fetchTransactions.js +31 -0
- package/js/test/Exchange/test.fetchWithdrawals.js +31 -0
- package/js/test/Exchange/test.ledgerItem.js +46 -0
- package/js/test/Exchange/test.leverageTier.js +33 -0
- package/js/test/Exchange/test.loadMarkets.js +35 -0
- package/js/test/Exchange/test.market.js +129 -0
- package/js/test/Exchange/test.ohlcv.js +33 -0
- package/js/test/Exchange/test.order.js +62 -0
- package/js/test/Exchange/test.orderbook.js +61 -0
- package/js/test/Exchange/test.position.js +21 -0
- package/js/test/Exchange/test.throttle.js +94 -0
- package/js/test/Exchange/test.ticker.js +95 -0
- package/js/test/Exchange/test.trade.js +68 -0
- package/js/test/Exchange/test.tradingFee.js +34 -0
- package/js/test/Exchange/test.transaction.js +35 -0
- package/js/test/base/.eslintrc +38 -0
- package/js/test/base/functions/test.crypto.js +110 -0
- package/js/test/base/functions/test.datetime.js +62 -0
- package/js/test/base/functions/test.generic.js +152 -0
- package/js/test/base/functions/test.number.js +362 -0
- package/js/test/base/functions/test.time.js +56 -0
- package/js/test/base/functions/test.type.js +53 -0
- package/js/test/base/test.base.js +193 -0
- package/js/test/errors/test.InsufficientFunds.js +86 -0
- package/js/test/errors/test.InvalidNonce.js +64 -0
- package/js/test/errors/test.InvalidOrder.js +35 -0
- package/js/test/errors/test.OrderNotFound.js +39 -0
- package/js/test/test.js +426 -0
- package/js/test/test.timeout_hang.js +12 -0
- package/js/therock.js +1431 -0
- package/js/tidebit.js +632 -0
- package/js/tidex.js +939 -0
- package/js/timex.js +1283 -0
- package/js/upbit.js +1622 -0
- package/js/vcc.js +1353 -0
- package/js/wavesexchange.js +2185 -0
- package/js/wazirx.js +732 -0
- package/js/whitebit.js +1352 -0
- package/js/woo.js +1577 -0
- package/js/xena.js +1948 -0
- package/js/yobit.js +1129 -0
- package/js/zaif.js +647 -0
- package/js/zb.js +4088 -0
- package/js/zipmex.js +40 -0
- package/js/zonda.js +1497 -0
- package/multilang.sh +159 -0
- package/package.json +591 -0
- package/postinstall.js +103 -0
@@ -0,0 +1,433 @@
|
|
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 EdwardsCurve(conf) {
|
12
|
+
// NOTE: Important as we are creating point in Base.call()
|
13
|
+
this.twisted = (conf.a | 0) !== 1;
|
14
|
+
this.mOneA = this.twisted && (conf.a | 0) === -1;
|
15
|
+
this.extended = this.mOneA;
|
16
|
+
|
17
|
+
Base.call(this, 'edwards', conf);
|
18
|
+
|
19
|
+
this.a = new BN(conf.a, 16).umod(this.red.m);
|
20
|
+
this.a = this.a.toRed(this.red);
|
21
|
+
this.c = new BN(conf.c, 16).toRed(this.red);
|
22
|
+
this.c2 = this.c.redSqr();
|
23
|
+
this.d = new BN(conf.d, 16).toRed(this.red);
|
24
|
+
this.dd = this.d.redAdd(this.d);
|
25
|
+
|
26
|
+
assert(!this.twisted || this.c.fromRed().cmpn(1) === 0);
|
27
|
+
this.oneC = (conf.c | 0) === 1;
|
28
|
+
}
|
29
|
+
inherits(EdwardsCurve, Base);
|
30
|
+
module.exports = EdwardsCurve;
|
31
|
+
|
32
|
+
EdwardsCurve.prototype._mulA = function _mulA(num) {
|
33
|
+
if (this.mOneA)
|
34
|
+
return num.redNeg();
|
35
|
+
else
|
36
|
+
return this.a.redMul(num);
|
37
|
+
};
|
38
|
+
|
39
|
+
EdwardsCurve.prototype._mulC = function _mulC(num) {
|
40
|
+
if (this.oneC)
|
41
|
+
return num;
|
42
|
+
else
|
43
|
+
return this.c.redMul(num);
|
44
|
+
};
|
45
|
+
|
46
|
+
// Just for compatibility with Short curve
|
47
|
+
EdwardsCurve.prototype.jpoint = function jpoint(x, y, z, t) {
|
48
|
+
return this.point(x, y, z, t);
|
49
|
+
};
|
50
|
+
|
51
|
+
EdwardsCurve.prototype.pointFromX = function pointFromX(x, odd) {
|
52
|
+
x = new BN(x, 16);
|
53
|
+
if (!x.red)
|
54
|
+
x = x.toRed(this.red);
|
55
|
+
|
56
|
+
var x2 = x.redSqr();
|
57
|
+
var rhs = this.c2.redSub(this.a.redMul(x2));
|
58
|
+
var lhs = this.one.redSub(this.c2.redMul(this.d).redMul(x2));
|
59
|
+
|
60
|
+
var y2 = rhs.redMul(lhs.redInvm());
|
61
|
+
var y = y2.redSqrt();
|
62
|
+
if (y.redSqr().redSub(y2).cmp(this.zero) !== 0)
|
63
|
+
throw new Error('invalid point');
|
64
|
+
|
65
|
+
var isOdd = y.fromRed().isOdd();
|
66
|
+
if (odd && !isOdd || !odd && isOdd)
|
67
|
+
y = y.redNeg();
|
68
|
+
|
69
|
+
return this.point(x, y);
|
70
|
+
};
|
71
|
+
|
72
|
+
EdwardsCurve.prototype.pointFromY = function pointFromY(y, odd) {
|
73
|
+
y = new BN(y, 16);
|
74
|
+
if (!y.red)
|
75
|
+
y = y.toRed(this.red);
|
76
|
+
|
77
|
+
// x^2 = (y^2 - 1) / (d y^2 + 1)
|
78
|
+
var y2 = y.redSqr();
|
79
|
+
var lhs = y2.redSub(this.one);
|
80
|
+
var rhs = y2.redMul(this.d).redAdd(this.one);
|
81
|
+
var x2 = lhs.redMul(rhs.redInvm());
|
82
|
+
|
83
|
+
if (x2.cmp(this.zero) === 0) {
|
84
|
+
if (odd)
|
85
|
+
throw new Error('invalid point');
|
86
|
+
else
|
87
|
+
return this.point(this.zero, y);
|
88
|
+
}
|
89
|
+
|
90
|
+
var x = x2.redSqrt();
|
91
|
+
if (x.redSqr().redSub(x2).cmp(this.zero) !== 0)
|
92
|
+
throw new Error('invalid point');
|
93
|
+
|
94
|
+
if (x.isOdd() !== odd)
|
95
|
+
x = x.redNeg();
|
96
|
+
|
97
|
+
return this.point(x, y);
|
98
|
+
};
|
99
|
+
|
100
|
+
EdwardsCurve.prototype.validate = function validate(point) {
|
101
|
+
if (point.isInfinity())
|
102
|
+
return true;
|
103
|
+
|
104
|
+
// Curve: A * X^2 + Y^2 = C^2 * (1 + D * X^2 * Y^2)
|
105
|
+
point.normalize();
|
106
|
+
|
107
|
+
var x2 = point.x.redSqr();
|
108
|
+
var y2 = point.y.redSqr();
|
109
|
+
var lhs = x2.redMul(this.a).redAdd(y2);
|
110
|
+
var rhs = this.c2.redMul(this.one.redAdd(this.d.redMul(x2).redMul(y2)));
|
111
|
+
|
112
|
+
return lhs.cmp(rhs) === 0;
|
113
|
+
};
|
114
|
+
|
115
|
+
function Point(curve, x, y, z, t) {
|
116
|
+
Base.BasePoint.call(this, curve, 'projective');
|
117
|
+
if (x === null && y === null && z === null) {
|
118
|
+
this.x = this.curve.zero;
|
119
|
+
this.y = this.curve.one;
|
120
|
+
this.z = this.curve.one;
|
121
|
+
this.t = this.curve.zero;
|
122
|
+
this.zOne = true;
|
123
|
+
} else {
|
124
|
+
this.x = new BN(x, 16);
|
125
|
+
this.y = new BN(y, 16);
|
126
|
+
this.z = z ? new BN(z, 16) : this.curve.one;
|
127
|
+
this.t = t && new BN(t, 16);
|
128
|
+
if (!this.x.red)
|
129
|
+
this.x = this.x.toRed(this.curve.red);
|
130
|
+
if (!this.y.red)
|
131
|
+
this.y = this.y.toRed(this.curve.red);
|
132
|
+
if (!this.z.red)
|
133
|
+
this.z = this.z.toRed(this.curve.red);
|
134
|
+
if (this.t && !this.t.red)
|
135
|
+
this.t = this.t.toRed(this.curve.red);
|
136
|
+
this.zOne = this.z === this.curve.one;
|
137
|
+
|
138
|
+
// Use extended coordinates
|
139
|
+
if (this.curve.extended && !this.t) {
|
140
|
+
this.t = this.x.redMul(this.y);
|
141
|
+
if (!this.zOne)
|
142
|
+
this.t = this.t.redMul(this.z.redInvm());
|
143
|
+
}
|
144
|
+
}
|
145
|
+
}
|
146
|
+
inherits(Point, Base.BasePoint);
|
147
|
+
|
148
|
+
EdwardsCurve.prototype.pointFromJSON = function pointFromJSON(obj) {
|
149
|
+
return Point.fromJSON(this, obj);
|
150
|
+
};
|
151
|
+
|
152
|
+
EdwardsCurve.prototype.point = function point(x, y, z, t) {
|
153
|
+
return new Point(this, x, y, z, t);
|
154
|
+
};
|
155
|
+
|
156
|
+
Point.fromJSON = function fromJSON(curve, obj) {
|
157
|
+
return new Point(curve, obj[0], obj[1], obj[2]);
|
158
|
+
};
|
159
|
+
|
160
|
+
Point.prototype.inspect = function inspect() {
|
161
|
+
if (this.isInfinity())
|
162
|
+
return '<EC Point Infinity>';
|
163
|
+
return '<EC Point x: ' + this.x.fromRed().toString(16, 2) +
|
164
|
+
' y: ' + this.y.fromRed().toString(16, 2) +
|
165
|
+
' z: ' + this.z.fromRed().toString(16, 2) + '>';
|
166
|
+
};
|
167
|
+
|
168
|
+
Point.prototype.isInfinity = function isInfinity() {
|
169
|
+
// XXX This code assumes that zero is always zero in red
|
170
|
+
return this.x.cmpn(0) === 0 &&
|
171
|
+
this.y.cmp(this.z) === 0;
|
172
|
+
};
|
173
|
+
|
174
|
+
Point.prototype._extDbl = function _extDbl() {
|
175
|
+
// hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html
|
176
|
+
// #doubling-dbl-2008-hwcd
|
177
|
+
// 4M + 4S
|
178
|
+
|
179
|
+
// A = X1^2
|
180
|
+
var a = this.x.redSqr();
|
181
|
+
// B = Y1^2
|
182
|
+
var b = this.y.redSqr();
|
183
|
+
// C = 2 * Z1^2
|
184
|
+
var c = this.z.redSqr();
|
185
|
+
c = c.redIAdd(c);
|
186
|
+
// D = a * A
|
187
|
+
var d = this.curve._mulA(a);
|
188
|
+
// E = (X1 + Y1)^2 - A - B
|
189
|
+
var e = this.x.redAdd(this.y).redSqr().redISub(a).redISub(b);
|
190
|
+
// G = D + B
|
191
|
+
var g = d.redAdd(b);
|
192
|
+
// F = G - C
|
193
|
+
var f = g.redSub(c);
|
194
|
+
// H = D - B
|
195
|
+
var h = d.redSub(b);
|
196
|
+
// X3 = E * F
|
197
|
+
var nx = e.redMul(f);
|
198
|
+
// Y3 = G * H
|
199
|
+
var ny = g.redMul(h);
|
200
|
+
// T3 = E * H
|
201
|
+
var nt = e.redMul(h);
|
202
|
+
// Z3 = F * G
|
203
|
+
var nz = f.redMul(g);
|
204
|
+
return this.curve.point(nx, ny, nz, nt);
|
205
|
+
};
|
206
|
+
|
207
|
+
Point.prototype._projDbl = function _projDbl() {
|
208
|
+
// hyperelliptic.org/EFD/g1p/auto-twisted-projective.html
|
209
|
+
// #doubling-dbl-2008-bbjlp
|
210
|
+
// #doubling-dbl-2007-bl
|
211
|
+
// and others
|
212
|
+
// Generally 3M + 4S or 2M + 4S
|
213
|
+
|
214
|
+
// B = (X1 + Y1)^2
|
215
|
+
var b = this.x.redAdd(this.y).redSqr();
|
216
|
+
// C = X1^2
|
217
|
+
var c = this.x.redSqr();
|
218
|
+
// D = Y1^2
|
219
|
+
var d = this.y.redSqr();
|
220
|
+
|
221
|
+
var nx;
|
222
|
+
var ny;
|
223
|
+
var nz;
|
224
|
+
if (this.curve.twisted) {
|
225
|
+
// E = a * C
|
226
|
+
var e = this.curve._mulA(c);
|
227
|
+
// F = E + D
|
228
|
+
var f = e.redAdd(d);
|
229
|
+
if (this.zOne) {
|
230
|
+
// X3 = (B - C - D) * (F - 2)
|
231
|
+
nx = b.redSub(c).redSub(d).redMul(f.redSub(this.curve.two));
|
232
|
+
// Y3 = F * (E - D)
|
233
|
+
ny = f.redMul(e.redSub(d));
|
234
|
+
// Z3 = F^2 - 2 * F
|
235
|
+
nz = f.redSqr().redSub(f).redSub(f);
|
236
|
+
} else {
|
237
|
+
// H = Z1^2
|
238
|
+
var h = this.z.redSqr();
|
239
|
+
// J = F - 2 * H
|
240
|
+
var j = f.redSub(h).redISub(h);
|
241
|
+
// X3 = (B-C-D)*J
|
242
|
+
nx = b.redSub(c).redISub(d).redMul(j);
|
243
|
+
// Y3 = F * (E - D)
|
244
|
+
ny = f.redMul(e.redSub(d));
|
245
|
+
// Z3 = F * J
|
246
|
+
nz = f.redMul(j);
|
247
|
+
}
|
248
|
+
} else {
|
249
|
+
// E = C + D
|
250
|
+
var e = c.redAdd(d);
|
251
|
+
// H = (c * Z1)^2
|
252
|
+
var h = this.curve._mulC(this.c.redMul(this.z)).redSqr();
|
253
|
+
// J = E - 2 * H
|
254
|
+
var j = e.redSub(h).redSub(h);
|
255
|
+
// X3 = c * (B - E) * J
|
256
|
+
nx = this.curve._mulC(b.redISub(e)).redMul(j);
|
257
|
+
// Y3 = c * E * (C - D)
|
258
|
+
ny = this.curve._mulC(e).redMul(c.redISub(d));
|
259
|
+
// Z3 = E * J
|
260
|
+
nz = e.redMul(j);
|
261
|
+
}
|
262
|
+
return this.curve.point(nx, ny, nz);
|
263
|
+
};
|
264
|
+
|
265
|
+
Point.prototype.dbl = function dbl() {
|
266
|
+
if (this.isInfinity())
|
267
|
+
return this;
|
268
|
+
|
269
|
+
// Double in extended coordinates
|
270
|
+
if (this.curve.extended)
|
271
|
+
return this._extDbl();
|
272
|
+
else
|
273
|
+
return this._projDbl();
|
274
|
+
};
|
275
|
+
|
276
|
+
Point.prototype._extAdd = function _extAdd(p) {
|
277
|
+
// hyperelliptic.org/EFD/g1p/auto-twisted-extended-1.html
|
278
|
+
// #addition-add-2008-hwcd-3
|
279
|
+
// 8M
|
280
|
+
|
281
|
+
// A = (Y1 - X1) * (Y2 - X2)
|
282
|
+
var a = this.y.redSub(this.x).redMul(p.y.redSub(p.x));
|
283
|
+
// B = (Y1 + X1) * (Y2 + X2)
|
284
|
+
var b = this.y.redAdd(this.x).redMul(p.y.redAdd(p.x));
|
285
|
+
// C = T1 * k * T2
|
286
|
+
var c = this.t.redMul(this.curve.dd).redMul(p.t);
|
287
|
+
// D = Z1 * 2 * Z2
|
288
|
+
var d = this.z.redMul(p.z.redAdd(p.z));
|
289
|
+
// E = B - A
|
290
|
+
var e = b.redSub(a);
|
291
|
+
// F = D - C
|
292
|
+
var f = d.redSub(c);
|
293
|
+
// G = D + C
|
294
|
+
var g = d.redAdd(c);
|
295
|
+
// H = B + A
|
296
|
+
var h = b.redAdd(a);
|
297
|
+
// X3 = E * F
|
298
|
+
var nx = e.redMul(f);
|
299
|
+
// Y3 = G * H
|
300
|
+
var ny = g.redMul(h);
|
301
|
+
// T3 = E * H
|
302
|
+
var nt = e.redMul(h);
|
303
|
+
// Z3 = F * G
|
304
|
+
var nz = f.redMul(g);
|
305
|
+
return this.curve.point(nx, ny, nz, nt);
|
306
|
+
};
|
307
|
+
|
308
|
+
Point.prototype._projAdd = function _projAdd(p) {
|
309
|
+
// hyperelliptic.org/EFD/g1p/auto-twisted-projective.html
|
310
|
+
// #addition-add-2008-bbjlp
|
311
|
+
// #addition-add-2007-bl
|
312
|
+
// 10M + 1S
|
313
|
+
|
314
|
+
// A = Z1 * Z2
|
315
|
+
var a = this.z.redMul(p.z);
|
316
|
+
// B = A^2
|
317
|
+
var b = a.redSqr();
|
318
|
+
// C = X1 * X2
|
319
|
+
var c = this.x.redMul(p.x);
|
320
|
+
// D = Y1 * Y2
|
321
|
+
var d = this.y.redMul(p.y);
|
322
|
+
// E = d * C * D
|
323
|
+
var e = this.curve.d.redMul(c).redMul(d);
|
324
|
+
// F = B - E
|
325
|
+
var f = b.redSub(e);
|
326
|
+
// G = B + E
|
327
|
+
var g = b.redAdd(e);
|
328
|
+
// X3 = A * F * ((X1 + Y1) * (X2 + Y2) - C - D)
|
329
|
+
var tmp = this.x.redAdd(this.y).redMul(p.x.redAdd(p.y)).redISub(c).redISub(d);
|
330
|
+
var nx = a.redMul(f).redMul(tmp);
|
331
|
+
var ny;
|
332
|
+
var nz;
|
333
|
+
if (this.curve.twisted) {
|
334
|
+
// Y3 = A * G * (D - a * C)
|
335
|
+
ny = a.redMul(g).redMul(d.redSub(this.curve._mulA(c)));
|
336
|
+
// Z3 = F * G
|
337
|
+
nz = f.redMul(g);
|
338
|
+
} else {
|
339
|
+
// Y3 = A * G * (D - C)
|
340
|
+
ny = a.redMul(g).redMul(d.redSub(c));
|
341
|
+
// Z3 = c * F * G
|
342
|
+
nz = this.curve._mulC(f).redMul(g);
|
343
|
+
}
|
344
|
+
return this.curve.point(nx, ny, nz);
|
345
|
+
};
|
346
|
+
|
347
|
+
Point.prototype.add = function add(p) {
|
348
|
+
if (this.isInfinity())
|
349
|
+
return p;
|
350
|
+
if (p.isInfinity())
|
351
|
+
return this;
|
352
|
+
|
353
|
+
if (this.curve.extended)
|
354
|
+
return this._extAdd(p);
|
355
|
+
else
|
356
|
+
return this._projAdd(p);
|
357
|
+
};
|
358
|
+
|
359
|
+
Point.prototype.mul = function mul(k) {
|
360
|
+
if (this._hasDoubles(k))
|
361
|
+
return this.curve._fixedNafMul(this, k);
|
362
|
+
else
|
363
|
+
return this.curve._wnafMul(this, k);
|
364
|
+
};
|
365
|
+
|
366
|
+
Point.prototype.mulAdd = function mulAdd(k1, p, k2) {
|
367
|
+
return this.curve._wnafMulAdd(1, [ this, p ], [ k1, k2 ], 2, false);
|
368
|
+
};
|
369
|
+
|
370
|
+
Point.prototype.jmulAdd = function jmulAdd(k1, p, k2) {
|
371
|
+
return this.curve._wnafMulAdd(1, [ this, p ], [ k1, k2 ], 2, true);
|
372
|
+
};
|
373
|
+
|
374
|
+
Point.prototype.normalize = function normalize() {
|
375
|
+
if (this.zOne)
|
376
|
+
return this;
|
377
|
+
|
378
|
+
// Normalize coordinates
|
379
|
+
var zi = this.z.redInvm();
|
380
|
+
this.x = this.x.redMul(zi);
|
381
|
+
this.y = this.y.redMul(zi);
|
382
|
+
if (this.t)
|
383
|
+
this.t = this.t.redMul(zi);
|
384
|
+
this.z = this.curve.one;
|
385
|
+
this.zOne = true;
|
386
|
+
return this;
|
387
|
+
};
|
388
|
+
|
389
|
+
Point.prototype.neg = function neg() {
|
390
|
+
return this.curve.point(this.x.redNeg(),
|
391
|
+
this.y,
|
392
|
+
this.z,
|
393
|
+
this.t && this.t.redNeg());
|
394
|
+
};
|
395
|
+
|
396
|
+
Point.prototype.getX = function getX() {
|
397
|
+
this.normalize();
|
398
|
+
return this.x.fromRed();
|
399
|
+
};
|
400
|
+
|
401
|
+
Point.prototype.getY = function getY() {
|
402
|
+
this.normalize();
|
403
|
+
return this.y.fromRed();
|
404
|
+
};
|
405
|
+
|
406
|
+
Point.prototype.eq = function eq(other) {
|
407
|
+
return this === other ||
|
408
|
+
this.getX().cmp(other.getX()) === 0 &&
|
409
|
+
this.getY().cmp(other.getY()) === 0;
|
410
|
+
};
|
411
|
+
|
412
|
+
Point.prototype.eqXToP = function eqXToP(x) {
|
413
|
+
var rx = x.toRed(this.curve.red).redMul(this.z);
|
414
|
+
if (this.x.cmp(rx) === 0)
|
415
|
+
return true;
|
416
|
+
|
417
|
+
var xc = x.clone();
|
418
|
+
var t = this.curve.redN.redMul(this.z);
|
419
|
+
for (;;) {
|
420
|
+
xc.iadd(this.curve.n);
|
421
|
+
if (xc.cmp(this.curve.p) >= 0)
|
422
|
+
return false;
|
423
|
+
|
424
|
+
rx.redIAdd(t);
|
425
|
+
if (this.x.cmp(rx) === 0)
|
426
|
+
return true;
|
427
|
+
}
|
428
|
+
return false;
|
429
|
+
};
|
430
|
+
|
431
|
+
// Compatibility with BaseCurve
|
432
|
+
Point.prototype.toP = Point.prototype.normalize;
|
433
|
+
Point.prototype.mixedAdd = Point.prototype.add;
|
@@ -0,0 +1,180 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var curve = require('../curve');
|
4
|
+
var BN = require('../../../../BN/bn');
|
5
|
+
var inherits = require('../../elliptic').inherits;
|
6
|
+
var Base = curve.base;
|
7
|
+
|
8
|
+
var elliptic = require('../../elliptic');
|
9
|
+
var utils = elliptic.utils;
|
10
|
+
|
11
|
+
function MontCurve(conf) {
|
12
|
+
Base.call(this, 'mont', 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.i4 = new BN(4).toRed(this.red).redInvm();
|
17
|
+
this.two = new BN(2).toRed(this.red);
|
18
|
+
this.a24 = this.i4.redMul(this.a.redAdd(this.two));
|
19
|
+
}
|
20
|
+
inherits(MontCurve, Base);
|
21
|
+
module.exports = MontCurve;
|
22
|
+
|
23
|
+
MontCurve.prototype.validate = function validate(point) {
|
24
|
+
var x = point.normalize().x;
|
25
|
+
var x2 = x.redSqr();
|
26
|
+
var rhs = x2.redMul(x).redAdd(x2.redMul(this.a)).redAdd(x);
|
27
|
+
var y = rhs.redSqrt();
|
28
|
+
|
29
|
+
return y.redSqr().cmp(rhs) === 0;
|
30
|
+
};
|
31
|
+
|
32
|
+
function Point(curve, x, z) {
|
33
|
+
Base.BasePoint.call(this, curve, 'projective');
|
34
|
+
if (x === null && z === null) {
|
35
|
+
this.x = this.curve.one;
|
36
|
+
this.z = this.curve.zero;
|
37
|
+
} else {
|
38
|
+
this.x = new BN(x, 16);
|
39
|
+
this.z = new BN(z, 16);
|
40
|
+
if (!this.x.red)
|
41
|
+
this.x = this.x.toRed(this.curve.red);
|
42
|
+
if (!this.z.red)
|
43
|
+
this.z = this.z.toRed(this.curve.red);
|
44
|
+
}
|
45
|
+
}
|
46
|
+
inherits(Point, Base.BasePoint);
|
47
|
+
|
48
|
+
MontCurve.prototype.decodePoint = function decodePoint(bytes, enc) {
|
49
|
+
return this.point(utils.toArray(bytes, enc), 1);
|
50
|
+
};
|
51
|
+
|
52
|
+
MontCurve.prototype.point = function point(x, z) {
|
53
|
+
return new Point(this, x, z);
|
54
|
+
};
|
55
|
+
|
56
|
+
MontCurve.prototype.pointFromJSON = function pointFromJSON(obj) {
|
57
|
+
return Point.fromJSON(this, obj);
|
58
|
+
};
|
59
|
+
|
60
|
+
Point.prototype.precompute = function precompute() {
|
61
|
+
// No-op
|
62
|
+
};
|
63
|
+
|
64
|
+
Point.prototype._encode = function _encode() {
|
65
|
+
return this.getX().toArray('be', this.curve.p.byteLength());
|
66
|
+
};
|
67
|
+
|
68
|
+
Point.fromJSON = function fromJSON(curve, obj) {
|
69
|
+
return new Point(curve, obj[0], obj[1] || curve.one);
|
70
|
+
};
|
71
|
+
|
72
|
+
Point.prototype.inspect = function inspect() {
|
73
|
+
if (this.isInfinity())
|
74
|
+
return '<EC Point Infinity>';
|
75
|
+
return '<EC Point x: ' + this.x.fromRed().toString(16, 2) +
|
76
|
+
' z: ' + this.z.fromRed().toString(16, 2) + '>';
|
77
|
+
};
|
78
|
+
|
79
|
+
Point.prototype.isInfinity = function isInfinity() {
|
80
|
+
// XXX This code assumes that zero is always zero in red
|
81
|
+
return this.z.cmpn(0) === 0;
|
82
|
+
};
|
83
|
+
|
84
|
+
Point.prototype.dbl = function dbl() {
|
85
|
+
// http://hyperelliptic.org/EFD/g1p/auto-montgom-xz.html#doubling-dbl-1987-m-3
|
86
|
+
// 2M + 2S + 4A
|
87
|
+
|
88
|
+
// A = X1 + Z1
|
89
|
+
var a = this.x.redAdd(this.z);
|
90
|
+
// AA = A^2
|
91
|
+
var aa = a.redSqr();
|
92
|
+
// B = X1 - Z1
|
93
|
+
var b = this.x.redSub(this.z);
|
94
|
+
// BB = B^2
|
95
|
+
var bb = b.redSqr();
|
96
|
+
// C = AA - BB
|
97
|
+
var c = aa.redSub(bb);
|
98
|
+
// X3 = AA * BB
|
99
|
+
var nx = aa.redMul(bb);
|
100
|
+
// Z3 = C * (BB + A24 * C)
|
101
|
+
var nz = c.redMul(bb.redAdd(this.curve.a24.redMul(c)));
|
102
|
+
return this.curve.point(nx, nz);
|
103
|
+
};
|
104
|
+
|
105
|
+
Point.prototype.add = function add() {
|
106
|
+
throw new Error('Not supported on Montgomery curve');
|
107
|
+
};
|
108
|
+
|
109
|
+
Point.prototype.diffAdd = function diffAdd(p, diff) {
|
110
|
+
// http://hyperelliptic.org/EFD/g1p/auto-montgom-xz.html#diffadd-dadd-1987-m-3
|
111
|
+
// 4M + 2S + 6A
|
112
|
+
|
113
|
+
// A = X2 + Z2
|
114
|
+
var a = this.x.redAdd(this.z);
|
115
|
+
// B = X2 - Z2
|
116
|
+
var b = this.x.redSub(this.z);
|
117
|
+
// C = X3 + Z3
|
118
|
+
var c = p.x.redAdd(p.z);
|
119
|
+
// D = X3 - Z3
|
120
|
+
var d = p.x.redSub(p.z);
|
121
|
+
// DA = D * A
|
122
|
+
var da = d.redMul(a);
|
123
|
+
// CB = C * B
|
124
|
+
var cb = c.redMul(b);
|
125
|
+
// X5 = Z1 * (DA + CB)^2
|
126
|
+
var nx = diff.z.redMul(da.redAdd(cb).redSqr());
|
127
|
+
// Z5 = X1 * (DA - CB)^2
|
128
|
+
var nz = diff.x.redMul(da.redISub(cb).redSqr());
|
129
|
+
return this.curve.point(nx, nz);
|
130
|
+
};
|
131
|
+
|
132
|
+
Point.prototype.mul = function mul(k) {
|
133
|
+
var t = k.clone();
|
134
|
+
var a = this; // (N / 2) * Q + Q
|
135
|
+
var b = this.curve.point(null, null); // (N / 2) * Q
|
136
|
+
var c = this; // Q
|
137
|
+
|
138
|
+
for (var bits = []; t.cmpn(0) !== 0; t.iushrn(1))
|
139
|
+
bits.push(t.andln(1));
|
140
|
+
|
141
|
+
for (var i = bits.length - 1; i >= 0; i--) {
|
142
|
+
if (bits[i] === 0) {
|
143
|
+
// N * Q + Q = ((N / 2) * Q + Q)) + (N / 2) * Q
|
144
|
+
a = a.diffAdd(b, c);
|
145
|
+
// N * Q = 2 * ((N / 2) * Q + Q))
|
146
|
+
b = b.dbl();
|
147
|
+
} else {
|
148
|
+
// N * Q = ((N / 2) * Q + Q) + ((N / 2) * Q)
|
149
|
+
b = a.diffAdd(b, c);
|
150
|
+
// N * Q + Q = 2 * ((N / 2) * Q + Q)
|
151
|
+
a = a.dbl();
|
152
|
+
}
|
153
|
+
}
|
154
|
+
return b;
|
155
|
+
};
|
156
|
+
|
157
|
+
Point.prototype.mulAdd = function mulAdd() {
|
158
|
+
throw new Error('Not supported on Montgomery curve');
|
159
|
+
};
|
160
|
+
|
161
|
+
Point.prototype.jumlAdd = function jumlAdd() {
|
162
|
+
throw new Error('Not supported on Montgomery curve');
|
163
|
+
};
|
164
|
+
|
165
|
+
Point.prototype.eq = function eq(other) {
|
166
|
+
return this.getX().cmp(other.getX()) === 0;
|
167
|
+
};
|
168
|
+
|
169
|
+
Point.prototype.normalize = function normalize() {
|
170
|
+
this.x = this.x.redMul(this.z.redInvm());
|
171
|
+
this.z = this.curve.one;
|
172
|
+
return this;
|
173
|
+
};
|
174
|
+
|
175
|
+
Point.prototype.getX = function getX() {
|
176
|
+
// Normalize coordinates
|
177
|
+
this.normalize();
|
178
|
+
|
179
|
+
return this.x.fromRed();
|
180
|
+
};
|