@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,119 @@
|
|
|
1
|
+
import { bytesEqual, ContextKernel, pick, toHex } from "./affine-state.js";
|
|
2
|
+
const checkStep = (step) => {
|
|
3
|
+
if (!Number.isSafeInteger(step) || step < 1)
|
|
4
|
+
throw new RangeError('invalid step');
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Runs the chain serially on a private instance and records the state every `step` bytes. The
|
|
8
|
+
* returned state equals a single call over the whole input when the kernel composes over a split.
|
|
9
|
+
*/
|
|
10
|
+
export function recordBoundaries(subject, input, options) {
|
|
11
|
+
checkStep(options.step);
|
|
12
|
+
const kernel = new ContextKernel(subject.module, subject.layout.entry);
|
|
13
|
+
const initial = options.initial ?? pick(subject.context, subject.state);
|
|
14
|
+
if (initial.length !== subject.state.length)
|
|
15
|
+
throw new RangeError('invalid initial state length');
|
|
16
|
+
const starts = [];
|
|
17
|
+
let state = initial;
|
|
18
|
+
for (let at = 0; at < input.length; at += options.step) {
|
|
19
|
+
starts.push(state);
|
|
20
|
+
const image = Uint8Array.from(subject.context);
|
|
21
|
+
subject.state.forEach((o, k) => (image[o] = state[k]));
|
|
22
|
+
const out = kernel.run(subject.layout, image, input.subarray(at, at + options.step));
|
|
23
|
+
if (!out)
|
|
24
|
+
throw new Error('the kernel trapped');
|
|
25
|
+
state = pick(out, subject.state);
|
|
26
|
+
}
|
|
27
|
+
return { state, log: { step: options.step, starts } };
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Runs each chunk in parallel from its recorded start and compares each end with the next start.
|
|
31
|
+
* Nothing about the bytes is hashed, so a stale log shows up as a difference and cannot change the
|
|
32
|
+
* result. At the first difference the rest runs serially and records fresh boundaries.
|
|
33
|
+
*/
|
|
34
|
+
export function verifyBoundaries(pool, subject, input, log, options = {}) {
|
|
35
|
+
const width = subject.state.length;
|
|
36
|
+
const initial = options.initial ?? pick(subject.context, subject.state);
|
|
37
|
+
if (initial.length !== width)
|
|
38
|
+
throw new RangeError('invalid initial state length');
|
|
39
|
+
const length = input.length;
|
|
40
|
+
const fresh = {
|
|
41
|
+
step: options.step ?? Math.max(1, Math.ceil(length / (2 * pool.width))),
|
|
42
|
+
starts: []
|
|
43
|
+
};
|
|
44
|
+
if (!length)
|
|
45
|
+
return {
|
|
46
|
+
state: initial,
|
|
47
|
+
log: fresh,
|
|
48
|
+
chunks: 0,
|
|
49
|
+
tasks: 0,
|
|
50
|
+
validated: 0,
|
|
51
|
+
mismatchAt: -1,
|
|
52
|
+
fallbackBytes: 0,
|
|
53
|
+
stale: false
|
|
54
|
+
};
|
|
55
|
+
const usable = Number.isSafeInteger(log?.step) &&
|
|
56
|
+
log.step > 0 &&
|
|
57
|
+
Array.isArray(log.starts) &&
|
|
58
|
+
log.starts.length > 0 &&
|
|
59
|
+
log.starts.every((s) => s instanceof Uint8Array && s.length === width) &&
|
|
60
|
+
bytesEqual(log.starts[0], initial);
|
|
61
|
+
const step = usable ? log.step : fresh.step;
|
|
62
|
+
const chunks = usable ? Math.min(log.starts.length, Math.ceil(length / step)) : 0;
|
|
63
|
+
let tasks = 0;
|
|
64
|
+
const submit = (name, from, to) => {
|
|
65
|
+
tasks++;
|
|
66
|
+
return pool.submit(name, input.slice(from, to));
|
|
67
|
+
};
|
|
68
|
+
const handles = Array.from({ length: chunks }, (_, k) => submit(`ctx:${toHex(log.starts[k])}`, k * step, Math.min((k + 1) * step, length)));
|
|
69
|
+
// every chunk up to `done` ran from a state that is the real state at its start
|
|
70
|
+
let done = -1;
|
|
71
|
+
let real = initial;
|
|
72
|
+
let mismatchAt = -1;
|
|
73
|
+
let validated = 0;
|
|
74
|
+
for (let k = 0; k < chunks; k++) {
|
|
75
|
+
const end = pool.join(handles[k]).bytes;
|
|
76
|
+
real = end;
|
|
77
|
+
done = k;
|
|
78
|
+
if (k === chunks - 1)
|
|
79
|
+
break;
|
|
80
|
+
if (!bytesEqual(end, log.starts[k + 1])) {
|
|
81
|
+
mismatchAt = k;
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
validated++;
|
|
85
|
+
}
|
|
86
|
+
const covered = Math.min((done + 1) * step, length);
|
|
87
|
+
const starts = usable ? log.starts.slice(0, done + 1) : [];
|
|
88
|
+
let state = real;
|
|
89
|
+
let fallbackBytes = 0;
|
|
90
|
+
if (covered < length) {
|
|
91
|
+
fallbackBytes = length - covered;
|
|
92
|
+
const task = submit(`ctxrec:${toHex(real)}:${step}`, covered, length);
|
|
93
|
+
// the speculative chunks after a mismatch are joined last so they do not delay the tail
|
|
94
|
+
const rest = pool.join(task).bytes;
|
|
95
|
+
const pieces = rest.length / width;
|
|
96
|
+
starts.push(real);
|
|
97
|
+
for (let p = 0; p < pieces - 1; p++)
|
|
98
|
+
starts.push(rest.slice(p * width, (p + 1) * width));
|
|
99
|
+
state = rest.slice((pieces - 1) * width);
|
|
100
|
+
}
|
|
101
|
+
for (let k = done + 1; k < chunks; k++) {
|
|
102
|
+
try {
|
|
103
|
+
pool.join(handles[k]);
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
// a speculative chunk that started from a forged state may trap; it is not part of the result
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
state,
|
|
111
|
+
log: { step, starts },
|
|
112
|
+
chunks,
|
|
113
|
+
tasks,
|
|
114
|
+
validated,
|
|
115
|
+
mismatchAt,
|
|
116
|
+
fallbackBytes,
|
|
117
|
+
stale: !usable
|
|
118
|
+
};
|
|
119
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { type FoldChunk } from '../execution/fold.ts';
|
|
2
|
+
import { type TaskLink, type TaskPool } from '../node/tasks.ts';
|
|
3
|
+
import { type AffineLayout } from './affine.ts';
|
|
4
|
+
import { type StreamCertificate } from './certify.ts';
|
|
5
|
+
/** The task name `serveCertified` answers: `certified:<start state in hex>`. */
|
|
6
|
+
export declare const CERTIFIED_TASK = "certified";
|
|
7
|
+
/** A module with its memory imported, and the bytes its first call writes into that memory. */
|
|
8
|
+
export interface SharedSubject {
|
|
9
|
+
/** The module with its memory imported as `env.memory` and its data segments passive. */
|
|
10
|
+
bytes: Uint8Array<ArrayBuffer>;
|
|
11
|
+
/** Data segments and the bytes the first call would write, in address order. */
|
|
12
|
+
prefill: {
|
|
13
|
+
address: number;
|
|
14
|
+
bytes: Uint8Array;
|
|
15
|
+
}[];
|
|
16
|
+
/** The size of the memory the module expects, in pages. */
|
|
17
|
+
pages: number;
|
|
18
|
+
certificate: StreamCertificate;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Rewrites a certified module to use caller-owned memory. Only the memory import and the data
|
|
22
|
+
* segments change, so the certificate still describes the functions.
|
|
23
|
+
*/
|
|
24
|
+
export declare function shareCertified(bytes: Uint8Array, certificate: StreamCertificate, pages: number): SharedSubject;
|
|
25
|
+
/** Writes the subject's prefill into `memory`, which must hold `pages` pages. */
|
|
26
|
+
export declare function installPrefill(subject: SharedSubject, memory: WebAssembly.Memory): void;
|
|
27
|
+
/** Worker side: runs `certified:<hex state>` tasks on the shared memory in place. */
|
|
28
|
+
export declare function serveCertified(link: TaskLink, subject: {
|
|
29
|
+
module: WebAssembly.Module;
|
|
30
|
+
memory: WebAssembly.Memory;
|
|
31
|
+
layout: AffineLayout;
|
|
32
|
+
}): void;
|
|
33
|
+
export interface CertifiedFoldOptions {
|
|
34
|
+
/** State before the first byte. The default is 0. */
|
|
35
|
+
initial?: bigint;
|
|
36
|
+
/** Explicit chunk ranges. The default splits the input into `pool.width` chunks. */
|
|
37
|
+
chunks?: FoldChunk[];
|
|
38
|
+
}
|
|
39
|
+
export interface CertifiedFoldResult {
|
|
40
|
+
state: bigint;
|
|
41
|
+
chunks: number;
|
|
42
|
+
tasks: number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Folds `input`, a view of `memory`, through the certified kernel on `pool`, whose workers must run
|
|
46
|
+
* `serveCertified` on that memory. Each chunk runs once from zero, in place; the certificate, not a
|
|
47
|
+
* comparison, makes the result equal the serial one, so the input must avoid the proof's memory.
|
|
48
|
+
*/
|
|
49
|
+
export declare function foldCertified(pool: TaskPool, input: Uint8Array, memory: WebAssembly.Memory, certificate: StreamCertificate, options?: CertifiedFoldOptions): CertifiedFoldResult;
|
|
50
|
+
/**
|
|
51
|
+
* Runs a certified kernel that rewrites its input in place, one call per chunk on `pool`. A chunk
|
|
52
|
+
* that ends in a state the model did not predict means the certificate does not match the kernel.
|
|
53
|
+
* The input is already rewritten by then, so this throws.
|
|
54
|
+
*/
|
|
55
|
+
export declare function reconstructCertified(pool: TaskPool, input: Uint8Array, memory: WebAssembly.Memory, certificate: StreamCertificate, options?: CertifiedFoldOptions): CertifiedFoldResult;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import binaryen from 'binaryen';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { chunkRanges } from "../execution/fold.js";
|
|
4
|
+
import { serveTasks } from "../node/tasks.js";
|
|
5
|
+
import { predictEnd } from "./affine.js";
|
|
6
|
+
import { guardInput } from "./certify.js";
|
|
7
|
+
/** The task name `serveCertified` answers: `certified:<start state in hex>`. */
|
|
8
|
+
export const CERTIFIED_TASK = 'certified';
|
|
9
|
+
const compactImports = binaryen.Features.CompactImports;
|
|
10
|
+
const PAGE = 65536;
|
|
11
|
+
/**
|
|
12
|
+
* Rewrites a certified module to use caller-owned memory. Only the memory import and the data
|
|
13
|
+
* segments change, so the certificate still describes the functions.
|
|
14
|
+
*/
|
|
15
|
+
export function shareCertified(bytes, certificate, pages) {
|
|
16
|
+
if (createHash('sha256').update(bytes).digest('hex') !== certificate.moduleSha256)
|
|
17
|
+
throw new Error('the certificate describes a different module');
|
|
18
|
+
const module = binaryen.readBinary(bytes);
|
|
19
|
+
try {
|
|
20
|
+
module.setFeatures(binaryen.Features.All & ~compactImports);
|
|
21
|
+
const memory = module.getMemoryInfo();
|
|
22
|
+
if (pages < memory.initial || pages > 65536)
|
|
23
|
+
throw new RangeError('pages out of range');
|
|
24
|
+
const prefill = [];
|
|
25
|
+
const count = module.getNumDataSegments();
|
|
26
|
+
for (let i = 0; i < count; i++) {
|
|
27
|
+
const seg = module.getDataSegmentInfo(module.getDataSegmentByIndex(i));
|
|
28
|
+
prefill.push({ address: seg.offset, bytes: new Uint8Array(seg.data) });
|
|
29
|
+
}
|
|
30
|
+
for (const w of certificate.initialization)
|
|
31
|
+
prefill.push({
|
|
32
|
+
address: w.address,
|
|
33
|
+
bytes: Uint8Array.from(w.hex.match(/../g) ?? [], (h) => parseInt(h, 16))
|
|
34
|
+
});
|
|
35
|
+
let text = module.emitText();
|
|
36
|
+
const declared = /\(memory (\$\S+) (\d+)(?: (\d+))?\)/.exec(text);
|
|
37
|
+
if (!declared)
|
|
38
|
+
throw new Error('the module has no plain memory declaration');
|
|
39
|
+
const imported = `(import "env" "memory" (memory ${declared[1]} ${declared[2]} ${pages} shared))`;
|
|
40
|
+
text = text
|
|
41
|
+
.replace(declared[0], '')
|
|
42
|
+
.replace(/^\(module[^\n]*\n/, (head) => `${head} ${imported}\n`);
|
|
43
|
+
text = text.replace(/\(data (\$\S+) \(i32\.const \d+\) /g, '(data $1 ');
|
|
44
|
+
const shared = binaryen.parseText(text);
|
|
45
|
+
try {
|
|
46
|
+
shared.setFeatures(binaryen.Features.All & ~compactImports);
|
|
47
|
+
if (!shared.validate())
|
|
48
|
+
throw new Error('the shared module does not validate');
|
|
49
|
+
return {
|
|
50
|
+
bytes: Uint8Array.from(shared.emitBinary()),
|
|
51
|
+
prefill,
|
|
52
|
+
pages,
|
|
53
|
+
certificate
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
shared.dispose();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
module.dispose();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** Writes the subject's prefill into `memory`, which must hold `pages` pages. */
|
|
65
|
+
export function installPrefill(subject, memory) {
|
|
66
|
+
if (memory.buffer.byteLength !== subject.pages * PAGE)
|
|
67
|
+
throw new RangeError('the memory is not the size the module was prepared for');
|
|
68
|
+
for (const part of subject.prefill)
|
|
69
|
+
new Uint8Array(memory.buffer, part.address, part.bytes.length).set(part.bytes);
|
|
70
|
+
}
|
|
71
|
+
/** Worker side: runs `certified:<hex state>` tasks on the shared memory in place. */
|
|
72
|
+
export function serveCertified(link, subject) {
|
|
73
|
+
const instance = new WebAssembly.Instance(subject.module, { env: { memory: subject.memory } });
|
|
74
|
+
const fn = instance.exports[subject.layout.entry];
|
|
75
|
+
const { layout } = subject;
|
|
76
|
+
serveTasks(link, (name, input) => {
|
|
77
|
+
const [task, hex] = name.split(':');
|
|
78
|
+
if (task !== CERTIFIED_TASK || !hex || typeof input === 'string')
|
|
79
|
+
throw new Error(`unknown task ${name}`);
|
|
80
|
+
const args = [0, 0, 0];
|
|
81
|
+
const state = BigInt(`0x${hex}`);
|
|
82
|
+
args[layout.state] =
|
|
83
|
+
layout.bits === 32 ? Number(BigInt.asIntN(32, state)) : BigInt.asIntN(64, state);
|
|
84
|
+
args[layout.ptr] = input.byteOffset;
|
|
85
|
+
args[layout.len] = input.length;
|
|
86
|
+
const end = BigInt.asUintN(layout.bits, BigInt(fn(...args)));
|
|
87
|
+
const out = new Uint8Array(8);
|
|
88
|
+
new DataView(out.buffer).setBigUint64(0, end, true);
|
|
89
|
+
return out;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Folds `input`, a view of `memory`, through the certified kernel on `pool`, whose workers must run
|
|
94
|
+
* `serveCertified` on that memory. Each chunk runs once from zero, in place; the certificate, not a
|
|
95
|
+
* comparison, makes the result equal the serial one, so the input must avoid the proof's memory.
|
|
96
|
+
*/
|
|
97
|
+
export function foldCertified(pool, input, memory, certificate, options = {}) {
|
|
98
|
+
if (certificate.output)
|
|
99
|
+
throw new RangeError('the kernel rewrites its input: use reconstructCertified');
|
|
100
|
+
const run = open(pool, input, memory, certificate, options);
|
|
101
|
+
if (!run.chunks)
|
|
102
|
+
return { state: run.whole(), chunks: 1, tasks: run.tasks() };
|
|
103
|
+
const handles = run.chunks.map((chunk) => run.submit(0n, chunk));
|
|
104
|
+
let state = run.initial;
|
|
105
|
+
run.chunks.forEach((chunk, i) => {
|
|
106
|
+
state = predictEnd(certificate.model, state, chunk.end - chunk.start, run.endOf(handles[i]));
|
|
107
|
+
});
|
|
108
|
+
return { state, chunks: run.chunks.length, tasks: run.tasks() };
|
|
109
|
+
}
|
|
110
|
+
/** Checks the input and the chunk ranges, and returns the calls that submit and join one chunk. */
|
|
111
|
+
function open(pool, input, memory, certificate, options) {
|
|
112
|
+
if (input.buffer !== memory.buffer)
|
|
113
|
+
throw new RangeError('the input is not a view of the memory');
|
|
114
|
+
if (!guardInput(certificate, memory.buffer.byteLength, input.byteOffset, input.length))
|
|
115
|
+
throw new RangeError('the input leaves the memory or overlaps what the certificate depends on');
|
|
116
|
+
const initial = BigInt.asUintN(certificate.model.bits, options.initial ?? 0n);
|
|
117
|
+
let tasks = 0;
|
|
118
|
+
const submit = (state, chunk) => {
|
|
119
|
+
tasks++;
|
|
120
|
+
return pool.submit(`${CERTIFIED_TASK}:${state.toString(16)}`, input.subarray(chunk.start, chunk.end), { share: true });
|
|
121
|
+
};
|
|
122
|
+
const endOf = (handle) => {
|
|
123
|
+
const part = pool.join(handle).bytes;
|
|
124
|
+
return new DataView(part.buffer, part.byteOffset).getBigUint64(0, true);
|
|
125
|
+
};
|
|
126
|
+
// the serial call itself, which also covers a zero length call that is not the identity
|
|
127
|
+
const whole = () => endOf(submit(initial, { start: 0, end: input.length }));
|
|
128
|
+
const base = { initial, submit, endOf, whole, tasks: () => tasks };
|
|
129
|
+
if (input.length === 0 || (options.chunks?.length ?? 0) === 1)
|
|
130
|
+
return { ...base, chunks: undefined };
|
|
131
|
+
const chunks = options.chunks ?? chunkRanges(input.length, pool.width, 1);
|
|
132
|
+
if (chunks.length === 1)
|
|
133
|
+
return { ...base, chunks: undefined };
|
|
134
|
+
let at = 0;
|
|
135
|
+
for (const [i, chunk] of chunks.entries()) {
|
|
136
|
+
if (chunk.start !== at || chunk.end <= chunk.start)
|
|
137
|
+
throw new RangeError(`chunk ${i} is not contiguous`);
|
|
138
|
+
at = chunk.end;
|
|
139
|
+
}
|
|
140
|
+
if (at !== input.length)
|
|
141
|
+
throw new RangeError(`chunks cover ${at} of ${input.length} bytes`);
|
|
142
|
+
return { ...base, chunks };
|
|
143
|
+
}
|
|
144
|
+
/** The state after `length` bytes from zero, for a step that ignores its byte. No kernel run. */
|
|
145
|
+
function zeroRun(certificate, length) {
|
|
146
|
+
const { model, output } = certificate;
|
|
147
|
+
let power = output.step;
|
|
148
|
+
let size = 1;
|
|
149
|
+
let result = 0n;
|
|
150
|
+
for (let n = length; n > 0; n = Math.floor(n / 2)) {
|
|
151
|
+
if (n % 2 === 1)
|
|
152
|
+
result = predictEnd(model, result, size, power);
|
|
153
|
+
power = predictEnd(model, power, size, power);
|
|
154
|
+
size *= 2;
|
|
155
|
+
}
|
|
156
|
+
return result;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Runs a certified kernel that rewrites its input in place, one call per chunk on `pool`. A chunk
|
|
160
|
+
* that ends in a state the model did not predict means the certificate does not match the kernel.
|
|
161
|
+
* The input is already rewritten by then, so this throws.
|
|
162
|
+
*/
|
|
163
|
+
export function reconstructCertified(pool, input, memory, certificate, options = {}) {
|
|
164
|
+
if (certificate.output?.step === undefined)
|
|
165
|
+
throw new RangeError('the certificate does not let chunks start without the ones before them');
|
|
166
|
+
const run = open(pool, input, memory, certificate, options);
|
|
167
|
+
if (!run.chunks)
|
|
168
|
+
return { state: run.whole(), chunks: 1, tasks: run.tasks() };
|
|
169
|
+
const starts = [run.initial];
|
|
170
|
+
for (const chunk of run.chunks)
|
|
171
|
+
starts.push(predictEnd(certificate.model, starts[starts.length - 1], chunk.end - chunk.start, zeroRun(certificate, chunk.end - chunk.start)));
|
|
172
|
+
const handles = run.chunks.map((chunk, i) => run.submit(starts[i], chunk));
|
|
173
|
+
handles.forEach((handle, i) => {
|
|
174
|
+
if (run.endOf(handle) !== starts[i + 1])
|
|
175
|
+
throw new Error(`chunk ${i} ended in a state the certificate did not predict`);
|
|
176
|
+
});
|
|
177
|
+
return { state: starts[starts.length - 1], chunks: run.chunks.length, tasks: run.tasks() };
|
|
178
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { AffineLayout, AffineModel } from './affine.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Ring of a form: `gf2` is affine over xor, `z2w` over add and multiply, and `zp` over two 16-bit
|
|
4
|
+
* digits with residues mod a constant (Adler-32 style).
|
|
5
|
+
*/
|
|
6
|
+
export type CertifiedRing = 'gf2' | 'z2w' | 'zp';
|
|
7
|
+
export interface CertifyOptions {
|
|
8
|
+
/** Parameter positions of the state, the pointer and the length. The default is 0, 1, 2. */
|
|
9
|
+
layout?: {
|
|
10
|
+
state: number;
|
|
11
|
+
ptr: number;
|
|
12
|
+
len: number;
|
|
13
|
+
};
|
|
14
|
+
/** Lengths below this run concretely, the rest as quotient and remainder. Default: 4, then 16. */
|
|
15
|
+
block?: number;
|
|
16
|
+
/** Wall clock limit of the whole call across the block ladder, in milliseconds. Default 30000. */
|
|
17
|
+
maxMs?: number;
|
|
18
|
+
/** Restricts the analysis to one ring. The default tries `gf2`, then `z2w`, then `zp`. */
|
|
19
|
+
ring?: CertifiedRing;
|
|
20
|
+
}
|
|
21
|
+
export interface CertificateRange {
|
|
22
|
+
address: number;
|
|
23
|
+
length: number;
|
|
24
|
+
sha256: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The byte transform of a call: byte `i` of the range becomes a fixed function of the input byte
|
|
28
|
+
* and the state before it. `step` is its result from zero when the byte is ignored.
|
|
29
|
+
*/
|
|
30
|
+
export interface CertificateOutput {
|
|
31
|
+
inPlace: true;
|
|
32
|
+
step?: bigint;
|
|
33
|
+
}
|
|
34
|
+
export interface CertificateWrite {
|
|
35
|
+
address: number;
|
|
36
|
+
hex: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* A proof that `entry(state, ptr, len)` is the left fold of one affine byte step, for every length
|
|
40
|
+
* of at least one byte. It holds for a module with hash `moduleSha256`, clear of `exclusions`.
|
|
41
|
+
*/
|
|
42
|
+
export interface StreamCertificate {
|
|
43
|
+
kind: 'certificate';
|
|
44
|
+
schema: 'folio.certificate.v1';
|
|
45
|
+
checker: string;
|
|
46
|
+
moduleSha256: string;
|
|
47
|
+
entry: string;
|
|
48
|
+
ring: CertifiedRing;
|
|
49
|
+
bits: 32 | 64;
|
|
50
|
+
block: number;
|
|
51
|
+
layout: AffineLayout;
|
|
52
|
+
model: AffineModel;
|
|
53
|
+
/** Present when the call also rewrites its input, which `CertificateOutput` describes. */
|
|
54
|
+
output?: CertificateOutput;
|
|
55
|
+
/** What a call over zero bytes returns: the state itself, or a function of it. */
|
|
56
|
+
zeroLength: 'identity' | 'transforms';
|
|
57
|
+
/** Memory the proof read, as it must be when the call starts. */
|
|
58
|
+
image: CertificateRange[];
|
|
59
|
+
/** Bytes the first call writes before it reads them, for modules that build a table lazily. */
|
|
60
|
+
initialization: CertificateWrite[];
|
|
61
|
+
/** Byte ranges, start inclusive and end exclusive, that an input must not overlap. */
|
|
62
|
+
exclusions: [number, number][];
|
|
63
|
+
stats: {
|
|
64
|
+
paths: number;
|
|
65
|
+
loops: number;
|
|
66
|
+
expressions: number;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
export interface CertificateRefusal {
|
|
70
|
+
kind: 'refusal';
|
|
71
|
+
reason: string;
|
|
72
|
+
attempts: {
|
|
73
|
+
ring: CertifiedRing;
|
|
74
|
+
reason: string;
|
|
75
|
+
}[];
|
|
76
|
+
}
|
|
77
|
+
export declare const CHECKER = "folio-stream-certifier-2";
|
|
78
|
+
/**
|
|
79
|
+
* Decides whether `entry(state, ptr, len)` in `bytes` is the byte fold of one affine step, for
|
|
80
|
+
* every length of at least one byte. Lengths below `block` run one by one, the rest as `block*q+r`.
|
|
81
|
+
* A refusal says nothing about the function.
|
|
82
|
+
*/
|
|
83
|
+
export declare function certifyStream(bytes: Uint8Array, entry: string, options?: CertifyOptions): StreamCertificate | CertificateRefusal;
|
|
84
|
+
/** Re-derives the certificate from the module and compares everything except the work counters. */
|
|
85
|
+
export declare function verifyCertificate(bytes: Uint8Array, certificate: StreamCertificate): boolean;
|
|
86
|
+
/** Whether `[ptr, ptr + len)` lies in memory and clear of every byte the proof depends on. */
|
|
87
|
+
export declare function guardInput(certificate: StreamCertificate, memoryBytes: number, pointer: number, length: number): boolean;
|