@li0ard/dstu 0.1.4 → 0.1.6
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/README.md +4 -1
- package/dstu4145/const.d.ts +2 -0
- package/dstu4145/const.js +17 -0
- package/dstu4145/ec/expand.js +3 -4
- package/dstu4145/ec/index.d.ts +12 -27
- package/dstu4145/ec/index.js +91 -40
- package/dstu4145/ec/math.d.ts +3 -0
- package/dstu4145/ec/math.js +21 -13
- package/dstu4145/ec/onb.js +6 -6
- package/dstu4145/ec/wnaf.js +1 -1
- package/dstu4145/index.d.ts +11 -0
- package/dstu4145/index.js +15 -1
- package/dstu9041/curve.d.ts +431 -0
- package/dstu9041/curve.js +132 -0
- package/dstu9041/index.d.ts +21 -0
- package/dstu9041/index.js +98 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/kupyna/index.js +2 -1
- package/kupyna/kmac.js +2 -1
- package/modes/cbc.js +6 -8
- package/modes/cfb.js +24 -31
- package/modes/ctr.js +2 -2
- package/modes/ecb.js +2 -2
- package/modes/gcm.js +14 -16
- package/modes/index.d.ts +2 -0
- package/modes/index.js +2 -0
- package/modes/kw.d.ts +7 -0
- package/modes/kw.js +79 -0
- package/modes/mac.js +55 -59
- package/modes/ofb.js +3 -4
- package/modes/xts.d.ts +7 -0
- package/modes/xts.js +82 -0
- package/package.json +3 -1
- package/strumok/index.d.ts +4 -3
- package/strumok/index.js +42 -277
- package/utils.d.ts +1 -7
- package/utils.js +1 -23
package/README.md
CHANGED
|
@@ -20,11 +20,12 @@ npm i @li0ard/dstu
|
|
|
20
20
|
|
|
21
21
|
## Supported algorithms
|
|
22
22
|
|
|
23
|
-
- Curves and
|
|
23
|
+
- Curves and ECDSA (DSTU 4145-2002, polynomial and optimal normal basis)
|
|
24
24
|
- Kalyna cipher (DSTU 7624:2014)
|
|
25
25
|
- Kupyna hash function (DSTU 7564:2014)
|
|
26
26
|
- Supports KMAC
|
|
27
27
|
- Strumok stream cipher (DSTU 8845:2019)
|
|
28
|
+
- ECIES encryption schema over Twisted Edwards curves (DSTU 9041:2020)
|
|
28
29
|
|
|
29
30
|
## Supported cipher modes
|
|
30
31
|
|
|
@@ -35,6 +36,8 @@ npm i @li0ard/dstu
|
|
|
35
36
|
- Galois Counter mode (GCM)
|
|
36
37
|
- Message Authentication Code mode (MAC and GMAC)
|
|
37
38
|
- Output Feedback mode (OFB)
|
|
39
|
+
- Key wrapping (KW)
|
|
40
|
+
- XEX Tweakable Block Ciphertext Stealing (XTS)
|
|
38
41
|
|
|
39
42
|
## Supported curves
|
|
40
43
|
|
package/dstu4145/const.d.ts
CHANGED
|
@@ -22,6 +22,8 @@ export interface DSTUShortParameters {
|
|
|
22
22
|
export interface DSTUParameters extends DSTUShortParameters {
|
|
23
23
|
/** Curve order */
|
|
24
24
|
order: string;
|
|
25
|
+
/** Curve cofactor */
|
|
26
|
+
cofactor: 2 | 4;
|
|
25
27
|
/** Base point `X` coordinate */
|
|
26
28
|
Gx: string;
|
|
27
29
|
/** Base point `Y` coordinate */
|
package/dstu4145/const.js
CHANGED
|
@@ -3,6 +3,7 @@ export const DSTU_163 = {
|
|
|
3
3
|
a: 1,
|
|
4
4
|
b: "05FF6108462A2DC8210AB403925E638A19C1455D21",
|
|
5
5
|
order: "0400000000000000000002BEC12BE2262D39BCF14D",
|
|
6
|
+
cofactor: 2,
|
|
6
7
|
Gx: "02E2F85F5DD74CE983A5C4237229DAF8A3F35823BE",
|
|
7
8
|
Gy: "03826F008A8C51D7B95284D9D03FF0E00CE2CD723A",
|
|
8
9
|
m: 163,
|
|
@@ -13,6 +14,7 @@ export const DSTU_163_TEST = {
|
|
|
13
14
|
a: 1,
|
|
14
15
|
b: "05FF6108462A2DC8210AB403925E638A19C1455D21",
|
|
15
16
|
order: "0400000000000000000002BEC12BE2262D39BCF14D",
|
|
17
|
+
cofactor: 2,
|
|
16
18
|
Gx: "072D867F93A93AC27DF9FF01AFFE74885C8C540420",
|
|
17
19
|
Gy: "00224A9C3947852B97C5599D5F4AB81122ADC3FD9B",
|
|
18
20
|
m: 163,
|
|
@@ -23,6 +25,7 @@ export const DSTU_167 = {
|
|
|
23
25
|
a: 1,
|
|
24
26
|
b: "6EE3CEEB230811759F20518A0930F1A4315A827DAC",
|
|
25
27
|
order: "3FFFFFFFFFFFFFFFFFFFFFB12EBCC7D7F29FF7701F",
|
|
28
|
+
cofactor: 2,
|
|
26
29
|
Gx: "7A1F6653786A68192803910A3D30B2A2018B21CD54",
|
|
27
30
|
Gy: "5F49EB26781C0EC6B8909156D98ED435E45FD59918",
|
|
28
31
|
m: 167,
|
|
@@ -33,6 +36,7 @@ export const DSTU_173 = {
|
|
|
33
36
|
a: 0,
|
|
34
37
|
b: "108576C80499DB2FC16EDDF6853BBB278F6B6FB437D9",
|
|
35
38
|
order: "0800000000000000000000189B4E67606E3825BB2831",
|
|
39
|
+
cofactor: 4,
|
|
36
40
|
Gx: "04D41A619BCC6EADF0448FA22FAD567A9181D37389CA",
|
|
37
41
|
Gy: "10B51CC12849B234C75E6DD2028BF7FF5C1CE0D991A1",
|
|
38
42
|
m: 173,
|
|
@@ -48,6 +52,7 @@ export const DSTU_173_ONB = {
|
|
|
48
52
|
a: 0,
|
|
49
53
|
b: "043D7E139319F43BA00944915740E1E6651B06E278C7",
|
|
50
54
|
order: "800000000000000000000189B4E67606E3825BB2831",
|
|
55
|
+
cofactor: 4,
|
|
51
56
|
Gx: "0A3B8B8D704089B24318CAA51917CB6E424B4E6DEA56",
|
|
52
57
|
Gy: "005A43D5EE1F400349CF35150C74F119A39145F0CB08",
|
|
53
58
|
m: 173,
|
|
@@ -59,6 +64,7 @@ export const DSTU_173_ONB_TEST = {
|
|
|
59
64
|
a: 0,
|
|
60
65
|
b: "043D7E139319F43BA00944915740E1E6651B06E278C7",
|
|
61
66
|
order: "800000000000000000000189B4E67606E3825BB2831",
|
|
67
|
+
cofactor: 4,
|
|
62
68
|
Gx: "1D26E5282C2647E1B0327CB4F6087932A0A1F006A89F",
|
|
63
69
|
Gy: "0322218E4551CF8F6E3345644A10B2408F5DF19EC2C7",
|
|
64
70
|
m: 173,
|
|
@@ -70,6 +76,7 @@ export const DSTU_179 = {
|
|
|
70
76
|
a: 1,
|
|
71
77
|
b: "04A6E0856526436F2F88DD07A341E32D04184572BEB710",
|
|
72
78
|
order: "03FFFFFFFFFFFFFFFFFFFFFFB981960435FE5AB64236EF",
|
|
79
|
+
cofactor: 2,
|
|
73
80
|
Gx: "06BA06FE51464B2BD26DC57F48819BA9954667022C7D03",
|
|
74
81
|
Gy: "025FBC363582DCEC065080CA8287AAFF09788A66DC3A9E",
|
|
75
82
|
m: 179,
|
|
@@ -80,6 +87,7 @@ export const DSTU_179_ONB = {
|
|
|
80
87
|
a: 1,
|
|
81
88
|
b: "019C9EBC4FD8308193D3A61762C547C82F2E6B2182CBCB",
|
|
82
89
|
order: "03FFFFFFFFFFFFFFFFFFFFFFB981960435FE5AB64236EF",
|
|
90
|
+
cofactor: 2,
|
|
83
91
|
Gx: "011F4112265410477159E0D3E39BC4AC4ADE43B21B66B9",
|
|
84
92
|
Gy: "02CA5F0B34C61A86216F13766591CB266E2819EE582B3C",
|
|
85
93
|
m: 179,
|
|
@@ -95,6 +103,7 @@ export const DSTU_191 = {
|
|
|
95
103
|
a: 1,
|
|
96
104
|
b: "7BC86E2102902EC4D5890E8B6B4981FF27E0482750FEFC03",
|
|
97
105
|
order: "40000000000000000000000069A779CAC1DABC6788F7474F",
|
|
106
|
+
cofactor: 2,
|
|
98
107
|
Gx: "714114B762F2FF4A7912A6D2AC58B9B5C2FCFE76DAEB7129",
|
|
99
108
|
Gy: "29C41E568B77C617EFE5902F11DB96FA9613CD8D03DB08DA",
|
|
100
109
|
m: 191,
|
|
@@ -105,6 +114,7 @@ export const DSTU_191_ONB = {
|
|
|
105
114
|
a: 1,
|
|
106
115
|
b: "13871C9D29D6CEEA740FD57444F72FDBAE559C13A1E31EF8",
|
|
107
116
|
order: "40000000000000000000000069A779CAC1DABC6788F7474F",
|
|
117
|
+
cofactor: 2,
|
|
108
118
|
Gx: "7159E0D3E39BC4AC4ADE43B21B66B94F2AF25ABEEB1D85D3",
|
|
109
119
|
Gy: "57E45D546BE6201E9705E029535D009D31F51D81E18226BE",
|
|
110
120
|
m: 191,
|
|
@@ -120,6 +130,7 @@ export const DSTU_233 = {
|
|
|
120
130
|
a: 1,
|
|
121
131
|
b: "6973B15095675534C7CF7E64A21BD54EF5DD3B8A0326AA936ECE454D2C",
|
|
122
132
|
order: "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
|
|
133
|
+
cofactor: 2,
|
|
123
134
|
Gx: "3FCDA526B6CDF83BA1118DF35B3C31761D3545F32728D003EEB25EFE96",
|
|
124
135
|
Gy: "9CA8B57A934C54DEEDA9E54A7BBAD95E3B2E91C54D32BE0B9DF96D8D35",
|
|
125
136
|
m: 233,
|
|
@@ -130,6 +141,7 @@ export const DSTU_233_ONB = {
|
|
|
130
141
|
a: 1,
|
|
131
142
|
b: "80F920952A702C75B704A424C018EEA55AA44664F3A003E0962D4F9A8E",
|
|
132
143
|
order: "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
|
|
144
|
+
cofactor: 2,
|
|
133
145
|
Gx: "A0823B8ACF069F1CDE256256F21D90DB35CA795792D5F758EC2E9992FB",
|
|
134
146
|
Gy: "019DAE222AF326D33AFB7761953F624A23D3EB1CA7A1C300F0AA9953CA85",
|
|
135
147
|
m: 233,
|
|
@@ -145,6 +157,7 @@ export const DSTU_257 = {
|
|
|
145
157
|
a: 0,
|
|
146
158
|
b: "01CEF494720115657E18F938D7A7942394FF9425C1458C57861F9EEA6ADBE3BE10",
|
|
147
159
|
order: "00800000000000000000000000000000006759213AF182E987D3E17714907D470D",
|
|
160
|
+
cofactor: 4,
|
|
148
161
|
Gx: "002A29EF207D0E9B6C55CD260B306C7E007AC491CA1B10C62334A9E8DCD8D20FB7",
|
|
149
162
|
Gy: "010686D41FF744D4449FCCF6D8EEA03102E6812C93A9D60B978B702CF156D814EF",
|
|
150
163
|
m: 257,
|
|
@@ -155,6 +168,7 @@ export const DSTU_307 = {
|
|
|
155
168
|
a: 1,
|
|
156
169
|
b: "0393C7F7D53666B5054B5E6C6D3DE94F4296C0C599E2E2E241050DF18B6090BDC90186904968BB",
|
|
157
170
|
order: "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC079C2F3825DA70D390FBBA588D4604022B7B7",
|
|
171
|
+
cofactor: 2,
|
|
158
172
|
Gx: "0216EE8B189D291A0224984C1E92F1D16BF75CCD825A087A239B276D3167743C52C02D6E7232AA",
|
|
159
173
|
Gy: "05D9306BACD22B7FAEB09D2E049C6E2866C5D1677762A8F2F2DC9A11C7F7BE8340AB2237C7F2A0",
|
|
160
174
|
m: 307,
|
|
@@ -165,6 +179,7 @@ export const DSTU_367 = {
|
|
|
165
179
|
a: 1,
|
|
166
180
|
b: "43FC8AD242B0B7A6F3D1627AD5654447556B47BF6AA4A64B0C2AFE42CADAB8F93D92394C79A79755437B56995136",
|
|
167
181
|
order: "40000000000000000000000000000000000000000000009C300B75A3FA824F22428FD28CE8812245EF44049B2D49",
|
|
182
|
+
cofactor: 2,
|
|
168
183
|
Gx: "324A6EDDD512F08C49A99AE0D3F961197A76413E7BE81A400CA681E09639B5FE12E59A109F78BF4A373541B3B9A1",
|
|
169
184
|
Gy: "01AB597A5B4477F59E39539007C7F977D1A567B92B043A49C6B61984C3FE3481AAF454CD41BA1F051626442B3C10",
|
|
170
185
|
m: 367,
|
|
@@ -175,6 +190,7 @@ export const DSTU_431 = {
|
|
|
175
190
|
a: 1,
|
|
176
191
|
b: "03CE10490F6A708FC26DFE8C3D27C4F94E690134D5BFF988D8D28AAEAEDE975936C66BAC536B18AE2DC312CA493117DAA469C640CAF3",
|
|
177
192
|
order: "3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBA3175458009A8C0A724F02F81AA8A1FCBAF80D90C7A95110504CF",
|
|
193
|
+
cofactor: 2,
|
|
178
194
|
Gx: "1A62BA79D98133A16BBAE7ED9A8E03C32E0824D57AEF72F88986874E5AAE49C27BED49A2A95058068426C2171E99FD3B43C5947C857D",
|
|
179
195
|
Gy: "70B5E1E14031C1F70BBEFE96BDDE66F451754B4CA5F48DA241F331AA396B8D1839A855C1769B1EA14BA53308B5E2723724E090E02DB9",
|
|
180
196
|
m: 431,
|
|
@@ -185,6 +201,7 @@ export const DSTU_431_ONB = {
|
|
|
185
201
|
a: 1,
|
|
186
202
|
b: "53FB7AF7B4407000A6F226AD6BAD28378646BD83F1F940810A4C19536EE65E53F40F973F2F06C5E80EFE3B43651BD5FF8B06BA5F9299",
|
|
187
203
|
order: "3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBA3175458009A8C0A724F02F81AA8A1FCBAF80D90C7A95110504CF",
|
|
204
|
+
cofactor: 2,
|
|
188
205
|
Gx: "2020182CFCD7C724157B363D4ABFD4C16251773C595D4E5C88572FA9D5EBB58142326448C08D3EE3E17E40B835D3090DCFBA7C5ADEA6",
|
|
189
206
|
Gy: "18E1B6EFB28EEF8A1F9C1859ED57C74CA747E4E1A60294C005D1920E80903A432929855828DD7F21714F837E119A044987F00110ADAD",
|
|
190
207
|
m: 431,
|
package/dstu4145/ec/expand.js
CHANGED
|
@@ -23,12 +23,11 @@ export const expandPoint = (xBytes, params) => {
|
|
|
23
23
|
const rhs = field.mul(x2, xClean);
|
|
24
24
|
if (a === 1)
|
|
25
25
|
rhs.ixor(x2);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const z = field.solve_quad(field.mul(rhs, field.invert(x2)));
|
|
26
|
+
rhs.ixor(b);
|
|
27
|
+
const z = field.solve_quad(field.div(rhs, x2));
|
|
29
28
|
const traceZ = field.trace(z);
|
|
30
29
|
if ((traceZ === 0 && bit) || (traceZ === 1 && !bit))
|
|
31
|
-
|
|
30
|
+
field.invBit(z, 0);
|
|
32
31
|
let y = field.mul(z, xClean);
|
|
33
32
|
if (onb && converter)
|
|
34
33
|
y = converter.pbToOnb(y);
|
package/dstu4145/ec/index.d.ts
CHANGED
|
@@ -7,12 +7,15 @@ export declare const binaryWeierstrass: (parameters: DSTUParameters) => Readonly
|
|
|
7
7
|
LENGTH: number;
|
|
8
8
|
mod: (f: BN) => BN;
|
|
9
9
|
mul: (x: BN, v: BN) => BN;
|
|
10
|
+
div: (x: BN, v: BN) => BN;
|
|
10
11
|
sqr: (x: BN) => BN;
|
|
12
|
+
sqrt: (x: BN) => BN;
|
|
11
13
|
invert: (f: BN) => BN;
|
|
12
14
|
solve_quad: (v: BN) => BN;
|
|
13
15
|
trace: (x: BN) => 0 | 1;
|
|
14
16
|
traceOnb: (x: BN) => 0 | 1;
|
|
15
17
|
hashToField: (hash: TArg<Uint8Array>) => BN;
|
|
18
|
+
invBit: (x: BN, i: number) => BN;
|
|
16
19
|
fromHexStringOrBytes: (v: string | Uint8Array) => BN;
|
|
17
20
|
toBytes: (x: BN, length?: number) => TRet<Uint8Array>;
|
|
18
21
|
}>;
|
|
@@ -25,8 +28,9 @@ export declare const binaryWeierstrass: (parameters: DSTUParameters) => Readonly
|
|
|
25
28
|
_double?: /*elided*/ any;
|
|
26
29
|
x: BN;
|
|
27
30
|
y: BN;
|
|
31
|
+
assertValidity(): /*elided*/ any;
|
|
28
32
|
add(p: /*elided*/ any): /*elided*/ any;
|
|
29
|
-
mul(f: BN): /*elided*/ any;
|
|
33
|
+
mul(f: BN | number): /*elided*/ any;
|
|
30
34
|
negate(): /*elided*/ any;
|
|
31
35
|
clone(): /*elided*/ any;
|
|
32
36
|
isZero(): boolean;
|
|
@@ -48,8 +52,9 @@ export declare const binaryWeierstrass: (parameters: DSTUParameters) => Readonly
|
|
|
48
52
|
_double?: /*elided*/ any;
|
|
49
53
|
x: BN;
|
|
50
54
|
y: BN;
|
|
55
|
+
assertValidity(): /*elided*/ any;
|
|
51
56
|
add(p: /*elided*/ any): /*elided*/ any;
|
|
52
|
-
mul(f: BN): /*elided*/ any;
|
|
57
|
+
mul(f: BN | number): /*elided*/ any;
|
|
53
58
|
negate(): /*elided*/ any;
|
|
54
59
|
clone(): /*elided*/ any;
|
|
55
60
|
isZero(): boolean;
|
|
@@ -71,31 +76,9 @@ export declare const binaryWeierstrass: (parameters: DSTUParameters) => Readonly
|
|
|
71
76
|
_double?: /*elided*/ any;
|
|
72
77
|
x: BN;
|
|
73
78
|
y: BN;
|
|
79
|
+
assertValidity(): /*elided*/ any;
|
|
74
80
|
add(p: /*elided*/ any): /*elided*/ any;
|
|
75
|
-
mul(f: BN): /*elided*/ any;
|
|
76
|
-
negate(): /*elided*/ any;
|
|
77
|
-
clone(): /*elided*/ any;
|
|
78
|
-
isZero(): boolean;
|
|
79
|
-
compress(): BN;
|
|
80
|
-
toBytes(isCompressed?: boolean): TRet<Uint8Array>;
|
|
81
|
-
double(): /*elided*/ any;
|
|
82
|
-
timesPow2(e: number): /*elided*/ any;
|
|
83
|
-
precomp(width: number): {
|
|
84
|
-
pos: /*elided*/ any[];
|
|
85
|
-
neg: /*elided*/ any[];
|
|
86
|
-
};
|
|
87
|
-
mulWnaf(f: BN): /*elided*/ any;
|
|
88
|
-
};
|
|
89
|
-
expand(xExt: BN): {
|
|
90
|
-
_precomp?: {
|
|
91
|
-
pos: /*elided*/ any[];
|
|
92
|
-
neg: /*elided*/ any[];
|
|
93
|
-
};
|
|
94
|
-
_double?: /*elided*/ any;
|
|
95
|
-
x: BN;
|
|
96
|
-
y: BN;
|
|
97
|
-
add(p: /*elided*/ any): /*elided*/ any;
|
|
98
|
-
mul(f: BN): /*elided*/ any;
|
|
81
|
+
mul(f: BN | number): /*elided*/ any;
|
|
99
82
|
negate(): /*elided*/ any;
|
|
100
83
|
clone(): /*elided*/ any;
|
|
101
84
|
isZero(): boolean;
|
|
@@ -109,6 +92,7 @@ export declare const binaryWeierstrass: (parameters: DSTUParameters) => Readonly
|
|
|
109
92
|
};
|
|
110
93
|
mulWnaf(f: BN): /*elided*/ any;
|
|
111
94
|
};
|
|
95
|
+
/** Deserialize point from bytes (compressed/uncompressed) */
|
|
112
96
|
fromBytes(bytes: TArg<Uint8Array>): {
|
|
113
97
|
_precomp?: {
|
|
114
98
|
pos: /*elided*/ any[];
|
|
@@ -117,8 +101,9 @@ export declare const binaryWeierstrass: (parameters: DSTUParameters) => Readonly
|
|
|
117
101
|
_double?: /*elided*/ any;
|
|
118
102
|
x: BN;
|
|
119
103
|
y: BN;
|
|
104
|
+
assertValidity(): /*elided*/ any;
|
|
120
105
|
add(p: /*elided*/ any): /*elided*/ any;
|
|
121
|
-
mul(f: BN): /*elided*/ any;
|
|
106
|
+
mul(f: BN | number): /*elided*/ any;
|
|
122
107
|
negate(): /*elided*/ any;
|
|
123
108
|
clone(): /*elided*/ any;
|
|
124
109
|
isZero(): boolean;
|
package/dstu4145/ec/index.js
CHANGED
|
@@ -10,6 +10,16 @@ export const binaryWeierstrass = (parameters) => {
|
|
|
10
10
|
const toExternal = (f) => onb ? onb.pbToOnb(f) : f.clone();
|
|
11
11
|
const order = field.fromHexStringOrBytes(parameters.order);
|
|
12
12
|
const b = toInternal(field.fromHexStringOrBytes(parameters.b));
|
|
13
|
+
const c = field.sqrt(b);
|
|
14
|
+
const isValidXY = (x, y) => {
|
|
15
|
+
const x2 = field.sqr(x);
|
|
16
|
+
const lhs = field.sqr(y).ixor(field.mul(x, y));
|
|
17
|
+
const rhs = field.mul(x2, x).ixor(b);
|
|
18
|
+
// `a` must be 0 or 1, so if a = 1, just add x^2
|
|
19
|
+
if (parameters.a === 1)
|
|
20
|
+
rhs.ixor(x2);
|
|
21
|
+
return lhs.eq(rhs);
|
|
22
|
+
};
|
|
13
23
|
const scalarByteLength = Math.ceil(order.bitLength() / 8), pointByteLength = field.LENGTH * 2;
|
|
14
24
|
const lengths = Object.freeze({
|
|
15
25
|
fieldByteLength: field.LENGTH,
|
|
@@ -17,6 +27,22 @@ export const binaryWeierstrass = (parameters) => {
|
|
|
17
27
|
scalarByteLength,
|
|
18
28
|
signatureByteLength: scalarByteLength * 2
|
|
19
29
|
});
|
|
30
|
+
const Madd = (X1, Z1, X2, Z2, x) => {
|
|
31
|
+
const A = field.mul(X1, Z2);
|
|
32
|
+
const B = field.mul(Z1, X2);
|
|
33
|
+
const T = field.mul(A, B);
|
|
34
|
+
const Znew = field.sqr(A.ixor(B));
|
|
35
|
+
const Xnew = field.mul(Znew, x).ixor(T);
|
|
36
|
+
return [Xnew, Znew];
|
|
37
|
+
};
|
|
38
|
+
const Mdouble = (X, Z) => {
|
|
39
|
+
const X2 = field.sqr(X);
|
|
40
|
+
const Z2 = field.sqr(Z);
|
|
41
|
+
const T = field.sqr(field.mul(Z2, c));
|
|
42
|
+
const Znew = field.mul(X2, Z2);
|
|
43
|
+
const Xnew = field.sqr(X2).ixor(T);
|
|
44
|
+
return [Xnew, Znew];
|
|
45
|
+
};
|
|
20
46
|
// Point operations
|
|
21
47
|
class Point {
|
|
22
48
|
x;
|
|
@@ -28,6 +54,13 @@ export const binaryWeierstrass = (parameters) => {
|
|
|
28
54
|
constructor(x, y) {
|
|
29
55
|
this.x = x;
|
|
30
56
|
this.y = y;
|
|
57
|
+
this.assertValidity();
|
|
58
|
+
}
|
|
59
|
+
assertValidity() {
|
|
60
|
+
// Skip if point is infinity
|
|
61
|
+
if (!isValidXY(this.x, this.y) && !this.x.isZero() && !this.y.isZero())
|
|
62
|
+
throw new Error("Assertation failed: point isn't on curve");
|
|
63
|
+
return this;
|
|
31
64
|
}
|
|
32
65
|
add(p) {
|
|
33
66
|
const pz = Point.ZERO.clone();
|
|
@@ -39,11 +72,10 @@ export const binaryWeierstrass = (parameters) => {
|
|
|
39
72
|
return this;
|
|
40
73
|
let lbd, x2;
|
|
41
74
|
if (x0.cmp(x1) !== 0) {
|
|
42
|
-
|
|
43
|
-
lbd = field.mul(tmp, field.invert(tmp2));
|
|
75
|
+
lbd = field.div(y0.xor(y1), x0.xor(x1));
|
|
44
76
|
x2 = field.sqr(lbd);
|
|
45
77
|
if (parameters.a === 1)
|
|
46
|
-
|
|
78
|
+
field.invBit(x2, 0);
|
|
47
79
|
x2.ixor(lbd).ixor(x0).ixor(x1);
|
|
48
80
|
}
|
|
49
81
|
else {
|
|
@@ -51,36 +83,55 @@ export const binaryWeierstrass = (parameters) => {
|
|
|
51
83
|
return pz;
|
|
52
84
|
if (x1.isZero())
|
|
53
85
|
return pz;
|
|
54
|
-
lbd = x1.xor(field.
|
|
86
|
+
lbd = x1.xor(field.div(p.y, p.x));
|
|
55
87
|
x2 = field.sqr(lbd);
|
|
56
88
|
if (parameters.a === 1)
|
|
57
|
-
|
|
89
|
+
field.invBit(x2, 0);
|
|
58
90
|
x2.ixor(lbd);
|
|
59
91
|
}
|
|
60
|
-
const y2 = field.mul(lbd, x1.xor(x2)).
|
|
92
|
+
const y2 = field.mul(lbd, x1.xor(x2)).ixor(x2).ixor(y1);
|
|
61
93
|
pz.x = x2;
|
|
62
94
|
pz.y = y2;
|
|
63
95
|
return pz;
|
|
64
96
|
}
|
|
65
97
|
mul(f) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
98
|
+
if (typeof f == "number")
|
|
99
|
+
f = new BN(f);
|
|
100
|
+
if (f.isZero() || this.x.isZero())
|
|
101
|
+
return Point.ZERO.clone();
|
|
102
|
+
let X1 = this.x.clone(), Z1 = new BN(1);
|
|
103
|
+
const xSqr = field.sqr(this.x);
|
|
104
|
+
let X2 = field.sqr(xSqr).ixor(b), Z2 = xSqr.clone();
|
|
105
|
+
for (let i = f.bitLength() - 2; i >= 0; i--) {
|
|
106
|
+
if (f.testn(i)) {
|
|
107
|
+
[X1, Z1] = Madd(X1, Z1, X2, Z2, this.x);
|
|
108
|
+
[X2, Z2] = Mdouble(X2, Z2);
|
|
71
109
|
}
|
|
72
110
|
else {
|
|
73
|
-
|
|
74
|
-
|
|
111
|
+
[X2, Z2] = Madd(X2, Z2, X1, Z1, this.x);
|
|
112
|
+
[X1, Z1] = Mdouble(X1, Z1);
|
|
75
113
|
}
|
|
76
114
|
}
|
|
77
|
-
|
|
115
|
+
// Convert projective coords to affine
|
|
116
|
+
if (Z1.isZero())
|
|
117
|
+
return Point.ZERO.clone();
|
|
118
|
+
if (Z2.isZero())
|
|
119
|
+
return this.negate();
|
|
120
|
+
const A = field.mul(this.x, Z1).ixor(X1);
|
|
121
|
+
const B = field.mul(this.x, Z2).ixor(X2);
|
|
122
|
+
const C = field.mul(Z1, Z2);
|
|
123
|
+
const D = field.mul(C, this.x);
|
|
124
|
+
const E = field.invert(field.mul(D, Z1));
|
|
125
|
+
const F = field.mul(A, B).ixor(field.mul(C, xSqr.ixor(this.y)));
|
|
126
|
+
const Xk = field.mul(X1, field.mul(D, E));
|
|
127
|
+
const Yk = field.mul(field.mul(A, F), E).ixor(this.y);
|
|
128
|
+
return new Point(Xk, Yk);
|
|
78
129
|
}
|
|
79
|
-
negate() { return new Point(this.x, this.x.xor(this.y)); }
|
|
80
|
-
clone() { return new Point(this.x, this.y); }
|
|
130
|
+
negate() { return new Point(this.x.clone(), this.x.xor(this.y)); }
|
|
131
|
+
clone() { return new Point(this.x.clone(), this.y.clone()); }
|
|
81
132
|
isZero() { return this.x.isZero() && this.y.isZero(); }
|
|
82
133
|
compress() {
|
|
83
|
-
const tmp = field.
|
|
134
|
+
const tmp = field.div(this.y, this.x);
|
|
84
135
|
return toExternal(this.x).setn(0, field.trace(tmp));
|
|
85
136
|
}
|
|
86
137
|
toBytes(isCompressed = false) {
|
|
@@ -145,37 +196,37 @@ export const binaryWeierstrass = (parameters) => {
|
|
|
145
196
|
}
|
|
146
197
|
return R;
|
|
147
198
|
}
|
|
148
|
-
|
|
149
|
-
const bit = xExt.testn(0);
|
|
150
|
-
const xCleanExt = xExt.clone();
|
|
151
|
-
xCleanExt.setn(0, 0);
|
|
152
|
-
const traceX = onb ? field.traceOnb(xCleanExt) : field.trace(xCleanExt);
|
|
153
|
-
if ((traceX === 1 && parameters.a === 0) || (traceX === 0 && parameters.a === 1))
|
|
154
|
-
xCleanExt.setn(0, 1);
|
|
155
|
-
const xClean = toInternal(xCleanExt);
|
|
156
|
-
const x2 = field.sqr(xClean);
|
|
157
|
-
const rhs = field.mul(x2, xClean);
|
|
158
|
-
if (parameters.a === 1)
|
|
159
|
-
rhs.ixor(x2);
|
|
160
|
-
if (b)
|
|
161
|
-
rhs.ixor(b);
|
|
162
|
-
const z = field.solve_quad(field.mul(rhs, field.invert(x2)));
|
|
163
|
-
const traceZ = field.trace(z);
|
|
164
|
-
if ((traceZ === 0 && bit) || (traceZ === 1 && !bit))
|
|
165
|
-
z.setn(0, !z.testn(0));
|
|
166
|
-
return new Point(xClean, field.mul(z, xClean));
|
|
167
|
-
}
|
|
199
|
+
/** Deserialize point from bytes (compressed/uncompressed) */
|
|
168
200
|
static fromBytes(bytes) {
|
|
201
|
+
// Uncompressed
|
|
169
202
|
if (bytes.length == pointByteLength)
|
|
170
203
|
return new Point(toInternal(field.fromHexStringOrBytes(bytes.subarray(0, field.LENGTH))), toInternal(field.fromHexStringOrBytes(bytes.subarray(field.LENGTH))));
|
|
171
|
-
|
|
172
|
-
|
|
204
|
+
// Compressed (need to recover y-coord)
|
|
205
|
+
else if (bytes.length == field.LENGTH) {
|
|
206
|
+
const xExt = field.fromHexStringOrBytes(bytes);
|
|
207
|
+
const bit = xExt.testn(0);
|
|
208
|
+
xExt.setn(0, 0);
|
|
209
|
+
const traceX = onb ? field.traceOnb(xExt) : field.trace(xExt);
|
|
210
|
+
if ((traceX === 1 && parameters.a === 0) || (traceX === 0 && parameters.a === 1))
|
|
211
|
+
xExt.setn(0, 1);
|
|
212
|
+
const x = toInternal(xExt);
|
|
213
|
+
const x2 = field.sqr(x);
|
|
214
|
+
const rhs = field.mul(x2, x);
|
|
215
|
+
if (parameters.a === 1)
|
|
216
|
+
rhs.ixor(x2);
|
|
217
|
+
rhs.ixor(b);
|
|
218
|
+
const z = field.solve_quad(field.div(rhs, x2));
|
|
219
|
+
const traceZ = field.trace(z);
|
|
220
|
+
if ((traceZ === 0 && bit) || (traceZ === 1 && !bit))
|
|
221
|
+
field.invBit(z, 0);
|
|
222
|
+
return new Point(x, field.mul(z, x));
|
|
223
|
+
}
|
|
173
224
|
else
|
|
174
225
|
throw new Error(`Invalid bytes length. Must be ${pointByteLength} for uncompressed and ${field.LENGTH} for compressed`);
|
|
175
226
|
}
|
|
176
227
|
}
|
|
177
228
|
// Precompute
|
|
178
|
-
Point.BASE.
|
|
229
|
+
Point.BASE.precomp(4);
|
|
179
230
|
return Object.freeze({
|
|
180
231
|
Field: field, Point,
|
|
181
232
|
ORDER: order,
|
package/dstu4145/ec/math.d.ts
CHANGED
|
@@ -8,12 +8,15 @@ export declare const createField: (m: number, ks: number[]) => Readonly<{
|
|
|
8
8
|
LENGTH: number;
|
|
9
9
|
mod: (f: BN) => BN;
|
|
10
10
|
mul: (x: BN, v: BN) => BN;
|
|
11
|
+
div: (x: BN, v: BN) => BN;
|
|
11
12
|
sqr: (x: BN) => BN;
|
|
13
|
+
sqrt: (x: BN) => BN;
|
|
12
14
|
invert: (f: BN) => BN;
|
|
13
15
|
solve_quad: (v: BN) => BN;
|
|
14
16
|
trace: (x: BN) => 0 | 1;
|
|
15
17
|
traceOnb: (x: BN) => 0 | 1;
|
|
16
18
|
hashToField: (hash: TArg<Uint8Array>) => BN;
|
|
19
|
+
invBit: (x: BN, i: number) => BN;
|
|
17
20
|
fromHexStringOrBytes: (v: string | Uint8Array) => BN;
|
|
18
21
|
toBytes: (x: BN, length?: number) => TRet<Uint8Array>;
|
|
19
22
|
}>;
|
package/dstu4145/ec/math.js
CHANGED
|
@@ -53,21 +53,21 @@ const le2BN = (arr) => {
|
|
|
53
53
|
}
|
|
54
54
|
return new BN(bytes, "le");
|
|
55
55
|
};
|
|
56
|
-
const xorShiftedWordInto = (
|
|
56
|
+
const xorShiftedWordInto = (dst, val, shift) => {
|
|
57
57
|
if (val === 0 || shift < 0)
|
|
58
58
|
return;
|
|
59
59
|
const wordShift = shift >>> 5, bitShift = shift & 31;
|
|
60
60
|
if (bitShift === 0) {
|
|
61
|
-
if (wordShift <
|
|
62
|
-
|
|
61
|
+
if (wordShift < dst.length)
|
|
62
|
+
dst[wordShift] ^= (val >>> 0);
|
|
63
63
|
}
|
|
64
64
|
else {
|
|
65
65
|
const lo = (val << bitShift) >>> 0;
|
|
66
66
|
const hi = (val >>> (WORD_BITS - bitShift)) >>> 0;
|
|
67
|
-
if (wordShift <
|
|
68
|
-
|
|
69
|
-
if (wordShift + 1 <
|
|
70
|
-
|
|
67
|
+
if (wordShift < dst.length)
|
|
68
|
+
dst[wordShift] ^= lo;
|
|
69
|
+
if (wordShift + 1 < dst.length)
|
|
70
|
+
dst[wordShift + 1] ^= hi;
|
|
71
71
|
}
|
|
72
72
|
};
|
|
73
73
|
/** Compute curve modulo */
|
|
@@ -83,12 +83,12 @@ export const computeMod = (m, ks) => {
|
|
|
83
83
|
export const createField = (m, ks) => {
|
|
84
84
|
const modulo = computeMod(m, ks), wordsPerElement = Math.ceil(m / WORD_BITS), wordsForProduct = 2 * wordsPerElement, vl = modulo.bitLength();
|
|
85
85
|
const bMod = (f) => {
|
|
86
|
+
// `f` will be mutated
|
|
86
87
|
if (f.bitLength() <= m)
|
|
87
88
|
return f;
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return bag;
|
|
89
|
+
while (f.bitLength() > m)
|
|
90
|
+
f.ixor(modulo.ushln(f.bitLength() - vl));
|
|
91
|
+
return f;
|
|
92
92
|
};
|
|
93
93
|
const mod = (f) => {
|
|
94
94
|
if (f.bitLength() <= m)
|
|
@@ -127,6 +127,7 @@ export const createField = (m, ks) => {
|
|
|
127
127
|
}
|
|
128
128
|
return mod(acc);
|
|
129
129
|
};
|
|
130
|
+
const div = (x, v) => mul(x, invert(v));
|
|
130
131
|
const sqr = (x) => {
|
|
131
132
|
const bytes = x.toArray();
|
|
132
133
|
const out = new Uint8Array(bytes.length * 2);
|
|
@@ -138,6 +139,13 @@ export const createField = (m, ks) => {
|
|
|
138
139
|
return mod(new BN(out));
|
|
139
140
|
};
|
|
140
141
|
const testBit = (x, i) => x.testn(i) ? 1 : 0;
|
|
142
|
+
const invBit = (x, i) => x.setn(i, !x.testn(i));
|
|
143
|
+
const sqrt = (x) => {
|
|
144
|
+
let r = mod(x);
|
|
145
|
+
for (let i = 0; i < m - 1; i++)
|
|
146
|
+
r = sqr(r);
|
|
147
|
+
return r;
|
|
148
|
+
};
|
|
141
149
|
const trace = (x) => {
|
|
142
150
|
let t = x;
|
|
143
151
|
for (let i = 1; i < m; i++)
|
|
@@ -189,8 +197,8 @@ export const createField = (m, ks) => {
|
|
|
189
197
|
const toBytes = (x, length) => new Uint8Array(x.toArray("be", length));
|
|
190
198
|
return Object.freeze({
|
|
191
199
|
MODULO: modulo, LENGTH: Math.ceil(m / 8),
|
|
192
|
-
mod, mul, sqr,
|
|
193
|
-
trace, traceOnb, hashToField,
|
|
200
|
+
mod, mul, div, sqr, sqrt, invert,
|
|
201
|
+
solve_quad, trace, traceOnb, hashToField, invBit,
|
|
194
202
|
fromHexStringOrBytes, toBytes
|
|
195
203
|
});
|
|
196
204
|
};
|
package/dstu4145/ec/onb.js
CHANGED
|
@@ -20,10 +20,10 @@ export const init_onb_parameters = (parameters) => {
|
|
|
20
20
|
const compress_mulp = new BN(hexToBytes(onb.matrix), "le");
|
|
21
21
|
decompress_matrix(compress_mulp, mulp);
|
|
22
22
|
const root1 = new BN(hexToBytes(onb.root1), "le"), root2 = new BN(hexToBytes(onb.root2), "le");
|
|
23
|
-
const multiplyOnb = (x
|
|
23
|
+
const multiplyOnb = (x) => {
|
|
24
24
|
const r = new BN(0);
|
|
25
25
|
const xb = (pos) => (x.testn(pos % m) ? 1 : 0);
|
|
26
|
-
const yb = (pos) => (
|
|
26
|
+
const yb = (pos) => (root2.testn(pos % m) ? 1 : 0);
|
|
27
27
|
for (let j = 0; j < m; j++) {
|
|
28
28
|
let bit = 0;
|
|
29
29
|
for (let i = 0; i < m - 1; i++) {
|
|
@@ -47,13 +47,13 @@ export const init_onb_parameters = (parameters) => {
|
|
|
47
47
|
};
|
|
48
48
|
const toPb = new Array(m);
|
|
49
49
|
toPb[0] = root1.clone();
|
|
50
|
-
const {
|
|
50
|
+
const { sqr } = createField(m, ks);
|
|
51
51
|
for (let i = 1; i < m; i++)
|
|
52
|
-
toPb[i] =
|
|
52
|
+
toPb[i] = sqr(toPb[i - 1]);
|
|
53
53
|
const _toOnb = new Array(m);
|
|
54
|
-
_toOnb[0] = new BN(1).
|
|
54
|
+
_toOnb[0] = new BN(1).ishln(m).isubn(1);
|
|
55
55
|
for (let i = 1; i < m; i++)
|
|
56
|
-
_toOnb[i] = multiplyOnb(_toOnb[i - 1]
|
|
56
|
+
_toOnb[i] = multiplyOnb(_toOnb[i - 1]);
|
|
57
57
|
const toOnb = _toOnb.map(reverseBits);
|
|
58
58
|
return Object.freeze({
|
|
59
59
|
onbToPb: (x) => {
|
package/dstu4145/ec/wnaf.js
CHANGED
|
@@ -4,7 +4,7 @@ import BN from "bn.js";
|
|
|
4
4
|
const DEFAULT_CUTOFFS = [13, 41, 121, 337, 897, 2305];
|
|
5
5
|
export const bitLength = (n) => n === 0 ? 0 : 32 - Math.clz32(n);
|
|
6
6
|
const compactNaf = (k) => {
|
|
7
|
-
if (k.bitLength() >= 1 << 16)
|
|
7
|
+
if (k.bitLength() >= (1 << 16))
|
|
8
8
|
throw new Error("'k' must have bitlength < 2^16");
|
|
9
9
|
if (k.isZero())
|
|
10
10
|
return new Int32Array(0);
|