@li0ard/dstu 0.1.7 → 0.1.9
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 +6 -2
- package/dstu4145/const.d.ts +2 -0
- package/dstu4145/const.js +42 -15
- package/dstu4145/ec/expand.d.ts +4 -3
- package/dstu4145/ec/expand.js +1 -2
- package/dstu4145/ec/index.js +1 -2
- package/dstu4145/ec/wnaf.d.ts +2 -1
- package/dstu4145/ecies.d.ts +82 -0
- package/dstu4145/ecies.js +62 -0
- package/dstu4145/{ec/math.d.ts → gf2m/index.d.ts} +1 -0
- package/dstu4145/gf2m/index.js +78 -0
- package/dstu4145/{ec → gf2m}/onb.js +31 -17
- package/dstu4145/gf2m/utils.d.ts +10 -0
- package/dstu4145/gf2m/utils.js +262 -0
- package/dstu4145/index.d.ts +23 -177
- package/dstu4145/index.js +37 -20
- package/dstu9041/curve.d.ts +4 -12
- package/dstu9041/curve.js +4 -3
- package/dstu9041/index.d.ts +2 -8
- package/dstu9041/index.js +2 -3
- package/dstu9311/hash.d.ts +10 -3
- package/dstu9311/hash.js +10 -3
- package/iso15946.d.ts +3 -0
- package/iso15946.js +49 -0
- package/kalyna/index.d.ts +2 -1
- package/kalyna/index.js +23 -28
- package/keywrap.d.ts +3 -3
- package/keywrap.js +21 -16
- package/modes/ccm.d.ts +4 -0
- package/modes/ccm.js +47 -0
- package/modes/gcm.d.ts +1 -1
- package/modes/gcm.js +1 -2
- package/modes/gmac.js +1 -2
- package/modes/index.d.ts +1 -0
- package/modes/index.js +1 -0
- package/modes/kw.d.ts +2 -6
- package/modes/xts.d.ts +2 -6
- package/modes/xts.js +2 -2
- package/package.json +6 -6
- package/strumok/index.d.ts +2 -1
- package/types.d.ts +77 -0
- package/utils.d.ts +1 -0
- package/utils.js +2 -2
- package/dstu4145/ec/math.js +0 -206
- /package/dstu4145/{ec → gf2m}/onb.d.ts +0 -0
package/dstu4145/ec/math.js
DELETED
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
import BN from "bn.js";
|
|
2
|
-
const SQR_PRECOMP = new Uint16Array([
|
|
3
|
-
0x0000, 0x0001, 0x0004, 0x0005, 0x0010, 0x0011, 0x0014, 0x0015,
|
|
4
|
-
0x0040, 0x0041, 0x0044, 0x0045, 0x0050, 0x0051, 0x0054, 0x0055,
|
|
5
|
-
0x0100, 0x0101, 0x0104, 0x0105, 0x0110, 0x0111, 0x0114, 0x0115,
|
|
6
|
-
0x0140, 0x0141, 0x0144, 0x0145, 0x0150, 0x0151, 0x0154, 0x0155,
|
|
7
|
-
0x0400, 0x0401, 0x0404, 0x0405, 0x0410, 0x0411, 0x0414, 0x0415,
|
|
8
|
-
0x0440, 0x0441, 0x0444, 0x0445, 0x0450, 0x0451, 0x0454, 0x0455,
|
|
9
|
-
0x0500, 0x0501, 0x0504, 0x0505, 0x0510, 0x0511, 0x0514, 0x0515,
|
|
10
|
-
0x0540, 0x0541, 0x0544, 0x0545, 0x0550, 0x0551, 0x0554, 0x0555,
|
|
11
|
-
0x1000, 0x1001, 0x1004, 0x1005, 0x1010, 0x1011, 0x1014, 0x1015,
|
|
12
|
-
0x1040, 0x1041, 0x1044, 0x1045, 0x1050, 0x1051, 0x1054, 0x1055,
|
|
13
|
-
0x1100, 0x1101, 0x1104, 0x1105, 0x1110, 0x1111, 0x1114, 0x1115,
|
|
14
|
-
0x1140, 0x1141, 0x1144, 0x1145, 0x1150, 0x1151, 0x1154, 0x1155,
|
|
15
|
-
0x1400, 0x1401, 0x1404, 0x1405, 0x1410, 0x1411, 0x1414, 0x1415,
|
|
16
|
-
0x1440, 0x1441, 0x1444, 0x1445, 0x1450, 0x1451, 0x1454, 0x1455,
|
|
17
|
-
0x1500, 0x1501, 0x1504, 0x1505, 0x1510, 0x1511, 0x1514, 0x1515,
|
|
18
|
-
0x1540, 0x1541, 0x1544, 0x1545, 0x1550, 0x1551, 0x1554, 0x1555,
|
|
19
|
-
0x4000, 0x4001, 0x4004, 0x4005, 0x4010, 0x4011, 0x4014, 0x4015,
|
|
20
|
-
0x4040, 0x4041, 0x4044, 0x4045, 0x4050, 0x4051, 0x4054, 0x4055,
|
|
21
|
-
0x4100, 0x4101, 0x4104, 0x4105, 0x4110, 0x4111, 0x4114, 0x4115,
|
|
22
|
-
0x4140, 0x4141, 0x4144, 0x4145, 0x4150, 0x4151, 0x4154, 0x4155,
|
|
23
|
-
0x4400, 0x4401, 0x4404, 0x4405, 0x4410, 0x4411, 0x4414, 0x4415,
|
|
24
|
-
0x4440, 0x4441, 0x4444, 0x4445, 0x4450, 0x4451, 0x4454, 0x4455,
|
|
25
|
-
0x4500, 0x4501, 0x4504, 0x4505, 0x4510, 0x4511, 0x4514, 0x4515,
|
|
26
|
-
0x4540, 0x4541, 0x4544, 0x4545, 0x4550, 0x4551, 0x4554, 0x4555,
|
|
27
|
-
0x5000, 0x5001, 0x5004, 0x5005, 0x5010, 0x5011, 0x5014, 0x5015,
|
|
28
|
-
0x5040, 0x5041, 0x5044, 0x5045, 0x5050, 0x5051, 0x5054, 0x5055,
|
|
29
|
-
0x5100, 0x5101, 0x5104, 0x5105, 0x5110, 0x5111, 0x5114, 0x5115,
|
|
30
|
-
0x5140, 0x5141, 0x5144, 0x5145, 0x5150, 0x5151, 0x5154, 0x5155,
|
|
31
|
-
0x5400, 0x5401, 0x5404, 0x5405, 0x5410, 0x5411, 0x5414, 0x5415,
|
|
32
|
-
0x5440, 0x5441, 0x5444, 0x5445, 0x5450, 0x5451, 0x5454, 0x5455,
|
|
33
|
-
0x5500, 0x5501, 0x5504, 0x5505, 0x5510, 0x5511, 0x5514, 0x5515,
|
|
34
|
-
0x5540, 0x5541, 0x5544, 0x5545, 0x5550, 0x5551, 0x5554, 0x5555
|
|
35
|
-
]);
|
|
36
|
-
const WORD_BITS = 32;
|
|
37
|
-
const MUL_WINDOW = 4, MUL_TABLE_SIZE = 1 << MUL_WINDOW;
|
|
38
|
-
const bn2LE = (x, numWords) => {
|
|
39
|
-
const arr = new Uint32Array(numWords);
|
|
40
|
-
const bytes = x.toArray("le");
|
|
41
|
-
for (let i = 0; i < bytes.length; i++)
|
|
42
|
-
arr[i >> 2] |= bytes[i] << ((i & 3) * 8);
|
|
43
|
-
return arr;
|
|
44
|
-
};
|
|
45
|
-
const le2BN = (arr) => {
|
|
46
|
-
const bytes = new Uint8Array(arr.length * 4);
|
|
47
|
-
for (let i = 0; i < arr.length; i++) {
|
|
48
|
-
const w = arr[i];
|
|
49
|
-
bytes[i * 4] = w & 0xff;
|
|
50
|
-
bytes[i * 4 + 1] = (w >>> 8) & 0xff;
|
|
51
|
-
bytes[i * 4 + 2] = (w >>> 16) & 0xff;
|
|
52
|
-
bytes[i * 4 + 3] = (w >>> 24) & 0xff;
|
|
53
|
-
}
|
|
54
|
-
return new BN(bytes, "le");
|
|
55
|
-
};
|
|
56
|
-
const xorShiftedWordInto = (dst, val, shift) => {
|
|
57
|
-
if (val === 0 || shift < 0)
|
|
58
|
-
return;
|
|
59
|
-
const wordShift = shift >>> 5, bitShift = shift & 31;
|
|
60
|
-
if (bitShift === 0) {
|
|
61
|
-
if (wordShift < dst.length)
|
|
62
|
-
dst[wordShift] ^= (val >>> 0);
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
const lo = (val << bitShift) >>> 0;
|
|
66
|
-
const hi = (val >>> (WORD_BITS - bitShift)) >>> 0;
|
|
67
|
-
if (wordShift < dst.length)
|
|
68
|
-
dst[wordShift] ^= lo;
|
|
69
|
-
if (wordShift + 1 < dst.length)
|
|
70
|
-
dst[wordShift + 1] ^= hi;
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
/** Compute curve modulo */
|
|
74
|
-
export const computeMod = (m, ks) => {
|
|
75
|
-
const modulo = new BN(0);
|
|
76
|
-
modulo.setn(m, 1);
|
|
77
|
-
modulo.setn(0, 1);
|
|
78
|
-
for (const i of ks)
|
|
79
|
-
modulo.setn(i, 1);
|
|
80
|
-
return modulo;
|
|
81
|
-
};
|
|
82
|
-
/** Create `GF(2^m)` field */
|
|
83
|
-
export const createField = (m, ks) => {
|
|
84
|
-
const modulo = computeMod(m, ks), wordsPerElement = Math.ceil(m / WORD_BITS), wordsForProduct = 2 * wordsPerElement, vl = modulo.bitLength();
|
|
85
|
-
const bMod = (f) => {
|
|
86
|
-
// `f` will be mutated
|
|
87
|
-
if (f.bitLength() <= m)
|
|
88
|
-
return f;
|
|
89
|
-
while (f.bitLength() > m)
|
|
90
|
-
f.ixor(modulo.ushln(f.bitLength() - vl));
|
|
91
|
-
return f;
|
|
92
|
-
};
|
|
93
|
-
const mod = (f) => {
|
|
94
|
-
if (f.bitLength() <= m)
|
|
95
|
-
return f.clone();
|
|
96
|
-
const words = bn2LE(f, Math.max(wordsForProduct, Math.ceil(f.bitLength() / WORD_BITS) + 1));
|
|
97
|
-
for (let i = words.length - 1; i >= wordsPerElement; i--) {
|
|
98
|
-
const T = words[i];
|
|
99
|
-
if (T === 0)
|
|
100
|
-
continue;
|
|
101
|
-
words[i] = 0;
|
|
102
|
-
const base = i * WORD_BITS - m;
|
|
103
|
-
xorShiftedWordInto(words, T, base);
|
|
104
|
-
for (const k of ks)
|
|
105
|
-
xorShiftedWordInto(words, T, base + k);
|
|
106
|
-
}
|
|
107
|
-
return bMod(le2BN(words));
|
|
108
|
-
};
|
|
109
|
-
const mul = (x, v) => {
|
|
110
|
-
if (x.eq(v))
|
|
111
|
-
return sqr(x);
|
|
112
|
-
const table = new Array(MUL_TABLE_SIZE);
|
|
113
|
-
table[0] = new BN(0);
|
|
114
|
-
for (let i = 1; i < MUL_TABLE_SIZE; i++) {
|
|
115
|
-
const lowBit = i & (-i);
|
|
116
|
-
const lowBitIdx = Math.log2(lowBit) | 0;
|
|
117
|
-
table[i] = table[i ^ lowBit].xor(x.ushln(lowBitIdx));
|
|
118
|
-
}
|
|
119
|
-
const vb = v.bitLength();
|
|
120
|
-
const acc = new BN(0);
|
|
121
|
-
for (let win = Math.ceil(vb / MUL_WINDOW) - 1; win >= 0; win--) {
|
|
122
|
-
let windowVal = 0;
|
|
123
|
-
for (let b = 0; b < MUL_WINDOW; b++) {
|
|
124
|
-
const bitPos = win * MUL_WINDOW + b;
|
|
125
|
-
if (bitPos < vb && v.testn(bitPos))
|
|
126
|
-
windowVal |= (1 << b);
|
|
127
|
-
}
|
|
128
|
-
acc.iushln(MUL_WINDOW).ixor(table[windowVal]);
|
|
129
|
-
}
|
|
130
|
-
return mod(acc);
|
|
131
|
-
};
|
|
132
|
-
const div = (x, v) => mul(x, invert(v));
|
|
133
|
-
const sqr = (x) => {
|
|
134
|
-
const bytes = x.toArray();
|
|
135
|
-
const out = new Uint8Array(bytes.length * 2);
|
|
136
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
137
|
-
const v = SQR_PRECOMP[bytes[i]];
|
|
138
|
-
out[2 * i] = (v >> 8) & 0xff;
|
|
139
|
-
out[2 * i + 1] = v & 0xff;
|
|
140
|
-
}
|
|
141
|
-
return mod(new BN(out));
|
|
142
|
-
};
|
|
143
|
-
const testBit = (x, i) => x.testn(i) ? 1 : 0;
|
|
144
|
-
const invBit = (x, i) => x.setn(i, !x.testn(i));
|
|
145
|
-
const sqrt = (x) => {
|
|
146
|
-
let r = mod(x);
|
|
147
|
-
for (let i = 0; i < m - 1; i++)
|
|
148
|
-
r = sqr(r);
|
|
149
|
-
return r;
|
|
150
|
-
};
|
|
151
|
-
const trace = (x) => {
|
|
152
|
-
let t = x;
|
|
153
|
-
for (let i = 1; i < m; i++)
|
|
154
|
-
t = sqr(t).ixor(x);
|
|
155
|
-
return testBit(t, 0);
|
|
156
|
-
};
|
|
157
|
-
const traceOnb = (x) => {
|
|
158
|
-
let t = 0;
|
|
159
|
-
for (let i = 0; i < m; i++)
|
|
160
|
-
t ^= testBit(x, i);
|
|
161
|
-
return t;
|
|
162
|
-
};
|
|
163
|
-
const htrace = (x) => {
|
|
164
|
-
let ht = x;
|
|
165
|
-
for (let i = 1; i <= Math.floor((m - 1) / 2); i++)
|
|
166
|
-
ht = sqr(sqr(ht)).ixor(x);
|
|
167
|
-
return ht;
|
|
168
|
-
};
|
|
169
|
-
const invert = (f) => {
|
|
170
|
-
let r = mod(f), s = modulo.clone();
|
|
171
|
-
let u = new BN(1), v = new BN(0);
|
|
172
|
-
while (r.bitLength() > 1) {
|
|
173
|
-
let j = s.bitLength() - r.bitLength();
|
|
174
|
-
if (j < 0) {
|
|
175
|
-
let buf = r;
|
|
176
|
-
r = s;
|
|
177
|
-
s = buf;
|
|
178
|
-
buf = u;
|
|
179
|
-
u = v;
|
|
180
|
-
v = buf;
|
|
181
|
-
j = -j;
|
|
182
|
-
}
|
|
183
|
-
s.ixor(r.ushln(j));
|
|
184
|
-
v.ixor(u.ushln(j));
|
|
185
|
-
}
|
|
186
|
-
return u;
|
|
187
|
-
};
|
|
188
|
-
const solve_quad = (v) => {
|
|
189
|
-
const a = mod(v), z = htrace(a);
|
|
190
|
-
if (sqr(z).ixor(z).cmp(a) == 0)
|
|
191
|
-
return mod(z);
|
|
192
|
-
throw new Error("squad eq fail: no square root exists");
|
|
193
|
-
};
|
|
194
|
-
const hashToField = (hash) => {
|
|
195
|
-
const h = new BN(hash);
|
|
196
|
-
return h.imaskn(Math.min(m, h.bitLength()));
|
|
197
|
-
};
|
|
198
|
-
const fromHexStringOrBytes = (v) => new BN(v, 16);
|
|
199
|
-
const toBytes = (x, length) => new Uint8Array(x.toArray("be", length));
|
|
200
|
-
return Object.freeze({
|
|
201
|
-
MODULO: modulo, LENGTH: Math.ceil(m / 8),
|
|
202
|
-
mod, mul, div, sqr, sqrt, invert,
|
|
203
|
-
solve_quad, trace, traceOnb, hashToField, invBit,
|
|
204
|
-
fromHexStringOrBytes, toBytes
|
|
205
|
-
});
|
|
206
|
-
};
|
|
File without changes
|