@paigy/mcp 0.12.1 → 0.13.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/dist/chunk-KNCXKXUG.js +224 -0
- package/dist/chunk-O6W2T6D2.js +3312 -0
- package/dist/index.js +134 -22
- package/dist/listen.js +6 -4
- package/dist/onboard.js +2 -2
- package/dist/statusline.js +3 -4
- package/package.json +4 -3
- package/dist/chunk-ACPCCDHI.js +0 -451
- package/dist/chunk-AILC3H3U.js +0 -81
- package/dist/chunk-GU7C5H6L.js +0 -15
|
@@ -0,0 +1,3312 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from 'node:module'; const require = __createRequire(import.meta.url);
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
9
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
10
|
+
}) : x)(function(x) {
|
|
11
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
12
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
13
|
+
});
|
|
14
|
+
var __commonJS = (cb, mod) => function __require2() {
|
|
15
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
16
|
+
};
|
|
17
|
+
var __copyProps = (to, from, except, desc) => {
|
|
18
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
19
|
+
for (let key of __getOwnPropNames(from))
|
|
20
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
21
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
22
|
+
}
|
|
23
|
+
return to;
|
|
24
|
+
};
|
|
25
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
26
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
27
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
28
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
29
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
30
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
31
|
+
mod
|
|
32
|
+
));
|
|
33
|
+
|
|
34
|
+
// ../../node_modules/.pnpm/tweetnacl@1.0.3/node_modules/tweetnacl/nacl-fast.js
|
|
35
|
+
var require_nacl_fast = __commonJS({
|
|
36
|
+
"../../node_modules/.pnpm/tweetnacl@1.0.3/node_modules/tweetnacl/nacl-fast.js"(exports, module) {
|
|
37
|
+
"use strict";
|
|
38
|
+
(function(nacl6) {
|
|
39
|
+
"use strict";
|
|
40
|
+
var gf = function(init) {
|
|
41
|
+
var i, r = new Float64Array(16);
|
|
42
|
+
if (init) for (i = 0; i < init.length; i++) r[i] = init[i];
|
|
43
|
+
return r;
|
|
44
|
+
};
|
|
45
|
+
var randombytes = function() {
|
|
46
|
+
throw new Error("no PRNG");
|
|
47
|
+
};
|
|
48
|
+
var _0 = new Uint8Array(16);
|
|
49
|
+
var _9 = new Uint8Array(32);
|
|
50
|
+
_9[0] = 9;
|
|
51
|
+
var gf0 = gf(), gf1 = gf([1]), _121665 = gf([56129, 1]), D = gf([30883, 4953, 19914, 30187, 55467, 16705, 2637, 112, 59544, 30585, 16505, 36039, 65139, 11119, 27886, 20995]), D2 = gf([61785, 9906, 39828, 60374, 45398, 33411, 5274, 224, 53552, 61171, 33010, 6542, 64743, 22239, 55772, 9222]), X = gf([54554, 36645, 11616, 51542, 42930, 38181, 51040, 26924, 56412, 64982, 57905, 49316, 21502, 52590, 14035, 8553]), Y = gf([26200, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214, 26214]), I = gf([41136, 18958, 6951, 50414, 58488, 44335, 6150, 12099, 55207, 15867, 153, 11085, 57099, 20417, 9344, 11139]);
|
|
52
|
+
function ts64(x, i, h, l) {
|
|
53
|
+
x[i] = h >> 24 & 255;
|
|
54
|
+
x[i + 1] = h >> 16 & 255;
|
|
55
|
+
x[i + 2] = h >> 8 & 255;
|
|
56
|
+
x[i + 3] = h & 255;
|
|
57
|
+
x[i + 4] = l >> 24 & 255;
|
|
58
|
+
x[i + 5] = l >> 16 & 255;
|
|
59
|
+
x[i + 6] = l >> 8 & 255;
|
|
60
|
+
x[i + 7] = l & 255;
|
|
61
|
+
}
|
|
62
|
+
function vn(x, xi, y, yi, n) {
|
|
63
|
+
var i, d = 0;
|
|
64
|
+
for (i = 0; i < n; i++) d |= x[xi + i] ^ y[yi + i];
|
|
65
|
+
return (1 & d - 1 >>> 8) - 1;
|
|
66
|
+
}
|
|
67
|
+
function crypto_verify_16(x, xi, y, yi) {
|
|
68
|
+
return vn(x, xi, y, yi, 16);
|
|
69
|
+
}
|
|
70
|
+
function crypto_verify_32(x, xi, y, yi) {
|
|
71
|
+
return vn(x, xi, y, yi, 32);
|
|
72
|
+
}
|
|
73
|
+
function core_salsa20(o, p, k, c) {
|
|
74
|
+
var j0 = c[0] & 255 | (c[1] & 255) << 8 | (c[2] & 255) << 16 | (c[3] & 255) << 24, j1 = k[0] & 255 | (k[1] & 255) << 8 | (k[2] & 255) << 16 | (k[3] & 255) << 24, j2 = k[4] & 255 | (k[5] & 255) << 8 | (k[6] & 255) << 16 | (k[7] & 255) << 24, j3 = k[8] & 255 | (k[9] & 255) << 8 | (k[10] & 255) << 16 | (k[11] & 255) << 24, j4 = k[12] & 255 | (k[13] & 255) << 8 | (k[14] & 255) << 16 | (k[15] & 255) << 24, j5 = c[4] & 255 | (c[5] & 255) << 8 | (c[6] & 255) << 16 | (c[7] & 255) << 24, j6 = p[0] & 255 | (p[1] & 255) << 8 | (p[2] & 255) << 16 | (p[3] & 255) << 24, j7 = p[4] & 255 | (p[5] & 255) << 8 | (p[6] & 255) << 16 | (p[7] & 255) << 24, j8 = p[8] & 255 | (p[9] & 255) << 8 | (p[10] & 255) << 16 | (p[11] & 255) << 24, j9 = p[12] & 255 | (p[13] & 255) << 8 | (p[14] & 255) << 16 | (p[15] & 255) << 24, j10 = c[8] & 255 | (c[9] & 255) << 8 | (c[10] & 255) << 16 | (c[11] & 255) << 24, j11 = k[16] & 255 | (k[17] & 255) << 8 | (k[18] & 255) << 16 | (k[19] & 255) << 24, j12 = k[20] & 255 | (k[21] & 255) << 8 | (k[22] & 255) << 16 | (k[23] & 255) << 24, j13 = k[24] & 255 | (k[25] & 255) << 8 | (k[26] & 255) << 16 | (k[27] & 255) << 24, j14 = k[28] & 255 | (k[29] & 255) << 8 | (k[30] & 255) << 16 | (k[31] & 255) << 24, j15 = c[12] & 255 | (c[13] & 255) << 8 | (c[14] & 255) << 16 | (c[15] & 255) << 24;
|
|
75
|
+
var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7, x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14, x15 = j15, u;
|
|
76
|
+
for (var i = 0; i < 20; i += 2) {
|
|
77
|
+
u = x0 + x12 | 0;
|
|
78
|
+
x4 ^= u << 7 | u >>> 32 - 7;
|
|
79
|
+
u = x4 + x0 | 0;
|
|
80
|
+
x8 ^= u << 9 | u >>> 32 - 9;
|
|
81
|
+
u = x8 + x4 | 0;
|
|
82
|
+
x12 ^= u << 13 | u >>> 32 - 13;
|
|
83
|
+
u = x12 + x8 | 0;
|
|
84
|
+
x0 ^= u << 18 | u >>> 32 - 18;
|
|
85
|
+
u = x5 + x1 | 0;
|
|
86
|
+
x9 ^= u << 7 | u >>> 32 - 7;
|
|
87
|
+
u = x9 + x5 | 0;
|
|
88
|
+
x13 ^= u << 9 | u >>> 32 - 9;
|
|
89
|
+
u = x13 + x9 | 0;
|
|
90
|
+
x1 ^= u << 13 | u >>> 32 - 13;
|
|
91
|
+
u = x1 + x13 | 0;
|
|
92
|
+
x5 ^= u << 18 | u >>> 32 - 18;
|
|
93
|
+
u = x10 + x6 | 0;
|
|
94
|
+
x14 ^= u << 7 | u >>> 32 - 7;
|
|
95
|
+
u = x14 + x10 | 0;
|
|
96
|
+
x2 ^= u << 9 | u >>> 32 - 9;
|
|
97
|
+
u = x2 + x14 | 0;
|
|
98
|
+
x6 ^= u << 13 | u >>> 32 - 13;
|
|
99
|
+
u = x6 + x2 | 0;
|
|
100
|
+
x10 ^= u << 18 | u >>> 32 - 18;
|
|
101
|
+
u = x15 + x11 | 0;
|
|
102
|
+
x3 ^= u << 7 | u >>> 32 - 7;
|
|
103
|
+
u = x3 + x15 | 0;
|
|
104
|
+
x7 ^= u << 9 | u >>> 32 - 9;
|
|
105
|
+
u = x7 + x3 | 0;
|
|
106
|
+
x11 ^= u << 13 | u >>> 32 - 13;
|
|
107
|
+
u = x11 + x7 | 0;
|
|
108
|
+
x15 ^= u << 18 | u >>> 32 - 18;
|
|
109
|
+
u = x0 + x3 | 0;
|
|
110
|
+
x1 ^= u << 7 | u >>> 32 - 7;
|
|
111
|
+
u = x1 + x0 | 0;
|
|
112
|
+
x2 ^= u << 9 | u >>> 32 - 9;
|
|
113
|
+
u = x2 + x1 | 0;
|
|
114
|
+
x3 ^= u << 13 | u >>> 32 - 13;
|
|
115
|
+
u = x3 + x2 | 0;
|
|
116
|
+
x0 ^= u << 18 | u >>> 32 - 18;
|
|
117
|
+
u = x5 + x4 | 0;
|
|
118
|
+
x6 ^= u << 7 | u >>> 32 - 7;
|
|
119
|
+
u = x6 + x5 | 0;
|
|
120
|
+
x7 ^= u << 9 | u >>> 32 - 9;
|
|
121
|
+
u = x7 + x6 | 0;
|
|
122
|
+
x4 ^= u << 13 | u >>> 32 - 13;
|
|
123
|
+
u = x4 + x7 | 0;
|
|
124
|
+
x5 ^= u << 18 | u >>> 32 - 18;
|
|
125
|
+
u = x10 + x9 | 0;
|
|
126
|
+
x11 ^= u << 7 | u >>> 32 - 7;
|
|
127
|
+
u = x11 + x10 | 0;
|
|
128
|
+
x8 ^= u << 9 | u >>> 32 - 9;
|
|
129
|
+
u = x8 + x11 | 0;
|
|
130
|
+
x9 ^= u << 13 | u >>> 32 - 13;
|
|
131
|
+
u = x9 + x8 | 0;
|
|
132
|
+
x10 ^= u << 18 | u >>> 32 - 18;
|
|
133
|
+
u = x15 + x14 | 0;
|
|
134
|
+
x12 ^= u << 7 | u >>> 32 - 7;
|
|
135
|
+
u = x12 + x15 | 0;
|
|
136
|
+
x13 ^= u << 9 | u >>> 32 - 9;
|
|
137
|
+
u = x13 + x12 | 0;
|
|
138
|
+
x14 ^= u << 13 | u >>> 32 - 13;
|
|
139
|
+
u = x14 + x13 | 0;
|
|
140
|
+
x15 ^= u << 18 | u >>> 32 - 18;
|
|
141
|
+
}
|
|
142
|
+
x0 = x0 + j0 | 0;
|
|
143
|
+
x1 = x1 + j1 | 0;
|
|
144
|
+
x2 = x2 + j2 | 0;
|
|
145
|
+
x3 = x3 + j3 | 0;
|
|
146
|
+
x4 = x4 + j4 | 0;
|
|
147
|
+
x5 = x5 + j5 | 0;
|
|
148
|
+
x6 = x6 + j6 | 0;
|
|
149
|
+
x7 = x7 + j7 | 0;
|
|
150
|
+
x8 = x8 + j8 | 0;
|
|
151
|
+
x9 = x9 + j9 | 0;
|
|
152
|
+
x10 = x10 + j10 | 0;
|
|
153
|
+
x11 = x11 + j11 | 0;
|
|
154
|
+
x12 = x12 + j12 | 0;
|
|
155
|
+
x13 = x13 + j13 | 0;
|
|
156
|
+
x14 = x14 + j14 | 0;
|
|
157
|
+
x15 = x15 + j15 | 0;
|
|
158
|
+
o[0] = x0 >>> 0 & 255;
|
|
159
|
+
o[1] = x0 >>> 8 & 255;
|
|
160
|
+
o[2] = x0 >>> 16 & 255;
|
|
161
|
+
o[3] = x0 >>> 24 & 255;
|
|
162
|
+
o[4] = x1 >>> 0 & 255;
|
|
163
|
+
o[5] = x1 >>> 8 & 255;
|
|
164
|
+
o[6] = x1 >>> 16 & 255;
|
|
165
|
+
o[7] = x1 >>> 24 & 255;
|
|
166
|
+
o[8] = x2 >>> 0 & 255;
|
|
167
|
+
o[9] = x2 >>> 8 & 255;
|
|
168
|
+
o[10] = x2 >>> 16 & 255;
|
|
169
|
+
o[11] = x2 >>> 24 & 255;
|
|
170
|
+
o[12] = x3 >>> 0 & 255;
|
|
171
|
+
o[13] = x3 >>> 8 & 255;
|
|
172
|
+
o[14] = x3 >>> 16 & 255;
|
|
173
|
+
o[15] = x3 >>> 24 & 255;
|
|
174
|
+
o[16] = x4 >>> 0 & 255;
|
|
175
|
+
o[17] = x4 >>> 8 & 255;
|
|
176
|
+
o[18] = x4 >>> 16 & 255;
|
|
177
|
+
o[19] = x4 >>> 24 & 255;
|
|
178
|
+
o[20] = x5 >>> 0 & 255;
|
|
179
|
+
o[21] = x5 >>> 8 & 255;
|
|
180
|
+
o[22] = x5 >>> 16 & 255;
|
|
181
|
+
o[23] = x5 >>> 24 & 255;
|
|
182
|
+
o[24] = x6 >>> 0 & 255;
|
|
183
|
+
o[25] = x6 >>> 8 & 255;
|
|
184
|
+
o[26] = x6 >>> 16 & 255;
|
|
185
|
+
o[27] = x6 >>> 24 & 255;
|
|
186
|
+
o[28] = x7 >>> 0 & 255;
|
|
187
|
+
o[29] = x7 >>> 8 & 255;
|
|
188
|
+
o[30] = x7 >>> 16 & 255;
|
|
189
|
+
o[31] = x7 >>> 24 & 255;
|
|
190
|
+
o[32] = x8 >>> 0 & 255;
|
|
191
|
+
o[33] = x8 >>> 8 & 255;
|
|
192
|
+
o[34] = x8 >>> 16 & 255;
|
|
193
|
+
o[35] = x8 >>> 24 & 255;
|
|
194
|
+
o[36] = x9 >>> 0 & 255;
|
|
195
|
+
o[37] = x9 >>> 8 & 255;
|
|
196
|
+
o[38] = x9 >>> 16 & 255;
|
|
197
|
+
o[39] = x9 >>> 24 & 255;
|
|
198
|
+
o[40] = x10 >>> 0 & 255;
|
|
199
|
+
o[41] = x10 >>> 8 & 255;
|
|
200
|
+
o[42] = x10 >>> 16 & 255;
|
|
201
|
+
o[43] = x10 >>> 24 & 255;
|
|
202
|
+
o[44] = x11 >>> 0 & 255;
|
|
203
|
+
o[45] = x11 >>> 8 & 255;
|
|
204
|
+
o[46] = x11 >>> 16 & 255;
|
|
205
|
+
o[47] = x11 >>> 24 & 255;
|
|
206
|
+
o[48] = x12 >>> 0 & 255;
|
|
207
|
+
o[49] = x12 >>> 8 & 255;
|
|
208
|
+
o[50] = x12 >>> 16 & 255;
|
|
209
|
+
o[51] = x12 >>> 24 & 255;
|
|
210
|
+
o[52] = x13 >>> 0 & 255;
|
|
211
|
+
o[53] = x13 >>> 8 & 255;
|
|
212
|
+
o[54] = x13 >>> 16 & 255;
|
|
213
|
+
o[55] = x13 >>> 24 & 255;
|
|
214
|
+
o[56] = x14 >>> 0 & 255;
|
|
215
|
+
o[57] = x14 >>> 8 & 255;
|
|
216
|
+
o[58] = x14 >>> 16 & 255;
|
|
217
|
+
o[59] = x14 >>> 24 & 255;
|
|
218
|
+
o[60] = x15 >>> 0 & 255;
|
|
219
|
+
o[61] = x15 >>> 8 & 255;
|
|
220
|
+
o[62] = x15 >>> 16 & 255;
|
|
221
|
+
o[63] = x15 >>> 24 & 255;
|
|
222
|
+
}
|
|
223
|
+
function core_hsalsa20(o, p, k, c) {
|
|
224
|
+
var j0 = c[0] & 255 | (c[1] & 255) << 8 | (c[2] & 255) << 16 | (c[3] & 255) << 24, j1 = k[0] & 255 | (k[1] & 255) << 8 | (k[2] & 255) << 16 | (k[3] & 255) << 24, j2 = k[4] & 255 | (k[5] & 255) << 8 | (k[6] & 255) << 16 | (k[7] & 255) << 24, j3 = k[8] & 255 | (k[9] & 255) << 8 | (k[10] & 255) << 16 | (k[11] & 255) << 24, j4 = k[12] & 255 | (k[13] & 255) << 8 | (k[14] & 255) << 16 | (k[15] & 255) << 24, j5 = c[4] & 255 | (c[5] & 255) << 8 | (c[6] & 255) << 16 | (c[7] & 255) << 24, j6 = p[0] & 255 | (p[1] & 255) << 8 | (p[2] & 255) << 16 | (p[3] & 255) << 24, j7 = p[4] & 255 | (p[5] & 255) << 8 | (p[6] & 255) << 16 | (p[7] & 255) << 24, j8 = p[8] & 255 | (p[9] & 255) << 8 | (p[10] & 255) << 16 | (p[11] & 255) << 24, j9 = p[12] & 255 | (p[13] & 255) << 8 | (p[14] & 255) << 16 | (p[15] & 255) << 24, j10 = c[8] & 255 | (c[9] & 255) << 8 | (c[10] & 255) << 16 | (c[11] & 255) << 24, j11 = k[16] & 255 | (k[17] & 255) << 8 | (k[18] & 255) << 16 | (k[19] & 255) << 24, j12 = k[20] & 255 | (k[21] & 255) << 8 | (k[22] & 255) << 16 | (k[23] & 255) << 24, j13 = k[24] & 255 | (k[25] & 255) << 8 | (k[26] & 255) << 16 | (k[27] & 255) << 24, j14 = k[28] & 255 | (k[29] & 255) << 8 | (k[30] & 255) << 16 | (k[31] & 255) << 24, j15 = c[12] & 255 | (c[13] & 255) << 8 | (c[14] & 255) << 16 | (c[15] & 255) << 24;
|
|
225
|
+
var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7, x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14, x15 = j15, u;
|
|
226
|
+
for (var i = 0; i < 20; i += 2) {
|
|
227
|
+
u = x0 + x12 | 0;
|
|
228
|
+
x4 ^= u << 7 | u >>> 32 - 7;
|
|
229
|
+
u = x4 + x0 | 0;
|
|
230
|
+
x8 ^= u << 9 | u >>> 32 - 9;
|
|
231
|
+
u = x8 + x4 | 0;
|
|
232
|
+
x12 ^= u << 13 | u >>> 32 - 13;
|
|
233
|
+
u = x12 + x8 | 0;
|
|
234
|
+
x0 ^= u << 18 | u >>> 32 - 18;
|
|
235
|
+
u = x5 + x1 | 0;
|
|
236
|
+
x9 ^= u << 7 | u >>> 32 - 7;
|
|
237
|
+
u = x9 + x5 | 0;
|
|
238
|
+
x13 ^= u << 9 | u >>> 32 - 9;
|
|
239
|
+
u = x13 + x9 | 0;
|
|
240
|
+
x1 ^= u << 13 | u >>> 32 - 13;
|
|
241
|
+
u = x1 + x13 | 0;
|
|
242
|
+
x5 ^= u << 18 | u >>> 32 - 18;
|
|
243
|
+
u = x10 + x6 | 0;
|
|
244
|
+
x14 ^= u << 7 | u >>> 32 - 7;
|
|
245
|
+
u = x14 + x10 | 0;
|
|
246
|
+
x2 ^= u << 9 | u >>> 32 - 9;
|
|
247
|
+
u = x2 + x14 | 0;
|
|
248
|
+
x6 ^= u << 13 | u >>> 32 - 13;
|
|
249
|
+
u = x6 + x2 | 0;
|
|
250
|
+
x10 ^= u << 18 | u >>> 32 - 18;
|
|
251
|
+
u = x15 + x11 | 0;
|
|
252
|
+
x3 ^= u << 7 | u >>> 32 - 7;
|
|
253
|
+
u = x3 + x15 | 0;
|
|
254
|
+
x7 ^= u << 9 | u >>> 32 - 9;
|
|
255
|
+
u = x7 + x3 | 0;
|
|
256
|
+
x11 ^= u << 13 | u >>> 32 - 13;
|
|
257
|
+
u = x11 + x7 | 0;
|
|
258
|
+
x15 ^= u << 18 | u >>> 32 - 18;
|
|
259
|
+
u = x0 + x3 | 0;
|
|
260
|
+
x1 ^= u << 7 | u >>> 32 - 7;
|
|
261
|
+
u = x1 + x0 | 0;
|
|
262
|
+
x2 ^= u << 9 | u >>> 32 - 9;
|
|
263
|
+
u = x2 + x1 | 0;
|
|
264
|
+
x3 ^= u << 13 | u >>> 32 - 13;
|
|
265
|
+
u = x3 + x2 | 0;
|
|
266
|
+
x0 ^= u << 18 | u >>> 32 - 18;
|
|
267
|
+
u = x5 + x4 | 0;
|
|
268
|
+
x6 ^= u << 7 | u >>> 32 - 7;
|
|
269
|
+
u = x6 + x5 | 0;
|
|
270
|
+
x7 ^= u << 9 | u >>> 32 - 9;
|
|
271
|
+
u = x7 + x6 | 0;
|
|
272
|
+
x4 ^= u << 13 | u >>> 32 - 13;
|
|
273
|
+
u = x4 + x7 | 0;
|
|
274
|
+
x5 ^= u << 18 | u >>> 32 - 18;
|
|
275
|
+
u = x10 + x9 | 0;
|
|
276
|
+
x11 ^= u << 7 | u >>> 32 - 7;
|
|
277
|
+
u = x11 + x10 | 0;
|
|
278
|
+
x8 ^= u << 9 | u >>> 32 - 9;
|
|
279
|
+
u = x8 + x11 | 0;
|
|
280
|
+
x9 ^= u << 13 | u >>> 32 - 13;
|
|
281
|
+
u = x9 + x8 | 0;
|
|
282
|
+
x10 ^= u << 18 | u >>> 32 - 18;
|
|
283
|
+
u = x15 + x14 | 0;
|
|
284
|
+
x12 ^= u << 7 | u >>> 32 - 7;
|
|
285
|
+
u = x12 + x15 | 0;
|
|
286
|
+
x13 ^= u << 9 | u >>> 32 - 9;
|
|
287
|
+
u = x13 + x12 | 0;
|
|
288
|
+
x14 ^= u << 13 | u >>> 32 - 13;
|
|
289
|
+
u = x14 + x13 | 0;
|
|
290
|
+
x15 ^= u << 18 | u >>> 32 - 18;
|
|
291
|
+
}
|
|
292
|
+
o[0] = x0 >>> 0 & 255;
|
|
293
|
+
o[1] = x0 >>> 8 & 255;
|
|
294
|
+
o[2] = x0 >>> 16 & 255;
|
|
295
|
+
o[3] = x0 >>> 24 & 255;
|
|
296
|
+
o[4] = x5 >>> 0 & 255;
|
|
297
|
+
o[5] = x5 >>> 8 & 255;
|
|
298
|
+
o[6] = x5 >>> 16 & 255;
|
|
299
|
+
o[7] = x5 >>> 24 & 255;
|
|
300
|
+
o[8] = x10 >>> 0 & 255;
|
|
301
|
+
o[9] = x10 >>> 8 & 255;
|
|
302
|
+
o[10] = x10 >>> 16 & 255;
|
|
303
|
+
o[11] = x10 >>> 24 & 255;
|
|
304
|
+
o[12] = x15 >>> 0 & 255;
|
|
305
|
+
o[13] = x15 >>> 8 & 255;
|
|
306
|
+
o[14] = x15 >>> 16 & 255;
|
|
307
|
+
o[15] = x15 >>> 24 & 255;
|
|
308
|
+
o[16] = x6 >>> 0 & 255;
|
|
309
|
+
o[17] = x6 >>> 8 & 255;
|
|
310
|
+
o[18] = x6 >>> 16 & 255;
|
|
311
|
+
o[19] = x6 >>> 24 & 255;
|
|
312
|
+
o[20] = x7 >>> 0 & 255;
|
|
313
|
+
o[21] = x7 >>> 8 & 255;
|
|
314
|
+
o[22] = x7 >>> 16 & 255;
|
|
315
|
+
o[23] = x7 >>> 24 & 255;
|
|
316
|
+
o[24] = x8 >>> 0 & 255;
|
|
317
|
+
o[25] = x8 >>> 8 & 255;
|
|
318
|
+
o[26] = x8 >>> 16 & 255;
|
|
319
|
+
o[27] = x8 >>> 24 & 255;
|
|
320
|
+
o[28] = x9 >>> 0 & 255;
|
|
321
|
+
o[29] = x9 >>> 8 & 255;
|
|
322
|
+
o[30] = x9 >>> 16 & 255;
|
|
323
|
+
o[31] = x9 >>> 24 & 255;
|
|
324
|
+
}
|
|
325
|
+
function crypto_core_salsa20(out, inp, k, c) {
|
|
326
|
+
core_salsa20(out, inp, k, c);
|
|
327
|
+
}
|
|
328
|
+
function crypto_core_hsalsa20(out, inp, k, c) {
|
|
329
|
+
core_hsalsa20(out, inp, k, c);
|
|
330
|
+
}
|
|
331
|
+
var sigma = new Uint8Array([101, 120, 112, 97, 110, 100, 32, 51, 50, 45, 98, 121, 116, 101, 32, 107]);
|
|
332
|
+
function crypto_stream_salsa20_xor(c, cpos, m, mpos, b, n, k) {
|
|
333
|
+
var z2 = new Uint8Array(16), x = new Uint8Array(64);
|
|
334
|
+
var u, i;
|
|
335
|
+
for (i = 0; i < 16; i++) z2[i] = 0;
|
|
336
|
+
for (i = 0; i < 8; i++) z2[i] = n[i];
|
|
337
|
+
while (b >= 64) {
|
|
338
|
+
crypto_core_salsa20(x, z2, k, sigma);
|
|
339
|
+
for (i = 0; i < 64; i++) c[cpos + i] = m[mpos + i] ^ x[i];
|
|
340
|
+
u = 1;
|
|
341
|
+
for (i = 8; i < 16; i++) {
|
|
342
|
+
u = u + (z2[i] & 255) | 0;
|
|
343
|
+
z2[i] = u & 255;
|
|
344
|
+
u >>>= 8;
|
|
345
|
+
}
|
|
346
|
+
b -= 64;
|
|
347
|
+
cpos += 64;
|
|
348
|
+
mpos += 64;
|
|
349
|
+
}
|
|
350
|
+
if (b > 0) {
|
|
351
|
+
crypto_core_salsa20(x, z2, k, sigma);
|
|
352
|
+
for (i = 0; i < b; i++) c[cpos + i] = m[mpos + i] ^ x[i];
|
|
353
|
+
}
|
|
354
|
+
return 0;
|
|
355
|
+
}
|
|
356
|
+
function crypto_stream_salsa20(c, cpos, b, n, k) {
|
|
357
|
+
var z2 = new Uint8Array(16), x = new Uint8Array(64);
|
|
358
|
+
var u, i;
|
|
359
|
+
for (i = 0; i < 16; i++) z2[i] = 0;
|
|
360
|
+
for (i = 0; i < 8; i++) z2[i] = n[i];
|
|
361
|
+
while (b >= 64) {
|
|
362
|
+
crypto_core_salsa20(x, z2, k, sigma);
|
|
363
|
+
for (i = 0; i < 64; i++) c[cpos + i] = x[i];
|
|
364
|
+
u = 1;
|
|
365
|
+
for (i = 8; i < 16; i++) {
|
|
366
|
+
u = u + (z2[i] & 255) | 0;
|
|
367
|
+
z2[i] = u & 255;
|
|
368
|
+
u >>>= 8;
|
|
369
|
+
}
|
|
370
|
+
b -= 64;
|
|
371
|
+
cpos += 64;
|
|
372
|
+
}
|
|
373
|
+
if (b > 0) {
|
|
374
|
+
crypto_core_salsa20(x, z2, k, sigma);
|
|
375
|
+
for (i = 0; i < b; i++) c[cpos + i] = x[i];
|
|
376
|
+
}
|
|
377
|
+
return 0;
|
|
378
|
+
}
|
|
379
|
+
function crypto_stream(c, cpos, d, n, k) {
|
|
380
|
+
var s = new Uint8Array(32);
|
|
381
|
+
crypto_core_hsalsa20(s, n, k, sigma);
|
|
382
|
+
var sn = new Uint8Array(8);
|
|
383
|
+
for (var i = 0; i < 8; i++) sn[i] = n[i + 16];
|
|
384
|
+
return crypto_stream_salsa20(c, cpos, d, sn, s);
|
|
385
|
+
}
|
|
386
|
+
function crypto_stream_xor(c, cpos, m, mpos, d, n, k) {
|
|
387
|
+
var s = new Uint8Array(32);
|
|
388
|
+
crypto_core_hsalsa20(s, n, k, sigma);
|
|
389
|
+
var sn = new Uint8Array(8);
|
|
390
|
+
for (var i = 0; i < 8; i++) sn[i] = n[i + 16];
|
|
391
|
+
return crypto_stream_salsa20_xor(c, cpos, m, mpos, d, sn, s);
|
|
392
|
+
}
|
|
393
|
+
var poly1305 = function(key) {
|
|
394
|
+
this.buffer = new Uint8Array(16);
|
|
395
|
+
this.r = new Uint16Array(10);
|
|
396
|
+
this.h = new Uint16Array(10);
|
|
397
|
+
this.pad = new Uint16Array(8);
|
|
398
|
+
this.leftover = 0;
|
|
399
|
+
this.fin = 0;
|
|
400
|
+
var t0, t1, t2, t3, t4, t5, t6, t7;
|
|
401
|
+
t0 = key[0] & 255 | (key[1] & 255) << 8;
|
|
402
|
+
this.r[0] = t0 & 8191;
|
|
403
|
+
t1 = key[2] & 255 | (key[3] & 255) << 8;
|
|
404
|
+
this.r[1] = (t0 >>> 13 | t1 << 3) & 8191;
|
|
405
|
+
t2 = key[4] & 255 | (key[5] & 255) << 8;
|
|
406
|
+
this.r[2] = (t1 >>> 10 | t2 << 6) & 7939;
|
|
407
|
+
t3 = key[6] & 255 | (key[7] & 255) << 8;
|
|
408
|
+
this.r[3] = (t2 >>> 7 | t3 << 9) & 8191;
|
|
409
|
+
t4 = key[8] & 255 | (key[9] & 255) << 8;
|
|
410
|
+
this.r[4] = (t3 >>> 4 | t4 << 12) & 255;
|
|
411
|
+
this.r[5] = t4 >>> 1 & 8190;
|
|
412
|
+
t5 = key[10] & 255 | (key[11] & 255) << 8;
|
|
413
|
+
this.r[6] = (t4 >>> 14 | t5 << 2) & 8191;
|
|
414
|
+
t6 = key[12] & 255 | (key[13] & 255) << 8;
|
|
415
|
+
this.r[7] = (t5 >>> 11 | t6 << 5) & 8065;
|
|
416
|
+
t7 = key[14] & 255 | (key[15] & 255) << 8;
|
|
417
|
+
this.r[8] = (t6 >>> 8 | t7 << 8) & 8191;
|
|
418
|
+
this.r[9] = t7 >>> 5 & 127;
|
|
419
|
+
this.pad[0] = key[16] & 255 | (key[17] & 255) << 8;
|
|
420
|
+
this.pad[1] = key[18] & 255 | (key[19] & 255) << 8;
|
|
421
|
+
this.pad[2] = key[20] & 255 | (key[21] & 255) << 8;
|
|
422
|
+
this.pad[3] = key[22] & 255 | (key[23] & 255) << 8;
|
|
423
|
+
this.pad[4] = key[24] & 255 | (key[25] & 255) << 8;
|
|
424
|
+
this.pad[5] = key[26] & 255 | (key[27] & 255) << 8;
|
|
425
|
+
this.pad[6] = key[28] & 255 | (key[29] & 255) << 8;
|
|
426
|
+
this.pad[7] = key[30] & 255 | (key[31] & 255) << 8;
|
|
427
|
+
};
|
|
428
|
+
poly1305.prototype.blocks = function(m, mpos, bytes) {
|
|
429
|
+
var hibit = this.fin ? 0 : 1 << 11;
|
|
430
|
+
var t0, t1, t2, t3, t4, t5, t6, t7, c;
|
|
431
|
+
var d0, d1, d2, d3, d4, d5, d6, d7, d8, d9;
|
|
432
|
+
var h0 = this.h[0], h1 = this.h[1], h2 = this.h[2], h3 = this.h[3], h4 = this.h[4], h5 = this.h[5], h6 = this.h[6], h7 = this.h[7], h8 = this.h[8], h9 = this.h[9];
|
|
433
|
+
var r0 = this.r[0], r1 = this.r[1], r2 = this.r[2], r3 = this.r[3], r4 = this.r[4], r5 = this.r[5], r6 = this.r[6], r7 = this.r[7], r8 = this.r[8], r9 = this.r[9];
|
|
434
|
+
while (bytes >= 16) {
|
|
435
|
+
t0 = m[mpos + 0] & 255 | (m[mpos + 1] & 255) << 8;
|
|
436
|
+
h0 += t0 & 8191;
|
|
437
|
+
t1 = m[mpos + 2] & 255 | (m[mpos + 3] & 255) << 8;
|
|
438
|
+
h1 += (t0 >>> 13 | t1 << 3) & 8191;
|
|
439
|
+
t2 = m[mpos + 4] & 255 | (m[mpos + 5] & 255) << 8;
|
|
440
|
+
h2 += (t1 >>> 10 | t2 << 6) & 8191;
|
|
441
|
+
t3 = m[mpos + 6] & 255 | (m[mpos + 7] & 255) << 8;
|
|
442
|
+
h3 += (t2 >>> 7 | t3 << 9) & 8191;
|
|
443
|
+
t4 = m[mpos + 8] & 255 | (m[mpos + 9] & 255) << 8;
|
|
444
|
+
h4 += (t3 >>> 4 | t4 << 12) & 8191;
|
|
445
|
+
h5 += t4 >>> 1 & 8191;
|
|
446
|
+
t5 = m[mpos + 10] & 255 | (m[mpos + 11] & 255) << 8;
|
|
447
|
+
h6 += (t4 >>> 14 | t5 << 2) & 8191;
|
|
448
|
+
t6 = m[mpos + 12] & 255 | (m[mpos + 13] & 255) << 8;
|
|
449
|
+
h7 += (t5 >>> 11 | t6 << 5) & 8191;
|
|
450
|
+
t7 = m[mpos + 14] & 255 | (m[mpos + 15] & 255) << 8;
|
|
451
|
+
h8 += (t6 >>> 8 | t7 << 8) & 8191;
|
|
452
|
+
h9 += t7 >>> 5 | hibit;
|
|
453
|
+
c = 0;
|
|
454
|
+
d0 = c;
|
|
455
|
+
d0 += h0 * r0;
|
|
456
|
+
d0 += h1 * (5 * r9);
|
|
457
|
+
d0 += h2 * (5 * r8);
|
|
458
|
+
d0 += h3 * (5 * r7);
|
|
459
|
+
d0 += h4 * (5 * r6);
|
|
460
|
+
c = d0 >>> 13;
|
|
461
|
+
d0 &= 8191;
|
|
462
|
+
d0 += h5 * (5 * r5);
|
|
463
|
+
d0 += h6 * (5 * r4);
|
|
464
|
+
d0 += h7 * (5 * r3);
|
|
465
|
+
d0 += h8 * (5 * r2);
|
|
466
|
+
d0 += h9 * (5 * r1);
|
|
467
|
+
c += d0 >>> 13;
|
|
468
|
+
d0 &= 8191;
|
|
469
|
+
d1 = c;
|
|
470
|
+
d1 += h0 * r1;
|
|
471
|
+
d1 += h1 * r0;
|
|
472
|
+
d1 += h2 * (5 * r9);
|
|
473
|
+
d1 += h3 * (5 * r8);
|
|
474
|
+
d1 += h4 * (5 * r7);
|
|
475
|
+
c = d1 >>> 13;
|
|
476
|
+
d1 &= 8191;
|
|
477
|
+
d1 += h5 * (5 * r6);
|
|
478
|
+
d1 += h6 * (5 * r5);
|
|
479
|
+
d1 += h7 * (5 * r4);
|
|
480
|
+
d1 += h8 * (5 * r3);
|
|
481
|
+
d1 += h9 * (5 * r2);
|
|
482
|
+
c += d1 >>> 13;
|
|
483
|
+
d1 &= 8191;
|
|
484
|
+
d2 = c;
|
|
485
|
+
d2 += h0 * r2;
|
|
486
|
+
d2 += h1 * r1;
|
|
487
|
+
d2 += h2 * r0;
|
|
488
|
+
d2 += h3 * (5 * r9);
|
|
489
|
+
d2 += h4 * (5 * r8);
|
|
490
|
+
c = d2 >>> 13;
|
|
491
|
+
d2 &= 8191;
|
|
492
|
+
d2 += h5 * (5 * r7);
|
|
493
|
+
d2 += h6 * (5 * r6);
|
|
494
|
+
d2 += h7 * (5 * r5);
|
|
495
|
+
d2 += h8 * (5 * r4);
|
|
496
|
+
d2 += h9 * (5 * r3);
|
|
497
|
+
c += d2 >>> 13;
|
|
498
|
+
d2 &= 8191;
|
|
499
|
+
d3 = c;
|
|
500
|
+
d3 += h0 * r3;
|
|
501
|
+
d3 += h1 * r2;
|
|
502
|
+
d3 += h2 * r1;
|
|
503
|
+
d3 += h3 * r0;
|
|
504
|
+
d3 += h4 * (5 * r9);
|
|
505
|
+
c = d3 >>> 13;
|
|
506
|
+
d3 &= 8191;
|
|
507
|
+
d3 += h5 * (5 * r8);
|
|
508
|
+
d3 += h6 * (5 * r7);
|
|
509
|
+
d3 += h7 * (5 * r6);
|
|
510
|
+
d3 += h8 * (5 * r5);
|
|
511
|
+
d3 += h9 * (5 * r4);
|
|
512
|
+
c += d3 >>> 13;
|
|
513
|
+
d3 &= 8191;
|
|
514
|
+
d4 = c;
|
|
515
|
+
d4 += h0 * r4;
|
|
516
|
+
d4 += h1 * r3;
|
|
517
|
+
d4 += h2 * r2;
|
|
518
|
+
d4 += h3 * r1;
|
|
519
|
+
d4 += h4 * r0;
|
|
520
|
+
c = d4 >>> 13;
|
|
521
|
+
d4 &= 8191;
|
|
522
|
+
d4 += h5 * (5 * r9);
|
|
523
|
+
d4 += h6 * (5 * r8);
|
|
524
|
+
d4 += h7 * (5 * r7);
|
|
525
|
+
d4 += h8 * (5 * r6);
|
|
526
|
+
d4 += h9 * (5 * r5);
|
|
527
|
+
c += d4 >>> 13;
|
|
528
|
+
d4 &= 8191;
|
|
529
|
+
d5 = c;
|
|
530
|
+
d5 += h0 * r5;
|
|
531
|
+
d5 += h1 * r4;
|
|
532
|
+
d5 += h2 * r3;
|
|
533
|
+
d5 += h3 * r2;
|
|
534
|
+
d5 += h4 * r1;
|
|
535
|
+
c = d5 >>> 13;
|
|
536
|
+
d5 &= 8191;
|
|
537
|
+
d5 += h5 * r0;
|
|
538
|
+
d5 += h6 * (5 * r9);
|
|
539
|
+
d5 += h7 * (5 * r8);
|
|
540
|
+
d5 += h8 * (5 * r7);
|
|
541
|
+
d5 += h9 * (5 * r6);
|
|
542
|
+
c += d5 >>> 13;
|
|
543
|
+
d5 &= 8191;
|
|
544
|
+
d6 = c;
|
|
545
|
+
d6 += h0 * r6;
|
|
546
|
+
d6 += h1 * r5;
|
|
547
|
+
d6 += h2 * r4;
|
|
548
|
+
d6 += h3 * r3;
|
|
549
|
+
d6 += h4 * r2;
|
|
550
|
+
c = d6 >>> 13;
|
|
551
|
+
d6 &= 8191;
|
|
552
|
+
d6 += h5 * r1;
|
|
553
|
+
d6 += h6 * r0;
|
|
554
|
+
d6 += h7 * (5 * r9);
|
|
555
|
+
d6 += h8 * (5 * r8);
|
|
556
|
+
d6 += h9 * (5 * r7);
|
|
557
|
+
c += d6 >>> 13;
|
|
558
|
+
d6 &= 8191;
|
|
559
|
+
d7 = c;
|
|
560
|
+
d7 += h0 * r7;
|
|
561
|
+
d7 += h1 * r6;
|
|
562
|
+
d7 += h2 * r5;
|
|
563
|
+
d7 += h3 * r4;
|
|
564
|
+
d7 += h4 * r3;
|
|
565
|
+
c = d7 >>> 13;
|
|
566
|
+
d7 &= 8191;
|
|
567
|
+
d7 += h5 * r2;
|
|
568
|
+
d7 += h6 * r1;
|
|
569
|
+
d7 += h7 * r0;
|
|
570
|
+
d7 += h8 * (5 * r9);
|
|
571
|
+
d7 += h9 * (5 * r8);
|
|
572
|
+
c += d7 >>> 13;
|
|
573
|
+
d7 &= 8191;
|
|
574
|
+
d8 = c;
|
|
575
|
+
d8 += h0 * r8;
|
|
576
|
+
d8 += h1 * r7;
|
|
577
|
+
d8 += h2 * r6;
|
|
578
|
+
d8 += h3 * r5;
|
|
579
|
+
d8 += h4 * r4;
|
|
580
|
+
c = d8 >>> 13;
|
|
581
|
+
d8 &= 8191;
|
|
582
|
+
d8 += h5 * r3;
|
|
583
|
+
d8 += h6 * r2;
|
|
584
|
+
d8 += h7 * r1;
|
|
585
|
+
d8 += h8 * r0;
|
|
586
|
+
d8 += h9 * (5 * r9);
|
|
587
|
+
c += d8 >>> 13;
|
|
588
|
+
d8 &= 8191;
|
|
589
|
+
d9 = c;
|
|
590
|
+
d9 += h0 * r9;
|
|
591
|
+
d9 += h1 * r8;
|
|
592
|
+
d9 += h2 * r7;
|
|
593
|
+
d9 += h3 * r6;
|
|
594
|
+
d9 += h4 * r5;
|
|
595
|
+
c = d9 >>> 13;
|
|
596
|
+
d9 &= 8191;
|
|
597
|
+
d9 += h5 * r4;
|
|
598
|
+
d9 += h6 * r3;
|
|
599
|
+
d9 += h7 * r2;
|
|
600
|
+
d9 += h8 * r1;
|
|
601
|
+
d9 += h9 * r0;
|
|
602
|
+
c += d9 >>> 13;
|
|
603
|
+
d9 &= 8191;
|
|
604
|
+
c = (c << 2) + c | 0;
|
|
605
|
+
c = c + d0 | 0;
|
|
606
|
+
d0 = c & 8191;
|
|
607
|
+
c = c >>> 13;
|
|
608
|
+
d1 += c;
|
|
609
|
+
h0 = d0;
|
|
610
|
+
h1 = d1;
|
|
611
|
+
h2 = d2;
|
|
612
|
+
h3 = d3;
|
|
613
|
+
h4 = d4;
|
|
614
|
+
h5 = d5;
|
|
615
|
+
h6 = d6;
|
|
616
|
+
h7 = d7;
|
|
617
|
+
h8 = d8;
|
|
618
|
+
h9 = d9;
|
|
619
|
+
mpos += 16;
|
|
620
|
+
bytes -= 16;
|
|
621
|
+
}
|
|
622
|
+
this.h[0] = h0;
|
|
623
|
+
this.h[1] = h1;
|
|
624
|
+
this.h[2] = h2;
|
|
625
|
+
this.h[3] = h3;
|
|
626
|
+
this.h[4] = h4;
|
|
627
|
+
this.h[5] = h5;
|
|
628
|
+
this.h[6] = h6;
|
|
629
|
+
this.h[7] = h7;
|
|
630
|
+
this.h[8] = h8;
|
|
631
|
+
this.h[9] = h9;
|
|
632
|
+
};
|
|
633
|
+
poly1305.prototype.finish = function(mac, macpos) {
|
|
634
|
+
var g = new Uint16Array(10);
|
|
635
|
+
var c, mask, f, i;
|
|
636
|
+
if (this.leftover) {
|
|
637
|
+
i = this.leftover;
|
|
638
|
+
this.buffer[i++] = 1;
|
|
639
|
+
for (; i < 16; i++) this.buffer[i] = 0;
|
|
640
|
+
this.fin = 1;
|
|
641
|
+
this.blocks(this.buffer, 0, 16);
|
|
642
|
+
}
|
|
643
|
+
c = this.h[1] >>> 13;
|
|
644
|
+
this.h[1] &= 8191;
|
|
645
|
+
for (i = 2; i < 10; i++) {
|
|
646
|
+
this.h[i] += c;
|
|
647
|
+
c = this.h[i] >>> 13;
|
|
648
|
+
this.h[i] &= 8191;
|
|
649
|
+
}
|
|
650
|
+
this.h[0] += c * 5;
|
|
651
|
+
c = this.h[0] >>> 13;
|
|
652
|
+
this.h[0] &= 8191;
|
|
653
|
+
this.h[1] += c;
|
|
654
|
+
c = this.h[1] >>> 13;
|
|
655
|
+
this.h[1] &= 8191;
|
|
656
|
+
this.h[2] += c;
|
|
657
|
+
g[0] = this.h[0] + 5;
|
|
658
|
+
c = g[0] >>> 13;
|
|
659
|
+
g[0] &= 8191;
|
|
660
|
+
for (i = 1; i < 10; i++) {
|
|
661
|
+
g[i] = this.h[i] + c;
|
|
662
|
+
c = g[i] >>> 13;
|
|
663
|
+
g[i] &= 8191;
|
|
664
|
+
}
|
|
665
|
+
g[9] -= 1 << 13;
|
|
666
|
+
mask = (c ^ 1) - 1;
|
|
667
|
+
for (i = 0; i < 10; i++) g[i] &= mask;
|
|
668
|
+
mask = ~mask;
|
|
669
|
+
for (i = 0; i < 10; i++) this.h[i] = this.h[i] & mask | g[i];
|
|
670
|
+
this.h[0] = (this.h[0] | this.h[1] << 13) & 65535;
|
|
671
|
+
this.h[1] = (this.h[1] >>> 3 | this.h[2] << 10) & 65535;
|
|
672
|
+
this.h[2] = (this.h[2] >>> 6 | this.h[3] << 7) & 65535;
|
|
673
|
+
this.h[3] = (this.h[3] >>> 9 | this.h[4] << 4) & 65535;
|
|
674
|
+
this.h[4] = (this.h[4] >>> 12 | this.h[5] << 1 | this.h[6] << 14) & 65535;
|
|
675
|
+
this.h[5] = (this.h[6] >>> 2 | this.h[7] << 11) & 65535;
|
|
676
|
+
this.h[6] = (this.h[7] >>> 5 | this.h[8] << 8) & 65535;
|
|
677
|
+
this.h[7] = (this.h[8] >>> 8 | this.h[9] << 5) & 65535;
|
|
678
|
+
f = this.h[0] + this.pad[0];
|
|
679
|
+
this.h[0] = f & 65535;
|
|
680
|
+
for (i = 1; i < 8; i++) {
|
|
681
|
+
f = (this.h[i] + this.pad[i] | 0) + (f >>> 16) | 0;
|
|
682
|
+
this.h[i] = f & 65535;
|
|
683
|
+
}
|
|
684
|
+
mac[macpos + 0] = this.h[0] >>> 0 & 255;
|
|
685
|
+
mac[macpos + 1] = this.h[0] >>> 8 & 255;
|
|
686
|
+
mac[macpos + 2] = this.h[1] >>> 0 & 255;
|
|
687
|
+
mac[macpos + 3] = this.h[1] >>> 8 & 255;
|
|
688
|
+
mac[macpos + 4] = this.h[2] >>> 0 & 255;
|
|
689
|
+
mac[macpos + 5] = this.h[2] >>> 8 & 255;
|
|
690
|
+
mac[macpos + 6] = this.h[3] >>> 0 & 255;
|
|
691
|
+
mac[macpos + 7] = this.h[3] >>> 8 & 255;
|
|
692
|
+
mac[macpos + 8] = this.h[4] >>> 0 & 255;
|
|
693
|
+
mac[macpos + 9] = this.h[4] >>> 8 & 255;
|
|
694
|
+
mac[macpos + 10] = this.h[5] >>> 0 & 255;
|
|
695
|
+
mac[macpos + 11] = this.h[5] >>> 8 & 255;
|
|
696
|
+
mac[macpos + 12] = this.h[6] >>> 0 & 255;
|
|
697
|
+
mac[macpos + 13] = this.h[6] >>> 8 & 255;
|
|
698
|
+
mac[macpos + 14] = this.h[7] >>> 0 & 255;
|
|
699
|
+
mac[macpos + 15] = this.h[7] >>> 8 & 255;
|
|
700
|
+
};
|
|
701
|
+
poly1305.prototype.update = function(m, mpos, bytes) {
|
|
702
|
+
var i, want;
|
|
703
|
+
if (this.leftover) {
|
|
704
|
+
want = 16 - this.leftover;
|
|
705
|
+
if (want > bytes)
|
|
706
|
+
want = bytes;
|
|
707
|
+
for (i = 0; i < want; i++)
|
|
708
|
+
this.buffer[this.leftover + i] = m[mpos + i];
|
|
709
|
+
bytes -= want;
|
|
710
|
+
mpos += want;
|
|
711
|
+
this.leftover += want;
|
|
712
|
+
if (this.leftover < 16)
|
|
713
|
+
return;
|
|
714
|
+
this.blocks(this.buffer, 0, 16);
|
|
715
|
+
this.leftover = 0;
|
|
716
|
+
}
|
|
717
|
+
if (bytes >= 16) {
|
|
718
|
+
want = bytes - bytes % 16;
|
|
719
|
+
this.blocks(m, mpos, want);
|
|
720
|
+
mpos += want;
|
|
721
|
+
bytes -= want;
|
|
722
|
+
}
|
|
723
|
+
if (bytes) {
|
|
724
|
+
for (i = 0; i < bytes; i++)
|
|
725
|
+
this.buffer[this.leftover + i] = m[mpos + i];
|
|
726
|
+
this.leftover += bytes;
|
|
727
|
+
}
|
|
728
|
+
};
|
|
729
|
+
function crypto_onetimeauth(out, outpos, m, mpos, n, k) {
|
|
730
|
+
var s = new poly1305(k);
|
|
731
|
+
s.update(m, mpos, n);
|
|
732
|
+
s.finish(out, outpos);
|
|
733
|
+
return 0;
|
|
734
|
+
}
|
|
735
|
+
function crypto_onetimeauth_verify(h, hpos, m, mpos, n, k) {
|
|
736
|
+
var x = new Uint8Array(16);
|
|
737
|
+
crypto_onetimeauth(x, 0, m, mpos, n, k);
|
|
738
|
+
return crypto_verify_16(h, hpos, x, 0);
|
|
739
|
+
}
|
|
740
|
+
function crypto_secretbox(c, m, d, n, k) {
|
|
741
|
+
var i;
|
|
742
|
+
if (d < 32) return -1;
|
|
743
|
+
crypto_stream_xor(c, 0, m, 0, d, n, k);
|
|
744
|
+
crypto_onetimeauth(c, 16, c, 32, d - 32, c);
|
|
745
|
+
for (i = 0; i < 16; i++) c[i] = 0;
|
|
746
|
+
return 0;
|
|
747
|
+
}
|
|
748
|
+
function crypto_secretbox_open(m, c, d, n, k) {
|
|
749
|
+
var i;
|
|
750
|
+
var x = new Uint8Array(32);
|
|
751
|
+
if (d < 32) return -1;
|
|
752
|
+
crypto_stream(x, 0, 32, n, k);
|
|
753
|
+
if (crypto_onetimeauth_verify(c, 16, c, 32, d - 32, x) !== 0) return -1;
|
|
754
|
+
crypto_stream_xor(m, 0, c, 0, d, n, k);
|
|
755
|
+
for (i = 0; i < 32; i++) m[i] = 0;
|
|
756
|
+
return 0;
|
|
757
|
+
}
|
|
758
|
+
function set25519(r, a) {
|
|
759
|
+
var i;
|
|
760
|
+
for (i = 0; i < 16; i++) r[i] = a[i] | 0;
|
|
761
|
+
}
|
|
762
|
+
function car25519(o) {
|
|
763
|
+
var i, v, c = 1;
|
|
764
|
+
for (i = 0; i < 16; i++) {
|
|
765
|
+
v = o[i] + c + 65535;
|
|
766
|
+
c = Math.floor(v / 65536);
|
|
767
|
+
o[i] = v - c * 65536;
|
|
768
|
+
}
|
|
769
|
+
o[0] += c - 1 + 37 * (c - 1);
|
|
770
|
+
}
|
|
771
|
+
function sel25519(p, q, b) {
|
|
772
|
+
var t, c = ~(b - 1);
|
|
773
|
+
for (var i = 0; i < 16; i++) {
|
|
774
|
+
t = c & (p[i] ^ q[i]);
|
|
775
|
+
p[i] ^= t;
|
|
776
|
+
q[i] ^= t;
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
function pack25519(o, n) {
|
|
780
|
+
var i, j, b;
|
|
781
|
+
var m = gf(), t = gf();
|
|
782
|
+
for (i = 0; i < 16; i++) t[i] = n[i];
|
|
783
|
+
car25519(t);
|
|
784
|
+
car25519(t);
|
|
785
|
+
car25519(t);
|
|
786
|
+
for (j = 0; j < 2; j++) {
|
|
787
|
+
m[0] = t[0] - 65517;
|
|
788
|
+
for (i = 1; i < 15; i++) {
|
|
789
|
+
m[i] = t[i] - 65535 - (m[i - 1] >> 16 & 1);
|
|
790
|
+
m[i - 1] &= 65535;
|
|
791
|
+
}
|
|
792
|
+
m[15] = t[15] - 32767 - (m[14] >> 16 & 1);
|
|
793
|
+
b = m[15] >> 16 & 1;
|
|
794
|
+
m[14] &= 65535;
|
|
795
|
+
sel25519(t, m, 1 - b);
|
|
796
|
+
}
|
|
797
|
+
for (i = 0; i < 16; i++) {
|
|
798
|
+
o[2 * i] = t[i] & 255;
|
|
799
|
+
o[2 * i + 1] = t[i] >> 8;
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
function neq25519(a, b) {
|
|
803
|
+
var c = new Uint8Array(32), d = new Uint8Array(32);
|
|
804
|
+
pack25519(c, a);
|
|
805
|
+
pack25519(d, b);
|
|
806
|
+
return crypto_verify_32(c, 0, d, 0);
|
|
807
|
+
}
|
|
808
|
+
function par25519(a) {
|
|
809
|
+
var d = new Uint8Array(32);
|
|
810
|
+
pack25519(d, a);
|
|
811
|
+
return d[0] & 1;
|
|
812
|
+
}
|
|
813
|
+
function unpack25519(o, n) {
|
|
814
|
+
var i;
|
|
815
|
+
for (i = 0; i < 16; i++) o[i] = n[2 * i] + (n[2 * i + 1] << 8);
|
|
816
|
+
o[15] &= 32767;
|
|
817
|
+
}
|
|
818
|
+
function A(o, a, b) {
|
|
819
|
+
for (var i = 0; i < 16; i++) o[i] = a[i] + b[i];
|
|
820
|
+
}
|
|
821
|
+
function Z(o, a, b) {
|
|
822
|
+
for (var i = 0; i < 16; i++) o[i] = a[i] - b[i];
|
|
823
|
+
}
|
|
824
|
+
function M(o, a, b) {
|
|
825
|
+
var v, c, t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0, t8 = 0, t9 = 0, t10 = 0, t11 = 0, t12 = 0, t13 = 0, t14 = 0, t15 = 0, t16 = 0, t17 = 0, t18 = 0, t19 = 0, t20 = 0, t21 = 0, t22 = 0, t23 = 0, t24 = 0, t25 = 0, t26 = 0, t27 = 0, t28 = 0, t29 = 0, t30 = 0, b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3], b4 = b[4], b5 = b[5], b6 = b[6], b7 = b[7], b8 = b[8], b9 = b[9], b10 = b[10], b11 = b[11], b12 = b[12], b13 = b[13], b14 = b[14], b15 = b[15];
|
|
826
|
+
v = a[0];
|
|
827
|
+
t0 += v * b0;
|
|
828
|
+
t1 += v * b1;
|
|
829
|
+
t2 += v * b2;
|
|
830
|
+
t3 += v * b3;
|
|
831
|
+
t4 += v * b4;
|
|
832
|
+
t5 += v * b5;
|
|
833
|
+
t6 += v * b6;
|
|
834
|
+
t7 += v * b7;
|
|
835
|
+
t8 += v * b8;
|
|
836
|
+
t9 += v * b9;
|
|
837
|
+
t10 += v * b10;
|
|
838
|
+
t11 += v * b11;
|
|
839
|
+
t12 += v * b12;
|
|
840
|
+
t13 += v * b13;
|
|
841
|
+
t14 += v * b14;
|
|
842
|
+
t15 += v * b15;
|
|
843
|
+
v = a[1];
|
|
844
|
+
t1 += v * b0;
|
|
845
|
+
t2 += v * b1;
|
|
846
|
+
t3 += v * b2;
|
|
847
|
+
t4 += v * b3;
|
|
848
|
+
t5 += v * b4;
|
|
849
|
+
t6 += v * b5;
|
|
850
|
+
t7 += v * b6;
|
|
851
|
+
t8 += v * b7;
|
|
852
|
+
t9 += v * b8;
|
|
853
|
+
t10 += v * b9;
|
|
854
|
+
t11 += v * b10;
|
|
855
|
+
t12 += v * b11;
|
|
856
|
+
t13 += v * b12;
|
|
857
|
+
t14 += v * b13;
|
|
858
|
+
t15 += v * b14;
|
|
859
|
+
t16 += v * b15;
|
|
860
|
+
v = a[2];
|
|
861
|
+
t2 += v * b0;
|
|
862
|
+
t3 += v * b1;
|
|
863
|
+
t4 += v * b2;
|
|
864
|
+
t5 += v * b3;
|
|
865
|
+
t6 += v * b4;
|
|
866
|
+
t7 += v * b5;
|
|
867
|
+
t8 += v * b6;
|
|
868
|
+
t9 += v * b7;
|
|
869
|
+
t10 += v * b8;
|
|
870
|
+
t11 += v * b9;
|
|
871
|
+
t12 += v * b10;
|
|
872
|
+
t13 += v * b11;
|
|
873
|
+
t14 += v * b12;
|
|
874
|
+
t15 += v * b13;
|
|
875
|
+
t16 += v * b14;
|
|
876
|
+
t17 += v * b15;
|
|
877
|
+
v = a[3];
|
|
878
|
+
t3 += v * b0;
|
|
879
|
+
t4 += v * b1;
|
|
880
|
+
t5 += v * b2;
|
|
881
|
+
t6 += v * b3;
|
|
882
|
+
t7 += v * b4;
|
|
883
|
+
t8 += v * b5;
|
|
884
|
+
t9 += v * b6;
|
|
885
|
+
t10 += v * b7;
|
|
886
|
+
t11 += v * b8;
|
|
887
|
+
t12 += v * b9;
|
|
888
|
+
t13 += v * b10;
|
|
889
|
+
t14 += v * b11;
|
|
890
|
+
t15 += v * b12;
|
|
891
|
+
t16 += v * b13;
|
|
892
|
+
t17 += v * b14;
|
|
893
|
+
t18 += v * b15;
|
|
894
|
+
v = a[4];
|
|
895
|
+
t4 += v * b0;
|
|
896
|
+
t5 += v * b1;
|
|
897
|
+
t6 += v * b2;
|
|
898
|
+
t7 += v * b3;
|
|
899
|
+
t8 += v * b4;
|
|
900
|
+
t9 += v * b5;
|
|
901
|
+
t10 += v * b6;
|
|
902
|
+
t11 += v * b7;
|
|
903
|
+
t12 += v * b8;
|
|
904
|
+
t13 += v * b9;
|
|
905
|
+
t14 += v * b10;
|
|
906
|
+
t15 += v * b11;
|
|
907
|
+
t16 += v * b12;
|
|
908
|
+
t17 += v * b13;
|
|
909
|
+
t18 += v * b14;
|
|
910
|
+
t19 += v * b15;
|
|
911
|
+
v = a[5];
|
|
912
|
+
t5 += v * b0;
|
|
913
|
+
t6 += v * b1;
|
|
914
|
+
t7 += v * b2;
|
|
915
|
+
t8 += v * b3;
|
|
916
|
+
t9 += v * b4;
|
|
917
|
+
t10 += v * b5;
|
|
918
|
+
t11 += v * b6;
|
|
919
|
+
t12 += v * b7;
|
|
920
|
+
t13 += v * b8;
|
|
921
|
+
t14 += v * b9;
|
|
922
|
+
t15 += v * b10;
|
|
923
|
+
t16 += v * b11;
|
|
924
|
+
t17 += v * b12;
|
|
925
|
+
t18 += v * b13;
|
|
926
|
+
t19 += v * b14;
|
|
927
|
+
t20 += v * b15;
|
|
928
|
+
v = a[6];
|
|
929
|
+
t6 += v * b0;
|
|
930
|
+
t7 += v * b1;
|
|
931
|
+
t8 += v * b2;
|
|
932
|
+
t9 += v * b3;
|
|
933
|
+
t10 += v * b4;
|
|
934
|
+
t11 += v * b5;
|
|
935
|
+
t12 += v * b6;
|
|
936
|
+
t13 += v * b7;
|
|
937
|
+
t14 += v * b8;
|
|
938
|
+
t15 += v * b9;
|
|
939
|
+
t16 += v * b10;
|
|
940
|
+
t17 += v * b11;
|
|
941
|
+
t18 += v * b12;
|
|
942
|
+
t19 += v * b13;
|
|
943
|
+
t20 += v * b14;
|
|
944
|
+
t21 += v * b15;
|
|
945
|
+
v = a[7];
|
|
946
|
+
t7 += v * b0;
|
|
947
|
+
t8 += v * b1;
|
|
948
|
+
t9 += v * b2;
|
|
949
|
+
t10 += v * b3;
|
|
950
|
+
t11 += v * b4;
|
|
951
|
+
t12 += v * b5;
|
|
952
|
+
t13 += v * b6;
|
|
953
|
+
t14 += v * b7;
|
|
954
|
+
t15 += v * b8;
|
|
955
|
+
t16 += v * b9;
|
|
956
|
+
t17 += v * b10;
|
|
957
|
+
t18 += v * b11;
|
|
958
|
+
t19 += v * b12;
|
|
959
|
+
t20 += v * b13;
|
|
960
|
+
t21 += v * b14;
|
|
961
|
+
t22 += v * b15;
|
|
962
|
+
v = a[8];
|
|
963
|
+
t8 += v * b0;
|
|
964
|
+
t9 += v * b1;
|
|
965
|
+
t10 += v * b2;
|
|
966
|
+
t11 += v * b3;
|
|
967
|
+
t12 += v * b4;
|
|
968
|
+
t13 += v * b5;
|
|
969
|
+
t14 += v * b6;
|
|
970
|
+
t15 += v * b7;
|
|
971
|
+
t16 += v * b8;
|
|
972
|
+
t17 += v * b9;
|
|
973
|
+
t18 += v * b10;
|
|
974
|
+
t19 += v * b11;
|
|
975
|
+
t20 += v * b12;
|
|
976
|
+
t21 += v * b13;
|
|
977
|
+
t22 += v * b14;
|
|
978
|
+
t23 += v * b15;
|
|
979
|
+
v = a[9];
|
|
980
|
+
t9 += v * b0;
|
|
981
|
+
t10 += v * b1;
|
|
982
|
+
t11 += v * b2;
|
|
983
|
+
t12 += v * b3;
|
|
984
|
+
t13 += v * b4;
|
|
985
|
+
t14 += v * b5;
|
|
986
|
+
t15 += v * b6;
|
|
987
|
+
t16 += v * b7;
|
|
988
|
+
t17 += v * b8;
|
|
989
|
+
t18 += v * b9;
|
|
990
|
+
t19 += v * b10;
|
|
991
|
+
t20 += v * b11;
|
|
992
|
+
t21 += v * b12;
|
|
993
|
+
t22 += v * b13;
|
|
994
|
+
t23 += v * b14;
|
|
995
|
+
t24 += v * b15;
|
|
996
|
+
v = a[10];
|
|
997
|
+
t10 += v * b0;
|
|
998
|
+
t11 += v * b1;
|
|
999
|
+
t12 += v * b2;
|
|
1000
|
+
t13 += v * b3;
|
|
1001
|
+
t14 += v * b4;
|
|
1002
|
+
t15 += v * b5;
|
|
1003
|
+
t16 += v * b6;
|
|
1004
|
+
t17 += v * b7;
|
|
1005
|
+
t18 += v * b8;
|
|
1006
|
+
t19 += v * b9;
|
|
1007
|
+
t20 += v * b10;
|
|
1008
|
+
t21 += v * b11;
|
|
1009
|
+
t22 += v * b12;
|
|
1010
|
+
t23 += v * b13;
|
|
1011
|
+
t24 += v * b14;
|
|
1012
|
+
t25 += v * b15;
|
|
1013
|
+
v = a[11];
|
|
1014
|
+
t11 += v * b0;
|
|
1015
|
+
t12 += v * b1;
|
|
1016
|
+
t13 += v * b2;
|
|
1017
|
+
t14 += v * b3;
|
|
1018
|
+
t15 += v * b4;
|
|
1019
|
+
t16 += v * b5;
|
|
1020
|
+
t17 += v * b6;
|
|
1021
|
+
t18 += v * b7;
|
|
1022
|
+
t19 += v * b8;
|
|
1023
|
+
t20 += v * b9;
|
|
1024
|
+
t21 += v * b10;
|
|
1025
|
+
t22 += v * b11;
|
|
1026
|
+
t23 += v * b12;
|
|
1027
|
+
t24 += v * b13;
|
|
1028
|
+
t25 += v * b14;
|
|
1029
|
+
t26 += v * b15;
|
|
1030
|
+
v = a[12];
|
|
1031
|
+
t12 += v * b0;
|
|
1032
|
+
t13 += v * b1;
|
|
1033
|
+
t14 += v * b2;
|
|
1034
|
+
t15 += v * b3;
|
|
1035
|
+
t16 += v * b4;
|
|
1036
|
+
t17 += v * b5;
|
|
1037
|
+
t18 += v * b6;
|
|
1038
|
+
t19 += v * b7;
|
|
1039
|
+
t20 += v * b8;
|
|
1040
|
+
t21 += v * b9;
|
|
1041
|
+
t22 += v * b10;
|
|
1042
|
+
t23 += v * b11;
|
|
1043
|
+
t24 += v * b12;
|
|
1044
|
+
t25 += v * b13;
|
|
1045
|
+
t26 += v * b14;
|
|
1046
|
+
t27 += v * b15;
|
|
1047
|
+
v = a[13];
|
|
1048
|
+
t13 += v * b0;
|
|
1049
|
+
t14 += v * b1;
|
|
1050
|
+
t15 += v * b2;
|
|
1051
|
+
t16 += v * b3;
|
|
1052
|
+
t17 += v * b4;
|
|
1053
|
+
t18 += v * b5;
|
|
1054
|
+
t19 += v * b6;
|
|
1055
|
+
t20 += v * b7;
|
|
1056
|
+
t21 += v * b8;
|
|
1057
|
+
t22 += v * b9;
|
|
1058
|
+
t23 += v * b10;
|
|
1059
|
+
t24 += v * b11;
|
|
1060
|
+
t25 += v * b12;
|
|
1061
|
+
t26 += v * b13;
|
|
1062
|
+
t27 += v * b14;
|
|
1063
|
+
t28 += v * b15;
|
|
1064
|
+
v = a[14];
|
|
1065
|
+
t14 += v * b0;
|
|
1066
|
+
t15 += v * b1;
|
|
1067
|
+
t16 += v * b2;
|
|
1068
|
+
t17 += v * b3;
|
|
1069
|
+
t18 += v * b4;
|
|
1070
|
+
t19 += v * b5;
|
|
1071
|
+
t20 += v * b6;
|
|
1072
|
+
t21 += v * b7;
|
|
1073
|
+
t22 += v * b8;
|
|
1074
|
+
t23 += v * b9;
|
|
1075
|
+
t24 += v * b10;
|
|
1076
|
+
t25 += v * b11;
|
|
1077
|
+
t26 += v * b12;
|
|
1078
|
+
t27 += v * b13;
|
|
1079
|
+
t28 += v * b14;
|
|
1080
|
+
t29 += v * b15;
|
|
1081
|
+
v = a[15];
|
|
1082
|
+
t15 += v * b0;
|
|
1083
|
+
t16 += v * b1;
|
|
1084
|
+
t17 += v * b2;
|
|
1085
|
+
t18 += v * b3;
|
|
1086
|
+
t19 += v * b4;
|
|
1087
|
+
t20 += v * b5;
|
|
1088
|
+
t21 += v * b6;
|
|
1089
|
+
t22 += v * b7;
|
|
1090
|
+
t23 += v * b8;
|
|
1091
|
+
t24 += v * b9;
|
|
1092
|
+
t25 += v * b10;
|
|
1093
|
+
t26 += v * b11;
|
|
1094
|
+
t27 += v * b12;
|
|
1095
|
+
t28 += v * b13;
|
|
1096
|
+
t29 += v * b14;
|
|
1097
|
+
t30 += v * b15;
|
|
1098
|
+
t0 += 38 * t16;
|
|
1099
|
+
t1 += 38 * t17;
|
|
1100
|
+
t2 += 38 * t18;
|
|
1101
|
+
t3 += 38 * t19;
|
|
1102
|
+
t4 += 38 * t20;
|
|
1103
|
+
t5 += 38 * t21;
|
|
1104
|
+
t6 += 38 * t22;
|
|
1105
|
+
t7 += 38 * t23;
|
|
1106
|
+
t8 += 38 * t24;
|
|
1107
|
+
t9 += 38 * t25;
|
|
1108
|
+
t10 += 38 * t26;
|
|
1109
|
+
t11 += 38 * t27;
|
|
1110
|
+
t12 += 38 * t28;
|
|
1111
|
+
t13 += 38 * t29;
|
|
1112
|
+
t14 += 38 * t30;
|
|
1113
|
+
c = 1;
|
|
1114
|
+
v = t0 + c + 65535;
|
|
1115
|
+
c = Math.floor(v / 65536);
|
|
1116
|
+
t0 = v - c * 65536;
|
|
1117
|
+
v = t1 + c + 65535;
|
|
1118
|
+
c = Math.floor(v / 65536);
|
|
1119
|
+
t1 = v - c * 65536;
|
|
1120
|
+
v = t2 + c + 65535;
|
|
1121
|
+
c = Math.floor(v / 65536);
|
|
1122
|
+
t2 = v - c * 65536;
|
|
1123
|
+
v = t3 + c + 65535;
|
|
1124
|
+
c = Math.floor(v / 65536);
|
|
1125
|
+
t3 = v - c * 65536;
|
|
1126
|
+
v = t4 + c + 65535;
|
|
1127
|
+
c = Math.floor(v / 65536);
|
|
1128
|
+
t4 = v - c * 65536;
|
|
1129
|
+
v = t5 + c + 65535;
|
|
1130
|
+
c = Math.floor(v / 65536);
|
|
1131
|
+
t5 = v - c * 65536;
|
|
1132
|
+
v = t6 + c + 65535;
|
|
1133
|
+
c = Math.floor(v / 65536);
|
|
1134
|
+
t6 = v - c * 65536;
|
|
1135
|
+
v = t7 + c + 65535;
|
|
1136
|
+
c = Math.floor(v / 65536);
|
|
1137
|
+
t7 = v - c * 65536;
|
|
1138
|
+
v = t8 + c + 65535;
|
|
1139
|
+
c = Math.floor(v / 65536);
|
|
1140
|
+
t8 = v - c * 65536;
|
|
1141
|
+
v = t9 + c + 65535;
|
|
1142
|
+
c = Math.floor(v / 65536);
|
|
1143
|
+
t9 = v - c * 65536;
|
|
1144
|
+
v = t10 + c + 65535;
|
|
1145
|
+
c = Math.floor(v / 65536);
|
|
1146
|
+
t10 = v - c * 65536;
|
|
1147
|
+
v = t11 + c + 65535;
|
|
1148
|
+
c = Math.floor(v / 65536);
|
|
1149
|
+
t11 = v - c * 65536;
|
|
1150
|
+
v = t12 + c + 65535;
|
|
1151
|
+
c = Math.floor(v / 65536);
|
|
1152
|
+
t12 = v - c * 65536;
|
|
1153
|
+
v = t13 + c + 65535;
|
|
1154
|
+
c = Math.floor(v / 65536);
|
|
1155
|
+
t13 = v - c * 65536;
|
|
1156
|
+
v = t14 + c + 65535;
|
|
1157
|
+
c = Math.floor(v / 65536);
|
|
1158
|
+
t14 = v - c * 65536;
|
|
1159
|
+
v = t15 + c + 65535;
|
|
1160
|
+
c = Math.floor(v / 65536);
|
|
1161
|
+
t15 = v - c * 65536;
|
|
1162
|
+
t0 += c - 1 + 37 * (c - 1);
|
|
1163
|
+
c = 1;
|
|
1164
|
+
v = t0 + c + 65535;
|
|
1165
|
+
c = Math.floor(v / 65536);
|
|
1166
|
+
t0 = v - c * 65536;
|
|
1167
|
+
v = t1 + c + 65535;
|
|
1168
|
+
c = Math.floor(v / 65536);
|
|
1169
|
+
t1 = v - c * 65536;
|
|
1170
|
+
v = t2 + c + 65535;
|
|
1171
|
+
c = Math.floor(v / 65536);
|
|
1172
|
+
t2 = v - c * 65536;
|
|
1173
|
+
v = t3 + c + 65535;
|
|
1174
|
+
c = Math.floor(v / 65536);
|
|
1175
|
+
t3 = v - c * 65536;
|
|
1176
|
+
v = t4 + c + 65535;
|
|
1177
|
+
c = Math.floor(v / 65536);
|
|
1178
|
+
t4 = v - c * 65536;
|
|
1179
|
+
v = t5 + c + 65535;
|
|
1180
|
+
c = Math.floor(v / 65536);
|
|
1181
|
+
t5 = v - c * 65536;
|
|
1182
|
+
v = t6 + c + 65535;
|
|
1183
|
+
c = Math.floor(v / 65536);
|
|
1184
|
+
t6 = v - c * 65536;
|
|
1185
|
+
v = t7 + c + 65535;
|
|
1186
|
+
c = Math.floor(v / 65536);
|
|
1187
|
+
t7 = v - c * 65536;
|
|
1188
|
+
v = t8 + c + 65535;
|
|
1189
|
+
c = Math.floor(v / 65536);
|
|
1190
|
+
t8 = v - c * 65536;
|
|
1191
|
+
v = t9 + c + 65535;
|
|
1192
|
+
c = Math.floor(v / 65536);
|
|
1193
|
+
t9 = v - c * 65536;
|
|
1194
|
+
v = t10 + c + 65535;
|
|
1195
|
+
c = Math.floor(v / 65536);
|
|
1196
|
+
t10 = v - c * 65536;
|
|
1197
|
+
v = t11 + c + 65535;
|
|
1198
|
+
c = Math.floor(v / 65536);
|
|
1199
|
+
t11 = v - c * 65536;
|
|
1200
|
+
v = t12 + c + 65535;
|
|
1201
|
+
c = Math.floor(v / 65536);
|
|
1202
|
+
t12 = v - c * 65536;
|
|
1203
|
+
v = t13 + c + 65535;
|
|
1204
|
+
c = Math.floor(v / 65536);
|
|
1205
|
+
t13 = v - c * 65536;
|
|
1206
|
+
v = t14 + c + 65535;
|
|
1207
|
+
c = Math.floor(v / 65536);
|
|
1208
|
+
t14 = v - c * 65536;
|
|
1209
|
+
v = t15 + c + 65535;
|
|
1210
|
+
c = Math.floor(v / 65536);
|
|
1211
|
+
t15 = v - c * 65536;
|
|
1212
|
+
t0 += c - 1 + 37 * (c - 1);
|
|
1213
|
+
o[0] = t0;
|
|
1214
|
+
o[1] = t1;
|
|
1215
|
+
o[2] = t2;
|
|
1216
|
+
o[3] = t3;
|
|
1217
|
+
o[4] = t4;
|
|
1218
|
+
o[5] = t5;
|
|
1219
|
+
o[6] = t6;
|
|
1220
|
+
o[7] = t7;
|
|
1221
|
+
o[8] = t8;
|
|
1222
|
+
o[9] = t9;
|
|
1223
|
+
o[10] = t10;
|
|
1224
|
+
o[11] = t11;
|
|
1225
|
+
o[12] = t12;
|
|
1226
|
+
o[13] = t13;
|
|
1227
|
+
o[14] = t14;
|
|
1228
|
+
o[15] = t15;
|
|
1229
|
+
}
|
|
1230
|
+
function S(o, a) {
|
|
1231
|
+
M(o, a, a);
|
|
1232
|
+
}
|
|
1233
|
+
function inv25519(o, i) {
|
|
1234
|
+
var c = gf();
|
|
1235
|
+
var a;
|
|
1236
|
+
for (a = 0; a < 16; a++) c[a] = i[a];
|
|
1237
|
+
for (a = 253; a >= 0; a--) {
|
|
1238
|
+
S(c, c);
|
|
1239
|
+
if (a !== 2 && a !== 4) M(c, c, i);
|
|
1240
|
+
}
|
|
1241
|
+
for (a = 0; a < 16; a++) o[a] = c[a];
|
|
1242
|
+
}
|
|
1243
|
+
function pow2523(o, i) {
|
|
1244
|
+
var c = gf();
|
|
1245
|
+
var a;
|
|
1246
|
+
for (a = 0; a < 16; a++) c[a] = i[a];
|
|
1247
|
+
for (a = 250; a >= 0; a--) {
|
|
1248
|
+
S(c, c);
|
|
1249
|
+
if (a !== 1) M(c, c, i);
|
|
1250
|
+
}
|
|
1251
|
+
for (a = 0; a < 16; a++) o[a] = c[a];
|
|
1252
|
+
}
|
|
1253
|
+
function crypto_scalarmult(q, n, p) {
|
|
1254
|
+
var z2 = new Uint8Array(32);
|
|
1255
|
+
var x = new Float64Array(80), r, i;
|
|
1256
|
+
var a = gf(), b = gf(), c = gf(), d = gf(), e = gf(), f = gf();
|
|
1257
|
+
for (i = 0; i < 31; i++) z2[i] = n[i];
|
|
1258
|
+
z2[31] = n[31] & 127 | 64;
|
|
1259
|
+
z2[0] &= 248;
|
|
1260
|
+
unpack25519(x, p);
|
|
1261
|
+
for (i = 0; i < 16; i++) {
|
|
1262
|
+
b[i] = x[i];
|
|
1263
|
+
d[i] = a[i] = c[i] = 0;
|
|
1264
|
+
}
|
|
1265
|
+
a[0] = d[0] = 1;
|
|
1266
|
+
for (i = 254; i >= 0; --i) {
|
|
1267
|
+
r = z2[i >>> 3] >>> (i & 7) & 1;
|
|
1268
|
+
sel25519(a, b, r);
|
|
1269
|
+
sel25519(c, d, r);
|
|
1270
|
+
A(e, a, c);
|
|
1271
|
+
Z(a, a, c);
|
|
1272
|
+
A(c, b, d);
|
|
1273
|
+
Z(b, b, d);
|
|
1274
|
+
S(d, e);
|
|
1275
|
+
S(f, a);
|
|
1276
|
+
M(a, c, a);
|
|
1277
|
+
M(c, b, e);
|
|
1278
|
+
A(e, a, c);
|
|
1279
|
+
Z(a, a, c);
|
|
1280
|
+
S(b, a);
|
|
1281
|
+
Z(c, d, f);
|
|
1282
|
+
M(a, c, _121665);
|
|
1283
|
+
A(a, a, d);
|
|
1284
|
+
M(c, c, a);
|
|
1285
|
+
M(a, d, f);
|
|
1286
|
+
M(d, b, x);
|
|
1287
|
+
S(b, e);
|
|
1288
|
+
sel25519(a, b, r);
|
|
1289
|
+
sel25519(c, d, r);
|
|
1290
|
+
}
|
|
1291
|
+
for (i = 0; i < 16; i++) {
|
|
1292
|
+
x[i + 16] = a[i];
|
|
1293
|
+
x[i + 32] = c[i];
|
|
1294
|
+
x[i + 48] = b[i];
|
|
1295
|
+
x[i + 64] = d[i];
|
|
1296
|
+
}
|
|
1297
|
+
var x32 = x.subarray(32);
|
|
1298
|
+
var x16 = x.subarray(16);
|
|
1299
|
+
inv25519(x32, x32);
|
|
1300
|
+
M(x16, x16, x32);
|
|
1301
|
+
pack25519(q, x16);
|
|
1302
|
+
return 0;
|
|
1303
|
+
}
|
|
1304
|
+
function crypto_scalarmult_base(q, n) {
|
|
1305
|
+
return crypto_scalarmult(q, n, _9);
|
|
1306
|
+
}
|
|
1307
|
+
function crypto_box_keypair(y, x) {
|
|
1308
|
+
randombytes(x, 32);
|
|
1309
|
+
return crypto_scalarmult_base(y, x);
|
|
1310
|
+
}
|
|
1311
|
+
function crypto_box_beforenm(k, y, x) {
|
|
1312
|
+
var s = new Uint8Array(32);
|
|
1313
|
+
crypto_scalarmult(s, x, y);
|
|
1314
|
+
return crypto_core_hsalsa20(k, _0, s, sigma);
|
|
1315
|
+
}
|
|
1316
|
+
var crypto_box_afternm = crypto_secretbox;
|
|
1317
|
+
var crypto_box_open_afternm = crypto_secretbox_open;
|
|
1318
|
+
function crypto_box(c, m, d, n, y, x) {
|
|
1319
|
+
var k = new Uint8Array(32);
|
|
1320
|
+
crypto_box_beforenm(k, y, x);
|
|
1321
|
+
return crypto_box_afternm(c, m, d, n, k);
|
|
1322
|
+
}
|
|
1323
|
+
function crypto_box_open(m, c, d, n, y, x) {
|
|
1324
|
+
var k = new Uint8Array(32);
|
|
1325
|
+
crypto_box_beforenm(k, y, x);
|
|
1326
|
+
return crypto_box_open_afternm(m, c, d, n, k);
|
|
1327
|
+
}
|
|
1328
|
+
var K = [
|
|
1329
|
+
1116352408,
|
|
1330
|
+
3609767458,
|
|
1331
|
+
1899447441,
|
|
1332
|
+
602891725,
|
|
1333
|
+
3049323471,
|
|
1334
|
+
3964484399,
|
|
1335
|
+
3921009573,
|
|
1336
|
+
2173295548,
|
|
1337
|
+
961987163,
|
|
1338
|
+
4081628472,
|
|
1339
|
+
1508970993,
|
|
1340
|
+
3053834265,
|
|
1341
|
+
2453635748,
|
|
1342
|
+
2937671579,
|
|
1343
|
+
2870763221,
|
|
1344
|
+
3664609560,
|
|
1345
|
+
3624381080,
|
|
1346
|
+
2734883394,
|
|
1347
|
+
310598401,
|
|
1348
|
+
1164996542,
|
|
1349
|
+
607225278,
|
|
1350
|
+
1323610764,
|
|
1351
|
+
1426881987,
|
|
1352
|
+
3590304994,
|
|
1353
|
+
1925078388,
|
|
1354
|
+
4068182383,
|
|
1355
|
+
2162078206,
|
|
1356
|
+
991336113,
|
|
1357
|
+
2614888103,
|
|
1358
|
+
633803317,
|
|
1359
|
+
3248222580,
|
|
1360
|
+
3479774868,
|
|
1361
|
+
3835390401,
|
|
1362
|
+
2666613458,
|
|
1363
|
+
4022224774,
|
|
1364
|
+
944711139,
|
|
1365
|
+
264347078,
|
|
1366
|
+
2341262773,
|
|
1367
|
+
604807628,
|
|
1368
|
+
2007800933,
|
|
1369
|
+
770255983,
|
|
1370
|
+
1495990901,
|
|
1371
|
+
1249150122,
|
|
1372
|
+
1856431235,
|
|
1373
|
+
1555081692,
|
|
1374
|
+
3175218132,
|
|
1375
|
+
1996064986,
|
|
1376
|
+
2198950837,
|
|
1377
|
+
2554220882,
|
|
1378
|
+
3999719339,
|
|
1379
|
+
2821834349,
|
|
1380
|
+
766784016,
|
|
1381
|
+
2952996808,
|
|
1382
|
+
2566594879,
|
|
1383
|
+
3210313671,
|
|
1384
|
+
3203337956,
|
|
1385
|
+
3336571891,
|
|
1386
|
+
1034457026,
|
|
1387
|
+
3584528711,
|
|
1388
|
+
2466948901,
|
|
1389
|
+
113926993,
|
|
1390
|
+
3758326383,
|
|
1391
|
+
338241895,
|
|
1392
|
+
168717936,
|
|
1393
|
+
666307205,
|
|
1394
|
+
1188179964,
|
|
1395
|
+
773529912,
|
|
1396
|
+
1546045734,
|
|
1397
|
+
1294757372,
|
|
1398
|
+
1522805485,
|
|
1399
|
+
1396182291,
|
|
1400
|
+
2643833823,
|
|
1401
|
+
1695183700,
|
|
1402
|
+
2343527390,
|
|
1403
|
+
1986661051,
|
|
1404
|
+
1014477480,
|
|
1405
|
+
2177026350,
|
|
1406
|
+
1206759142,
|
|
1407
|
+
2456956037,
|
|
1408
|
+
344077627,
|
|
1409
|
+
2730485921,
|
|
1410
|
+
1290863460,
|
|
1411
|
+
2820302411,
|
|
1412
|
+
3158454273,
|
|
1413
|
+
3259730800,
|
|
1414
|
+
3505952657,
|
|
1415
|
+
3345764771,
|
|
1416
|
+
106217008,
|
|
1417
|
+
3516065817,
|
|
1418
|
+
3606008344,
|
|
1419
|
+
3600352804,
|
|
1420
|
+
1432725776,
|
|
1421
|
+
4094571909,
|
|
1422
|
+
1467031594,
|
|
1423
|
+
275423344,
|
|
1424
|
+
851169720,
|
|
1425
|
+
430227734,
|
|
1426
|
+
3100823752,
|
|
1427
|
+
506948616,
|
|
1428
|
+
1363258195,
|
|
1429
|
+
659060556,
|
|
1430
|
+
3750685593,
|
|
1431
|
+
883997877,
|
|
1432
|
+
3785050280,
|
|
1433
|
+
958139571,
|
|
1434
|
+
3318307427,
|
|
1435
|
+
1322822218,
|
|
1436
|
+
3812723403,
|
|
1437
|
+
1537002063,
|
|
1438
|
+
2003034995,
|
|
1439
|
+
1747873779,
|
|
1440
|
+
3602036899,
|
|
1441
|
+
1955562222,
|
|
1442
|
+
1575990012,
|
|
1443
|
+
2024104815,
|
|
1444
|
+
1125592928,
|
|
1445
|
+
2227730452,
|
|
1446
|
+
2716904306,
|
|
1447
|
+
2361852424,
|
|
1448
|
+
442776044,
|
|
1449
|
+
2428436474,
|
|
1450
|
+
593698344,
|
|
1451
|
+
2756734187,
|
|
1452
|
+
3733110249,
|
|
1453
|
+
3204031479,
|
|
1454
|
+
2999351573,
|
|
1455
|
+
3329325298,
|
|
1456
|
+
3815920427,
|
|
1457
|
+
3391569614,
|
|
1458
|
+
3928383900,
|
|
1459
|
+
3515267271,
|
|
1460
|
+
566280711,
|
|
1461
|
+
3940187606,
|
|
1462
|
+
3454069534,
|
|
1463
|
+
4118630271,
|
|
1464
|
+
4000239992,
|
|
1465
|
+
116418474,
|
|
1466
|
+
1914138554,
|
|
1467
|
+
174292421,
|
|
1468
|
+
2731055270,
|
|
1469
|
+
289380356,
|
|
1470
|
+
3203993006,
|
|
1471
|
+
460393269,
|
|
1472
|
+
320620315,
|
|
1473
|
+
685471733,
|
|
1474
|
+
587496836,
|
|
1475
|
+
852142971,
|
|
1476
|
+
1086792851,
|
|
1477
|
+
1017036298,
|
|
1478
|
+
365543100,
|
|
1479
|
+
1126000580,
|
|
1480
|
+
2618297676,
|
|
1481
|
+
1288033470,
|
|
1482
|
+
3409855158,
|
|
1483
|
+
1501505948,
|
|
1484
|
+
4234509866,
|
|
1485
|
+
1607167915,
|
|
1486
|
+
987167468,
|
|
1487
|
+
1816402316,
|
|
1488
|
+
1246189591
|
|
1489
|
+
];
|
|
1490
|
+
function crypto_hashblocks_hl(hh, hl, m, n) {
|
|
1491
|
+
var wh = new Int32Array(16), wl = new Int32Array(16), bh0, bh1, bh2, bh3, bh4, bh5, bh6, bh7, bl0, bl1, bl2, bl3, bl4, bl5, bl6, bl7, th, tl, i, j, h, l, a, b, c, d;
|
|
1492
|
+
var ah0 = hh[0], ah1 = hh[1], ah2 = hh[2], ah3 = hh[3], ah4 = hh[4], ah5 = hh[5], ah6 = hh[6], ah7 = hh[7], al0 = hl[0], al1 = hl[1], al2 = hl[2], al3 = hl[3], al4 = hl[4], al5 = hl[5], al6 = hl[6], al7 = hl[7];
|
|
1493
|
+
var pos = 0;
|
|
1494
|
+
while (n >= 128) {
|
|
1495
|
+
for (i = 0; i < 16; i++) {
|
|
1496
|
+
j = 8 * i + pos;
|
|
1497
|
+
wh[i] = m[j + 0] << 24 | m[j + 1] << 16 | m[j + 2] << 8 | m[j + 3];
|
|
1498
|
+
wl[i] = m[j + 4] << 24 | m[j + 5] << 16 | m[j + 6] << 8 | m[j + 7];
|
|
1499
|
+
}
|
|
1500
|
+
for (i = 0; i < 80; i++) {
|
|
1501
|
+
bh0 = ah0;
|
|
1502
|
+
bh1 = ah1;
|
|
1503
|
+
bh2 = ah2;
|
|
1504
|
+
bh3 = ah3;
|
|
1505
|
+
bh4 = ah4;
|
|
1506
|
+
bh5 = ah5;
|
|
1507
|
+
bh6 = ah6;
|
|
1508
|
+
bh7 = ah7;
|
|
1509
|
+
bl0 = al0;
|
|
1510
|
+
bl1 = al1;
|
|
1511
|
+
bl2 = al2;
|
|
1512
|
+
bl3 = al3;
|
|
1513
|
+
bl4 = al4;
|
|
1514
|
+
bl5 = al5;
|
|
1515
|
+
bl6 = al6;
|
|
1516
|
+
bl7 = al7;
|
|
1517
|
+
h = ah7;
|
|
1518
|
+
l = al7;
|
|
1519
|
+
a = l & 65535;
|
|
1520
|
+
b = l >>> 16;
|
|
1521
|
+
c = h & 65535;
|
|
1522
|
+
d = h >>> 16;
|
|
1523
|
+
h = (ah4 >>> 14 | al4 << 32 - 14) ^ (ah4 >>> 18 | al4 << 32 - 18) ^ (al4 >>> 41 - 32 | ah4 << 32 - (41 - 32));
|
|
1524
|
+
l = (al4 >>> 14 | ah4 << 32 - 14) ^ (al4 >>> 18 | ah4 << 32 - 18) ^ (ah4 >>> 41 - 32 | al4 << 32 - (41 - 32));
|
|
1525
|
+
a += l & 65535;
|
|
1526
|
+
b += l >>> 16;
|
|
1527
|
+
c += h & 65535;
|
|
1528
|
+
d += h >>> 16;
|
|
1529
|
+
h = ah4 & ah5 ^ ~ah4 & ah6;
|
|
1530
|
+
l = al4 & al5 ^ ~al4 & al6;
|
|
1531
|
+
a += l & 65535;
|
|
1532
|
+
b += l >>> 16;
|
|
1533
|
+
c += h & 65535;
|
|
1534
|
+
d += h >>> 16;
|
|
1535
|
+
h = K[i * 2];
|
|
1536
|
+
l = K[i * 2 + 1];
|
|
1537
|
+
a += l & 65535;
|
|
1538
|
+
b += l >>> 16;
|
|
1539
|
+
c += h & 65535;
|
|
1540
|
+
d += h >>> 16;
|
|
1541
|
+
h = wh[i % 16];
|
|
1542
|
+
l = wl[i % 16];
|
|
1543
|
+
a += l & 65535;
|
|
1544
|
+
b += l >>> 16;
|
|
1545
|
+
c += h & 65535;
|
|
1546
|
+
d += h >>> 16;
|
|
1547
|
+
b += a >>> 16;
|
|
1548
|
+
c += b >>> 16;
|
|
1549
|
+
d += c >>> 16;
|
|
1550
|
+
th = c & 65535 | d << 16;
|
|
1551
|
+
tl = a & 65535 | b << 16;
|
|
1552
|
+
h = th;
|
|
1553
|
+
l = tl;
|
|
1554
|
+
a = l & 65535;
|
|
1555
|
+
b = l >>> 16;
|
|
1556
|
+
c = h & 65535;
|
|
1557
|
+
d = h >>> 16;
|
|
1558
|
+
h = (ah0 >>> 28 | al0 << 32 - 28) ^ (al0 >>> 34 - 32 | ah0 << 32 - (34 - 32)) ^ (al0 >>> 39 - 32 | ah0 << 32 - (39 - 32));
|
|
1559
|
+
l = (al0 >>> 28 | ah0 << 32 - 28) ^ (ah0 >>> 34 - 32 | al0 << 32 - (34 - 32)) ^ (ah0 >>> 39 - 32 | al0 << 32 - (39 - 32));
|
|
1560
|
+
a += l & 65535;
|
|
1561
|
+
b += l >>> 16;
|
|
1562
|
+
c += h & 65535;
|
|
1563
|
+
d += h >>> 16;
|
|
1564
|
+
h = ah0 & ah1 ^ ah0 & ah2 ^ ah1 & ah2;
|
|
1565
|
+
l = al0 & al1 ^ al0 & al2 ^ al1 & al2;
|
|
1566
|
+
a += l & 65535;
|
|
1567
|
+
b += l >>> 16;
|
|
1568
|
+
c += h & 65535;
|
|
1569
|
+
d += h >>> 16;
|
|
1570
|
+
b += a >>> 16;
|
|
1571
|
+
c += b >>> 16;
|
|
1572
|
+
d += c >>> 16;
|
|
1573
|
+
bh7 = c & 65535 | d << 16;
|
|
1574
|
+
bl7 = a & 65535 | b << 16;
|
|
1575
|
+
h = bh3;
|
|
1576
|
+
l = bl3;
|
|
1577
|
+
a = l & 65535;
|
|
1578
|
+
b = l >>> 16;
|
|
1579
|
+
c = h & 65535;
|
|
1580
|
+
d = h >>> 16;
|
|
1581
|
+
h = th;
|
|
1582
|
+
l = tl;
|
|
1583
|
+
a += l & 65535;
|
|
1584
|
+
b += l >>> 16;
|
|
1585
|
+
c += h & 65535;
|
|
1586
|
+
d += h >>> 16;
|
|
1587
|
+
b += a >>> 16;
|
|
1588
|
+
c += b >>> 16;
|
|
1589
|
+
d += c >>> 16;
|
|
1590
|
+
bh3 = c & 65535 | d << 16;
|
|
1591
|
+
bl3 = a & 65535 | b << 16;
|
|
1592
|
+
ah1 = bh0;
|
|
1593
|
+
ah2 = bh1;
|
|
1594
|
+
ah3 = bh2;
|
|
1595
|
+
ah4 = bh3;
|
|
1596
|
+
ah5 = bh4;
|
|
1597
|
+
ah6 = bh5;
|
|
1598
|
+
ah7 = bh6;
|
|
1599
|
+
ah0 = bh7;
|
|
1600
|
+
al1 = bl0;
|
|
1601
|
+
al2 = bl1;
|
|
1602
|
+
al3 = bl2;
|
|
1603
|
+
al4 = bl3;
|
|
1604
|
+
al5 = bl4;
|
|
1605
|
+
al6 = bl5;
|
|
1606
|
+
al7 = bl6;
|
|
1607
|
+
al0 = bl7;
|
|
1608
|
+
if (i % 16 === 15) {
|
|
1609
|
+
for (j = 0; j < 16; j++) {
|
|
1610
|
+
h = wh[j];
|
|
1611
|
+
l = wl[j];
|
|
1612
|
+
a = l & 65535;
|
|
1613
|
+
b = l >>> 16;
|
|
1614
|
+
c = h & 65535;
|
|
1615
|
+
d = h >>> 16;
|
|
1616
|
+
h = wh[(j + 9) % 16];
|
|
1617
|
+
l = wl[(j + 9) % 16];
|
|
1618
|
+
a += l & 65535;
|
|
1619
|
+
b += l >>> 16;
|
|
1620
|
+
c += h & 65535;
|
|
1621
|
+
d += h >>> 16;
|
|
1622
|
+
th = wh[(j + 1) % 16];
|
|
1623
|
+
tl = wl[(j + 1) % 16];
|
|
1624
|
+
h = (th >>> 1 | tl << 32 - 1) ^ (th >>> 8 | tl << 32 - 8) ^ th >>> 7;
|
|
1625
|
+
l = (tl >>> 1 | th << 32 - 1) ^ (tl >>> 8 | th << 32 - 8) ^ (tl >>> 7 | th << 32 - 7);
|
|
1626
|
+
a += l & 65535;
|
|
1627
|
+
b += l >>> 16;
|
|
1628
|
+
c += h & 65535;
|
|
1629
|
+
d += h >>> 16;
|
|
1630
|
+
th = wh[(j + 14) % 16];
|
|
1631
|
+
tl = wl[(j + 14) % 16];
|
|
1632
|
+
h = (th >>> 19 | tl << 32 - 19) ^ (tl >>> 61 - 32 | th << 32 - (61 - 32)) ^ th >>> 6;
|
|
1633
|
+
l = (tl >>> 19 | th << 32 - 19) ^ (th >>> 61 - 32 | tl << 32 - (61 - 32)) ^ (tl >>> 6 | th << 32 - 6);
|
|
1634
|
+
a += l & 65535;
|
|
1635
|
+
b += l >>> 16;
|
|
1636
|
+
c += h & 65535;
|
|
1637
|
+
d += h >>> 16;
|
|
1638
|
+
b += a >>> 16;
|
|
1639
|
+
c += b >>> 16;
|
|
1640
|
+
d += c >>> 16;
|
|
1641
|
+
wh[j] = c & 65535 | d << 16;
|
|
1642
|
+
wl[j] = a & 65535 | b << 16;
|
|
1643
|
+
}
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
h = ah0;
|
|
1647
|
+
l = al0;
|
|
1648
|
+
a = l & 65535;
|
|
1649
|
+
b = l >>> 16;
|
|
1650
|
+
c = h & 65535;
|
|
1651
|
+
d = h >>> 16;
|
|
1652
|
+
h = hh[0];
|
|
1653
|
+
l = hl[0];
|
|
1654
|
+
a += l & 65535;
|
|
1655
|
+
b += l >>> 16;
|
|
1656
|
+
c += h & 65535;
|
|
1657
|
+
d += h >>> 16;
|
|
1658
|
+
b += a >>> 16;
|
|
1659
|
+
c += b >>> 16;
|
|
1660
|
+
d += c >>> 16;
|
|
1661
|
+
hh[0] = ah0 = c & 65535 | d << 16;
|
|
1662
|
+
hl[0] = al0 = a & 65535 | b << 16;
|
|
1663
|
+
h = ah1;
|
|
1664
|
+
l = al1;
|
|
1665
|
+
a = l & 65535;
|
|
1666
|
+
b = l >>> 16;
|
|
1667
|
+
c = h & 65535;
|
|
1668
|
+
d = h >>> 16;
|
|
1669
|
+
h = hh[1];
|
|
1670
|
+
l = hl[1];
|
|
1671
|
+
a += l & 65535;
|
|
1672
|
+
b += l >>> 16;
|
|
1673
|
+
c += h & 65535;
|
|
1674
|
+
d += h >>> 16;
|
|
1675
|
+
b += a >>> 16;
|
|
1676
|
+
c += b >>> 16;
|
|
1677
|
+
d += c >>> 16;
|
|
1678
|
+
hh[1] = ah1 = c & 65535 | d << 16;
|
|
1679
|
+
hl[1] = al1 = a & 65535 | b << 16;
|
|
1680
|
+
h = ah2;
|
|
1681
|
+
l = al2;
|
|
1682
|
+
a = l & 65535;
|
|
1683
|
+
b = l >>> 16;
|
|
1684
|
+
c = h & 65535;
|
|
1685
|
+
d = h >>> 16;
|
|
1686
|
+
h = hh[2];
|
|
1687
|
+
l = hl[2];
|
|
1688
|
+
a += l & 65535;
|
|
1689
|
+
b += l >>> 16;
|
|
1690
|
+
c += h & 65535;
|
|
1691
|
+
d += h >>> 16;
|
|
1692
|
+
b += a >>> 16;
|
|
1693
|
+
c += b >>> 16;
|
|
1694
|
+
d += c >>> 16;
|
|
1695
|
+
hh[2] = ah2 = c & 65535 | d << 16;
|
|
1696
|
+
hl[2] = al2 = a & 65535 | b << 16;
|
|
1697
|
+
h = ah3;
|
|
1698
|
+
l = al3;
|
|
1699
|
+
a = l & 65535;
|
|
1700
|
+
b = l >>> 16;
|
|
1701
|
+
c = h & 65535;
|
|
1702
|
+
d = h >>> 16;
|
|
1703
|
+
h = hh[3];
|
|
1704
|
+
l = hl[3];
|
|
1705
|
+
a += l & 65535;
|
|
1706
|
+
b += l >>> 16;
|
|
1707
|
+
c += h & 65535;
|
|
1708
|
+
d += h >>> 16;
|
|
1709
|
+
b += a >>> 16;
|
|
1710
|
+
c += b >>> 16;
|
|
1711
|
+
d += c >>> 16;
|
|
1712
|
+
hh[3] = ah3 = c & 65535 | d << 16;
|
|
1713
|
+
hl[3] = al3 = a & 65535 | b << 16;
|
|
1714
|
+
h = ah4;
|
|
1715
|
+
l = al4;
|
|
1716
|
+
a = l & 65535;
|
|
1717
|
+
b = l >>> 16;
|
|
1718
|
+
c = h & 65535;
|
|
1719
|
+
d = h >>> 16;
|
|
1720
|
+
h = hh[4];
|
|
1721
|
+
l = hl[4];
|
|
1722
|
+
a += l & 65535;
|
|
1723
|
+
b += l >>> 16;
|
|
1724
|
+
c += h & 65535;
|
|
1725
|
+
d += h >>> 16;
|
|
1726
|
+
b += a >>> 16;
|
|
1727
|
+
c += b >>> 16;
|
|
1728
|
+
d += c >>> 16;
|
|
1729
|
+
hh[4] = ah4 = c & 65535 | d << 16;
|
|
1730
|
+
hl[4] = al4 = a & 65535 | b << 16;
|
|
1731
|
+
h = ah5;
|
|
1732
|
+
l = al5;
|
|
1733
|
+
a = l & 65535;
|
|
1734
|
+
b = l >>> 16;
|
|
1735
|
+
c = h & 65535;
|
|
1736
|
+
d = h >>> 16;
|
|
1737
|
+
h = hh[5];
|
|
1738
|
+
l = hl[5];
|
|
1739
|
+
a += l & 65535;
|
|
1740
|
+
b += l >>> 16;
|
|
1741
|
+
c += h & 65535;
|
|
1742
|
+
d += h >>> 16;
|
|
1743
|
+
b += a >>> 16;
|
|
1744
|
+
c += b >>> 16;
|
|
1745
|
+
d += c >>> 16;
|
|
1746
|
+
hh[5] = ah5 = c & 65535 | d << 16;
|
|
1747
|
+
hl[5] = al5 = a & 65535 | b << 16;
|
|
1748
|
+
h = ah6;
|
|
1749
|
+
l = al6;
|
|
1750
|
+
a = l & 65535;
|
|
1751
|
+
b = l >>> 16;
|
|
1752
|
+
c = h & 65535;
|
|
1753
|
+
d = h >>> 16;
|
|
1754
|
+
h = hh[6];
|
|
1755
|
+
l = hl[6];
|
|
1756
|
+
a += l & 65535;
|
|
1757
|
+
b += l >>> 16;
|
|
1758
|
+
c += h & 65535;
|
|
1759
|
+
d += h >>> 16;
|
|
1760
|
+
b += a >>> 16;
|
|
1761
|
+
c += b >>> 16;
|
|
1762
|
+
d += c >>> 16;
|
|
1763
|
+
hh[6] = ah6 = c & 65535 | d << 16;
|
|
1764
|
+
hl[6] = al6 = a & 65535 | b << 16;
|
|
1765
|
+
h = ah7;
|
|
1766
|
+
l = al7;
|
|
1767
|
+
a = l & 65535;
|
|
1768
|
+
b = l >>> 16;
|
|
1769
|
+
c = h & 65535;
|
|
1770
|
+
d = h >>> 16;
|
|
1771
|
+
h = hh[7];
|
|
1772
|
+
l = hl[7];
|
|
1773
|
+
a += l & 65535;
|
|
1774
|
+
b += l >>> 16;
|
|
1775
|
+
c += h & 65535;
|
|
1776
|
+
d += h >>> 16;
|
|
1777
|
+
b += a >>> 16;
|
|
1778
|
+
c += b >>> 16;
|
|
1779
|
+
d += c >>> 16;
|
|
1780
|
+
hh[7] = ah7 = c & 65535 | d << 16;
|
|
1781
|
+
hl[7] = al7 = a & 65535 | b << 16;
|
|
1782
|
+
pos += 128;
|
|
1783
|
+
n -= 128;
|
|
1784
|
+
}
|
|
1785
|
+
return n;
|
|
1786
|
+
}
|
|
1787
|
+
function crypto_hash(out, m, n) {
|
|
1788
|
+
var hh = new Int32Array(8), hl = new Int32Array(8), x = new Uint8Array(256), i, b = n;
|
|
1789
|
+
hh[0] = 1779033703;
|
|
1790
|
+
hh[1] = 3144134277;
|
|
1791
|
+
hh[2] = 1013904242;
|
|
1792
|
+
hh[3] = 2773480762;
|
|
1793
|
+
hh[4] = 1359893119;
|
|
1794
|
+
hh[5] = 2600822924;
|
|
1795
|
+
hh[6] = 528734635;
|
|
1796
|
+
hh[7] = 1541459225;
|
|
1797
|
+
hl[0] = 4089235720;
|
|
1798
|
+
hl[1] = 2227873595;
|
|
1799
|
+
hl[2] = 4271175723;
|
|
1800
|
+
hl[3] = 1595750129;
|
|
1801
|
+
hl[4] = 2917565137;
|
|
1802
|
+
hl[5] = 725511199;
|
|
1803
|
+
hl[6] = 4215389547;
|
|
1804
|
+
hl[7] = 327033209;
|
|
1805
|
+
crypto_hashblocks_hl(hh, hl, m, n);
|
|
1806
|
+
n %= 128;
|
|
1807
|
+
for (i = 0; i < n; i++) x[i] = m[b - n + i];
|
|
1808
|
+
x[n] = 128;
|
|
1809
|
+
n = 256 - 128 * (n < 112 ? 1 : 0);
|
|
1810
|
+
x[n - 9] = 0;
|
|
1811
|
+
ts64(x, n - 8, b / 536870912 | 0, b << 3);
|
|
1812
|
+
crypto_hashblocks_hl(hh, hl, x, n);
|
|
1813
|
+
for (i = 0; i < 8; i++) ts64(out, 8 * i, hh[i], hl[i]);
|
|
1814
|
+
return 0;
|
|
1815
|
+
}
|
|
1816
|
+
function add(p, q) {
|
|
1817
|
+
var a = gf(), b = gf(), c = gf(), d = gf(), e = gf(), f = gf(), g = gf(), h = gf(), t = gf();
|
|
1818
|
+
Z(a, p[1], p[0]);
|
|
1819
|
+
Z(t, q[1], q[0]);
|
|
1820
|
+
M(a, a, t);
|
|
1821
|
+
A(b, p[0], p[1]);
|
|
1822
|
+
A(t, q[0], q[1]);
|
|
1823
|
+
M(b, b, t);
|
|
1824
|
+
M(c, p[3], q[3]);
|
|
1825
|
+
M(c, c, D2);
|
|
1826
|
+
M(d, p[2], q[2]);
|
|
1827
|
+
A(d, d, d);
|
|
1828
|
+
Z(e, b, a);
|
|
1829
|
+
Z(f, d, c);
|
|
1830
|
+
A(g, d, c);
|
|
1831
|
+
A(h, b, a);
|
|
1832
|
+
M(p[0], e, f);
|
|
1833
|
+
M(p[1], h, g);
|
|
1834
|
+
M(p[2], g, f);
|
|
1835
|
+
M(p[3], e, h);
|
|
1836
|
+
}
|
|
1837
|
+
function cswap(p, q, b) {
|
|
1838
|
+
var i;
|
|
1839
|
+
for (i = 0; i < 4; i++) {
|
|
1840
|
+
sel25519(p[i], q[i], b);
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1843
|
+
function pack(r, p) {
|
|
1844
|
+
var tx = gf(), ty = gf(), zi = gf();
|
|
1845
|
+
inv25519(zi, p[2]);
|
|
1846
|
+
M(tx, p[0], zi);
|
|
1847
|
+
M(ty, p[1], zi);
|
|
1848
|
+
pack25519(r, ty);
|
|
1849
|
+
r[31] ^= par25519(tx) << 7;
|
|
1850
|
+
}
|
|
1851
|
+
function scalarmult(p, q, s) {
|
|
1852
|
+
var b, i;
|
|
1853
|
+
set25519(p[0], gf0);
|
|
1854
|
+
set25519(p[1], gf1);
|
|
1855
|
+
set25519(p[2], gf1);
|
|
1856
|
+
set25519(p[3], gf0);
|
|
1857
|
+
for (i = 255; i >= 0; --i) {
|
|
1858
|
+
b = s[i / 8 | 0] >> (i & 7) & 1;
|
|
1859
|
+
cswap(p, q, b);
|
|
1860
|
+
add(q, p);
|
|
1861
|
+
add(p, p);
|
|
1862
|
+
cswap(p, q, b);
|
|
1863
|
+
}
|
|
1864
|
+
}
|
|
1865
|
+
function scalarbase(p, s) {
|
|
1866
|
+
var q = [gf(), gf(), gf(), gf()];
|
|
1867
|
+
set25519(q[0], X);
|
|
1868
|
+
set25519(q[1], Y);
|
|
1869
|
+
set25519(q[2], gf1);
|
|
1870
|
+
M(q[3], X, Y);
|
|
1871
|
+
scalarmult(p, q, s);
|
|
1872
|
+
}
|
|
1873
|
+
function crypto_sign_keypair(pk, sk, seeded) {
|
|
1874
|
+
var d = new Uint8Array(64);
|
|
1875
|
+
var p = [gf(), gf(), gf(), gf()];
|
|
1876
|
+
var i;
|
|
1877
|
+
if (!seeded) randombytes(sk, 32);
|
|
1878
|
+
crypto_hash(d, sk, 32);
|
|
1879
|
+
d[0] &= 248;
|
|
1880
|
+
d[31] &= 127;
|
|
1881
|
+
d[31] |= 64;
|
|
1882
|
+
scalarbase(p, d);
|
|
1883
|
+
pack(pk, p);
|
|
1884
|
+
for (i = 0; i < 32; i++) sk[i + 32] = pk[i];
|
|
1885
|
+
return 0;
|
|
1886
|
+
}
|
|
1887
|
+
var L = new Float64Array([237, 211, 245, 92, 26, 99, 18, 88, 214, 156, 247, 162, 222, 249, 222, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16]);
|
|
1888
|
+
function modL(r, x) {
|
|
1889
|
+
var carry, i, j, k;
|
|
1890
|
+
for (i = 63; i >= 32; --i) {
|
|
1891
|
+
carry = 0;
|
|
1892
|
+
for (j = i - 32, k = i - 12; j < k; ++j) {
|
|
1893
|
+
x[j] += carry - 16 * x[i] * L[j - (i - 32)];
|
|
1894
|
+
carry = Math.floor((x[j] + 128) / 256);
|
|
1895
|
+
x[j] -= carry * 256;
|
|
1896
|
+
}
|
|
1897
|
+
x[j] += carry;
|
|
1898
|
+
x[i] = 0;
|
|
1899
|
+
}
|
|
1900
|
+
carry = 0;
|
|
1901
|
+
for (j = 0; j < 32; j++) {
|
|
1902
|
+
x[j] += carry - (x[31] >> 4) * L[j];
|
|
1903
|
+
carry = x[j] >> 8;
|
|
1904
|
+
x[j] &= 255;
|
|
1905
|
+
}
|
|
1906
|
+
for (j = 0; j < 32; j++) x[j] -= carry * L[j];
|
|
1907
|
+
for (i = 0; i < 32; i++) {
|
|
1908
|
+
x[i + 1] += x[i] >> 8;
|
|
1909
|
+
r[i] = x[i] & 255;
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
function reduce(r) {
|
|
1913
|
+
var x = new Float64Array(64), i;
|
|
1914
|
+
for (i = 0; i < 64; i++) x[i] = r[i];
|
|
1915
|
+
for (i = 0; i < 64; i++) r[i] = 0;
|
|
1916
|
+
modL(r, x);
|
|
1917
|
+
}
|
|
1918
|
+
function crypto_sign(sm, m, n, sk) {
|
|
1919
|
+
var d = new Uint8Array(64), h = new Uint8Array(64), r = new Uint8Array(64);
|
|
1920
|
+
var i, j, x = new Float64Array(64);
|
|
1921
|
+
var p = [gf(), gf(), gf(), gf()];
|
|
1922
|
+
crypto_hash(d, sk, 32);
|
|
1923
|
+
d[0] &= 248;
|
|
1924
|
+
d[31] &= 127;
|
|
1925
|
+
d[31] |= 64;
|
|
1926
|
+
var smlen = n + 64;
|
|
1927
|
+
for (i = 0; i < n; i++) sm[64 + i] = m[i];
|
|
1928
|
+
for (i = 0; i < 32; i++) sm[32 + i] = d[32 + i];
|
|
1929
|
+
crypto_hash(r, sm.subarray(32), n + 32);
|
|
1930
|
+
reduce(r);
|
|
1931
|
+
scalarbase(p, r);
|
|
1932
|
+
pack(sm, p);
|
|
1933
|
+
for (i = 32; i < 64; i++) sm[i] = sk[i];
|
|
1934
|
+
crypto_hash(h, sm, n + 64);
|
|
1935
|
+
reduce(h);
|
|
1936
|
+
for (i = 0; i < 64; i++) x[i] = 0;
|
|
1937
|
+
for (i = 0; i < 32; i++) x[i] = r[i];
|
|
1938
|
+
for (i = 0; i < 32; i++) {
|
|
1939
|
+
for (j = 0; j < 32; j++) {
|
|
1940
|
+
x[i + j] += h[i] * d[j];
|
|
1941
|
+
}
|
|
1942
|
+
}
|
|
1943
|
+
modL(sm.subarray(32), x);
|
|
1944
|
+
return smlen;
|
|
1945
|
+
}
|
|
1946
|
+
function unpackneg(r, p) {
|
|
1947
|
+
var t = gf(), chk = gf(), num = gf(), den = gf(), den2 = gf(), den4 = gf(), den6 = gf();
|
|
1948
|
+
set25519(r[2], gf1);
|
|
1949
|
+
unpack25519(r[1], p);
|
|
1950
|
+
S(num, r[1]);
|
|
1951
|
+
M(den, num, D);
|
|
1952
|
+
Z(num, num, r[2]);
|
|
1953
|
+
A(den, r[2], den);
|
|
1954
|
+
S(den2, den);
|
|
1955
|
+
S(den4, den2);
|
|
1956
|
+
M(den6, den4, den2);
|
|
1957
|
+
M(t, den6, num);
|
|
1958
|
+
M(t, t, den);
|
|
1959
|
+
pow2523(t, t);
|
|
1960
|
+
M(t, t, num);
|
|
1961
|
+
M(t, t, den);
|
|
1962
|
+
M(t, t, den);
|
|
1963
|
+
M(r[0], t, den);
|
|
1964
|
+
S(chk, r[0]);
|
|
1965
|
+
M(chk, chk, den);
|
|
1966
|
+
if (neq25519(chk, num)) M(r[0], r[0], I);
|
|
1967
|
+
S(chk, r[0]);
|
|
1968
|
+
M(chk, chk, den);
|
|
1969
|
+
if (neq25519(chk, num)) return -1;
|
|
1970
|
+
if (par25519(r[0]) === p[31] >> 7) Z(r[0], gf0, r[0]);
|
|
1971
|
+
M(r[3], r[0], r[1]);
|
|
1972
|
+
return 0;
|
|
1973
|
+
}
|
|
1974
|
+
function crypto_sign_open(m, sm, n, pk) {
|
|
1975
|
+
var i;
|
|
1976
|
+
var t = new Uint8Array(32), h = new Uint8Array(64);
|
|
1977
|
+
var p = [gf(), gf(), gf(), gf()], q = [gf(), gf(), gf(), gf()];
|
|
1978
|
+
if (n < 64) return -1;
|
|
1979
|
+
if (unpackneg(q, pk)) return -1;
|
|
1980
|
+
for (i = 0; i < n; i++) m[i] = sm[i];
|
|
1981
|
+
for (i = 0; i < 32; i++) m[i + 32] = pk[i];
|
|
1982
|
+
crypto_hash(h, m, n);
|
|
1983
|
+
reduce(h);
|
|
1984
|
+
scalarmult(p, q, h);
|
|
1985
|
+
scalarbase(q, sm.subarray(32));
|
|
1986
|
+
add(p, q);
|
|
1987
|
+
pack(t, p);
|
|
1988
|
+
n -= 64;
|
|
1989
|
+
if (crypto_verify_32(sm, 0, t, 0)) {
|
|
1990
|
+
for (i = 0; i < n; i++) m[i] = 0;
|
|
1991
|
+
return -1;
|
|
1992
|
+
}
|
|
1993
|
+
for (i = 0; i < n; i++) m[i] = sm[i + 64];
|
|
1994
|
+
return n;
|
|
1995
|
+
}
|
|
1996
|
+
var crypto_secretbox_KEYBYTES = 32, crypto_secretbox_NONCEBYTES = 24, crypto_secretbox_ZEROBYTES = 32, crypto_secretbox_BOXZEROBYTES = 16, crypto_scalarmult_BYTES = 32, crypto_scalarmult_SCALARBYTES = 32, crypto_box_PUBLICKEYBYTES = 32, crypto_box_SECRETKEYBYTES = 32, crypto_box_BEFORENMBYTES = 32, crypto_box_NONCEBYTES = crypto_secretbox_NONCEBYTES, crypto_box_ZEROBYTES = crypto_secretbox_ZEROBYTES, crypto_box_BOXZEROBYTES = crypto_secretbox_BOXZEROBYTES, crypto_sign_BYTES = 64, crypto_sign_PUBLICKEYBYTES = 32, crypto_sign_SECRETKEYBYTES = 64, crypto_sign_SEEDBYTES = 32, crypto_hash_BYTES = 64;
|
|
1997
|
+
nacl6.lowlevel = {
|
|
1998
|
+
crypto_core_hsalsa20,
|
|
1999
|
+
crypto_stream_xor,
|
|
2000
|
+
crypto_stream,
|
|
2001
|
+
crypto_stream_salsa20_xor,
|
|
2002
|
+
crypto_stream_salsa20,
|
|
2003
|
+
crypto_onetimeauth,
|
|
2004
|
+
crypto_onetimeauth_verify,
|
|
2005
|
+
crypto_verify_16,
|
|
2006
|
+
crypto_verify_32,
|
|
2007
|
+
crypto_secretbox,
|
|
2008
|
+
crypto_secretbox_open,
|
|
2009
|
+
crypto_scalarmult,
|
|
2010
|
+
crypto_scalarmult_base,
|
|
2011
|
+
crypto_box_beforenm,
|
|
2012
|
+
crypto_box_afternm,
|
|
2013
|
+
crypto_box,
|
|
2014
|
+
crypto_box_open,
|
|
2015
|
+
crypto_box_keypair,
|
|
2016
|
+
crypto_hash,
|
|
2017
|
+
crypto_sign,
|
|
2018
|
+
crypto_sign_keypair,
|
|
2019
|
+
crypto_sign_open,
|
|
2020
|
+
crypto_secretbox_KEYBYTES,
|
|
2021
|
+
crypto_secretbox_NONCEBYTES,
|
|
2022
|
+
crypto_secretbox_ZEROBYTES,
|
|
2023
|
+
crypto_secretbox_BOXZEROBYTES,
|
|
2024
|
+
crypto_scalarmult_BYTES,
|
|
2025
|
+
crypto_scalarmult_SCALARBYTES,
|
|
2026
|
+
crypto_box_PUBLICKEYBYTES,
|
|
2027
|
+
crypto_box_SECRETKEYBYTES,
|
|
2028
|
+
crypto_box_BEFORENMBYTES,
|
|
2029
|
+
crypto_box_NONCEBYTES,
|
|
2030
|
+
crypto_box_ZEROBYTES,
|
|
2031
|
+
crypto_box_BOXZEROBYTES,
|
|
2032
|
+
crypto_sign_BYTES,
|
|
2033
|
+
crypto_sign_PUBLICKEYBYTES,
|
|
2034
|
+
crypto_sign_SECRETKEYBYTES,
|
|
2035
|
+
crypto_sign_SEEDBYTES,
|
|
2036
|
+
crypto_hash_BYTES,
|
|
2037
|
+
gf,
|
|
2038
|
+
D,
|
|
2039
|
+
L,
|
|
2040
|
+
pack25519,
|
|
2041
|
+
unpack25519,
|
|
2042
|
+
M,
|
|
2043
|
+
A,
|
|
2044
|
+
S,
|
|
2045
|
+
Z,
|
|
2046
|
+
pow2523,
|
|
2047
|
+
add,
|
|
2048
|
+
set25519,
|
|
2049
|
+
modL,
|
|
2050
|
+
scalarmult,
|
|
2051
|
+
scalarbase
|
|
2052
|
+
};
|
|
2053
|
+
function checkLengths(k, n) {
|
|
2054
|
+
if (k.length !== crypto_secretbox_KEYBYTES) throw new Error("bad key size");
|
|
2055
|
+
if (n.length !== crypto_secretbox_NONCEBYTES) throw new Error("bad nonce size");
|
|
2056
|
+
}
|
|
2057
|
+
function checkBoxLengths(pk, sk) {
|
|
2058
|
+
if (pk.length !== crypto_box_PUBLICKEYBYTES) throw new Error("bad public key size");
|
|
2059
|
+
if (sk.length !== crypto_box_SECRETKEYBYTES) throw new Error("bad secret key size");
|
|
2060
|
+
}
|
|
2061
|
+
function checkArrayTypes() {
|
|
2062
|
+
for (var i = 0; i < arguments.length; i++) {
|
|
2063
|
+
if (!(arguments[i] instanceof Uint8Array))
|
|
2064
|
+
throw new TypeError("unexpected type, use Uint8Array");
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
function cleanup(arr) {
|
|
2068
|
+
for (var i = 0; i < arr.length; i++) arr[i] = 0;
|
|
2069
|
+
}
|
|
2070
|
+
nacl6.randomBytes = function(n) {
|
|
2071
|
+
var b = new Uint8Array(n);
|
|
2072
|
+
randombytes(b, n);
|
|
2073
|
+
return b;
|
|
2074
|
+
};
|
|
2075
|
+
nacl6.secretbox = function(msg, nonce, key) {
|
|
2076
|
+
checkArrayTypes(msg, nonce, key);
|
|
2077
|
+
checkLengths(key, nonce);
|
|
2078
|
+
var m = new Uint8Array(crypto_secretbox_ZEROBYTES + msg.length);
|
|
2079
|
+
var c = new Uint8Array(m.length);
|
|
2080
|
+
for (var i = 0; i < msg.length; i++) m[i + crypto_secretbox_ZEROBYTES] = msg[i];
|
|
2081
|
+
crypto_secretbox(c, m, m.length, nonce, key);
|
|
2082
|
+
return c.subarray(crypto_secretbox_BOXZEROBYTES);
|
|
2083
|
+
};
|
|
2084
|
+
nacl6.secretbox.open = function(box, nonce, key) {
|
|
2085
|
+
checkArrayTypes(box, nonce, key);
|
|
2086
|
+
checkLengths(key, nonce);
|
|
2087
|
+
var c = new Uint8Array(crypto_secretbox_BOXZEROBYTES + box.length);
|
|
2088
|
+
var m = new Uint8Array(c.length);
|
|
2089
|
+
for (var i = 0; i < box.length; i++) c[i + crypto_secretbox_BOXZEROBYTES] = box[i];
|
|
2090
|
+
if (c.length < 32) return null;
|
|
2091
|
+
if (crypto_secretbox_open(m, c, c.length, nonce, key) !== 0) return null;
|
|
2092
|
+
return m.subarray(crypto_secretbox_ZEROBYTES);
|
|
2093
|
+
};
|
|
2094
|
+
nacl6.secretbox.keyLength = crypto_secretbox_KEYBYTES;
|
|
2095
|
+
nacl6.secretbox.nonceLength = crypto_secretbox_NONCEBYTES;
|
|
2096
|
+
nacl6.secretbox.overheadLength = crypto_secretbox_BOXZEROBYTES;
|
|
2097
|
+
nacl6.scalarMult = function(n, p) {
|
|
2098
|
+
checkArrayTypes(n, p);
|
|
2099
|
+
if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error("bad n size");
|
|
2100
|
+
if (p.length !== crypto_scalarmult_BYTES) throw new Error("bad p size");
|
|
2101
|
+
var q = new Uint8Array(crypto_scalarmult_BYTES);
|
|
2102
|
+
crypto_scalarmult(q, n, p);
|
|
2103
|
+
return q;
|
|
2104
|
+
};
|
|
2105
|
+
nacl6.scalarMult.base = function(n) {
|
|
2106
|
+
checkArrayTypes(n);
|
|
2107
|
+
if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error("bad n size");
|
|
2108
|
+
var q = new Uint8Array(crypto_scalarmult_BYTES);
|
|
2109
|
+
crypto_scalarmult_base(q, n);
|
|
2110
|
+
return q;
|
|
2111
|
+
};
|
|
2112
|
+
nacl6.scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES;
|
|
2113
|
+
nacl6.scalarMult.groupElementLength = crypto_scalarmult_BYTES;
|
|
2114
|
+
nacl6.box = function(msg, nonce, publicKey, secretKey) {
|
|
2115
|
+
var k = nacl6.box.before(publicKey, secretKey);
|
|
2116
|
+
return nacl6.secretbox(msg, nonce, k);
|
|
2117
|
+
};
|
|
2118
|
+
nacl6.box.before = function(publicKey, secretKey) {
|
|
2119
|
+
checkArrayTypes(publicKey, secretKey);
|
|
2120
|
+
checkBoxLengths(publicKey, secretKey);
|
|
2121
|
+
var k = new Uint8Array(crypto_box_BEFORENMBYTES);
|
|
2122
|
+
crypto_box_beforenm(k, publicKey, secretKey);
|
|
2123
|
+
return k;
|
|
2124
|
+
};
|
|
2125
|
+
nacl6.box.after = nacl6.secretbox;
|
|
2126
|
+
nacl6.box.open = function(msg, nonce, publicKey, secretKey) {
|
|
2127
|
+
var k = nacl6.box.before(publicKey, secretKey);
|
|
2128
|
+
return nacl6.secretbox.open(msg, nonce, k);
|
|
2129
|
+
};
|
|
2130
|
+
nacl6.box.open.after = nacl6.secretbox.open;
|
|
2131
|
+
nacl6.box.keyPair = function() {
|
|
2132
|
+
var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
|
|
2133
|
+
var sk = new Uint8Array(crypto_box_SECRETKEYBYTES);
|
|
2134
|
+
crypto_box_keypair(pk, sk);
|
|
2135
|
+
return { publicKey: pk, secretKey: sk };
|
|
2136
|
+
};
|
|
2137
|
+
nacl6.box.keyPair.fromSecretKey = function(secretKey) {
|
|
2138
|
+
checkArrayTypes(secretKey);
|
|
2139
|
+
if (secretKey.length !== crypto_box_SECRETKEYBYTES)
|
|
2140
|
+
throw new Error("bad secret key size");
|
|
2141
|
+
var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
|
|
2142
|
+
crypto_scalarmult_base(pk, secretKey);
|
|
2143
|
+
return { publicKey: pk, secretKey: new Uint8Array(secretKey) };
|
|
2144
|
+
};
|
|
2145
|
+
nacl6.box.publicKeyLength = crypto_box_PUBLICKEYBYTES;
|
|
2146
|
+
nacl6.box.secretKeyLength = crypto_box_SECRETKEYBYTES;
|
|
2147
|
+
nacl6.box.sharedKeyLength = crypto_box_BEFORENMBYTES;
|
|
2148
|
+
nacl6.box.nonceLength = crypto_box_NONCEBYTES;
|
|
2149
|
+
nacl6.box.overheadLength = nacl6.secretbox.overheadLength;
|
|
2150
|
+
nacl6.sign = function(msg, secretKey) {
|
|
2151
|
+
checkArrayTypes(msg, secretKey);
|
|
2152
|
+
if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
|
|
2153
|
+
throw new Error("bad secret key size");
|
|
2154
|
+
var signedMsg = new Uint8Array(crypto_sign_BYTES + msg.length);
|
|
2155
|
+
crypto_sign(signedMsg, msg, msg.length, secretKey);
|
|
2156
|
+
return signedMsg;
|
|
2157
|
+
};
|
|
2158
|
+
nacl6.sign.open = function(signedMsg, publicKey) {
|
|
2159
|
+
checkArrayTypes(signedMsg, publicKey);
|
|
2160
|
+
if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
|
|
2161
|
+
throw new Error("bad public key size");
|
|
2162
|
+
var tmp = new Uint8Array(signedMsg.length);
|
|
2163
|
+
var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey);
|
|
2164
|
+
if (mlen < 0) return null;
|
|
2165
|
+
var m = new Uint8Array(mlen);
|
|
2166
|
+
for (var i = 0; i < m.length; i++) m[i] = tmp[i];
|
|
2167
|
+
return m;
|
|
2168
|
+
};
|
|
2169
|
+
nacl6.sign.detached = function(msg, secretKey) {
|
|
2170
|
+
var signedMsg = nacl6.sign(msg, secretKey);
|
|
2171
|
+
var sig = new Uint8Array(crypto_sign_BYTES);
|
|
2172
|
+
for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i];
|
|
2173
|
+
return sig;
|
|
2174
|
+
};
|
|
2175
|
+
nacl6.sign.detached.verify = function(msg, sig, publicKey) {
|
|
2176
|
+
checkArrayTypes(msg, sig, publicKey);
|
|
2177
|
+
if (sig.length !== crypto_sign_BYTES)
|
|
2178
|
+
throw new Error("bad signature size");
|
|
2179
|
+
if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
|
|
2180
|
+
throw new Error("bad public key size");
|
|
2181
|
+
var sm = new Uint8Array(crypto_sign_BYTES + msg.length);
|
|
2182
|
+
var m = new Uint8Array(crypto_sign_BYTES + msg.length);
|
|
2183
|
+
var i;
|
|
2184
|
+
for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i];
|
|
2185
|
+
for (i = 0; i < msg.length; i++) sm[i + crypto_sign_BYTES] = msg[i];
|
|
2186
|
+
return crypto_sign_open(m, sm, sm.length, publicKey) >= 0;
|
|
2187
|
+
};
|
|
2188
|
+
nacl6.sign.keyPair = function() {
|
|
2189
|
+
var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
|
|
2190
|
+
var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
|
|
2191
|
+
crypto_sign_keypair(pk, sk);
|
|
2192
|
+
return { publicKey: pk, secretKey: sk };
|
|
2193
|
+
};
|
|
2194
|
+
nacl6.sign.keyPair.fromSecretKey = function(secretKey) {
|
|
2195
|
+
checkArrayTypes(secretKey);
|
|
2196
|
+
if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
|
|
2197
|
+
throw new Error("bad secret key size");
|
|
2198
|
+
var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
|
|
2199
|
+
for (var i = 0; i < pk.length; i++) pk[i] = secretKey[32 + i];
|
|
2200
|
+
return { publicKey: pk, secretKey: new Uint8Array(secretKey) };
|
|
2201
|
+
};
|
|
2202
|
+
nacl6.sign.keyPair.fromSeed = function(seed) {
|
|
2203
|
+
checkArrayTypes(seed);
|
|
2204
|
+
if (seed.length !== crypto_sign_SEEDBYTES)
|
|
2205
|
+
throw new Error("bad seed size");
|
|
2206
|
+
var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
|
|
2207
|
+
var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
|
|
2208
|
+
for (var i = 0; i < 32; i++) sk[i] = seed[i];
|
|
2209
|
+
crypto_sign_keypair(pk, sk, true);
|
|
2210
|
+
return { publicKey: pk, secretKey: sk };
|
|
2211
|
+
};
|
|
2212
|
+
nacl6.sign.publicKeyLength = crypto_sign_PUBLICKEYBYTES;
|
|
2213
|
+
nacl6.sign.secretKeyLength = crypto_sign_SECRETKEYBYTES;
|
|
2214
|
+
nacl6.sign.seedLength = crypto_sign_SEEDBYTES;
|
|
2215
|
+
nacl6.sign.signatureLength = crypto_sign_BYTES;
|
|
2216
|
+
nacl6.hash = function(msg) {
|
|
2217
|
+
checkArrayTypes(msg);
|
|
2218
|
+
var h = new Uint8Array(crypto_hash_BYTES);
|
|
2219
|
+
crypto_hash(h, msg, msg.length);
|
|
2220
|
+
return h;
|
|
2221
|
+
};
|
|
2222
|
+
nacl6.hash.hashLength = crypto_hash_BYTES;
|
|
2223
|
+
nacl6.verify = function(x, y) {
|
|
2224
|
+
checkArrayTypes(x, y);
|
|
2225
|
+
if (x.length === 0 || y.length === 0) return false;
|
|
2226
|
+
if (x.length !== y.length) return false;
|
|
2227
|
+
return vn(x, 0, y, 0, x.length) === 0 ? true : false;
|
|
2228
|
+
};
|
|
2229
|
+
nacl6.setPRNG = function(fn) {
|
|
2230
|
+
randombytes = fn;
|
|
2231
|
+
};
|
|
2232
|
+
(function() {
|
|
2233
|
+
var crypto = typeof self !== "undefined" ? self.crypto || self.msCrypto : null;
|
|
2234
|
+
if (crypto && crypto.getRandomValues) {
|
|
2235
|
+
var QUOTA = 65536;
|
|
2236
|
+
nacl6.setPRNG(function(x, n) {
|
|
2237
|
+
var i, v = new Uint8Array(n);
|
|
2238
|
+
for (i = 0; i < n; i += QUOTA) {
|
|
2239
|
+
crypto.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)));
|
|
2240
|
+
}
|
|
2241
|
+
for (i = 0; i < n; i++) x[i] = v[i];
|
|
2242
|
+
cleanup(v);
|
|
2243
|
+
});
|
|
2244
|
+
} else if (typeof __require !== "undefined") {
|
|
2245
|
+
crypto = __require("crypto");
|
|
2246
|
+
if (crypto && crypto.randomBytes) {
|
|
2247
|
+
nacl6.setPRNG(function(x, n) {
|
|
2248
|
+
var i, v = crypto.randomBytes(n);
|
|
2249
|
+
for (i = 0; i < n; i++) x[i] = v[i];
|
|
2250
|
+
cleanup(v);
|
|
2251
|
+
});
|
|
2252
|
+
}
|
|
2253
|
+
}
|
|
2254
|
+
})();
|
|
2255
|
+
})(typeof module !== "undefined" && module.exports ? module.exports : self.nacl = self.nacl || {});
|
|
2256
|
+
}
|
|
2257
|
+
});
|
|
2258
|
+
|
|
2259
|
+
// ../../packages/schema/dist/index.js
|
|
2260
|
+
import { z } from "zod";
|
|
2261
|
+
var ContextSchema = z.object({
|
|
2262
|
+
title: z.string().min(1).describe("One-line headline of what you need (required, non-empty)."),
|
|
2263
|
+
description: z.array(z.string().min(1)).min(1).describe("Semantic chunks of detail (each a standalone, non-empty piece). The user can select chunks to ask you to expand.")
|
|
2264
|
+
});
|
|
2265
|
+
var OptionSchema = z.object({
|
|
2266
|
+
id: z.string(),
|
|
2267
|
+
label: z.string(),
|
|
2268
|
+
// .describe() flows into the MCP notify_user JSON schema (zodToJsonSchema), so
|
|
2269
|
+
// the constraints below are what an agent reads when deciding to use these.
|
|
2270
|
+
html: z.string().max(16384).describe(
|
|
2271
|
+
"Optional sandboxed HTML/CSS preview for a visual 'pick one' (shown in the option card). Untrusted-sandboxed: NO JavaScript, NO external network or images \u2014 inline CSS and data: URIs only; <=16KB. Use for layout/CSS mockups, tables, diffs. For a hosted image use `image` instead."
|
|
2272
|
+
).optional(),
|
|
2273
|
+
image: z.string().url().describe(
|
|
2274
|
+
"Optional image URL rendered as the option's preview (plain image, not sandboxed). For agent-generated HTML/CSS mockups, use `html` instead."
|
|
2275
|
+
).optional()
|
|
2276
|
+
});
|
|
2277
|
+
var VisualSchema = z.object({
|
|
2278
|
+
url: z.string().url(),
|
|
2279
|
+
label: z.string().optional()
|
|
2280
|
+
});
|
|
2281
|
+
var NotifyLevelSchema = z.enum(["inbox", "push", "banner", "call"]);
|
|
2282
|
+
var NotifyRequestSchema = z.object({
|
|
2283
|
+
/** Plaintext message content. Present on the plaintext path (today's shape);
|
|
2284
|
+
* ABSENT on the E2EE path, where the sealed `envelope` below carries it. The
|
|
2285
|
+
* superRefine at the bottom enforces exactly one of the two. */
|
|
2286
|
+
context: ContextSchema.optional(),
|
|
2287
|
+
options: z.array(OptionSchema.omit({ id: true })).min(1).optional().describe(
|
|
2288
|
+
"The choices, in order \u2014 required when select is 'one'/'many'/'rank', omitted otherwise. Ids are assigned automatically by position ('1', '2', \u2026); the user's answer references them as optionId(s)."
|
|
2289
|
+
),
|
|
2290
|
+
points: z.array(z.string().min(1)).optional().describe(
|
|
2291
|
+
"The distinct things you need answered, each a short phrase \u2014 on a call the broker keeps the conversation going until each is addressed, and the reply reports which were covered, so a half-answer is never silently returned as final. Omit for single-part asks."
|
|
2292
|
+
),
|
|
2293
|
+
visuals: z.array(VisualSchema).optional().describe(
|
|
2294
|
+
"Images attached to the message itself \u2014 context for the whole question (a screenshot, a chart). For a preview on one selectable choice, use that option's `html`/`image` instead."
|
|
2295
|
+
),
|
|
2296
|
+
/** Git repo the agent is working in ("owner/name"). Local MCP fills this from the checkout — omit unless overriding. */
|
|
2297
|
+
repo: z.string().optional(),
|
|
2298
|
+
/** Git branch the agent is on. Local MCP fills this from the checkout — omit unless overriding. */
|
|
2299
|
+
branch: z.string().optional(),
|
|
2300
|
+
/** Continue an existing conversation; omitted = start a new thread. */
|
|
2301
|
+
threadId: z.string().uuid().optional(),
|
|
2302
|
+
urgency: NotifyLevelSchema.default("inbox").describe(
|
|
2303
|
+
"The level you're requesting \u2014 the user's account permissions + session mode can lower it. 'inbox' (default) = sits silently in the inbox for the user to get to. 'push' = a quiet passive push (lands in Notification Center, no sound) \u2014 a gentle heads-up. 'banner' = a time-sensitive banner/lock-screen push with sound (a 'paige') they tap to open \u2014 use when you need them soon-ish but it's not worth ringing them. 'call' = rings the user's phone now (a CallKit voice call) \u2014 use only when you genuinely need them in the moment (blocked and waiting, time-sensitive). context.title is what they see on the banner/ring, so make it specific."
|
|
2304
|
+
),
|
|
2305
|
+
/** The request this one was spawned from, for a clarification. */
|
|
2306
|
+
parentId: z.string().optional(),
|
|
2307
|
+
/** E2EE (text lane): when the pairing is E2EE, the sealed replacements for the
|
|
2308
|
+
* plaintext content fields, keyed by field name. FINALIZED wire shape (was
|
|
2309
|
+
* provisional in the storage PR): a per-field map `{ context?, options?,
|
|
2310
|
+
* visuals? }` of opaque Envelopes — one seal per present content field, so a
|
|
2311
|
+
* message with no options/visuals seals only `context`. It COEXISTS with the
|
|
2312
|
+
* plaintext fields by mutual exclusion: the superRefine below requires that
|
|
2313
|
+
* when `envelope` is present the plaintext `context`/`options`/`visuals` are
|
|
2314
|
+
* ABSENT (and vice-versa), so a row is either fully plaintext or fully sealed —
|
|
2315
|
+
* never a readable half. The server persists this OPAQUELY into
|
|
2316
|
+
* notifications.envelope and relays it blindly; it never decrypts. Absent =
|
|
2317
|
+
* today's plaintext path (context/options/visuals carry the cleartext).
|
|
2318
|
+
* z.lazy because EnvelopeSchema is declared further down (E2EE section). */
|
|
2319
|
+
envelope: z.object({
|
|
2320
|
+
context: z.lazy(() => EnvelopeSchema).optional(),
|
|
2321
|
+
options: z.lazy(() => EnvelopeSchema).optional(),
|
|
2322
|
+
visuals: z.lazy(() => EnvelopeSchema).optional()
|
|
2323
|
+
}).optional(),
|
|
2324
|
+
select: z.enum(["one", "many", "rank", "confirm", "text"]).optional().describe(
|
|
2325
|
+
"How the user answers \u2014 required on the fully-shaped form, pick the shape that fits the question: 'one' = pick one option, 'many' = pick several, 'rank' = pick & order (each needs `options`); 'confirm' = yes/no or approve/deny; 'text' = free-form reply only (status updates, open questions). 'confirm' and 'text' take no options. Omit only when sending the simplified `ask` form \u2014 the broker picks the shape."
|
|
2326
|
+
),
|
|
2327
|
+
/** The simplified form (#395): instead of shaping the notification yourself
|
|
2328
|
+
* (context/select/options/urgency), state what you need to learn and why it
|
|
2329
|
+
* matters now — the broker derives the optimal shape and channel. Mutually
|
|
2330
|
+
* exclusive with `context` (and never sent alongside `envelope`: E2EE pairings
|
|
2331
|
+
* derive agent-side before sealing, so the server only ever shapes plaintext). */
|
|
2332
|
+
ask: z.string().min(1).optional().describe(
|
|
2333
|
+
'SIMPLIFIED FORM \u2014 state in plain prose what you need to learn from the user and why it matters now (e.g. "I need to know whether to deploy the auth fix \u2014 tests are green, staging verified"). Paigy derives the title, answer shape, options, and delivery channel for you. Mutually exclusive with context/select/options \u2014 send one form or the other.'
|
|
2334
|
+
),
|
|
2335
|
+
needs: z.array(z.string().min(1)).optional().describe(
|
|
2336
|
+
"With `ask` only: the distinct things you need answered when the ask is multi-part \u2014 becomes the coverage contract (`points`), so a half-answer is never silently final."
|
|
2337
|
+
),
|
|
2338
|
+
urgencyHint: z.enum(["whenever", "soon", "now"]).optional().describe(
|
|
2339
|
+
"With `ask` only: how urgently you need the answer \u2014 'whenever' (inbox), 'soon' (worth a heads-up), 'now' (you're blocked this minute). A hint, not a command: the user's settings still have the final word."
|
|
2340
|
+
),
|
|
2341
|
+
confirmStyle: z.enum(["yesno", "approve"]).default("yesno").describe(
|
|
2342
|
+
"Labels for a select:'confirm' paige \u2014 'yesno' (Yes/No) or 'approve' (Approve/Deny). Ignored unless select is 'confirm'."
|
|
2343
|
+
),
|
|
2344
|
+
blocking: z.boolean().default(false).describe(
|
|
2345
|
+
"Set true when real downstream work is stuck behind this specific decision \u2014 you can't make meaningful progress until it's answered. This is the real signal for how urgently the user should be reached; it's what the premier use case (an agent that stays unblocked instead of going idle) depends on. Independent of `urgency`: a `banner`-level question can still be `blocking` (something IS stuck, just not time-critical enough to ring for immediately) \u2014 if it goes unanswered a while, Paigy escalates it to a real call using this flag rather than guessing from how many other things happen to be pending. Leave false for anything you could work around, defer, or where other useful work exists meanwhile."
|
|
2346
|
+
)
|
|
2347
|
+
}).superRefine((r, ctx) => {
|
|
2348
|
+
const sealed = !!r.envelope;
|
|
2349
|
+
if (sealed) {
|
|
2350
|
+
if (!r.envelope?.context)
|
|
2351
|
+
ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["envelope", "context"], message: "sealed request must include envelope.context" });
|
|
2352
|
+
for (const f of ["context", "options", "visuals"]) {
|
|
2353
|
+
if (r[f] !== void 0)
|
|
2354
|
+
ctx.addIssue({ code: z.ZodIssueCode.custom, path: [f], message: `E2EE request must not carry plaintext ${f} \u2014 it's sealed in envelope.${f}` });
|
|
2355
|
+
}
|
|
2356
|
+
if (r.points !== void 0)
|
|
2357
|
+
ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["points"], message: "E2EE request must not carry plaintext points" });
|
|
2358
|
+
if (r.ask !== void 0)
|
|
2359
|
+
ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["ask"], message: "E2EE request must not carry a plaintext ask \u2014 derive the shape agent-side and seal it" });
|
|
2360
|
+
if (r.needs !== void 0)
|
|
2361
|
+
ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["needs"], message: "E2EE request must not carry plaintext needs" });
|
|
2362
|
+
return;
|
|
2363
|
+
}
|
|
2364
|
+
if (r.ask !== void 0) {
|
|
2365
|
+
for (const f of ["context", "options", "select", "points", "visuals"]) {
|
|
2366
|
+
if (r[f] !== void 0)
|
|
2367
|
+
ctx.addIssue({ code: z.ZodIssueCode.custom, path: [f], message: `the simplified \`ask\` form takes no ${f} \u2014 the broker derives it (use \`needs\` for multi-part asks)` });
|
|
2368
|
+
}
|
|
2369
|
+
return;
|
|
2370
|
+
}
|
|
2371
|
+
if (r.needs !== void 0 || r.urgencyHint !== void 0)
|
|
2372
|
+
ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["needs"], message: "needs/urgencyHint belong to the simplified `ask` form \u2014 with a shaped request use points/urgency" });
|
|
2373
|
+
if (!r.context)
|
|
2374
|
+
ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["context"], message: "context is required (plaintext path)" });
|
|
2375
|
+
if (!r.select)
|
|
2376
|
+
ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["select"], message: "select is required on the shaped form" });
|
|
2377
|
+
const needsOptions = r.select === "one" || r.select === "many" || r.select === "rank";
|
|
2378
|
+
if (needsOptions && !r.options?.length)
|
|
2379
|
+
ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["options"], message: `select:'${r.select}' requires options` });
|
|
2380
|
+
if (!needsOptions && r.options?.length)
|
|
2381
|
+
ctx.addIssue({ code: z.ZodIssueCode.custom, path: ["options"], message: `select:'${r.select}' takes no options` });
|
|
2382
|
+
});
|
|
2383
|
+
function deriveAsk(req) {
|
|
2384
|
+
if (!req.ask) return req;
|
|
2385
|
+
const text = req.ask.trim();
|
|
2386
|
+
const firstSentence = (/^[^.!?\n]+[.!?]?/.exec(text)?.[0] ?? text).trim();
|
|
2387
|
+
const title = firstSentence.length > 90 ? `${firstSentence.slice(0, 87).trimEnd()}\u2026` : firstSentence;
|
|
2388
|
+
const urgency = req.urgencyHint === "now" ? "call" : req.urgencyHint === "soon" ? "banner" : req.urgencyHint === "whenever" ? "inbox" : req.urgency;
|
|
2389
|
+
const { ask: _ask, needs, urgencyHint: _hint, ...rest } = req;
|
|
2390
|
+
return {
|
|
2391
|
+
...rest,
|
|
2392
|
+
context: { title, description: [text] },
|
|
2393
|
+
select: "text",
|
|
2394
|
+
urgency,
|
|
2395
|
+
...needs?.length ? { points: needs } : {}
|
|
2396
|
+
};
|
|
2397
|
+
}
|
|
2398
|
+
var NotifyStatusSchema = z.enum(["pending", "answered", "ignored"]);
|
|
2399
|
+
var AgentStateSchema = z.enum(["idle", "in_progress", "completed", "needs_input"]);
|
|
2400
|
+
var SetTaskStateSchema = z.object({
|
|
2401
|
+
state: z.enum(["in_progress", "completed", "needs_input"])
|
|
2402
|
+
});
|
|
2403
|
+
var TurnSchema = z.object({
|
|
2404
|
+
prompt: z.string(),
|
|
2405
|
+
reply: z.string()
|
|
2406
|
+
});
|
|
2407
|
+
var UserAnswerSchema = z.discriminatedUnion("kind", [
|
|
2408
|
+
z.object({ kind: z.literal("option"), optionId: z.string() }),
|
|
2409
|
+
z.object({ kind: z.literal("text"), text: z.string() }),
|
|
2410
|
+
z.object({ kind: z.literal("ignored") }),
|
|
2411
|
+
z.object({ kind: z.literal("multi"), optionIds: z.array(z.string()) }),
|
|
2412
|
+
z.object({ kind: z.literal("ranked"), optionIds: z.array(z.string()) }),
|
|
2413
|
+
z.object({ kind: z.literal("clarify"), chunks: z.array(z.string()).min(1) }),
|
|
2414
|
+
z.object({ kind: z.literal("confirm"), approved: z.boolean() }),
|
|
2415
|
+
z.object({ kind: z.literal("turns"), turns: z.array(TurnSchema).min(1) })
|
|
2416
|
+
]);
|
|
2417
|
+
var IntentSchema = z.object({
|
|
2418
|
+
kind: z.enum(["defer", "delegate", "channel"]),
|
|
2419
|
+
detail: z.string(),
|
|
2420
|
+
/** Landed defer (#397): the MCP parses common spoken forms ("in 20 minutes",
|
|
2421
|
+
* "after lunch") against the agent machine's clock — the user's — and attaches
|
|
2422
|
+
* the seconds, ready to pass straight to schedule_callback. Absent when the
|
|
2423
|
+
* detail didn't parse (the agent interprets it) or the kind isn't defer. */
|
|
2424
|
+
dueInSeconds: z.number().int().positive().optional()
|
|
2425
|
+
});
|
|
2426
|
+
var AwaitItemSchema = z.discriminatedUnion("type", [
|
|
2427
|
+
z.object({
|
|
2428
|
+
type: z.literal("reply"),
|
|
2429
|
+
threadId: z.string(),
|
|
2430
|
+
notificationId: z.string(),
|
|
2431
|
+
answer: UserAnswerSchema,
|
|
2432
|
+
/** E2EE: present when the answer is sealed. The server relays the opaque answer
|
|
2433
|
+
* envelope + the plaintext `ignored` status hint; the receiving agent OPENS it
|
|
2434
|
+
* with its device key and re-derives the real UserAnswer, treating a mismatch
|
|
2435
|
+
* against `ignored` as tampering. Absent = today's plaintext answer (in `answer`).
|
|
2436
|
+
* On a sealed reply the plaintext `answer` is a placeholder (kind reflects only
|
|
2437
|
+
* the `ignored` bit) — never the real content, which stays sealed. */
|
|
2438
|
+
sealed: z.lazy(() => SealedAnswerSchema).optional(),
|
|
2439
|
+
/** Broker extras (#381), present when the answer was mapped from a call: next
|
|
2440
|
+
* steps the user attached ("call me after lunch" → defer — act on it via
|
|
2441
|
+
* schedule_callback) and the raw words the shaped answer was mapped from. */
|
|
2442
|
+
intents: z.array(IntentSchema).optional(),
|
|
2443
|
+
transcript: z.string().optional(),
|
|
2444
|
+
/** Coverage report (#396), when the ask declared `points`: which of them this
|
|
2445
|
+
* answer addressed. Missing points = re-ask or proceed knowingly partial. */
|
|
2446
|
+
covered: z.array(z.string()).optional()
|
|
2447
|
+
}),
|
|
2448
|
+
z.object({
|
|
2449
|
+
type: z.literal("remind"),
|
|
2450
|
+
threadId: z.string(),
|
|
2451
|
+
notificationId: z.string(),
|
|
2452
|
+
remindAt: z.string().datetime({ offset: true }),
|
|
2453
|
+
/** Seconds until remindAt, server-computed — pass straight to ScheduleWakeup. */
|
|
2454
|
+
remindInSeconds: z.number()
|
|
2455
|
+
}),
|
|
2456
|
+
z.object({ type: z.literal("idle") })
|
|
2457
|
+
]);
|
|
2458
|
+
var CallbackTriggerSchema = z.enum(["on_done", "on_blocked", "scheduled"]);
|
|
2459
|
+
var ScheduleCallbackSchema = z.object({
|
|
2460
|
+
threadId: z.string().describe("The thread to call back on (from a prior notify_user / reply / request)."),
|
|
2461
|
+
trigger: CallbackTriggerSchema,
|
|
2462
|
+
dueInSeconds: z.number().int().positive().optional().describe("For 'scheduled' only: how many seconds from now to fire."),
|
|
2463
|
+
note: z.string().optional().describe("What to tell the user when you follow up.")
|
|
2464
|
+
});
|
|
2465
|
+
var PendingRepliesSchema = z.object({
|
|
2466
|
+
replies: z.array(
|
|
2467
|
+
z.object({
|
|
2468
|
+
threadId: z.string(),
|
|
2469
|
+
notificationId: z.string(),
|
|
2470
|
+
answer: UserAnswerSchema,
|
|
2471
|
+
/** E2EE: the sealed answer (opaque envelope + plaintext `ignored` hint) when the
|
|
2472
|
+
* pairing is E2EE — the agent opens it and re-derives the real answer. Absent =
|
|
2473
|
+
* plaintext answer (in `answer`). See AwaitItemSchema's reply variant. */
|
|
2474
|
+
sealed: z.lazy(() => SealedAnswerSchema).optional(),
|
|
2475
|
+
/** Broker extras (#381): next steps the user attached to a call-mapped answer
|
|
2476
|
+
* ("call me after lunch" → defer — act on it via schedule_callback) and the
|
|
2477
|
+
* raw words the shaped answer was mapped from. */
|
|
2478
|
+
intents: z.array(IntentSchema).optional(),
|
|
2479
|
+
transcript: z.string().optional(),
|
|
2480
|
+
/** Coverage report (#396): which declared `points` this answer addressed. */
|
|
2481
|
+
covered: z.array(z.string()).optional()
|
|
2482
|
+
})
|
|
2483
|
+
),
|
|
2484
|
+
pending: z.array(
|
|
2485
|
+
z.object({ threadId: z.string(), notificationId: z.string(), createdAt: z.string() })
|
|
2486
|
+
),
|
|
2487
|
+
/** User-initiated requests addressed to this agent; act on them and reply via
|
|
2488
|
+
* notify_user on the same threadId. Keeps reappearing until you call
|
|
2489
|
+
* set_task_state on its notificationId. */
|
|
2490
|
+
requests: z.array(
|
|
2491
|
+
z.object({ threadId: z.string(), notificationId: z.string(), text: z.string(), createdAt: z.string() })
|
|
2492
|
+
),
|
|
2493
|
+
/** Callbacks you owe the user that are now DUE (you said you'd follow up when done,
|
|
2494
|
+
* if blocked, or at a time that has passed). Re-surfaced every sweep until you
|
|
2495
|
+
* fulfill one by calling notify_user on its threadId. */
|
|
2496
|
+
owedCallbacks: z.array(
|
|
2497
|
+
z.object({ threadId: z.string(), trigger: CallbackTriggerSchema, note: z.string() })
|
|
2498
|
+
),
|
|
2499
|
+
/** Work (either direction) you reported in_progress a while ago and never reported
|
|
2500
|
+
* completed — likely left half-done by this session or a prior one that crashed or
|
|
2501
|
+
* went idle. Report a real state (set_task_state) or continue the work. */
|
|
2502
|
+
stalled: z.array(
|
|
2503
|
+
z.object({ threadId: z.string(), notificationId: z.string(), title: z.string().nullable(), startedAt: z.string() })
|
|
2504
|
+
)
|
|
2505
|
+
});
|
|
2506
|
+
var NotifyResponseSchema = z.object({
|
|
2507
|
+
notificationId: z.string(),
|
|
2508
|
+
status: NotifyStatusSchema,
|
|
2509
|
+
createdAt: z.string().datetime(),
|
|
2510
|
+
answer: UserAnswerSchema.optional(),
|
|
2511
|
+
answeredAt: z.string().datetime().optional()
|
|
2512
|
+
});
|
|
2513
|
+
var UserResponseSchema = z.object({
|
|
2514
|
+
requestId: z.string(),
|
|
2515
|
+
answer: UserAnswerSchema,
|
|
2516
|
+
answeredAt: z.string().datetime(),
|
|
2517
|
+
intents: z.array(IntentSchema).optional(),
|
|
2518
|
+
transcript: z.string().optional(),
|
|
2519
|
+
/** E2EE: the sealed answer (opaque envelope + plaintext `ignored` hint) when the item was
|
|
2520
|
+
* E2EE-sealed. Present → the server persists it opaquely and branches status on `ignored`;
|
|
2521
|
+
* the plaintext `answer` is a placeholder (`{ kind: "ignored" }`) the server ignores for a
|
|
2522
|
+
* sealed row. Absent = today's plaintext answer, unchanged. */
|
|
2523
|
+
sealed: z.lazy(() => SealedAnswerSchema).optional(),
|
|
2524
|
+
/** Coverage report (#396): which of the ask's declared `points` were addressed. */
|
|
2525
|
+
covered: z.array(z.string()).optional()
|
|
2526
|
+
});
|
|
2527
|
+
var InboxItemSchema = z.object({
|
|
2528
|
+
id: z.string(),
|
|
2529
|
+
status: NotifyStatusSchema,
|
|
2530
|
+
context: ContextSchema,
|
|
2531
|
+
options: z.array(OptionSchema).optional(),
|
|
2532
|
+
/** The ask's declared coverage points (#396), when the agent sent them. */
|
|
2533
|
+
points: z.array(z.string()).optional(),
|
|
2534
|
+
/** On a replied detail (#397): the next steps the user attached to the answer
|
|
2535
|
+
* ("call back after lunch") — shown so they can see the commitment was captured. */
|
|
2536
|
+
intents: z.array(IntentSchema).optional(),
|
|
2537
|
+
visuals: z.array(VisualSchema).optional(),
|
|
2538
|
+
agent: z.string(),
|
|
2539
|
+
nickname: z.string(),
|
|
2540
|
+
repo: z.string().optional(),
|
|
2541
|
+
branch: z.string().optional(),
|
|
2542
|
+
createdAt: z.string().datetime(),
|
|
2543
|
+
snoozedUntil: z.string().datetime().optional(),
|
|
2544
|
+
agentState: AgentStateSchema.default("idle"),
|
|
2545
|
+
/** Whose action the item is waiting on: "you" = an agent asked you (the default,
|
|
2546
|
+
* every agent→user notification); "agent" = you sent a request and it's awaiting the
|
|
2547
|
+
* agent (held in the inbox until the agent replies on the thread). */
|
|
2548
|
+
turn: z.enum(["you", "agent"]).default("you"),
|
|
2549
|
+
/** Hard error reason on an awaiting request (turn="agent") — the wake failed to reach
|
|
2550
|
+
* the agent (provider-agnostic; set server-side). Absent = no hard error, though the
|
|
2551
|
+
* client may still flag a stall by age. Drives the inbox error badge + Retry. */
|
|
2552
|
+
error: z.string().optional(),
|
|
2553
|
+
parentId: z.string().optional(),
|
|
2554
|
+
select: z.enum(["one", "many", "rank", "confirm", "text"]).default("one"),
|
|
2555
|
+
confirmStyle: z.enum(["yesno", "approve"]).default("yesno").describe(
|
|
2556
|
+
"Labels for a select:'confirm' paige \u2014 'yesno' (Yes/No) or 'approve' (Approve/Deny). Ignored unless select is 'confirm'."
|
|
2557
|
+
),
|
|
2558
|
+
/** Real downstream work is stuck behind this one — set by the agent, independent of
|
|
2559
|
+
* urgency (see the main README's "premier use case" + notify/states.md). Drives the
|
|
2560
|
+
* inbox's blocking badge and the extra confirm step before dismissing it. */
|
|
2561
|
+
blocking: z.boolean().default(false),
|
|
2562
|
+
/** The user's locked-in answer; present only for replied items (GET /api/replied/:id). */
|
|
2563
|
+
answer: UserAnswerSchema.optional(),
|
|
2564
|
+
/** E2EE (text lane): the sealed content this item carries when the pairing is E2EE, in
|
|
2565
|
+
* place of the plaintext `context`/`options`/`visuals` (which the operator would
|
|
2566
|
+
* otherwise read). A per-field map of opaque Envelopes — the SAME shape as
|
|
2567
|
+
* NotifyRequest.envelope. The native app decrypts it locally via openNotification;
|
|
2568
|
+
* web / an un-enrolled device can't and renders a locked placeholder. Absent = today's
|
|
2569
|
+
* plaintext item (context carries the cleartext), so plaintext items are unchanged. */
|
|
2570
|
+
envelope: z.object({
|
|
2571
|
+
context: z.lazy(() => EnvelopeSchema).optional(),
|
|
2572
|
+
options: z.lazy(() => EnvelopeSchema).optional(),
|
|
2573
|
+
visuals: z.lazy(() => EnvelopeSchema).optional()
|
|
2574
|
+
}).optional(),
|
|
2575
|
+
/** E2EE: the agent's device X25519 public key to seal the user's answer BACK to (the
|
|
2576
|
+
* sender the phone replies to). Sourced server-side from this item's pairing credential.
|
|
2577
|
+
* Present only alongside `envelope`; the phone seals via sealAnswer(answer, this, id). */
|
|
2578
|
+
agentX25519: z.string().optional()
|
|
2579
|
+
});
|
|
2580
|
+
var SnoozeRequestSchema = z.object({
|
|
2581
|
+
requestId: z.string(),
|
|
2582
|
+
until: z.string().datetime()
|
|
2583
|
+
});
|
|
2584
|
+
var PushTokenSchema = z.object({
|
|
2585
|
+
voipToken: z.string().min(1).optional(),
|
|
2586
|
+
alertToken: z.string().min(1).optional(),
|
|
2587
|
+
fcmToken: z.string().min(1).optional(),
|
|
2588
|
+
platform: z.enum(["ios", "android"])
|
|
2589
|
+
});
|
|
2590
|
+
var MissedCallSchema = z.enum([
|
|
2591
|
+
"retry_10m",
|
|
2592
|
+
"retry_30m",
|
|
2593
|
+
"retry_60m",
|
|
2594
|
+
"backoff_gentle",
|
|
2595
|
+
"backoff_standard",
|
|
2596
|
+
"backoff_aggressive",
|
|
2597
|
+
"inbox",
|
|
2598
|
+
"dismiss"
|
|
2599
|
+
]);
|
|
2600
|
+
var UserSettingsSchema = z.object({
|
|
2601
|
+
permissions: z.object({
|
|
2602
|
+
call: z.boolean(),
|
|
2603
|
+
banner: z.boolean(),
|
|
2604
|
+
push: z.boolean()
|
|
2605
|
+
}),
|
|
2606
|
+
sessionMode: z.enum(["default", "all_calls", "silent"]),
|
|
2607
|
+
silentPush: z.boolean(),
|
|
2608
|
+
autoCallback: z.boolean(),
|
|
2609
|
+
/** Opt-in (default false) to using your content to improve Paigy and train models. */
|
|
2610
|
+
improveConsent: z.boolean(),
|
|
2611
|
+
missedCall: MissedCallSchema.default("backoff_standard"),
|
|
2612
|
+
/** Where voice audio is processed. 'hosted' (default) = Paigy's voice services
|
|
2613
|
+
* (ElevenLabs TTS, faster-whisper STT, the call bot); 'on_device' = the phone
|
|
2614
|
+
* synthesizes and transcribes locally — no audio or spoken text leaves it.
|
|
2615
|
+
* Optional, NOT defaulted: a stale client PATCHing the full settings object
|
|
2616
|
+
* must not silently reset this privacy choice. Absent = leave unchanged on
|
|
2617
|
+
* write, 'hosted' on read (see store.ts). */
|
|
2618
|
+
voiceMode: z.enum(["hosted", "on_device"]).optional(),
|
|
2619
|
+
/** Account E2EE state (text lane): 'off' (default) = today's plaintext; 'on' =
|
|
2620
|
+
* content is sealed end-to-end between the local agent and the phone. Like
|
|
2621
|
+
* voiceMode, OPTIONAL and NOT defaulted so a stale client PATCHing the full
|
|
2622
|
+
* settings object without it can't silently flip the account's E2EE state.
|
|
2623
|
+
* Absent = leave unchanged on write, 'off' on read (see store.ts). The demo
|
|
2624
|
+
* account is plaintext by construction and refuses any non-'off' value. */
|
|
2625
|
+
e2eeMode: z.enum(["off", "on"]).optional()
|
|
2626
|
+
});
|
|
2627
|
+
var HistoryItemSchema = z.object({
|
|
2628
|
+
id: z.string(),
|
|
2629
|
+
threadId: z.string(),
|
|
2630
|
+
/** 'user' = a request you sent; 'agent' = a notification an agent sent you. */
|
|
2631
|
+
initiator: z.enum(["user", "agent"]),
|
|
2632
|
+
title: z.string(),
|
|
2633
|
+
/** The agent on the other end (nickname). */
|
|
2634
|
+
agent: z.string(),
|
|
2635
|
+
createdAt: z.string(),
|
|
2636
|
+
/** When the agent fetched your request (user→agent only). */
|
|
2637
|
+
agentAckedAt: z.string().nullable(),
|
|
2638
|
+
/** When you answered the agent's notification (agent→user only). */
|
|
2639
|
+
humanAckedAt: z.string().nullable()
|
|
2640
|
+
});
|
|
2641
|
+
var ConnectionSummarySchema = z.object({
|
|
2642
|
+
/** The connection = the agent's token id (used to address a request). */
|
|
2643
|
+
id: z.string(),
|
|
2644
|
+
agent: z.string(),
|
|
2645
|
+
device: z.string().nullable(),
|
|
2646
|
+
nickname: z.string(),
|
|
2647
|
+
createdAt: z.string().datetime(),
|
|
2648
|
+
/** Most recent notification on this connection, either direction. Null = no contact yet.
|
|
2649
|
+
* Drives the agents-page recency grouping (Today / This week / …). */
|
|
2650
|
+
lastContactAt: z.string().datetime().nullable(),
|
|
2651
|
+
/** True = a provider-managed agent running in the provider's cloud (e.g. Anthropic CMA);
|
|
2652
|
+
* false = a local MCP connection running on the user's computer (Claude Code/Codex/…). */
|
|
2653
|
+
managed: z.boolean()
|
|
2654
|
+
});
|
|
2655
|
+
var CreateRequestSchema = z.object({
|
|
2656
|
+
/** The connection (token id) to send to, from GET /api/tokens. */
|
|
2657
|
+
tokenId: z.string(),
|
|
2658
|
+
/** The user's message to the agent. */
|
|
2659
|
+
text: z.string().min(1)
|
|
2660
|
+
});
|
|
2661
|
+
var DeliveryModeSchema = z.enum(["poll", "self_hosted"]);
|
|
2662
|
+
var WAKE_EVENT = "wake";
|
|
2663
|
+
var wakeChannel = (tokenId) => `wake:${tokenId}`;
|
|
2664
|
+
var RegisterDeliverySchema = z.object({ mode: DeliveryModeSchema });
|
|
2665
|
+
var OAuthStartSchema = z.object({
|
|
2666
|
+
provider: z.enum(["cma"]),
|
|
2667
|
+
returnTo: z.string().min(1)
|
|
2668
|
+
});
|
|
2669
|
+
var DeliveryConfigSchema = z.object({
|
|
2670
|
+
tokenId: z.string(),
|
|
2671
|
+
mode: DeliveryModeSchema,
|
|
2672
|
+
/** null when the server has no SUPABASE_ANON_KEY set — the listener then falls
|
|
2673
|
+
* back to its own PAIGY_SUPABASE_URL / PAIGY_SUPABASE_ANON_KEY env. */
|
|
2674
|
+
realtime: z.object({ url: z.string(), anonKey: z.string() }).nullable()
|
|
2675
|
+
});
|
|
2676
|
+
var StatusSchema = z.object({
|
|
2677
|
+
agent: z.string(),
|
|
2678
|
+
nickname: z.string(),
|
|
2679
|
+
sessionMode: z.enum(["default", "all_calls", "silent"]),
|
|
2680
|
+
/** A phone is registered for push/ring (any push token on the account). */
|
|
2681
|
+
phone: z.boolean()
|
|
2682
|
+
});
|
|
2683
|
+
var EnvelopeRecipientSchema = z.object({
|
|
2684
|
+
keyId: z.string(),
|
|
2685
|
+
epk: z.string(),
|
|
2686
|
+
wnonce: z.string(),
|
|
2687
|
+
wrap: z.string()
|
|
2688
|
+
});
|
|
2689
|
+
var EnvelopeHeaderSchema = z.object({
|
|
2690
|
+
field: z.enum(["context", "options", "visuals", "answer"]),
|
|
2691
|
+
kind: z.string(),
|
|
2692
|
+
senderRole: z.enum(["agent", "user"]),
|
|
2693
|
+
recipientKeyIds: z.array(z.string()),
|
|
2694
|
+
seq: z.number().int().nonnegative()
|
|
2695
|
+
});
|
|
2696
|
+
var EnvelopeSchema = z.object({
|
|
2697
|
+
v: z.literal(1),
|
|
2698
|
+
alg: z.literal("x25519-xsalsa20poly1305"),
|
|
2699
|
+
msgId: z.string(),
|
|
2700
|
+
hdr: EnvelopeHeaderSchema,
|
|
2701
|
+
recipients: z.array(EnvelopeRecipientSchema).min(1),
|
|
2702
|
+
nonce: z.string(),
|
|
2703
|
+
ct: z.string()
|
|
2704
|
+
});
|
|
2705
|
+
var SealedAnswerSchema = z.object({
|
|
2706
|
+
ignored: z.boolean(),
|
|
2707
|
+
envelope: EnvelopeSchema
|
|
2708
|
+
});
|
|
2709
|
+
var DeviceCredentialSchema = z.object({
|
|
2710
|
+
deviceId: z.string(),
|
|
2711
|
+
kind: z.enum(["phone", "web", "agent"]),
|
|
2712
|
+
x25519Pub: z.string(),
|
|
2713
|
+
ed25519Pub: z.string(),
|
|
2714
|
+
sig: z.string()
|
|
2715
|
+
});
|
|
2716
|
+
var DeviceRosterSchema = z.object({
|
|
2717
|
+
uikPub: z.string(),
|
|
2718
|
+
devices: z.array(DeviceCredentialSchema)
|
|
2719
|
+
});
|
|
2720
|
+
var WakeNudgeSchema = z.object({
|
|
2721
|
+
kind: z.enum(["reply", "request", "callback"]),
|
|
2722
|
+
notificationId: z.string().optional(),
|
|
2723
|
+
threadId: z.string()
|
|
2724
|
+
});
|
|
2725
|
+
var DeviceAgentTokenSchema = z.object({
|
|
2726
|
+
token: z.string(),
|
|
2727
|
+
deviceId: z.string(),
|
|
2728
|
+
agentName: z.string(),
|
|
2729
|
+
/** User-chosen session label; defaults to `<agent> <device>`. */
|
|
2730
|
+
nickname: z.string(),
|
|
2731
|
+
createdAt: z.string().datetime()
|
|
2732
|
+
});
|
|
2733
|
+
var PairingStatusSchema = z.enum(["pending", "approved", "denied", "expired"]);
|
|
2734
|
+
var PairingRevealSchema = z.object({
|
|
2735
|
+
x25519: z.string(),
|
|
2736
|
+
ed25519: z.string(),
|
|
2737
|
+
nonce: z.string()
|
|
2738
|
+
});
|
|
2739
|
+
var DeviceCodeRequestSchema = z.object({
|
|
2740
|
+
agent: z.union([z.string(), z.object({ name: z.string().optional() })]).optional(),
|
|
2741
|
+
device: z.string().optional(),
|
|
2742
|
+
proto: z.string().optional(),
|
|
2743
|
+
// compiled-in tag, e.g. "paigy-pair-v2|sas=24"
|
|
2744
|
+
commitment: z.string().optional()
|
|
2745
|
+
// agent's Ca
|
|
2746
|
+
});
|
|
2747
|
+
var DeviceCodeSchema = z.object({
|
|
2748
|
+
device_code: z.string(),
|
|
2749
|
+
user_code: z.string(),
|
|
2750
|
+
verification_uri: z.string().url(),
|
|
2751
|
+
verification_uri_complete: z.string().url(),
|
|
2752
|
+
interval: z.number(),
|
|
2753
|
+
expires_in: z.number()
|
|
2754
|
+
});
|
|
2755
|
+
var DeviceInfoSchema = z.object({
|
|
2756
|
+
code: z.string(),
|
|
2757
|
+
agent: z.string(),
|
|
2758
|
+
device: z.string().nullable(),
|
|
2759
|
+
status: PairingStatusSchema,
|
|
2760
|
+
proto: z.string().nullable().optional(),
|
|
2761
|
+
agent_commitment: z.string().nullable().optional(),
|
|
2762
|
+
// Ca
|
|
2763
|
+
agent_reveal: PairingRevealSchema.nullable().optional(),
|
|
2764
|
+
// present once the agent reveals
|
|
2765
|
+
agent_credential: DeviceCredentialSchema.nullable().optional(),
|
|
2766
|
+
// the phone's UIK-signed cred over the agent's keys — the agent fetches it here to finalizeE2ee
|
|
2767
|
+
// The phone's reveal + identity key, exposed here so a GATED agent (e2ee_mode='on', token
|
|
2768
|
+
// withheld until e2ee=true) can compute the SAS WITHOUT a token. Public keys — same safety
|
|
2769
|
+
// class as agent_reveal above. Present only once the phone reveals; null otherwise.
|
|
2770
|
+
phone_reveal: PairingRevealSchema.nullable().optional(),
|
|
2771
|
+
uik_pub: z.string().nullable().optional()
|
|
2772
|
+
});
|
|
2773
|
+
var DeviceTokenRequestSchema = z.object({
|
|
2774
|
+
device_code: z.string(),
|
|
2775
|
+
reveal: PairingRevealSchema.optional()
|
|
2776
|
+
// agent's reveal {x25519, ed25519, nonce}
|
|
2777
|
+
});
|
|
2778
|
+
var DeviceTokenSchema = z.object({
|
|
2779
|
+
access_token: z.string(),
|
|
2780
|
+
nickname: z.string(),
|
|
2781
|
+
agent: z.string(),
|
|
2782
|
+
device: z.string().nullable(),
|
|
2783
|
+
phone_reveal: PairingRevealSchema.nullable().optional(),
|
|
2784
|
+
// present once the phone reveals
|
|
2785
|
+
uik_pub: z.string().nullable().optional()
|
|
2786
|
+
});
|
|
2787
|
+
var DeviceCommitRequestSchema = z.object({
|
|
2788
|
+
code: z.string(),
|
|
2789
|
+
commitment: z.string()
|
|
2790
|
+
// Cb
|
|
2791
|
+
});
|
|
2792
|
+
var DevicePeerCommitSchema = z.object({
|
|
2793
|
+
commitment: z.string().nullable()
|
|
2794
|
+
});
|
|
2795
|
+
var SupportRequestSchema = z.object({
|
|
2796
|
+
email: z.string().email().max(320),
|
|
2797
|
+
message: z.string().trim().min(1).max(5e3),
|
|
2798
|
+
name: z.string().trim().max(120).optional()
|
|
2799
|
+
});
|
|
2800
|
+
|
|
2801
|
+
// src/http.ts
|
|
2802
|
+
var BACKEND_URL = process.env.PAIGY_BACKEND_URL ?? "https://paigy.ai";
|
|
2803
|
+
var NETWORK_MSG = `Can't reach ${BACKEND_URL} \u2014 the connection was blocked or dropped before an HTTP response. If this agent runs in a sandboxed environment with a network allowlist (e.g. Claude Code on the web, CI), ask the user to add the Paigy domain (paigy.ai) to the environment's allowed domains, then retry.`;
|
|
2804
|
+
async function reach(url, init) {
|
|
2805
|
+
try {
|
|
2806
|
+
return await fetch(url, init);
|
|
2807
|
+
} catch (e) {
|
|
2808
|
+
throw new Error(`${NETWORK_MSG} (${e?.message ?? String(e)})`);
|
|
2809
|
+
}
|
|
2810
|
+
}
|
|
2811
|
+
|
|
2812
|
+
// src/device.ts
|
|
2813
|
+
import { execFile } from "child_process";
|
|
2814
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";
|
|
2815
|
+
import { homedir, platform } from "os";
|
|
2816
|
+
import { join } from "path";
|
|
2817
|
+
|
|
2818
|
+
// ../../packages/crypto/dist/index.js
|
|
2819
|
+
var import_tweetnacl = __toESM(require_nacl_fast(), 1);
|
|
2820
|
+
var import_tweetnacl2 = __toESM(require_nacl_fast(), 1);
|
|
2821
|
+
var import_tweetnacl3 = __toESM(require_nacl_fast(), 1);
|
|
2822
|
+
var import_tweetnacl4 = __toESM(require_nacl_fast(), 1);
|
|
2823
|
+
var import_tweetnacl5 = __toESM(require_nacl_fast(), 1);
|
|
2824
|
+
function utf8ToBytes(s) {
|
|
2825
|
+
const out = [];
|
|
2826
|
+
for (let i = 0; i < s.length; i++) {
|
|
2827
|
+
const c = s.charCodeAt(i);
|
|
2828
|
+
if (c < 128) out.push(c);
|
|
2829
|
+
else if (c < 2048) out.push(192 | c >> 6, 128 | c & 63);
|
|
2830
|
+
else if (c >= 55296 && c <= 56319) {
|
|
2831
|
+
const c2 = s.charCodeAt(i + 1);
|
|
2832
|
+
if (c2 >= 56320 && c2 <= 57343) {
|
|
2833
|
+
i++;
|
|
2834
|
+
const cp = 65536 + ((c & 1023) << 10) + (c2 & 1023);
|
|
2835
|
+
out.push(240 | cp >> 18, 128 | cp >> 12 & 63, 128 | cp >> 6 & 63, 128 | cp & 63);
|
|
2836
|
+
} else out.push(239, 191, 189);
|
|
2837
|
+
} else if (c >= 56320 && c <= 57343) out.push(239, 191, 189);
|
|
2838
|
+
else out.push(224 | c >> 12, 128 | c >> 6 & 63, 128 | c & 63);
|
|
2839
|
+
}
|
|
2840
|
+
return Uint8Array.from(out);
|
|
2841
|
+
}
|
|
2842
|
+
function bytesToUtf8(b) {
|
|
2843
|
+
let out = "";
|
|
2844
|
+
for (let i = 0; i < b.length; ) {
|
|
2845
|
+
const c = b[i++];
|
|
2846
|
+
if (c < 128) out += String.fromCharCode(c);
|
|
2847
|
+
else if (c < 224) out += String.fromCharCode((c & 31) << 6 | b[i++] & 63);
|
|
2848
|
+
else if (c < 240) out += String.fromCharCode((c & 15) << 12 | (b[i++] & 63) << 6 | b[i++] & 63);
|
|
2849
|
+
else {
|
|
2850
|
+
const cp = (c & 7) << 18 | (b[i++] & 63) << 12 | (b[i++] & 63) << 6 | b[i++] & 63;
|
|
2851
|
+
const u = cp - 65536;
|
|
2852
|
+
out += String.fromCharCode(55296 + (u >> 10), 56320 + (u & 1023));
|
|
2853
|
+
}
|
|
2854
|
+
}
|
|
2855
|
+
return out;
|
|
2856
|
+
}
|
|
2857
|
+
var B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
2858
|
+
function toB64(bytes) {
|
|
2859
|
+
let out = "";
|
|
2860
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
2861
|
+
const b0 = bytes[i];
|
|
2862
|
+
const has1 = i + 1 < bytes.length;
|
|
2863
|
+
const has2 = i + 2 < bytes.length;
|
|
2864
|
+
const b1 = has1 ? bytes[i + 1] : 0;
|
|
2865
|
+
const b2 = has2 ? bytes[i + 2] : 0;
|
|
2866
|
+
out += B64[b0 >> 2] + B64[(b0 & 3) << 4 | b1 >> 4];
|
|
2867
|
+
out += has1 ? B64[(b1 & 15) << 2 | b2 >> 6] : "=";
|
|
2868
|
+
out += has2 ? B64[b2 & 63] : "=";
|
|
2869
|
+
}
|
|
2870
|
+
return out;
|
|
2871
|
+
}
|
|
2872
|
+
function fromB64Lenient(s) {
|
|
2873
|
+
const clean = s.replace(/[^A-Za-z0-9+/]/g, "");
|
|
2874
|
+
const out = [];
|
|
2875
|
+
for (let i = 0; i < clean.length; i += 4) {
|
|
2876
|
+
const c0 = B64.indexOf(clean[i]);
|
|
2877
|
+
const c1 = B64.indexOf(clean[i + 1] ?? "A");
|
|
2878
|
+
const c2 = clean[i + 2] !== void 0 ? B64.indexOf(clean[i + 2]) : -1;
|
|
2879
|
+
const c3 = clean[i + 3] !== void 0 ? B64.indexOf(clean[i + 3]) : -1;
|
|
2880
|
+
out.push(c0 << 2 | c1 >> 4);
|
|
2881
|
+
if (c2 >= 0) out.push((c1 & 15) << 4 | c2 >> 2);
|
|
2882
|
+
if (c3 >= 0) out.push((c2 & 3) << 6 | c3);
|
|
2883
|
+
}
|
|
2884
|
+
return Uint8Array.from(out);
|
|
2885
|
+
}
|
|
2886
|
+
function fromB64(s) {
|
|
2887
|
+
const bytes = fromB64Lenient(s);
|
|
2888
|
+
if (toB64(bytes) !== s) throw new Error("non-canonical base64");
|
|
2889
|
+
return bytes;
|
|
2890
|
+
}
|
|
2891
|
+
function concat(parts) {
|
|
2892
|
+
let n = 0;
|
|
2893
|
+
for (const p of parts) n += p.length;
|
|
2894
|
+
const out = new Uint8Array(n);
|
|
2895
|
+
let o = 0;
|
|
2896
|
+
for (const p of parts) {
|
|
2897
|
+
out.set(p, o);
|
|
2898
|
+
o += p.length;
|
|
2899
|
+
}
|
|
2900
|
+
return out;
|
|
2901
|
+
}
|
|
2902
|
+
function u16be(n) {
|
|
2903
|
+
return Uint8Array.of(n >>> 8 & 255, n & 255);
|
|
2904
|
+
}
|
|
2905
|
+
function u32be(n) {
|
|
2906
|
+
return Uint8Array.of(n >>> 24 & 255, n >>> 16 & 255, n >>> 8 & 255, n & 255);
|
|
2907
|
+
}
|
|
2908
|
+
function readU32be(b, o) {
|
|
2909
|
+
return (b[o] << 24 | b[o + 1] << 16 | b[o + 2] << 8 | b[o + 3]) >>> 0;
|
|
2910
|
+
}
|
|
2911
|
+
function lp(b) {
|
|
2912
|
+
return concat([u32be(b.length), b]);
|
|
2913
|
+
}
|
|
2914
|
+
function eqCt(a, b) {
|
|
2915
|
+
if (a.length !== b.length) return false;
|
|
2916
|
+
let d = 0;
|
|
2917
|
+
for (let i = 0; i < a.length; i++) d |= a[i] ^ b[i];
|
|
2918
|
+
return d === 0;
|
|
2919
|
+
}
|
|
2920
|
+
var ok = false;
|
|
2921
|
+
function ensureCsprng() {
|
|
2922
|
+
if (ok) return;
|
|
2923
|
+
let a;
|
|
2924
|
+
let b;
|
|
2925
|
+
try {
|
|
2926
|
+
a = import_tweetnacl.default.randomBytes(32);
|
|
2927
|
+
b = import_tweetnacl.default.randomBytes(32);
|
|
2928
|
+
} catch {
|
|
2929
|
+
throw new Error("paigy-crypto: no secure RNG available \u2014 refusing to generate keys (fail-closed)");
|
|
2930
|
+
}
|
|
2931
|
+
if (a.every((x) => x === 0) || eqCt(a, b)) {
|
|
2932
|
+
throw new Error("paigy-crypto: RNG self-test failed (constant/low-entropy output)");
|
|
2933
|
+
}
|
|
2934
|
+
ok = true;
|
|
2935
|
+
}
|
|
2936
|
+
function randomBytes(n) {
|
|
2937
|
+
ensureCsprng();
|
|
2938
|
+
return import_tweetnacl.default.randomBytes(n);
|
|
2939
|
+
}
|
|
2940
|
+
function kp(k) {
|
|
2941
|
+
return { publicKey: toB64(k.publicKey), secretKey: toB64(k.secretKey) };
|
|
2942
|
+
}
|
|
2943
|
+
function genDeviceKeypair() {
|
|
2944
|
+
ensureCsprng();
|
|
2945
|
+
return { x25519: kp(import_tweetnacl2.default.box.keyPair()), ed25519: kp(import_tweetnacl2.default.sign.keyPair()) };
|
|
2946
|
+
}
|
|
2947
|
+
function sharedSecret(peerX25519PubB64, mySecretB64) {
|
|
2948
|
+
const raw = import_tweetnacl2.default.scalarMult(fromB64(mySecretB64), fromB64(peerX25519PubB64));
|
|
2949
|
+
if (raw.every((b) => b === 0)) throw new Error("low-order X25519 point \u2014 shared secret rejected");
|
|
2950
|
+
return toB64(raw);
|
|
2951
|
+
}
|
|
2952
|
+
function keyId(publicKeyB64) {
|
|
2953
|
+
return toB64(import_tweetnacl2.default.hash(fromB64(publicKeyB64)).slice(0, 8));
|
|
2954
|
+
}
|
|
2955
|
+
function credentialBody(c) {
|
|
2956
|
+
return concat([
|
|
2957
|
+
lp(utf8ToBytes("paigy-device-credential-v1")),
|
|
2958
|
+
lp(utf8ToBytes(c.deviceId)),
|
|
2959
|
+
lp(utf8ToBytes(c.kind)),
|
|
2960
|
+
lp(fromB64(c.x25519Pub)),
|
|
2961
|
+
lp(fromB64(c.ed25519Pub))
|
|
2962
|
+
]);
|
|
2963
|
+
}
|
|
2964
|
+
function verifyDeviceCredential(cred, uikPublicB64) {
|
|
2965
|
+
const { sig, ...fields } = cred;
|
|
2966
|
+
try {
|
|
2967
|
+
return import_tweetnacl2.default.sign.detached.verify(credentialBody(fields), fromB64(sig), fromB64(uikPublicB64));
|
|
2968
|
+
} catch {
|
|
2969
|
+
return false;
|
|
2970
|
+
}
|
|
2971
|
+
}
|
|
2972
|
+
var ENVELOPE_VERSION = 1;
|
|
2973
|
+
var ENVELOPE_ALG = "x25519-xsalsa20poly1305";
|
|
2974
|
+
function padme(L) {
|
|
2975
|
+
if (L < 2) return L;
|
|
2976
|
+
const E = Math.floor(Math.log2(L));
|
|
2977
|
+
const S = Math.floor(Math.log2(E)) + 1;
|
|
2978
|
+
const lastBits = E - S;
|
|
2979
|
+
if (lastBits <= 0) return L;
|
|
2980
|
+
const mask = (1 << lastBits) - 1;
|
|
2981
|
+
return L + mask & ~mask;
|
|
2982
|
+
}
|
|
2983
|
+
function canonicalHeader(msgId, hdr) {
|
|
2984
|
+
const parts = [
|
|
2985
|
+
u16be(ENVELOPE_VERSION),
|
|
2986
|
+
lp(utf8ToBytes(ENVELOPE_ALG)),
|
|
2987
|
+
lp(msgId),
|
|
2988
|
+
lp(utf8ToBytes(hdr.field)),
|
|
2989
|
+
lp(utf8ToBytes(hdr.kind)),
|
|
2990
|
+
lp(utf8ToBytes(hdr.senderRole)),
|
|
2991
|
+
u32be(hdr.seq),
|
|
2992
|
+
u32be(hdr.recipientKeyIds.length),
|
|
2993
|
+
...hdr.recipientKeyIds.map((k) => lp(utf8ToBytes(k)))
|
|
2994
|
+
];
|
|
2995
|
+
return concat(parts);
|
|
2996
|
+
}
|
|
2997
|
+
function seal(input) {
|
|
2998
|
+
ensureCsprng();
|
|
2999
|
+
if (input.recipients.length === 0) throw new Error("seal: no recipients");
|
|
3000
|
+
const kind = input.kind ?? "";
|
|
3001
|
+
const recs = input.recipients.map((pub) => ({ pub, id: keyId(pub) })).sort((a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
3002
|
+
const hdr = {
|
|
3003
|
+
field: input.field,
|
|
3004
|
+
kind,
|
|
3005
|
+
senderRole: input.senderRole,
|
|
3006
|
+
recipientKeyIds: recs.map((r) => r.id),
|
|
3007
|
+
seq: input.seq
|
|
3008
|
+
};
|
|
3009
|
+
const msgIdBytes = input.msgId ? fromB64(input.msgId) : randomBytes(16);
|
|
3010
|
+
const cek = randomBytes(32);
|
|
3011
|
+
const canon = canonicalHeader(msgIdBytes, hdr);
|
|
3012
|
+
const plaintext = concat([u32be(canon.length), canon, u32be(input.body.length), input.body]);
|
|
3013
|
+
const padded = new Uint8Array(padme(plaintext.length));
|
|
3014
|
+
padded.set(plaintext);
|
|
3015
|
+
const nonce = randomBytes(24);
|
|
3016
|
+
const ct = import_tweetnacl3.default.secretbox(padded, nonce, cek);
|
|
3017
|
+
const recipients = recs.map((r) => {
|
|
3018
|
+
const eph = import_tweetnacl3.default.box.keyPair();
|
|
3019
|
+
const wnonce = randomBytes(24);
|
|
3020
|
+
const wrap = import_tweetnacl3.default.box(cek, wnonce, fromB64(r.pub), eph.secretKey);
|
|
3021
|
+
return { keyId: r.id, epk: toB64(eph.publicKey), wnonce: toB64(wnonce), wrap: toB64(wrap) };
|
|
3022
|
+
});
|
|
3023
|
+
return {
|
|
3024
|
+
v: ENVELOPE_VERSION,
|
|
3025
|
+
alg: ENVELOPE_ALG,
|
|
3026
|
+
msgId: toB64(msgIdBytes),
|
|
3027
|
+
hdr,
|
|
3028
|
+
recipients,
|
|
3029
|
+
nonce: toB64(nonce),
|
|
3030
|
+
ct: toB64(ct)
|
|
3031
|
+
};
|
|
3032
|
+
}
|
|
3033
|
+
function open(envelope, myKeyId, mySecretKeyB64) {
|
|
3034
|
+
if (envelope.v !== ENVELOPE_VERSION) throw new Error(`unsupported envelope version ${String(envelope.v)}`);
|
|
3035
|
+
if (envelope.alg !== ENVELOPE_ALG) throw new Error(`unsupported alg ${String(envelope.alg)}`);
|
|
3036
|
+
const rec = envelope.recipients.find((r) => r.keyId === myKeyId);
|
|
3037
|
+
if (!rec) throw new Error("not a recipient of this envelope");
|
|
3038
|
+
const cek = import_tweetnacl3.default.box.open(fromB64(rec.wrap), fromB64(rec.wnonce), fromB64(rec.epk), fromB64(mySecretKeyB64));
|
|
3039
|
+
if (!cek) throw new Error("CEK unwrap failed (wrong key or tampered)");
|
|
3040
|
+
const padded = import_tweetnacl3.default.secretbox.open(fromB64(envelope.ct), fromB64(envelope.nonce), cek);
|
|
3041
|
+
if (!padded) throw new Error("body authentication failed (tampered ciphertext)");
|
|
3042
|
+
if (padded.length < 8) throw new Error("malformed sealed plaintext");
|
|
3043
|
+
const canonLen = readU32be(padded, 0);
|
|
3044
|
+
if (canonLen < 0 || 8 + canonLen > padded.length) throw new Error("malformed sealed plaintext");
|
|
3045
|
+
const sealedCanon = padded.slice(4, 4 + canonLen);
|
|
3046
|
+
const bodyLen = readU32be(padded, 4 + canonLen);
|
|
3047
|
+
const bodyStart = 8 + canonLen;
|
|
3048
|
+
if (bodyStart + bodyLen > padded.length) throw new Error("malformed sealed plaintext");
|
|
3049
|
+
const body = padded.slice(bodyStart, bodyStart + bodyLen);
|
|
3050
|
+
const expected = canonicalHeader(fromB64(envelope.msgId), envelope.hdr);
|
|
3051
|
+
if (!eqCt(sealedCanon, expected)) throw new Error("header mismatch (tampered metadata)");
|
|
3052
|
+
return { header: envelope.hdr, body };
|
|
3053
|
+
}
|
|
3054
|
+
var SAS_DOMAIN = "paigy-sas-v2";
|
|
3055
|
+
function deriveSAS(input, bits = 24) {
|
|
3056
|
+
const bundle = concat([
|
|
3057
|
+
lp(utf8ToBytes(SAS_DOMAIN)),
|
|
3058
|
+
lp(utf8ToBytes(input.proto)),
|
|
3059
|
+
lp(fromB64(input.ss)),
|
|
3060
|
+
lp(fromB64(input.ca)),
|
|
3061
|
+
lp(fromB64(input.cb))
|
|
3062
|
+
]);
|
|
3063
|
+
const d = import_tweetnacl4.default.hash(bundle);
|
|
3064
|
+
const nbytes = Math.ceil(bits / 8);
|
|
3065
|
+
let n = 0n;
|
|
3066
|
+
for (let i = 0; i < nbytes; i++) n = n << 8n | BigInt(d[i]);
|
|
3067
|
+
n &= (1n << BigInt(bits)) - 1n;
|
|
3068
|
+
const digits = (2n ** BigInt(bits) - 1n).toString().length;
|
|
3069
|
+
return n.toString().padStart(digits, "0").replace(/(\d{3})(?=\d)/g, "$1 ");
|
|
3070
|
+
}
|
|
3071
|
+
var COMMIT_DOMAIN = "paigy-commit-v1";
|
|
3072
|
+
function commit(valuesB64) {
|
|
3073
|
+
return toB64(import_tweetnacl5.default.hash(concat([lp(utf8ToBytes(COMMIT_DOMAIN)), ...valuesB64.map((v) => lp(fromB64(v)))])).slice(0, 32));
|
|
3074
|
+
}
|
|
3075
|
+
function commitPairing(proto, role, x25519Pub, ed25519Pub, nonce, uikPub) {
|
|
3076
|
+
const vals = [toB64(utf8ToBytes(proto)), toB64(utf8ToBytes(role)), x25519Pub, ed25519Pub, nonce];
|
|
3077
|
+
if (uikPub !== void 0) vals.push(uikPub);
|
|
3078
|
+
return commit(vals);
|
|
3079
|
+
}
|
|
3080
|
+
function verifyPairingCommit(commitment, proto, role, x25519Pub, ed25519Pub, nonce, uikPub) {
|
|
3081
|
+
try {
|
|
3082
|
+
return eqCt(fromB64(commitment), fromB64(commitPairing(proto, role, x25519Pub, ed25519Pub, nonce, uikPub)));
|
|
3083
|
+
} catch {
|
|
3084
|
+
return false;
|
|
3085
|
+
}
|
|
3086
|
+
}
|
|
3087
|
+
|
|
3088
|
+
// src/device.ts
|
|
3089
|
+
var AGENT_NAME = process.env.PAIGY_AGENT ?? "mcp-agent";
|
|
3090
|
+
var PROTO = "paigy-pair-v2|sas=24";
|
|
3091
|
+
var SAS_BITS = 24;
|
|
3092
|
+
var TOKEN_PATH = join(homedir(), ".paigy", "token.json");
|
|
3093
|
+
var KEY_PATH = join(homedir(), ".paigy", "key.json");
|
|
3094
|
+
function openBrowser(url) {
|
|
3095
|
+
const cmd = platform() === "darwin" ? "open" : platform() === "win32" ? "cmd" : "xdg-open";
|
|
3096
|
+
const args = platform() === "win32" ? ["/c", "start", url] : [url];
|
|
3097
|
+
execFile(cmd, args, () => {
|
|
3098
|
+
});
|
|
3099
|
+
}
|
|
3100
|
+
function saveToken(token) {
|
|
3101
|
+
mkdirSync(join(homedir(), ".paigy"), { recursive: true });
|
|
3102
|
+
writeFileSync(TOKEN_PATH, JSON.stringify(token, null, 2) + "\n", { mode: 384 });
|
|
3103
|
+
}
|
|
3104
|
+
var sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
3105
|
+
function readToken() {
|
|
3106
|
+
if (process.env.PAIGY_TOKEN) return process.env.PAIGY_TOKEN;
|
|
3107
|
+
if (existsSync(TOKEN_PATH)) {
|
|
3108
|
+
try {
|
|
3109
|
+
return JSON.parse(readFileSync(TOKEN_PATH, "utf8")).access_token ?? "";
|
|
3110
|
+
} catch {
|
|
3111
|
+
}
|
|
3112
|
+
}
|
|
3113
|
+
return "";
|
|
3114
|
+
}
|
|
3115
|
+
function deleteToken() {
|
|
3116
|
+
if (!existsSync(TOKEN_PATH)) return false;
|
|
3117
|
+
rmSync(TOKEN_PATH);
|
|
3118
|
+
return true;
|
|
3119
|
+
}
|
|
3120
|
+
async function revokeToken(token) {
|
|
3121
|
+
const res = await reach(`${BACKEND_URL}/api/device/revoke`, {
|
|
3122
|
+
method: "POST",
|
|
3123
|
+
headers: { authorization: `Bearer ${token}` }
|
|
3124
|
+
});
|
|
3125
|
+
return res.ok;
|
|
3126
|
+
}
|
|
3127
|
+
async function requestCode(agent = AGENT_NAME, e2ee) {
|
|
3128
|
+
const res = await reach(`${BACKEND_URL}/api/device/code`, {
|
|
3129
|
+
method: "POST",
|
|
3130
|
+
headers: { "content-type": "application/json" },
|
|
3131
|
+
body: JSON.stringify(e2ee ? { agent, proto: e2ee.proto, commitment: e2ee.commitment } : { agent })
|
|
3132
|
+
});
|
|
3133
|
+
if (!res.ok) throw new Error(`/api/device/code failed: ${res.status} ${await res.text()}`);
|
|
3134
|
+
return await res.json();
|
|
3135
|
+
}
|
|
3136
|
+
async function pollToken(deviceCode, reveal) {
|
|
3137
|
+
const res = await reach(`${BACKEND_URL}/api/device/token`, {
|
|
3138
|
+
method: "POST",
|
|
3139
|
+
headers: { "content-type": "application/json" },
|
|
3140
|
+
body: JSON.stringify(reveal ? { device_code: deviceCode, reveal } : { device_code: deviceCode })
|
|
3141
|
+
});
|
|
3142
|
+
if (res.ok) return await res.json();
|
|
3143
|
+
const body = await res.json().catch(() => ({ error: "unknown" }));
|
|
3144
|
+
const err = body.error ?? "unknown";
|
|
3145
|
+
if (err === "authorization_pending") return null;
|
|
3146
|
+
if (err === "commit_required") return null;
|
|
3147
|
+
if (err === "rate_limited") throw new Error("Too many pairing attempts. Wait a moment and retry.");
|
|
3148
|
+
if (err === "expired_token") throw new Error("Device code expired. Start pairing again.");
|
|
3149
|
+
if (err === "access_denied") throw new Error("Access denied by user. Pairing cancelled.");
|
|
3150
|
+
throw new Error(`Unexpected error from /api/device/token: ${err}`);
|
|
3151
|
+
}
|
|
3152
|
+
function saveKeyFile(kf) {
|
|
3153
|
+
mkdirSync(join(homedir(), ".paigy"), { recursive: true });
|
|
3154
|
+
writeFileSync(KEY_PATH, JSON.stringify(kf, null, 2) + "\n", { mode: 384 });
|
|
3155
|
+
}
|
|
3156
|
+
function readKeyFile() {
|
|
3157
|
+
if (!existsSync(KEY_PATH)) return null;
|
|
3158
|
+
try {
|
|
3159
|
+
return JSON.parse(readFileSync(KEY_PATH, "utf8"));
|
|
3160
|
+
} catch {
|
|
3161
|
+
return null;
|
|
3162
|
+
}
|
|
3163
|
+
}
|
|
3164
|
+
function deleteKeyFile() {
|
|
3165
|
+
if (!existsSync(KEY_PATH)) return false;
|
|
3166
|
+
rmSync(KEY_PATH);
|
|
3167
|
+
return true;
|
|
3168
|
+
}
|
|
3169
|
+
function startE2ee() {
|
|
3170
|
+
const keypair = genDeviceKeypair();
|
|
3171
|
+
const nonce = toB64(randomBytes(32));
|
|
3172
|
+
const ca = commitPairing(PROTO, "A", keypair.x25519.publicKey, keypair.ed25519.publicKey, nonce);
|
|
3173
|
+
const keyFile = { proto: PROTO, keypair, nonce, ca, e2ee: false };
|
|
3174
|
+
saveKeyFile(keyFile);
|
|
3175
|
+
return { keyFile, offer: { proto: PROTO, commitment: ca } };
|
|
3176
|
+
}
|
|
3177
|
+
async function fetchPeerCommit(deviceCode) {
|
|
3178
|
+
const res = await reach(`${BACKEND_URL}/api/device/peer-commit?device_code=${encodeURIComponent(deviceCode)}`);
|
|
3179
|
+
if (!res.ok) return null;
|
|
3180
|
+
const body = await res.json().catch(() => ({}));
|
|
3181
|
+
return body.commitment ?? null;
|
|
3182
|
+
}
|
|
3183
|
+
function agentReveal(kf) {
|
|
3184
|
+
return { x25519: kf.keypair.x25519.publicKey, ed25519: kf.keypair.ed25519.publicKey, nonce: kf.nonce };
|
|
3185
|
+
}
|
|
3186
|
+
function computeSAS(kf, cb, phoneReveal, uikPub) {
|
|
3187
|
+
const opens = verifyPairingCommit(cb, PROTO, "P", phoneReveal.x25519, phoneReveal.ed25519, phoneReveal.nonce, uikPub);
|
|
3188
|
+
if (!opens) throw new Error("phone reveal does not open its commitment \u2014 aborting E2EE");
|
|
3189
|
+
const ss = sharedSecret(phoneReveal.x25519, kf.keypair.x25519.secretKey);
|
|
3190
|
+
return deriveSAS({ proto: PROTO, ss, ca: kf.ca, cb }, SAS_BITS);
|
|
3191
|
+
}
|
|
3192
|
+
function finalizeE2ee(kf, credential, uikPub) {
|
|
3193
|
+
if (!verifyDeviceCredential(credential, uikPub)) return false;
|
|
3194
|
+
if (credential.x25519Pub !== kf.keypair.x25519.publicKey || credential.ed25519Pub !== kf.keypair.ed25519.publicKey) {
|
|
3195
|
+
return false;
|
|
3196
|
+
}
|
|
3197
|
+
saveKeyFile({ ...kf, e2ee: true, uikPub });
|
|
3198
|
+
return true;
|
|
3199
|
+
}
|
|
3200
|
+
async function fetchCredential(userCode) {
|
|
3201
|
+
const res = await reach(`${BACKEND_URL}/api/device/info?code=${encodeURIComponent(userCode)}`);
|
|
3202
|
+
if (!res.ok) return null;
|
|
3203
|
+
const body = await res.json().catch(() => ({}));
|
|
3204
|
+
return body.agent_credential ?? null;
|
|
3205
|
+
}
|
|
3206
|
+
async function fetchPhoneReveal(userCode) {
|
|
3207
|
+
const res = await reach(`${BACKEND_URL}/api/device/info?code=${encodeURIComponent(userCode)}`);
|
|
3208
|
+
if (!res.ok) return null;
|
|
3209
|
+
const body = await res.json().catch(() => ({}));
|
|
3210
|
+
if (!body.phone_reveal || !body.uik_pub) return null;
|
|
3211
|
+
return { phone_reveal: body.phone_reveal, uik_pub: body.uik_pub };
|
|
3212
|
+
}
|
|
3213
|
+
async function pairStep(deviceCode, keyFile) {
|
|
3214
|
+
if (!keyFile) {
|
|
3215
|
+
const token2 = await pollToken(deviceCode);
|
|
3216
|
+
return token2 ? { kind: "paired", token: token2 } : { kind: "pending" };
|
|
3217
|
+
}
|
|
3218
|
+
const cb = await fetchPeerCommit(deviceCode);
|
|
3219
|
+
if (!cb) {
|
|
3220
|
+
const token2 = await pollToken(deviceCode);
|
|
3221
|
+
return token2 ? { kind: "paired", token: token2 } : { kind: "pending" };
|
|
3222
|
+
}
|
|
3223
|
+
const token = await pollToken(deviceCode, agentReveal(keyFile));
|
|
3224
|
+
if (!token) {
|
|
3225
|
+
const gated = keyFile.userCode ? await fetchPhoneReveal(keyFile.userCode) : null;
|
|
3226
|
+
if (!gated) return { kind: "pending" };
|
|
3227
|
+
try {
|
|
3228
|
+
const sas = computeSAS(keyFile, cb, gated.phone_reveal, gated.uik_pub);
|
|
3229
|
+
return { kind: "awaiting_confirm", sas, uikPub: gated.uik_pub };
|
|
3230
|
+
} catch (e) {
|
|
3231
|
+
return { kind: "e2ee_aborted", warning: e.message };
|
|
3232
|
+
}
|
|
3233
|
+
}
|
|
3234
|
+
if (!token.phone_reveal || !token.uik_pub) return { kind: "paired", token };
|
|
3235
|
+
try {
|
|
3236
|
+
const sas = computeSAS(keyFile, cb, token.phone_reveal, token.uik_pub);
|
|
3237
|
+
return { kind: "paired", token, sas, uikPub: token.uik_pub };
|
|
3238
|
+
} catch (e) {
|
|
3239
|
+
return { kind: "e2ee_aborted", warning: e.message };
|
|
3240
|
+
}
|
|
3241
|
+
}
|
|
3242
|
+
async function fetchRoster(token) {
|
|
3243
|
+
const res = await reach(`${BACKEND_URL}/api/roster`, { headers: { authorization: `Bearer ${token}` } });
|
|
3244
|
+
if (!res.ok) return null;
|
|
3245
|
+
return await res.json().catch(() => null);
|
|
3246
|
+
}
|
|
3247
|
+
function verifiedRecipients(roster, pinnedUikPub) {
|
|
3248
|
+
return roster.devices.filter((cred) => verifyDeviceCredential(cred, pinnedUikPub)).map((cred) => cred.x25519Pub);
|
|
3249
|
+
}
|
|
3250
|
+
function sealFields(content, recipients) {
|
|
3251
|
+
if (recipients.length === 0) throw new Error("sealFields: no verified recipients");
|
|
3252
|
+
const seq = Math.floor(Date.now() / 1e3);
|
|
3253
|
+
const out = {};
|
|
3254
|
+
for (const field of ["context", "options", "visuals"]) {
|
|
3255
|
+
if (content[field] === void 0) continue;
|
|
3256
|
+
out[field] = seal({
|
|
3257
|
+
body: utf8ToBytes(JSON.stringify(content[field])),
|
|
3258
|
+
field,
|
|
3259
|
+
senderRole: "agent",
|
|
3260
|
+
seq,
|
|
3261
|
+
recipients
|
|
3262
|
+
});
|
|
3263
|
+
}
|
|
3264
|
+
return out;
|
|
3265
|
+
}
|
|
3266
|
+
function openSealedAnswer(sealed, keypair) {
|
|
3267
|
+
const myKeyId = keyId(keypair.x25519.publicKey);
|
|
3268
|
+
const { header, body } = open(sealed.envelope, myKeyId, keypair.x25519.secretKey);
|
|
3269
|
+
if (header.field !== "answer" || header.senderRole !== "user") {
|
|
3270
|
+
throw new Error("sealed answer has the wrong envelope field/role \u2014 refusing (possible replay/misattribution)");
|
|
3271
|
+
}
|
|
3272
|
+
const parsed = UserAnswerSchema.safeParse(JSON.parse(bytesToUtf8(body)));
|
|
3273
|
+
if (!parsed.success) throw new Error("sealed answer body is not a valid UserAnswer");
|
|
3274
|
+
const answer = parsed.data;
|
|
3275
|
+
const bodyIsIgnored = answer.kind === "ignored";
|
|
3276
|
+
if (sealed.ignored !== bodyIsIgnored) {
|
|
3277
|
+
throw new Error("sealed answer tamper: `ignored` hint disagrees with the decrypted body");
|
|
3278
|
+
}
|
|
3279
|
+
return answer;
|
|
3280
|
+
}
|
|
3281
|
+
|
|
3282
|
+
export {
|
|
3283
|
+
NotifyRequestSchema,
|
|
3284
|
+
deriveAsk,
|
|
3285
|
+
SetTaskStateSchema,
|
|
3286
|
+
ScheduleCallbackSchema,
|
|
3287
|
+
WAKE_EVENT,
|
|
3288
|
+
wakeChannel,
|
|
3289
|
+
BACKEND_URL,
|
|
3290
|
+
reach,
|
|
3291
|
+
AGENT_NAME,
|
|
3292
|
+
TOKEN_PATH,
|
|
3293
|
+
openBrowser,
|
|
3294
|
+
saveToken,
|
|
3295
|
+
sleep,
|
|
3296
|
+
readToken,
|
|
3297
|
+
deleteToken,
|
|
3298
|
+
revokeToken,
|
|
3299
|
+
requestCode,
|
|
3300
|
+
pollToken,
|
|
3301
|
+
saveKeyFile,
|
|
3302
|
+
readKeyFile,
|
|
3303
|
+
deleteKeyFile,
|
|
3304
|
+
startE2ee,
|
|
3305
|
+
finalizeE2ee,
|
|
3306
|
+
fetchCredential,
|
|
3307
|
+
pairStep,
|
|
3308
|
+
fetchRoster,
|
|
3309
|
+
verifiedRecipients,
|
|
3310
|
+
sealFields,
|
|
3311
|
+
openSealedAnswer
|
|
3312
|
+
};
|