| #!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| import { readFileSync, writeFileSync, existsSync, appendFileSync } from 'fs';
|
| import { join, resolve } from 'path';
|
| import { createHash } from 'crypto';
|
|
|
| const ROOT = resolve(import.meta.dirname, '..', '..');
|
| const CONV_LOG = join(ROOT, '.agentos', 'pnp', 'convergence_log.jsonl');
|
| const CHAIN_LOG = join(ROOT, '.agentos', 'pnp', 'seb_chain.jsonl');
|
|
|
|
|
| const EVENT_PROBLEM_SOLVED = 0x0400n;
|
| const EVENT_ATTACK_DETECTED = 0x0401n;
|
| const EVENT_CHAIN_VERIFY = 0x0402n;
|
|
|
|
|
| let tip = Buffer.alloc(32, 0);
|
|
|
|
|
| if (existsSync(CHAIN_LOG)) {
|
| const lines = readFileSync(CHAIN_LOG, 'utf8').split('\n').filter(l => l.trim());
|
| if (lines.length > 0) {
|
| const last = JSON.parse(lines[lines.length - 1]);
|
| tip = Buffer.from(last.commitment, 'hex');
|
| }
|
| }
|
|
|
|
|
| function gf256_mul(x, y) {
|
| let z = 0;
|
| for (let i = 0; i < 8; i++) {
|
| if (y & 1) z ^= x;
|
| const hi = x & 0x80;
|
| x = (x << 1) & 0xFF;
|
| if (hi) x ^= 0x1B;
|
| y >>>= 1;
|
| }
|
| return z;
|
| }
|
|
|
|
|
| function cyclic_convolve(a, b) {
|
| const c = Buffer.alloc(32);
|
| for (let k = 0; k < 32; k++) {
|
| let s = 0;
|
| for (let i = 0; i < 32; i++) s ^= gf256_mul(a[i], b[(k - i + 32) & 31]);
|
| c[k] = s;
|
| }
|
| return c;
|
| }
|
|
|
|
|
| const K0 = Buffer.alloc(32); K0[0] = 1;
|
| const K1 = Buffer.alloc(32); K1[1] = 1;
|
| const K2 = Buffer.alloc(32); K2[2] = 1;
|
|
|
|
|
|
|
| function circuit(prev32, payload64) {
|
| const b = payload64.slice(0, 32);
|
| const c = payload64.slice(32, 64);
|
| const t0 = cyclic_convolve(K0, prev32);
|
| const t1 = cyclic_convolve(K1, b);
|
| const t2 = cyclic_convolve(K2, c);
|
| const next = Buffer.alloc(32);
|
| for (let i = 0; i < 32; i++) next[i] = t0[i] ^ t1[i] ^ t2[i];
|
| return next;
|
| }
|
|
|
|
|
| function buildPayload(entry) {
|
| const buf = Buffer.alloc(64, 0);
|
|
|
| const delta = entry.universeSumDelta || 0;
|
| const eventType = delta < 0 ? EVENT_ATTACK_DETECTED : EVENT_PROBLEM_SOLVED;
|
|
|
|
|
| buf.writeBigUInt64LE(eventType, 0);
|
|
|
|
|
| const ts = BigInt(new Date(entry.timestamp || new Date()).getTime()) * 1_000_000n;
|
| buf.writeBigUInt64LE(ts, 8);
|
|
|
|
|
| const pidHash = createHash('sha256').update(entry.problemId || '').digest();
|
| pidHash.copy(buf, 16);
|
|
|
|
|
| buf.writeDoubleLE(delta, 48);
|
|
|
|
|
| return buf;
|
| }
|
|
|
|
|
| function appendToChain(entry) {
|
| const payload = buildPayload(entry);
|
| const commitment = circuit(tip, payload);
|
|
|
| const record = {
|
| n: existsSync(CHAIN_LOG)
|
| ? readFileSync(CHAIN_LOG,'utf8').split('\n').filter(l=>l.trim()).length
|
| : 0,
|
| event: entry.event,
|
| problemId: entry.problemId,
|
| solver: entry.solver || null,
|
| delta: entry.universeSumDelta || 0,
|
| timestamp: entry.timestamp || new Date().toISOString(),
|
| payload: payload.toString('hex'),
|
| commitment: commitment.toString('hex'),
|
| prev_tip: tip.toString('hex')
|
| };
|
|
|
| appendFileSync(CHAIN_LOG, JSON.stringify(record) + '\n');
|
| tip = commitment;
|
| return record;
|
| }
|
|
|
|
|
| function verifyChain() {
|
| if (!existsSync(CHAIN_LOG)) return { ok: true, count: 0 };
|
| const lines = readFileSync(CHAIN_LOG, 'utf8').split('\n').filter(l => l.trim());
|
| let expectedTip = Buffer.alloc(32, 0);
|
| for (let i = 0; i < lines.length; i++) {
|
| const rec = JSON.parse(lines[i]);
|
| const payload = Buffer.from(rec.payload, 'hex');
|
| const computed = circuit(expectedTip, payload);
|
| const stored = Buffer.from(rec.commitment, 'hex');
|
| if (!computed.equals(stored)) {
|
| return { ok: false, broken_at: i, expected: computed.toString('hex'), got: stored.toString('hex') };
|
| }
|
| expectedTip = computed;
|
| }
|
| return { ok: true, count: lines.length, tip: expectedTip.toString('hex') };
|
| }
|
|
|
|
|
| function run() {
|
| if (!existsSync(CONV_LOG)) {
|
| console.log('No convergence log. Nothing to seal.');
|
| return;
|
| }
|
|
|
|
|
| const sealed = new Set();
|
| if (existsSync(CHAIN_LOG)) {
|
| readFileSync(CHAIN_LOG, 'utf8').split('\n').filter(l => l.trim())
|
| .forEach(l => {
|
| const r = JSON.parse(l);
|
| sealed.add(`${r.problemId}:${r.timestamp}`);
|
| });
|
| }
|
|
|
| const entries = readFileSync(CONV_LOG, 'utf8').split('\n')
|
| .filter(l => l.trim()).map(l => JSON.parse(l));
|
|
|
| let appended = 0;
|
| let attacks = 0;
|
|
|
| for (const entry of entries) {
|
| const key = `${entry.problemId}:${entry.timestamp}`;
|
| if (sealed.has(key)) continue;
|
|
|
| const record = appendToChain(entry);
|
| appended++;
|
|
|
| const delta = entry.universeSumDelta || 0;
|
| if (delta < 0) {
|
| attacks++;
|
| console.log(`โ ATTACK EVENT sealed: ${entry.problemId} delta=${delta}`);
|
| console.log(` commitment: ${record.commitment}`);
|
| } else {
|
| console.log(`โ Sealed: ${entry.problemId} delta=+${delta}`);
|
| }
|
| }
|
|
|
| if (appended === 0) {
|
| console.log('Chain up to date. Nothing new to seal.');
|
| }
|
|
|
|
|
| const result = verifyChain();
|
| if (!result.ok) {
|
| console.error(`\nโ CHAIN INTEGRITY FAILURE at record ${result.broken_at}`);
|
| console.error(` Expected: ${result.expected}`);
|
| console.error(` Got: ${result.got}`);
|
| console.error(' Chain is tampered. Halting.');
|
| process.exit(1);
|
| }
|
|
|
| const universeSum = entries.reduce((s, e) => s + (e.universeSumDelta || 0), 0);
|
| console.log(`\n๐ Universe sum: ${universeSum.toFixed(6)}`);
|
| console.log(`๐ Chain records: ${result.count || 0}`);
|
| console.log(`โ Attack events: ${attacks}`);
|
| console.log(`๐ Tip: ${tip.toString('hex').slice(0, 16)}...`);
|
| console.log(`\nโ
SEB chain: VERIFIED`);
|
|
|
| if (attacks > 0 && universeSum < 0) {
|
| console.error('\nโ NEGATIVE UNIVERSE SUM โ active attack condition. Investigate.');
|
| process.exit(2);
|
| }
|
| }
|
|
|
| run();
|
|
|