@gardenfi/utils 2.5.3-beta.1 → 2.5.3-beta.2
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/index10.cjs +1 -1
- package/dist/index10.js +4 -4
- package/dist/index100.cjs +1 -1
- package/dist/index100.js +13 -7
- package/dist/index101.cjs +1 -1
- package/dist/index101.js +3 -12
- package/dist/index102.cjs +1 -1
- package/dist/index102.js +26 -18
- package/dist/index103.cjs +2 -1
- package/dist/index103.js +19 -22
- package/dist/index104.cjs +1 -1
- package/dist/index104.js +9 -21
- package/dist/index105.cjs +1 -1
- package/dist/index105.js +8 -51
- package/dist/index106.cjs +1 -1
- package/dist/index106.js +10 -11
- package/dist/index107.cjs +1 -1
- package/dist/index107.js +11 -161
- package/dist/index108.cjs +1 -1
- package/dist/index108.js +13 -21
- package/dist/index109.cjs +1 -1
- package/dist/index109.js +14 -127
- package/dist/index110.cjs +1 -1
- package/dist/index110.js +4 -38
- package/dist/index111.cjs +1 -1
- package/dist/index111.js +6 -6
- package/dist/index112.cjs +1 -1
- package/dist/index112.js +16 -25
- package/dist/index113.cjs +1 -1
- package/dist/index113.js +13 -55
- package/dist/index114.cjs +1 -1
- package/dist/index114.js +6 -45
- package/dist/index115.cjs +1 -1
- package/dist/index115.js +7 -191
- package/dist/index116.cjs +1 -1
- package/dist/index116.js +22 -4
- package/dist/index117.cjs +1 -1
- package/dist/index117.js +7 -64
- package/dist/index118.cjs +1 -1
- package/dist/index118.js +22 -3
- package/dist/index119.cjs +1 -1
- package/dist/index119.js +16 -19
- package/dist/index120.cjs +1 -1
- package/dist/index120.js +39 -15
- package/dist/index121.cjs +1 -1
- package/dist/index121.js +21 -23
- package/dist/index122.cjs +1 -1
- package/dist/index122.js +80 -11
- package/dist/index123.cjs +1 -1
- package/dist/index123.js +11 -18
- package/dist/index124.cjs +1 -1
- package/dist/index124.js +55 -31
- package/dist/index125.cjs +1 -1
- package/dist/index125.js +14 -3
- package/dist/index126.cjs +1 -1
- package/dist/index126.js +8 -18
- package/dist/index127.cjs +1 -1
- package/dist/index127.js +20 -27
- package/dist/index128.cjs +1 -1
- package/dist/index128.js +6 -36
- package/dist/index129.cjs +1 -1
- package/dist/index129.js +35 -8
- package/dist/index130.cjs +1 -1
- package/dist/index130.js +32 -10
- package/dist/index131.cjs +1 -1
- package/dist/index131.js +8 -24
- package/dist/index132.cjs +1 -1
- package/dist/index132.js +50 -28
- package/dist/index133.cjs +1 -1
- package/dist/index133.js +19 -65
- package/dist/index134.cjs +1 -1
- package/dist/index134.js +106 -9
- package/dist/index135.cjs +1 -1
- package/dist/index135.js +66 -21
- package/dist/index136.cjs +1 -1
- package/dist/index136.js +9 -24
- package/dist/index137.cjs +1 -1
- package/dist/index137.js +71 -24
- package/dist/index138.cjs +2 -1
- package/dist/index138.js +3 -4
- package/dist/index139.cjs +1 -1
- package/dist/index139.js +9 -15
- package/dist/index140.cjs +1 -1
- package/dist/index140.js +11 -101
- package/dist/index141.cjs +1 -1
- package/dist/index141.js +4 -80
- package/dist/index142.cjs +1 -1
- package/dist/index142.js +12 -5
- package/dist/index143.cjs +1 -1
- package/dist/index143.js +2 -7
- package/dist/index144.cjs +1 -1
- package/dist/index144.js +31 -27
- package/dist/index145.cjs +1 -1
- package/dist/index145.js +21 -26
- package/dist/index146.cjs +1 -1
- package/dist/index146.js +5 -13
- package/dist/index147.cjs +1 -1
- package/dist/index147.js +5 -42
- package/dist/index148.cjs +1 -1
- package/dist/index148.js +5 -19
- package/dist/index149.cjs +9 -1
- package/dist/index149.js +31 -74
- package/dist/index150.cjs +1 -1
- package/dist/index150.js +3 -2
- package/dist/index151.cjs +1 -1
- package/dist/index151.js +13 -32
- package/dist/index152.cjs +1 -1
- package/dist/index152.js +16 -107
- package/dist/index153.cjs +1 -1
- package/dist/index153.js +33 -164
- package/dist/index154.cjs +1 -1
- package/dist/index154.js +6 -47
- package/dist/index155.cjs +4 -1
- package/dist/index155.js +194 -32
- package/dist/index156.cjs +1 -1
- package/dist/index156.js +27 -8
- package/dist/index157.cjs +1 -1
- package/dist/index157.js +161 -6
- package/dist/index158.cjs +1 -1
- package/dist/index158.js +127 -6
- package/dist/index159.cjs +1 -1
- package/dist/index159.js +27 -11
- package/dist/index160.cjs +1 -1
- package/dist/index160.js +54 -9
- package/dist/index161.cjs +1 -1
- package/dist/index161.js +190 -12
- package/dist/index162.cjs +1 -1
- package/dist/index162.js +4 -33
- package/dist/index163.cjs +1 -1
- package/dist/index163.js +21 -5
- package/dist/index164.cjs +1 -1
- package/dist/index164.js +3 -8
- package/dist/index165.cjs +1 -1
- package/dist/index165.js +14 -6
- package/dist/index166.cjs +1 -1
- package/dist/index166.js +11 -20
- package/dist/index167.cjs +1 -1
- package/dist/index167.js +32 -29
- package/dist/index168.cjs +1 -1
- package/dist/index168.js +3 -4
- package/dist/index169.cjs +1 -1
- package/dist/index169.js +27 -17
- package/dist/index17.cjs +1 -1
- package/dist/index17.js +2 -2
- package/dist/index170.cjs +1 -1
- package/dist/index170.js +8 -10
- package/dist/index171.cjs +1 -1
- package/dist/index171.js +24 -20
- package/dist/index172.cjs +1 -1
- package/dist/index172.js +28 -22
- package/dist/index173.cjs +1 -1
- package/dist/index173.js +3 -5
- package/dist/index174.cjs +1 -1
- package/dist/index174.js +6 -5
- package/dist/index175.cjs +1 -1
- package/dist/index175.js +6 -8
- package/dist/index176.cjs +1 -1
- package/dist/index176.js +30 -102
- package/dist/index177.cjs +1 -1
- package/dist/index177.js +5 -3
- package/dist/index178.cjs +1 -1
- package/dist/index178.js +36 -17
- package/dist/index179.cjs +1 -1
- package/dist/index179.js +45 -167
- package/dist/index18.cjs +1 -1
- package/dist/index18.js +8 -8
- package/dist/index180.cjs +1 -1
- package/dist/index180.js +32 -17
- package/dist/index181.cjs +1 -1
- package/dist/index181.js +6 -28
- package/dist/index182.cjs +1 -1
- package/dist/index182.js +43 -2
- package/dist/index183.cjs +1 -1
- package/dist/index183.js +51 -13
- package/dist/index184.cjs +1 -1
- package/dist/index184.js +104 -12
- package/dist/index185.cjs +1 -1
- package/dist/index185.js +3 -48
- package/dist/index186.cjs +1 -1
- package/dist/index186.js +5 -11
- package/dist/index187.cjs +1 -1
- package/dist/index187.js +3 -7
- package/dist/index188.cjs +1 -1
- package/dist/index188.js +16 -88
- package/dist/index189.cjs +1 -1
- package/dist/index189.js +5 -46
- package/dist/index190.cjs +1 -1
- package/dist/index190.js +8 -37
- package/dist/index191.cjs +1 -1
- package/dist/index191.js +103 -17
- package/dist/index192.cjs +1 -1
- package/dist/index192.js +5 -6
- package/dist/index193.cjs +1 -1
- package/dist/index193.js +6 -9
- package/dist/index194.cjs +1 -1
- package/dist/index194.js +27 -13
- package/dist/index195.cjs +1 -1
- package/dist/index195.js +34 -8
- package/dist/index196.cjs +1 -1
- package/dist/index196.js +13 -8
- package/dist/index197.cjs +1 -1
- package/dist/index197.js +78 -35
- package/dist/index198.cjs +1 -1
- package/dist/index198.js +2 -17
- package/dist/index199.cjs +1 -1
- package/dist/index199.js +18 -14
- package/dist/index20.cjs +1 -1
- package/dist/index20.js +2 -2
- package/dist/index200.cjs +1 -1
- package/dist/index200.js +171 -13
- package/dist/index201.cjs +1 -2
- package/dist/index201.js +16 -23
- package/dist/index202.cjs +1 -1
- package/dist/index202.js +10 -6
- package/dist/index203.cjs +1 -1
- package/dist/index203.js +2 -2
- package/dist/index204.cjs +1 -1
- package/dist/index204.js +5 -5
- package/dist/index206.cjs +1 -1
- package/dist/index206.js +1 -1
- package/dist/index207.cjs +1 -1
- package/dist/index207.js +2 -2
- package/dist/index208.cjs +1 -1
- package/dist/index208.js +1 -1
- package/dist/index209.cjs +1 -1
- package/dist/index209.js +5 -5
- package/dist/index21.cjs +1 -1
- package/dist/index21.js +17 -4
- package/dist/index210.cjs +1 -2
- package/dist/index210.js +24 -41
- package/dist/index211.cjs +1 -1
- package/dist/index211.js +47 -2
- package/dist/index212.cjs +1 -1
- package/dist/index212.js +9 -5
- package/dist/index213.cjs +2 -1
- package/dist/index213.js +40 -9
- package/dist/index214.cjs +1 -1
- package/dist/index214.js +2 -20
- package/dist/index215.cjs +1 -1
- package/dist/index215.js +5 -8
- package/dist/index216.cjs +1 -1
- package/dist/index216.js +9 -65
- package/dist/index217.cjs +1 -1
- package/dist/index217.js +17 -63
- package/dist/index218.cjs +1 -1
- package/dist/index218.js +8 -186
- package/dist/index219.cjs +1 -1
- package/dist/index219.js +66 -28
- package/dist/index22.cjs +1 -1
- package/dist/index22.js +3 -3
- package/dist/index220.cjs +1 -1
- package/dist/index220.js +61 -72
- package/dist/index221.cjs +1 -1
- package/dist/index221.js +183 -35
- package/dist/index222.cjs +1 -1
- package/dist/index222.js +21 -9
- package/dist/index223.cjs +1 -1
- package/dist/index223.js +70 -7
- package/dist/index224.cjs +1 -1
- package/dist/index224.js +40 -12
- package/dist/index225.cjs +1 -1
- package/dist/index225.js +16 -11
- package/dist/index226.cjs +1 -1
- package/dist/index226.js +12 -102
- package/dist/index227.cjs +1 -1
- package/dist/index227.js +11 -6
- package/dist/index228.cjs +1 -1
- package/dist/index228.js +10 -242
- package/dist/index229.cjs +1 -1
- package/dist/index229.js +2 -2
- package/dist/index23.cjs +1 -1
- package/dist/index23.js +12 -26
- package/dist/index230.cjs +1 -1
- package/dist/index230.js +2 -2
- package/dist/index231.cjs +1 -1
- package/dist/index231.js +18 -25
- package/dist/index232.cjs +1 -1
- package/dist/index232.js +107 -18
- package/dist/index233.cjs +1 -1
- package/dist/index233.js +99 -142
- package/dist/index234.cjs +1 -1
- package/dist/index234.js +26 -24
- package/dist/index235.cjs +1 -1
- package/dist/index235.js +15 -45
- package/dist/index236.cjs +1 -1
- package/dist/index236.js +213 -8
- package/dist/index237.cjs +1 -1
- package/dist/index237.js +1 -1
- package/dist/index238.cjs +1 -1
- package/dist/index238.js +1 -1
- package/dist/index24.cjs +1 -1
- package/dist/index24.js +59 -8
- package/dist/index240.cjs +1 -1
- package/dist/index240.js +1 -1
- package/dist/index241.cjs +1 -1
- package/dist/index241.js +3 -3
- package/dist/index242.cjs +2 -1
- package/dist/index242.js +66 -68
- package/dist/index243.cjs +1 -1
- package/dist/index243.js +14 -788
- package/dist/index244.cjs +1 -1
- package/dist/index244.js +67 -147
- package/dist/index245.cjs +1 -1
- package/dist/index245.js +640 -183
- package/dist/index246.cjs +1 -2
- package/dist/index246.js +203 -63
- package/dist/index247.cjs +1 -1
- package/dist/index247.js +177 -14
- package/dist/index25.cjs +16 -1
- package/dist/index25.js +113 -12
- package/dist/index26.cjs +1 -1
- package/dist/index26.js +30 -58
- package/dist/index27.cjs +1 -16
- package/dist/index27.js +24 -111
- package/dist/index28.cjs +1 -1
- package/dist/index28.js +7 -29
- package/dist/index29.cjs +1 -1
- package/dist/index29.js +171 -7
- package/dist/index30.cjs +1 -1
- package/dist/index30.js +110 -30
- package/dist/index31.cjs +1 -1
- package/dist/index31.js +30 -29
- package/dist/index32.cjs +1 -1
- package/dist/index32.js +3 -16
- package/dist/index33.cjs +1 -1
- package/dist/index33.js +2 -34
- package/dist/index34.cjs +1 -1
- package/dist/index34.js +4 -45
- package/dist/index35.cjs +2 -1
- package/dist/index35.js +59 -151
- package/dist/index36.cjs +2 -1
- package/dist/index36.js +50 -31
- package/dist/index37.cjs +3 -1
- package/dist/index37.js +210 -52
- package/dist/index38.cjs +1 -2
- package/dist/index38.js +22 -58
- package/dist/index39.cjs +1 -1
- package/dist/index39.js +18 -13
- package/dist/index40.cjs +1 -1
- package/dist/index40.js +10 -37
- package/dist/index41.cjs +1 -1
- package/dist/index41.js +20 -15
- package/dist/index42.cjs +1 -1
- package/dist/index42.js +45 -8
- package/dist/index43.cjs +1 -1
- package/dist/index43.js +12 -19
- package/dist/index44.cjs +1 -1
- package/dist/index44.js +13 -2
- package/dist/index45.cjs +1 -1
- package/dist/index45.js +24 -45
- package/dist/index46.cjs +1 -1
- package/dist/index46.js +27 -31
- package/dist/index47.cjs +1 -1
- package/dist/index47.js +47 -3
- package/dist/index48.cjs +1 -1
- package/dist/index48.js +19 -9
- package/dist/index49.cjs +1 -1
- package/dist/index49.js +10 -67
- package/dist/index50.cjs +1 -1
- package/dist/index50.js +31 -9
- package/dist/index51.cjs +1 -1
- package/dist/index51.js +36 -28
- package/dist/index52.cjs +1 -1
- package/dist/index52.js +7 -71
- package/dist/index53.cjs +1 -1
- package/dist/index53.js +29 -19
- package/dist/index54.cjs +1 -1
- package/dist/index54.js +22 -50
- package/dist/index55.cjs +1 -1
- package/dist/index55.js +7 -7
- package/dist/index56.cjs +1 -1
- package/dist/index56.js +89 -16
- package/dist/index57.cjs +1 -1
- package/dist/index57.js +46 -10
- package/dist/index58.cjs +1 -1
- package/dist/index58.js +23 -5
- package/dist/index59.cjs +1 -1
- package/dist/index59.js +36 -13
- package/dist/index60.cjs +1 -1
- package/dist/index60.js +17 -3
- package/dist/index61.cjs +1 -1
- package/dist/index61.js +6 -26
- package/dist/index62.cjs +1 -1
- package/dist/index62.js +9 -109
- package/dist/index63.cjs +1 -1
- package/dist/index63.js +13 -4
- package/dist/index64.cjs +1 -1
- package/dist/index64.js +109 -6
- package/dist/index65.cjs +1 -1
- package/dist/index65.js +7 -87
- package/dist/index66.cjs +1 -1
- package/dist/index66.js +23 -6
- package/dist/index67.cjs +1 -1
- package/dist/index67.js +6 -124
- package/dist/index68.cjs +1 -1
- package/dist/index68.js +21 -7
- package/dist/index69.cjs +1 -1
- package/dist/index69.js +8 -21
- package/dist/index70.cjs +1 -1
- package/dist/index70.js +31 -17
- package/dist/index71.cjs +1 -1
- package/dist/index71.js +7 -40
- package/dist/index72.cjs +1 -1
- package/dist/index72.js +16 -21
- package/dist/index73.cjs +1 -1
- package/dist/index73.js +72 -10
- package/dist/index74.cjs +1 -1
- package/dist/index74.js +35 -6
- package/dist/index75.cjs +1 -1
- package/dist/index75.js +16 -106
- package/dist/index76.cjs +1 -1
- package/dist/index76.js +66 -3
- package/dist/index77.cjs +1 -1
- package/dist/index77.js +12 -9
- package/dist/index78.cjs +1 -2
- package/dist/index78.js +15 -3
- package/dist/index79.cjs +3 -20
- package/dist/index79.js +71 -302
- package/dist/index80.cjs +1 -1
- package/dist/index80.js +24 -13
- package/dist/index81.cjs +1 -1
- package/dist/index81.js +33 -4
- package/dist/index82.cjs +1 -1
- package/dist/index82.js +45 -2
- package/dist/index83.cjs +1 -1
- package/dist/index83.js +6 -7
- package/dist/index84.cjs +1 -3
- package/dist/index84.js +148 -212
- package/dist/index85.cjs +20 -1
- package/dist/index85.js +327 -32
- package/dist/index86.cjs +3 -1
- package/dist/index86.js +451 -21
- package/dist/index87.cjs +1 -1
- package/dist/index87.js +64 -5
- package/dist/index88.cjs +1 -1
- package/dist/index88.js +36 -72
- package/dist/index89.cjs +1 -1
- package/dist/index89.js +15 -5
- package/dist/index90.cjs +1 -1
- package/dist/index90.js +2 -6
- package/dist/index91.cjs +1 -9
- package/dist/index91.js +16 -35
- package/dist/index92.cjs +1 -3
- package/dist/index92.js +10 -95
- package/dist/index93.cjs +1 -1
- package/dist/index93.js +126 -3
- package/dist/index94.cjs +1 -1
- package/dist/index94.js +5 -23
- package/dist/index95.cjs +1 -3
- package/dist/index95.js +21 -451
- package/dist/index96.cjs +1 -2
- package/dist/index96.js +43 -52
- package/dist/index97.cjs +1 -1
- package/dist/index97.js +35 -17
- package/dist/index98.cjs +1 -1
- package/dist/index98.js +6 -35
- package/dist/index99.cjs +1 -4
- package/dist/index99.js +85 -191
- package/package.json +1 -1
package/dist/index243.js
CHANGED
|
@@ -1,791 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
if (A < L || A >= E || U < L || U >= E)
|
|
15
|
-
throw new Error("splitScalar (endomorphism): failed, k=" + r);
|
|
16
|
-
return { k1neg: D, k1: A, k2neg: X, k2: U };
|
|
17
|
-
}
|
|
18
|
-
function ct(r) {
|
|
19
|
-
if (!["compact", "recovered", "der"].includes(r))
|
|
20
|
-
throw new Error('Signature format must be "compact", "recovered", or "der"');
|
|
21
|
-
return r;
|
|
22
|
-
}
|
|
23
|
-
function it(r, s) {
|
|
24
|
-
const c = {};
|
|
25
|
-
for (let t of Object.keys(s))
|
|
26
|
-
c[t] = r[t] === void 0 ? s[t] : r[t];
|
|
27
|
-
return rt(c.lowS, "lowS"), rt(c.prehash, "prehash"), c.format !== void 0 && ct(c.format), c;
|
|
28
|
-
}
|
|
29
|
-
class Ht extends Error {
|
|
30
|
-
constructor(s = "") {
|
|
31
|
-
super(s);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
const C = {
|
|
35
|
-
// asn.1 DER encoding utils
|
|
36
|
-
Err: Ht,
|
|
37
|
-
// Basic building block is TLV (Tag-Length-Value)
|
|
38
|
-
_tlv: {
|
|
39
|
-
encode: (r, s) => {
|
|
40
|
-
const { Err: c } = C;
|
|
41
|
-
if (r < 0 || r > 256)
|
|
42
|
-
throw new c("tlv.encode: wrong tag");
|
|
43
|
-
if (s.length & 1)
|
|
44
|
-
throw new c("tlv.encode: unpadded data");
|
|
45
|
-
const t = s.length / 2, m = J(t);
|
|
46
|
-
if (m.length / 2 & 128)
|
|
47
|
-
throw new c("tlv.encode: long form length too big");
|
|
48
|
-
const w = t > 127 ? J(m.length / 2 | 128) : "";
|
|
49
|
-
return J(r) + w + m + s;
|
|
50
|
-
},
|
|
51
|
-
// v - value, l - left bytes (unparsed)
|
|
52
|
-
decode(r, s) {
|
|
53
|
-
const { Err: c } = C;
|
|
54
|
-
let t = 0;
|
|
55
|
-
if (r < 0 || r > 256)
|
|
56
|
-
throw new c("tlv.encode: wrong tag");
|
|
57
|
-
if (s.length < 2 || s[t++] !== r)
|
|
58
|
-
throw new c("tlv.decode: wrong tlv");
|
|
59
|
-
const m = s[t++], w = !!(m & 128);
|
|
60
|
-
let l = 0;
|
|
61
|
-
if (!w)
|
|
62
|
-
l = m;
|
|
63
|
-
else {
|
|
64
|
-
const B = m & 127;
|
|
65
|
-
if (!B)
|
|
66
|
-
throw new c("tlv.decode(long): indefinite length not supported");
|
|
67
|
-
if (B > 4)
|
|
68
|
-
throw new c("tlv.decode(long): byte length is too big");
|
|
69
|
-
const A = s.subarray(t, t + B);
|
|
70
|
-
if (A.length !== B)
|
|
71
|
-
throw new c("tlv.decode: length bytes not complete");
|
|
72
|
-
if (A[0] === 0)
|
|
73
|
-
throw new c("tlv.decode(long): zero leftmost byte");
|
|
74
|
-
for (const U of A)
|
|
75
|
-
l = l << 8 | U;
|
|
76
|
-
if (t += B, l < 128)
|
|
77
|
-
throw new c("tlv.decode(long): not minimal encoding");
|
|
78
|
-
}
|
|
79
|
-
const V = s.subarray(t, t + l);
|
|
80
|
-
if (V.length !== l)
|
|
81
|
-
throw new c("tlv.decode: wrong value length");
|
|
82
|
-
return { v: V, l: s.subarray(t + l) };
|
|
83
|
-
}
|
|
84
|
-
},
|
|
85
|
-
// https://crypto.stackexchange.com/a/57734 Leftmost bit of first byte is 'negative' flag,
|
|
86
|
-
// since we always use positive integers here. It must always be empty:
|
|
87
|
-
// - add zero byte if exists
|
|
88
|
-
// - if next byte doesn't have a flag, leading zero is not allowed (minimal encoding)
|
|
89
|
-
_int: {
|
|
90
|
-
encode(r) {
|
|
91
|
-
const { Err: s } = C;
|
|
92
|
-
if (r < L)
|
|
93
|
-
throw new s("integer: negative integers are not allowed");
|
|
94
|
-
let c = J(r);
|
|
95
|
-
if (Number.parseInt(c[0], 16) & 8 && (c = "00" + c), c.length & 1)
|
|
96
|
-
throw new s("unexpected DER parsing assertion: unpadded hex");
|
|
97
|
-
return c;
|
|
98
|
-
},
|
|
99
|
-
decode(r) {
|
|
100
|
-
const { Err: s } = C;
|
|
101
|
-
if (r[0] & 128)
|
|
102
|
-
throw new s("invalid signature integer: negative");
|
|
103
|
-
if (r[0] === 0 && !(r[1] & 128))
|
|
104
|
-
throw new s("invalid signature integer: unnecessary leading zero");
|
|
105
|
-
return wt(r);
|
|
106
|
-
}
|
|
107
|
-
},
|
|
108
|
-
toSig(r) {
|
|
109
|
-
const { Err: s, _int: c, _tlv: t } = C, m = N("signature", r), { v: w, l } = t.decode(48, m);
|
|
110
|
-
if (l.length)
|
|
111
|
-
throw new s("invalid signature: left bytes after parsing");
|
|
112
|
-
const { v: V, l: B } = t.decode(2, w), { v: A, l: U } = t.decode(2, B);
|
|
113
|
-
if (U.length)
|
|
114
|
-
throw new s("invalid signature: left bytes after parsing");
|
|
115
|
-
return { r: c.decode(V), s: c.decode(A) };
|
|
116
|
-
},
|
|
117
|
-
hexFromSig(r) {
|
|
118
|
-
const { _tlv: s, _int: c } = C, t = s.encode(2, c.encode(r.r)), m = s.encode(2, c.encode(r.s)), w = t + m;
|
|
119
|
-
return s.encode(48, w);
|
|
120
|
-
}
|
|
121
|
-
}, L = BigInt(0), $ = BigInt(1), gt = BigInt(2), tt = BigInt(3), _t = BigInt(4);
|
|
122
|
-
function M(r, s) {
|
|
123
|
-
const { BYTES: c } = r;
|
|
124
|
-
let t;
|
|
125
|
-
if (typeof s == "bigint")
|
|
126
|
-
t = s;
|
|
127
|
-
else {
|
|
128
|
-
let m = N("private key", s);
|
|
129
|
-
try {
|
|
130
|
-
t = r.fromBytes(m);
|
|
131
|
-
} catch {
|
|
132
|
-
throw new Error(`invalid private key: expected ui8a of size ${c}, got ${typeof s}`);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
if (!r.isValidNot0(t))
|
|
136
|
-
throw new Error("invalid private key: out of range [1..N-1]");
|
|
137
|
-
return t;
|
|
138
|
-
}
|
|
139
|
-
function Ct(r, s = {}) {
|
|
140
|
-
const c = Zt("weierstrass", r, s), { Fp: t, Fn: m } = c;
|
|
141
|
-
let w = c.CURVE;
|
|
142
|
-
const { h: l, n: V } = w;
|
|
143
|
-
mt(s, {}, {
|
|
144
|
-
allowInfinityPoint: "boolean",
|
|
145
|
-
clearCofactor: "function",
|
|
146
|
-
isTorsionFree: "function",
|
|
147
|
-
fromBytes: "function",
|
|
148
|
-
toBytes: "function",
|
|
149
|
-
endo: "object",
|
|
150
|
-
wrapPrivateKey: "boolean"
|
|
151
|
-
});
|
|
152
|
-
const { endo: B } = s;
|
|
153
|
-
if (B && (!t.is0(w.a) || typeof B.beta != "bigint" || !Array.isArray(B.basises)))
|
|
154
|
-
throw new Error('invalid endo: expected "beta": bigint and "basises": array');
|
|
155
|
-
const A = bt(t, m);
|
|
156
|
-
function U() {
|
|
157
|
-
if (!t.isOdd)
|
|
158
|
-
throw new Error("compression is not supported: Field does not have .isOdd()");
|
|
159
|
-
}
|
|
160
|
-
function D(b, o, n) {
|
|
161
|
-
const { x: e, y: i } = o.toAffine(), a = t.toBytes(e);
|
|
162
|
-
if (rt(n, "isCompressed"), n) {
|
|
163
|
-
U();
|
|
164
|
-
const f = !t.isOdd(i);
|
|
165
|
-
return I(Et(f), a);
|
|
166
|
-
} else
|
|
167
|
-
return I(Uint8Array.of(4), a, t.toBytes(i));
|
|
168
|
-
}
|
|
169
|
-
function X(b) {
|
|
170
|
-
k(b, void 0, "Point");
|
|
171
|
-
const { publicKey: o, publicKeyUncompressed: n } = A, e = b.length, i = b[0], a = b.subarray(1);
|
|
172
|
-
if (e === o && (i === 2 || i === 3)) {
|
|
173
|
-
const f = t.fromBytes(a);
|
|
174
|
-
if (!t.isValid(f))
|
|
175
|
-
throw new Error("bad point: is not on curve, wrong x");
|
|
176
|
-
const d = T(f);
|
|
177
|
-
let u;
|
|
178
|
-
try {
|
|
179
|
-
u = t.sqrt(d);
|
|
180
|
-
} catch (O) {
|
|
181
|
-
const v = O instanceof Error ? ": " + O.message : "";
|
|
182
|
-
throw new Error("bad point: is not on curve, sqrt error" + v);
|
|
183
|
-
}
|
|
184
|
-
U();
|
|
185
|
-
const h = t.isOdd(u);
|
|
186
|
-
return (i & 1) === 1 !== h && (u = t.neg(u)), { x: f, y: u };
|
|
187
|
-
} else if (e === n && i === 4) {
|
|
188
|
-
const f = t.BYTES, d = t.fromBytes(a.subarray(0, f)), u = t.fromBytes(a.subarray(f, f * 2));
|
|
189
|
-
if (!H(d, u))
|
|
190
|
-
throw new Error("bad point: is not on curve");
|
|
191
|
-
return { x: d, y: u };
|
|
192
|
-
} else
|
|
193
|
-
throw new Error(`bad point: got length ${e}, expected compressed=${o} or uncompressed=${n}`);
|
|
194
|
-
}
|
|
195
|
-
const E = s.toBytes || D, x = s.fromBytes || X;
|
|
196
|
-
function T(b) {
|
|
197
|
-
const o = t.sqr(b), n = t.mul(o, b);
|
|
198
|
-
return t.add(t.add(n, t.mul(b, w.a)), w.b);
|
|
199
|
-
}
|
|
200
|
-
function H(b, o) {
|
|
201
|
-
const n = t.sqr(o), e = T(b);
|
|
202
|
-
return t.eql(n, e);
|
|
203
|
-
}
|
|
204
|
-
if (!H(w.Gx, w.Gy))
|
|
205
|
-
throw new Error("bad curve params: generator point");
|
|
206
|
-
const _ = t.mul(t.pow(w.a, tt), _t), nt = t.mul(t.sqr(w.b), BigInt(27));
|
|
207
|
-
if (t.is0(t.add(_, nt)))
|
|
208
|
-
throw new Error("bad curve params: a or b");
|
|
209
|
-
function F(b, o, n = !1) {
|
|
210
|
-
if (!t.isValid(o) || n && t.is0(o))
|
|
211
|
-
throw new Error(`bad point coordinate ${b}`);
|
|
212
|
-
return o;
|
|
213
|
-
}
|
|
214
|
-
function G(b) {
|
|
215
|
-
if (!(b instanceof p))
|
|
216
|
-
throw new Error("ProjectivePoint expected");
|
|
217
|
-
}
|
|
218
|
-
function j(b) {
|
|
219
|
-
if (!B || !B.basises)
|
|
220
|
-
throw new Error("no endo");
|
|
221
|
-
return Tt(b, B.basises, m.ORDER);
|
|
222
|
-
}
|
|
223
|
-
const ot = dt((b, o) => {
|
|
224
|
-
const { X: n, Y: e, Z: i } = b;
|
|
225
|
-
if (t.eql(i, t.ONE))
|
|
226
|
-
return { x: n, y: e };
|
|
227
|
-
const a = b.is0();
|
|
228
|
-
o == null && (o = a ? t.ONE : t.inv(i));
|
|
229
|
-
const f = t.mul(n, o), d = t.mul(e, o), u = t.mul(i, o);
|
|
230
|
-
if (a)
|
|
231
|
-
return { x: t.ZERO, y: t.ZERO };
|
|
232
|
-
if (!t.eql(u, t.ONE))
|
|
233
|
-
throw new Error("invZ was invalid");
|
|
234
|
-
return { x: f, y: d };
|
|
235
|
-
}), P = dt((b) => {
|
|
236
|
-
if (b.is0()) {
|
|
237
|
-
if (s.allowInfinityPoint && !t.is0(b.Y))
|
|
238
|
-
return;
|
|
239
|
-
throw new Error("bad point: ZERO");
|
|
240
|
-
}
|
|
241
|
-
const { x: o, y: n } = b.toAffine();
|
|
242
|
-
if (!t.isValid(o) || !t.isValid(n))
|
|
243
|
-
throw new Error("bad point: x or y not field elements");
|
|
244
|
-
if (!H(o, n))
|
|
245
|
-
throw new Error("bad point: equation left != right");
|
|
246
|
-
if (!b.isTorsionFree())
|
|
247
|
-
throw new Error("bad point: not in prime-order subgroup");
|
|
248
|
-
return !0;
|
|
249
|
-
});
|
|
250
|
-
function z(b, o, n, e, i) {
|
|
251
|
-
return n = new p(t.mul(n.X, b), n.Y, n.Z), o = ft(e, o), n = ft(i, n), o.add(n);
|
|
252
|
-
}
|
|
253
|
-
class p {
|
|
254
|
-
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
255
|
-
constructor(o, n, e) {
|
|
256
|
-
this.X = F("x", o), this.Y = F("y", n, !0), this.Z = F("z", e), Object.freeze(this);
|
|
257
|
-
}
|
|
258
|
-
static CURVE() {
|
|
259
|
-
return w;
|
|
260
|
-
}
|
|
261
|
-
/** Does NOT validate if the point is valid. Use `.assertValidity()`. */
|
|
262
|
-
static fromAffine(o) {
|
|
263
|
-
const { x: n, y: e } = o || {};
|
|
264
|
-
if (!o || !t.isValid(n) || !t.isValid(e))
|
|
265
|
-
throw new Error("invalid affine point");
|
|
266
|
-
if (o instanceof p)
|
|
267
|
-
throw new Error("projective point not allowed");
|
|
268
|
-
return t.is0(n) && t.is0(e) ? p.ZERO : new p(n, e, t.ONE);
|
|
269
|
-
}
|
|
270
|
-
static fromBytes(o) {
|
|
271
|
-
const n = p.fromAffine(x(k(o, void 0, "point")));
|
|
272
|
-
return n.assertValidity(), n;
|
|
273
|
-
}
|
|
274
|
-
static fromHex(o) {
|
|
275
|
-
return p.fromBytes(N("pointHex", o));
|
|
276
|
-
}
|
|
277
|
-
get x() {
|
|
278
|
-
return this.toAffine().x;
|
|
279
|
-
}
|
|
280
|
-
get y() {
|
|
281
|
-
return this.toAffine().y;
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
*
|
|
285
|
-
* @param windowSize
|
|
286
|
-
* @param isLazy true will defer table computation until the first multiplication
|
|
287
|
-
* @returns
|
|
288
|
-
*/
|
|
289
|
-
precompute(o = 8, n = !0) {
|
|
290
|
-
return q.createCache(this, o), n || this.multiply(tt), this;
|
|
291
|
-
}
|
|
292
|
-
// TODO: return `this`
|
|
293
|
-
/** A point on curve is valid if it conforms to equation. */
|
|
294
|
-
assertValidity() {
|
|
295
|
-
P(this);
|
|
296
|
-
}
|
|
297
|
-
hasEvenY() {
|
|
298
|
-
const { y: o } = this.toAffine();
|
|
299
|
-
if (!t.isOdd)
|
|
300
|
-
throw new Error("Field doesn't support isOdd");
|
|
301
|
-
return !t.isOdd(o);
|
|
302
|
-
}
|
|
303
|
-
/** Compare one point to another. */
|
|
304
|
-
equals(o) {
|
|
305
|
-
G(o);
|
|
306
|
-
const { X: n, Y: e, Z: i } = this, { X: a, Y: f, Z: d } = o, u = t.eql(t.mul(n, d), t.mul(a, i)), h = t.eql(t.mul(e, d), t.mul(f, i));
|
|
307
|
-
return u && h;
|
|
308
|
-
}
|
|
309
|
-
/** Flips point to one corresponding to (x, -y) in Affine coordinates. */
|
|
310
|
-
negate() {
|
|
311
|
-
return new p(this.X, t.neg(this.Y), this.Z);
|
|
312
|
-
}
|
|
313
|
-
// Renes-Costello-Batina exception-free doubling formula.
|
|
314
|
-
// There is 30% faster Jacobian formula, but it is not complete.
|
|
315
|
-
// https://eprint.iacr.org/2015/1060, algorithm 3
|
|
316
|
-
// Cost: 8M + 3S + 3*a + 2*b3 + 15add.
|
|
317
|
-
double() {
|
|
318
|
-
const { a: o, b: n } = w, e = t.mul(n, tt), { X: i, Y: a, Z: f } = this;
|
|
319
|
-
let d = t.ZERO, u = t.ZERO, h = t.ZERO, y = t.mul(i, i), O = t.mul(a, a), v = t.mul(f, f), g = t.mul(i, a);
|
|
320
|
-
return g = t.add(g, g), h = t.mul(i, f), h = t.add(h, h), d = t.mul(o, h), u = t.mul(e, v), u = t.add(d, u), d = t.sub(O, u), u = t.add(O, u), u = t.mul(d, u), d = t.mul(g, d), h = t.mul(e, h), v = t.mul(o, v), g = t.sub(y, v), g = t.mul(o, g), g = t.add(g, h), h = t.add(y, y), y = t.add(h, y), y = t.add(y, v), y = t.mul(y, g), u = t.add(u, y), v = t.mul(a, f), v = t.add(v, v), y = t.mul(v, g), d = t.sub(d, y), h = t.mul(v, O), h = t.add(h, h), h = t.add(h, h), new p(d, u, h);
|
|
321
|
-
}
|
|
322
|
-
// Renes-Costello-Batina exception-free addition formula.
|
|
323
|
-
// There is 30% faster Jacobian formula, but it is not complete.
|
|
324
|
-
// https://eprint.iacr.org/2015/1060, algorithm 1
|
|
325
|
-
// Cost: 12M + 0S + 3*a + 3*b3 + 23add.
|
|
326
|
-
add(o) {
|
|
327
|
-
G(o);
|
|
328
|
-
const { X: n, Y: e, Z: i } = this, { X: a, Y: f, Z: d } = o;
|
|
329
|
-
let u = t.ZERO, h = t.ZERO, y = t.ZERO;
|
|
330
|
-
const O = w.a, v = t.mul(w.b, tt);
|
|
331
|
-
let g = t.mul(n, a), R = t.mul(e, f), Z = t.mul(i, d), K = t.add(n, e), S = t.add(a, f);
|
|
332
|
-
K = t.mul(K, S), S = t.add(g, R), K = t.sub(K, S), S = t.add(n, i);
|
|
333
|
-
let Y = t.add(a, d);
|
|
334
|
-
return S = t.mul(S, Y), Y = t.add(g, Z), S = t.sub(S, Y), Y = t.add(e, i), u = t.add(f, d), Y = t.mul(Y, u), u = t.add(R, Z), Y = t.sub(Y, u), y = t.mul(O, S), u = t.mul(v, Z), y = t.add(u, y), u = t.sub(R, y), y = t.add(R, y), h = t.mul(u, y), R = t.add(g, g), R = t.add(R, g), Z = t.mul(O, Z), S = t.mul(v, S), R = t.add(R, Z), Z = t.sub(g, Z), Z = t.mul(O, Z), S = t.add(S, Z), g = t.mul(R, S), h = t.add(h, g), g = t.mul(Y, S), u = t.mul(K, u), u = t.sub(u, g), g = t.mul(K, R), y = t.mul(Y, y), y = t.add(y, g), new p(u, h, y);
|
|
335
|
-
}
|
|
336
|
-
subtract(o) {
|
|
337
|
-
return this.add(o.negate());
|
|
338
|
-
}
|
|
339
|
-
is0() {
|
|
340
|
-
return this.equals(p.ZERO);
|
|
341
|
-
}
|
|
342
|
-
/**
|
|
343
|
-
* Constant time multiplication.
|
|
344
|
-
* Uses wNAF method. Windowed method may be 10% faster,
|
|
345
|
-
* but takes 2x longer to generate and consumes 2x memory.
|
|
346
|
-
* Uses precomputes when available.
|
|
347
|
-
* Uses endomorphism for Koblitz curves.
|
|
348
|
-
* @param scalar by which the point would be multiplied
|
|
349
|
-
* @returns New point
|
|
350
|
-
*/
|
|
351
|
-
multiply(o) {
|
|
352
|
-
const { endo: n } = s;
|
|
353
|
-
if (!m.isValidNot0(o))
|
|
354
|
-
throw new Error("invalid scalar: out of range");
|
|
355
|
-
let e, i;
|
|
356
|
-
const a = (f) => q.cached(this, f, (d) => st(p, d));
|
|
357
|
-
if (n) {
|
|
358
|
-
const { k1neg: f, k1: d, k2neg: u, k2: h } = j(o), { p: y, f: O } = a(d), { p: v, f: g } = a(h);
|
|
359
|
-
i = O.add(g), e = z(n.beta, y, v, f, u);
|
|
360
|
-
} else {
|
|
361
|
-
const { p: f, f: d } = a(o);
|
|
362
|
-
e = f, i = d;
|
|
363
|
-
}
|
|
364
|
-
return st(p, [e, i])[0];
|
|
365
|
-
}
|
|
366
|
-
/**
|
|
367
|
-
* Non-constant-time multiplication. Uses double-and-add algorithm.
|
|
368
|
-
* It's faster, but should only be used when you don't care about
|
|
369
|
-
* an exposed secret key e.g. sig verification, which works over *public* keys.
|
|
370
|
-
*/
|
|
371
|
-
multiplyUnsafe(o) {
|
|
372
|
-
const { endo: n } = s, e = this;
|
|
373
|
-
if (!m.isValid(o))
|
|
374
|
-
throw new Error("invalid scalar: out of range");
|
|
375
|
-
if (o === L || e.is0())
|
|
376
|
-
return p.ZERO;
|
|
377
|
-
if (o === $)
|
|
378
|
-
return e;
|
|
379
|
-
if (q.hasCache(this))
|
|
380
|
-
return this.multiply(o);
|
|
381
|
-
if (n) {
|
|
382
|
-
const { k1neg: i, k1: a, k2neg: f, k2: d } = j(o), { p1: u, p2: h } = Ut(p, e, a, d);
|
|
383
|
-
return z(n.beta, u, h, i, f);
|
|
384
|
-
} else
|
|
385
|
-
return q.unsafe(e, o);
|
|
386
|
-
}
|
|
387
|
-
multiplyAndAddUnsafe(o, n, e) {
|
|
388
|
-
const i = this.multiplyUnsafe(n).add(o.multiplyUnsafe(e));
|
|
389
|
-
return i.is0() ? void 0 : i;
|
|
390
|
-
}
|
|
391
|
-
/**
|
|
392
|
-
* Converts Projective point to affine (x, y) coordinates.
|
|
393
|
-
* @param invertedZ Z^-1 (inverted zero) - optional, precomputation is useful for invertBatch
|
|
394
|
-
*/
|
|
395
|
-
toAffine(o) {
|
|
396
|
-
return ot(this, o);
|
|
397
|
-
}
|
|
398
|
-
/**
|
|
399
|
-
* Checks whether Point is free of torsion elements (is in prime subgroup).
|
|
400
|
-
* Always torsion-free for cofactor=1 curves.
|
|
401
|
-
*/
|
|
402
|
-
isTorsionFree() {
|
|
403
|
-
const { isTorsionFree: o } = s;
|
|
404
|
-
return l === $ ? !0 : o ? o(p, this) : q.unsafe(this, V).is0();
|
|
405
|
-
}
|
|
406
|
-
clearCofactor() {
|
|
407
|
-
const { clearCofactor: o } = s;
|
|
408
|
-
return l === $ ? this : o ? o(p, this) : this.multiplyUnsafe(l);
|
|
409
|
-
}
|
|
410
|
-
isSmallOrder() {
|
|
411
|
-
return this.multiplyUnsafe(l).is0();
|
|
412
|
-
}
|
|
413
|
-
toBytes(o = !0) {
|
|
414
|
-
return rt(o, "isCompressed"), this.assertValidity(), E(p, this, o);
|
|
415
|
-
}
|
|
416
|
-
toHex(o = !0) {
|
|
417
|
-
return et(this.toBytes(o));
|
|
418
|
-
}
|
|
419
|
-
toString() {
|
|
420
|
-
return `<Point ${this.is0() ? "ZERO" : this.toHex()}>`;
|
|
421
|
-
}
|
|
422
|
-
// TODO: remove
|
|
423
|
-
get px() {
|
|
424
|
-
return this.X;
|
|
425
|
-
}
|
|
426
|
-
get py() {
|
|
427
|
-
return this.X;
|
|
428
|
-
}
|
|
429
|
-
get pz() {
|
|
430
|
-
return this.Z;
|
|
431
|
-
}
|
|
432
|
-
toRawBytes(o = !0) {
|
|
433
|
-
return this.toBytes(o);
|
|
434
|
-
}
|
|
435
|
-
_setWindowSize(o) {
|
|
436
|
-
this.precompute(o);
|
|
437
|
-
}
|
|
438
|
-
static normalizeZ(o) {
|
|
439
|
-
return st(p, o);
|
|
440
|
-
}
|
|
441
|
-
static msm(o, n) {
|
|
442
|
-
return Ft(p, m, o, n);
|
|
443
|
-
}
|
|
444
|
-
static fromPrivateKey(o) {
|
|
445
|
-
return p.BASE.multiply(M(m, o));
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
p.BASE = new p(w.Gx, w.Gy, t.ONE), p.ZERO = new p(t.ZERO, t.ONE, t.ZERO), p.Fp = t, p.Fn = m;
|
|
449
|
-
const W = m.BITS, q = new At(p, s.endo ? Math.ceil(W / 2) : W);
|
|
450
|
-
return p.BASE.precompute(8), p;
|
|
451
|
-
}
|
|
452
|
-
function Et(r) {
|
|
453
|
-
return Uint8Array.of(r ? 2 : 3);
|
|
454
|
-
}
|
|
455
|
-
function bt(r, s) {
|
|
456
|
-
return {
|
|
457
|
-
secretKey: s.BYTES,
|
|
458
|
-
publicKey: 1 + r.BYTES,
|
|
459
|
-
publicKeyUncompressed: 1 + 2 * r.BYTES,
|
|
460
|
-
publicKeyHasPrefix: !0,
|
|
461
|
-
signature: 2 * s.BYTES
|
|
462
|
-
};
|
|
463
|
-
}
|
|
464
|
-
function Lt(r, s = {}) {
|
|
465
|
-
const { Fn: c } = r, t = s.randomBytes || yt, m = Object.assign(bt(r.Fp, c), { seed: Kt(c.ORDER) });
|
|
466
|
-
function w(E) {
|
|
467
|
-
try {
|
|
468
|
-
return !!M(c, E);
|
|
469
|
-
} catch {
|
|
470
|
-
return !1;
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
function l(E, x) {
|
|
474
|
-
const { publicKey: T, publicKeyUncompressed: H } = m;
|
|
475
|
-
try {
|
|
476
|
-
const _ = E.length;
|
|
477
|
-
return x === !0 && _ !== T || x === !1 && _ !== H ? !1 : !!r.fromBytes(E);
|
|
478
|
-
} catch {
|
|
479
|
-
return !1;
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
function V(E = t(m.seed)) {
|
|
483
|
-
return Nt(k(E, m.seed, "seed"), c.ORDER);
|
|
484
|
-
}
|
|
485
|
-
function B(E, x = !0) {
|
|
486
|
-
return r.BASE.multiply(M(c, E)).toBytes(x);
|
|
487
|
-
}
|
|
488
|
-
function A(E) {
|
|
489
|
-
const x = V(E);
|
|
490
|
-
return { secretKey: x, publicKey: B(x) };
|
|
491
|
-
}
|
|
492
|
-
function U(E) {
|
|
493
|
-
if (typeof E == "bigint")
|
|
494
|
-
return !1;
|
|
495
|
-
if (E instanceof r)
|
|
496
|
-
return !0;
|
|
497
|
-
const { secretKey: x, publicKey: T, publicKeyUncompressed: H } = m;
|
|
498
|
-
if (c.allowedLengths || x === T)
|
|
499
|
-
return;
|
|
500
|
-
const _ = N("key", E).length;
|
|
501
|
-
return _ === T || _ === H;
|
|
502
|
-
}
|
|
503
|
-
function D(E, x, T = !0) {
|
|
504
|
-
if (U(E) === !0)
|
|
505
|
-
throw new Error("first arg must be private key");
|
|
506
|
-
if (U(x) === !1)
|
|
507
|
-
throw new Error("second arg must be public key");
|
|
508
|
-
const H = M(c, E);
|
|
509
|
-
return r.fromHex(x).multiply(H).toBytes(T);
|
|
510
|
-
}
|
|
511
|
-
return Object.freeze({ getPublicKey: B, getSharedSecret: D, keygen: A, Point: r, utils: {
|
|
512
|
-
isValidSecretKey: w,
|
|
513
|
-
isValidPublicKey: l,
|
|
514
|
-
randomSecretKey: V,
|
|
515
|
-
// TODO: remove
|
|
516
|
-
isValidPrivateKey: w,
|
|
517
|
-
randomPrivateKey: V,
|
|
518
|
-
normPrivateKeyToScalar: (E) => M(c, E),
|
|
519
|
-
precompute(E = 8, x = r.BASE) {
|
|
520
|
-
return x.precompute(E, !1);
|
|
521
|
-
}
|
|
522
|
-
}, lengths: m });
|
|
523
|
-
}
|
|
524
|
-
function Dt(r, s, c = {}) {
|
|
525
|
-
Bt(s), mt(c, {}, {
|
|
526
|
-
hmac: "function",
|
|
527
|
-
lowS: "boolean",
|
|
528
|
-
randomBytes: "function",
|
|
529
|
-
bits2int: "function",
|
|
530
|
-
bits2int_modN: "function"
|
|
531
|
-
});
|
|
532
|
-
const t = c.randomBytes || yt, m = c.hmac || ((n, ...e) => vt(s, n, I(...e))), { Fp: w, Fn: l } = r, { ORDER: V, BITS: B } = l, { keygen: A, getPublicKey: U, getSharedSecret: D, utils: X, lengths: E } = Lt(r, c), x = {
|
|
533
|
-
prehash: !1,
|
|
534
|
-
lowS: typeof c.lowS == "boolean" ? c.lowS : !1,
|
|
535
|
-
format: void 0,
|
|
536
|
-
//'compact' as ECDSASigFormat,
|
|
537
|
-
extraEntropy: !1
|
|
538
|
-
}, T = "compact";
|
|
539
|
-
function H(n) {
|
|
540
|
-
const e = V >> $;
|
|
541
|
-
return n > e;
|
|
542
|
-
}
|
|
543
|
-
function _(n, e) {
|
|
544
|
-
if (!l.isValidNot0(e))
|
|
545
|
-
throw new Error(`invalid signature ${n}: out of range 1..Point.Fn.ORDER`);
|
|
546
|
-
return e;
|
|
547
|
-
}
|
|
548
|
-
function nt(n, e) {
|
|
549
|
-
ct(e);
|
|
550
|
-
const i = E.signature, a = e === "compact" ? i : e === "recovered" ? i + 1 : void 0;
|
|
551
|
-
return k(n, a, `${e} signature`);
|
|
552
|
-
}
|
|
553
|
-
class F {
|
|
554
|
-
constructor(e, i, a) {
|
|
555
|
-
this.r = _("r", e), this.s = _("s", i), a != null && (this.recovery = a), Object.freeze(this);
|
|
556
|
-
}
|
|
557
|
-
static fromBytes(e, i = T) {
|
|
558
|
-
nt(e, i);
|
|
559
|
-
let a;
|
|
560
|
-
if (i === "der") {
|
|
561
|
-
const { r: h, s: y } = C.toSig(k(e));
|
|
562
|
-
return new F(h, y);
|
|
563
|
-
}
|
|
564
|
-
i === "recovered" && (a = e[0], i = "compact", e = e.subarray(1));
|
|
565
|
-
const f = l.BYTES, d = e.subarray(0, f), u = e.subarray(f, f * 2);
|
|
566
|
-
return new F(l.fromBytes(d), l.fromBytes(u), a);
|
|
567
|
-
}
|
|
568
|
-
static fromHex(e, i) {
|
|
569
|
-
return this.fromBytes(lt(e), i);
|
|
570
|
-
}
|
|
571
|
-
addRecoveryBit(e) {
|
|
572
|
-
return new F(this.r, this.s, e);
|
|
573
|
-
}
|
|
574
|
-
recoverPublicKey(e) {
|
|
575
|
-
const i = w.ORDER, { r: a, s: f, recovery: d } = this;
|
|
576
|
-
if (d == null || ![0, 1, 2, 3].includes(d))
|
|
577
|
-
throw new Error("recovery id invalid");
|
|
578
|
-
if (V * gt < i && d > 1)
|
|
579
|
-
throw new Error("recovery id is ambiguous for h>1 curve");
|
|
580
|
-
const h = d === 2 || d === 3 ? a + V : a;
|
|
581
|
-
if (!w.isValid(h))
|
|
582
|
-
throw new Error("recovery id 2 or 3 invalid");
|
|
583
|
-
const y = w.toBytes(h), O = r.fromBytes(I(Et((d & 1) === 0), y)), v = l.inv(h), g = j(N("msgHash", e)), R = l.create(-g * v), Z = l.create(f * v), K = r.BASE.multiplyUnsafe(R).add(O.multiplyUnsafe(Z));
|
|
584
|
-
if (K.is0())
|
|
585
|
-
throw new Error("point at infinify");
|
|
586
|
-
return K.assertValidity(), K;
|
|
587
|
-
}
|
|
588
|
-
// Signatures should be low-s, to prevent malleability.
|
|
589
|
-
hasHighS() {
|
|
590
|
-
return H(this.s);
|
|
591
|
-
}
|
|
592
|
-
toBytes(e = T) {
|
|
593
|
-
if (ct(e), e === "der")
|
|
594
|
-
return lt(C.hexFromSig(this));
|
|
595
|
-
const i = l.toBytes(this.r), a = l.toBytes(this.s);
|
|
596
|
-
if (e === "recovered") {
|
|
597
|
-
if (this.recovery == null)
|
|
598
|
-
throw new Error("recovery bit must be present");
|
|
599
|
-
return I(Uint8Array.of(this.recovery), i, a);
|
|
600
|
-
}
|
|
601
|
-
return I(i, a);
|
|
602
|
-
}
|
|
603
|
-
toHex(e) {
|
|
604
|
-
return et(this.toBytes(e));
|
|
605
|
-
}
|
|
606
|
-
// TODO: remove
|
|
607
|
-
assertValidity() {
|
|
608
|
-
}
|
|
609
|
-
static fromCompact(e) {
|
|
610
|
-
return F.fromBytes(N("sig", e), "compact");
|
|
611
|
-
}
|
|
612
|
-
static fromDER(e) {
|
|
613
|
-
return F.fromBytes(N("sig", e), "der");
|
|
614
|
-
}
|
|
615
|
-
normalizeS() {
|
|
616
|
-
return this.hasHighS() ? new F(this.r, l.neg(this.s), this.recovery) : this;
|
|
617
|
-
}
|
|
618
|
-
toDERRawBytes() {
|
|
619
|
-
return this.toBytes("der");
|
|
620
|
-
}
|
|
621
|
-
toDERHex() {
|
|
622
|
-
return et(this.toBytes("der"));
|
|
623
|
-
}
|
|
624
|
-
toCompactRawBytes() {
|
|
625
|
-
return this.toBytes("compact");
|
|
626
|
-
}
|
|
627
|
-
toCompactHex() {
|
|
628
|
-
return et(this.toBytes("compact"));
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
const G = c.bits2int || function(e) {
|
|
632
|
-
if (e.length > 8192)
|
|
633
|
-
throw new Error("input is too large");
|
|
634
|
-
const i = wt(e), a = e.length * 8 - B;
|
|
635
|
-
return a > 0 ? i >> BigInt(a) : i;
|
|
636
|
-
}, j = c.bits2int_modN || function(e) {
|
|
637
|
-
return l.create(G(e));
|
|
638
|
-
}, ot = pt(B);
|
|
639
|
-
function P(n) {
|
|
640
|
-
return Ot("num < 2^" + B, n, L, ot), l.toBytes(n);
|
|
641
|
-
}
|
|
642
|
-
function z(n, e) {
|
|
643
|
-
return k(n, void 0, "message"), e ? k(s(n), void 0, "prehashed message") : n;
|
|
644
|
-
}
|
|
645
|
-
function p(n, e, i) {
|
|
646
|
-
if (["recovered", "canonical"].some((R) => R in i))
|
|
647
|
-
throw new Error("sign() legacy options not supported");
|
|
648
|
-
const { lowS: a, prehash: f, extraEntropy: d } = it(i, x);
|
|
649
|
-
n = z(n, f);
|
|
650
|
-
const u = j(n), h = M(l, e), y = [P(h), P(u)];
|
|
651
|
-
if (d != null && d !== !1) {
|
|
652
|
-
const R = d === !0 ? t(E.secretKey) : d;
|
|
653
|
-
y.push(N("extraEntropy", R));
|
|
654
|
-
}
|
|
655
|
-
const O = I(...y), v = u;
|
|
656
|
-
function g(R) {
|
|
657
|
-
const Z = G(R);
|
|
658
|
-
if (!l.isValidNot0(Z))
|
|
659
|
-
return;
|
|
660
|
-
const K = l.inv(Z), S = r.BASE.multiply(Z).toAffine(), Y = l.create(S.x);
|
|
661
|
-
if (Y === L)
|
|
662
|
-
return;
|
|
663
|
-
const Q = l.create(K * l.create(v + Y * h));
|
|
664
|
-
if (Q === L)
|
|
665
|
-
return;
|
|
666
|
-
let at = (S.x === Y ? 0 : 2) | Number(S.y & $), ut = Q;
|
|
667
|
-
return a && H(Q) && (ut = l.neg(Q), at ^= 1), new F(Y, ut, at);
|
|
668
|
-
}
|
|
669
|
-
return { seed: O, k2sig: g };
|
|
670
|
-
}
|
|
671
|
-
function W(n, e, i = {}) {
|
|
672
|
-
n = N("message", n);
|
|
673
|
-
const { seed: a, k2sig: f } = p(n, e, i);
|
|
674
|
-
return St(s.outputLen, l.BYTES, m)(a, f);
|
|
675
|
-
}
|
|
676
|
-
function q(n) {
|
|
677
|
-
let e;
|
|
678
|
-
const i = typeof n == "string" || Rt(n), a = !i && n !== null && typeof n == "object" && typeof n.r == "bigint" && typeof n.s == "bigint";
|
|
679
|
-
if (!i && !a)
|
|
680
|
-
throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");
|
|
681
|
-
if (a)
|
|
682
|
-
e = new F(n.r, n.s);
|
|
683
|
-
else if (i) {
|
|
684
|
-
try {
|
|
685
|
-
e = F.fromBytes(N("sig", n), "der");
|
|
686
|
-
} catch (f) {
|
|
687
|
-
if (!(f instanceof C.Err))
|
|
688
|
-
throw f;
|
|
689
|
-
}
|
|
690
|
-
if (!e)
|
|
691
|
-
try {
|
|
692
|
-
e = F.fromBytes(N("sig", n), "compact");
|
|
693
|
-
} catch {
|
|
694
|
-
return !1;
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
return e || !1;
|
|
698
|
-
}
|
|
699
|
-
function b(n, e, i, a = {}) {
|
|
700
|
-
const { lowS: f, prehash: d, format: u } = it(a, x);
|
|
701
|
-
if (i = N("publicKey", i), e = z(N("message", e), d), "strict" in a)
|
|
702
|
-
throw new Error("options.strict was renamed to lowS");
|
|
703
|
-
const h = u === void 0 ? q(n) : F.fromBytes(N("sig", n), u);
|
|
704
|
-
if (h === !1)
|
|
705
|
-
return !1;
|
|
706
|
-
try {
|
|
707
|
-
const y = r.fromBytes(i);
|
|
708
|
-
if (f && h.hasHighS())
|
|
709
|
-
return !1;
|
|
710
|
-
const { r: O, s: v } = h, g = j(e), R = l.inv(v), Z = l.create(g * R), K = l.create(O * R), S = r.BASE.multiplyUnsafe(Z).add(y.multiplyUnsafe(K));
|
|
711
|
-
return S.is0() ? !1 : l.create(S.x) === O;
|
|
712
|
-
} catch {
|
|
713
|
-
return !1;
|
|
714
|
-
}
|
|
715
|
-
}
|
|
716
|
-
function o(n, e, i = {}) {
|
|
717
|
-
const { prehash: a } = it(i, x);
|
|
718
|
-
return e = z(e, a), F.fromBytes(n, "recovered").recoverPublicKey(e).toBytes();
|
|
719
|
-
}
|
|
720
|
-
return Object.freeze({
|
|
721
|
-
keygen: A,
|
|
722
|
-
getPublicKey: U,
|
|
723
|
-
getSharedSecret: D,
|
|
724
|
-
utils: X,
|
|
725
|
-
lengths: E,
|
|
726
|
-
Point: r,
|
|
727
|
-
sign: W,
|
|
728
|
-
verify: b,
|
|
729
|
-
recoverPublicKey: o,
|
|
730
|
-
Signature: F,
|
|
731
|
-
hash: s
|
|
732
|
-
});
|
|
733
|
-
}
|
|
734
|
-
function Xt(r) {
|
|
735
|
-
const s = {
|
|
736
|
-
a: r.a,
|
|
737
|
-
b: r.b,
|
|
738
|
-
p: r.Fp.ORDER,
|
|
739
|
-
n: r.n,
|
|
740
|
-
h: r.h,
|
|
741
|
-
Gx: r.Gx,
|
|
742
|
-
Gy: r.Gy
|
|
743
|
-
}, c = r.Fp;
|
|
744
|
-
let t = r.allowedPrivateKeyLengths ? Array.from(new Set(r.allowedPrivateKeyLengths.map((l) => Math.ceil(l / 2)))) : void 0;
|
|
745
|
-
const m = Yt(s.n, {
|
|
746
|
-
BITS: r.nBitLength,
|
|
747
|
-
allowedLengths: t,
|
|
748
|
-
modFromBytes: r.wrapPrivateKey
|
|
749
|
-
}), w = {
|
|
750
|
-
Fp: c,
|
|
751
|
-
Fn: m,
|
|
752
|
-
allowInfinityPoint: r.allowInfinityPoint,
|
|
753
|
-
endo: r.endo,
|
|
754
|
-
isTorsionFree: r.isTorsionFree,
|
|
755
|
-
clearCofactor: r.clearCofactor,
|
|
756
|
-
fromBytes: r.fromBytes,
|
|
757
|
-
toBytes: r.toBytes
|
|
758
|
-
};
|
|
759
|
-
return { CURVE: s, curveOpts: w };
|
|
760
|
-
}
|
|
761
|
-
function qt(r) {
|
|
762
|
-
const { CURVE: s, curveOpts: c } = Xt(r), t = {
|
|
763
|
-
hmac: r.hmac,
|
|
764
|
-
randomBytes: r.randomBytes,
|
|
765
|
-
lowS: r.lowS,
|
|
766
|
-
bits2int: r.bits2int,
|
|
767
|
-
bits2int_modN: r.bits2int_modN
|
|
768
|
-
};
|
|
769
|
-
return { CURVE: s, curveOpts: c, hash: r.hash, ecdsaOpts: t };
|
|
770
|
-
}
|
|
771
|
-
function It(r, s) {
|
|
772
|
-
const c = s.Point;
|
|
773
|
-
return Object.assign({}, s, {
|
|
774
|
-
ProjectivePoint: c,
|
|
775
|
-
CURVE: Object.assign({}, r, Vt(c.Fn.ORDER, c.Fn.BITS))
|
|
776
|
-
});
|
|
777
|
-
}
|
|
778
|
-
function Gt(r) {
|
|
779
|
-
const { CURVE: s, curveOpts: c, hash: t, ecdsaOpts: m } = qt(r), w = Ct(s, c), l = Dt(w, t, m);
|
|
780
|
-
return It(r, l);
|
|
1
|
+
import { SizeExceedsPaddingSizeError as a } from "./index211.js";
|
|
2
|
+
function o(r, i = {}) {
|
|
3
|
+
const { dir: n, size: e = 32 } = i;
|
|
4
|
+
if (e === 0)
|
|
5
|
+
return r;
|
|
6
|
+
const t = r.replace("0x", "");
|
|
7
|
+
if (t.length > e * 2)
|
|
8
|
+
throw new a({
|
|
9
|
+
size: Math.ceil(t.length / 2),
|
|
10
|
+
targetSize: e,
|
|
11
|
+
type: "Hex"
|
|
12
|
+
});
|
|
13
|
+
return `0x${t[n === "right" ? "padEnd" : "padStart"](e * 2, "0")}`;
|
|
781
14
|
}
|
|
782
15
|
export {
|
|
783
|
-
|
|
784
|
-
Ht as DERErr,
|
|
785
|
-
M as _normFnElement,
|
|
786
|
-
Tt as _splitEndoScalar,
|
|
787
|
-
Lt as ecdh,
|
|
788
|
-
Dt as ecdsa,
|
|
789
|
-
Gt as weierstrass,
|
|
790
|
-
Ct as weierstrassN
|
|
16
|
+
o as pad
|
|
791
17
|
};
|