conformance: 97 rules of 9 verifiers, 206 vectors; verify-layer and agents refuse a classical key under an ML-DSA-65 name; market-settlement reads the notarization time from the certified read; proof-of-ai README in English

This commit is contained in:
Aere Network committed 2026-10-05 17:17:59 +03:00
1 parent c9f2932e04
commit c9798a0013
111 files changed
+15871 -292

No files matched your search

+43 -33
View File
@@ -90,6 +90,7 @@ export function sessionId(agentId, policyHash) { return sha(`aere-agent-ledger-s
const ceasulLocal = () => Math.floor(Date.now() / 1000);
function pregateste({ identity, policy: politicaData, policyHash: hashDat }) {
if (!identity || !identity.privateKey || !identity.publicKey) throw new Error('agent-ledger: an identity with privateKey and publicKey is required');
if (identity.publicKey.asymmetricKeyType !== ALG || identity.privateKey.asymmetricKeyType !== ALG) throw new Error('agent-ledger: the identity key is not ML-DSA-65');
if (identity.agentId !== agentIdFromKey(identity.publicKey)) throw new Error('agent-ledger: agentId is not derived from the identity key');
const { policy, policyHash } = hashPolicy(politicaData);
if (hashDat != null && String(hashDat).toLowerCase() !== policyHash) throw new Error('agent-ledger: the policy does not hash to the policyHash given');
@@ -214,32 +215,41 @@ function registru(identity, { policy, policyHash }, now, entries, revocatInitial
* @param {number} [o.anchorTolerance] cat poate intarzia martorul fata de scriere (implicit TOLERANTA_S)
* @returns {{ok:boolean, seq?:number, error?:string, allowedPayments?:number, spent?:string, humanApproved?:number, revocations:object, time:object}}
*/
/** The rules verifyLedger names: every refusal carries `rule`, one of these (the helper throws on any other). Each check of a rule
* carries its marker, so that a conformance suite can take the rule out, in a copy, and see which of its vectors then pass. */
export const REGULI = ['registru', 'politica', 'alta-politica', 'sesiune', 'cheie', 'identitate', 'ancora', 'seq', 'lant', 'corp', 'semnatura', 'hash', 'timp', 'revocare', 'aprobari', 'decizie'];
export function verifyLedger(ledger, { policy: politicaData, policyHash: hashFixat, maxTime = Math.floor(Date.now() / 1000) + TOLERANTA_S,
revocations = [], anchors = [], notBefore = null, anchorTolerance = TOLERANTA_S } = {}) {
const timp = { maxTime: Number(maxTime), notBefore: notBefore == null ? null : Number(notBefore), anchors: (anchors || []).length,
lowerBound: notBefore != null || (anchors || []).length ? 'witnessed' : 'none: entry times are the statement of the agent key holder' };
const rezultat = (x, extra = {}) => ({ ...x, ...extra, time: timp });
if (!ledger || !Array.isArray(ledger.entries)) return rezultat({ ok: false, error: 'a ledger with entries is required' });
let policy, policyHash;
try { ({ policy, policyHash } = hashPolicy(politicaData)); } catch (e) { return rezultat({ ok: false, error: `the policy given is not valid: ${e.message}` }); }
if (hashFixat != null && String(hashFixat).toLowerCase() !== policyHash) return rezultat({ ok: false, error: 'the policy given does not hash to the pinned policyHash (a different policy)' });
const refuz = (rule, error, extra = {}, seq = undefined) => { if (!REGULI.includes(rule)) throw new Error('agent-ledger: undeclared rule ' + rule); return rezultat({ ok: false, ...(seq === undefined ? {} : { seq }), rule, error }, extra); };
if (!(!!ledger && Array.isArray(ledger.entries) /*PAZA:registru*/)) return refuz('registru', 'a ledger with entries is required');
let policy = null, policyHash = null, politicaRea = null;
try { ({ policy, policyHash } = hashPolicy(politicaData)); } catch (e) { politicaRea = e.message; }
if (!(politicaRea === null /*PAZA:politica*/)) return refuz('politica', `the policy given is not valid: ${politicaRea}`);
if (!(hashFixat == null || String(hashFixat).toLowerCase() === policyHash /*PAZA:politica*/)) return refuz('politica', 'the policy given does not hash to the pinned policyHash (a different policy)');
const ext = validRevocations(revocations, { policy, policyHash });
let revokedAt = ext.revokedAt;
const judecat = () => ({ revocations: { given: (revocations || []).length, rejected: ext.rejected.length, revokedAt, setHash: ext.setHash } });
if (String(ledger.policyHash).toLowerCase() !== policyHash) return rezultat({ ok: false, error: 'the ledger names another policy (policyHash differs)' }, judecat());
if (ledger.session !== sessionId(ledger.agentId, policyHash)) return rezultat({ ok: false, error: 'the ledger session is not the one derived from the agent and the policy' }, judecat());
let pub;
try { pub = crypto.createPublicKey(ledger.publicKeyPem); } catch { return rezultat({ ok: false, error: 'the public key cannot be read' }, judecat()); }
if (ledger.agentId !== agentIdFromKey(pub)) return rezultat({ ok: false, error: 'agentId is not derived from the public key (false identity)' }, judecat());
if (policy.agentId !== ledger.agentId) return rezultat({ ok: false, error: 'the policy belongs to another agent' }, judecat());
if (!(String(ledger.policyHash).toLowerCase() === policyHash /*PAZA:alta-politica*/)) return refuz('alta-politica', 'the ledger names another policy (policyHash differs)', judecat());
if (!(ledger.session === sessionId(ledger.agentId, policyHash) /*PAZA:sesiune*/)) return refuz('sesiune', 'the ledger session is not the one derived from the agent and the policy', judecat());
let pub = null; try { pub = crypto.createPublicKey(ledger.publicKeyPem); } catch { pub = null; }
if (!(pub !== null /*PAZA:cheie*/)) return refuz('cheie', 'the public key cannot be read', judecat());
// The agent's key must BE an ML-DSA-65 key. The id is derived from whatever key is shown and crypto.verify(null, ...) verifies with
// whatever key it is handed: until 2026-10-03 a ledger whose "agent identity" was an Ed25519 (or RSA, or P-256) key verified here
// as the ledger of a post-quantum identity.
if (!(!!pub && pub.asymmetricKeyType === ALG /*PAZA:cheie*/)) return refuz('cheie', `the agent key is ${pub ? pub.asymmetricKeyType : 'unreadable'}, not ML-DSA-65: an agent identity is an ML-DSA-65 key`, judecat());
if (!(!!pub && ledger.agentId === agentIdFromKey(pub) /*PAZA:identitate*/)) return refuz('identitate', 'agentId is not derived from the public key (false identity)', judecat());
if (!(policy.agentId === ledger.agentId /*PAZA:alta-politica*/)) return refuz('alta-politica', 'the policy belongs to another agent', judecat());
// ancorele: fiecare numeste o intrare a ACESTUI registru (altfel e alta ramura sau alt registru)
const anc = [];
for (const a of anchors || []) {
const s = Number(a && a.seq);
if (!Number.isInteger(s) || s < 0 || s >= ledger.entries.length) return rezultat({ ok: false, error: `anchor at seq ${a && a.seq} is outside the ledger` }, judecat());
if (!ledger.entries[s] || ledger.entries[s].hash !== a.hash) return rezultat({ ok: false, seq: s, error: `anchor at seq ${s} does not match the ledger (another branch or another ledger)` }, judecat());
if (!Number.isFinite(Number(a.at))) return rezultat({ ok: false, error: `anchor at seq ${s} has no time` }, judecat());
anc.push({ seq: s, at: Number(a.at) });
if (!(Number.isInteger(s) && s >= 0 && s < ledger.entries.length /*PAZA:ancora*/)) return refuz('ancora', `anchor at seq ${a && a.seq} is outside the ledger`, judecat());
if (!(!!ledger.entries[s] && !!a && ledger.entries[s].hash === a.hash /*PAZA:ancora*/)) return refuz('ancora', `anchor at seq ${s} does not match the ledger (another branch or another ledger)`, judecat(), s);
if (!(!!a && Number.isFinite(Number(a.at)) /*PAZA:ancora*/)) return refuz('ancora', `anchor at seq ${s} has no time`, judecat());
anc.push({ seq: s, at: Number(a && a.at) });
}
const tol = Number(anchorTolerance);
let prev = GEN, humanApproved = 0;
@@ -247,44 +257,44 @@ export function verifyLedger(ledger, { policy: politicaData, policyHash: hashFix
const permise = []; // cheltuielile permise re-derivate, pentru fereastra
for (let i = 0; i < ledger.entries.length; i++) {
const e = ledger.entries[i];
const esec = (error) => rezultat({ ok: false, seq: i, error }, judecat());
if (e.seq !== i) return esec(`seq ${e.seq} instead of ${i}`);
if (e.prev !== prev) return esec('prev does not link to the previous hash (an entry was removed or reordered)');
const esec = (rule, error) => refuz(rule, error, judecat(), i);
if (!(!!e && e.seq === i /*PAZA:seq*/)) return esec('seq', `seq ${e && e.seq} instead of ${i}`);
if (!(e.prev === prev /*PAZA:lant*/)) return esec('lant', 'prev does not link to the previous hash (an entry was removed or reordered)');
const b = e.body;
if (!b || b.v !== 2 || b.seq !== i || b.agentId !== ledger.agentId || b.session !== ledger.session) return esec('the entry body does not match its header (agent, session or seq)');
if (String(b.policyHash).toLowerCase() !== policyHash) return esec('the entry names another policy');
if (!(!!b && b.v === 2 && b.seq === i && b.agentId === ledger.agentId && b.session === ledger.session /*PAZA:corp*/)) return esec('corp', 'the entry body does not match its header (agent, session or seq)');
if (!(!!b && String(b.policyHash).toLowerCase() === policyHash /*PAZA:alta-politica*/)) return esec('alta-politica', 'the entry names another policy');
const mesaj = Buffer.from(`${i}|${prev}|${canonical(b)}`, 'utf8');
let sigOk = false;
try { sigOk = crypto.verify(null, mesaj, pub, Buffer.from(String(e.signature), 'base64')); } catch { sigOk = false; }
if (!sigOk) return esec('the signature does not verify (content changed or another key)');
if (!(sigOk /*PAZA:semnatura*/)) return esec('semnatura', 'the signature does not verify (content changed or another key)');
const hash = sha(`${i}|${prev}|${canonical(b)}|${e.signature}`);
if (hash !== e.hash) return esec('the entry hash does not reproduce');
if (!(hash === e.hash /*PAZA:hash*/)) return esec('hash', 'the entry hash does not reproduce');
// timpul: nedescrescator, sub marginea de sus, peste marginea de jos si intre ancorele care il incadreaza
const atI = Number(b.at);
if (!Number.isFinite(atI) || !b.action || Number(b.action.at) !== atI) return esec('the entry time is missing or differs from the time judged');
if (i > 0 && atI < Number(ledger.entries[i - 1].body.at)) return esec('entry times go backwards');
if (atI > Number(maxTime)) return esec(`entry from the future: ${atI} > ${maxTime} (the verifier clock or an anchored head)`);
if (notBefore != null && atI < Number(notBefore)) return esec(`entry declares ${atI}, before the witnessed start ${notBefore}`);
if (!(Number.isFinite(atI) && !!b.action && Number(b.action.at) === atI /*PAZA:timp*/)) return esec('timp', 'the entry time is missing or differs from the time judged');
if (!(i === 0 || atI >= Number(ledger.entries[i - 1].body.at) /*PAZA:timp*/)) return esec('timp', 'entry times go backwards');
if (!(atI <= Number(maxTime) /*PAZA:timp*/)) return esec('timp', `entry from the future: ${atI} > ${maxTime} (the verifier clock or an anchored head)`);
if (!(notBefore == null || atI >= Number(notBefore) /*PAZA:timp*/)) return esec('timp', `entry declares ${atI}, before the witnessed start ${notBefore}`);
for (const a of anc) {
if (i <= a.seq && atI > a.at + tol) return esec(`entry declares ${atI}, after the anchor at seq ${a.seq} that covers it was witnessed (${a.at})`);
if (i > a.seq && atI < a.at - tol) return esec(`entry declares ${atI}, before the anchor at seq ${a.seq} that precedes it was witnessed (${a.at}): backdated`);
if (!(!(i <= a.seq && atI > a.at + tol) /*PAZA:timp*/)) return esec('timp', `entry declares ${atI}, after the anchor at seq ${a.seq} that covers it was witnessed (${a.at})`);
if (!(!(i > a.seq && atI < a.at - tol) /*PAZA:timp*/)) return esec('timp', `entry declares ${atI}, before the anchor at seq ${a.seq} that precedes it was witnessed (${a.at}): backdated`);
}
// o intrare de revocare: revocarea din ea trebuie sa verifice, si ea nu permite nimic
if (b.revocation !== undefined) {
const r = verifyRevocation(b.revocation, { policy, policyHash });
if (!r.ok) return esec(`the revocation entry carries a revocation that does not verify (${r.error})`);
if (b.action.kind !== 'revocation' || Number(b.action.revokedAt) !== r.revokedAt || b.decision.allowed) return esec('the revocation entry does not say what it carries');
if (!(r.ok /*PAZA:revocare*/)) return esec('revocare', `the revocation entry carries a revocation that does not verify (${r.error})`);
if (!(b.action.kind === 'revocation' && Number(b.action.revokedAt) === r.revokedAt && !b.decision.allowed /*PAZA:revocare*/)) return esec('revocare', 'the revocation entry does not say what it carries');
revokedAt = revokedAt == null ? r.revokedAt : Math.min(revokedAt, r.revokedAt);
prev = hash;
continue;
}
if (b.action.kind === 'revocation') return esec('a revocation entry without the revocation');
if (b.approvals !== undefined && !(Array.isArray(b.approvals) && b.approvals.length)) return esec('the approvals field is empty or not a list');
if (!(b.action.kind !== 'revocation' /*PAZA:revocare*/)) return esec('revocare', 'a revocation entry without the revocation');
if (!(b.approvals === undefined || (Array.isArray(b.approvals) && b.approvals.length > 0) /*PAZA:aprobari*/)) return esec('aprobari', 'the approvals field is empty or not a list');
const { valizi } = aprobatoriValizi(b.approvals, { policy, policyHash, action: b.action, at: atI, folosite });
for (const n of nonceuri(b.approvals)) folosite.add(n);
const decizieReala = checkAction(policy, b.action, permise, { approvers: valizi, revokedAt });
if (!b.decision || !!decizieReala.allowed !== !!b.decision.allowed) {
return esec(`false decision: the ledger says allowed=${b.decision && b.decision.allowed}, the policy gives allowed=${decizieReala.allowed} (${decizieReala.reason})`);
if (!(!!b.decision && !!decizieReala.allowed === !!b.decision.allowed /*PAZA:decizie*/)) {
return esec('decizie', `false decision: the ledger says allowed=${b.decision && b.decision.allowed}, the policy gives allowed=${decizieReala.allowed} (${decizieReala.reason})`);
}
if (decizieReala.allowed && decizieReala.approvedBy) humanApproved++;
if (decizieReala.allowed && b.action.kind === 'payment') permise.push({ amount: String(b.action.amount), at: b.action.at });