@blockend/widget 1.0.31 → 1.0.33
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/dist/AllRoutes-B04uZfaa.js +2209 -0
- package/dist/Box-BWDJCGbI.js +55 -0
- package/dist/EstGas-QSPrdq3Y.js +12863 -0
- package/dist/Grow-37t11tle.js +1656 -0
- package/dist/InputComp-bKVjlcbT.js +106 -0
- package/dist/LoadRoute-C3vokLk0.js +126 -0
- package/dist/QuotesProgress-DlgLQ2zo.js +28 -0
- package/dist/SwapAsset-tD6wSJDD.js +64 -0
- package/dist/Tooltip-BqYgFn1F.js +572 -0
- package/dist/TxnButton-DefErcCE.js +207 -0
- package/dist/WidgetForm.module-QHJ0-m5f.js +9 -0
- package/dist/{add-B8AfyNIx.js → add-D32G_xKI.js} +3 -3
- package/dist/{all-wallets-BaLLU39i.js → all-wallets-LjjqiTnJ.js} +3 -3
- package/dist/{app-store-DNpoABj1.js → app-store-S7C23kTz.js} +3 -3
- package/dist/{apple-DzzHtaL4.js → apple-DMd4RArz.js} +3 -3
- package/dist/{arrow-bottom-D2c13c51.js → arrow-bottom-C0cVMl2n.js} +3 -3
- package/dist/{arrow-bottom-circle-ls9OCLx1.js → arrow-bottom-circle-veMvHYVR.js} +3 -3
- package/dist/{arrow-left-BI3rQkNI.js → arrow-left-B6VdOxSY.js} +3 -3
- package/dist/{arrow-right-rnMzhVod.js → arrow-right-CyrokJyU.js} +3 -3
- package/dist/{arrow-top-CfA6PVMz.js → arrow-top-S8qh5z92.js} +3 -3
- package/dist/{bank-CmpAmHgg.js → bank-B9ySjxm-.js} +3 -3
- package/dist/basic-BXha5sM_.js +5783 -0
- package/dist/{browser-mEcjgS1H.js → browser-CWmyZDF1.js} +3 -3
- package/dist/{card-DkgIWcMc.js → card-TzoSVbiE.js} +3 -3
- package/dist/ccip-B97isPV8.js +228 -0
- package/dist/{checkmark-B_A013n2.js → checkmark-Be4tNZY8.js} +3 -3
- package/dist/{checkmark-bold-DestuG2T.js → checkmark-bold-sAoooMoy.js} +3 -3
- package/dist/{chevron-bottom-Di3S86dj.js → chevron-bottom-Cdd52Yir.js} +3 -3
- package/dist/{chevron-left-A9Wj-3tj.js → chevron-left-Dp8rM2qD.js} +3 -3
- package/dist/{chevron-right-UFxJogOZ.js → chevron-right-kH3Pgt8h.js} +3 -3
- package/dist/{chevron-top-BbZs3eaC.js → chevron-top-lx-9iWm1.js} +3 -3
- package/dist/{chrome-store-B4F6hJmy.js → chrome-store-BGxPgdkC.js} +3 -3
- package/dist/{clock-BbAQ09uV.js → clock-B3r9UiFY.js} +3 -3
- package/dist/clockIcon-C2W5xvyt.js +76 -0
- package/dist/{close-CY3oZP_D.js → close-Cc6SJ0A9.js} +3 -3
- package/dist/{coinPlaceholder-DpfsobZT.js → coinPlaceholder-DqH2n2v1.js} +3 -3
- package/dist/{compass-CHS7RZFk.js → compass-CiKYczAi.js} +3 -3
- package/dist/{copy-CEd4nGhs.js → copy-DR0dlbF5.js} +3 -3
- package/dist/core-D9oRINim.js +14967 -0
- package/dist/{cursor-transparent-Tm6k6Ws4.js → cursor-transparent-BABlb938.js} +3 -3
- package/dist/cursor-vv-pXAzC.js +7 -0
- package/dist/{desktop-CrSyXZkL.js → desktop-aFWdAU4O.js} +3 -3
- package/dist/{disconnect-BooT10E1.js → disconnect-Cz0MwjuJ.js} +3 -3
- package/dist/{discord-B982Qaml.js → discord-BkNiORDh.js} +3 -3
- package/dist/down-BAe_uoMx.js +606 -0
- package/dist/{etherscan-CFL0IcNl.js → etherscan-4wT3rhj5.js} +3 -3
- package/dist/ethertousd-CC_ezooz.js +6 -0
- package/dist/{exclamation-triangle-BBeGXYgt.js → exclamation-triangle-CSv7sjYs.js} +3 -3
- package/dist/{extension-C5WqJw73.js → extension-B4acJb16.js} +3 -3
- package/dist/{external-link-CcSxVDcd.js → external-link-zFdRyGh8.js} +3 -3
- package/dist/{facebook-DF0v9ED8.js → facebook-D2h4REjy.js} +3 -3
- package/dist/{farcaster-Boq7M4VK.js → farcaster-acRwWMSn.js} +3 -3
- package/dist/{filters-DfeymbPx.js → filters-DE1yHCJy.js} +3 -3
- package/dist/getChainIds-B4BLdhKd.js +1888 -0
- package/dist/getCosmosAddress-BfMdDDaH.js +9565 -0
- package/dist/{github-DQ8FV4bD.js → github-BrCBPjDS.js} +3 -3
- package/dist/{google-Dm8FDDZR.js → google-DYC6Um96.js} +3 -3
- package/dist/hamburger-sg42nvNd.js +3915 -0
- package/dist/{help-circle-CbgiAebR.js → help-circle-CKvIeM4J.js} +3 -3
- package/dist/{id-CzqxUksI.js → id-FXzF1V3L.js} +3 -3
- package/dist/{image-CsCGTIN-.js → image-rOZMtN9o.js} +3 -3
- package/dist/index-B0E-rGOh.js +159 -0
- package/dist/index-B5gV3APh.js +992 -0
- package/dist/index-CDq3vNnP.js +22 -0
- package/dist/index-CIjTGwYH.js +9 -0
- package/dist/index-Cmj3dp3U.js +473 -0
- package/dist/index-DEovHqJS.js +3010 -0
- package/dist/index-DOcwHfd3.js +13583 -0
- package/dist/index-DSlXIHtr.js +28701 -0
- package/dist/index-DTish3yr.js +1014 -0
- package/dist/index-PIAjHNgG.js +18613 -0
- package/dist/index-SiMIoFT9.js +331 -0
- package/dist/index-Y94UWILI.js +1460 -0
- package/dist/index-baetUfmH.js +252 -0
- package/dist/index.es-D6RvS0tB.js +366 -0
- package/dist/index.es-TLoHC_Sf.js +11678 -0
- package/dist/index.es.js +4 -4
- package/dist/{info-circle-Bt350Yzj.js → info-circle-BYfEprqC.js} +3 -3
- package/dist/{info-fKCfLW0k.js → info-dh7qmm_R.js} +3 -3
- package/dist/{lightbulb-BL9YBxiA.js → lightbulb-3S8w5B31.js} +3 -3
- package/dist/{mail-_g6CHQeQ.js → mail-DgcPoOPs.js} +3 -3
- package/dist/{mobile-B-EQ4IcH.js → mobile-C8egRR0V.js} +3 -3
- package/dist/{more-C3rpOdUy.js → more-CU_oPwwY.js} +3 -3
- package/dist/{network-placeholder-D9Ak7Xj_.js → network-placeholder-CqjaxlS7.js} +3 -3
- package/dist/{nftPlaceholder-CIIm2V_o.js → nftPlaceholder-D6mZlZmz.js} +3 -3
- package/dist/{off-DZjgI0eE.js → off-DHeUICx6.js} +3 -3
- package/dist/{play-store-DvRnagoF.js → play-store-DyGxjYv5.js} +3 -3
- package/dist/{plus-C2jN2huZ.js → plus-k4hEdLAH.js} +3 -3
- package/dist/{qr-code-B64HnZgi.js → qr-code-CkavLc0Z.js} +3 -3
- package/dist/{recycle-horizontal-CovcJGa_.js → recycle-horizontal-YzkyX2fY.js} +3 -3
- package/dist/{refresh-J1Vw6QMA.js → refresh-E1vpdqCO.js} +3 -3
- package/dist/{reown-logo-BsXoZ3KT.js → reown-logo-BZbc_-R6.js} +3 -3
- package/dist/{search-BC86wYXR.js → search-B9vQkIsQ.js} +3 -3
- package/dist/{send-BPA1bK5G.js → send-C9ROxhya.js} +3 -3
- package/dist/shareicon-CLveBf8l.js +262 -0
- package/dist/{sortDown-cDA6MpOw.js → sortDown-CjBaDQTU.js} +13 -13
- package/dist/{swapHorizontal-47_8ePle.js → swapHorizontal-BhLDDuiL.js} +3 -3
- package/dist/{swapHorizontalBold-CJ7TzeQP.js → swapHorizontalBold-B1IxJLem.js} +3 -3
- package/dist/{swapHorizontalMedium-CTACgUR9.js → swapHorizontalMedium--fvn_6KH.js} +3 -3
- package/dist/{swapHorizontalRoundedBold-Ht3_LABp.js → swapHorizontalRoundedBold-fZowAp5m.js} +3 -3
- package/dist/{swapVertical-DlS0V6DD.js → swapVertical-CDwndQ4s.js} +3 -3
- package/dist/{telegram-UZ00kvIE.js → telegram-BLbguJ3E.js} +3 -3
- package/dist/{three-dots-D9zVXr1j.js → three-dots-DyIG0Aiq.js} +3 -3
- package/dist/truncate-D9t92mVX.js +7 -0
- package/dist/{twitch-CP6rpz4m.js → twitch-054mPrps.js} +3 -3
- package/dist/{twitterIcon-Cm3I3FRU.js → twitterIcon-DrlgKmas.js} +3 -3
- package/dist/unsupportedProp-Dr6Tadlk.js +10 -0
- package/dist/useRegisterError-C52MuupC.js +9857 -0
- package/dist/validError-CDlJpg_f.js +6 -0
- package/dist/{verify-filled-CfgydKPa.js → verify-filled-Dw1_4VEj.js} +3 -3
- package/dist/{verify-DuUE85WT.js → verify-uz7q7EMX.js} +3 -3
- package/dist/w3m-modal-CJM8um8G.js +1471 -0
- package/dist/{wallet-BC2LSDlF.js → wallet-a9cCMI05.js} +3 -3
- package/dist/{wallet-placeholder-DugEJ7xM.js → wallet-placeholder-Cqiz2_rA.js} +3 -3
- package/dist/{walletconnect-wayCaYfk.js → walletconnect-96twp7MP.js} +7 -9
- package/dist/{warning-circle-Cy9YuvwU.js → warning-circle-C4mV6fIZ.js} +3 -3
- package/dist/{x-jggPFiy-.js → x-vRe7FHUS.js} +3 -3
- package/package.json +7 -19
- package/dist/AllRoutes-Bdi5ZW8W.js +0 -2604
- package/dist/Box-e4gA-y_H.js +0 -60
- package/dist/EstGas-C5ihS0Mw.js +0 -17108
- package/dist/Grow-B4K-I-E-.js +0 -2212
- package/dist/InputComp-C_UUZxNU.js +0 -114
- package/dist/LoadRoute-CnDhVKdh.js +0 -142
- package/dist/QuotesProgress-DxgPgPJE.js +0 -29
- package/dist/SwapAsset-CmmuxeQF.js +0 -65
- package/dist/Tooltip-D8YjOOKU.js +0 -718
- package/dist/TxnButton-CF-TLZrD.js +0 -302
- package/dist/WidgetForm.module-DhUUlwZP.js +0 -13
- package/dist/basic-Cx2xxgd9.js +0 -7109
- package/dist/ccip-BKpquA5i.js +0 -250
- package/dist/clockIcon-jSNFFrLy.js +0 -76
- package/dist/core-C-gr9X18.js +0 -17217
- package/dist/cursor-BTtIlTHP.js +0 -7
- package/dist/down-DDk8OoJG.js +0 -809
- package/dist/ethertousd-BOknTgU4.js +0 -7
- package/dist/getChainIds-6iugk2h0.js +0 -2431
- package/dist/getCosmosAddress-BgD5iV6f.js +0 -13998
- package/dist/hamburger-BYe_G11E.js +0 -4562
- package/dist/index-BAmjBHYR.js +0 -1087
- package/dist/index-BxHeZ9IF.js +0 -543
- package/dist/index-C7cb2O7r.js +0 -16078
- package/dist/index-CF9p4S_X.js +0 -11
- package/dist/index-C_vo1L7p.js +0 -24384
- package/dist/index-Ch7w83dW.js +0 -4217
- package/dist/index-Crhenmgw.js +0 -188
- package/dist/index-Cz5Dosd2.js +0 -412
- package/dist/index-D5OnT-__.js +0 -35858
- package/dist/index-DArR1lwi.js +0 -1759
- package/dist/index-hEJSpZe-.js +0 -308
- package/dist/index-m2O0cbD8.js +0 -1883
- package/dist/index-sLx4Edw4.js +0 -22
- package/dist/index.es-DIbGJsfa.js +0 -475
- package/dist/index.es-t6sHlseh.js +0 -12916
- package/dist/secp256k1-BkBK0mD5.js +0 -1681
- package/dist/shareicon-C88Wx-AM.js +0 -314
- package/dist/truncate-DSxFWEga.js +0 -7
- package/dist/unsupportedProp-DkeDzLfM.js +0 -14
- package/dist/useRegisterError-W_UfecIN.js +0 -19288
- package/dist/validError-rariWQ7G.js +0 -14
- package/dist/w3m-modal-Bl-DPlah.js +0 -1721
@@ -1,1681 +0,0 @@
|
|
1
|
-
import { aG as anumber, x as randomBytes, aH as hmac, J as concatBytes$1, aI as sha256 } from "./index-D5OnT-__.js";
|
2
|
-
const _0n$4 = /* @__PURE__ */ BigInt(0);
|
3
|
-
const _1n$4 = /* @__PURE__ */ BigInt(1);
|
4
|
-
function isBytes(a) {
|
5
|
-
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
6
|
-
}
|
7
|
-
function abytes(item) {
|
8
|
-
if (!isBytes(item))
|
9
|
-
throw new Error("Uint8Array expected");
|
10
|
-
}
|
11
|
-
function abool(title, value) {
|
12
|
-
if (typeof value !== "boolean")
|
13
|
-
throw new Error(title + " boolean expected, got " + value);
|
14
|
-
}
|
15
|
-
function numberToHexUnpadded(num) {
|
16
|
-
const hex = num.toString(16);
|
17
|
-
return hex.length & 1 ? "0" + hex : hex;
|
18
|
-
}
|
19
|
-
function hexToNumber(hex) {
|
20
|
-
if (typeof hex !== "string")
|
21
|
-
throw new Error("hex string expected, got " + typeof hex);
|
22
|
-
return hex === "" ? _0n$4 : BigInt("0x" + hex);
|
23
|
-
}
|
24
|
-
const hasHexBuiltin = (
|
25
|
-
// @ts-ignore
|
26
|
-
typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function"
|
27
|
-
);
|
28
|
-
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
29
|
-
function bytesToHex(bytes) {
|
30
|
-
abytes(bytes);
|
31
|
-
if (hasHexBuiltin)
|
32
|
-
return bytes.toHex();
|
33
|
-
let hex = "";
|
34
|
-
for (let i = 0; i < bytes.length; i++) {
|
35
|
-
hex += hexes[bytes[i]];
|
36
|
-
}
|
37
|
-
return hex;
|
38
|
-
}
|
39
|
-
const asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
40
|
-
function asciiToBase16(ch) {
|
41
|
-
if (ch >= asciis._0 && ch <= asciis._9)
|
42
|
-
return ch - asciis._0;
|
43
|
-
if (ch >= asciis.A && ch <= asciis.F)
|
44
|
-
return ch - (asciis.A - 10);
|
45
|
-
if (ch >= asciis.a && ch <= asciis.f)
|
46
|
-
return ch - (asciis.a - 10);
|
47
|
-
return;
|
48
|
-
}
|
49
|
-
function hexToBytes(hex) {
|
50
|
-
if (typeof hex !== "string")
|
51
|
-
throw new Error("hex string expected, got " + typeof hex);
|
52
|
-
if (hasHexBuiltin)
|
53
|
-
return Uint8Array.fromHex(hex);
|
54
|
-
const hl = hex.length;
|
55
|
-
const al = hl / 2;
|
56
|
-
if (hl % 2)
|
57
|
-
throw new Error("hex string expected, got unpadded hex of length " + hl);
|
58
|
-
const array = new Uint8Array(al);
|
59
|
-
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
60
|
-
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
61
|
-
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
62
|
-
if (n1 === void 0 || n2 === void 0) {
|
63
|
-
const char = hex[hi] + hex[hi + 1];
|
64
|
-
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
65
|
-
}
|
66
|
-
array[ai] = n1 * 16 + n2;
|
67
|
-
}
|
68
|
-
return array;
|
69
|
-
}
|
70
|
-
function bytesToNumberBE(bytes) {
|
71
|
-
return hexToNumber(bytesToHex(bytes));
|
72
|
-
}
|
73
|
-
function bytesToNumberLE(bytes) {
|
74
|
-
abytes(bytes);
|
75
|
-
return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse()));
|
76
|
-
}
|
77
|
-
function numberToBytesBE(n, len) {
|
78
|
-
return hexToBytes(n.toString(16).padStart(len * 2, "0"));
|
79
|
-
}
|
80
|
-
function numberToBytesLE(n, len) {
|
81
|
-
return numberToBytesBE(n, len).reverse();
|
82
|
-
}
|
83
|
-
function ensureBytes(title, hex, expectedLength) {
|
84
|
-
let res;
|
85
|
-
if (typeof hex === "string") {
|
86
|
-
try {
|
87
|
-
res = hexToBytes(hex);
|
88
|
-
} catch (e) {
|
89
|
-
throw new Error(title + " must be hex string or Uint8Array, cause: " + e);
|
90
|
-
}
|
91
|
-
} else if (isBytes(hex)) {
|
92
|
-
res = Uint8Array.from(hex);
|
93
|
-
} else {
|
94
|
-
throw new Error(title + " must be hex string or Uint8Array");
|
95
|
-
}
|
96
|
-
const len = res.length;
|
97
|
-
if (typeof expectedLength === "number" && len !== expectedLength)
|
98
|
-
throw new Error(title + " of length " + expectedLength + " expected, got " + len);
|
99
|
-
return res;
|
100
|
-
}
|
101
|
-
function concatBytes(...arrays) {
|
102
|
-
let sum = 0;
|
103
|
-
for (let i = 0; i < arrays.length; i++) {
|
104
|
-
const a = arrays[i];
|
105
|
-
abytes(a);
|
106
|
-
sum += a.length;
|
107
|
-
}
|
108
|
-
const res = new Uint8Array(sum);
|
109
|
-
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
110
|
-
const a = arrays[i];
|
111
|
-
res.set(a, pad);
|
112
|
-
pad += a.length;
|
113
|
-
}
|
114
|
-
return res;
|
115
|
-
}
|
116
|
-
const isPosBig = (n) => typeof n === "bigint" && _0n$4 <= n;
|
117
|
-
function inRange(n, min, max) {
|
118
|
-
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
119
|
-
}
|
120
|
-
function aInRange(title, n, min, max) {
|
121
|
-
if (!inRange(n, min, max))
|
122
|
-
throw new Error("expected valid " + title + ": " + min + " <= n < " + max + ", got " + n);
|
123
|
-
}
|
124
|
-
function bitLen(n) {
|
125
|
-
let len;
|
126
|
-
for (len = 0; n > _0n$4; n >>= _1n$4, len += 1)
|
127
|
-
;
|
128
|
-
return len;
|
129
|
-
}
|
130
|
-
const bitMask = (n) => (_1n$4 << BigInt(n)) - _1n$4;
|
131
|
-
const u8n = (len) => new Uint8Array(len);
|
132
|
-
const u8fr = (arr) => Uint8Array.from(arr);
|
133
|
-
function createHmacDrbg(hashLen, qByteLen, hmacFn) {
|
134
|
-
if (typeof hashLen !== "number" || hashLen < 2)
|
135
|
-
throw new Error("hashLen must be a number");
|
136
|
-
if (typeof qByteLen !== "number" || qByteLen < 2)
|
137
|
-
throw new Error("qByteLen must be a number");
|
138
|
-
if (typeof hmacFn !== "function")
|
139
|
-
throw new Error("hmacFn must be a function");
|
140
|
-
let v = u8n(hashLen);
|
141
|
-
let k = u8n(hashLen);
|
142
|
-
let i = 0;
|
143
|
-
const reset = () => {
|
144
|
-
v.fill(1);
|
145
|
-
k.fill(0);
|
146
|
-
i = 0;
|
147
|
-
};
|
148
|
-
const h = (...b) => hmacFn(k, v, ...b);
|
149
|
-
const reseed = (seed = u8n(0)) => {
|
150
|
-
k = h(u8fr([0]), seed);
|
151
|
-
v = h();
|
152
|
-
if (seed.length === 0)
|
153
|
-
return;
|
154
|
-
k = h(u8fr([1]), seed);
|
155
|
-
v = h();
|
156
|
-
};
|
157
|
-
const gen = () => {
|
158
|
-
if (i++ >= 1e3)
|
159
|
-
throw new Error("drbg: tried 1000 values");
|
160
|
-
let len = 0;
|
161
|
-
const out = [];
|
162
|
-
while (len < qByteLen) {
|
163
|
-
v = h();
|
164
|
-
const sl = v.slice();
|
165
|
-
out.push(sl);
|
166
|
-
len += v.length;
|
167
|
-
}
|
168
|
-
return concatBytes(...out);
|
169
|
-
};
|
170
|
-
const genUntil = (seed, pred) => {
|
171
|
-
reset();
|
172
|
-
reseed(seed);
|
173
|
-
let res = void 0;
|
174
|
-
while (!(res = pred(gen())))
|
175
|
-
reseed();
|
176
|
-
reset();
|
177
|
-
return res;
|
178
|
-
};
|
179
|
-
return genUntil;
|
180
|
-
}
|
181
|
-
const validatorFns = {
|
182
|
-
bigint: (val) => typeof val === "bigint",
|
183
|
-
function: (val) => typeof val === "function",
|
184
|
-
boolean: (val) => typeof val === "boolean",
|
185
|
-
string: (val) => typeof val === "string",
|
186
|
-
stringOrUint8Array: (val) => typeof val === "string" || isBytes(val),
|
187
|
-
isSafeInteger: (val) => Number.isSafeInteger(val),
|
188
|
-
array: (val) => Array.isArray(val),
|
189
|
-
field: (val, object) => object.Fp.isValid(val),
|
190
|
-
hash: (val) => typeof val === "function" && Number.isSafeInteger(val.outputLen)
|
191
|
-
};
|
192
|
-
function validateObject(object, validators, optValidators = {}) {
|
193
|
-
const checkField = (fieldName, type, isOptional) => {
|
194
|
-
const checkVal = validatorFns[type];
|
195
|
-
if (typeof checkVal !== "function")
|
196
|
-
throw new Error("invalid validator function");
|
197
|
-
const val = object[fieldName];
|
198
|
-
if (isOptional && val === void 0)
|
199
|
-
return;
|
200
|
-
if (!checkVal(val, object)) {
|
201
|
-
throw new Error("param " + String(fieldName) + " is invalid. Expected " + type + ", got " + val);
|
202
|
-
}
|
203
|
-
};
|
204
|
-
for (const [fieldName, type] of Object.entries(validators))
|
205
|
-
checkField(fieldName, type, false);
|
206
|
-
for (const [fieldName, type] of Object.entries(optValidators))
|
207
|
-
checkField(fieldName, type, true);
|
208
|
-
return object;
|
209
|
-
}
|
210
|
-
function memoized(fn) {
|
211
|
-
const map = /* @__PURE__ */ new WeakMap();
|
212
|
-
return (arg, ...args) => {
|
213
|
-
const val = map.get(arg);
|
214
|
-
if (val !== void 0)
|
215
|
-
return val;
|
216
|
-
const computed = fn(arg, ...args);
|
217
|
-
map.set(arg, computed);
|
218
|
-
return computed;
|
219
|
-
};
|
220
|
-
}
|
221
|
-
const _0n$3 = BigInt(0), _1n$3 = BigInt(1), _2n$1 = /* @__PURE__ */ BigInt(2), _3n$1 = /* @__PURE__ */ BigInt(3);
|
222
|
-
const _4n$1 = /* @__PURE__ */ BigInt(4), _5n = /* @__PURE__ */ BigInt(5), _8n = /* @__PURE__ */ BigInt(8);
|
223
|
-
function mod(a, b) {
|
224
|
-
const result = a % b;
|
225
|
-
return result >= _0n$3 ? result : b + result;
|
226
|
-
}
|
227
|
-
function pow2(x, power, modulo) {
|
228
|
-
let res = x;
|
229
|
-
while (power-- > _0n$3) {
|
230
|
-
res *= res;
|
231
|
-
res %= modulo;
|
232
|
-
}
|
233
|
-
return res;
|
234
|
-
}
|
235
|
-
function invert(number, modulo) {
|
236
|
-
if (number === _0n$3)
|
237
|
-
throw new Error("invert: expected non-zero number");
|
238
|
-
if (modulo <= _0n$3)
|
239
|
-
throw new Error("invert: expected positive modulus, got " + modulo);
|
240
|
-
let a = mod(number, modulo);
|
241
|
-
let b = modulo;
|
242
|
-
let x = _0n$3, u = _1n$3;
|
243
|
-
while (a !== _0n$3) {
|
244
|
-
const q = b / a;
|
245
|
-
const r = b % a;
|
246
|
-
const m = x - u * q;
|
247
|
-
b = a, a = r, x = u, u = m;
|
248
|
-
}
|
249
|
-
const gcd = b;
|
250
|
-
if (gcd !== _1n$3)
|
251
|
-
throw new Error("invert: does not exist");
|
252
|
-
return mod(x, modulo);
|
253
|
-
}
|
254
|
-
function sqrt3mod4(Fp, n) {
|
255
|
-
const p1div4 = (Fp.ORDER + _1n$3) / _4n$1;
|
256
|
-
const root = Fp.pow(n, p1div4);
|
257
|
-
if (!Fp.eql(Fp.sqr(root), n))
|
258
|
-
throw new Error("Cannot find square root");
|
259
|
-
return root;
|
260
|
-
}
|
261
|
-
function sqrt5mod8(Fp, n) {
|
262
|
-
const p5div8 = (Fp.ORDER - _5n) / _8n;
|
263
|
-
const n2 = Fp.mul(n, _2n$1);
|
264
|
-
const v = Fp.pow(n2, p5div8);
|
265
|
-
const nv = Fp.mul(n, v);
|
266
|
-
const i = Fp.mul(Fp.mul(nv, _2n$1), v);
|
267
|
-
const root = Fp.mul(nv, Fp.sub(i, Fp.ONE));
|
268
|
-
if (!Fp.eql(Fp.sqr(root), n))
|
269
|
-
throw new Error("Cannot find square root");
|
270
|
-
return root;
|
271
|
-
}
|
272
|
-
function tonelliShanks(P) {
|
273
|
-
if (P < BigInt(3))
|
274
|
-
throw new Error("sqrt is not defined for small field");
|
275
|
-
let Q = P - _1n$3;
|
276
|
-
let S = 0;
|
277
|
-
while (Q % _2n$1 === _0n$3) {
|
278
|
-
Q /= _2n$1;
|
279
|
-
S++;
|
280
|
-
}
|
281
|
-
let Z = _2n$1;
|
282
|
-
const _Fp = Field(P);
|
283
|
-
while (FpLegendre(_Fp, Z) === 1) {
|
284
|
-
if (Z++ > 1e3)
|
285
|
-
throw new Error("Cannot find square root: probably non-prime P");
|
286
|
-
}
|
287
|
-
if (S === 1)
|
288
|
-
return sqrt3mod4;
|
289
|
-
let cc = _Fp.pow(Z, Q);
|
290
|
-
const Q1div2 = (Q + _1n$3) / _2n$1;
|
291
|
-
return function tonelliSlow(Fp, n) {
|
292
|
-
if (Fp.is0(n))
|
293
|
-
return n;
|
294
|
-
if (FpLegendre(Fp, n) !== 1)
|
295
|
-
throw new Error("Cannot find square root");
|
296
|
-
let M = S;
|
297
|
-
let c = Fp.mul(Fp.ONE, cc);
|
298
|
-
let t = Fp.pow(n, Q);
|
299
|
-
let R = Fp.pow(n, Q1div2);
|
300
|
-
while (!Fp.eql(t, Fp.ONE)) {
|
301
|
-
if (Fp.is0(t))
|
302
|
-
return Fp.ZERO;
|
303
|
-
let i = 1;
|
304
|
-
let t_tmp = Fp.sqr(t);
|
305
|
-
while (!Fp.eql(t_tmp, Fp.ONE)) {
|
306
|
-
i++;
|
307
|
-
t_tmp = Fp.sqr(t_tmp);
|
308
|
-
if (i === M)
|
309
|
-
throw new Error("Cannot find square root");
|
310
|
-
}
|
311
|
-
const exponent = _1n$3 << BigInt(M - i - 1);
|
312
|
-
const b = Fp.pow(c, exponent);
|
313
|
-
M = i;
|
314
|
-
c = Fp.sqr(b);
|
315
|
-
t = Fp.mul(t, c);
|
316
|
-
R = Fp.mul(R, b);
|
317
|
-
}
|
318
|
-
return R;
|
319
|
-
};
|
320
|
-
}
|
321
|
-
function FpSqrt(P) {
|
322
|
-
if (P % _4n$1 === _3n$1)
|
323
|
-
return sqrt3mod4;
|
324
|
-
if (P % _8n === _5n)
|
325
|
-
return sqrt5mod8;
|
326
|
-
return tonelliShanks(P);
|
327
|
-
}
|
328
|
-
const FIELD_FIELDS = [
|
329
|
-
"create",
|
330
|
-
"isValid",
|
331
|
-
"is0",
|
332
|
-
"neg",
|
333
|
-
"inv",
|
334
|
-
"sqrt",
|
335
|
-
"sqr",
|
336
|
-
"eql",
|
337
|
-
"add",
|
338
|
-
"sub",
|
339
|
-
"mul",
|
340
|
-
"pow",
|
341
|
-
"div",
|
342
|
-
"addN",
|
343
|
-
"subN",
|
344
|
-
"mulN",
|
345
|
-
"sqrN"
|
346
|
-
];
|
347
|
-
function validateField(field) {
|
348
|
-
const initial = {
|
349
|
-
ORDER: "bigint",
|
350
|
-
MASK: "bigint",
|
351
|
-
BYTES: "isSafeInteger",
|
352
|
-
BITS: "isSafeInteger"
|
353
|
-
};
|
354
|
-
const opts = FIELD_FIELDS.reduce((map, val) => {
|
355
|
-
map[val] = "function";
|
356
|
-
return map;
|
357
|
-
}, initial);
|
358
|
-
return validateObject(field, opts);
|
359
|
-
}
|
360
|
-
function FpPow(Fp, num, power) {
|
361
|
-
if (power < _0n$3)
|
362
|
-
throw new Error("invalid exponent, negatives unsupported");
|
363
|
-
if (power === _0n$3)
|
364
|
-
return Fp.ONE;
|
365
|
-
if (power === _1n$3)
|
366
|
-
return num;
|
367
|
-
let p = Fp.ONE;
|
368
|
-
let d = num;
|
369
|
-
while (power > _0n$3) {
|
370
|
-
if (power & _1n$3)
|
371
|
-
p = Fp.mul(p, d);
|
372
|
-
d = Fp.sqr(d);
|
373
|
-
power >>= _1n$3;
|
374
|
-
}
|
375
|
-
return p;
|
376
|
-
}
|
377
|
-
function FpInvertBatch(Fp, nums, passZero = false) {
|
378
|
-
const inverted = new Array(nums.length).fill(passZero ? Fp.ZERO : void 0);
|
379
|
-
const multipliedAcc = nums.reduce((acc, num, i) => {
|
380
|
-
if (Fp.is0(num))
|
381
|
-
return acc;
|
382
|
-
inverted[i] = acc;
|
383
|
-
return Fp.mul(acc, num);
|
384
|
-
}, Fp.ONE);
|
385
|
-
const invertedAcc = Fp.inv(multipliedAcc);
|
386
|
-
nums.reduceRight((acc, num, i) => {
|
387
|
-
if (Fp.is0(num))
|
388
|
-
return acc;
|
389
|
-
inverted[i] = Fp.mul(acc, inverted[i]);
|
390
|
-
return Fp.mul(acc, num);
|
391
|
-
}, invertedAcc);
|
392
|
-
return inverted;
|
393
|
-
}
|
394
|
-
function FpLegendre(Fp, n) {
|
395
|
-
const p1mod2 = (Fp.ORDER - _1n$3) / _2n$1;
|
396
|
-
const powered = Fp.pow(n, p1mod2);
|
397
|
-
const yes = Fp.eql(powered, Fp.ONE);
|
398
|
-
const zero = Fp.eql(powered, Fp.ZERO);
|
399
|
-
const no = Fp.eql(powered, Fp.neg(Fp.ONE));
|
400
|
-
if (!yes && !zero && !no)
|
401
|
-
throw new Error("invalid Legendre symbol result");
|
402
|
-
return yes ? 1 : zero ? 0 : -1;
|
403
|
-
}
|
404
|
-
function nLength(n, nBitLength) {
|
405
|
-
if (nBitLength !== void 0)
|
406
|
-
anumber(nBitLength);
|
407
|
-
const _nBitLength = nBitLength !== void 0 ? nBitLength : n.toString(2).length;
|
408
|
-
const nByteLength = Math.ceil(_nBitLength / 8);
|
409
|
-
return { nBitLength: _nBitLength, nByteLength };
|
410
|
-
}
|
411
|
-
function Field(ORDER, bitLen2, isLE = false, redef = {}) {
|
412
|
-
if (ORDER <= _0n$3)
|
413
|
-
throw new Error("invalid field: expected ORDER > 0, got " + ORDER);
|
414
|
-
const { nBitLength: BITS, nByteLength: BYTES } = nLength(ORDER, bitLen2);
|
415
|
-
if (BYTES > 2048)
|
416
|
-
throw new Error("invalid field: expected ORDER of <= 2048 bytes");
|
417
|
-
let sqrtP;
|
418
|
-
const f = Object.freeze({
|
419
|
-
ORDER,
|
420
|
-
isLE,
|
421
|
-
BITS,
|
422
|
-
BYTES,
|
423
|
-
MASK: bitMask(BITS),
|
424
|
-
ZERO: _0n$3,
|
425
|
-
ONE: _1n$3,
|
426
|
-
create: (num) => mod(num, ORDER),
|
427
|
-
isValid: (num) => {
|
428
|
-
if (typeof num !== "bigint")
|
429
|
-
throw new Error("invalid field element: expected bigint, got " + typeof num);
|
430
|
-
return _0n$3 <= num && num < ORDER;
|
431
|
-
},
|
432
|
-
is0: (num) => num === _0n$3,
|
433
|
-
isOdd: (num) => (num & _1n$3) === _1n$3,
|
434
|
-
neg: (num) => mod(-num, ORDER),
|
435
|
-
eql: (lhs, rhs) => lhs === rhs,
|
436
|
-
sqr: (num) => mod(num * num, ORDER),
|
437
|
-
add: (lhs, rhs) => mod(lhs + rhs, ORDER),
|
438
|
-
sub: (lhs, rhs) => mod(lhs - rhs, ORDER),
|
439
|
-
mul: (lhs, rhs) => mod(lhs * rhs, ORDER),
|
440
|
-
pow: (num, power) => FpPow(f, num, power),
|
441
|
-
div: (lhs, rhs) => mod(lhs * invert(rhs, ORDER), ORDER),
|
442
|
-
// Same as above, but doesn't normalize
|
443
|
-
sqrN: (num) => num * num,
|
444
|
-
addN: (lhs, rhs) => lhs + rhs,
|
445
|
-
subN: (lhs, rhs) => lhs - rhs,
|
446
|
-
mulN: (lhs, rhs) => lhs * rhs,
|
447
|
-
inv: (num) => invert(num, ORDER),
|
448
|
-
sqrt: redef.sqrt || ((n) => {
|
449
|
-
if (!sqrtP)
|
450
|
-
sqrtP = FpSqrt(ORDER);
|
451
|
-
return sqrtP(f, n);
|
452
|
-
}),
|
453
|
-
toBytes: (num) => isLE ? numberToBytesLE(num, BYTES) : numberToBytesBE(num, BYTES),
|
454
|
-
fromBytes: (bytes) => {
|
455
|
-
if (bytes.length !== BYTES)
|
456
|
-
throw new Error("Field.fromBytes: expected " + BYTES + " bytes, got " + bytes.length);
|
457
|
-
return isLE ? bytesToNumberLE(bytes) : bytesToNumberBE(bytes);
|
458
|
-
},
|
459
|
-
// TODO: we don't need it here, move out to separate fn
|
460
|
-
invertBatch: (lst) => FpInvertBatch(f, lst),
|
461
|
-
// We can't move this out because Fp6, Fp12 implement it
|
462
|
-
// and it's unclear what to return in there.
|
463
|
-
cmov: (a, b, c) => c ? b : a
|
464
|
-
});
|
465
|
-
return Object.freeze(f);
|
466
|
-
}
|
467
|
-
function getFieldBytesLength(fieldOrder) {
|
468
|
-
if (typeof fieldOrder !== "bigint")
|
469
|
-
throw new Error("field order must be bigint");
|
470
|
-
const bitLength = fieldOrder.toString(2).length;
|
471
|
-
return Math.ceil(bitLength / 8);
|
472
|
-
}
|
473
|
-
function getMinHashLength(fieldOrder) {
|
474
|
-
const length = getFieldBytesLength(fieldOrder);
|
475
|
-
return length + Math.ceil(length / 2);
|
476
|
-
}
|
477
|
-
function mapHashToField(key, fieldOrder, isLE = false) {
|
478
|
-
const len = key.length;
|
479
|
-
const fieldLen = getFieldBytesLength(fieldOrder);
|
480
|
-
const minLen = getMinHashLength(fieldOrder);
|
481
|
-
if (len < 16 || len < minLen || len > 1024)
|
482
|
-
throw new Error("expected " + minLen + "-1024 bytes of input, got " + len);
|
483
|
-
const num = isLE ? bytesToNumberLE(key) : bytesToNumberBE(key);
|
484
|
-
const reduced = mod(num, fieldOrder - _1n$3) + _1n$3;
|
485
|
-
return isLE ? numberToBytesLE(reduced, fieldLen) : numberToBytesBE(reduced, fieldLen);
|
486
|
-
}
|
487
|
-
const _0n$2 = BigInt(0);
|
488
|
-
const _1n$2 = BigInt(1);
|
489
|
-
function constTimeNegate(condition, item) {
|
490
|
-
const neg = item.negate();
|
491
|
-
return condition ? neg : item;
|
492
|
-
}
|
493
|
-
function validateW(W, bits) {
|
494
|
-
if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
|
495
|
-
throw new Error("invalid window size, expected [1.." + bits + "], got W=" + W);
|
496
|
-
}
|
497
|
-
function calcWOpts(W, scalarBits) {
|
498
|
-
validateW(W, scalarBits);
|
499
|
-
const windows = Math.ceil(scalarBits / W) + 1;
|
500
|
-
const windowSize = 2 ** (W - 1);
|
501
|
-
const maxNumber = 2 ** W;
|
502
|
-
const mask = bitMask(W);
|
503
|
-
const shiftBy = BigInt(W);
|
504
|
-
return { windows, windowSize, mask, maxNumber, shiftBy };
|
505
|
-
}
|
506
|
-
function calcOffsets(n, window, wOpts) {
|
507
|
-
const { windowSize, mask, maxNumber, shiftBy } = wOpts;
|
508
|
-
let wbits = Number(n & mask);
|
509
|
-
let nextN = n >> shiftBy;
|
510
|
-
if (wbits > windowSize) {
|
511
|
-
wbits -= maxNumber;
|
512
|
-
nextN += _1n$2;
|
513
|
-
}
|
514
|
-
const offsetStart = window * windowSize;
|
515
|
-
const offset = offsetStart + Math.abs(wbits) - 1;
|
516
|
-
const isZero = wbits === 0;
|
517
|
-
const isNeg = wbits < 0;
|
518
|
-
const isNegF = window % 2 !== 0;
|
519
|
-
const offsetF = offsetStart;
|
520
|
-
return { nextN, offset, isZero, isNeg, isNegF, offsetF };
|
521
|
-
}
|
522
|
-
function validateMSMPoints(points, c) {
|
523
|
-
if (!Array.isArray(points))
|
524
|
-
throw new Error("array expected");
|
525
|
-
points.forEach((p, i) => {
|
526
|
-
if (!(p instanceof c))
|
527
|
-
throw new Error("invalid point at index " + i);
|
528
|
-
});
|
529
|
-
}
|
530
|
-
function validateMSMScalars(scalars, field) {
|
531
|
-
if (!Array.isArray(scalars))
|
532
|
-
throw new Error("array of scalars expected");
|
533
|
-
scalars.forEach((s, i) => {
|
534
|
-
if (!field.isValid(s))
|
535
|
-
throw new Error("invalid scalar at index " + i);
|
536
|
-
});
|
537
|
-
}
|
538
|
-
const pointPrecomputes = /* @__PURE__ */ new WeakMap();
|
539
|
-
const pointWindowSizes = /* @__PURE__ */ new WeakMap();
|
540
|
-
function getW(P) {
|
541
|
-
return pointWindowSizes.get(P) || 1;
|
542
|
-
}
|
543
|
-
function wNAF(c, bits) {
|
544
|
-
return {
|
545
|
-
constTimeNegate,
|
546
|
-
hasPrecomputes(elm) {
|
547
|
-
return getW(elm) !== 1;
|
548
|
-
},
|
549
|
-
// non-const time multiplication ladder
|
550
|
-
unsafeLadder(elm, n, p = c.ZERO) {
|
551
|
-
let d = elm;
|
552
|
-
while (n > _0n$2) {
|
553
|
-
if (n & _1n$2)
|
554
|
-
p = p.add(d);
|
555
|
-
d = d.double();
|
556
|
-
n >>= _1n$2;
|
557
|
-
}
|
558
|
-
return p;
|
559
|
-
},
|
560
|
-
/**
|
561
|
-
* Creates a wNAF precomputation window. Used for caching.
|
562
|
-
* Default window size is set by `utils.precompute()` and is equal to 8.
|
563
|
-
* Number of precomputed points depends on the curve size:
|
564
|
-
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
565
|
-
* - 𝑊 is the window size
|
566
|
-
* - 𝑛 is the bitlength of the curve order.
|
567
|
-
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
568
|
-
* @param elm Point instance
|
569
|
-
* @param W window size
|
570
|
-
* @returns precomputed point tables flattened to a single array
|
571
|
-
*/
|
572
|
-
precomputeWindow(elm, W) {
|
573
|
-
const { windows, windowSize } = calcWOpts(W, bits);
|
574
|
-
const points = [];
|
575
|
-
let p = elm;
|
576
|
-
let base = p;
|
577
|
-
for (let window = 0; window < windows; window++) {
|
578
|
-
base = p;
|
579
|
-
points.push(base);
|
580
|
-
for (let i = 1; i < windowSize; i++) {
|
581
|
-
base = base.add(p);
|
582
|
-
points.push(base);
|
583
|
-
}
|
584
|
-
p = base.double();
|
585
|
-
}
|
586
|
-
return points;
|
587
|
-
},
|
588
|
-
/**
|
589
|
-
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
590
|
-
* @param W window size
|
591
|
-
* @param precomputes precomputed tables
|
592
|
-
* @param n scalar (we don't check here, but should be less than curve order)
|
593
|
-
* @returns real and fake (for const-time) points
|
594
|
-
*/
|
595
|
-
wNAF(W, precomputes, n) {
|
596
|
-
let p = c.ZERO;
|
597
|
-
let f = c.BASE;
|
598
|
-
const wo = calcWOpts(W, bits);
|
599
|
-
for (let window = 0; window < wo.windows; window++) {
|
600
|
-
const { nextN, offset, isZero, isNeg, isNegF, offsetF } = calcOffsets(n, window, wo);
|
601
|
-
n = nextN;
|
602
|
-
if (isZero) {
|
603
|
-
f = f.add(constTimeNegate(isNegF, precomputes[offsetF]));
|
604
|
-
} else {
|
605
|
-
p = p.add(constTimeNegate(isNeg, precomputes[offset]));
|
606
|
-
}
|
607
|
-
}
|
608
|
-
return { p, f };
|
609
|
-
},
|
610
|
-
/**
|
611
|
-
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
612
|
-
* @param W window size
|
613
|
-
* @param precomputes precomputed tables
|
614
|
-
* @param n scalar (we don't check here, but should be less than curve order)
|
615
|
-
* @param acc accumulator point to add result of multiplication
|
616
|
-
* @returns point
|
617
|
-
*/
|
618
|
-
wNAFUnsafe(W, precomputes, n, acc = c.ZERO) {
|
619
|
-
const wo = calcWOpts(W, bits);
|
620
|
-
for (let window = 0; window < wo.windows; window++) {
|
621
|
-
if (n === _0n$2)
|
622
|
-
break;
|
623
|
-
const { nextN, offset, isZero, isNeg } = calcOffsets(n, window, wo);
|
624
|
-
n = nextN;
|
625
|
-
if (isZero) {
|
626
|
-
continue;
|
627
|
-
} else {
|
628
|
-
const item = precomputes[offset];
|
629
|
-
acc = acc.add(isNeg ? item.negate() : item);
|
630
|
-
}
|
631
|
-
}
|
632
|
-
return acc;
|
633
|
-
},
|
634
|
-
getPrecomputes(W, P, transform) {
|
635
|
-
let comp = pointPrecomputes.get(P);
|
636
|
-
if (!comp) {
|
637
|
-
comp = this.precomputeWindow(P, W);
|
638
|
-
if (W !== 1)
|
639
|
-
pointPrecomputes.set(P, transform(comp));
|
640
|
-
}
|
641
|
-
return comp;
|
642
|
-
},
|
643
|
-
wNAFCached(P, n, transform) {
|
644
|
-
const W = getW(P);
|
645
|
-
return this.wNAF(W, this.getPrecomputes(W, P, transform), n);
|
646
|
-
},
|
647
|
-
wNAFCachedUnsafe(P, n, transform, prev) {
|
648
|
-
const W = getW(P);
|
649
|
-
if (W === 1)
|
650
|
-
return this.unsafeLadder(P, n, prev);
|
651
|
-
return this.wNAFUnsafe(W, this.getPrecomputes(W, P, transform), n, prev);
|
652
|
-
},
|
653
|
-
// We calculate precomputes for elliptic curve point multiplication
|
654
|
-
// using windowed method. This specifies window size and
|
655
|
-
// stores precomputed values. Usually only base point would be precomputed.
|
656
|
-
setWindowSize(P, W) {
|
657
|
-
validateW(W, bits);
|
658
|
-
pointWindowSizes.set(P, W);
|
659
|
-
pointPrecomputes.delete(P);
|
660
|
-
}
|
661
|
-
};
|
662
|
-
}
|
663
|
-
function pippenger(c, fieldN, points, scalars) {
|
664
|
-
validateMSMPoints(points, c);
|
665
|
-
validateMSMScalars(scalars, fieldN);
|
666
|
-
const plength = points.length;
|
667
|
-
const slength = scalars.length;
|
668
|
-
if (plength !== slength)
|
669
|
-
throw new Error("arrays of points and scalars must have equal length");
|
670
|
-
const zero = c.ZERO;
|
671
|
-
const wbits = bitLen(BigInt(plength));
|
672
|
-
let windowSize = 1;
|
673
|
-
if (wbits > 12)
|
674
|
-
windowSize = wbits - 3;
|
675
|
-
else if (wbits > 4)
|
676
|
-
windowSize = wbits - 2;
|
677
|
-
else if (wbits > 0)
|
678
|
-
windowSize = 2;
|
679
|
-
const MASK = bitMask(windowSize);
|
680
|
-
const buckets = new Array(Number(MASK) + 1).fill(zero);
|
681
|
-
const lastBits = Math.floor((fieldN.BITS - 1) / windowSize) * windowSize;
|
682
|
-
let sum = zero;
|
683
|
-
for (let i = lastBits; i >= 0; i -= windowSize) {
|
684
|
-
buckets.fill(zero);
|
685
|
-
for (let j = 0; j < slength; j++) {
|
686
|
-
const scalar = scalars[j];
|
687
|
-
const wbits2 = Number(scalar >> BigInt(i) & MASK);
|
688
|
-
buckets[wbits2] = buckets[wbits2].add(points[j]);
|
689
|
-
}
|
690
|
-
let resI = zero;
|
691
|
-
for (let j = buckets.length - 1, sumI = zero; j > 0; j--) {
|
692
|
-
sumI = sumI.add(buckets[j]);
|
693
|
-
resI = resI.add(sumI);
|
694
|
-
}
|
695
|
-
sum = sum.add(resI);
|
696
|
-
if (i !== 0)
|
697
|
-
for (let j = 0; j < windowSize; j++)
|
698
|
-
sum = sum.double();
|
699
|
-
}
|
700
|
-
return sum;
|
701
|
-
}
|
702
|
-
function validateBasic(curve) {
|
703
|
-
validateField(curve.Fp);
|
704
|
-
validateObject(curve, {
|
705
|
-
n: "bigint",
|
706
|
-
h: "bigint",
|
707
|
-
Gx: "field",
|
708
|
-
Gy: "field"
|
709
|
-
}, {
|
710
|
-
nBitLength: "isSafeInteger",
|
711
|
-
nByteLength: "isSafeInteger"
|
712
|
-
});
|
713
|
-
return Object.freeze({
|
714
|
-
...nLength(curve.n, curve.nBitLength),
|
715
|
-
...curve,
|
716
|
-
...{ p: curve.Fp.ORDER }
|
717
|
-
});
|
718
|
-
}
|
719
|
-
function validateSigVerOpts(opts) {
|
720
|
-
if (opts.lowS !== void 0)
|
721
|
-
abool("lowS", opts.lowS);
|
722
|
-
if (opts.prehash !== void 0)
|
723
|
-
abool("prehash", opts.prehash);
|
724
|
-
}
|
725
|
-
function validatePointOpts(curve) {
|
726
|
-
const opts = validateBasic(curve);
|
727
|
-
validateObject(opts, {
|
728
|
-
a: "field",
|
729
|
-
b: "field"
|
730
|
-
}, {
|
731
|
-
allowInfinityPoint: "boolean",
|
732
|
-
allowedPrivateKeyLengths: "array",
|
733
|
-
clearCofactor: "function",
|
734
|
-
fromBytes: "function",
|
735
|
-
isTorsionFree: "function",
|
736
|
-
toBytes: "function",
|
737
|
-
wrapPrivateKey: "boolean"
|
738
|
-
});
|
739
|
-
const { endo, Fp, a } = opts;
|
740
|
-
if (endo) {
|
741
|
-
if (!Fp.eql(a, Fp.ZERO)) {
|
742
|
-
throw new Error("invalid endo: CURVE.a must be 0");
|
743
|
-
}
|
744
|
-
if (typeof endo !== "object" || typeof endo.beta !== "bigint" || typeof endo.splitScalar !== "function") {
|
745
|
-
throw new Error('invalid endo: expected "beta": bigint and "splitScalar": function');
|
746
|
-
}
|
747
|
-
}
|
748
|
-
return Object.freeze({ ...opts });
|
749
|
-
}
|
750
|
-
class DERErr extends Error {
|
751
|
-
constructor(m = "") {
|
752
|
-
super(m);
|
753
|
-
}
|
754
|
-
}
|
755
|
-
const DER = {
|
756
|
-
// asn.1 DER encoding utils
|
757
|
-
Err: DERErr,
|
758
|
-
// Basic building block is TLV (Tag-Length-Value)
|
759
|
-
_tlv: {
|
760
|
-
encode: (tag, data) => {
|
761
|
-
const { Err: E } = DER;
|
762
|
-
if (tag < 0 || tag > 256)
|
763
|
-
throw new E("tlv.encode: wrong tag");
|
764
|
-
if (data.length & 1)
|
765
|
-
throw new E("tlv.encode: unpadded data");
|
766
|
-
const dataLen = data.length / 2;
|
767
|
-
const len = numberToHexUnpadded(dataLen);
|
768
|
-
if (len.length / 2 & 128)
|
769
|
-
throw new E("tlv.encode: long form length too big");
|
770
|
-
const lenLen = dataLen > 127 ? numberToHexUnpadded(len.length / 2 | 128) : "";
|
771
|
-
const t = numberToHexUnpadded(tag);
|
772
|
-
return t + lenLen + len + data;
|
773
|
-
},
|
774
|
-
// v - value, l - left bytes (unparsed)
|
775
|
-
decode(tag, data) {
|
776
|
-
const { Err: E } = DER;
|
777
|
-
let pos = 0;
|
778
|
-
if (tag < 0 || tag > 256)
|
779
|
-
throw new E("tlv.encode: wrong tag");
|
780
|
-
if (data.length < 2 || data[pos++] !== tag)
|
781
|
-
throw new E("tlv.decode: wrong tlv");
|
782
|
-
const first = data[pos++];
|
783
|
-
const isLong = !!(first & 128);
|
784
|
-
let length = 0;
|
785
|
-
if (!isLong)
|
786
|
-
length = first;
|
787
|
-
else {
|
788
|
-
const lenLen = first & 127;
|
789
|
-
if (!lenLen)
|
790
|
-
throw new E("tlv.decode(long): indefinite length not supported");
|
791
|
-
if (lenLen > 4)
|
792
|
-
throw new E("tlv.decode(long): byte length is too big");
|
793
|
-
const lengthBytes = data.subarray(pos, pos + lenLen);
|
794
|
-
if (lengthBytes.length !== lenLen)
|
795
|
-
throw new E("tlv.decode: length bytes not complete");
|
796
|
-
if (lengthBytes[0] === 0)
|
797
|
-
throw new E("tlv.decode(long): zero leftmost byte");
|
798
|
-
for (const b of lengthBytes)
|
799
|
-
length = length << 8 | b;
|
800
|
-
pos += lenLen;
|
801
|
-
if (length < 128)
|
802
|
-
throw new E("tlv.decode(long): not minimal encoding");
|
803
|
-
}
|
804
|
-
const v = data.subarray(pos, pos + length);
|
805
|
-
if (v.length !== length)
|
806
|
-
throw new E("tlv.decode: wrong value length");
|
807
|
-
return { v, l: data.subarray(pos + length) };
|
808
|
-
}
|
809
|
-
},
|
810
|
-
// https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
|
811
|
-
// since we always use positive integers here. It must always be empty:
|
812
|
-
// - add zero byte if exists
|
813
|
-
// - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
|
814
|
-
_int: {
|
815
|
-
encode(num) {
|
816
|
-
const { Err: E } = DER;
|
817
|
-
if (num < _0n$1)
|
818
|
-
throw new E("integer: negative integers are not allowed");
|
819
|
-
let hex = numberToHexUnpadded(num);
|
820
|
-
if (Number.parseInt(hex[0], 16) & 8)
|
821
|
-
hex = "00" + hex;
|
822
|
-
if (hex.length & 1)
|
823
|
-
throw new E("unexpected DER parsing assertion: unpadded hex");
|
824
|
-
return hex;
|
825
|
-
},
|
826
|
-
decode(data) {
|
827
|
-
const { Err: E } = DER;
|
828
|
-
if (data[0] & 128)
|
829
|
-
throw new E("invalid signature integer: negative");
|
830
|
-
if (data[0] === 0 && !(data[1] & 128))
|
831
|
-
throw new E("invalid signature integer: unnecessary leading zero");
|
832
|
-
return bytesToNumberBE(data);
|
833
|
-
}
|
834
|
-
},
|
835
|
-
toSig(hex) {
|
836
|
-
const { Err: E, _int: int, _tlv: tlv } = DER;
|
837
|
-
const data = ensureBytes("signature", hex);
|
838
|
-
const { v: seqBytes, l: seqLeftBytes } = tlv.decode(48, data);
|
839
|
-
if (seqLeftBytes.length)
|
840
|
-
throw new E("invalid signature: left bytes after parsing");
|
841
|
-
const { v: rBytes, l: rLeftBytes } = tlv.decode(2, seqBytes);
|
842
|
-
const { v: sBytes, l: sLeftBytes } = tlv.decode(2, rLeftBytes);
|
843
|
-
if (sLeftBytes.length)
|
844
|
-
throw new E("invalid signature: left bytes after parsing");
|
845
|
-
return { r: int.decode(rBytes), s: int.decode(sBytes) };
|
846
|
-
},
|
847
|
-
hexFromSig(sig) {
|
848
|
-
const { _tlv: tlv, _int: int } = DER;
|
849
|
-
const rs = tlv.encode(2, int.encode(sig.r));
|
850
|
-
const ss = tlv.encode(2, int.encode(sig.s));
|
851
|
-
const seq = rs + ss;
|
852
|
-
return tlv.encode(48, seq);
|
853
|
-
}
|
854
|
-
};
|
855
|
-
function numToSizedHex(num, size) {
|
856
|
-
return bytesToHex(numberToBytesBE(num, size));
|
857
|
-
}
|
858
|
-
const _0n$1 = BigInt(0), _1n$1 = BigInt(1);
|
859
|
-
BigInt(2);
|
860
|
-
const _3n = BigInt(3), _4n = BigInt(4);
|
861
|
-
function weierstrassPoints(opts) {
|
862
|
-
const CURVE = validatePointOpts(opts);
|
863
|
-
const { Fp } = CURVE;
|
864
|
-
const Fn = Field(CURVE.n, CURVE.nBitLength);
|
865
|
-
const toBytes = CURVE.toBytes || ((_c, point, _isCompressed) => {
|
866
|
-
const a = point.toAffine();
|
867
|
-
return concatBytes(Uint8Array.from([4]), Fp.toBytes(a.x), Fp.toBytes(a.y));
|
868
|
-
});
|
869
|
-
const fromBytes = CURVE.fromBytes || ((bytes) => {
|
870
|
-
const tail = bytes.subarray(1);
|
871
|
-
const x = Fp.fromBytes(tail.subarray(0, Fp.BYTES));
|
872
|
-
const y = Fp.fromBytes(tail.subarray(Fp.BYTES, 2 * Fp.BYTES));
|
873
|
-
return { x, y };
|
874
|
-
});
|
875
|
-
function weierstrassEquation(x) {
|
876
|
-
const { a, b } = CURVE;
|
877
|
-
const x2 = Fp.sqr(x);
|
878
|
-
const x3 = Fp.mul(x2, x);
|
879
|
-
return Fp.add(Fp.add(x3, Fp.mul(x, a)), b);
|
880
|
-
}
|
881
|
-
function isValidXY(x, y) {
|
882
|
-
const left = Fp.sqr(y);
|
883
|
-
const right = weierstrassEquation(x);
|
884
|
-
return Fp.eql(left, right);
|
885
|
-
}
|
886
|
-
if (!isValidXY(CURVE.Gx, CURVE.Gy))
|
887
|
-
throw new Error("bad curve params: generator point");
|
888
|
-
const _4a3 = Fp.mul(Fp.pow(CURVE.a, _3n), _4n);
|
889
|
-
const _27b2 = Fp.mul(Fp.sqr(CURVE.b), BigInt(27));
|
890
|
-
if (Fp.is0(Fp.add(_4a3, _27b2)))
|
891
|
-
throw new Error("bad curve params: a or b");
|
892
|
-
function isWithinCurveOrder(num) {
|
893
|
-
return inRange(num, _1n$1, CURVE.n);
|
894
|
-
}
|
895
|
-
function normPrivateKeyToScalar(key) {
|
896
|
-
const { allowedPrivateKeyLengths: lengths, nByteLength, wrapPrivateKey, n: N } = CURVE;
|
897
|
-
if (lengths && typeof key !== "bigint") {
|
898
|
-
if (isBytes(key))
|
899
|
-
key = bytesToHex(key);
|
900
|
-
if (typeof key !== "string" || !lengths.includes(key.length))
|
901
|
-
throw new Error("invalid private key");
|
902
|
-
key = key.padStart(nByteLength * 2, "0");
|
903
|
-
}
|
904
|
-
let num;
|
905
|
-
try {
|
906
|
-
num = typeof key === "bigint" ? key : bytesToNumberBE(ensureBytes("private key", key, nByteLength));
|
907
|
-
} catch (error) {
|
908
|
-
throw new Error("invalid private key, expected hex or " + nByteLength + " bytes, got " + typeof key);
|
909
|
-
}
|
910
|
-
if (wrapPrivateKey)
|
911
|
-
num = mod(num, N);
|
912
|
-
aInRange("private key", num, _1n$1, N);
|
913
|
-
return num;
|
914
|
-
}
|
915
|
-
function aprjpoint(other) {
|
916
|
-
if (!(other instanceof Point))
|
917
|
-
throw new Error("ProjectivePoint expected");
|
918
|
-
}
|
919
|
-
const toAffineMemo = memoized((p, iz) => {
|
920
|
-
const { px: x, py: y, pz: z } = p;
|
921
|
-
if (Fp.eql(z, Fp.ONE))
|
922
|
-
return { x, y };
|
923
|
-
const is0 = p.is0();
|
924
|
-
if (iz == null)
|
925
|
-
iz = is0 ? Fp.ONE : Fp.inv(z);
|
926
|
-
const ax = Fp.mul(x, iz);
|
927
|
-
const ay = Fp.mul(y, iz);
|
928
|
-
const zz = Fp.mul(z, iz);
|
929
|
-
if (is0)
|
930
|
-
return { x: Fp.ZERO, y: Fp.ZERO };
|
931
|
-
if (!Fp.eql(zz, Fp.ONE))
|
932
|
-
throw new Error("invZ was invalid");
|
933
|
-
return { x: ax, y: ay };
|
934
|
-
});
|
935
|
-
const assertValidMemo = memoized((p) => {
|
936
|
-
if (p.is0()) {
|
937
|
-
if (CURVE.allowInfinityPoint && !Fp.is0(p.py))
|
938
|
-
return;
|
939
|
-
throw new Error("bad point: ZERO");
|
940
|
-
}
|
941
|
-
const { x, y } = p.toAffine();
|
942
|
-
if (!Fp.isValid(x) || !Fp.isValid(y))
|
943
|
-
throw new Error("bad point: x or y not FE");
|
944
|
-
if (!isValidXY(x, y))
|
945
|
-
throw new Error("bad point: equation left != right");
|
946
|
-
if (!p.isTorsionFree())
|
947
|
-
throw new Error("bad point: not in prime-order subgroup");
|
948
|
-
return true;
|
949
|
-
});
|
950
|
-
class Point {
|
951
|
-
constructor(px, py, pz) {
|
952
|
-
if (px == null || !Fp.isValid(px))
|
953
|
-
throw new Error("x required");
|
954
|
-
if (py == null || !Fp.isValid(py) || Fp.is0(py))
|
955
|
-
throw new Error("y required");
|
956
|
-
if (pz == null || !Fp.isValid(pz))
|
957
|
-
throw new Error("z required");
|
958
|
-
this.px = px;
|
959
|
-
this.py = py;
|
960
|
-
this.pz = pz;
|
961
|
-
Object.freeze(this);
|
962
|
-
}
|
963
|
-
// Does not validate if the point is on-curve.
|
964
|
-
// Use fromHex instead, or call assertValidity() later.
|
965
|
-
static fromAffine(p) {
|
966
|
-
const { x, y } = p || {};
|
967
|
-
if (!p || !Fp.isValid(x) || !Fp.isValid(y))
|
968
|
-
throw new Error("invalid affine point");
|
969
|
-
if (p instanceof Point)
|
970
|
-
throw new Error("projective point not allowed");
|
971
|
-
const is0 = (i) => Fp.eql(i, Fp.ZERO);
|
972
|
-
if (is0(x) && is0(y))
|
973
|
-
return Point.ZERO;
|
974
|
-
return new Point(x, y, Fp.ONE);
|
975
|
-
}
|
976
|
-
get x() {
|
977
|
-
return this.toAffine().x;
|
978
|
-
}
|
979
|
-
get y() {
|
980
|
-
return this.toAffine().y;
|
981
|
-
}
|
982
|
-
/**
|
983
|
-
* Takes a bunch of Projective Points but executes only one
|
984
|
-
* inversion on all of them. Inversion is very slow operation,
|
985
|
-
* so this improves performance massively.
|
986
|
-
* Optimization: converts a list of projective points to a list of identical points with Z=1.
|
987
|
-
*/
|
988
|
-
static normalizeZ(points) {
|
989
|
-
const toInv = FpInvertBatch(Fp, points.map((p) => p.pz));
|
990
|
-
return points.map((p, i) => p.toAffine(toInv[i])).map(Point.fromAffine);
|
991
|
-
}
|
992
|
-
/**
|
993
|
-
* Converts hash string or Uint8Array to Point.
|
994
|
-
* @param hex short/long ECDSA hex
|
995
|
-
*/
|
996
|
-
static fromHex(hex) {
|
997
|
-
const P = Point.fromAffine(fromBytes(ensureBytes("pointHex", hex)));
|
998
|
-
P.assertValidity();
|
999
|
-
return P;
|
1000
|
-
}
|
1001
|
-
// Multiplies generator point by privateKey.
|
1002
|
-
static fromPrivateKey(privateKey) {
|
1003
|
-
return Point.BASE.multiply(normPrivateKeyToScalar(privateKey));
|
1004
|
-
}
|
1005
|
-
// Multiscalar Multiplication
|
1006
|
-
static msm(points, scalars) {
|
1007
|
-
return pippenger(Point, Fn, points, scalars);
|
1008
|
-
}
|
1009
|
-
// "Private method", don't use it directly
|
1010
|
-
_setWindowSize(windowSize) {
|
1011
|
-
wnaf.setWindowSize(this, windowSize);
|
1012
|
-
}
|
1013
|
-
// A point on curve is valid if it conforms to equation.
|
1014
|
-
assertValidity() {
|
1015
|
-
assertValidMemo(this);
|
1016
|
-
}
|
1017
|
-
hasEvenY() {
|
1018
|
-
const { y } = this.toAffine();
|
1019
|
-
if (Fp.isOdd)
|
1020
|
-
return !Fp.isOdd(y);
|
1021
|
-
throw new Error("Field doesn't support isOdd");
|
1022
|
-
}
|
1023
|
-
/**
|
1024
|
-
* Compare one point to another.
|
1025
|
-
*/
|
1026
|
-
equals(other) {
|
1027
|
-
aprjpoint(other);
|
1028
|
-
const { px: X1, py: Y1, pz: Z1 } = this;
|
1029
|
-
const { px: X2, py: Y2, pz: Z2 } = other;
|
1030
|
-
const U1 = Fp.eql(Fp.mul(X1, Z2), Fp.mul(X2, Z1));
|
1031
|
-
const U2 = Fp.eql(Fp.mul(Y1, Z2), Fp.mul(Y2, Z1));
|
1032
|
-
return U1 && U2;
|
1033
|
-
}
|
1034
|
-
/**
|
1035
|
-
* Flips point to one corresponding to (x, -y) in Affine coordinates.
|
1036
|
-
*/
|
1037
|
-
negate() {
|
1038
|
-
return new Point(this.px, Fp.neg(this.py), this.pz);
|
1039
|
-
}
|
1040
|
-
// Renes-Costello-Batina exception-free doubling formula.
|
1041
|
-
// There is 30% faster Jacobian formula, but it is not complete.
|
1042
|
-
// https://eprint.iacr.org/2015/1060, algorithm 3
|
1043
|
-
// Cost: 8M + 3S + 3*a + 2*b3 + 15add.
|
1044
|
-
double() {
|
1045
|
-
const { a, b } = CURVE;
|
1046
|
-
const b3 = Fp.mul(b, _3n);
|
1047
|
-
const { px: X1, py: Y1, pz: Z1 } = this;
|
1048
|
-
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
1049
|
-
let t0 = Fp.mul(X1, X1);
|
1050
|
-
let t1 = Fp.mul(Y1, Y1);
|
1051
|
-
let t2 = Fp.mul(Z1, Z1);
|
1052
|
-
let t3 = Fp.mul(X1, Y1);
|
1053
|
-
t3 = Fp.add(t3, t3);
|
1054
|
-
Z3 = Fp.mul(X1, Z1);
|
1055
|
-
Z3 = Fp.add(Z3, Z3);
|
1056
|
-
X3 = Fp.mul(a, Z3);
|
1057
|
-
Y3 = Fp.mul(b3, t2);
|
1058
|
-
Y3 = Fp.add(X3, Y3);
|
1059
|
-
X3 = Fp.sub(t1, Y3);
|
1060
|
-
Y3 = Fp.add(t1, Y3);
|
1061
|
-
Y3 = Fp.mul(X3, Y3);
|
1062
|
-
X3 = Fp.mul(t3, X3);
|
1063
|
-
Z3 = Fp.mul(b3, Z3);
|
1064
|
-
t2 = Fp.mul(a, t2);
|
1065
|
-
t3 = Fp.sub(t0, t2);
|
1066
|
-
t3 = Fp.mul(a, t3);
|
1067
|
-
t3 = Fp.add(t3, Z3);
|
1068
|
-
Z3 = Fp.add(t0, t0);
|
1069
|
-
t0 = Fp.add(Z3, t0);
|
1070
|
-
t0 = Fp.add(t0, t2);
|
1071
|
-
t0 = Fp.mul(t0, t3);
|
1072
|
-
Y3 = Fp.add(Y3, t0);
|
1073
|
-
t2 = Fp.mul(Y1, Z1);
|
1074
|
-
t2 = Fp.add(t2, t2);
|
1075
|
-
t0 = Fp.mul(t2, t3);
|
1076
|
-
X3 = Fp.sub(X3, t0);
|
1077
|
-
Z3 = Fp.mul(t2, t1);
|
1078
|
-
Z3 = Fp.add(Z3, Z3);
|
1079
|
-
Z3 = Fp.add(Z3, Z3);
|
1080
|
-
return new Point(X3, Y3, Z3);
|
1081
|
-
}
|
1082
|
-
// Renes-Costello-Batina exception-free addition formula.
|
1083
|
-
// There is 30% faster Jacobian formula, but it is not complete.
|
1084
|
-
// https://eprint.iacr.org/2015/1060, algorithm 1
|
1085
|
-
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
|
1086
|
-
add(other) {
|
1087
|
-
aprjpoint(other);
|
1088
|
-
const { px: X1, py: Y1, pz: Z1 } = this;
|
1089
|
-
const { px: X2, py: Y2, pz: Z2 } = other;
|
1090
|
-
let X3 = Fp.ZERO, Y3 = Fp.ZERO, Z3 = Fp.ZERO;
|
1091
|
-
const a = CURVE.a;
|
1092
|
-
const b3 = Fp.mul(CURVE.b, _3n);
|
1093
|
-
let t0 = Fp.mul(X1, X2);
|
1094
|
-
let t1 = Fp.mul(Y1, Y2);
|
1095
|
-
let t2 = Fp.mul(Z1, Z2);
|
1096
|
-
let t3 = Fp.add(X1, Y1);
|
1097
|
-
let t4 = Fp.add(X2, Y2);
|
1098
|
-
t3 = Fp.mul(t3, t4);
|
1099
|
-
t4 = Fp.add(t0, t1);
|
1100
|
-
t3 = Fp.sub(t3, t4);
|
1101
|
-
t4 = Fp.add(X1, Z1);
|
1102
|
-
let t5 = Fp.add(X2, Z2);
|
1103
|
-
t4 = Fp.mul(t4, t5);
|
1104
|
-
t5 = Fp.add(t0, t2);
|
1105
|
-
t4 = Fp.sub(t4, t5);
|
1106
|
-
t5 = Fp.add(Y1, Z1);
|
1107
|
-
X3 = Fp.add(Y2, Z2);
|
1108
|
-
t5 = Fp.mul(t5, X3);
|
1109
|
-
X3 = Fp.add(t1, t2);
|
1110
|
-
t5 = Fp.sub(t5, X3);
|
1111
|
-
Z3 = Fp.mul(a, t4);
|
1112
|
-
X3 = Fp.mul(b3, t2);
|
1113
|
-
Z3 = Fp.add(X3, Z3);
|
1114
|
-
X3 = Fp.sub(t1, Z3);
|
1115
|
-
Z3 = Fp.add(t1, Z3);
|
1116
|
-
Y3 = Fp.mul(X3, Z3);
|
1117
|
-
t1 = Fp.add(t0, t0);
|
1118
|
-
t1 = Fp.add(t1, t0);
|
1119
|
-
t2 = Fp.mul(a, t2);
|
1120
|
-
t4 = Fp.mul(b3, t4);
|
1121
|
-
t1 = Fp.add(t1, t2);
|
1122
|
-
t2 = Fp.sub(t0, t2);
|
1123
|
-
t2 = Fp.mul(a, t2);
|
1124
|
-
t4 = Fp.add(t4, t2);
|
1125
|
-
t0 = Fp.mul(t1, t4);
|
1126
|
-
Y3 = Fp.add(Y3, t0);
|
1127
|
-
t0 = Fp.mul(t5, t4);
|
1128
|
-
X3 = Fp.mul(t3, X3);
|
1129
|
-
X3 = Fp.sub(X3, t0);
|
1130
|
-
t0 = Fp.mul(t3, t1);
|
1131
|
-
Z3 = Fp.mul(t5, Z3);
|
1132
|
-
Z3 = Fp.add(Z3, t0);
|
1133
|
-
return new Point(X3, Y3, Z3);
|
1134
|
-
}
|
1135
|
-
subtract(other) {
|
1136
|
-
return this.add(other.negate());
|
1137
|
-
}
|
1138
|
-
is0() {
|
1139
|
-
return this.equals(Point.ZERO);
|
1140
|
-
}
|
1141
|
-
wNAF(n) {
|
1142
|
-
return wnaf.wNAFCached(this, n, Point.normalizeZ);
|
1143
|
-
}
|
1144
|
-
/**
|
1145
|
-
* Non-constant-time multiplication. Uses double-and-add algorithm.
|
1146
|
-
* It's faster, but should only be used when you don't care about
|
1147
|
-
* an exposed private key e.g. sig verification, which works over *public* keys.
|
1148
|
-
*/
|
1149
|
-
multiplyUnsafe(sc) {
|
1150
|
-
const { endo: endo2, n: N } = CURVE;
|
1151
|
-
aInRange("scalar", sc, _0n$1, N);
|
1152
|
-
const I = Point.ZERO;
|
1153
|
-
if (sc === _0n$1)
|
1154
|
-
return I;
|
1155
|
-
if (this.is0() || sc === _1n$1)
|
1156
|
-
return this;
|
1157
|
-
if (!endo2 || wnaf.hasPrecomputes(this))
|
1158
|
-
return wnaf.wNAFCachedUnsafe(this, sc, Point.normalizeZ);
|
1159
|
-
let { k1neg, k1, k2neg, k2 } = endo2.splitScalar(sc);
|
1160
|
-
let k1p = I;
|
1161
|
-
let k2p = I;
|
1162
|
-
let d = this;
|
1163
|
-
while (k1 > _0n$1 || k2 > _0n$1) {
|
1164
|
-
if (k1 & _1n$1)
|
1165
|
-
k1p = k1p.add(d);
|
1166
|
-
if (k2 & _1n$1)
|
1167
|
-
k2p = k2p.add(d);
|
1168
|
-
d = d.double();
|
1169
|
-
k1 >>= _1n$1;
|
1170
|
-
k2 >>= _1n$1;
|
1171
|
-
}
|
1172
|
-
if (k1neg)
|
1173
|
-
k1p = k1p.negate();
|
1174
|
-
if (k2neg)
|
1175
|
-
k2p = k2p.negate();
|
1176
|
-
k2p = new Point(Fp.mul(k2p.px, endo2.beta), k2p.py, k2p.pz);
|
1177
|
-
return k1p.add(k2p);
|
1178
|
-
}
|
1179
|
-
/**
|
1180
|
-
* Constant time multiplication.
|
1181
|
-
* Uses wNAF method. Windowed method may be 10% faster,
|
1182
|
-
* but takes 2x longer to generate and consumes 2x memory.
|
1183
|
-
* Uses precomputes when available.
|
1184
|
-
* Uses endomorphism for Koblitz curves.
|
1185
|
-
* @param scalar by which the point would be multiplied
|
1186
|
-
* @returns New point
|
1187
|
-
*/
|
1188
|
-
multiply(scalar) {
|
1189
|
-
const { endo: endo2, n: N } = CURVE;
|
1190
|
-
aInRange("scalar", scalar, _1n$1, N);
|
1191
|
-
let point, fake;
|
1192
|
-
if (endo2) {
|
1193
|
-
const { k1neg, k1, k2neg, k2 } = endo2.splitScalar(scalar);
|
1194
|
-
let { p: k1p, f: f1p } = this.wNAF(k1);
|
1195
|
-
let { p: k2p, f: f2p } = this.wNAF(k2);
|
1196
|
-
k1p = wnaf.constTimeNegate(k1neg, k1p);
|
1197
|
-
k2p = wnaf.constTimeNegate(k2neg, k2p);
|
1198
|
-
k2p = new Point(Fp.mul(k2p.px, endo2.beta), k2p.py, k2p.pz);
|
1199
|
-
point = k1p.add(k2p);
|
1200
|
-
fake = f1p.add(f2p);
|
1201
|
-
} else {
|
1202
|
-
const { p, f } = this.wNAF(scalar);
|
1203
|
-
point = p;
|
1204
|
-
fake = f;
|
1205
|
-
}
|
1206
|
-
return Point.normalizeZ([point, fake])[0];
|
1207
|
-
}
|
1208
|
-
/**
|
1209
|
-
* Efficiently calculate `aP + bQ`. Unsafe, can expose private key, if used incorrectly.
|
1210
|
-
* Not using Strauss-Shamir trick: precomputation tables are faster.
|
1211
|
-
* The trick could be useful if both P and Q are not G (not in our case).
|
1212
|
-
* @returns non-zero affine point
|
1213
|
-
*/
|
1214
|
-
multiplyAndAddUnsafe(Q, a, b) {
|
1215
|
-
const G = Point.BASE;
|
1216
|
-
const mul = (P, a2) => a2 === _0n$1 || a2 === _1n$1 || !P.equals(G) ? P.multiplyUnsafe(a2) : P.multiply(a2);
|
1217
|
-
const sum = mul(this, a).add(mul(Q, b));
|
1218
|
-
return sum.is0() ? void 0 : sum;
|
1219
|
-
}
|
1220
|
-
// Converts Projective point to affine (x, y) coordinates.
|
1221
|
-
// Can accept precomputed Z^-1 - for example, from invertBatch.
|
1222
|
-
// (x, y, z) ∋ (x=x/z, y=y/z)
|
1223
|
-
toAffine(iz) {
|
1224
|
-
return toAffineMemo(this, iz);
|
1225
|
-
}
|
1226
|
-
isTorsionFree() {
|
1227
|
-
const { h: cofactor, isTorsionFree } = CURVE;
|
1228
|
-
if (cofactor === _1n$1)
|
1229
|
-
return true;
|
1230
|
-
if (isTorsionFree)
|
1231
|
-
return isTorsionFree(Point, this);
|
1232
|
-
throw new Error("isTorsionFree() has not been declared for the elliptic curve");
|
1233
|
-
}
|
1234
|
-
clearCofactor() {
|
1235
|
-
const { h: cofactor, clearCofactor } = CURVE;
|
1236
|
-
if (cofactor === _1n$1)
|
1237
|
-
return this;
|
1238
|
-
if (clearCofactor)
|
1239
|
-
return clearCofactor(Point, this);
|
1240
|
-
return this.multiplyUnsafe(CURVE.h);
|
1241
|
-
}
|
1242
|
-
toRawBytes(isCompressed = true) {
|
1243
|
-
abool("isCompressed", isCompressed);
|
1244
|
-
this.assertValidity();
|
1245
|
-
return toBytes(Point, this, isCompressed);
|
1246
|
-
}
|
1247
|
-
toHex(isCompressed = true) {
|
1248
|
-
abool("isCompressed", isCompressed);
|
1249
|
-
return bytesToHex(this.toRawBytes(isCompressed));
|
1250
|
-
}
|
1251
|
-
}
|
1252
|
-
Point.BASE = new Point(CURVE.Gx, CURVE.Gy, Fp.ONE);
|
1253
|
-
Point.ZERO = new Point(Fp.ZERO, Fp.ONE, Fp.ZERO);
|
1254
|
-
const { endo, nBitLength } = CURVE;
|
1255
|
-
const wnaf = wNAF(Point, endo ? Math.ceil(nBitLength / 2) : nBitLength);
|
1256
|
-
return {
|
1257
|
-
CURVE,
|
1258
|
-
ProjectivePoint: Point,
|
1259
|
-
normPrivateKeyToScalar,
|
1260
|
-
weierstrassEquation,
|
1261
|
-
isWithinCurveOrder
|
1262
|
-
};
|
1263
|
-
}
|
1264
|
-
function validateOpts(curve) {
|
1265
|
-
const opts = validateBasic(curve);
|
1266
|
-
validateObject(opts, {
|
1267
|
-
hash: "hash",
|
1268
|
-
hmac: "function",
|
1269
|
-
randomBytes: "function"
|
1270
|
-
}, {
|
1271
|
-
bits2int: "function",
|
1272
|
-
bits2int_modN: "function",
|
1273
|
-
lowS: "boolean"
|
1274
|
-
});
|
1275
|
-
return Object.freeze({ lowS: true, ...opts });
|
1276
|
-
}
|
1277
|
-
function weierstrass(curveDef) {
|
1278
|
-
const CURVE = validateOpts(curveDef);
|
1279
|
-
const { Fp, n: CURVE_ORDER, nByteLength, nBitLength } = CURVE;
|
1280
|
-
const compressedLen = Fp.BYTES + 1;
|
1281
|
-
const uncompressedLen = 2 * Fp.BYTES + 1;
|
1282
|
-
function modN(a) {
|
1283
|
-
return mod(a, CURVE_ORDER);
|
1284
|
-
}
|
1285
|
-
function invN(a) {
|
1286
|
-
return invert(a, CURVE_ORDER);
|
1287
|
-
}
|
1288
|
-
const { ProjectivePoint: Point, normPrivateKeyToScalar, weierstrassEquation, isWithinCurveOrder } = weierstrassPoints({
|
1289
|
-
...CURVE,
|
1290
|
-
toBytes(_c, point, isCompressed) {
|
1291
|
-
const a = point.toAffine();
|
1292
|
-
const x = Fp.toBytes(a.x);
|
1293
|
-
const cat = concatBytes;
|
1294
|
-
abool("isCompressed", isCompressed);
|
1295
|
-
if (isCompressed) {
|
1296
|
-
return cat(Uint8Array.from([point.hasEvenY() ? 2 : 3]), x);
|
1297
|
-
} else {
|
1298
|
-
return cat(Uint8Array.from([4]), x, Fp.toBytes(a.y));
|
1299
|
-
}
|
1300
|
-
},
|
1301
|
-
fromBytes(bytes) {
|
1302
|
-
const len = bytes.length;
|
1303
|
-
const head = bytes[0];
|
1304
|
-
const tail = bytes.subarray(1);
|
1305
|
-
if (len === compressedLen && (head === 2 || head === 3)) {
|
1306
|
-
const x = bytesToNumberBE(tail);
|
1307
|
-
if (!inRange(x, _1n$1, Fp.ORDER))
|
1308
|
-
throw new Error("Point is not on curve");
|
1309
|
-
const y2 = weierstrassEquation(x);
|
1310
|
-
let y;
|
1311
|
-
try {
|
1312
|
-
y = Fp.sqrt(y2);
|
1313
|
-
} catch (sqrtError) {
|
1314
|
-
const suffix = sqrtError instanceof Error ? ": " + sqrtError.message : "";
|
1315
|
-
throw new Error("Point is not on curve" + suffix);
|
1316
|
-
}
|
1317
|
-
const isYOdd = (y & _1n$1) === _1n$1;
|
1318
|
-
const isHeadOdd = (head & 1) === 1;
|
1319
|
-
if (isHeadOdd !== isYOdd)
|
1320
|
-
y = Fp.neg(y);
|
1321
|
-
return { x, y };
|
1322
|
-
} else if (len === uncompressedLen && head === 4) {
|
1323
|
-
const x = Fp.fromBytes(tail.subarray(0, Fp.BYTES));
|
1324
|
-
const y = Fp.fromBytes(tail.subarray(Fp.BYTES, 2 * Fp.BYTES));
|
1325
|
-
return { x, y };
|
1326
|
-
} else {
|
1327
|
-
const cl = compressedLen;
|
1328
|
-
const ul = uncompressedLen;
|
1329
|
-
throw new Error("invalid Point, expected length of " + cl + ", or uncompressed " + ul + ", got " + len);
|
1330
|
-
}
|
1331
|
-
}
|
1332
|
-
});
|
1333
|
-
function isBiggerThanHalfOrder(number) {
|
1334
|
-
const HALF = CURVE_ORDER >> _1n$1;
|
1335
|
-
return number > HALF;
|
1336
|
-
}
|
1337
|
-
function normalizeS(s) {
|
1338
|
-
return isBiggerThanHalfOrder(s) ? modN(-s) : s;
|
1339
|
-
}
|
1340
|
-
const slcNum = (b, from, to) => bytesToNumberBE(b.slice(from, to));
|
1341
|
-
class Signature {
|
1342
|
-
constructor(r, s, recovery) {
|
1343
|
-
aInRange("r", r, _1n$1, CURVE_ORDER);
|
1344
|
-
aInRange("s", s, _1n$1, CURVE_ORDER);
|
1345
|
-
this.r = r;
|
1346
|
-
this.s = s;
|
1347
|
-
if (recovery != null)
|
1348
|
-
this.recovery = recovery;
|
1349
|
-
Object.freeze(this);
|
1350
|
-
}
|
1351
|
-
// pair (bytes of r, bytes of s)
|
1352
|
-
static fromCompact(hex) {
|
1353
|
-
const l = nByteLength;
|
1354
|
-
hex = ensureBytes("compactSignature", hex, l * 2);
|
1355
|
-
return new Signature(slcNum(hex, 0, l), slcNum(hex, l, 2 * l));
|
1356
|
-
}
|
1357
|
-
// DER encoded ECDSA signature
|
1358
|
-
// https://bitcoin.stackexchange.com/questions/57644/what-are-the-parts-of-a-bitcoin-transaction-input-script
|
1359
|
-
static fromDER(hex) {
|
1360
|
-
const { r, s } = DER.toSig(ensureBytes("DER", hex));
|
1361
|
-
return new Signature(r, s);
|
1362
|
-
}
|
1363
|
-
/**
|
1364
|
-
* @todo remove
|
1365
|
-
* @deprecated
|
1366
|
-
*/
|
1367
|
-
assertValidity() {
|
1368
|
-
}
|
1369
|
-
addRecoveryBit(recovery) {
|
1370
|
-
return new Signature(this.r, this.s, recovery);
|
1371
|
-
}
|
1372
|
-
recoverPublicKey(msgHash) {
|
1373
|
-
const { r, s, recovery: rec } = this;
|
1374
|
-
const h = bits2int_modN(ensureBytes("msgHash", msgHash));
|
1375
|
-
if (rec == null || ![0, 1, 2, 3].includes(rec))
|
1376
|
-
throw new Error("recovery id invalid");
|
1377
|
-
const radj = rec === 2 || rec === 3 ? r + CURVE.n : r;
|
1378
|
-
if (radj >= Fp.ORDER)
|
1379
|
-
throw new Error("recovery id 2 or 3 invalid");
|
1380
|
-
const prefix = (rec & 1) === 0 ? "02" : "03";
|
1381
|
-
const R = Point.fromHex(prefix + numToSizedHex(radj, Fp.BYTES));
|
1382
|
-
const ir = invN(radj);
|
1383
|
-
const u1 = modN(-h * ir);
|
1384
|
-
const u2 = modN(s * ir);
|
1385
|
-
const Q = Point.BASE.multiplyAndAddUnsafe(R, u1, u2);
|
1386
|
-
if (!Q)
|
1387
|
-
throw new Error("point at infinify");
|
1388
|
-
Q.assertValidity();
|
1389
|
-
return Q;
|
1390
|
-
}
|
1391
|
-
// Signatures should be low-s, to prevent malleability.
|
1392
|
-
hasHighS() {
|
1393
|
-
return isBiggerThanHalfOrder(this.s);
|
1394
|
-
}
|
1395
|
-
normalizeS() {
|
1396
|
-
return this.hasHighS() ? new Signature(this.r, modN(-this.s), this.recovery) : this;
|
1397
|
-
}
|
1398
|
-
// DER-encoded
|
1399
|
-
toDERRawBytes() {
|
1400
|
-
return hexToBytes(this.toDERHex());
|
1401
|
-
}
|
1402
|
-
toDERHex() {
|
1403
|
-
return DER.hexFromSig(this);
|
1404
|
-
}
|
1405
|
-
// padded bytes of r, then padded bytes of s
|
1406
|
-
toCompactRawBytes() {
|
1407
|
-
return hexToBytes(this.toCompactHex());
|
1408
|
-
}
|
1409
|
-
toCompactHex() {
|
1410
|
-
const l = nByteLength;
|
1411
|
-
return numToSizedHex(this.r, l) + numToSizedHex(this.s, l);
|
1412
|
-
}
|
1413
|
-
}
|
1414
|
-
const utils = {
|
1415
|
-
isValidPrivateKey(privateKey) {
|
1416
|
-
try {
|
1417
|
-
normPrivateKeyToScalar(privateKey);
|
1418
|
-
return true;
|
1419
|
-
} catch (error) {
|
1420
|
-
return false;
|
1421
|
-
}
|
1422
|
-
},
|
1423
|
-
normPrivateKeyToScalar,
|
1424
|
-
/**
|
1425
|
-
* Produces cryptographically secure private key from random of size
|
1426
|
-
* (groupLen + ceil(groupLen / 2)) with modulo bias being negligible.
|
1427
|
-
*/
|
1428
|
-
randomPrivateKey: () => {
|
1429
|
-
const length = getMinHashLength(CURVE.n);
|
1430
|
-
return mapHashToField(CURVE.randomBytes(length), CURVE.n);
|
1431
|
-
},
|
1432
|
-
/**
|
1433
|
-
* Creates precompute table for an arbitrary EC point. Makes point "cached".
|
1434
|
-
* Allows to massively speed-up `point.multiply(scalar)`.
|
1435
|
-
* @returns cached point
|
1436
|
-
* @example
|
1437
|
-
* const fast = utils.precompute(8, ProjectivePoint.fromHex(someonesPubKey));
|
1438
|
-
* fast.multiply(privKey); // much faster ECDH now
|
1439
|
-
*/
|
1440
|
-
precompute(windowSize = 8, point = Point.BASE) {
|
1441
|
-
point._setWindowSize(windowSize);
|
1442
|
-
point.multiply(BigInt(3));
|
1443
|
-
return point;
|
1444
|
-
}
|
1445
|
-
};
|
1446
|
-
function getPublicKey(privateKey, isCompressed = true) {
|
1447
|
-
return Point.fromPrivateKey(privateKey).toRawBytes(isCompressed);
|
1448
|
-
}
|
1449
|
-
function isProbPub(item) {
|
1450
|
-
if (typeof item === "bigint")
|
1451
|
-
return false;
|
1452
|
-
if (item instanceof Point)
|
1453
|
-
return true;
|
1454
|
-
const arr = ensureBytes("key", item);
|
1455
|
-
const len = arr.length;
|
1456
|
-
const fpl = Fp.BYTES;
|
1457
|
-
const compLen = fpl + 1;
|
1458
|
-
const uncompLen = 2 * fpl + 1;
|
1459
|
-
if (CURVE.allowedPrivateKeyLengths || nByteLength === compLen) {
|
1460
|
-
return void 0;
|
1461
|
-
} else {
|
1462
|
-
return len === compLen || len === uncompLen;
|
1463
|
-
}
|
1464
|
-
}
|
1465
|
-
function getSharedSecret(privateA, publicB, isCompressed = true) {
|
1466
|
-
if (isProbPub(privateA) === true)
|
1467
|
-
throw new Error("first arg must be private key");
|
1468
|
-
if (isProbPub(publicB) === false)
|
1469
|
-
throw new Error("second arg must be public key");
|
1470
|
-
const b = Point.fromHex(publicB);
|
1471
|
-
return b.multiply(normPrivateKeyToScalar(privateA)).toRawBytes(isCompressed);
|
1472
|
-
}
|
1473
|
-
const bits2int = CURVE.bits2int || function(bytes) {
|
1474
|
-
if (bytes.length > 8192)
|
1475
|
-
throw new Error("input is too large");
|
1476
|
-
const num = bytesToNumberBE(bytes);
|
1477
|
-
const delta = bytes.length * 8 - nBitLength;
|
1478
|
-
return delta > 0 ? num >> BigInt(delta) : num;
|
1479
|
-
};
|
1480
|
-
const bits2int_modN = CURVE.bits2int_modN || function(bytes) {
|
1481
|
-
return modN(bits2int(bytes));
|
1482
|
-
};
|
1483
|
-
const ORDER_MASK = bitMask(nBitLength);
|
1484
|
-
function int2octets(num) {
|
1485
|
-
aInRange("num < 2^" + nBitLength, num, _0n$1, ORDER_MASK);
|
1486
|
-
return numberToBytesBE(num, nByteLength);
|
1487
|
-
}
|
1488
|
-
function prepSig(msgHash, privateKey, opts = defaultSigOpts) {
|
1489
|
-
if (["recovered", "canonical"].some((k) => k in opts))
|
1490
|
-
throw new Error("sign() legacy options not supported");
|
1491
|
-
const { hash, randomBytes: randomBytes2 } = CURVE;
|
1492
|
-
let { lowS, prehash, extraEntropy: ent } = opts;
|
1493
|
-
if (lowS == null)
|
1494
|
-
lowS = true;
|
1495
|
-
msgHash = ensureBytes("msgHash", msgHash);
|
1496
|
-
validateSigVerOpts(opts);
|
1497
|
-
if (prehash)
|
1498
|
-
msgHash = ensureBytes("prehashed msgHash", hash(msgHash));
|
1499
|
-
const h1int = bits2int_modN(msgHash);
|
1500
|
-
const d = normPrivateKeyToScalar(privateKey);
|
1501
|
-
const seedArgs = [int2octets(d), int2octets(h1int)];
|
1502
|
-
if (ent != null && ent !== false) {
|
1503
|
-
const e = ent === true ? randomBytes2(Fp.BYTES) : ent;
|
1504
|
-
seedArgs.push(ensureBytes("extraEntropy", e));
|
1505
|
-
}
|
1506
|
-
const seed = concatBytes(...seedArgs);
|
1507
|
-
const m = h1int;
|
1508
|
-
function k2sig(kBytes) {
|
1509
|
-
const k = bits2int(kBytes);
|
1510
|
-
if (!isWithinCurveOrder(k))
|
1511
|
-
return;
|
1512
|
-
const ik = invN(k);
|
1513
|
-
const q = Point.BASE.multiply(k).toAffine();
|
1514
|
-
const r = modN(q.x);
|
1515
|
-
if (r === _0n$1)
|
1516
|
-
return;
|
1517
|
-
const s = modN(ik * modN(m + r * d));
|
1518
|
-
if (s === _0n$1)
|
1519
|
-
return;
|
1520
|
-
let recovery = (q.x === r ? 0 : 2) | Number(q.y & _1n$1);
|
1521
|
-
let normS = s;
|
1522
|
-
if (lowS && isBiggerThanHalfOrder(s)) {
|
1523
|
-
normS = normalizeS(s);
|
1524
|
-
recovery ^= 1;
|
1525
|
-
}
|
1526
|
-
return new Signature(r, normS, recovery);
|
1527
|
-
}
|
1528
|
-
return { seed, k2sig };
|
1529
|
-
}
|
1530
|
-
const defaultSigOpts = { lowS: CURVE.lowS, prehash: false };
|
1531
|
-
const defaultVerOpts = { lowS: CURVE.lowS, prehash: false };
|
1532
|
-
function sign(msgHash, privKey, opts = defaultSigOpts) {
|
1533
|
-
const { seed, k2sig } = prepSig(msgHash, privKey, opts);
|
1534
|
-
const C = CURVE;
|
1535
|
-
const drbg = createHmacDrbg(C.hash.outputLen, C.nByteLength, C.hmac);
|
1536
|
-
return drbg(seed, k2sig);
|
1537
|
-
}
|
1538
|
-
Point.BASE._setWindowSize(8);
|
1539
|
-
function verify(signature, msgHash, publicKey, opts = defaultVerOpts) {
|
1540
|
-
var _a;
|
1541
|
-
const sg = signature;
|
1542
|
-
msgHash = ensureBytes("msgHash", msgHash);
|
1543
|
-
publicKey = ensureBytes("publicKey", publicKey);
|
1544
|
-
const { lowS, prehash, format } = opts;
|
1545
|
-
validateSigVerOpts(opts);
|
1546
|
-
if ("strict" in opts)
|
1547
|
-
throw new Error("options.strict was renamed to lowS");
|
1548
|
-
if (format !== void 0 && format !== "compact" && format !== "der")
|
1549
|
-
throw new Error("format must be compact or der");
|
1550
|
-
const isHex = typeof sg === "string" || isBytes(sg);
|
1551
|
-
const isObj = !isHex && !format && typeof sg === "object" && sg !== null && typeof sg.r === "bigint" && typeof sg.s === "bigint";
|
1552
|
-
if (!isHex && !isObj)
|
1553
|
-
throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");
|
1554
|
-
let _sig = void 0;
|
1555
|
-
let P;
|
1556
|
-
try {
|
1557
|
-
if (isObj)
|
1558
|
-
_sig = new Signature(sg.r, sg.s);
|
1559
|
-
if (isHex) {
|
1560
|
-
try {
|
1561
|
-
if (format !== "compact")
|
1562
|
-
_sig = Signature.fromDER(sg);
|
1563
|
-
} catch (derError) {
|
1564
|
-
if (!(derError instanceof DER.Err))
|
1565
|
-
throw derError;
|
1566
|
-
}
|
1567
|
-
if (!_sig && format !== "der")
|
1568
|
-
_sig = Signature.fromCompact(sg);
|
1569
|
-
}
|
1570
|
-
P = Point.fromHex(publicKey);
|
1571
|
-
} catch (error) {
|
1572
|
-
return false;
|
1573
|
-
}
|
1574
|
-
if (!_sig)
|
1575
|
-
return false;
|
1576
|
-
if (lowS && _sig.hasHighS())
|
1577
|
-
return false;
|
1578
|
-
if (prehash)
|
1579
|
-
msgHash = CURVE.hash(msgHash);
|
1580
|
-
const { r, s } = _sig;
|
1581
|
-
const h = bits2int_modN(msgHash);
|
1582
|
-
const is = invN(s);
|
1583
|
-
const u1 = modN(h * is);
|
1584
|
-
const u2 = modN(r * is);
|
1585
|
-
const R = (_a = Point.BASE.multiplyAndAddUnsafe(P, u1, u2)) == null ? void 0 : _a.toAffine();
|
1586
|
-
if (!R)
|
1587
|
-
return false;
|
1588
|
-
const v = modN(R.x);
|
1589
|
-
return v === r;
|
1590
|
-
}
|
1591
|
-
return {
|
1592
|
-
CURVE,
|
1593
|
-
getPublicKey,
|
1594
|
-
getSharedSecret,
|
1595
|
-
sign,
|
1596
|
-
verify,
|
1597
|
-
ProjectivePoint: Point,
|
1598
|
-
Signature,
|
1599
|
-
utils
|
1600
|
-
};
|
1601
|
-
}
|
1602
|
-
function getHash(hash) {
|
1603
|
-
return {
|
1604
|
-
hash,
|
1605
|
-
hmac: (key, ...msgs) => hmac(hash, key, concatBytes$1(...msgs)),
|
1606
|
-
randomBytes
|
1607
|
-
};
|
1608
|
-
}
|
1609
|
-
function createCurve(curveDef, defHash) {
|
1610
|
-
const create = (hash) => weierstrass({ ...curveDef, ...getHash(hash) });
|
1611
|
-
return { ...create(defHash), create };
|
1612
|
-
}
|
1613
|
-
const secp256k1P = BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f");
|
1614
|
-
const secp256k1N = BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
|
1615
|
-
const _0n = BigInt(0);
|
1616
|
-
const _1n = BigInt(1);
|
1617
|
-
const _2n = BigInt(2);
|
1618
|
-
const divNearest = (a, b) => (a + b / _2n) / b;
|
1619
|
-
function sqrtMod(y) {
|
1620
|
-
const P = secp256k1P;
|
1621
|
-
const _3n2 = BigInt(3), _6n = BigInt(6), _11n = BigInt(11), _22n = BigInt(22);
|
1622
|
-
const _23n = BigInt(23), _44n = BigInt(44), _88n = BigInt(88);
|
1623
|
-
const b2 = y * y * y % P;
|
1624
|
-
const b3 = b2 * b2 * y % P;
|
1625
|
-
const b6 = pow2(b3, _3n2, P) * b3 % P;
|
1626
|
-
const b9 = pow2(b6, _3n2, P) * b3 % P;
|
1627
|
-
const b11 = pow2(b9, _2n, P) * b2 % P;
|
1628
|
-
const b22 = pow2(b11, _11n, P) * b11 % P;
|
1629
|
-
const b44 = pow2(b22, _22n, P) * b22 % P;
|
1630
|
-
const b88 = pow2(b44, _44n, P) * b44 % P;
|
1631
|
-
const b176 = pow2(b88, _88n, P) * b88 % P;
|
1632
|
-
const b220 = pow2(b176, _44n, P) * b44 % P;
|
1633
|
-
const b223 = pow2(b220, _3n2, P) * b3 % P;
|
1634
|
-
const t1 = pow2(b223, _23n, P) * b22 % P;
|
1635
|
-
const t2 = pow2(t1, _6n, P) * b2 % P;
|
1636
|
-
const root = pow2(t2, _2n, P);
|
1637
|
-
if (!Fpk1.eql(Fpk1.sqr(root), y))
|
1638
|
-
throw new Error("Cannot find square root");
|
1639
|
-
return root;
|
1640
|
-
}
|
1641
|
-
const Fpk1 = Field(secp256k1P, void 0, void 0, { sqrt: sqrtMod });
|
1642
|
-
const secp256k1 = createCurve({
|
1643
|
-
a: _0n,
|
1644
|
-
b: BigInt(7),
|
1645
|
-
Fp: Fpk1,
|
1646
|
-
n: secp256k1N,
|
1647
|
-
Gx: BigInt("55066263022277343669578718895168534326250603453777594175500187360389116729240"),
|
1648
|
-
Gy: BigInt("32670510020758816978083085130507043184471273380659243275938904335757337482424"),
|
1649
|
-
h: BigInt(1),
|
1650
|
-
lowS: true,
|
1651
|
-
// Allow only low-S signatures by default in sign() and verify()
|
1652
|
-
endo: {
|
1653
|
-
// Endomorphism, see above
|
1654
|
-
beta: BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),
|
1655
|
-
splitScalar: (k) => {
|
1656
|
-
const n = secp256k1N;
|
1657
|
-
const a1 = BigInt("0x3086d221a7d46bcde86c90e49284eb15");
|
1658
|
-
const b1 = -_1n * BigInt("0xe4437ed6010e88286f547fa90abfe4c3");
|
1659
|
-
const a2 = BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8");
|
1660
|
-
const b2 = a1;
|
1661
|
-
const POW_2_128 = BigInt("0x100000000000000000000000000000000");
|
1662
|
-
const c1 = divNearest(b2 * k, n);
|
1663
|
-
const c2 = divNearest(-b1 * k, n);
|
1664
|
-
let k1 = mod(k - c1 * a1 - c2 * a2, n);
|
1665
|
-
let k2 = mod(-c1 * b1 - c2 * b2, n);
|
1666
|
-
const k1neg = k1 > POW_2_128;
|
1667
|
-
const k2neg = k2 > POW_2_128;
|
1668
|
-
if (k1neg)
|
1669
|
-
k1 = n - k1;
|
1670
|
-
if (k2neg)
|
1671
|
-
k2 = n - k2;
|
1672
|
-
if (k1 > POW_2_128 || k2 > POW_2_128) {
|
1673
|
-
throw new Error("splitScalar: Endomorphism failed, k=" + k);
|
1674
|
-
}
|
1675
|
-
return { k1neg, k1, k2neg, k2 };
|
1676
|
-
}
|
1677
|
-
}
|
1678
|
-
}, sha256);
|
1679
|
-
export {
|
1680
|
-
secp256k1
|
1681
|
-
};
|