@gmitch215/folio 1.0.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -460
- package/dist/boot.d.ts +1 -0
- package/dist/boot.js +1 -0
- package/dist/browser/index.d.ts +73 -0
- package/dist/browser/index.js +277 -0
- package/dist/browser.d.ts +1 -68
- package/dist/browser.js +1 -274
- package/dist/continuation.d.ts +1 -0
- package/dist/continuation.js +1 -0
- package/dist/{compiler.d.ts → execution/compiler.d.ts} +1 -1
- package/dist/{compiler.js → execution/compiler.js} +5 -12
- package/dist/{coordinator.d.ts → execution/coordinator.d.ts} +1 -1
- package/dist/{coordinator.js → execution/coordinator.js} +55 -8
- package/dist/{extract.d.ts → execution/extract.d.ts} +22 -13
- package/dist/{extract.js → execution/extract.js} +137 -10
- package/dist/execution/fold.d.ts +44 -0
- package/dist/execution/fold.js +105 -0
- package/dist/{guards.d.ts → execution/guards.d.ts} +2 -1
- package/dist/execution/runs.d.ts +44 -0
- package/dist/execution/runs.js +182 -0
- package/dist/{transaction.d.ts → execution/transaction.d.ts} +10 -0
- package/dist/execution/types.d.ts +221 -0
- package/dist/folds/affine-state.d.ts +142 -0
- package/dist/folds/affine-state.js +737 -0
- package/dist/folds/affine.d.ts +148 -0
- package/dist/folds/affine.js +562 -0
- package/dist/folds/boundaries.d.ts +48 -0
- package/dist/folds/boundaries.js +119 -0
- package/dist/folds/certified.d.ts +55 -0
- package/dist/folds/certified.js +178 -0
- package/dist/folds/certify.d.ts +87 -0
- package/dist/folds/certify.js +2611 -0
- package/dist/footprints/memo.d.ts +74 -0
- package/dist/footprints/memo.js +256 -0
- package/dist/footprints/ranges.d.ts +78 -0
- package/dist/footprints/ranges.js +263 -0
- package/dist/footprints/read-scan.d.ts +18 -0
- package/dist/footprints/read-scan.js +94 -0
- package/dist/footprints/read-windows.d.ts +77 -0
- package/dist/footprints/read-windows.js +245 -0
- package/dist/index.d.ts +16 -4
- package/dist/index.js +14 -2
- package/dist/iteration/admission.d.ts +149 -0
- package/dist/iteration/admission.js +201 -0
- package/dist/iteration/index.d.ts +239 -0
- package/dist/iteration/index.js +1210 -0
- package/dist/iteration/inline.d.ts +182 -0
- package/dist/{inline.js → iteration/inline.js} +277 -21
- package/dist/{iteration-owner.d.ts → iteration/owner.d.ts} +16 -3
- package/dist/{iteration-owner.js → iteration/owner.js} +6 -5
- package/dist/iteration/worker.js +216 -0
- package/dist/{managed.d.ts → managed/index.d.ts} +75 -3
- package/dist/managed/index.js +817 -0
- package/dist/managed/outbox.d.ts +128 -0
- package/dist/managed/outbox.js +156 -0
- package/dist/managed/pool.d.ts +221 -0
- package/dist/managed/pool.js +310 -0
- package/dist/managed/tasks.d.ts +154 -0
- package/dist/managed/tasks.js +183 -0
- package/dist/{bake.d.ts → memory/bake.d.ts} +12 -6
- package/dist/{bake.js → memory/bake.js} +9 -7
- package/dist/memory/continuation.d.ts +141 -0
- package/dist/memory/continuation.js +495 -0
- package/dist/memory/elastic-peer.d.ts +20 -0
- package/dist/memory/elastic-peer.js +39 -0
- package/dist/memory/elastic-sql.d.ts +11 -0
- package/dist/memory/elastic-sql.js +35 -0
- package/dist/memory/elastic.d.ts +130 -0
- package/dist/memory/elastic.js +475 -0
- package/dist/memory/growth-governor.d.ts +16 -0
- package/dist/memory/growth-governor.js +42 -0
- package/dist/memory/imports.d.ts +10 -0
- package/dist/memory/imports.js +89 -0
- package/dist/{iteration-layout.d.ts → memory/iteration-layout.d.ts} +61 -1
- package/dist/{iteration-layout.js → memory/iteration-layout.js} +54 -3
- package/dist/memory/lease.d.ts +45 -0
- package/dist/memory/lease.js +274 -0
- package/dist/{pages.d.ts → memory/pages.d.ts} +3 -3
- package/dist/memory.d.ts +5 -10
- package/dist/memory.js +5 -89
- package/dist/{context.d.ts → node/context.d.ts} +17 -3
- package/dist/{context.js → node/context.js} +60 -14
- package/dist/{host.d.ts → node/host.d.ts} +1 -1
- package/dist/{host.js → node/host.js} +2 -2
- package/dist/{node.d.ts → node/index.d.ts} +46 -49
- package/dist/{node.js → node/index.js} +101 -26
- package/dist/{tasks.d.ts → node/tasks.d.ts} +16 -17
- package/dist/{tasks.js → node/tasks.js} +12 -11
- package/dist/node/worker.d.ts +1 -0
- package/dist/{worker.js → node/worker.js} +2 -2
- package/dist/ownership/index.d.ts +161 -0
- package/dist/ownership/index.js +6565 -0
- package/dist/ownership/redirect.d.ts +30 -0
- package/dist/ownership/redirect.js +179 -0
- package/dist/ownership/slices.d.ts +44 -0
- package/dist/ownership/slices.js +72 -0
- package/dist/{frames.d.ts → remote/frames.d.ts} +6 -2
- package/dist/remote/frames.js +306 -0
- package/dist/{remote.d.ts → remote/index.d.ts} +7 -2
- package/dist/{remote.js → remote/index.js} +7 -2
- package/dist/routing/index.d.ts +203 -0
- package/dist/routing/index.js +671 -0
- package/dist/routing/worker.d.ts +1 -0
- package/dist/routing/worker.js +4 -0
- package/dist/slices/index.d.ts +126 -0
- package/dist/slices/index.js +441 -0
- package/dist/slices/worker.d.ts +1 -0
- package/dist/slices/worker.js +38 -0
- package/dist/workers.d.ts +5 -0
- package/dist/workers.js +5 -0
- package/package.json +32 -46
- package/dist/frames.js +0 -159
- package/dist/inline.d.ts +0 -68
- package/dist/iteration-admission.d.ts +0 -76
- package/dist/iteration-admission.js +0 -72
- package/dist/iteration-worker.js +0 -150
- package/dist/iteration.d.ts +0 -163
- package/dist/iteration.js +0 -435
- package/dist/managed.js +0 -421
- /package/dist/{browser-worker.d.ts → browser/worker.d.ts} +0 -0
- /package/dist/{browser-worker.js → browser/worker.js} +0 -0
- /package/dist/{guards.js → execution/guards.js} +0 -0
- /package/dist/{import-order.d.ts → execution/import-order.d.ts} +0 -0
- /package/dist/{import-order.js → execution/import-order.js} +0 -0
- /package/dist/{transaction.js → execution/transaction.js} +0 -0
- /package/dist/{iteration-worker.d.ts → execution/types.js} +0 -0
- /package/dist/{worker.d.ts → iteration/worker.d.ts} +0 -0
- /package/dist/{pages.js → memory/pages.js} +0 -0
- /package/dist/{values.d.ts → remote/values.d.ts} +0 -0
- /package/dist/{values.js → remote/values.js} +0 -0
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
import { chunkRanges } from "../execution/fold.js";
|
|
2
|
+
import { serveTasks } from "../node/tasks.js";
|
|
3
|
+
/** The task name `serveAffine` answers: `affine:<start state in hex>`. */
|
|
4
|
+
export const AFFINE_TASK = 'affine';
|
|
5
|
+
const DATA = 1 << 20;
|
|
6
|
+
const PAGE = 65536;
|
|
7
|
+
export const mask = (bits) => (1n << BigInt(bits)) - 1n;
|
|
8
|
+
export function mulberry(seed) {
|
|
9
|
+
let a = seed >>> 0;
|
|
10
|
+
return () => {
|
|
11
|
+
a = (a + 0x6d2b79f5) >>> 0;
|
|
12
|
+
let t = Math.imul(a ^ (a >>> 15), a | 1);
|
|
13
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
14
|
+
return (t ^ (t >>> 14)) >>> 0;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/** One private instance of the kernel, the memory it exports and a count of calls into it. */
|
|
18
|
+
class Kernel {
|
|
19
|
+
#fn;
|
|
20
|
+
#memory;
|
|
21
|
+
arity;
|
|
22
|
+
layout = { entry: '', state: 0, ptr: 1, len: 2, bits: 32 };
|
|
23
|
+
calls = 0;
|
|
24
|
+
constructor(module, entry) {
|
|
25
|
+
const compiled = module instanceof WebAssembly.Module
|
|
26
|
+
? module
|
|
27
|
+
: new WebAssembly.Module(module);
|
|
28
|
+
if (WebAssembly.Module.imports(compiled).length)
|
|
29
|
+
throw new Error('the kernel imports something; only freestanding modules are probed');
|
|
30
|
+
const exports = new WebAssembly.Instance(compiled, {}).exports;
|
|
31
|
+
const fn = exports[entry];
|
|
32
|
+
const memory = exports.memory;
|
|
33
|
+
if (typeof fn !== 'function')
|
|
34
|
+
throw new Error(`no function export ${entry}`);
|
|
35
|
+
if (!(memory instanceof WebAssembly.Memory))
|
|
36
|
+
throw new Error('the kernel exports no memory');
|
|
37
|
+
this.#fn = fn;
|
|
38
|
+
this.#memory = memory;
|
|
39
|
+
this.arity = fn.length;
|
|
40
|
+
this.layout.entry = entry;
|
|
41
|
+
}
|
|
42
|
+
#ensure(end) {
|
|
43
|
+
const have = this.#memory.buffer.byteLength;
|
|
44
|
+
if (have < end)
|
|
45
|
+
this.#memory.grow(Math.ceil((end - have) / PAGE));
|
|
46
|
+
}
|
|
47
|
+
place(at, bytes) {
|
|
48
|
+
this.#ensure(at + bytes.length);
|
|
49
|
+
new Uint8Array(this.#memory.buffer, at, bytes.length).set(bytes);
|
|
50
|
+
}
|
|
51
|
+
/** The raw call: every argument is passed as given. */
|
|
52
|
+
raw(args) {
|
|
53
|
+
this.calls++;
|
|
54
|
+
return this.#fn(...args);
|
|
55
|
+
}
|
|
56
|
+
call(state, ptr, len, layout = this.layout) {
|
|
57
|
+
const args = [0, 0, 0];
|
|
58
|
+
args[layout.state] =
|
|
59
|
+
layout.bits === 32 ? Number(BigInt.asIntN(32, state)) : BigInt.asIntN(64, state);
|
|
60
|
+
args[layout.ptr] = ptr;
|
|
61
|
+
args[layout.len] = len;
|
|
62
|
+
return BigInt.asUintN(layout.bits, BigInt(this.raw(args)));
|
|
63
|
+
}
|
|
64
|
+
run(state, input) {
|
|
65
|
+
this.place(DATA, input);
|
|
66
|
+
return this.call(state, DATA, input.length);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// #region gf2 and z2w arithmetic
|
|
70
|
+
export function gfApply(columns, vector) {
|
|
71
|
+
let out = 0n;
|
|
72
|
+
for (let k = 0; vector; k++, vector >>= 1n)
|
|
73
|
+
if (vector & 1n)
|
|
74
|
+
out ^= columns[k];
|
|
75
|
+
return out;
|
|
76
|
+
}
|
|
77
|
+
export const gfMul = (a, b) => b.map((column) => gfApply(a, column));
|
|
78
|
+
const powers = new WeakMap();
|
|
79
|
+
function powersOf(model) {
|
|
80
|
+
let found = powers.get(model);
|
|
81
|
+
if (!found) {
|
|
82
|
+
found = { squares: [], byUnits: new Map(), byMultiplier: new Map(), byResidues: new Map() };
|
|
83
|
+
if (model.ring === 'gf2')
|
|
84
|
+
found.squares.push(model.operator);
|
|
85
|
+
powers.set(model, found);
|
|
86
|
+
}
|
|
87
|
+
return found;
|
|
88
|
+
}
|
|
89
|
+
function matrixPower(model, units) {
|
|
90
|
+
const cache = powersOf(model);
|
|
91
|
+
const cached = cache.byUnits.get(units);
|
|
92
|
+
if (cached)
|
|
93
|
+
return cached;
|
|
94
|
+
let result = Array.from({ length: model.bits }, (_, k) => 1n << BigInt(k));
|
|
95
|
+
for (let n = units, k = 0; n > 0; n = Math.floor(n / 2), k++) {
|
|
96
|
+
if (!cache.squares[k])
|
|
97
|
+
cache.squares[k] = gfMul(cache.squares[k - 1], cache.squares[k - 1]);
|
|
98
|
+
if (n % 2 === 1)
|
|
99
|
+
result = gfMul(cache.squares[k], result);
|
|
100
|
+
}
|
|
101
|
+
cache.byUnits.set(units, result);
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
function multiplierPower(model, units) {
|
|
105
|
+
const cache = powersOf(model);
|
|
106
|
+
const cached = cache.byMultiplier.get(units);
|
|
107
|
+
if (cached !== undefined)
|
|
108
|
+
return cached;
|
|
109
|
+
const modulus = 1n << BigInt(model.bits);
|
|
110
|
+
let result = 1n;
|
|
111
|
+
let base = model.operator;
|
|
112
|
+
for (let n = units; n > 0; n = Math.floor(n / 2)) {
|
|
113
|
+
if (n % 2 === 1)
|
|
114
|
+
result = (result * base) % modulus;
|
|
115
|
+
base = (base * base) % modulus;
|
|
116
|
+
}
|
|
117
|
+
cache.byMultiplier.set(units, result);
|
|
118
|
+
return result;
|
|
119
|
+
}
|
|
120
|
+
// the digits of a zp state, lowest first
|
|
121
|
+
function residues(model, state) {
|
|
122
|
+
const k = model.digits;
|
|
123
|
+
const width = BigInt(model.bits / k);
|
|
124
|
+
return Array.from({ length: k }, (_, i) => (state >> (width * BigInt(i))) & mask(model.bits / k));
|
|
125
|
+
}
|
|
126
|
+
function residueState(model, digits) {
|
|
127
|
+
const width = BigInt(model.bits / model.digits);
|
|
128
|
+
return digits.reduce((acc, d, i) => acc | (d << (width * BigInt(i))), 0n);
|
|
129
|
+
}
|
|
130
|
+
function residuePower(model, units) {
|
|
131
|
+
const cache = powersOf(model);
|
|
132
|
+
const cached = cache.byResidues.get(units);
|
|
133
|
+
if (cached)
|
|
134
|
+
return cached;
|
|
135
|
+
const k = model.digits;
|
|
136
|
+
const p = model.modulus;
|
|
137
|
+
const times = (a, b) => Array.from({ length: k * k }, (_, at) => {
|
|
138
|
+
const i = Math.floor(at / k);
|
|
139
|
+
const j = at % k;
|
|
140
|
+
let sum = 0n;
|
|
141
|
+
for (let t = 0; t < k; t++)
|
|
142
|
+
sum += a[i * k + t] * b[t * k + j];
|
|
143
|
+
return sum % p;
|
|
144
|
+
});
|
|
145
|
+
let result = Array.from({ length: k * k }, (_, at) => (at % (k + 1) === 0 ? 1n : 0n));
|
|
146
|
+
let base = model.operator;
|
|
147
|
+
for (let n = units; n > 0; n = Math.floor(n / 2)) {
|
|
148
|
+
if (n % 2 === 1)
|
|
149
|
+
result = times(base, result);
|
|
150
|
+
base = times(base, base);
|
|
151
|
+
}
|
|
152
|
+
cache.byResidues.set(units, result);
|
|
153
|
+
return result;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* The state-dependent part of T(state, x) for `length` bytes: A(length) state. `length` must be a
|
|
157
|
+
* multiple of `model.align`. Over `zp` a zero length leaves the state unchanged.
|
|
158
|
+
*/
|
|
159
|
+
export function advance(model, state, length) {
|
|
160
|
+
if (!Number.isSafeInteger(length) || length < 0 || length % model.align)
|
|
161
|
+
throw new RangeError(`length ${length} is not a multiple of ${model.align}`);
|
|
162
|
+
const units = length / model.align;
|
|
163
|
+
if (model.ring === 'gf2')
|
|
164
|
+
return gfApply(matrixPower(model, units), state);
|
|
165
|
+
if (model.ring === 'zp') {
|
|
166
|
+
if (units === 0)
|
|
167
|
+
return state;
|
|
168
|
+
const k = model.digits;
|
|
169
|
+
const m = residuePower(model, units);
|
|
170
|
+
const s = residues(model, state);
|
|
171
|
+
return residueState(model, s.map((_, i) => s.reduce((sum, d, j) => sum + m[i * k + j] * d, 0n) % model.modulus));
|
|
172
|
+
}
|
|
173
|
+
return (multiplierPower(model, units) * state) & mask(model.bits);
|
|
174
|
+
}
|
|
175
|
+
/** T(start, x) predicted from `offset` = T(0, x): A(length) start combined with the offset. */
|
|
176
|
+
export function predictEnd(model, start, length, offset) {
|
|
177
|
+
const moved = advance(model, start, length);
|
|
178
|
+
if (model.ring === 'gf2')
|
|
179
|
+
return moved ^ offset;
|
|
180
|
+
if (model.ring === 'zp') {
|
|
181
|
+
if (length === 0)
|
|
182
|
+
return start;
|
|
183
|
+
const o = residues(model, offset);
|
|
184
|
+
return residueState(model, residues(model, moved).map((d, i) => (d + o[i]) % model.modulus));
|
|
185
|
+
}
|
|
186
|
+
return (moved + offset) & mask(model.bits);
|
|
187
|
+
}
|
|
188
|
+
// #endregion
|
|
189
|
+
/**
|
|
190
|
+
* Probes `entry`, a (state, pointer, length) -> state function, by trial. Returns the affine model
|
|
191
|
+
* or the check that failed. A pass is evidence on the sampled inputs only; `foldAffine` in `exact`
|
|
192
|
+
* mode is what makes the result independent of the probe.
|
|
193
|
+
*/
|
|
194
|
+
export function probeAffine(module, entry, options = {}) {
|
|
195
|
+
const trials = options.trials ?? 32;
|
|
196
|
+
const align = options.align ?? 1;
|
|
197
|
+
if (!Number.isSafeInteger(trials) || trials < 1)
|
|
198
|
+
throw new RangeError('invalid trial count');
|
|
199
|
+
if (!Number.isSafeInteger(align) || align < 1)
|
|
200
|
+
throw new RangeError('invalid alignment');
|
|
201
|
+
const kernel = new Kernel(module, entry);
|
|
202
|
+
const checks = [];
|
|
203
|
+
const refuse = (reason) => ({
|
|
204
|
+
kind: 'refusal',
|
|
205
|
+
reason,
|
|
206
|
+
checks,
|
|
207
|
+
calls: kernel.calls
|
|
208
|
+
});
|
|
209
|
+
try {
|
|
210
|
+
return probe(kernel, checks, refuse, trials, align, options.seed ?? 0x9e3779b9);
|
|
211
|
+
}
|
|
212
|
+
catch (error) {
|
|
213
|
+
return refuse(`the kernel trapped: ${error.message}`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function probe(kernel, checks, refuse, trials, align, seed) {
|
|
217
|
+
if (kernel.arity !== 3)
|
|
218
|
+
return refuse(`expected 3 parameters, found ${kernel.arity}`);
|
|
219
|
+
const rng = mulberry(seed);
|
|
220
|
+
const word = (bits) => bits === 32 ? BigInt(rng()) : (BigInt(rng()) << 32n) | BigInt(rng());
|
|
221
|
+
const bytes = (length) => {
|
|
222
|
+
const out = new Uint8Array(length);
|
|
223
|
+
for (let i = 0; i < length; i += 4) {
|
|
224
|
+
const value = rng();
|
|
225
|
+
for (let k = 0; k < 4 && i + k < length; k++)
|
|
226
|
+
out[i + k] = (value >>> (8 * k)) & 255;
|
|
227
|
+
}
|
|
228
|
+
return out;
|
|
229
|
+
};
|
|
230
|
+
const units = (most) => align * (1 + (rng() % Math.max(1, Math.floor(most / align))));
|
|
231
|
+
const rounded = (length) => Math.ceil(length / align) * align;
|
|
232
|
+
// parameter types: the combination of numbers and bigints the engine accepts for zeros
|
|
233
|
+
let types;
|
|
234
|
+
let result = 'i32';
|
|
235
|
+
for (let pattern = 0; pattern < 8 && !types; pattern++) {
|
|
236
|
+
const kinds = [0, 1, 2].map((k) => ((pattern >> k) & 1 ? 'i64' : 'i32'));
|
|
237
|
+
try {
|
|
238
|
+
const value = kernel.raw(kinds.map((kind) => (kind === 'i64' ? 0n : 0)));
|
|
239
|
+
types = kinds;
|
|
240
|
+
result = typeof value === 'bigint' ? 'i64' : 'i32';
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
if (!(error instanceof TypeError)) {
|
|
244
|
+
types = kinds;
|
|
245
|
+
result = 'i32';
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if (!types)
|
|
250
|
+
return refuse('no combination of i32 and i64 parameters was accepted');
|
|
251
|
+
const bits = result === 'i64' ? 64 : 32;
|
|
252
|
+
// roles: the order of (state, pointer, length) under which the kernel composes over a split
|
|
253
|
+
const orders = [
|
|
254
|
+
[0, 1, 2],
|
|
255
|
+
[0, 2, 1],
|
|
256
|
+
[1, 0, 2],
|
|
257
|
+
[1, 2, 0],
|
|
258
|
+
[2, 0, 1],
|
|
259
|
+
[2, 1, 0]
|
|
260
|
+
];
|
|
261
|
+
let layout;
|
|
262
|
+
for (const [state, ptr, len] of orders) {
|
|
263
|
+
if (types[state] !== result || types[ptr] !== 'i32' || types[len] !== 'i32')
|
|
264
|
+
continue;
|
|
265
|
+
const candidate = { entry: kernel.layout.entry, state, ptr, len, bits };
|
|
266
|
+
const T = (s, at, length) => kernel.call(s, at, length, candidate);
|
|
267
|
+
try {
|
|
268
|
+
const sample = bytes(rounded(256));
|
|
269
|
+
kernel.place(DATA, sample);
|
|
270
|
+
const probeLength = units(16);
|
|
271
|
+
if (T(0n, DATA, probeLength) === T(word(bits) | 1n, DATA, probeLength))
|
|
272
|
+
continue;
|
|
273
|
+
const other = Uint8Array.from(sample);
|
|
274
|
+
other[0] = other[0] ^ 0x55;
|
|
275
|
+
kernel.place(DATA + 4096, other);
|
|
276
|
+
if (T(0n, DATA, probeLength) === T(0n, DATA + 4096, probeLength))
|
|
277
|
+
continue;
|
|
278
|
+
let composes = true;
|
|
279
|
+
for (let i = 0; i < 6 && composes; i++) {
|
|
280
|
+
const s = word(bits);
|
|
281
|
+
const a = units(100);
|
|
282
|
+
const b = units(100);
|
|
283
|
+
composes = T(s, DATA, a + b) === T(T(s, DATA, a), DATA + a, b);
|
|
284
|
+
}
|
|
285
|
+
if (!composes)
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
layout = candidate;
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
294
|
+
if (!layout)
|
|
295
|
+
return refuse('no parameter order is a streaming update over (state, pointer, length)');
|
|
296
|
+
kernel.layout = layout;
|
|
297
|
+
const T = (s, at, length) => kernel.call(s, at, length);
|
|
298
|
+
const ringOps = (ring) => {
|
|
299
|
+
const modulus = 1n << BigInt(bits);
|
|
300
|
+
return ring === 'gf2'
|
|
301
|
+
? { plus: (a, b) => a ^ b, minus: (a, b) => a ^ b }
|
|
302
|
+
: {
|
|
303
|
+
plus: (a, b) => (a + b) % modulus,
|
|
304
|
+
minus: (a, b) => (a - b + modulus) % modulus
|
|
305
|
+
};
|
|
306
|
+
};
|
|
307
|
+
const run = (ring, name, count, body) => {
|
|
308
|
+
for (let i = 0; i < count; i++)
|
|
309
|
+
if (!body(i)) {
|
|
310
|
+
checks.push({ ring, name, trials: i + 1, passed: false });
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
checks.push({ ring, name, trials: count, passed: true });
|
|
314
|
+
return true;
|
|
315
|
+
};
|
|
316
|
+
const failures = [];
|
|
317
|
+
for (const ring of ['gf2', 'z2w']) {
|
|
318
|
+
const { plus, minus } = ringOps(ring);
|
|
319
|
+
const additive = run(ring, 'additivity of the state part', trials, () => {
|
|
320
|
+
const length = units(4096);
|
|
321
|
+
kernel.place(DATA, bytes(length));
|
|
322
|
+
const s1 = word(bits);
|
|
323
|
+
const s2 = word(bits);
|
|
324
|
+
const zero = T(0n, DATA, length);
|
|
325
|
+
const lhs = T(plus(s1, s2), DATA, length);
|
|
326
|
+
return lhs === minus(plus(T(s1, DATA, length), T(s2, DATA, length)), zero);
|
|
327
|
+
});
|
|
328
|
+
if (!additive) {
|
|
329
|
+
failures.push(`${ring} additivity`);
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
// the align-byte operator: columns T(e_k, x) - T(0, x) over GF(2), T(1, x) - T(0, x) over Z
|
|
333
|
+
const unitData = bytes(align);
|
|
334
|
+
kernel.place(DATA, unitData);
|
|
335
|
+
const zeroUnit = T(0n, DATA, align);
|
|
336
|
+
const operator = ring === 'gf2'
|
|
337
|
+
? Array.from({ length: bits }, (_, k) => minus(T(1n << BigInt(k), DATA, align), zeroUnit))
|
|
338
|
+
: minus(T(1n, DATA, align), zeroUnit);
|
|
339
|
+
const model = {
|
|
340
|
+
kind: 'model',
|
|
341
|
+
ring,
|
|
342
|
+
bits,
|
|
343
|
+
align,
|
|
344
|
+
layout,
|
|
345
|
+
operator,
|
|
346
|
+
checks,
|
|
347
|
+
calls: 0
|
|
348
|
+
};
|
|
349
|
+
const A = (s, length) => advance(model, s, length);
|
|
350
|
+
const independent = run(ring, 'data independence of the state part', trials, () => {
|
|
351
|
+
const length = units(4096);
|
|
352
|
+
const x = bytes(length);
|
|
353
|
+
const y = bytes(length);
|
|
354
|
+
const s = word(bits);
|
|
355
|
+
kernel.place(DATA, x);
|
|
356
|
+
const fromX = minus(T(s, DATA, length), T(0n, DATA, length));
|
|
357
|
+
kernel.place(DATA, y);
|
|
358
|
+
return fromX === minus(T(s, DATA, length), T(0n, DATA, length));
|
|
359
|
+
});
|
|
360
|
+
const homogeneous = () => {
|
|
361
|
+
const lengths = [1, 2, 7, 64, 1000, 65537].map(rounded);
|
|
362
|
+
let tried = 0;
|
|
363
|
+
for (const length of lengths) {
|
|
364
|
+
kernel.place(DATA, bytes(length));
|
|
365
|
+
for (let i = 0; i < 4; i++, tried++) {
|
|
366
|
+
const s = word(bits);
|
|
367
|
+
if (minus(T(s, DATA, length), T(0n, DATA, length)) !== A(s, length)) {
|
|
368
|
+
checks.push({
|
|
369
|
+
ring,
|
|
370
|
+
name: 'homogeneity',
|
|
371
|
+
trials: tried + 1,
|
|
372
|
+
passed: false
|
|
373
|
+
});
|
|
374
|
+
return false;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
checks.push({ ring, name: 'homogeneity', trials: tried, passed: true });
|
|
379
|
+
return true;
|
|
380
|
+
};
|
|
381
|
+
const additiveData = () => run(ring, 'additivity of the data part', trials, () => {
|
|
382
|
+
const a = units(2048);
|
|
383
|
+
const b = units(2048);
|
|
384
|
+
kernel.place(DATA, bytes(a + b));
|
|
385
|
+
const whole = T(0n, DATA, a + b);
|
|
386
|
+
return whole === plus(A(T(0n, DATA, a), b), T(0n, DATA + a, b));
|
|
387
|
+
});
|
|
388
|
+
const streaming = () => run(ring, 'streaming composition', trials, () => {
|
|
389
|
+
const a = units(2048);
|
|
390
|
+
const b = units(2048);
|
|
391
|
+
kernel.place(DATA, bytes(a + b));
|
|
392
|
+
const s = word(bits);
|
|
393
|
+
return T(s, DATA, a + b) === T(T(s, DATA, a), DATA + a, b);
|
|
394
|
+
});
|
|
395
|
+
if (independent && homogeneous() && additiveData() && streaming()) {
|
|
396
|
+
model.calls = kernel.calls;
|
|
397
|
+
return model;
|
|
398
|
+
}
|
|
399
|
+
const failed = checks.filter((c) => c.ring === ring && !c.passed)[0];
|
|
400
|
+
return refuse(`${ring} passed additivity and failed ${failed.name}`);
|
|
401
|
+
}
|
|
402
|
+
return refuse(`not affine over either ring: failed ${failures.join(' and ')}`);
|
|
403
|
+
}
|
|
404
|
+
/** Worker side: answers `affine:<hex state>` tasks with the 8-byte little-endian end state. */
|
|
405
|
+
export function serveAffine(link, subject) {
|
|
406
|
+
const kernel = new Kernel(subject.module, subject.layout.entry);
|
|
407
|
+
kernel.layout = subject.layout;
|
|
408
|
+
serveTasks(link, (name, input) => {
|
|
409
|
+
const [task, hex] = name.split(':');
|
|
410
|
+
if (task !== AFFINE_TASK || !hex)
|
|
411
|
+
throw new Error(`unknown task ${name}`);
|
|
412
|
+
const end = kernel.run(BigInt(`0x${hex}`), input);
|
|
413
|
+
const out = new Uint8Array(8);
|
|
414
|
+
new DataView(out.buffer).setBigUint64(0, end, true);
|
|
415
|
+
return out;
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
/** The serial reference: one call over all the bytes in a private instance. */
|
|
419
|
+
export function affineSerial(subject) {
|
|
420
|
+
const kernel = new Kernel(subject.module, subject.layout.entry);
|
|
421
|
+
kernel.layout = subject.layout;
|
|
422
|
+
return (input, state = 0n) => kernel.run(BigInt.asUintN(subject.layout.bits, state), input);
|
|
423
|
+
}
|
|
424
|
+
function checkRanges(chunks, length, align) {
|
|
425
|
+
let at = 0;
|
|
426
|
+
chunks.forEach((chunk, index) => {
|
|
427
|
+
if (chunk.start !== at || chunk.end <= chunk.start)
|
|
428
|
+
throw new RangeError(`chunk ${index} is not contiguous`);
|
|
429
|
+
if (index < chunks.length - 1 && chunk.end % align !== 0)
|
|
430
|
+
throw new RangeError(`chunk ${index} ends at ${chunk.end}, not a multiple of ${align}`);
|
|
431
|
+
at = chunk.end;
|
|
432
|
+
});
|
|
433
|
+
if (at !== length)
|
|
434
|
+
throw new RangeError(`chunks cover ${at} of ${length} bytes`);
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* The two pass exact fold over at least two chunks. The result is the serial state by induction:
|
|
438
|
+
* every pass 2 chunk ran from the start state its predecessor's end state is compared with. The
|
|
439
|
+
* first disagreement hands the rest of the input to one serial task.
|
|
440
|
+
*/
|
|
441
|
+
export function foldExact(chunks, length, initial, hooks) {
|
|
442
|
+
const n = chunks.length;
|
|
443
|
+
const size = (chunk) => chunk.end - chunk.start;
|
|
444
|
+
const first = Array.from({ length: n - 1 }, (_, i) => hooks.submit(i === 0 ? initial : hooks.zero, chunks[i].start, chunks[i].end));
|
|
445
|
+
const predicted = [initial];
|
|
446
|
+
const second = [''];
|
|
447
|
+
for (let i = 0; i < n - 1; i++) {
|
|
448
|
+
const offset = hooks.end(first[i]);
|
|
449
|
+
predicted[i + 1] =
|
|
450
|
+
i === 0 ? offset : hooks.predict(predicted[i], size(chunks[i]), offset);
|
|
451
|
+
second[i + 1] = hooks.submit(predicted[i + 1], chunks[i + 1].start, chunks[i + 1].end);
|
|
452
|
+
}
|
|
453
|
+
const ends = second.map((handle, i) => (i ? hooks.end(handle) : hooks.zero));
|
|
454
|
+
for (let i = 1; i < n; i++) {
|
|
455
|
+
if (i === n - 1)
|
|
456
|
+
return { state: ends[i], validated: n - 1, mismatchAt: -1, fallbackBytes: 0 };
|
|
457
|
+
if (!hooks.equal(ends[i], predicted[i + 1])) {
|
|
458
|
+
const rest = chunks[i + 1].start;
|
|
459
|
+
const state = hooks.end(hooks.submit(ends[i], rest, length));
|
|
460
|
+
return { state, validated: i, mismatchAt: i, fallbackBytes: length - rest };
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
throw new Error('unreachable');
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Folds `input` through the kernel on `pool` workers started with `serveAffine`, and returns the
|
|
467
|
+
* state the serial call over the same bytes returns.
|
|
468
|
+
*
|
|
469
|
+
* `exact`: pass 1 runs every chunk but the last from state zero (the first from `initial`); the
|
|
470
|
+
* model turns the offsets into each chunk's predicted start; pass 2 runs chunks 1 to n-1 from
|
|
471
|
+
* their predicted starts; an ordered check compares each chunk's real end state with the next
|
|
472
|
+
* chunk's predicted start. If all agree, the final state is the serial one by induction, whatever
|
|
473
|
+
* the model got wrong. At the first disagreement the rest of the input runs serially from the
|
|
474
|
+
* last real state. This equals the serial call when the kernel composes over a split
|
|
475
|
+
* (T(s, x||y) = T(T(s, x), y)), which the probe checks on samples and nothing here proves.
|
|
476
|
+
*
|
|
477
|
+
* `audit`: one pass from state zero, the model combines the offsets, and `audit` chunks run again
|
|
478
|
+
* from their predicted starts. A disagreement reruns the whole input serially. A chunk that is not
|
|
479
|
+
* sampled is trusted: the model's error in it goes undetected.
|
|
480
|
+
*/
|
|
481
|
+
export function foldAffine(pool, input, model, options = {}) {
|
|
482
|
+
const mode = options.mode ?? 'exact';
|
|
483
|
+
if (mode !== 'exact' && mode !== 'audit')
|
|
484
|
+
throw new RangeError('invalid mode');
|
|
485
|
+
const initial = BigInt.asUintN(model.bits, options.initial ?? 0n);
|
|
486
|
+
const result = (state, extra) => ({
|
|
487
|
+
mode,
|
|
488
|
+
state,
|
|
489
|
+
validated: 0,
|
|
490
|
+
mismatchAt: -1,
|
|
491
|
+
audited: [],
|
|
492
|
+
fallbackBytes: 0,
|
|
493
|
+
...extra
|
|
494
|
+
});
|
|
495
|
+
if (!input.length)
|
|
496
|
+
return result(initial, { tasks: 0, chunks: 0 });
|
|
497
|
+
const chunks = options.chunks ?? chunkRanges(input.length, pool.width, model.align);
|
|
498
|
+
checkRanges(chunks, input.length, model.align);
|
|
499
|
+
const n = chunks.length;
|
|
500
|
+
const size = (chunk) => chunk.end - chunk.start;
|
|
501
|
+
let tasks = 0;
|
|
502
|
+
const submit = (start, from, to) => {
|
|
503
|
+
tasks++;
|
|
504
|
+
return pool.submit(`${AFFINE_TASK}:${start.toString(16)}`, input.slice(from, to));
|
|
505
|
+
};
|
|
506
|
+
const endOf = (handle) => {
|
|
507
|
+
const part = pool.join(handle);
|
|
508
|
+
return (new DataView(part.bytes.buffer, part.bytes.byteOffset).getBigUint64(0, true) &
|
|
509
|
+
mask(model.bits));
|
|
510
|
+
};
|
|
511
|
+
const serial = (start, from) => endOf(submit(start, from, input.length));
|
|
512
|
+
if (n === 1)
|
|
513
|
+
return result(serial(initial, 0), { tasks, chunks: n });
|
|
514
|
+
if (mode === 'exact') {
|
|
515
|
+
const outcome = foldExact(chunks, input.length, initial, {
|
|
516
|
+
zero: 0n,
|
|
517
|
+
submit: (start, from, to) => submit(start, from, to),
|
|
518
|
+
end: endOf,
|
|
519
|
+
predict: (start, length, offset) => predictEnd(model, start, length, offset),
|
|
520
|
+
equal: (a, b) => a === b
|
|
521
|
+
});
|
|
522
|
+
return result(outcome.state, {
|
|
523
|
+
tasks,
|
|
524
|
+
chunks: n,
|
|
525
|
+
validated: outcome.validated,
|
|
526
|
+
mismatchAt: outcome.mismatchAt,
|
|
527
|
+
fallbackBytes: outcome.fallbackBytes
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
const first = chunks.map((chunk, i) => submit(i === 0 ? initial : 0n, chunk.start, chunk.end));
|
|
531
|
+
const predicted = [initial];
|
|
532
|
+
chunks.forEach((chunk, i) => {
|
|
533
|
+
const offset = endOf(first[i]);
|
|
534
|
+
if (i === n - 1 && size(chunk) % model.align)
|
|
535
|
+
throw new RangeError('audit mode needs a last chunk that is a multiple of the alignment');
|
|
536
|
+
predicted[i + 1] = i === 0 ? offset : predictEnd(model, predicted[i], size(chunk), offset);
|
|
537
|
+
});
|
|
538
|
+
const count = Math.min(options.audit ?? 2, n - 1);
|
|
539
|
+
const rng = mulberry(options.seed ?? 1);
|
|
540
|
+
const order = Array.from({ length: n - 1 }, (_, i) => i + 1);
|
|
541
|
+
for (let i = order.length - 1; i > 0; i--) {
|
|
542
|
+
const j = rng() % (i + 1);
|
|
543
|
+
[order[i], order[j]] = [order[j], order[i]];
|
|
544
|
+
}
|
|
545
|
+
const audited = order.slice(0, count).sort((a, b) => a - b);
|
|
546
|
+
const reruns = audited.map((i) => submit(predicted[i], chunks[i].start, chunks[i].end));
|
|
547
|
+
let mismatchAt = -1;
|
|
548
|
+
audited.forEach((i, k) => {
|
|
549
|
+
const end = endOf(reruns[k]);
|
|
550
|
+
if (end !== predicted[i + 1] && mismatchAt < 0)
|
|
551
|
+
mismatchAt = i;
|
|
552
|
+
});
|
|
553
|
+
if (mismatchAt < 0)
|
|
554
|
+
return result(predicted[n], { tasks, chunks: n, audited, validated: audited.length });
|
|
555
|
+
return result(serial(initial, 0), {
|
|
556
|
+
tasks,
|
|
557
|
+
chunks: n,
|
|
558
|
+
audited,
|
|
559
|
+
mismatchAt,
|
|
560
|
+
fallbackBytes: input.length
|
|
561
|
+
});
|
|
562
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { TaskPool } from '../node/tasks.ts';
|
|
2
|
+
import { type ContextSubject } from './affine-state.ts';
|
|
3
|
+
/** States of a serial chain every `step` bytes: `starts[k]` is the state before byte `k * step`. */
|
|
4
|
+
export interface BoundaryLog {
|
|
5
|
+
step: number;
|
|
6
|
+
starts: Uint8Array[];
|
|
7
|
+
}
|
|
8
|
+
export interface RecordOptions {
|
|
9
|
+
/** Bytes between recorded states. */
|
|
10
|
+
step: number;
|
|
11
|
+
/** State before the first byte. The default is the state bytes of `subject.context`. */
|
|
12
|
+
initial?: Uint8Array;
|
|
13
|
+
}
|
|
14
|
+
export interface VerifyOptions {
|
|
15
|
+
initial?: Uint8Array;
|
|
16
|
+
/** Step of the fresh log when the given log is unusable. The default is two chunks per worker. */
|
|
17
|
+
step?: number;
|
|
18
|
+
}
|
|
19
|
+
export interface VerifyResult {
|
|
20
|
+
state: Uint8Array;
|
|
21
|
+
/** The log for this input: the verified prefix of the old one, then fresh states. */
|
|
22
|
+
log: BoundaryLog;
|
|
23
|
+
/** Chunks that ran in parallel from a recorded start. */
|
|
24
|
+
chunks: number;
|
|
25
|
+
tasks: number;
|
|
26
|
+
/** Boundaries where a chunk's end state equalled the next recorded start. */
|
|
27
|
+
validated: number;
|
|
28
|
+
/** The first chunk whose end state differed from the next recorded start, or -1. */
|
|
29
|
+
mismatchAt: number;
|
|
30
|
+
/** Bytes that ran serially after the parallel pass. */
|
|
31
|
+
fallbackBytes: number;
|
|
32
|
+
/** The log was unusable or its first start differed from the initial state. */
|
|
33
|
+
stale: boolean;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Runs the chain serially on a private instance and records the state every `step` bytes. The
|
|
37
|
+
* returned state equals a single call over the whole input when the kernel composes over a split.
|
|
38
|
+
*/
|
|
39
|
+
export declare function recordBoundaries(subject: ContextSubject, input: Uint8Array, options: RecordOptions): {
|
|
40
|
+
state: Uint8Array;
|
|
41
|
+
log: BoundaryLog;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Runs each chunk in parallel from its recorded start and compares each end with the next start.
|
|
45
|
+
* Nothing about the bytes is hashed, so a stale log shows up as a difference and cannot change the
|
|
46
|
+
* result. At the first difference the rest runs serially and records fresh boundaries.
|
|
47
|
+
*/
|
|
48
|
+
export declare function verifyBoundaries(pool: TaskPool, subject: ContextSubject, input: Uint8Array, log: BoundaryLog, options?: VerifyOptions): VerifyResult;
|