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

+32 -22
View File
@@ -57,14 +57,19 @@ function citesteJurnal(p) {
* Verifica lantul de hash-uri end-to-end. Returneaza {ok, count, head, rupt, motiv} - rupt = primul seq stricat sau null.
* NU spune nimic despre un lant refacut in intregime: pentru asta, verificaFataDeCap.
*/
// Every refusal of the checks below NAMES its rule (`regula`); a rule that is not declared cannot be named. 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 = ['intrare', 'seq', 'lant', 'hash', 'cap-forma', 'cap-hash', 'cap-nesemnat', 'cap-schema', 'cap-cheie', 'cap-semnatura', 'cap-semnatar', 'cap-scurt', 'cap-rescris'];
const refuz = (regula, motiv, extra = {}) => { if (!REGULI.includes(regula)) throw new Error('sidecar: undeclared rule ' + regula); return { ok: false, regula, motiv, ...extra }; };
export function verificaJurnal(intrari) {
if (!(Array.isArray(intrari) /*PAZA:intrare*/)) return refuz('intrare', 'the log is not a list of entries', { count: 0, head: null, rupt: 0 });
let prev = GENEZA;
for (let i = 0; i < intrari.length; i++) {
const e = intrari[i];
if (!e || typeof e !== 'object' || e.necitibil) return { ok: false, count: intrari.length, head: null, rupt: i, motiv: `entry ${i} is not a JSON log entry` };
if (e.seq !== i) return { ok: false, count: intrari.length, head: null, rupt: i, motiv: `wrong seq: ${e.seq} instead of ${i}` };
if (e.prev !== prev) return { ok: false, count: intrari.length, head: null, rupt: i, motiv: `prev does not link entry ${i - 1}` };
if (hashIntrare(e.seq, e.prev, e.proof) !== e.hash) return { ok: false, count: intrari.length, head: null, rupt: i, motiv: `the hash of entry ${i} does not match its content (modified)` };
const e = intrari[i]; const la = { count: intrari.length, head: null, rupt: i };
if (!(!!e && typeof e === 'object' && !e.necitibil /*PAZA:intrare*/)) return refuz('intrare', `entry ${i} is not a JSON log entry`, la);
if (!(e.seq === i /*PAZA:seq*/)) return refuz('seq', `wrong seq: ${e.seq} instead of ${i}`, la);
if (!(e.prev === prev /*PAZA:lant*/)) return refuz('lant', `prev does not link entry ${i - 1}`, la);
if (!(hashIntrare(e.seq, e.prev, e.proof) === e.hash /*PAZA:hash*/)) return refuz('hash', `the hash of entry ${i} does not match its content (modified)`, la);
prev = e.hash;
}
return { ok: true, count: intrari.length, head: prev, rupt: null };
@@ -77,16 +82,16 @@ export function verificaJurnal(intrari) {
*/
export function verificaFataDeCap(intrari, cap, { semnatar = null } = {}) {
const st = cap && cap.statement;
if (!cap || cap.kind !== 'aere-audit-head-attestation' || !st || st.kind !== 'aere-audit-head') return { ok: false, motiv: 'the file is not an aere-audit-head attestation' };
if (!eDigest(cap.statementHash) || sha256(Buffer.from(JSON.stringify(st), 'utf8')) !== cap.statementHash.toLowerCase()) return { ok: false, motiv: 'the attestation statementHash does not match its statement (modified attestation)' };
if (!(!!cap && cap.kind === 'aere-audit-head-attestation' && !!st && st.kind === 'aere-audit-head' /*PAZA:cap-forma*/)) return refuz('cap-forma', 'the file is not an aere-audit-head attestation');
if (!(!!cap && eDigest(cap.statementHash) && sha256(Buffer.from(String(JSON.stringify(st)), 'utf8')) === cap.statementHash.toLowerCase() /*PAZA:cap-hash*/)) return refuz('cap-hash', 'the attestation statementHash does not match its statement (modified attestation)');
const sg = verificaSemnaturaCap(cap, semnatar);
if (!sg.ok) return { ok: false, motiv: sg.motiv };
if (!Number.isInteger(st.count) || st.count < 0 || !eDigest(st.head)) return { ok: false, motiv: 'the attestation has no valid count and head' };
if (!sg.ok) return sg;
if (!(!!st && Number.isInteger(st.count) && st.count >= 0 && eDigest(st.head) /*PAZA:cap-forma*/)) return refuz('cap-forma', 'the attestation has no valid count and head');
const v = verificaJurnal(intrari);
if (!v.ok) return { ok: false, motiv: `the log is broken at seq ${v.rupt}: ${v.motiv}` };
if (intrari.length < st.count) return { ok: false, motiv: `the log has ${intrari.length} entries and the attested head covers ${st.count}: entries were removed` };
const h = st.count === 0 ? GENEZA : intrari[st.count - 1].hash;
if (h !== st.head.toLowerCase()) return { ok: false, motiv: `entry ${st.count - 1} is not the attested head: the history before the attested point was rewritten` };
if (!v.ok) return refuz(v.regula, `the log is broken at seq ${v.rupt}: ${v.motiv}`);
if (!(intrari.length >= st.count /*PAZA:cap-scurt*/)) return refuz('cap-scurt', `the log has ${intrari.length} entries and the attested head covers ${st.count}: entries were removed`);
const ultima = intrari[st.count - 1]; const h = st.count === 0 ? GENEZA : (ultima ? ultima.hash : null);
if (!(h === String(st.head).toLowerCase() /*PAZA:cap-rescris*/)) return refuz('cap-rescris', `entry ${st.count - 1} is not the attested head: the history before the attested point was rewritten`);
return { ok: true, count: st.count, extra: intrari.length - st.count, semnatDe: sg.semnat ? sg.keyId : null };
}
@@ -103,19 +108,24 @@ export function semneazaCap(plic, cheiePem) {
const sig = crypto.sign(null, Buffer.from(JSON.stringify(plic.statement), 'utf8'), priv);
return { ...plic, signature: { scheme: 'ml-dsa-65', publicKey: pub.export({ type: 'spki', format: 'der' }).toString('base64'), signature: sig.toString('base64') } };
}
/** @returns {{ok:boolean, semnat:boolean, keyId?:string, motiv?:string}} semnatarPem: cheia publica asteptata (optional) */
/** @returns {{ok:boolean, semnat:boolean, keyId?:string, motiv?:string, regula?:string}} semnatarPem: cheia publica asteptata (optional) */
export function verificaSemnaturaCap(plic, semnatarPem = null) {
const s = plic && plic.signature;
if (!s) return semnatarPem ? { ok: false, semnat: false, motiv: 'the head is not signed, and a signer was required' } : { ok: true, semnat: false };
if (String(s.scheme).toLowerCase() !== 'ml-dsa-65' || !s.publicKey || !s.signature) return { ok: false, semnat: true, motiv: 'the head signature is not an ml-dsa-65 signature' };
let pub; try { pub = crypto.createPublicKey({ key: Buffer.from(String(s.publicKey), 'base64'), format: 'der', type: 'spki' }); } catch { return { ok: false, semnat: true, motiv: 'the head signature public key cannot be read' }; }
let ok = false; try { ok = crypto.verify(null, Buffer.from(JSON.stringify(plic.statement), 'utf8'), pub, Buffer.from(String(s.signature), 'base64')); } catch { ok = false; }
if (!ok) return { ok: false, semnat: true, keyId: idCheie(pub), motiv: 'the head signature does not verify over the statement' };
if (!s) { if (!(!semnatarPem /*PAZA:cap-nesemnat*/)) return refuz('cap-nesemnat', 'the head is not signed, and a signer was required', { semnat: false }); return { ok: true, semnat: false }; }
if (!(String(s.scheme).toLowerCase() === 'ml-dsa-65' && !!s.publicKey && !!s.signature /*PAZA:cap-schema*/)) return refuz('cap-schema', 'the head signature is not an ml-dsa-65 signature', { semnat: true });
let pub = null; try { pub = crypto.createPublicKey({ key: Buffer.from(String(s.publicKey), 'base64'), format: 'der', type: 'spki' }); } catch { pub = null; }
if (!(pub !== null /*PAZA:cap-cheie*/)) return refuz('cap-cheie', 'the head signature public key cannot be read', { semnat: true });
// The key must BE an ML-DSA-65 key. crypto.verify(null, ...) verifies with whatever key it is handed: until 2026-10-03 a head
// "signed" with an Ed25519 (or RSA, or P-256) key that declared the scheme ml-dsa-65 verified here as an ML-DSA-65 signed head.
if (!(!!pub && pub.asymmetricKeyType === 'ml-dsa-65' /*PAZA:cap-schema*/)) return refuz('cap-schema', `the head declares an ml-dsa-65 signature and its public key is ${pub ? pub.asymmetricKeyType : 'unreadable'}`, { semnat: true });
const id = pub ? idCheie(pub) : null;
let ok = false; try { ok = crypto.verify(null, Buffer.from(String(JSON.stringify(plic.statement)), 'utf8'), pub, Buffer.from(String(s.signature), 'base64')); } catch { ok = false; }
if (!(ok /*PAZA:cap-semnatura*/)) return refuz('cap-semnatura', 'the head signature does not verify over the statement', { semnat: true, keyId: id });
if (semnatarPem) {
let asteptat; try { asteptat = crypto.createPublicKey(semnatarPem); } catch { return { ok: false, semnat: true, motiv: 'the expected signer key cannot be read' }; }
if (idCheie(asteptat) !== idCheie(pub)) return { ok: false, semnat: true, keyId: idCheie(pub), motiv: `the head is signed by another key (${idCheie(pub).slice(0, 18)}), not the expected signer` };
let asteptat = null; try { asteptat = crypto.createPublicKey(semnatarPem); } catch { asteptat = null; }
if (!(asteptat !== null && idCheie(asteptat) === id /*PAZA:cap-semnatar*/)) return refuz('cap-semnatar', asteptat ? `the head is signed by another key (${String(id).slice(0, 18)}), not the expected signer` : 'the expected signer key cannot be read', { semnat: true, keyId: id });
}
return { ok: true, semnat: true, keyId: idCheie(pub) };
return { ok: true, semnat: true, keyId: id };
}
// ---- adaptorul de runtime: exportul pe care operatorul il face el insusi, fara acreditari de cloud ------------------------------