@li0ard/dstu 0.1.1 → 0.1.3
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 +26 -116
- package/dstu4145/ec/index.d.ts +52 -725
- package/dstu4145/ec/index.js +69 -180
- package/dstu4145/ec/math.d.ts +21 -0
- package/dstu4145/ec/math.js +222 -0
- package/dstu4145/ec/onb.d.ts +2 -2
- package/dstu4145/ec/onb.js +12 -38
- package/dstu4145/index.js +25 -30
- package/kupyna/index.d.ts +6 -6
- package/kupyna/index.js +6 -6
- package/package.json +2 -2
package/dstu4145/ec/index.js
CHANGED
|
@@ -2,123 +2,27 @@ import { concatBytes } from "@noble/hashes/utils.js";
|
|
|
2
2
|
import BN from "bn.js";
|
|
3
3
|
import { bitLength, getWindowSize, windowNaf } from "./wnaf.js";
|
|
4
4
|
import { init_onb_parameters } from "./onb.js";
|
|
5
|
-
|
|
6
|
-
const modulo = new BN(0);
|
|
7
|
-
modulo.setn(m, 1);
|
|
8
|
-
modulo.setn(0, 1);
|
|
9
|
-
for (const i of ks)
|
|
10
|
-
modulo.setn(i, 1);
|
|
11
|
-
return modulo;
|
|
12
|
-
};
|
|
5
|
+
import { createField } from "./math.js";
|
|
13
6
|
export const binaryWeierstrass = (parameters) => {
|
|
14
|
-
|
|
15
|
-
class Field {
|
|
16
|
-
value;
|
|
17
|
-
constructor(value = new BN(0)) {
|
|
18
|
-
this.value = value;
|
|
19
|
-
}
|
|
20
|
-
compare(v) { return this.value.cmp(v.value); }
|
|
21
|
-
clone() { return new Field(this.value.clone()); }
|
|
22
|
-
getLength() { return this.value.bitLength(); }
|
|
23
|
-
testBit(i) { return this.value.testn(i) ? 1 : 0; }
|
|
24
|
-
setBit(i, v) { this.value = this.value.setn(i, v == 1); }
|
|
25
|
-
add(v) { return new Field(this.value.xor(v.value)); }
|
|
26
|
-
is0() { return this.value.isZero(); }
|
|
27
|
-
shiftLeft(n) {
|
|
28
|
-
if (n < 0)
|
|
29
|
-
throw new Error("Shift amount cannot be negative.");
|
|
30
|
-
return new Field(this.value.ushln(n));
|
|
31
|
-
}
|
|
32
|
-
mod() {
|
|
33
|
-
const cmp = this.compare(modulo);
|
|
34
|
-
if (cmp === 0)
|
|
35
|
-
return Field.get0();
|
|
36
|
-
if (cmp < 0)
|
|
37
|
-
return this.clone();
|
|
38
|
-
let bag = this;
|
|
39
|
-
const vl = modulo.getLength();
|
|
40
|
-
while (true) {
|
|
41
|
-
bag = bag.add(modulo.shiftLeft(bag.getLength() - vl));
|
|
42
|
-
if (bag.getLength() < vl)
|
|
43
|
-
return bag;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
mul(v) {
|
|
47
|
-
let bag = Field.get0();
|
|
48
|
-
let shift = this;
|
|
49
|
-
const vLen = v.getLength();
|
|
50
|
-
for (let i = 0; i < vLen; i++) {
|
|
51
|
-
if (v.testBit(i) == 1)
|
|
52
|
-
bag = bag.add(shift);
|
|
53
|
-
shift = shift.shiftLeft(1);
|
|
54
|
-
}
|
|
55
|
-
return bag.mod();
|
|
56
|
-
}
|
|
57
|
-
trace() {
|
|
58
|
-
let t = this;
|
|
59
|
-
for (let i = 1; i < parameters.m; i++)
|
|
60
|
-
t = t.mul(t).add(this);
|
|
61
|
-
return t.testBit(0);
|
|
62
|
-
}
|
|
63
|
-
traceOnb() {
|
|
64
|
-
let t = 0;
|
|
65
|
-
for (let i = 0; i < parameters.m; i++)
|
|
66
|
-
t ^= this.testBit(i);
|
|
67
|
-
return t;
|
|
68
|
-
}
|
|
69
|
-
invert() {
|
|
70
|
-
let r = this.mod(), s = modulo;
|
|
71
|
-
let u = Field.get1(), v = Field.get0();
|
|
72
|
-
while (r.getLength() > 1) {
|
|
73
|
-
let j = s.getLength() - r.getLength();
|
|
74
|
-
if (j < 0) {
|
|
75
|
-
[r, s] = [s, r];
|
|
76
|
-
[u, v] = [v, u];
|
|
77
|
-
j = -j;
|
|
78
|
-
}
|
|
79
|
-
s = s.add(r.shiftLeft(j));
|
|
80
|
-
v = v.add(u.shiftLeft(j));
|
|
81
|
-
}
|
|
82
|
-
return u;
|
|
83
|
-
}
|
|
84
|
-
fsquad() {
|
|
85
|
-
const range_to = Math.floor((parameters.m - 1) / 2);
|
|
86
|
-
const val_a = this.mod();
|
|
87
|
-
let val_z = val_a.clone();
|
|
88
|
-
for (let idx = 1; idx <= range_to; idx++) {
|
|
89
|
-
val_z = val_z.mul(val_z);
|
|
90
|
-
val_z = val_z.mul(val_z).add(val_a);
|
|
91
|
-
}
|
|
92
|
-
const val_w = val_z.mul(val_z).add(val_z);
|
|
93
|
-
if (val_w.compare(val_a) == 0)
|
|
94
|
-
return val_z.mod();
|
|
95
|
-
throw new Error("squad eq fail: no square root exists");
|
|
96
|
-
}
|
|
97
|
-
toBytes(length) { return new Uint8Array(this.value.toArray("be", length)); }
|
|
98
|
-
static fromString(str) { return new Field(new BN(str, 16)); }
|
|
99
|
-
static fromBytes(v) { return new Field(new BN(v, 16)); }
|
|
100
|
-
static get0() { return new Field(new BN(0)); }
|
|
101
|
-
static get1() { return new Field(new BN(1)); }
|
|
102
|
-
}
|
|
7
|
+
const field = createField(parameters.m, parameters.ks);
|
|
103
8
|
const onb = parameters.onb ? init_onb_parameters(parameters) : undefined;
|
|
104
|
-
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const lengths = Object.freeze({ fieldByteLength, pointByteLength, scalarByteLength, signatureByteLength });
|
|
9
|
+
const toInternal = (f) => onb ? onb.onbToPb(f) : f.clone();
|
|
10
|
+
const toExternal = (f) => onb ? onb.pbToOnb(f) : f.clone();
|
|
11
|
+
const order = field.fromHexStringOrBytes(parameters.order);
|
|
12
|
+
const b = toInternal(field.fromHexStringOrBytes(parameters.b));
|
|
13
|
+
const scalarByteLength = Math.ceil(order.bitLength() / 8), pointByteLength = field.LENGTH * 2;
|
|
14
|
+
const lengths = Object.freeze({
|
|
15
|
+
fieldByteLength: field.LENGTH,
|
|
16
|
+
pointByteLength,
|
|
17
|
+
scalarByteLength,
|
|
18
|
+
signatureByteLength: scalarByteLength * 2
|
|
19
|
+
});
|
|
116
20
|
// Point operations
|
|
117
21
|
class Point {
|
|
118
22
|
x;
|
|
119
23
|
y;
|
|
120
|
-
static BASE = new Point(toInternal(
|
|
121
|
-
static ZERO = new Point(
|
|
24
|
+
static BASE = new Point(toInternal(field.fromHexStringOrBytes(parameters.Gx)), toInternal(field.fromHexStringOrBytes(parameters.Gy)));
|
|
25
|
+
static ZERO = new Point(new BN(0), new BN(0));
|
|
122
26
|
_precomp;
|
|
123
27
|
_double;
|
|
124
28
|
constructor(x, y) {
|
|
@@ -129,40 +33,39 @@ export const binaryWeierstrass = (parameters) => {
|
|
|
129
33
|
const pz = Point.ZERO.clone();
|
|
130
34
|
const x0 = this.x.clone(), y0 = this.y.clone();
|
|
131
35
|
const x1 = p.x.clone(), y1 = p.y.clone();
|
|
132
|
-
if (this.
|
|
36
|
+
if (this.isZero())
|
|
133
37
|
return p;
|
|
134
|
-
if (p.
|
|
38
|
+
if (p.isZero())
|
|
135
39
|
return this;
|
|
136
40
|
let lbd, x2;
|
|
137
|
-
if (x0.
|
|
138
|
-
const tmp = y0.
|
|
139
|
-
lbd =
|
|
140
|
-
x2 =
|
|
41
|
+
if (x0.cmp(x1) !== 0) {
|
|
42
|
+
const tmp = y0.xor(y1), tmp2 = x0.xor(x1);
|
|
43
|
+
lbd = field.mul(tmp, field.invert(tmp2));
|
|
44
|
+
x2 = field.sqr(lbd);
|
|
141
45
|
if (parameters.a === 1)
|
|
142
|
-
x2.setBit(0, 1 ^
|
|
143
|
-
x2 = x2.
|
|
46
|
+
x2 = field.setBit(x2, 0, 1 ^ field.testBit(x2, 0));
|
|
47
|
+
x2 = x2.xor(lbd).xor(x0).xor(x1);
|
|
144
48
|
}
|
|
145
49
|
else {
|
|
146
|
-
if (y1.
|
|
50
|
+
if (y1.cmp(y0) !== 0)
|
|
147
51
|
return pz;
|
|
148
|
-
if (x1.
|
|
52
|
+
if (x1.isZero())
|
|
149
53
|
return pz;
|
|
150
|
-
lbd = x1.
|
|
151
|
-
x2 =
|
|
54
|
+
lbd = x1.xor(field.mul(p.y, field.invert(p.x)));
|
|
55
|
+
x2 = field.sqr(lbd);
|
|
152
56
|
if (parameters.a === 1)
|
|
153
|
-
x2.setBit(0, 1 ^
|
|
154
|
-
x2 = x2.
|
|
57
|
+
x2 = field.setBit(x2, 0, 1 ^ field.testBit(x2, 0));
|
|
58
|
+
x2 = x2.xor(lbd);
|
|
155
59
|
}
|
|
156
|
-
const y2 =
|
|
60
|
+
const y2 = field.mul(lbd, x1.xor(x2)).xor(x2).xor(y1);
|
|
157
61
|
pz.x = x2;
|
|
158
62
|
pz.y = y2;
|
|
159
63
|
return pz;
|
|
160
64
|
}
|
|
161
65
|
mul(f) {
|
|
162
|
-
let pz = Point.ZERO.clone();
|
|
163
|
-
let
|
|
164
|
-
|
|
165
|
-
if (f.testBit(j) === 1) {
|
|
66
|
+
let pz = Point.ZERO.clone(), p = this.clone();
|
|
67
|
+
for (let j = f.bitLength() - 1; j >= 0; j--) {
|
|
68
|
+
if (f.testn(j)) {
|
|
166
69
|
pz = pz.add(p);
|
|
167
70
|
p = p.add(p);
|
|
168
71
|
}
|
|
@@ -173,21 +76,17 @@ export const binaryWeierstrass = (parameters) => {
|
|
|
173
76
|
}
|
|
174
77
|
return pz;
|
|
175
78
|
}
|
|
176
|
-
negate() { return new Point(this.x, this.x.
|
|
79
|
+
negate() { return new Point(this.x, this.x.xor(this.y)); }
|
|
177
80
|
clone() { return new Point(this.x, this.y); }
|
|
178
|
-
|
|
81
|
+
isZero() { return this.x.isZero() && this.y.isZero(); }
|
|
179
82
|
compress() {
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
const trace = tmp.trace();
|
|
183
|
-
const xExt = toExternal(this.x);
|
|
184
|
-
xExt.setBit(0, trace);
|
|
185
|
-
return xExt;
|
|
83
|
+
const tmp = field.mul(field.invert(this.x), this.y);
|
|
84
|
+
return field.setBit(toExternal(this.x), 0, field.trace(tmp));
|
|
186
85
|
}
|
|
187
86
|
toBytes(isCompressed = false) {
|
|
188
87
|
if (isCompressed)
|
|
189
|
-
return this.compress().
|
|
190
|
-
return concatBytes(toExternal(this.x).
|
|
88
|
+
return field.toBytes(this.compress(), field.LENGTH);
|
|
89
|
+
return concatBytes(field.toBytes(toExternal(this.x), field.LENGTH), field.toBytes(toExternal(this.y), field.LENGTH));
|
|
191
90
|
}
|
|
192
91
|
double() { return this.add(this); }
|
|
193
92
|
timesPow2(e) {
|
|
@@ -213,10 +112,10 @@ export const binaryWeierstrass = (parameters) => {
|
|
|
213
112
|
return { pos, neg };
|
|
214
113
|
}
|
|
215
114
|
mulWnaf(f) {
|
|
216
|
-
let width = getWindowSize(f.
|
|
115
|
+
let width = getWindowSize(f.bitLength());
|
|
217
116
|
width = Math.max(2, Math.min(16, width));
|
|
218
117
|
const { pos, neg } = this.precomp(width);
|
|
219
|
-
const wnaf = windowNaf(width, f
|
|
118
|
+
const wnaf = windowNaf(width, f);
|
|
220
119
|
let R = Point.ZERO;
|
|
221
120
|
let i = wnaf.length;
|
|
222
121
|
if (i > 1) {
|
|
@@ -239,8 +138,7 @@ export const binaryWeierstrass = (parameters) => {
|
|
|
239
138
|
R = R.timesPow2(zeroes);
|
|
240
139
|
}
|
|
241
140
|
while (i > 0) {
|
|
242
|
-
const wi = wnaf[--i];
|
|
243
|
-
const digit = wi >> 16;
|
|
141
|
+
const wi = wnaf[--i], digit = wi >> 16;
|
|
244
142
|
const table = digit < 0 ? neg : pos;
|
|
245
143
|
R = R.double().add(table[Math.abs(digit) >>> 1]);
|
|
246
144
|
R = R.timesPow2(wi & 0xffff);
|
|
@@ -248,53 +146,44 @@ export const binaryWeierstrass = (parameters) => {
|
|
|
248
146
|
return R;
|
|
249
147
|
}
|
|
250
148
|
static expand(xExt) {
|
|
251
|
-
const bit =
|
|
252
|
-
|
|
253
|
-
xCleanExt.setBit(0, 0);
|
|
254
|
-
const
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
xCleanExt.setBit(0, 1);
|
|
149
|
+
const bit = field.testBit(xExt, 0);
|
|
150
|
+
let xCleanExt = xExt.clone();
|
|
151
|
+
xCleanExt = field.setBit(xCleanExt, 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 = field.setBit(xCleanExt, 0, 1);
|
|
258
155
|
const xClean = toInternal(xCleanExt);
|
|
259
|
-
const x2 =
|
|
260
|
-
let rhs =
|
|
261
|
-
if (a === 1)
|
|
262
|
-
rhs = rhs.
|
|
156
|
+
const x2 = field.sqr(xClean);
|
|
157
|
+
let rhs = field.mul(x2, xClean);
|
|
158
|
+
if (parameters.a === 1)
|
|
159
|
+
rhs = rhs.xor(x2);
|
|
263
160
|
if (b)
|
|
264
|
-
rhs = rhs.
|
|
265
|
-
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
const currentBit = z.testBit(0);
|
|
271
|
-
z.setBit(0, 1 ^ currentBit);
|
|
272
|
-
}
|
|
273
|
-
return new Point(xClean, z.mul(xClean));
|
|
161
|
+
rhs = rhs.xor(b);
|
|
162
|
+
let z = field.solve_quad(field.mul(rhs, field.invert(x2)));
|
|
163
|
+
const traceZ = field.trace(z);
|
|
164
|
+
if ((traceZ === 0 && bit === 1) || (traceZ === 1 && bit === 0))
|
|
165
|
+
z = field.setBit(z, 0, 1 ^ field.testBit(z, 0));
|
|
166
|
+
return new Point(xClean, field.mul(z, xClean));
|
|
274
167
|
}
|
|
275
168
|
static fromBytes(bytes) {
|
|
276
169
|
if (bytes.length == pointByteLength)
|
|
277
|
-
return new Point(toInternal(
|
|
278
|
-
else if (bytes.length ==
|
|
279
|
-
return Point.expand(
|
|
170
|
+
return new Point(toInternal(field.fromHexStringOrBytes(bytes.subarray(0, field.LENGTH))), toInternal(field.fromHexStringOrBytes(bytes.subarray(field.LENGTH))));
|
|
171
|
+
else if (bytes.length == field.LENGTH)
|
|
172
|
+
return Point.expand(field.fromHexStringOrBytes(bytes));
|
|
280
173
|
else
|
|
281
|
-
throw new Error(`Invalid bytes length. Must be ${pointByteLength} for uncompressed and ${
|
|
174
|
+
throw new Error(`Invalid bytes length. Must be ${pointByteLength} for uncompressed and ${field.LENGTH} for compressed`);
|
|
282
175
|
}
|
|
283
176
|
}
|
|
284
|
-
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
return new Field(bn.maskn(k));
|
|
289
|
-
};
|
|
290
|
-
// precompute
|
|
291
|
-
Point.BASE.mulWnaf(new Field(new BN(3)));
|
|
177
|
+
const k = order.bitLength() - 1;
|
|
178
|
+
const truncate = (x) => x.maskn(k);
|
|
179
|
+
// Precompute
|
|
180
|
+
Point.BASE.mulWnaf(new BN(3));
|
|
292
181
|
return Object.freeze({
|
|
293
|
-
Field, Point,
|
|
294
|
-
ORDER: order,
|
|
182
|
+
Field: field, Point,
|
|
183
|
+
ORDER: order,
|
|
295
184
|
parameters, lengths,
|
|
296
|
-
hashToField,
|
|
297
185
|
isOnb: !!onb,
|
|
186
|
+
truncate,
|
|
298
187
|
toInternalField: toInternal,
|
|
299
188
|
toExternalField: toExternal
|
|
300
189
|
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { TArg, TRet } from "@noble/hashes/utils.js";
|
|
2
|
+
import BN from "bn.js";
|
|
3
|
+
/** Compute curve modulo */
|
|
4
|
+
export declare const computeMod: (m: number, ks: number[]) => BN;
|
|
5
|
+
/** Create `GF(2^m)` field */
|
|
6
|
+
export declare const createField: (m: number, ks: number[]) => Readonly<{
|
|
7
|
+
MODULO: BN;
|
|
8
|
+
LENGTH: number;
|
|
9
|
+
mod: (f: BN) => BN;
|
|
10
|
+
mul: (x: BN, v: BN) => BN;
|
|
11
|
+
sqr: (x: BN) => BN;
|
|
12
|
+
invert: (f: BN) => BN;
|
|
13
|
+
solve_quad: (v: BN) => BN;
|
|
14
|
+
testBit: (x: BN, i: number) => 0 | 1;
|
|
15
|
+
setBit: (x: BN, i: number, v: number) => BN;
|
|
16
|
+
trace: (x: BN) => 0 | 1;
|
|
17
|
+
traceOnb: (x: BN) => 0 | 1;
|
|
18
|
+
hashToField: (hash: TArg<Uint8Array>) => BN;
|
|
19
|
+
fromHexStringOrBytes: (v: string | Uint8Array) => BN;
|
|
20
|
+
toBytes: (x: BN, length?: number) => TRet<Uint8Array>;
|
|
21
|
+
}>;
|
|
@@ -0,0 +1,222 @@
|
|
|
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
|
+
};
|
|
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);
|
|
85
|
+
/*const mod = (f: BN): BN => {
|
|
86
|
+
const cmp = f.cmp(modulo);
|
|
87
|
+
if(cmp === 0) return new BN(0);
|
|
88
|
+
if(cmp < 0) return f.clone();
|
|
89
|
+
|
|
90
|
+
let bag = f;
|
|
91
|
+
const vl = modulo.bitLength();
|
|
92
|
+
while(true) {
|
|
93
|
+
bag = bag.xor(modulo.ushln(bag.bitLength() - vl));
|
|
94
|
+
if (bag.bitLength() < vl) return bag;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const mul = (x: BN, v: BN): BN => {
|
|
99
|
+
let bag = new BN(0), shift = x;
|
|
100
|
+
for(let i = 0; i < v.bitLength(); i++) {
|
|
101
|
+
if(v.testn(i)) bag = bag.xor(shift);
|
|
102
|
+
shift = shift.ushln(1);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return mod(bag);
|
|
106
|
+
}*/
|
|
107
|
+
const wordsPerElement = Math.ceil(m / WORD_BITS);
|
|
108
|
+
const wordsForProduct = 2 * wordsPerElement;
|
|
109
|
+
const vl = modulo.bitLength();
|
|
110
|
+
const bMod = (f) => {
|
|
111
|
+
if (f.bitLength() <= m)
|
|
112
|
+
return f.clone();
|
|
113
|
+
let bag = f;
|
|
114
|
+
while (bag.bitLength() > m)
|
|
115
|
+
bag = bag.xor(modulo.ushln(bag.bitLength() - vl));
|
|
116
|
+
return bag;
|
|
117
|
+
};
|
|
118
|
+
const mod = (f) => {
|
|
119
|
+
if (f.bitLength() <= m)
|
|
120
|
+
return f.clone();
|
|
121
|
+
const words = bn2LE(f, Math.max(wordsForProduct, Math.ceil(f.bitLength() / WORD_BITS) + 1));
|
|
122
|
+
for (let i = words.length - 1; i >= wordsPerElement; i--) {
|
|
123
|
+
const T = words[i];
|
|
124
|
+
if (T === 0)
|
|
125
|
+
continue;
|
|
126
|
+
words[i] = 0;
|
|
127
|
+
const base = i * WORD_BITS - m;
|
|
128
|
+
xorShiftedWordInto(words, T, base);
|
|
129
|
+
for (const k of ks)
|
|
130
|
+
xorShiftedWordInto(words, T, base + k);
|
|
131
|
+
}
|
|
132
|
+
return bMod(le2BN(words));
|
|
133
|
+
};
|
|
134
|
+
const mul = (x, v) => {
|
|
135
|
+
const table = new Array(MUL_TABLE_SIZE);
|
|
136
|
+
table[0] = new BN(0);
|
|
137
|
+
for (let i = 1; i < MUL_TABLE_SIZE; i++) {
|
|
138
|
+
const lowBit = i & (-i);
|
|
139
|
+
const lowBitIdx = Math.log2(lowBit) | 0;
|
|
140
|
+
table[i] = table[i ^ lowBit].xor(x.ushln(lowBitIdx));
|
|
141
|
+
}
|
|
142
|
+
const vb = v.bitLength();
|
|
143
|
+
let acc = new BN(0);
|
|
144
|
+
for (let win = Math.ceil(vb / MUL_WINDOW) - 1; win >= 0; win--) {
|
|
145
|
+
let windowVal = 0;
|
|
146
|
+
for (let b = 0; b < MUL_WINDOW; b++) {
|
|
147
|
+
const bitPos = win * MUL_WINDOW + b;
|
|
148
|
+
if (bitPos < vb && v.testn(bitPos))
|
|
149
|
+
windowVal |= (1 << b);
|
|
150
|
+
}
|
|
151
|
+
acc = acc.ushln(MUL_WINDOW).xor(table[windowVal]);
|
|
152
|
+
}
|
|
153
|
+
return mod(acc);
|
|
154
|
+
};
|
|
155
|
+
const sqr = (x) => {
|
|
156
|
+
const bytes = x.toArray();
|
|
157
|
+
const out = new Uint8Array(bytes.length * 2);
|
|
158
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
159
|
+
const v = SQR_PRECOMP[bytes[i]];
|
|
160
|
+
out[2 * i] = (v >> 8) & 0xff;
|
|
161
|
+
out[2 * i + 1] = v & 0xff;
|
|
162
|
+
}
|
|
163
|
+
return mod(new BN(out));
|
|
164
|
+
};
|
|
165
|
+
const testBit = (x, i) => x.testn(i) ? 1 : 0;
|
|
166
|
+
const setBit = (x, i, v) => x.setn(i, v == 1);
|
|
167
|
+
const trace = (x) => {
|
|
168
|
+
let t = x;
|
|
169
|
+
for (let i = 1; i < m; i++)
|
|
170
|
+
t = sqr(t).xor(x);
|
|
171
|
+
return testBit(t, 0);
|
|
172
|
+
};
|
|
173
|
+
const traceOnb = (x) => {
|
|
174
|
+
let t = 0;
|
|
175
|
+
for (let i = 0; i < m; i++)
|
|
176
|
+
t ^= testBit(x, i);
|
|
177
|
+
return t;
|
|
178
|
+
};
|
|
179
|
+
const htrace = (x) => {
|
|
180
|
+
let ht = x;
|
|
181
|
+
for (let i = 1; i <= Math.floor((m - 1) / 2); i++)
|
|
182
|
+
ht = sqr(sqr(ht)).xor(x);
|
|
183
|
+
return ht;
|
|
184
|
+
};
|
|
185
|
+
const invert = (f) => {
|
|
186
|
+
let r = mod(f), s = modulo;
|
|
187
|
+
let u = new BN(1), v = new BN(0);
|
|
188
|
+
while (r.bitLength() > 1) {
|
|
189
|
+
let j = s.bitLength() - r.bitLength();
|
|
190
|
+
if (j < 0) {
|
|
191
|
+
let buf = r;
|
|
192
|
+
r = s;
|
|
193
|
+
s = buf;
|
|
194
|
+
buf = u;
|
|
195
|
+
u = v;
|
|
196
|
+
v = buf;
|
|
197
|
+
j = -j;
|
|
198
|
+
}
|
|
199
|
+
s = s.xor(r.ushln(j));
|
|
200
|
+
v = v.xor(u.ushln(j));
|
|
201
|
+
}
|
|
202
|
+
return u;
|
|
203
|
+
};
|
|
204
|
+
const solve_quad = (v) => {
|
|
205
|
+
const a = mod(v), z = htrace(a);
|
|
206
|
+
if (sqr(z).xor(z).cmp(a) == 0)
|
|
207
|
+
return mod(z);
|
|
208
|
+
throw new Error("squad eq fail: no square root exists");
|
|
209
|
+
};
|
|
210
|
+
const hashToField = (hash) => {
|
|
211
|
+
const h = new BN(hash);
|
|
212
|
+
return h.maskn(Math.min(m, h.bitLength()));
|
|
213
|
+
};
|
|
214
|
+
const fromHexStringOrBytes = (v) => new BN(v, 16);
|
|
215
|
+
const toBytes = (x, length) => new Uint8Array(x.toArray("be", length));
|
|
216
|
+
return Object.freeze({
|
|
217
|
+
MODULO: modulo, LENGTH: Math.ceil(m / 8),
|
|
218
|
+
mod, mul, sqr, invert, solve_quad,
|
|
219
|
+
testBit, setBit, trace, traceOnb, hashToField,
|
|
220
|
+
fromHexStringOrBytes, toBytes
|
|
221
|
+
});
|
|
222
|
+
};
|
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
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import BN from "bn.js";
|
|
2
|
-
import { computeMod } from "./index.js";
|
|
3
2
|
import { hexToBytes } from "@noble/hashes/utils.js";
|
|
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
|
let temp = compress_mulp;
|
|
@@ -13,15 +12,14 @@ const decompress_matrix = (compress_mulp, mulp) => {
|
|
|
13
12
|
temp = temp.shrn(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, ks } =
|
|
20
|
-
const modulo = computeMod(m, ks);
|
|
18
|
+
const { m, onb, ks } = parameters;
|
|
21
19
|
const mulp = new Uint16Array(2 * m - 1);
|
|
22
|
-
const compress_mulp = new BN(hexToBytes(
|
|
20
|
+
const compress_mulp = new BN(hexToBytes(onb.matrix), "le");
|
|
23
21
|
decompress_matrix(compress_mulp, mulp);
|
|
24
|
-
const root1 = new BN(hexToBytes(
|
|
22
|
+
const root1 = new BN(hexToBytes(onb.root1), "le"), root2 = new BN(hexToBytes(onb.root2), "le");
|
|
25
23
|
const multiplyOnb = (x, y) => {
|
|
26
24
|
const r = new BN(0);
|
|
27
25
|
const xb = (pos) => (x.testn(pos % m) ? 1 : 0);
|
|
@@ -40,48 +38,24 @@ export const init_onb_parameters = (curve) => {
|
|
|
40
38
|
}
|
|
41
39
|
return r;
|
|
42
40
|
};
|
|
43
|
-
const reverseBits = (x
|
|
41
|
+
const reverseBits = (x) => {
|
|
44
42
|
const r = new BN(0);
|
|
45
43
|
for (let i = 0; i < m; i++)
|
|
46
44
|
if (x.testn(i))
|
|
47
45
|
r.setn(m - 1 - i, 1);
|
|
48
46
|
return r;
|
|
49
47
|
};
|
|
50
|
-
const mod = (f) => {
|
|
51
|
-
const cmp = f.cmp(modulo);
|
|
52
|
-
if (cmp === 0)
|
|
53
|
-
return new BN(0);
|
|
54
|
-
if (cmp < 0)
|
|
55
|
-
return f.clone();
|
|
56
|
-
let bag = f;
|
|
57
|
-
const vl = modulo.bitLength();
|
|
58
|
-
while (true) {
|
|
59
|
-
bag = bag.xor(modulo.ushln(bag.bitLength() - vl));
|
|
60
|
-
if (bag.bitLength() < vl)
|
|
61
|
-
return bag;
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
const mul = (x, v) => {
|
|
65
|
-
let bag = new BN(0);
|
|
66
|
-
let shift = x;
|
|
67
|
-
const vLen = v.bitLength();
|
|
68
|
-
for (let i = 0; i < vLen; i++) {
|
|
69
|
-
if (v.testn(i))
|
|
70
|
-
bag = bag.xor(shift);
|
|
71
|
-
shift = shift.ushln(1);
|
|
72
|
-
}
|
|
73
|
-
return mod(bag);
|
|
74
|
-
};
|
|
75
48
|
const toPb = new Array(m);
|
|
76
49
|
toPb[0] = root1.clone();
|
|
50
|
+
const { mul } = createField(m, ks);
|
|
77
51
|
for (let i = 1; i < m; i++)
|
|
78
52
|
toPb[i] = mul(toPb[i - 1], toPb[i - 1]);
|
|
79
53
|
const _toOnb = new Array(m);
|
|
80
54
|
_toOnb[0] = new BN(1).shln(m).subn(1);
|
|
81
55
|
for (let i = 1; i < m; i++)
|
|
82
56
|
_toOnb[i] = multiplyOnb(_toOnb[i - 1], root2);
|
|
83
|
-
const toOnb = _toOnb.map(
|
|
84
|
-
return {
|
|
57
|
+
const toOnb = _toOnb.map(reverseBits);
|
|
58
|
+
return Object.freeze({
|
|
85
59
|
onbToPb: (x) => {
|
|
86
60
|
let r = new BN(0);
|
|
87
61
|
for (let p = 0; p < m; p++)
|
|
@@ -96,5 +70,5 @@ export const init_onb_parameters = (curve) => {
|
|
|
96
70
|
r = r.xor(toOnb[j]);
|
|
97
71
|
return r;
|
|
98
72
|
}
|
|
99
|
-
};
|
|
73
|
+
});
|
|
100
74
|
};
|