Add verify-layer (an audit-log sidecar whose head can be notarized on Aere Network for post-quantum finality) and proof-kinds (the AIP-23 envelope builder it uses)
This commit is contained in:
1 parent
2a2ed74d32
commit
3465550e91
12 files changed
+1041
-3
No files matched your search
@@ -0,0 +1,21 @@
|
||||
# proof-kinds: one builder for every AIP-23 proof kind
|
||||
|
||||
A generic builder of AERE Proof Protocol envelopes (AIP-23) for the standard proof kinds: `data`, `execution`, `identity`,
|
||||
`compliance`, `runtime`, `location`, `device`, `payment`, `ownership`, `time`, `block`, `authorization`, `settlement` and
|
||||
`provenance`. Every kind produces the same envelope (`statement` plus `statementHash`, the SHA-256 of the canonical statement
|
||||
text), so the same AIP-23 reference verifier checks all of them with no kind-specific verification code.
|
||||
|
||||
```
|
||||
import { buildProof, KINDS } from './proof-kinds.mjs';
|
||||
const envelope = buildProof('runtime', { host, artifactContent, attested, matches: true, createdAt });
|
||||
```
|
||||
|
||||
A kind is a schema: its required fields, some of them digests, plus `createdAt`. Raw content given for a digest field is hashed
|
||||
(bytes as they are, strings as UTF-8, other values as their JSON text), and a value that is already a 32-byte digest is kept, so
|
||||
an envelope carries digests, never the raw content. A missing required field stops the build with an error instead of producing
|
||||
an incomplete envelope. Notarization and post-quantum finality come from AIP-23, not from this builder.
|
||||
|
||||
Test: `node proba-proof-kinds.mjs` (24 checks: every kind built and found `VALID` by the reference verifier, six negative
|
||||
controls such as content changed after hashing or a required field missing, no raw content in any envelope, bytes hashed as
|
||||
bytes). It needs the AIP-23 reference verifier: `AERE_VERIFY_PROOF=<path to verify-proof.mjs>` (from the `aere-node`
|
||||
repository, after `npm install` there); without it the test reports that it did not measure and exits with 2. Node.js 24.
|
||||
@@ -0,0 +1,75 @@
|
||||
'use strict';
|
||||
// Proba "Proof of everything": pentru FIECARE fel construieste un plic, il verifica cu ACELASI verificator AIP-23 (zero cod nou), si
|
||||
// controale negative: continut atins dupa hash -> INVALID; camp obligatoriu lipsa -> eroare la constructie; niciun continut brut in plic.
|
||||
// node proba-proof-kinds.mjs -> 0 toate cum trebuia, 1 altfel
|
||||
|
||||
import fs from 'node:fs'; import crypto from 'node:crypto'; import os from 'node:os'; import path from 'node:path';
|
||||
import { execFileSync } from 'node:child_process'; import { fileURLToPath } from 'node:url';
|
||||
import { buildProof, KINDS } from './proof-kinds.mjs';
|
||||
|
||||
const AICI = path.dirname(fileURLToPath(import.meta.url));
|
||||
const VERIFY = process.env.AERE_VERIFY_PROOF ? path.resolve(process.env.AERE_VERIFY_PROOF) : path.resolve(AICI, '..', 'aere-proof-protocol', 'verify.mjs');
|
||||
if (!fs.existsSync(VERIFY)) { console.log(`NEMASURAT: verificatorul AIP-23 nu e la ${VERIFY}; dati AERE_VERIFY_PROOF (de ex. verify-proof.mjs din aere-node/tools)`); process.exit(2); }
|
||||
const T = fs.mkdtempSync(path.join(os.tmpdir(), 'pk-'));
|
||||
let rele = 0; const cer = (n, c) => { console.log(` [${c ? 'OK ' : 'RAU '}] ${n}`); if (!c) rele++; };
|
||||
function verdict(plicPath) { try { return JSON.parse(execFileSync(process.execPath, [VERIFY, plicPath, '--json'], { encoding: 'utf8' })).verdict; } catch (e) { try { return JSON.parse((e.stdout || '')).verdict; } catch { return '?'; } } }
|
||||
|
||||
// intrari de proba pentru fiecare fel (continut brut, ca sa verific ca nu se scurge)
|
||||
const BRUT = 'CONTINUT-BRUT-BRUT-42';
|
||||
const INTRARI = {
|
||||
data: { name: 'raport.csv', content: BRUT, createdAt: '2026-09-26T09:00:00Z' },
|
||||
execution: { program: 'aere-node', input: BRUT, output: BRUT + 'o', exitCode: 0, createdAt: '2026-09-26T09:00:00Z' },
|
||||
identity: { subjectId: 'agent-1', publicKey: BRUT, method: 'ml-dsa-65', createdAt: '2026-09-26T09:00:00Z' },
|
||||
compliance: { subject: 'org-x', policy: 'pq-ready', result: 'pass', evidence: BRUT, createdAt: '2026-09-26T09:00:00Z' },
|
||||
runtime: { host: 'h1', artifactContent: BRUT, attested: BRUT, matches: true, createdAt: '2026-09-26T09:00:00Z' },
|
||||
location: { subject: 'srv-eu', region: 'eu-central', evidence: BRUT, createdAt: '2026-09-26T09:00:00Z' },
|
||||
device: { deviceId: 'dev-9', attestation: BRUT, publicKey: BRUT, posture: 'secure-boot', createdAt: '2026-09-26T09:00:00Z' },
|
||||
payment: { from: '0xa', to: '0xb', amount: '100', asset: 'AERE', tx: BRUT, createdAt: '2026-09-26T09:00:00Z' },
|
||||
ownership: { owner: '0xowner', assetId: 'nft-7', asset: BRUT, createdAt: '2026-09-26T09:00:00Z' },
|
||||
time: { subject: BRUT, source: 'aere-anchor', at: '2026-09-26T09:00:00Z', createdAt: '2026-09-26T09:00:00Z' },
|
||||
block: { blockHash: '0x' + 'ab'.repeat(32), stateRoot: '0x' + 'cd'.repeat(32), anchorHeight: 20255488, certificateDigest: '0x' + 'ef'.repeat(32), createdAt: '2026-09-26T09:00:00Z' },
|
||||
authorization: { grantor: 'org-x', grantee: 'agent-7', scope: 'payments:send<=100', policy: BRUT, expiresAt: '2026-12-31T00:00:00Z', createdAt: '2026-09-28T09:00:00Z' },
|
||||
settlement: { chainId: 2800, txHash: '0x' + '12'.repeat(32), blockHash: '0x' + '34'.repeat(32), from: '0xa', to: '0xb', amount: '100', asset: 'AERE', instruction: BRUT, createdAt: '2026-09-28T09:00:00Z' },
|
||||
provenance: { subject: BRUT, parents: [BRUT + '1', BRUT + '2'], process: BRUT + 'p', actor: 'build-bot', createdAt: '2026-09-28T09:00:00Z' },
|
||||
};
|
||||
|
||||
try {
|
||||
cer('schema acopera cele 14 feluri (10 + bloc + autorizare, decontare, provenienta)', KINDS.length >= 14 && ['authorization', 'settlement', 'provenance'].every((k) => KINDS.includes(k)));
|
||||
for (const k of KINDS) {
|
||||
// fiecare fel se judeca separat: un fel care nu se mai poate construi e un RAU numit, nu o exceptie care opreste proba (2026-09-28)
|
||||
let plic; try { plic = buildProof(k, INTRARI[k]); } catch (e) { cer(`${k}: constructia a cazut (${e.message})`, false); continue; }
|
||||
const f = path.join(T, k + '.json'); fs.writeFileSync(f, JSON.stringify(plic, null, 1));
|
||||
const v = verdict(f);
|
||||
const faraBrut = !JSON.stringify(plic).includes(BRUT);
|
||||
cer(`${k}: verificatorul AIP-23 il valideaza si nu contine continut brut`, v === 'VALID' && faraBrut);
|
||||
}
|
||||
// CONTROL NEGATIV 1: statement atins dupa hash -> INVALID (pe 'data')
|
||||
const p = buildProof('data', INTRARI.data); p.statement.name = 'ALTCEVA';
|
||||
const fr = path.join(T, 'rau.json'); fs.writeFileSync(fr, JSON.stringify(p, null, 1));
|
||||
cer('CONTROL: statement schimbat dupa hash -> INVALID', verdict(fr) === 'INVALID');
|
||||
// CONTROL NEGATIV 2: camp obligatoriu lipsa -> eroare la constructie
|
||||
let aAruncat = false; try { buildProof('data', { name: 'x', createdAt: '2026-09-26T09:00:00Z' }); } catch { aAruncat = true; }
|
||||
cer('CONTROL: camp obligatoriu lipsa (sha256) -> eroare la constructie', aAruncat);
|
||||
// CONTROL NEGATIV 3: fel necunoscut -> eroare
|
||||
let aAruncat2 = false; try { buildProof('inexistent', { createdAt: '2026-09-26T09:00:00Z' }); } catch { aAruncat2 = true; }
|
||||
cer('CONTROL: fel necunoscut -> eroare', aAruncat2);
|
||||
// 2026-09-28: felurile noi isi refuza formele gresite la constructie, nu produc un plic gresit
|
||||
const arunca = (f) => { try { f(); return false; } catch { return true; } };
|
||||
cer('CONTROL: decontare cu txHash care nu e hash de 32 de octeti -> eroare', arunca(() => buildProof('settlement', { ...INTRARI.settlement, txHash: '0x1234' })));
|
||||
cer('CONTROL: provenienta cu lista de parinti goala -> eroare', arunca(() => buildProof('provenance', { ...INTRARI.provenance, parents: [] })));
|
||||
cer('CONTROL: autorizare fara scope -> eroare', arunca(() => buildProof('authorization', { ...INTRARI.authorization, scope: undefined })));
|
||||
const pv = buildProof('provenance', INTRARI.provenance);
|
||||
cer('provenienta: fiecare parinte e digestul continutului lui, in ordine', pv.statement.parents.length === 2 && pv.statement.parents[0] === '0x' + crypto.createHash('sha256').update(BRUT + '1', 'utf8').digest('hex'));
|
||||
// OCTETII unui Buffer (2026-09-27): digestul continutului dat ca Buffer = sha256 al octetilor (cel pe care il reface sha256sum pe
|
||||
// fisier) = digestul aceluiasi continut dat ca sir. Pana azi un Buffer se hashuia ca JSON {"type":"Buffer",...}.
|
||||
const oct = Buffer.from('ARTEFACT-DESFASURAT-\u0000\u00ff', 'latin1');
|
||||
const asteptat = '0x' + crypto.createHash('sha256').update(oct).digest('hex');
|
||||
const pb = buildProof('data', { name: 'a', content: oct, createdAt: '2026-09-26T09:00:00Z' });
|
||||
const pu = buildProof('data', { name: 'a', content: new Uint8Array(oct), createdAt: '2026-09-26T09:00:00Z' });
|
||||
const ps = buildProof('data', { name: 'a', content: 'abc', createdAt: '2026-09-26T09:00:00Z' });
|
||||
cer('Buffer/Uint8Array: digestul = sha256 al octetilor (reproductibil cu sha256sum)', pb.statement.sha256 === asteptat && pu.statement.sha256 === asteptat);
|
||||
cer('sirurile raman ca inainte (vectorii publicati neschimbati)', ps.statement.sha256 === '0x' + crypto.createHash('sha256').update('abc', 'utf8').digest('hex'));
|
||||
} finally { fs.rmSync(T, { recursive: true, force: true }); }
|
||||
const total = KINDS.length + 10;
|
||||
console.log(`\nProof of everything: ${total - rele}/${total} cum trebuia (${KINDS.length} feluri + 6 controale negative + acoperire + Buffer + parinti)`);
|
||||
process.exit(rele ? 1 : 0);
|
||||
@@ -0,0 +1,86 @@
|
||||
'use strict';
|
||||
// AERE Proof of everything (roadmap #56-66): un constructor generic de plicuri AERE Proof Protocol (AIP-23) pentru felurile de
|
||||
// dovada standard - date, executie, identitate, conformitate, runtime, locatie, dispozitiv, plata, proprietate, timp, bloc, si din
|
||||
// 2026-09-28 autorizare, decontare, provenienta. Toate produc
|
||||
// ACELASI plic (statement + statementHash SHA-256 al textului canonic) pe care il verifica ACELASI verificator (tools/aere-proof-
|
||||
// protocol/verify.mjs), fara niciun cod nou de verificare. Notarizarea + finalitatea post-cuantica vin din AIP-23. Numai Node 24.
|
||||
//
|
||||
// Un fel de dovada = o schema: campurile obligatorii (unele DIGESTURI, ca nimic sensibil sa nu intre brut) + createdAt. Constructorul
|
||||
// hashuieste continutul brut daca i se da, sau accepta un digest gata (0x+64). Un camp obligatoriu lipsa -> eroare (nu un plic gol).
|
||||
|
||||
import crypto from 'node:crypto';
|
||||
|
||||
const sha256 = (b) => '0x' + crypto.createHash('sha256').update(b).digest('hex');
|
||||
export const isDigest = (s) => typeof s === 'string' && /^0x[0-9a-fA-F]{64}$/.test(s);
|
||||
// hashuieste un continut in digest; daca e deja digest, il pastreaza
|
||||
// OCTETII unui Buffer/Uint8Array se hashuiesc ca atare. Platit 2026-09-27: un Buffer trecea prin JSON.stringify si se hashuia
|
||||
// forma {"type":"Buffer","data":[...]}, deci digestul unui artefact dat ca fisier (sidecar record --kind deployment) nu se mai
|
||||
// putea reface din fisier cu sha256sum. Sirurile si obiectele raman exact ca inainte (vectorii AIP-23 publicati nu se schimba).
|
||||
const dg = (v) => (v == null ? null : (isDigest(v) ? v.toLowerCase()
|
||||
: (Buffer.isBuffer(v) || v instanceof Uint8Array) ? sha256(Buffer.from(v))
|
||||
: sha256(Buffer.from(typeof v === 'string' ? v : JSON.stringify(v), 'utf8'))));
|
||||
|
||||
// schema fiecarui fel: campuri de tip 'digest' (se hashuiesc), 'plain' (raman ca atare), 'bool', 'list'. Toate obligatorii daca in `req`.
|
||||
export const SCHEME = {
|
||||
'data': { digest: { sha256: 'content' }, plain: { name: 'subject' }, req: ['sha256', 'name'] },
|
||||
'execution': { digest: { inputHash: 'input', outputHash: 'output', programSha256: 'programContent' }, plain: { program: 'program', exitCode: 'exitCode' }, req: ['program', 'inputHash', 'outputHash'] },
|
||||
'identity': { digest: { publicKeyHash: 'publicKey', subjectHash: 'subject' }, plain: { method: 'method', subjectId: 'subjectId' }, req: ['subjectId', 'publicKeyHash'] },
|
||||
'compliance': { digest: { evidenceHash: 'evidence' }, plain: { subject: 'subject', policy: 'policy', result: 'result' }, req: ['subject', 'policy', 'result'] },
|
||||
'runtime': { digest: { artifactSha256: 'artifactContent', attestedSha256: 'attested' }, plain: { host: 'host', unit: 'unit', matches: 'matches' }, req: ['host', 'artifactSha256'] },
|
||||
'location': { digest: { evidenceHash: 'evidence' }, plain: { subject: 'subject', region: 'region', method: 'method' }, req: ['subject', 'region'] },
|
||||
'device': { digest: { attestationHash: 'attestation', publicKeyHash: 'publicKey' }, plain: { deviceId: 'deviceId', posture: 'posture' }, req: ['deviceId', 'attestationHash'] },
|
||||
'payment': { digest: { txHash: 'tx' }, plain: { from: 'from', to: 'to', amount: 'amount', asset: 'asset' }, req: ['from', 'to', 'amount'] },
|
||||
'ownership': { digest: { assetHash: 'asset' }, plain: { owner: 'owner', assetId: 'assetId' }, req: ['owner', 'assetId'] },
|
||||
'time': { digest: { subjectHash: 'subject' }, plain: { source: 'source', at: 'at' }, req: ['subjectHash', 'at'] },
|
||||
// A4: plicul de consens al unui bloc (blockHash, stateRoot ancorate de certificatul de ancora PQ). Dovada de executie per bloc
|
||||
// (stateTransitionProof) e optionala si vine cand exista proverul; fara ea, plicul poarta increderea CONSENSULUI, nu a executiei.
|
||||
'block': { digest: { stateTransitionProof: 'stateTransitionProof' }, plain: { blockHash: 'blockHash', stateRoot: 'stateRoot', anchorHeight: 'anchorHeight', certificateDigest: 'certificateDigest' }, req: ['blockHash', 'stateRoot', 'certificateDigest'] },
|
||||
// 2026-09-28 (roadmap master, punctul 11): cele trei feluri numite in plan care lipseau. `hash` = campuri care SUNT deja un hash de
|
||||
// 32 de octeti (0x+64 hex; o valoare de alta forma e refuzata, nu hashuita), `list` = o lista nevida de digesturi (fiecare element
|
||||
// brut se hashuieste, un digest ramane). Felurile vechi nu au nici una, deci plicurile si vectorii lor raman octet cu octet aceiasi.
|
||||
// authorization: cine (grantor) a dat cui (grantee) dreptul de a face ce (scope), sub ce politica, pana cand
|
||||
'authorization': { digest: { policyHash: 'policy' }, plain: { grantor: 'grantor', grantee: 'grantee', scope: 'scope', expiresAt: 'expiresAt' }, req: ['grantor', 'grantee', 'scope'] },
|
||||
// settlement: o decontare incheiata pe un lant: tranzactia, blocul care o contine, suma; finalitatea blocului o da AIP-23 (notarizare
|
||||
// + ancora) sau clientul usor PQ (interop_core::pq), nu plicul
|
||||
'settlement': { digest: { instructionHash: 'instruction' }, hash: ['txHash', 'blockHash'], plain: { chainId: 'chainId', from: 'from', to: 'to', amount: 'amount', asset: 'asset' }, req: ['chainId', 'txHash', 'blockHash', 'amount', 'asset'] },
|
||||
// provenance: din ce a iesit un artefact (parents), prin ce proces, cine l-a produs
|
||||
'provenance': { digest: { subjectHash: 'subject', processHash: 'process' }, list: { parents: 'parentsContent' }, plain: { actor: 'actor' }, req: ['subjectHash', 'parents'] },
|
||||
};
|
||||
export const KINDS = Object.keys(SCHEME);
|
||||
|
||||
/**
|
||||
* Construieste un plic AIP-23 pentru un fel de dovada.
|
||||
* @param {string} kind unul din KINDS
|
||||
* @param {object} p campurile (brute sau digesturi); vezi SCHEME
|
||||
* @returns {object} plicul { v, kind: 'aere-proof-of-<kind>-attestation', statement, statementHash }
|
||||
*/
|
||||
export function buildProof(kind, p = {}) {
|
||||
const s = SCHEME[kind];
|
||||
if (!s) throw new Error('proof-kinds: unknown kind "' + kind + '" (' + KINDS.join(', ') + ')');
|
||||
if (!p.createdAt) throw new Error('proof-kinds: createdAt is required (RFC 3339)');
|
||||
const statement = { v: 1, kind: 'aere-proof-of-' + kind };
|
||||
// campuri digest: se hashuiesc din sursa (numele sursei din schema) sau se iau gata din campul de digest
|
||||
for (const [camp, sursa] of Object.entries(s.digest || {})) {
|
||||
statement[camp] = p[camp] !== undefined ? dg(p[camp]) : dg(p[sursa]);
|
||||
}
|
||||
for (const camp of s.hash || []) {
|
||||
if (p[camp] === undefined || p[camp] === null) continue;
|
||||
if (!isDigest(p[camp])) throw new Error('proof-kinds: ' + kind + ' needs "' + camp + '" to be a 32-byte hash (0x + 64 hex)');
|
||||
statement[camp] = p[camp].toLowerCase();
|
||||
}
|
||||
for (const [camp, sursa] of Object.entries(s.list || {})) {
|
||||
const v = p[camp] !== undefined ? p[camp] : p[sursa];
|
||||
if (v === undefined || v === null) continue;
|
||||
if (!Array.isArray(v) || v.length === 0) throw new Error('proof-kinds: ' + kind + ' needs "' + camp + '" to be a non-empty list');
|
||||
statement[camp] = v.map(dg);
|
||||
}
|
||||
for (const [camp] of Object.entries(s.plain || {})) {
|
||||
if (p[camp] !== undefined) statement[camp] = p[camp];
|
||||
}
|
||||
statement.createdAt = p.createdAt;
|
||||
for (const r of s.req) {
|
||||
if (statement[r] === undefined || statement[r] === null) throw new Error('proof-kinds: ' + kind + ' needs the field "' + r + '"');
|
||||
}
|
||||
const text = JSON.stringify(statement); // canonic AIP-23
|
||||
return { v: 1, kind: 'aere-proof-of-' + kind + '-attestation', statement, statementHash: sha256(Buffer.from(text, 'utf8')) };
|
||||
}
|
||||
Reference in new issue
Block a user