@cogitator-ai/wasm-tools 0.4.1 → 0.5.0
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 +141 -6
- package/dist/index.d.ts +227 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +194 -0
- package/dist/index.js.map +1 -1
- package/dist/plugins/compression.d.ts +2 -0
- package/dist/plugins/compression.d.ts.map +1 -0
- package/dist/plugins/compression.js +530 -0
- package/dist/plugins/compression.js.map +1 -0
- package/dist/plugins/csv.d.ts +2 -0
- package/dist/plugins/csv.d.ts.map +1 -0
- package/dist/plugins/csv.js +138 -0
- package/dist/plugins/csv.js.map +1 -0
- package/dist/plugins/datetime.d.ts +2 -0
- package/dist/plugins/datetime.d.ts.map +1 -0
- package/dist/plugins/datetime.js +184 -0
- package/dist/plugins/datetime.js.map +1 -0
- package/dist/plugins/diff.d.ts +2 -0
- package/dist/plugins/diff.d.ts.map +1 -0
- package/dist/plugins/diff.js +187 -0
- package/dist/plugins/diff.js.map +1 -0
- package/dist/plugins/markdown.d.ts +2 -0
- package/dist/plugins/markdown.d.ts.map +1 -0
- package/dist/plugins/markdown.js +203 -0
- package/dist/plugins/markdown.js.map +1 -0
- package/dist/plugins/regex.d.ts +2 -0
- package/dist/plugins/regex.d.ts.map +1 -0
- package/dist/plugins/regex.js +119 -0
- package/dist/plugins/regex.js.map +1 -0
- package/dist/plugins/signing.d.ts +2 -0
- package/dist/plugins/signing.d.ts.map +1 -0
- package/dist/plugins/signing.js +565 -0
- package/dist/plugins/signing.js.map +1 -0
- package/dist/plugins/slug.d.ts +2 -0
- package/dist/plugins/slug.d.ts.map +1 -0
- package/dist/plugins/slug.js +180 -0
- package/dist/plugins/slug.js.map +1 -0
- package/dist/plugins/validation.d.ts +2 -0
- package/dist/plugins/validation.d.ts.map +1 -0
- package/dist/plugins/validation.js +105 -0
- package/dist/plugins/validation.js.map +1 -0
- package/dist/plugins/xml.d.ts +2 -0
- package/dist/plugins/xml.d.ts.map +1 -0
- package/dist/plugins/xml.js +293 -0
- package/dist/plugins/xml.js.map +1 -0
- package/dist/temp/compression.js +614 -0
- package/dist/temp/csv.js +154 -0
- package/dist/temp/datetime.js +196 -0
- package/dist/temp/diff.js +198 -0
- package/dist/temp/markdown.js +220 -0
- package/dist/temp/regex.js +140 -0
- package/dist/temp/signing.js +569 -0
- package/dist/temp/slug.js +198 -0
- package/dist/temp/validation.js +125 -0
- package/dist/temp/xml.js +289 -0
- package/dist/wasm/base64.wasm +0 -0
- package/dist/wasm/calc.wasm +0 -0
- package/dist/wasm/compression.wasm +0 -0
- package/dist/wasm/csv.wasm +0 -0
- package/dist/wasm/datetime.wasm +0 -0
- package/dist/wasm/diff.wasm +0 -0
- package/dist/wasm/hash.wasm +0 -0
- package/dist/wasm/json.wasm +0 -0
- package/dist/wasm/markdown.wasm +0 -0
- package/dist/wasm/regex.wasm +0 -0
- package/dist/wasm/signing.wasm +0 -0
- package/dist/wasm/slug.wasm +0 -0
- package/dist/wasm/validation.wasm +0 -0
- package/dist/wasm/xml.wasm +0 -0
- package/package.json +2 -2
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod2) => __copyProps(__defProp({}, "__esModule", { value: true }), mod2);
|
|
19
|
+
|
|
20
|
+
// src/plugins/signing.ts
|
|
21
|
+
var signing_exports = {};
|
|
22
|
+
__export(signing_exports, {
|
|
23
|
+
signing: () => signing
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(signing_exports);
|
|
26
|
+
function hexToBytes(hex) {
|
|
27
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
28
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
29
|
+
bytes[i / 2] = parseInt(hex.substr(i, 2), 16);
|
|
30
|
+
}
|
|
31
|
+
return bytes;
|
|
32
|
+
}
|
|
33
|
+
function bytesToHex(bytes) {
|
|
34
|
+
return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
35
|
+
}
|
|
36
|
+
var B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
37
|
+
function base64ToBytes(b64) {
|
|
38
|
+
const clean = b64.replace(/[^A-Za-z0-9+/]/g, "");
|
|
39
|
+
const len = clean.length;
|
|
40
|
+
const outLen = Math.floor(len * 3 / 4) - (b64.endsWith("==") ? 2 : b64.endsWith("=") ? 1 : 0);
|
|
41
|
+
const out = new Uint8Array(outLen);
|
|
42
|
+
const lookup = {};
|
|
43
|
+
for (let i = 0; i < B64.length; i++) lookup[B64[i]] = i;
|
|
44
|
+
let j = 0;
|
|
45
|
+
for (let i = 0; i < len; i += 4) {
|
|
46
|
+
const a = lookup[clean[i]] || 0;
|
|
47
|
+
const b = lookup[clean[i + 1]] || 0;
|
|
48
|
+
const c = lookup[clean[i + 2]] || 0;
|
|
49
|
+
const d = lookup[clean[i + 3]] || 0;
|
|
50
|
+
out[j++] = a << 2 | b >> 4;
|
|
51
|
+
if (j < outLen) out[j++] = (b & 15) << 4 | c >> 2;
|
|
52
|
+
if (j < outLen) out[j++] = (c & 3) << 6 | d;
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
function bytesToBase64(bytes) {
|
|
57
|
+
let r = "";
|
|
58
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
59
|
+
const b1 = bytes[i], b2 = bytes[i + 1] ?? 0, b3 = bytes[i + 2] ?? 0;
|
|
60
|
+
r += B64[b1 >> 2];
|
|
61
|
+
r += B64[(b1 & 3) << 4 | b2 >> 4];
|
|
62
|
+
r += i + 1 < bytes.length ? B64[(b2 & 15) << 2 | b3 >> 6] : "=";
|
|
63
|
+
r += i + 2 < bytes.length ? B64[b3 & 63] : "=";
|
|
64
|
+
}
|
|
65
|
+
return r;
|
|
66
|
+
}
|
|
67
|
+
function decodeKey(key, encoding) {
|
|
68
|
+
return encoding === "base64" ? base64ToBytes(key) : hexToBytes(key);
|
|
69
|
+
}
|
|
70
|
+
function encodeKey(bytes, encoding) {
|
|
71
|
+
return encoding === "base64" ? bytesToBase64(bytes) : bytesToHex(bytes);
|
|
72
|
+
}
|
|
73
|
+
function sha512(message) {
|
|
74
|
+
const K = [
|
|
75
|
+
0x428a2f98n,
|
|
76
|
+
0xd728ae22n,
|
|
77
|
+
0x71374491n,
|
|
78
|
+
0x23ef65cdn,
|
|
79
|
+
0xb5c0fbcfn,
|
|
80
|
+
0xec4d3b2fn,
|
|
81
|
+
0xe9b5dba5n,
|
|
82
|
+
0x8189dbbcn,
|
|
83
|
+
0x3956c25bn,
|
|
84
|
+
0xf348b538n,
|
|
85
|
+
0x59f111f1n,
|
|
86
|
+
0xb605d019n,
|
|
87
|
+
0x923f82a4n,
|
|
88
|
+
0xaf194f9bn,
|
|
89
|
+
0xab1c5ed5n,
|
|
90
|
+
0xda6d8118n,
|
|
91
|
+
0xd807aa98n,
|
|
92
|
+
0xa3030242n,
|
|
93
|
+
0x12835b01n,
|
|
94
|
+
0x45706fben,
|
|
95
|
+
0x243185ben,
|
|
96
|
+
0x4ee4b28cn,
|
|
97
|
+
0x550c7dc3n,
|
|
98
|
+
0xd5ffb4e2n,
|
|
99
|
+
0x72be5d74n,
|
|
100
|
+
0xf27b896fn,
|
|
101
|
+
0x80deb1fen,
|
|
102
|
+
0x3b1696b1n,
|
|
103
|
+
0x9bdc06a7n,
|
|
104
|
+
0x25c71235n,
|
|
105
|
+
0xc19bf174n,
|
|
106
|
+
0xcf692694n,
|
|
107
|
+
0xe49b69c1n,
|
|
108
|
+
0x9ef14ad2n,
|
|
109
|
+
0xefbe4786n,
|
|
110
|
+
0x384f25e3n,
|
|
111
|
+
0x0fc19dc6n,
|
|
112
|
+
0x8b8cd5b5n,
|
|
113
|
+
0x240ca1ccn,
|
|
114
|
+
0x77ac9c65n,
|
|
115
|
+
0x2de92c6fn,
|
|
116
|
+
0x592b0275n,
|
|
117
|
+
0x4a7484aan,
|
|
118
|
+
0x6ea6e483n,
|
|
119
|
+
0x5cb0a9dcn,
|
|
120
|
+
0xbd41fbd4n,
|
|
121
|
+
0x76f988dan,
|
|
122
|
+
0x831153b5n,
|
|
123
|
+
0x983e5152n,
|
|
124
|
+
0xee66dfabn,
|
|
125
|
+
0xa831c66dn,
|
|
126
|
+
0x2db43210n,
|
|
127
|
+
0xb00327c8n,
|
|
128
|
+
0x98fb213fn,
|
|
129
|
+
0xbf597fc7n,
|
|
130
|
+
0xbeef0ee4n,
|
|
131
|
+
0xc6e00bf3n,
|
|
132
|
+
0x3da88fc2n,
|
|
133
|
+
0xd5a79147n,
|
|
134
|
+
0x930aa725n,
|
|
135
|
+
0x06ca6351n,
|
|
136
|
+
0xe003826fn,
|
|
137
|
+
0x14292967n,
|
|
138
|
+
0x0a0e6e70n,
|
|
139
|
+
0x27b70a85n,
|
|
140
|
+
0x46d22ffcn,
|
|
141
|
+
0x2e1b2138n,
|
|
142
|
+
0x5c26c926n,
|
|
143
|
+
0x4d2c6dfcn,
|
|
144
|
+
0x5ac42aedn,
|
|
145
|
+
0x53380d13n,
|
|
146
|
+
0x9d95b3dfn,
|
|
147
|
+
0x650a7354n,
|
|
148
|
+
0x8baf63den,
|
|
149
|
+
0x766a0abbn,
|
|
150
|
+
0x3c77b2a8n,
|
|
151
|
+
0x81c2c92en,
|
|
152
|
+
0x47edaee6n,
|
|
153
|
+
0x92722c85n,
|
|
154
|
+
0x1482353bn,
|
|
155
|
+
0xa2bfe8a1n,
|
|
156
|
+
0x4cf10364n,
|
|
157
|
+
0xa81a664bn,
|
|
158
|
+
0xbc423001n,
|
|
159
|
+
0xc24b8b70n,
|
|
160
|
+
0xd0f89791n,
|
|
161
|
+
0xc76c51a3n,
|
|
162
|
+
0x0654be30n,
|
|
163
|
+
0xd192e819n,
|
|
164
|
+
0xd6ef5218n,
|
|
165
|
+
0xd6990624n,
|
|
166
|
+
0x5565a910n,
|
|
167
|
+
0xf40e3585n,
|
|
168
|
+
0x5771202an,
|
|
169
|
+
0x106aa070n,
|
|
170
|
+
0x32bbd1b8n,
|
|
171
|
+
0x19a4c116n,
|
|
172
|
+
0xb8d2d0c8n,
|
|
173
|
+
0x1e376c08n,
|
|
174
|
+
0x5141ab53n,
|
|
175
|
+
0x2748774cn,
|
|
176
|
+
0xdf8eeb99n,
|
|
177
|
+
0x34b0bcb5n,
|
|
178
|
+
0xe19b48a8n,
|
|
179
|
+
0x391c0cb3n,
|
|
180
|
+
0xc5c95a63n,
|
|
181
|
+
0x4ed8aa4an,
|
|
182
|
+
0xe3418acbn,
|
|
183
|
+
0x5b9cca4fn,
|
|
184
|
+
0x7763e373n,
|
|
185
|
+
0x682e6ff3n,
|
|
186
|
+
0xd6b2b8a3n,
|
|
187
|
+
0x748f82een,
|
|
188
|
+
0x5defb2fcn,
|
|
189
|
+
0x78a5636fn,
|
|
190
|
+
0x43172f60n,
|
|
191
|
+
0x84c87814n,
|
|
192
|
+
0xa1f0ab72n,
|
|
193
|
+
0x8cc70208n,
|
|
194
|
+
0x1a6439ecn,
|
|
195
|
+
0x90befffan,
|
|
196
|
+
0x23631e28n,
|
|
197
|
+
0xa4506cebn,
|
|
198
|
+
0xde82bde9n,
|
|
199
|
+
0xbef9a3f7n,
|
|
200
|
+
0xb2c67915n,
|
|
201
|
+
0xc67178f2n,
|
|
202
|
+
0xe372532bn,
|
|
203
|
+
0xca273ecen,
|
|
204
|
+
0xea26619cn,
|
|
205
|
+
0xd186b8c7n,
|
|
206
|
+
0x21c0c207n,
|
|
207
|
+
0xeada7dd6n,
|
|
208
|
+
0xcde0eb1en,
|
|
209
|
+
0xf57d4f7fn,
|
|
210
|
+
0xee6ed178n,
|
|
211
|
+
0x06f067aan,
|
|
212
|
+
0x72176fban,
|
|
213
|
+
0x0a637dc5n,
|
|
214
|
+
0xa2c898a6n,
|
|
215
|
+
0x113f9804n,
|
|
216
|
+
0xbef90daen,
|
|
217
|
+
0x1b710b35n,
|
|
218
|
+
0x131c471bn,
|
|
219
|
+
0x28db77f5n,
|
|
220
|
+
0x23047d84n,
|
|
221
|
+
0x32caab7bn,
|
|
222
|
+
0x40c72493n,
|
|
223
|
+
0x3c9ebe0an,
|
|
224
|
+
0x15c9bebcn,
|
|
225
|
+
0x431d67c4n,
|
|
226
|
+
0x9c100d4cn,
|
|
227
|
+
0x4cc5d4ben,
|
|
228
|
+
0xcb3e42b6n,
|
|
229
|
+
0x597f299cn,
|
|
230
|
+
0xfc657e2an,
|
|
231
|
+
0x5fcb6fabn,
|
|
232
|
+
0x3ad6faecn,
|
|
233
|
+
0x6c44198cn,
|
|
234
|
+
0x4a475817n
|
|
235
|
+
];
|
|
236
|
+
const H = [
|
|
237
|
+
0x6a09e667f3bcc908n,
|
|
238
|
+
0xbb67ae8584caa73bn,
|
|
239
|
+
0x3c6ef372fe94f82bn,
|
|
240
|
+
0xa54ff53a5f1d36f1n,
|
|
241
|
+
0x510e527fade682d1n,
|
|
242
|
+
0x9b05688c2b3e6c1fn,
|
|
243
|
+
0x1f83d9abfb41bd6bn,
|
|
244
|
+
0x5be0cd19137e2179n
|
|
245
|
+
];
|
|
246
|
+
const msg = new Uint8Array(message);
|
|
247
|
+
const bitLen = BigInt(msg.length * 8);
|
|
248
|
+
const padLen = (128 - (msg.length + 17) % 128) % 128;
|
|
249
|
+
const padded = new Uint8Array(msg.length + 1 + padLen + 16);
|
|
250
|
+
padded.set(msg);
|
|
251
|
+
padded[msg.length] = 128;
|
|
252
|
+
for (let i = 0; i < 8; i++) {
|
|
253
|
+
padded[padded.length - 1 - i] = Number(bitLen >> BigInt(i * 8) & 0xffn);
|
|
254
|
+
}
|
|
255
|
+
const rotr = (x, n) => (x >> n | x << 64n - n) & 0xffffffffffffffffn;
|
|
256
|
+
for (let chunk = 0; chunk < padded.length; chunk += 128) {
|
|
257
|
+
const w = new Array(80);
|
|
258
|
+
for (let i = 0; i < 16; i++) {
|
|
259
|
+
w[i] = 0n;
|
|
260
|
+
for (let j = 0; j < 8; j++) {
|
|
261
|
+
w[i] = w[i] << 8n | BigInt(padded[chunk + i * 8 + j]);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
for (let i = 16; i < 80; i++) {
|
|
265
|
+
const s0 = rotr(w[i - 15], 1n) ^ rotr(w[i - 15], 8n) ^ w[i - 15] >> 7n;
|
|
266
|
+
const s1 = rotr(w[i - 2], 19n) ^ rotr(w[i - 2], 61n) ^ w[i - 2] >> 6n;
|
|
267
|
+
w[i] = w[i - 16] + s0 + w[i - 7] + s1 & 0xffffffffffffffffn;
|
|
268
|
+
}
|
|
269
|
+
let [a, b, c, d, e, f, g, h] = H;
|
|
270
|
+
for (let i = 0; i < 80; i++) {
|
|
271
|
+
const S1 = rotr(e, 14n) ^ rotr(e, 18n) ^ rotr(e, 41n);
|
|
272
|
+
const ch = e & f ^ ~e & g;
|
|
273
|
+
const ki = K[i * 2] << 32n | K[i * 2 + 1];
|
|
274
|
+
const temp1 = h + S1 + ch + ki + w[i] & 0xffffffffffffffffn;
|
|
275
|
+
const S0 = rotr(a, 28n) ^ rotr(a, 34n) ^ rotr(a, 39n);
|
|
276
|
+
const maj = a & b ^ a & c ^ b & c;
|
|
277
|
+
const temp2 = S0 + maj & 0xffffffffffffffffn;
|
|
278
|
+
h = g;
|
|
279
|
+
g = f;
|
|
280
|
+
f = e;
|
|
281
|
+
e = d + temp1 & 0xffffffffffffffffn;
|
|
282
|
+
d = c;
|
|
283
|
+
c = b;
|
|
284
|
+
b = a;
|
|
285
|
+
a = temp1 + temp2 & 0xffffffffffffffffn;
|
|
286
|
+
}
|
|
287
|
+
H[0] = H[0] + a & 0xffffffffffffffffn;
|
|
288
|
+
H[1] = H[1] + b & 0xffffffffffffffffn;
|
|
289
|
+
H[2] = H[2] + c & 0xffffffffffffffffn;
|
|
290
|
+
H[3] = H[3] + d & 0xffffffffffffffffn;
|
|
291
|
+
H[4] = H[4] + e & 0xffffffffffffffffn;
|
|
292
|
+
H[5] = H[5] + f & 0xffffffffffffffffn;
|
|
293
|
+
H[6] = H[6] + g & 0xffffffffffffffffn;
|
|
294
|
+
H[7] = H[7] + h & 0xffffffffffffffffn;
|
|
295
|
+
}
|
|
296
|
+
const result = new Uint8Array(64);
|
|
297
|
+
for (let i = 0; i < 8; i++) {
|
|
298
|
+
for (let j = 0; j < 8; j++) {
|
|
299
|
+
result[i * 8 + j] = Number(H[i] >> BigInt((7 - j) * 8) & 0xffn);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
304
|
+
var ED25519_P = 2n ** 255n - 19n;
|
|
305
|
+
var ED25519_L = 2n ** 252n + 27742317777372353535851937790883648493n;
|
|
306
|
+
var ED25519_D = -121665n * modInv(121666n, ED25519_P) % ED25519_P;
|
|
307
|
+
function mod(a, m) {
|
|
308
|
+
return (a % m + m) % m;
|
|
309
|
+
}
|
|
310
|
+
function modInv(a, m) {
|
|
311
|
+
let [old_r, r] = [a, m];
|
|
312
|
+
let [old_s, s] = [1n, 0n];
|
|
313
|
+
while (r !== 0n) {
|
|
314
|
+
const q = old_r / r;
|
|
315
|
+
[old_r, r] = [r, old_r - q * r];
|
|
316
|
+
[old_s, s] = [s, old_s - q * s];
|
|
317
|
+
}
|
|
318
|
+
return mod(old_s, m);
|
|
319
|
+
}
|
|
320
|
+
function modPow(base, exp, m) {
|
|
321
|
+
let result = 1n;
|
|
322
|
+
base = mod(base, m);
|
|
323
|
+
while (exp > 0n) {
|
|
324
|
+
if (exp & 1n) result = mod(result * base, m);
|
|
325
|
+
exp >>= 1n;
|
|
326
|
+
base = mod(base * base, m);
|
|
327
|
+
}
|
|
328
|
+
return result;
|
|
329
|
+
}
|
|
330
|
+
function pointAdd(p1, p2) {
|
|
331
|
+
const a = mod(p1.y - p1.x, ED25519_P) * mod(p2.y - p2.x, ED25519_P) % ED25519_P;
|
|
332
|
+
const b = mod(p1.y + p1.x, ED25519_P) * mod(p2.y + p2.x, ED25519_P) % ED25519_P;
|
|
333
|
+
const c = 2n * p1.t * p2.t * ED25519_D % ED25519_P;
|
|
334
|
+
const d = 2n * p1.z * p2.z % ED25519_P;
|
|
335
|
+
const e = mod(b - a, ED25519_P);
|
|
336
|
+
const f = mod(d - c, ED25519_P);
|
|
337
|
+
const g = mod(d + c, ED25519_P);
|
|
338
|
+
const h = mod(b + a, ED25519_P);
|
|
339
|
+
return {
|
|
340
|
+
x: mod(e * f, ED25519_P),
|
|
341
|
+
y: mod(g * h, ED25519_P),
|
|
342
|
+
z: mod(f * g, ED25519_P),
|
|
343
|
+
t: mod(e * h, ED25519_P)
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
function pointDouble(p) {
|
|
347
|
+
const a = mod(p.x * p.x, ED25519_P);
|
|
348
|
+
const b = mod(p.y * p.y, ED25519_P);
|
|
349
|
+
const c = 2n * mod(p.z * p.z, ED25519_P);
|
|
350
|
+
const h = mod(a + b, ED25519_P);
|
|
351
|
+
const e = mod(h - mod((p.x + p.y) * (p.x + p.y), ED25519_P), ED25519_P);
|
|
352
|
+
const g = mod(a - b, ED25519_P);
|
|
353
|
+
const f = mod(c + g, ED25519_P);
|
|
354
|
+
return {
|
|
355
|
+
x: mod(e * f, ED25519_P),
|
|
356
|
+
y: mod(g * h, ED25519_P),
|
|
357
|
+
z: mod(f * g, ED25519_P),
|
|
358
|
+
t: mod(e * h, ED25519_P)
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
function scalarMult(k, p) {
|
|
362
|
+
let result = { x: 0n, y: 1n, z: 1n, t: 0n };
|
|
363
|
+
let temp = p;
|
|
364
|
+
while (k > 0n) {
|
|
365
|
+
if (k & 1n) result = pointAdd(result, temp);
|
|
366
|
+
temp = pointDouble(temp);
|
|
367
|
+
k >>= 1n;
|
|
368
|
+
}
|
|
369
|
+
return result;
|
|
370
|
+
}
|
|
371
|
+
function pointToBytes(p) {
|
|
372
|
+
const zi = modInv(p.z, ED25519_P);
|
|
373
|
+
const x = mod(p.x * zi, ED25519_P);
|
|
374
|
+
const y = mod(p.y * zi, ED25519_P);
|
|
375
|
+
const bytes = new Uint8Array(32);
|
|
376
|
+
for (let i = 0; i < 32; i++) {
|
|
377
|
+
bytes[i] = Number(y >> BigInt(i * 8) & 0xffn);
|
|
378
|
+
}
|
|
379
|
+
bytes[31] |= Number((x & 1n) << 7n);
|
|
380
|
+
return bytes;
|
|
381
|
+
}
|
|
382
|
+
function bytesToPoint(bytes) {
|
|
383
|
+
let y = 0n;
|
|
384
|
+
for (let i = 0; i < 32; i++) {
|
|
385
|
+
y |= BigInt(bytes[i] & (i === 31 ? 127 : 255)) << BigInt(i * 8);
|
|
386
|
+
}
|
|
387
|
+
const xSign = bytes[31] >> 7 & 1;
|
|
388
|
+
const y2 = mod(y * y, ED25519_P);
|
|
389
|
+
const x2 = mod((y2 - 1n) * modInv(ED25519_D * y2 + 1n, ED25519_P), ED25519_P);
|
|
390
|
+
let x = modPow(x2, (ED25519_P + 3n) / 8n, ED25519_P);
|
|
391
|
+
if (mod(x * x - x2, ED25519_P) !== 0n) {
|
|
392
|
+
x = mod(x * modPow(2n, (ED25519_P - 1n) / 4n, ED25519_P), ED25519_P);
|
|
393
|
+
}
|
|
394
|
+
if (Number(x & 1n) !== xSign) {
|
|
395
|
+
x = mod(-x, ED25519_P);
|
|
396
|
+
}
|
|
397
|
+
return { x, y, z: 1n, t: mod(x * y, ED25519_P) };
|
|
398
|
+
}
|
|
399
|
+
var ED25519_G = (() => {
|
|
400
|
+
const gy = 4n * modInv(5n, ED25519_P) % ED25519_P;
|
|
401
|
+
const gx2 = mod((gy * gy - 1n) * modInv(ED25519_D * gy * gy + 1n, ED25519_P), ED25519_P);
|
|
402
|
+
let gx = modPow(gx2, (ED25519_P + 3n) / 8n, ED25519_P);
|
|
403
|
+
if (mod(gx * gx - gx2, ED25519_P) !== 0n) {
|
|
404
|
+
gx = mod(gx * modPow(2n, (ED25519_P - 1n) / 4n, ED25519_P), ED25519_P);
|
|
405
|
+
}
|
|
406
|
+
if (gx & 1n) gx = mod(-gx, ED25519_P);
|
|
407
|
+
return { x: gx, y: gy, z: 1n, t: mod(gx * gy, ED25519_P) };
|
|
408
|
+
})();
|
|
409
|
+
function ed25519GenerateKeypair(seed) {
|
|
410
|
+
const h = sha512(seed);
|
|
411
|
+
h[0] &= 248;
|
|
412
|
+
h[31] &= 127;
|
|
413
|
+
h[31] |= 64;
|
|
414
|
+
let scalar = 0n;
|
|
415
|
+
for (let i = 0; i < 32; i++) {
|
|
416
|
+
scalar |= BigInt(h[i]) << BigInt(i * 8);
|
|
417
|
+
}
|
|
418
|
+
const pubPoint = scalarMult(scalar, ED25519_G);
|
|
419
|
+
const publicKey = pointToBytes(pubPoint);
|
|
420
|
+
return { privateKey: seed, publicKey };
|
|
421
|
+
}
|
|
422
|
+
function ed25519Sign(message, privateKey) {
|
|
423
|
+
const h = sha512(privateKey);
|
|
424
|
+
h[0] &= 248;
|
|
425
|
+
h[31] &= 127;
|
|
426
|
+
h[31] |= 64;
|
|
427
|
+
let scalar = 0n;
|
|
428
|
+
for (let i = 0; i < 32; i++) {
|
|
429
|
+
scalar |= BigInt(h[i]) << BigInt(i * 8);
|
|
430
|
+
}
|
|
431
|
+
const pubPoint = scalarMult(scalar, ED25519_G);
|
|
432
|
+
const publicKey = pointToBytes(pubPoint);
|
|
433
|
+
const prefix = h.slice(32);
|
|
434
|
+
const rHash = sha512(new Uint8Array([...prefix, ...message]));
|
|
435
|
+
let r = 0n;
|
|
436
|
+
for (let i = 0; i < 64; i++) {
|
|
437
|
+
r |= BigInt(rHash[i]) << BigInt(i * 8);
|
|
438
|
+
}
|
|
439
|
+
r = mod(r, ED25519_L);
|
|
440
|
+
const R = scalarMult(r, ED25519_G);
|
|
441
|
+
const Rbytes = pointToBytes(R);
|
|
442
|
+
const kHash = sha512(new Uint8Array([...Rbytes, ...publicKey, ...message]));
|
|
443
|
+
let k = 0n;
|
|
444
|
+
for (let i = 0; i < 64; i++) {
|
|
445
|
+
k |= BigInt(kHash[i]) << BigInt(i * 8);
|
|
446
|
+
}
|
|
447
|
+
k = mod(k, ED25519_L);
|
|
448
|
+
const s = mod(r + k * scalar, ED25519_L);
|
|
449
|
+
const signature = new Uint8Array(64);
|
|
450
|
+
signature.set(Rbytes);
|
|
451
|
+
for (let i = 0; i < 32; i++) {
|
|
452
|
+
signature[32 + i] = Number(s >> BigInt(i * 8) & 0xffn);
|
|
453
|
+
}
|
|
454
|
+
return signature;
|
|
455
|
+
}
|
|
456
|
+
function ed25519Verify(message, signature, publicKey) {
|
|
457
|
+
if (signature.length !== 64 || publicKey.length !== 32) return false;
|
|
458
|
+
try {
|
|
459
|
+
const Rbytes = signature.slice(0, 32);
|
|
460
|
+
const R = bytesToPoint(Rbytes);
|
|
461
|
+
const A = bytesToPoint(publicKey);
|
|
462
|
+
let s = 0n;
|
|
463
|
+
for (let i = 0; i < 32; i++) {
|
|
464
|
+
s |= BigInt(signature[32 + i]) << BigInt(i * 8);
|
|
465
|
+
}
|
|
466
|
+
if (s >= ED25519_L) return false;
|
|
467
|
+
const kHash = sha512(new Uint8Array([...Rbytes, ...publicKey, ...message]));
|
|
468
|
+
let k = 0n;
|
|
469
|
+
for (let i = 0; i < 64; i++) {
|
|
470
|
+
k |= BigInt(kHash[i]) << BigInt(i * 8);
|
|
471
|
+
}
|
|
472
|
+
k = mod(k, ED25519_L);
|
|
473
|
+
const sB = scalarMult(s, ED25519_G);
|
|
474
|
+
const kA = scalarMult(k, A);
|
|
475
|
+
const RkA = pointAdd(R, kA);
|
|
476
|
+
const left = pointToBytes(sB);
|
|
477
|
+
const right = pointToBytes(RkA);
|
|
478
|
+
for (let i = 0; i < 32; i++) {
|
|
479
|
+
if (left[i] !== right[i]) return false;
|
|
480
|
+
}
|
|
481
|
+
return true;
|
|
482
|
+
} catch {
|
|
483
|
+
return false;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
function getRandomBytes(len) {
|
|
487
|
+
const bytes = new Uint8Array(len);
|
|
488
|
+
for (let i = 0; i < len; i++) {
|
|
489
|
+
bytes[i] = Math.floor(Math.random() * 256);
|
|
490
|
+
}
|
|
491
|
+
return bytes;
|
|
492
|
+
}
|
|
493
|
+
function signing() {
|
|
494
|
+
try {
|
|
495
|
+
const inputStr = Host.inputString();
|
|
496
|
+
const input = JSON.parse(inputStr);
|
|
497
|
+
const encoding = input.encoding ?? "hex";
|
|
498
|
+
const algorithm = input.algorithm;
|
|
499
|
+
if (algorithm !== "ed25519") {
|
|
500
|
+
throw new Error("Only ed25519 is currently supported");
|
|
501
|
+
}
|
|
502
|
+
let output;
|
|
503
|
+
switch (input.operation) {
|
|
504
|
+
case "generateKeypair": {
|
|
505
|
+
const seed = getRandomBytes(32);
|
|
506
|
+
const { privateKey, publicKey } = ed25519GenerateKeypair(seed);
|
|
507
|
+
output = {
|
|
508
|
+
privateKey: encodeKey(privateKey, encoding),
|
|
509
|
+
publicKey: encodeKey(publicKey, encoding),
|
|
510
|
+
algorithm
|
|
511
|
+
};
|
|
512
|
+
break;
|
|
513
|
+
}
|
|
514
|
+
case "sign": {
|
|
515
|
+
if (!input.privateKey) throw new Error("privateKey required for signing");
|
|
516
|
+
if (!input.message) throw new Error("message required for signing");
|
|
517
|
+
const privateKey = decodeKey(input.privateKey, encoding);
|
|
518
|
+
const message = new TextEncoder().encode(input.message);
|
|
519
|
+
const signature = ed25519Sign(message, privateKey);
|
|
520
|
+
output = {
|
|
521
|
+
signature: encodeKey(signature, encoding),
|
|
522
|
+
algorithm
|
|
523
|
+
};
|
|
524
|
+
break;
|
|
525
|
+
}
|
|
526
|
+
case "verify": {
|
|
527
|
+
if (!input.publicKey) throw new Error("publicKey required for verification");
|
|
528
|
+
if (!input.signature) throw new Error("signature required for verification");
|
|
529
|
+
if (!input.message) throw new Error("message required for verification");
|
|
530
|
+
const publicKey = decodeKey(input.publicKey, encoding);
|
|
531
|
+
const signature = decodeKey(input.signature, encoding);
|
|
532
|
+
const message = new TextEncoder().encode(input.message);
|
|
533
|
+
const valid = ed25519Verify(message, signature, publicKey);
|
|
534
|
+
output = {
|
|
535
|
+
valid,
|
|
536
|
+
algorithm
|
|
537
|
+
};
|
|
538
|
+
break;
|
|
539
|
+
}
|
|
540
|
+
default:
|
|
541
|
+
throw new Error(`Unknown operation: ${input.operation}`);
|
|
542
|
+
}
|
|
543
|
+
Host.outputString(JSON.stringify(output));
|
|
544
|
+
return 0;
|
|
545
|
+
} catch (error) {
|
|
546
|
+
const output = {
|
|
547
|
+
algorithm: "unknown",
|
|
548
|
+
error: error instanceof Error ? error.message : String(error)
|
|
549
|
+
};
|
|
550
|
+
Host.outputString(JSON.stringify(output));
|
|
551
|
+
return 1;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
var TextEncoder = class {
|
|
555
|
+
encode(str) {
|
|
556
|
+
const bytes = [];
|
|
557
|
+
for (let i = 0; i < str.length; i++) {
|
|
558
|
+
let c = str.charCodeAt(i);
|
|
559
|
+
if (c < 128) {
|
|
560
|
+
bytes.push(c);
|
|
561
|
+
} else if (c < 2048) {
|
|
562
|
+
bytes.push(192 | c >> 6, 128 | c & 63);
|
|
563
|
+
} else {
|
|
564
|
+
bytes.push(224 | c >> 12, 128 | c >> 6 & 63, 128 | c & 63);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
return new Uint8Array(bytes);
|
|
568
|
+
}
|
|
569
|
+
};
|