-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathutils.js
More file actions
251 lines (233 loc) · 6.76 KB
/
utils.js
File metadata and controls
251 lines (233 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* global BigInt */
import * as Scalar from "./scalar.js";
export function stringifyBigInts(o) {
if (o === null) return null;
if (typeof o == "bigint" || o.eq !== undefined) {
return o.toString(10);
} else if (o instanceof Uint8Array) {
return Scalar.fromRprLE(o, 0);
} else if (Array.isArray(o)) {
return o.map(stringifyBigInts);
} else if (typeof o == "object") {
const res = {};
const keys = Object.keys(o);
keys.forEach((k) => {
res[k] = stringifyBigInts(o[k]);
});
return res;
} else {
return o;
}
}
export function unstringifyBigInts(o) {
if (typeof o == "string" && /^[0-9]+$/.test(o)) {
return BigInt(o);
} else if (typeof o == "string" && /^0x[0-9a-fA-F]+$/.test(o)) {
return BigInt(o);
} else if (Array.isArray(o)) {
return o.map(unstringifyBigInts);
} else if (typeof o == "object") {
if (o === null) return null;
const res = {};
const keys = Object.keys(o);
keys.forEach((k) => {
res[k] = unstringifyBigInts(o[k]);
});
return res;
} else {
return o;
}
}
export function beBuff2int(buff) {
let res = BigInt(0);
let i = buff.length;
let offset = 0;
const buffV = new DataView(buff.buffer, buff.byteOffset, buff.byteLength);
while (i > 0) {
if (i >= 4) {
i -= 4;
res += BigInt(buffV.getUint32(i)) << BigInt(offset * 8);
offset += 4;
} else if (i >= 2) {
i -= 2;
res += BigInt(buffV.getUint16(i)) << BigInt(offset * 8);
offset += 2;
} else {
i -= 1;
res += BigInt(buffV.getUint8(i)) << BigInt(offset * 8);
offset += 1;
}
}
return res;
}
export function beInt2Buff(n, len) {
let r = n;
const buff = new Uint8Array(len);
const buffV = new DataView(buff.buffer);
let o = len;
while (o > 0) {
if (o - 4 >= 0) {
o -= 4;
buffV.setUint32(o, Number(r & BigInt(0xffffffff)));
r = r >> BigInt(32);
} else if (o - 2 >= 0) {
o -= 2;
buffV.setUint16(o, Number(r & BigInt(0xffff)));
r = r >> BigInt(16);
} else {
o -= 1;
buffV.setUint8(o, Number(r & BigInt(0xff)));
r = r >> BigInt(8);
}
}
if (r) {
throw new Error("Number does not fit in this length");
}
return buff;
}
export function leBuff2int(buff) {
let res = BigInt(0);
let i = 0;
const buffV = new DataView(buff.buffer, buff.byteOffset, buff.byteLength);
while (i < buff.length) {
if (i + 4 <= buff.length) {
res += BigInt(buffV.getUint32(i, true)) << BigInt(i * 8);
i += 4;
} else if (i + 2 <= buff.length) {
res += BigInt(buffV.getUint16(i, true)) << BigInt(i * 8);
i += 2;
} else {
res += BigInt(buffV.getUint8(i, true)) << BigInt(i * 8);
i += 1;
}
}
return res;
}
export function leInt2Buff(n, len) {
let r = n;
if (typeof len === "undefined") {
len = Math.floor((Scalar.bitLength(n) - 1) / 8) + 1;
if (len == 0) len = 1;
}
const buff = new Uint8Array(len);
const buffV = new DataView(buff.buffer);
let o = 0;
while (o < len) {
if (o + 4 <= len) {
buffV.setUint32(o, Number(r & BigInt(0xffffffff)), true);
o += 4;
r = r >> BigInt(32);
} else if (o + 2 <= len) {
buffV.setUint16(o, Number(r & BigInt(0xffff)), true);
o += 2;
r = r >> BigInt(16);
} else {
buffV.setUint8(o, Number(r & BigInt(0xff)), true);
o += 1;
r = r >> BigInt(8);
}
}
if (r) {
throw new Error("Number does not fit in this length");
}
return buff;
}
export function stringifyFElements(F, o) {
if (typeof o == "bigint" || o.eq !== undefined) {
return o.toString(10);
} else if (o instanceof Uint8Array) {
return F.toString(F.e(o));
} else if (Array.isArray(o)) {
return o.map(stringifyFElements.bind(this, F));
} else if (typeof o == "object") {
const res = {};
const keys = Object.keys(o);
keys.forEach((k) => {
res[k] = stringifyFElements(F, o[k]);
});
return res;
} else {
return o;
}
}
export function unstringifyFElements(F, o) {
if (typeof o == "string" && /^[0-9]+$/.test(o)) {
return F.e(o);
} else if (typeof o == "string" && /^0x[0-9a-fA-F]+$/.test(o)) {
return F.e(o);
} else if (Array.isArray(o)) {
return o.map(unstringifyFElements.bind(this, F));
} else if (typeof o == "object") {
if (o === null) return null;
const res = {};
const keys = Object.keys(o);
keys.forEach((k) => {
res[k] = unstringifyFElements(F, o[k]);
});
return res;
} else {
return o;
}
}
const _revTable = [];
for (let i = 0; i < 256; i++) {
_revTable[i] = _revSlow(i, 8);
}
function _revSlow(idx, bits) {
let res = 0;
let a = idx;
for (let i = 0; i < bits; i++) {
res <<= 1;
res = res | (a & 1);
a >>= 1;
}
return res;
}
export function bitReverse(idx, bits) {
return (
(_revTable[idx >>> 24] |
(_revTable[(idx >>> 16) & 0xff] << 8) |
(_revTable[(idx >>> 8) & 0xff] << 16) |
(_revTable[idx & 0xff] << 24)) >>>
(32 - bits)
);
}
export function log2(V) {
return (
((V & 0xffff0000) !== 0 ? ((V &= 0xffff0000), 16) : 0) |
((V & 0xff00ff00) !== 0 ? ((V &= 0xff00ff00), 8) : 0) |
((V & 0xf0f0f0f0) !== 0 ? ((V &= 0xf0f0f0f0), 4) : 0) |
((V & 0xcccccccc) !== 0 ? ((V &= 0xcccccccc), 2) : 0) |
((V & 0xaaaaaaaa) !== 0)
);
}
export function buffReverseBits(buff, eSize) {
const n = buff.byteLength / eSize;
const bits = log2(n);
if (n != 1 << bits) {
throw new Error("Invalid number of pointers");
}
for (let i = 0; i < n; i++) {
const r = bitReverse(i, bits);
if (i > r) {
const tmp = buff.slice(i * eSize, (i + 1) * eSize);
buff.set(buff.slice(r * eSize, (r + 1) * eSize), i * eSize);
buff.set(tmp, r * eSize);
}
}
}
export function array2buffer(arr, sG) {
const buff = new Uint8Array(sG * arr.length);
for (let i = 0; i < arr.length; i++) {
buff.set(arr[i], i * sG);
}
return buff;
}
export function buffer2array(buff, sG) {
const n = buff.byteLength / sG;
const arr = new Array(n);
for (let i = 0; i < n; i++) {
arr[i] = buff.slice(i * sG, i * sG + sG);
}
return arr;
}