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