@li0ard/dstu 0.1.2 → 0.1.4
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/dstu4145/const.d.ts +4 -0
- package/dstu4145/ec/expand.js +17 -23
- package/dstu4145/ec/index.d.ts +4 -7
- package/dstu4145/ec/index.js +19 -25
- package/dstu4145/ec/math.d.ts +4 -7
- package/dstu4145/ec/math.js +146 -50
- package/dstu4145/ec/onb.d.ts +2 -2
- package/dstu4145/ec/onb.js +17 -18
- package/dstu4145/index.js +20 -22
- package/kupyna/index.d.ts +6 -6
- package/kupyna/index.js +6 -6
- package/package.json +3 -3
package/dstu4145/const.d.ts
CHANGED
|
@@ -8,9 +8,13 @@ export interface DSTUShortParameters {
|
|
|
8
8
|
m: number;
|
|
9
9
|
/** Polynomial coefficients */
|
|
10
10
|
ks: number[];
|
|
11
|
+
/** Parameters for Optimal normal basis (ONB) */
|
|
11
12
|
onb?: {
|
|
13
|
+
/** Compressed matrix for ONB multiplication */
|
|
12
14
|
matrix: string;
|
|
15
|
+
/** Roots in ONB for PB-irreducible */
|
|
13
16
|
root1: string;
|
|
17
|
+
/** Roots in PB for ONB-irreducible */
|
|
14
18
|
root2: string;
|
|
15
19
|
};
|
|
16
20
|
}
|
package/dstu4145/ec/expand.js
CHANGED
|
@@ -2,44 +2,38 @@ import { init_onb_parameters } from "./onb.js";
|
|
|
2
2
|
import { createField } from "./math.js";
|
|
3
3
|
/** Uncompress point without creating full API curve */
|
|
4
4
|
export const expandPoint = (xBytes, params) => {
|
|
5
|
-
const { m, ks, a } = params;
|
|
5
|
+
const { m, ks, a, onb } = params;
|
|
6
6
|
const field = createField(m, ks);
|
|
7
7
|
let converter;
|
|
8
|
-
if (
|
|
8
|
+
if (onb)
|
|
9
9
|
converter = init_onb_parameters(params);
|
|
10
10
|
const bInt = field.fromHexStringOrBytes(params.b);
|
|
11
|
-
const b = (
|
|
12
|
-
? converter.onbToPb(bInt)
|
|
13
|
-
: bInt;
|
|
11
|
+
const b = (onb && converter) ? converter.onbToPb(bInt) : bInt;
|
|
14
12
|
const x = field.fromHexStringOrBytes(xBytes);
|
|
15
|
-
const bit =
|
|
13
|
+
const bit = x.testn(0);
|
|
16
14
|
let xClean = x.clone();
|
|
17
|
-
xClean
|
|
18
|
-
const traceX =
|
|
15
|
+
xClean.setn(0, 0);
|
|
16
|
+
const traceX = onb ? field.traceOnb(xClean) : field.trace(xClean);
|
|
19
17
|
if ((traceX === 1 && a === 0) || (traceX === 0 && a === 1))
|
|
20
|
-
xClean
|
|
21
|
-
const
|
|
22
|
-
if (
|
|
18
|
+
xClean.setn(0, 1);
|
|
19
|
+
const xOut = xClean.clone();
|
|
20
|
+
if (onb && converter)
|
|
23
21
|
xClean = converter.onbToPb(xClean);
|
|
24
22
|
const x2 = field.sqr(xClean);
|
|
25
|
-
|
|
23
|
+
const rhs = field.mul(x2, xClean);
|
|
26
24
|
if (a === 1)
|
|
27
|
-
rhs
|
|
25
|
+
rhs.ixor(x2);
|
|
28
26
|
if (b)
|
|
29
|
-
rhs
|
|
30
|
-
const
|
|
31
|
-
const c = field.mul(rhs, x2inv);
|
|
32
|
-
let z = field.sqrt(c);
|
|
27
|
+
rhs.ixor(b);
|
|
28
|
+
const z = field.solve_quad(field.mul(rhs, field.invert(x2)));
|
|
33
29
|
const traceZ = field.trace(z);
|
|
34
|
-
if ((traceZ === 0 && bit
|
|
35
|
-
|
|
36
|
-
z = field.setBit(z, 0, 1 ^ currentBit);
|
|
37
|
-
}
|
|
30
|
+
if ((traceZ === 0 && bit) || (traceZ === 1 && !bit))
|
|
31
|
+
z.setn(0, !z.testn(0));
|
|
38
32
|
let y = field.mul(z, xClean);
|
|
39
|
-
if (
|
|
33
|
+
if (onb && converter)
|
|
40
34
|
y = converter.pbToOnb(y);
|
|
41
35
|
return {
|
|
42
|
-
x: field.toBytes(
|
|
36
|
+
x: field.toBytes(xOut, field.LENGTH),
|
|
43
37
|
y: field.toBytes(y, field.LENGTH)
|
|
44
38
|
};
|
|
45
39
|
};
|
package/dstu4145/ec/index.d.ts
CHANGED
|
@@ -8,16 +8,13 @@ export declare const binaryWeierstrass: (parameters: DSTUParameters) => Readonly
|
|
|
8
8
|
mod: (f: BN) => BN;
|
|
9
9
|
mul: (x: BN, v: BN) => BN;
|
|
10
10
|
sqr: (x: BN) => BN;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
shiftLeft: (x: BN, n: number) => BN;
|
|
11
|
+
invert: (f: BN) => BN;
|
|
12
|
+
solve_quad: (v: BN) => BN;
|
|
14
13
|
trace: (x: BN) => 0 | 1;
|
|
15
14
|
traceOnb: (x: BN) => 0 | 1;
|
|
16
|
-
|
|
17
|
-
sqrt: (v: BN) => BN;
|
|
15
|
+
hashToField: (hash: TArg<Uint8Array>) => BN;
|
|
18
16
|
fromHexStringOrBytes: (v: string | Uint8Array) => BN;
|
|
19
17
|
toBytes: (x: BN, length?: number) => TRet<Uint8Array>;
|
|
20
|
-
hashToField: (hash: TArg<Uint8Array>) => BN;
|
|
21
18
|
}>;
|
|
22
19
|
Point: {
|
|
23
20
|
new (x: BN, y: BN): {
|
|
@@ -137,6 +134,7 @@ export declare const binaryWeierstrass: (parameters: DSTUParameters) => Readonly
|
|
|
137
134
|
};
|
|
138
135
|
};
|
|
139
136
|
ORDER: BN;
|
|
137
|
+
MASK: number;
|
|
140
138
|
parameters: DSTUParameters;
|
|
141
139
|
lengths: Readonly<{
|
|
142
140
|
fieldByteLength: number;
|
|
@@ -145,7 +143,6 @@ export declare const binaryWeierstrass: (parameters: DSTUParameters) => Readonly
|
|
|
145
143
|
signatureByteLength: number;
|
|
146
144
|
}>;
|
|
147
145
|
isOnb: boolean;
|
|
148
|
-
truncate: (x: BN) => BN;
|
|
149
146
|
toInternalField: (f: BN) => BN;
|
|
150
147
|
toExternalField: (f: BN) => BN;
|
|
151
148
|
}>;
|
package/dstu4145/ec/index.js
CHANGED
|
@@ -43,8 +43,8 @@ export const binaryWeierstrass = (parameters) => {
|
|
|
43
43
|
lbd = field.mul(tmp, field.invert(tmp2));
|
|
44
44
|
x2 = field.sqr(lbd);
|
|
45
45
|
if (parameters.a === 1)
|
|
46
|
-
x2
|
|
47
|
-
x2
|
|
46
|
+
x2.setn(0, !x2.testn(0));
|
|
47
|
+
x2.ixor(lbd).ixor(x0).ixor(x1);
|
|
48
48
|
}
|
|
49
49
|
else {
|
|
50
50
|
if (y1.cmp(y0) !== 0)
|
|
@@ -54,8 +54,8 @@ export const binaryWeierstrass = (parameters) => {
|
|
|
54
54
|
lbd = x1.xor(field.mul(p.y, field.invert(p.x)));
|
|
55
55
|
x2 = field.sqr(lbd);
|
|
56
56
|
if (parameters.a === 1)
|
|
57
|
-
x2
|
|
58
|
-
x2
|
|
57
|
+
x2.setn(0, !x2.testn(0));
|
|
58
|
+
x2.ixor(lbd);
|
|
59
59
|
}
|
|
60
60
|
const y2 = field.mul(lbd, x1.xor(x2)).xor(x2).xor(y1);
|
|
61
61
|
pz.x = x2;
|
|
@@ -81,7 +81,7 @@ export const binaryWeierstrass = (parameters) => {
|
|
|
81
81
|
isZero() { return this.x.isZero() && this.y.isZero(); }
|
|
82
82
|
compress() {
|
|
83
83
|
const tmp = field.mul(field.invert(this.x), this.y);
|
|
84
|
-
return
|
|
84
|
+
return toExternal(this.x).setn(0, field.trace(tmp));
|
|
85
85
|
}
|
|
86
86
|
toBytes(isCompressed = false) {
|
|
87
87
|
if (isCompressed)
|
|
@@ -138,8 +138,7 @@ export const binaryWeierstrass = (parameters) => {
|
|
|
138
138
|
R = R.timesPow2(zeroes);
|
|
139
139
|
}
|
|
140
140
|
while (i > 0) {
|
|
141
|
-
const wi = wnaf[--i];
|
|
142
|
-
const digit = wi >> 16;
|
|
141
|
+
const wi = wnaf[--i], digit = wi >> 16;
|
|
143
142
|
const table = digit < 0 ? neg : pos;
|
|
144
143
|
R = R.double().add(table[Math.abs(digit) >>> 1]);
|
|
145
144
|
R = R.timesPow2(wi & 0xffff);
|
|
@@ -147,26 +146,23 @@ export const binaryWeierstrass = (parameters) => {
|
|
|
147
146
|
return R;
|
|
148
147
|
}
|
|
149
148
|
static expand(xExt) {
|
|
150
|
-
const bit =
|
|
151
|
-
|
|
152
|
-
xCleanExt
|
|
153
|
-
const a = parameters.a ?? 0;
|
|
149
|
+
const bit = xExt.testn(0);
|
|
150
|
+
const xCleanExt = xExt.clone();
|
|
151
|
+
xCleanExt.setn(0, 0);
|
|
154
152
|
const traceX = onb ? field.traceOnb(xCleanExt) : field.trace(xCleanExt);
|
|
155
|
-
if ((traceX === 1 && a === 0) || (traceX === 0 && a === 1))
|
|
156
|
-
xCleanExt
|
|
153
|
+
if ((traceX === 1 && parameters.a === 0) || (traceX === 0 && parameters.a === 1))
|
|
154
|
+
xCleanExt.setn(0, 1);
|
|
157
155
|
const xClean = toInternal(xCleanExt);
|
|
158
156
|
const x2 = field.sqr(xClean);
|
|
159
|
-
|
|
160
|
-
if (a === 1)
|
|
161
|
-
rhs
|
|
157
|
+
const rhs = field.mul(x2, xClean);
|
|
158
|
+
if (parameters.a === 1)
|
|
159
|
+
rhs.ixor(x2);
|
|
162
160
|
if (b)
|
|
163
|
-
rhs
|
|
164
|
-
|
|
161
|
+
rhs.ixor(b);
|
|
162
|
+
const z = field.solve_quad(field.mul(rhs, field.invert(x2)));
|
|
165
163
|
const traceZ = field.trace(z);
|
|
166
|
-
if ((traceZ === 0 && bit
|
|
167
|
-
|
|
168
|
-
z = field.setBit(z, 0, 1 ^ currentBit);
|
|
169
|
-
}
|
|
164
|
+
if ((traceZ === 0 && bit) || (traceZ === 1 && !bit))
|
|
165
|
+
z.setn(0, !z.testn(0));
|
|
170
166
|
return new Point(xClean, field.mul(z, xClean));
|
|
171
167
|
}
|
|
172
168
|
static fromBytes(bytes) {
|
|
@@ -178,16 +174,14 @@ export const binaryWeierstrass = (parameters) => {
|
|
|
178
174
|
throw new Error(`Invalid bytes length. Must be ${pointByteLength} for uncompressed and ${field.LENGTH} for compressed`);
|
|
179
175
|
}
|
|
180
176
|
}
|
|
181
|
-
const k = order.bitLength() - 1;
|
|
182
|
-
const truncate = (x) => x.maskn(k);
|
|
183
177
|
// Precompute
|
|
184
178
|
Point.BASE.mulWnaf(new BN(3));
|
|
185
179
|
return Object.freeze({
|
|
186
180
|
Field: field, Point,
|
|
187
181
|
ORDER: order,
|
|
182
|
+
MASK: order.bitLength() - 1,
|
|
188
183
|
parameters, lengths,
|
|
189
184
|
isOnb: !!onb,
|
|
190
|
-
truncate,
|
|
191
185
|
toInternalField: toInternal,
|
|
192
186
|
toExternalField: toExternal
|
|
193
187
|
});
|
package/dstu4145/ec/math.d.ts
CHANGED
|
@@ -2,21 +2,18 @@ import type { TArg, TRet } from "@noble/hashes/utils.js";
|
|
|
2
2
|
import BN from "bn.js";
|
|
3
3
|
/** Compute curve modulo */
|
|
4
4
|
export declare const computeMod: (m: number, ks: number[]) => BN;
|
|
5
|
-
/** Create GF(2^m)
|
|
5
|
+
/** Create `GF(2^m)` field */
|
|
6
6
|
export declare const createField: (m: number, ks: number[]) => Readonly<{
|
|
7
7
|
MODULO: BN;
|
|
8
8
|
LENGTH: number;
|
|
9
9
|
mod: (f: BN) => BN;
|
|
10
10
|
mul: (x: BN, v: BN) => BN;
|
|
11
11
|
sqr: (x: BN) => BN;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
shiftLeft: (x: BN, n: number) => BN;
|
|
12
|
+
invert: (f: BN) => BN;
|
|
13
|
+
solve_quad: (v: BN) => BN;
|
|
15
14
|
trace: (x: BN) => 0 | 1;
|
|
16
15
|
traceOnb: (x: BN) => 0 | 1;
|
|
17
|
-
|
|
18
|
-
sqrt: (v: BN) => BN;
|
|
16
|
+
hashToField: (hash: TArg<Uint8Array>) => BN;
|
|
19
17
|
fromHexStringOrBytes: (v: string | Uint8Array) => BN;
|
|
20
18
|
toBytes: (x: BN, length?: number) => TRet<Uint8Array>;
|
|
21
|
-
hashToField: (hash: TArg<Uint8Array>) => BN;
|
|
22
19
|
}>;
|
package/dstu4145/ec/math.js
CHANGED
|
@@ -1,4 +1,75 @@
|
|
|
1
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 = (arr, 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 < arr.length)
|
|
62
|
+
arr[wordShift] ^= (val >>> 0);
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
const lo = (val << bitShift) >>> 0;
|
|
66
|
+
const hi = (val >>> (WORD_BITS - bitShift)) >>> 0;
|
|
67
|
+
if (wordShift < arr.length)
|
|
68
|
+
arr[wordShift] ^= lo;
|
|
69
|
+
if (wordShift + 1 < arr.length)
|
|
70
|
+
arr[wordShift + 1] ^= hi;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
2
73
|
/** Compute curve modulo */
|
|
3
74
|
export const computeMod = (m, ks) => {
|
|
4
75
|
const modulo = new BN(0);
|
|
@@ -8,47 +79,69 @@ export const computeMod = (m, ks) => {
|
|
|
8
79
|
modulo.setn(i, 1);
|
|
9
80
|
return modulo;
|
|
10
81
|
};
|
|
11
|
-
/** Create GF(2^m)
|
|
82
|
+
/** Create `GF(2^m)` field */
|
|
12
83
|
export const createField = (m, ks) => {
|
|
13
|
-
const modulo = computeMod(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
|
+
if (f.bitLength() <= m)
|
|
87
|
+
return f;
|
|
88
|
+
const bag = f;
|
|
89
|
+
while (bag.bitLength() > m)
|
|
90
|
+
bag.ixor(modulo.ushln(bag.bitLength() - vl));
|
|
91
|
+
return bag;
|
|
92
|
+
};
|
|
14
93
|
const mod = (f) => {
|
|
15
|
-
|
|
16
|
-
if (cmp === 0)
|
|
17
|
-
return new BN(0);
|
|
18
|
-
if (cmp < 0)
|
|
94
|
+
if (f.bitLength() <= m)
|
|
19
95
|
return f.clone();
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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);
|
|
26
106
|
}
|
|
107
|
+
return bMod(le2BN(words));
|
|
27
108
|
};
|
|
28
109
|
const mul = (x, v) => {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
110
|
+
const table = new Array(MUL_TABLE_SIZE);
|
|
111
|
+
table[0] = new BN(0);
|
|
112
|
+
for (let i = 1; i < MUL_TABLE_SIZE; i++) {
|
|
113
|
+
const lowBit = i & (-i);
|
|
114
|
+
const lowBitIdx = Math.log2(lowBit) | 0;
|
|
115
|
+
table[i] = table[i ^ lowBit].xor(x.ushln(lowBitIdx));
|
|
116
|
+
}
|
|
117
|
+
const vb = v.bitLength();
|
|
118
|
+
const acc = new BN(0);
|
|
119
|
+
for (let win = Math.ceil(vb / MUL_WINDOW) - 1; win >= 0; win--) {
|
|
120
|
+
let windowVal = 0;
|
|
121
|
+
for (let b = 0; b < MUL_WINDOW; b++) {
|
|
122
|
+
const bitPos = win * MUL_WINDOW + b;
|
|
123
|
+
if (bitPos < vb && v.testn(bitPos))
|
|
124
|
+
windowVal |= (1 << b);
|
|
125
|
+
}
|
|
126
|
+
acc.iushln(MUL_WINDOW).ixor(table[windowVal]);
|
|
36
127
|
}
|
|
37
|
-
return mod(
|
|
128
|
+
return mod(acc);
|
|
38
129
|
};
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
130
|
+
const sqr = (x) => {
|
|
131
|
+
const bytes = x.toArray();
|
|
132
|
+
const out = new Uint8Array(bytes.length * 2);
|
|
133
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
134
|
+
const v = SQR_PRECOMP[bytes[i]];
|
|
135
|
+
out[2 * i] = (v >> 8) & 0xff;
|
|
136
|
+
out[2 * i + 1] = v & 0xff;
|
|
137
|
+
}
|
|
138
|
+
return mod(new BN(out));
|
|
47
139
|
};
|
|
140
|
+
const testBit = (x, i) => x.testn(i) ? 1 : 0;
|
|
48
141
|
const trace = (x) => {
|
|
49
142
|
let t = x;
|
|
50
143
|
for (let i = 1; i < m; i++)
|
|
51
|
-
t = sqr(t).
|
|
144
|
+
t = sqr(t).ixor(x);
|
|
52
145
|
return testBit(t, 0);
|
|
53
146
|
};
|
|
54
147
|
const traceOnb = (x) => {
|
|
@@ -57,44 +150,47 @@ export const createField = (m, ks) => {
|
|
|
57
150
|
t ^= testBit(x, i);
|
|
58
151
|
return t;
|
|
59
152
|
};
|
|
153
|
+
const htrace = (x) => {
|
|
154
|
+
let ht = x;
|
|
155
|
+
for (let i = 1; i <= Math.floor((m - 1) / 2); i++)
|
|
156
|
+
ht = sqr(sqr(ht)).ixor(x);
|
|
157
|
+
return ht;
|
|
158
|
+
};
|
|
60
159
|
const invert = (f) => {
|
|
61
|
-
let r = mod(f), s = modulo;
|
|
160
|
+
let r = mod(f), s = modulo.clone();
|
|
62
161
|
let u = new BN(1), v = new BN(0);
|
|
63
162
|
while (r.bitLength() > 1) {
|
|
64
163
|
let j = s.bitLength() - r.bitLength();
|
|
65
164
|
if (j < 0) {
|
|
66
|
-
|
|
67
|
-
|
|
165
|
+
let buf = r;
|
|
166
|
+
r = s;
|
|
167
|
+
s = buf;
|
|
168
|
+
buf = u;
|
|
169
|
+
u = v;
|
|
170
|
+
v = buf;
|
|
68
171
|
j = -j;
|
|
69
172
|
}
|
|
70
|
-
s
|
|
71
|
-
v
|
|
173
|
+
s.ixor(r.ushln(j));
|
|
174
|
+
v.ixor(u.ushln(j));
|
|
72
175
|
}
|
|
73
176
|
return u;
|
|
74
177
|
};
|
|
75
|
-
const
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
let z = a.clone();
|
|
79
|
-
for (let idx = 1; idx <= range_to; idx++) {
|
|
80
|
-
z = sqr(sqr(z)).xor(a);
|
|
81
|
-
}
|
|
82
|
-
const w = sqr(z).xor(z);
|
|
83
|
-
if (w.cmp(a) == 0)
|
|
178
|
+
const solve_quad = (v) => {
|
|
179
|
+
const a = mod(v), z = htrace(a);
|
|
180
|
+
if (sqr(z).ixor(z).cmp(a) == 0)
|
|
84
181
|
return mod(z);
|
|
85
182
|
throw new Error("squad eq fail: no square root exists");
|
|
86
183
|
};
|
|
87
184
|
const hashToField = (hash) => {
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
return bn.maskn(k);
|
|
185
|
+
const h = new BN(hash);
|
|
186
|
+
return h.imaskn(Math.min(m, h.bitLength()));
|
|
91
187
|
};
|
|
92
188
|
const fromHexStringOrBytes = (v) => new BN(v, 16);
|
|
93
189
|
const toBytes = (x, length) => new Uint8Array(x.toArray("be", length));
|
|
94
190
|
return Object.freeze({
|
|
95
|
-
MODULO: modulo,
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
fromHexStringOrBytes, toBytes
|
|
191
|
+
MODULO: modulo, LENGTH: Math.ceil(m / 8),
|
|
192
|
+
mod, mul, sqr, invert, solve_quad,
|
|
193
|
+
trace, traceOnb, hashToField,
|
|
194
|
+
fromHexStringOrBytes, toBytes
|
|
99
195
|
});
|
|
100
196
|
};
|
package/dstu4145/ec/onb.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import BN from "bn.js";
|
|
2
2
|
import type { DSTUShortParameters } from "../const.js";
|
|
3
|
-
export declare const init_onb_parameters: (
|
|
3
|
+
export declare const init_onb_parameters: (parameters: DSTUShortParameters) => Readonly<{
|
|
4
4
|
onbToPb: (x: BN) => BN;
|
|
5
5
|
pbToOnb: (x: BN) => BN;
|
|
6
|
-
}
|
|
6
|
+
}>;
|
package/dstu4145/ec/onb.js
CHANGED
|
@@ -2,25 +2,24 @@ import BN from "bn.js";
|
|
|
2
2
|
import { hexToBytes } from "@noble/hashes/utils.js";
|
|
3
3
|
import { createField } from "./math.js";
|
|
4
4
|
const decompress_matrix = (compress_mulp, mulp) => {
|
|
5
|
-
const
|
|
6
|
-
const mlen = Math.ceil(bitLen / 9);
|
|
5
|
+
const mlen = Math.ceil(compress_mulp.bitLength() / 9);
|
|
7
6
|
if (mlen === 0)
|
|
8
7
|
return;
|
|
9
|
-
|
|
8
|
+
const temp = compress_mulp;
|
|
10
9
|
const mask = new BN(0x1ff);
|
|
11
10
|
for (let j = mlen - 1; j >= 0; j--) {
|
|
12
11
|
mulp[j] = temp.and(mask).toNumber();
|
|
13
|
-
temp
|
|
12
|
+
temp.ishrn(9);
|
|
14
13
|
}
|
|
15
14
|
};
|
|
16
|
-
export const init_onb_parameters = (
|
|
17
|
-
if (!
|
|
15
|
+
export const init_onb_parameters = (parameters) => {
|
|
16
|
+
if (!parameters.onb)
|
|
18
17
|
throw new Error("Invalid curve: Curve doesn't support ONB");
|
|
19
|
-
const { m } =
|
|
18
|
+
const { m, onb, ks } = parameters;
|
|
20
19
|
const mulp = new Uint16Array(2 * m - 1);
|
|
21
|
-
const compress_mulp = new BN(hexToBytes(
|
|
20
|
+
const compress_mulp = new BN(hexToBytes(onb.matrix), "le");
|
|
22
21
|
decompress_matrix(compress_mulp, mulp);
|
|
23
|
-
const root1 = new BN(hexToBytes(
|
|
22
|
+
const root1 = new BN(hexToBytes(onb.root1), "le"), root2 = new BN(hexToBytes(onb.root2), "le");
|
|
24
23
|
const multiplyOnb = (x, y) => {
|
|
25
24
|
const r = new BN(0);
|
|
26
25
|
const xb = (pos) => (x.testn(pos % m) ? 1 : 0);
|
|
@@ -39,7 +38,7 @@ export const init_onb_parameters = (curve) => {
|
|
|
39
38
|
}
|
|
40
39
|
return r;
|
|
41
40
|
};
|
|
42
|
-
const reverseBits = (x
|
|
41
|
+
const reverseBits = (x) => {
|
|
43
42
|
const r = new BN(0);
|
|
44
43
|
for (let i = 0; i < m; i++)
|
|
45
44
|
if (x.testn(i))
|
|
@@ -48,28 +47,28 @@ export const init_onb_parameters = (curve) => {
|
|
|
48
47
|
};
|
|
49
48
|
const toPb = new Array(m);
|
|
50
49
|
toPb[0] = root1.clone();
|
|
51
|
-
const { mul } = createField(m,
|
|
50
|
+
const { mul } = createField(m, ks);
|
|
52
51
|
for (let i = 1; i < m; i++)
|
|
53
52
|
toPb[i] = mul(toPb[i - 1], toPb[i - 1]);
|
|
54
53
|
const _toOnb = new Array(m);
|
|
55
54
|
_toOnb[0] = new BN(1).shln(m).subn(1);
|
|
56
55
|
for (let i = 1; i < m; i++)
|
|
57
56
|
_toOnb[i] = multiplyOnb(_toOnb[i - 1], root2);
|
|
58
|
-
const toOnb = _toOnb.map(
|
|
59
|
-
return {
|
|
57
|
+
const toOnb = _toOnb.map(reverseBits);
|
|
58
|
+
return Object.freeze({
|
|
60
59
|
onbToPb: (x) => {
|
|
61
|
-
|
|
60
|
+
const r = new BN(0);
|
|
62
61
|
for (let p = 0; p < m; p++)
|
|
63
62
|
if (x.testn(p))
|
|
64
|
-
r
|
|
63
|
+
r.ixor(toPb[m - 1 - p]);
|
|
65
64
|
return r;
|
|
66
65
|
},
|
|
67
66
|
pbToOnb: (x) => {
|
|
68
|
-
|
|
67
|
+
const r = new BN(0);
|
|
69
68
|
for (let j = 0; j < m; j++)
|
|
70
69
|
if (x.testn(j))
|
|
71
|
-
r
|
|
70
|
+
r.ixor(toOnb[j]);
|
|
72
71
|
return r;
|
|
73
72
|
}
|
|
74
|
-
};
|
|
73
|
+
});
|
|
75
74
|
};
|
package/dstu4145/index.js
CHANGED
|
@@ -5,51 +5,49 @@ import BN from "bn.js";
|
|
|
5
5
|
/** Create DSTU 4145-2002 signer (Big-Endian) */
|
|
6
6
|
export const dstu4145 = (parameters) => {
|
|
7
7
|
const curve = binaryWeierstrass(parameters);
|
|
8
|
-
const { Field, Point,
|
|
8
|
+
const { Field, Point, MASK, lengths } = curve;
|
|
9
9
|
const getPublicKey = (secretKey, isCompressed = false) => Point.BASE.mul(Field.fromHexStringOrBytes(secretKey)).negate().toBytes(isCompressed);
|
|
10
|
-
const randomPrivateKey = () => Field.toBytes(
|
|
10
|
+
const randomPrivateKey = () => Field.toBytes(new BN(randomBytes(lengths.scalarByteLength)).imaskn(MASK), lengths.scalarByteLength);
|
|
11
11
|
const computePresign = (rand) => {
|
|
12
|
-
const e =
|
|
12
|
+
const e = (rand
|
|
13
13
|
? new BN(rand)
|
|
14
|
-
: new BN(randomBytes(
|
|
14
|
+
: new BN(randomBytes(lengths.scalarByteLength))).imaskn(MASK);
|
|
15
15
|
if (rand && e.isZero())
|
|
16
16
|
throw new Error("Invalid custom rand for presign (rand = 0)");
|
|
17
17
|
if (e.isZero())
|
|
18
18
|
return computePresign(rand);
|
|
19
|
-
const
|
|
20
|
-
if (
|
|
19
|
+
const Fe = Point.BASE.mul(e).x;
|
|
20
|
+
if (Fe.isZero())
|
|
21
21
|
return computePresign(rand);
|
|
22
|
-
return { Fe
|
|
22
|
+
return { Fe, e };
|
|
23
23
|
};
|
|
24
24
|
const prepareHash = (digest) => {
|
|
25
|
-
let h = curve.toInternalField(
|
|
25
|
+
let h = curve.toInternalField(Field.hashToField(digest));
|
|
26
26
|
if (h.isZero())
|
|
27
27
|
h = new BN(1);
|
|
28
28
|
return h;
|
|
29
29
|
};
|
|
30
30
|
const sign = (secretKey, digest, rand) => {
|
|
31
|
-
const d = new BN(secretKey)
|
|
32
|
-
const
|
|
33
|
-
const { Fe, e } = computePresign(rand);
|
|
34
|
-
const r = truncate(curve.toExternalField(Field.mul(h, Fe)));
|
|
31
|
+
const d = new BN(secretKey), h = prepareHash(digest), { Fe, e } = computePresign(rand);
|
|
32
|
+
const r = curve.toExternalField(Field.mul(h, Fe)).imaskn(MASK);
|
|
35
33
|
if (r.isZero())
|
|
36
34
|
return sign(secretKey, digest, rand);
|
|
37
|
-
const s = e.add(d.mul(r)).mod(curve.ORDER);
|
|
35
|
+
const s = e.add(d.mul(r)).mod(curve.ORDER);
|
|
38
36
|
if (s.isZero())
|
|
39
37
|
return sign(secretKey, digest, rand);
|
|
40
|
-
return concatBytes(Field.toBytes(s,
|
|
38
|
+
return concatBytes(Field.toBytes(s, lengths.scalarByteLength), Field.toBytes(r, lengths.scalarByteLength));
|
|
41
39
|
};
|
|
42
40
|
const verify = (publicKey, digest, signature) => {
|
|
43
|
-
if (signature.length !=
|
|
41
|
+
if (signature.length != lengths.signatureByteLength)
|
|
44
42
|
throw new Error("Invalid signature length");
|
|
45
43
|
const Q = Point.fromBytes(publicKey);
|
|
46
|
-
const
|
|
47
|
-
if (
|
|
44
|
+
const s = Field.fromHexStringOrBytes(signature.subarray(0, lengths.scalarByteLength)), r = Field.fromHexStringOrBytes(signature.subarray(lengths.scalarByteLength));
|
|
45
|
+
if (s.isZero() || s.gte(curve.ORDER) || r.isZero() || r.gte(curve.ORDER))
|
|
48
46
|
return false;
|
|
49
|
-
const h = prepareHash(digest);
|
|
50
|
-
const R = Point.BASE.mulWnaf(
|
|
51
|
-
const y =
|
|
52
|
-
return y.eq(
|
|
47
|
+
const h = prepareHash(digest);
|
|
48
|
+
const R = Point.BASE.mulWnaf(s).add(Q.mulWnaf(r));
|
|
49
|
+
const y = curve.toExternalField(Field.mul(h, R.x)).imaskn(MASK);
|
|
50
|
+
return y.eq(r);
|
|
53
51
|
};
|
|
54
52
|
const keygen = (isCompressed = false) => {
|
|
55
53
|
const secretKey = randomPrivateKey();
|
|
@@ -61,7 +59,7 @@ export const dstu4145 = (parameters) => {
|
|
|
61
59
|
sign,
|
|
62
60
|
verify,
|
|
63
61
|
keygen,
|
|
64
|
-
lengths
|
|
62
|
+
lengths
|
|
65
63
|
});
|
|
66
64
|
};
|
|
67
65
|
export * from "./const.js";
|
package/kupyna/index.d.ts
CHANGED
|
@@ -61,28 +61,28 @@ export declare class Kupyna512 extends Kupyna<Kupyna512> {
|
|
|
61
61
|
export declare class Kupyna48 extends KupynaDerived<Kupyna256> {
|
|
62
62
|
/** Kupyna 48 bit version */
|
|
63
63
|
constructor();
|
|
64
|
-
/** Create hash instance */
|
|
65
|
-
static create(): Kupyna48;
|
|
66
64
|
_cloneInto(to?: Kupyna48): Kupyna48;
|
|
67
65
|
clone(): Kupyna48;
|
|
66
|
+
/** Create hash instance */
|
|
67
|
+
static create(): Kupyna48;
|
|
68
68
|
}
|
|
69
69
|
/** Kupyna 304 bit version */
|
|
70
70
|
export declare class Kupyna304 extends KupynaDerived<Kupyna512> {
|
|
71
71
|
/** Kupyna 304 bit version */
|
|
72
72
|
constructor();
|
|
73
|
-
/** Create hash instance */
|
|
74
|
-
static create(): Kupyna304;
|
|
75
73
|
_cloneInto(to?: Kupyna304): Kupyna304;
|
|
76
74
|
clone(): Kupyna304;
|
|
75
|
+
/** Create hash instance */
|
|
76
|
+
static create(): Kupyna304;
|
|
77
77
|
}
|
|
78
78
|
/** Kupyna 384 bit version */
|
|
79
79
|
export declare class Kupyna384 extends KupynaDerived<Kupyna512> {
|
|
80
80
|
/** Kupyna 384 bit version */
|
|
81
81
|
constructor();
|
|
82
|
-
/** Create hash instance */
|
|
83
|
-
static create(): Kupyna384;
|
|
84
82
|
_cloneInto(to?: Kupyna384): Kupyna384;
|
|
85
83
|
clone(): Kupyna384;
|
|
84
|
+
/** Create hash instance */
|
|
85
|
+
static create(): Kupyna384;
|
|
86
86
|
}
|
|
87
87
|
/** Kupyna 48 bit version */
|
|
88
88
|
export declare const kupyna48: {
|
package/kupyna/index.js
CHANGED
|
@@ -185,40 +185,40 @@ export class Kupyna512 extends Kupyna {
|
|
|
185
185
|
export class Kupyna48 extends KupynaDerived {
|
|
186
186
|
/** Kupyna 48 bit version */
|
|
187
187
|
constructor() { super(Kupyna256.create, -6); }
|
|
188
|
-
/** Create hash instance */
|
|
189
|
-
static create() { return new Kupyna48(); }
|
|
190
188
|
_cloneInto(to) {
|
|
191
189
|
to ||= new Kupyna48();
|
|
192
190
|
to.buffer = new Uint8Array(this.buffer);
|
|
193
191
|
return to;
|
|
194
192
|
}
|
|
195
193
|
clone() { return this._cloneInto(); }
|
|
194
|
+
/** Create hash instance */
|
|
195
|
+
static create() { return new Kupyna48(); }
|
|
196
196
|
}
|
|
197
197
|
/** Kupyna 304 bit version */
|
|
198
198
|
export class Kupyna304 extends KupynaDerived {
|
|
199
199
|
/** Kupyna 304 bit version */
|
|
200
200
|
constructor() { super(Kupyna512.create, -38); }
|
|
201
|
-
/** Create hash instance */
|
|
202
|
-
static create() { return new Kupyna304(); }
|
|
203
201
|
_cloneInto(to) {
|
|
204
202
|
to ||= new Kupyna304();
|
|
205
203
|
to.buffer = new Uint8Array(this.buffer);
|
|
206
204
|
return to;
|
|
207
205
|
}
|
|
208
206
|
clone() { return this._cloneInto(); }
|
|
207
|
+
/** Create hash instance */
|
|
208
|
+
static create() { return new Kupyna304(); }
|
|
209
209
|
}
|
|
210
210
|
/** Kupyna 384 bit version */
|
|
211
211
|
export class Kupyna384 extends KupynaDerived {
|
|
212
212
|
/** Kupyna 384 bit version */
|
|
213
213
|
constructor() { super(Kupyna512.create, -48); }
|
|
214
|
-
/** Create hash instance */
|
|
215
|
-
static create() { return new Kupyna384(); }
|
|
216
214
|
_cloneInto(to) {
|
|
217
215
|
to ||= new Kupyna384();
|
|
218
216
|
to.buffer = new Uint8Array(this.buffer);
|
|
219
217
|
return to;
|
|
220
218
|
}
|
|
221
219
|
clone() { return this._cloneInto(); }
|
|
220
|
+
/** Create hash instance */
|
|
221
|
+
static create() { return new Kupyna384(); }
|
|
222
222
|
}
|
|
223
223
|
/** Kupyna 48 bit version */
|
|
224
224
|
export const kupyna48 = createHasher(Kupyna48.create);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@li0ard/dstu",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"types": "index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"docs": "typedoc --options .config/typedoc/config.cjs"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@li0ard/gost": "^0.2.
|
|
33
|
+
"@li0ard/gost": "^0.2.2",
|
|
34
34
|
"@peculiar/asn1-schema": "^2.8.0",
|
|
35
35
|
"@peculiar/asn1-x509": "^2.8.0",
|
|
36
36
|
"@types/bn.js": "^5.2.0",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"typescript": "^5"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@noble/hashes": "^2.
|
|
44
|
+
"@noble/hashes": "^2.3.0",
|
|
45
45
|
"bn.js": "^5.2.3"
|
|
46
46
|
},
|
|
47
47
|
"files": ["**/*.js", "**/*.d.ts"],
|