sm-parc 0.2.0__py3-none-any.whl
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.
- sm_parc/__init__.py +45 -0
- sm_parc/admission.py +396 -0
- sm_parc/context.py +17 -0
- sm_parc/credential.py +144 -0
- sm_parc/disclosure.py +105 -0
- sm_parc/py.typed +0 -0
- sm_parc-0.2.0.dist-info/METADATA +204 -0
- sm_parc-0.2.0.dist-info/RECORD +10 -0
- sm_parc-0.2.0.dist-info/WHEEL +4 -0
- sm_parc-0.2.0.dist-info/licenses/LICENSE +21 -0
sm_parc/__init__.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""PARC — Portable Agent Reputation Credential.
|
|
2
|
+
|
|
3
|
+
A verifiable, recomputable reputation Verifiable Credential (a W3C VC over a VRP
|
|
4
|
+
Receipts Ledger) consumed at chapter admission. The admitting chapter verifies the
|
|
5
|
+
issuer signature and recomputes the ledger itself — it never trusts the issuing
|
|
6
|
+
chapter's live server.
|
|
7
|
+
|
|
8
|
+
from sm_parc import build_reputation_credential, verify_credential_proof
|
|
9
|
+
from sm_parc import AdmissionPolicy, admit
|
|
10
|
+
|
|
11
|
+
Composes ``sm_arp.vrp`` (behavioral_merkle_root + nanda-rep/0.2 corroborated,
|
|
12
|
+
collusion-resistant scoring + ledger verify) from the public ``sm-arp`` package.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
from .admission import (
|
|
18
|
+
AdmissionPolicy,
|
|
19
|
+
AdmissionResult,
|
|
20
|
+
admit,
|
|
21
|
+
admit_over_published_ledger,
|
|
22
|
+
subject_severed_score,
|
|
23
|
+
)
|
|
24
|
+
from .credential import (
|
|
25
|
+
build_reputation_credential,
|
|
26
|
+
did_from_private_key,
|
|
27
|
+
verify_credential_proof,
|
|
28
|
+
)
|
|
29
|
+
from .disclosure import InclusionProof, inclusion_proof, verify_inclusion
|
|
30
|
+
|
|
31
|
+
__version__ = "0.2.0"
|
|
32
|
+
|
|
33
|
+
__all__ = [
|
|
34
|
+
"AdmissionPolicy",
|
|
35
|
+
"AdmissionResult",
|
|
36
|
+
"InclusionProof",
|
|
37
|
+
"admit",
|
|
38
|
+
"admit_over_published_ledger",
|
|
39
|
+
"build_reputation_credential",
|
|
40
|
+
"did_from_private_key",
|
|
41
|
+
"inclusion_proof",
|
|
42
|
+
"subject_severed_score",
|
|
43
|
+
"verify_credential_proof",
|
|
44
|
+
"verify_inclusion",
|
|
45
|
+
]
|
sm_parc/admission.py
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
"""The admission gate — verify a ReputationCredential at chapter admission.
|
|
2
|
+
|
|
3
|
+
The point of PARC: a chapter admits (or rejects) an agent on the strength of a
|
|
4
|
+
reputation credential it can verify *without trusting the issuing chapter's live
|
|
5
|
+
server*. The gate trusts two things only — the issuer's signature and its OWN
|
|
6
|
+
recomputation of the ledger:
|
|
7
|
+
|
|
8
|
+
1. the VC proof verifies under the issuer did;
|
|
9
|
+
2. the issuer is trusted by policy (a chapter OR a credentialed auditor);
|
|
10
|
+
3. the credential is not revoked and is inside its validity window
|
|
11
|
+
(``validFrom`` ≤ now ≤ ``validUntil``);
|
|
12
|
+
4. the credential's ``scoring_method`` is the corroborated, collusion-resistant
|
|
13
|
+
``nanda-rep/0.2`` — a credential carrying the un-corroborated ``nanda-rep/0.1``
|
|
14
|
+
score is rejected (``scoring_method_unsupported``);
|
|
15
|
+
5. the presented ledger is recomputable and its receipt count matches the
|
|
16
|
+
credential's signed ``receipt_count`` (no withholding);
|
|
17
|
+
6. the ledger recomputes to the VC's ``behavioral_merkle_root`` and the
|
|
18
|
+
``nanda-rep/0.2`` ``reputation_score`` / ``validity_rate`` / ``corroboration_rate``;
|
|
19
|
+
7. the scores clear the policy thresholds.
|
|
20
|
+
|
|
21
|
+
Why bind admission to ``nanda-rep/0.2`` (step 4 + the v2 recompute in step 6): the
|
|
22
|
+
0.2 score counts only receipts that are ARP-valid AND corroborated by a distinct
|
|
23
|
+
counterparty AND not part of a severed collusion component. A wash-trading ring
|
|
24
|
+
that mutually co-signs its own receipts is severed to a score of ~0, so its
|
|
25
|
+
credential FAILS the gate at the threshold. The un-corroborated ``nanda-rep/0.1``
|
|
26
|
+
score has no such defence, so the gate refuses to admit on it.
|
|
27
|
+
|
|
28
|
+
Step 5-6 recomputation is ``sm_arp.vrp``. Pure: receipts' validity is supplied
|
|
29
|
+
by the caller as ``is_valid`` (normally the ARP verifier), and ``now`` is supplied
|
|
30
|
+
by the caller (no wall-clock here).
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
from __future__ import annotations
|
|
34
|
+
|
|
35
|
+
from collections.abc import Callable, Sequence
|
|
36
|
+
from dataclasses import dataclass, field
|
|
37
|
+
from typing import Any
|
|
38
|
+
|
|
39
|
+
from sm_arp.vrp import (
|
|
40
|
+
DEFAULT_CATEGORY_WEIGHTS,
|
|
41
|
+
SCORING_METHOD_V2,
|
|
42
|
+
_effective_receipts,
|
|
43
|
+
behavioral_merkle_root,
|
|
44
|
+
corroboration_rate,
|
|
45
|
+
reputation_score_v2,
|
|
46
|
+
validity_rate,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
from .credential import verify_credential_proof
|
|
50
|
+
|
|
51
|
+
IsValid = Callable[[dict[str, Any]], bool]
|
|
52
|
+
Fetch = Callable[[str], dict[str, Any]]
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@dataclass
|
|
56
|
+
class AdmissionPolicy:
|
|
57
|
+
"""What an admitting chapter requires of a reputation credential."""
|
|
58
|
+
|
|
59
|
+
trusted_issuers: set[str] = field(default_factory=set)
|
|
60
|
+
min_reputation_score: float = 0.0
|
|
61
|
+
min_validity_rate: float = 0.0
|
|
62
|
+
require_recomputation: bool = True
|
|
63
|
+
revocation: set[str] = field(default_factory=set)
|
|
64
|
+
score_tolerance: float = 1e-9
|
|
65
|
+
# The scoring method the gate will admit on. Defaults to the corroborated,
|
|
66
|
+
# collusion-resistant nanda-rep/0.2 — a credential carrying any other method
|
|
67
|
+
# (notably the un-corroborated nanda-rep/0.1) is rejected before recomputation.
|
|
68
|
+
required_scoring_method: str = SCORING_METHOD_V2
|
|
69
|
+
# Pointer-mode stopgap for the curated-ledger residual (until a notary layer
|
|
70
|
+
# exists): a local allowlist of known-honest "anchor" dids. A fetched ledger that
|
|
71
|
+
# does NOT involve at least one of these anchors is treated as incomplete and
|
|
72
|
+
# rejected (anchor_absent) — it defeats the trivial "publish my ring with no anchor"
|
|
73
|
+
# laundering. Empty set = check disabled. See THREATMODEL.md for the residual that
|
|
74
|
+
# remains (an attacker who wires one real edge to an anchor still evades severance).
|
|
75
|
+
required_anchors: set[str] = field(default_factory=set)
|
|
76
|
+
# Deterministic execution budget: the gate runs SCC severance over the whole
|
|
77
|
+
# fetched ledger, so a hostile issuer could publish an enormous graph to exhaust
|
|
78
|
+
# the verifier. A fetched ledger with more than this many receipts is rejected
|
|
79
|
+
# (ledger_too_large) BEFORE the expensive recomputation. None = unbounded.
|
|
80
|
+
max_ledger_receipts: int | None = None
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
@dataclass
|
|
84
|
+
class AdmissionResult:
|
|
85
|
+
ok: bool
|
|
86
|
+
# signature | untrusted_issuer | revoked | not_yet_valid | stale |
|
|
87
|
+
# scoring_method_unsupported | wrong_mode | ledger_too_large | count_mismatch |
|
|
88
|
+
# root_mismatch | anchor_absent | score_mismatch | below_threshold | admitted
|
|
89
|
+
stage: str
|
|
90
|
+
detail: str
|
|
91
|
+
|
|
92
|
+
@classmethod
|
|
93
|
+
def admitted(cls) -> AdmissionResult:
|
|
94
|
+
return cls(True, "admitted", "credential verifies and clears policy")
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _check_envelope(
|
|
98
|
+
vc: dict[str, Any], *, policy: AdmissionPolicy, now: str | None
|
|
99
|
+
) -> AdmissionResult | None:
|
|
100
|
+
"""Shared envelope checks for both admission modes: proof, trusted issuer,
|
|
101
|
+
revocation, validity window, and the required scoring method.
|
|
102
|
+
|
|
103
|
+
Returns the failing :class:`AdmissionResult`, or ``None`` if the envelope is
|
|
104
|
+
sound (in which case the caller proceeds to its mode-specific recomputation).
|
|
105
|
+
"""
|
|
106
|
+
if not verify_credential_proof(vc):
|
|
107
|
+
return AdmissionResult(False, "signature", "credential proof does not verify")
|
|
108
|
+
|
|
109
|
+
issuer = vc.get("issuer", "")
|
|
110
|
+
if issuer not in policy.trusted_issuers:
|
|
111
|
+
return AdmissionResult(
|
|
112
|
+
False, "untrusted_issuer", f"issuer {issuer} not in policy.trusted_issuers"
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
cred_id = vc.get("id")
|
|
116
|
+
if cred_id is not None and cred_id in policy.revocation:
|
|
117
|
+
return AdmissionResult(False, "revoked", f"credential {cred_id} is revoked")
|
|
118
|
+
|
|
119
|
+
# Not-yet-valid: a credential's window has a lower bound too.
|
|
120
|
+
valid_from = vc.get("validFrom")
|
|
121
|
+
if now is not None and isinstance(valid_from, str) and now < valid_from:
|
|
122
|
+
return AdmissionResult(
|
|
123
|
+
False, "not_yet_valid", f"credential not valid until {valid_from} (now {now})"
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
valid_until = vc.get("validUntil")
|
|
127
|
+
if now is not None and isinstance(valid_until, str) and now > valid_until:
|
|
128
|
+
return AdmissionResult(False, "stale", f"credential expired at {valid_until} (now {now})")
|
|
129
|
+
|
|
130
|
+
# The credential MUST carry the corroborated, collusion-resistant scoring method.
|
|
131
|
+
# A credential whose facet is the un-corroborated nanda-rep/0.1 score is rejected
|
|
132
|
+
# here: that score lets a wash-trading ring inflate itself, so the gate refuses to
|
|
133
|
+
# admit on it (corroboration_required).
|
|
134
|
+
subject = vc.get("credentialSubject") or {}
|
|
135
|
+
scoring_method = subject.get("scoring_method")
|
|
136
|
+
if scoring_method != policy.required_scoring_method:
|
|
137
|
+
return AdmissionResult(
|
|
138
|
+
False,
|
|
139
|
+
"scoring_method_unsupported",
|
|
140
|
+
f"scoring_method {scoring_method!r} != required "
|
|
141
|
+
f"{policy.required_scoring_method!r} (corroboration_required)",
|
|
142
|
+
)
|
|
143
|
+
return None
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def subject_severed_score(
|
|
147
|
+
receipts: Sequence[dict[str, Any]],
|
|
148
|
+
*,
|
|
149
|
+
subject: str,
|
|
150
|
+
is_valid: IsValid,
|
|
151
|
+
weights: dict[str, float] = DEFAULT_CATEGORY_WEIGHTS,
|
|
152
|
+
) -> float:
|
|
153
|
+
"""The subject's OWN collusion-severed reputation within a *full* community ledger.
|
|
154
|
+
|
|
155
|
+
Severance (Tarjan SCC over the corroboration graph) is run over the WHOLE
|
|
156
|
+
``receipts`` set, so an isolated dense ring is severed only when the ledger also
|
|
157
|
+
carries the honest anchor it is isolated from. Then only the subject's own
|
|
158
|
+
receipts (``issuer_did == subject``) that SURVIVE severance are scored.
|
|
159
|
+
|
|
160
|
+
A ring member's receipts are all severed → 0; an anchor member's survive → its
|
|
161
|
+
real score. This is the per-subject number the pointer-mode gate derives ITSELF
|
|
162
|
+
from a fetched ledger — it is deliberately NOT carried in the credential, so the
|
|
163
|
+
gate does the collusion analysis rather than trusting an attested number.
|
|
164
|
+
|
|
165
|
+
Why this catches what :func:`admit` (inline mode) cannot: a single-subject
|
|
166
|
+
credential's corroboration graph is a star (the subject → its counterparties),
|
|
167
|
+
never a strongly-connected ring, so severance can never fire on it. Severance is
|
|
168
|
+
a property of the *whole* graph; only a credential that points at the full
|
|
169
|
+
community ledger lets the gate see — and sever — an N-party ring.
|
|
170
|
+
"""
|
|
171
|
+
effective = _effective_receipts(receipts, is_valid=is_valid)
|
|
172
|
+
return sum(
|
|
173
|
+
weights.get((r.get("action") or {}).get("category", ""), 0.0)
|
|
174
|
+
for r in effective
|
|
175
|
+
if r.get("issuer_did") == subject
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def admit(
|
|
180
|
+
vc: dict[str, Any],
|
|
181
|
+
*,
|
|
182
|
+
policy: AdmissionPolicy,
|
|
183
|
+
ledger: dict[str, Any] | None = None,
|
|
184
|
+
is_valid: IsValid | None = None,
|
|
185
|
+
now: str | None = None,
|
|
186
|
+
) -> AdmissionResult:
|
|
187
|
+
"""Decide admission for ``vc`` under ``policy`` (INLINE, self-contained mode).
|
|
188
|
+
|
|
189
|
+
The credential carries the subject's OWN receipts inline; the gate recomputes the
|
|
190
|
+
facet from them. This is fully offline and leaks nothing but the subject's own
|
|
191
|
+
history — but a single-subject credential's corroboration graph is a star, so this
|
|
192
|
+
mode catches *self-dealing* (the corroboration filter) but structurally CANNOT see
|
|
193
|
+
an N-party Sybil ring. For that, see :func:`admit_over_published_ledger`.
|
|
194
|
+
|
|
195
|
+
``ledger`` (the agent's presented Receipts Ledger) + ``is_valid`` are required
|
|
196
|
+
when ``policy.require_recomputation`` is True (the default and recommended
|
|
197
|
+
posture). ``now`` (RFC 3339) enables the validity-window checks against both
|
|
198
|
+
``validFrom`` and ``validUntil``. Under recomputation the credential's signed
|
|
199
|
+
``receipt_count`` must also match the presented ledger (anti-withholding), and
|
|
200
|
+
the recomputed nanda-rep/0.2 ``reputation_score`` / ``validity_rate`` /
|
|
201
|
+
``corroboration_rate`` must match the signed facet.
|
|
202
|
+
"""
|
|
203
|
+
envelope = _check_envelope(vc, policy=policy, now=now)
|
|
204
|
+
if envelope is not None:
|
|
205
|
+
return envelope
|
|
206
|
+
|
|
207
|
+
subject = vc.get("credentialSubject") or {}
|
|
208
|
+
|
|
209
|
+
# Mode pairing: a pointer credential carries a ledger-wide score in
|
|
210
|
+
# ``reputation_score`` and a ``ledger_uri``. Admitting it here would threshold the
|
|
211
|
+
# WHOLE-community score (not the subject's severed score) and silently re-admit a
|
|
212
|
+
# ring member. Refuse — pointer credentials MUST go through
|
|
213
|
+
# admit_over_published_ledger, which derives the per-subject severed score.
|
|
214
|
+
if subject.get("ledger_uri") is not None:
|
|
215
|
+
return AdmissionResult(
|
|
216
|
+
False, "wrong_mode", "pointer credential; use admit_over_published_ledger"
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
if policy.require_recomputation:
|
|
220
|
+
if ledger is None or is_valid is None:
|
|
221
|
+
return AdmissionResult(
|
|
222
|
+
False, "root_mismatch", "recomputation required but ledger/is_valid not provided"
|
|
223
|
+
)
|
|
224
|
+
receipts = ledger.get("receipts")
|
|
225
|
+
if not isinstance(receipts, list) or any("action" not in r for r in receipts):
|
|
226
|
+
return AdmissionResult(
|
|
227
|
+
False, "root_mismatch", "presented ledger is not recomputable (refs-only)"
|
|
228
|
+
)
|
|
229
|
+
# 6a. The credential's signed receipt_count MUST match the presented ledger —
|
|
230
|
+
# a presenter cannot withhold receipts to change the recomputed scores.
|
|
231
|
+
if subject.get("receipt_count") != len(receipts):
|
|
232
|
+
return AdmissionResult(
|
|
233
|
+
False,
|
|
234
|
+
"count_mismatch",
|
|
235
|
+
f"count {subject.get('receipt_count')} != {len(receipts)} presented",
|
|
236
|
+
)
|
|
237
|
+
if behavioral_merkle_root(receipts) != subject.get("behavioral_merkle_root"):
|
|
238
|
+
return AdmissionResult(
|
|
239
|
+
False, "root_mismatch", "ledger does not recompute to the credential root"
|
|
240
|
+
)
|
|
241
|
+
# 6b. Recompute the nanda-rep/0.2 facet (corroborated + collusion-severed)
|
|
242
|
+
# and confirm it matches the signed credentialSubject — a signed-but-inflated
|
|
243
|
+
# score is caught here, and a SELF-DEALT receipt (self-cosigned, no distinct
|
|
244
|
+
# counterparty) earns 0 via the corroboration filter. Note: this inline path
|
|
245
|
+
# canNOT sever an N-party ring — the subject's own receipts form a star, not a
|
|
246
|
+
# strongly-connected component. Ring severance needs the full community graph;
|
|
247
|
+
# that is admit_over_published_ledger, not this function.
|
|
248
|
+
recomputed_score = reputation_score_v2(receipts, is_valid=is_valid)
|
|
249
|
+
recomputed_validity = validity_rate(receipts, is_valid=is_valid)
|
|
250
|
+
recomputed_corroboration = corroboration_rate(receipts, is_valid=is_valid)
|
|
251
|
+
if (
|
|
252
|
+
abs(recomputed_score - float(subject.get("reputation_score", 0)))
|
|
253
|
+
> policy.score_tolerance
|
|
254
|
+
):
|
|
255
|
+
return AdmissionResult(False, "score_mismatch", "reputation_score does not recompute")
|
|
256
|
+
if (
|
|
257
|
+
abs(recomputed_validity - float(subject.get("validity_rate", 0)))
|
|
258
|
+
> policy.score_tolerance
|
|
259
|
+
):
|
|
260
|
+
return AdmissionResult(False, "score_mismatch", "validity_rate does not recompute")
|
|
261
|
+
if (
|
|
262
|
+
abs(recomputed_corroboration - float(subject.get("corroboration_rate", 0)))
|
|
263
|
+
> policy.score_tolerance
|
|
264
|
+
):
|
|
265
|
+
return AdmissionResult(False, "score_mismatch", "corroboration_rate does not recompute")
|
|
266
|
+
|
|
267
|
+
if float(subject.get("reputation_score", 0)) < policy.min_reputation_score:
|
|
268
|
+
return AdmissionResult(False, "below_threshold", "reputation_score below policy minimum")
|
|
269
|
+
if float(subject.get("validity_rate", 0)) < policy.min_validity_rate:
|
|
270
|
+
return AdmissionResult(False, "below_threshold", "validity_rate below policy minimum")
|
|
271
|
+
|
|
272
|
+
return AdmissionResult.admitted()
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def _involves_anchor(receipts: list[dict[str, Any]], anchors: set[str]) -> bool:
|
|
276
|
+
"""True iff any receipt is issued by, or transacted with, a required anchor did."""
|
|
277
|
+
for r in receipts:
|
|
278
|
+
if r.get("issuer_did") in anchors:
|
|
279
|
+
return True
|
|
280
|
+
if (r.get("action") or {}).get("counterparty_did") in anchors:
|
|
281
|
+
return True
|
|
282
|
+
return False
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def admit_over_published_ledger(
|
|
286
|
+
vc: dict[str, Any],
|
|
287
|
+
*,
|
|
288
|
+
policy: AdmissionPolicy,
|
|
289
|
+
fetch: Fetch,
|
|
290
|
+
is_valid: IsValid,
|
|
291
|
+
now: str | None = None,
|
|
292
|
+
) -> AdmissionResult:
|
|
293
|
+
"""Decide admission for ``vc`` in POINTER mode — the credential names a published
|
|
294
|
+
community ledger (``credentialSubject.ledger_uri``) and the gate FETCHES it and
|
|
295
|
+
re-runs the collusion analysis itself.
|
|
296
|
+
|
|
297
|
+
Where :func:`admit` (inline) verifies a self-contained, single-subject credential
|
|
298
|
+
offline, this path trades that for collusion-resistance the inline mode cannot
|
|
299
|
+
have. The facet describes the LEDGER (root + ledger-wide scores); the gate:
|
|
300
|
+
|
|
301
|
+
1. verifies the envelope (proof, trusted issuer, window, ``nanda-rep/0.2``);
|
|
302
|
+
2. ``fetch``-es the full ledger named by ``ledger_uri`` (caller supplies the
|
|
303
|
+
resolver — no network here);
|
|
304
|
+
3. checks the fetched ledger hashes to the credential's SIGNED
|
|
305
|
+
``behavioral_merkle_root`` (tamper-evidence: the host cannot serve a
|
|
306
|
+
different ledger than the one the issuer committed to);
|
|
307
|
+
4. checks the ledger-wide ``reputation_score`` recomputes from those receipts
|
|
308
|
+
(anti-withholding / anti-substitution of the published ledger);
|
|
309
|
+
5. **derives the subject's own collusion-severed score** over the full graph
|
|
310
|
+
via :func:`subject_severed_score` and applies the threshold. An isolated
|
|
311
|
+
N-party Sybil ring is severed to 0 HERE, by the gate — not trusted from an
|
|
312
|
+
attested number.
|
|
313
|
+
|
|
314
|
+
Honest residual — the trust this does NOT remove: severance is only as good as
|
|
315
|
+
the ledger's COMPLETENESS. A *colluding* issuer that publishes its ring WITHOUT
|
|
316
|
+
the honest anchor (so the ring is the largest SCC and is never severed), or that
|
|
317
|
+
injects a single cross-edge to the anchor, yields a ledger that recomputes HIGH
|
|
318
|
+
and is admitted — and the gate, seeing only the published ledger, cannot tell.
|
|
319
|
+
Pointer mode catches a LAZY-but-honest issuer; the colluding issuer is the notary
|
|
320
|
+
/ multi-issuer-attestation layer's problem. See ``THREATMODEL.md``.
|
|
321
|
+
"""
|
|
322
|
+
envelope = _check_envelope(vc, policy=policy, now=now)
|
|
323
|
+
if envelope is not None:
|
|
324
|
+
return envelope
|
|
325
|
+
|
|
326
|
+
subject = vc.get("credentialSubject") or {}
|
|
327
|
+
ledger_uri = subject.get("ledger_uri")
|
|
328
|
+
if not isinstance(ledger_uri, str):
|
|
329
|
+
return AdmissionResult(False, "wrong_mode", "inline credential (no ledger_uri); use admit")
|
|
330
|
+
|
|
331
|
+
ledger = fetch(ledger_uri)
|
|
332
|
+
receipts = ledger.get("receipts")
|
|
333
|
+
if not isinstance(receipts, list) or any("action" not in r for r in receipts):
|
|
334
|
+
return AdmissionResult(
|
|
335
|
+
False, "root_mismatch", "fetched ledger is refs-only (not recomputable)"
|
|
336
|
+
)
|
|
337
|
+
|
|
338
|
+
# 2a. Execution budget: reject an oversized ledger BEFORE any O(n) hashing or the
|
|
339
|
+
# severance recomputation, so a hostile issuer cannot exhaust the verifier.
|
|
340
|
+
if policy.max_ledger_receipts is not None and len(receipts) > policy.max_ledger_receipts:
|
|
341
|
+
return AdmissionResult(
|
|
342
|
+
False,
|
|
343
|
+
"ledger_too_large",
|
|
344
|
+
f"fetched ledger has {len(receipts)} receipts > max {policy.max_ledger_receipts}",
|
|
345
|
+
)
|
|
346
|
+
|
|
347
|
+
# 3. Tamper-evidence: the fetched ledger MUST hash to the credential's signed root.
|
|
348
|
+
if behavioral_merkle_root(receipts) != subject.get("behavioral_merkle_root"):
|
|
349
|
+
return AdmissionResult(
|
|
350
|
+
False, "root_mismatch", "fetched ledger does not match the signed root"
|
|
351
|
+
)
|
|
352
|
+
# 4. Anti-withholding: the signed ledger-wide score must recompute from the fetched
|
|
353
|
+
# receipts (the issuer cannot publish a ledger that differs from what it attested).
|
|
354
|
+
if subject.get("receipt_count") != len(receipts):
|
|
355
|
+
return AdmissionResult(
|
|
356
|
+
False,
|
|
357
|
+
"count_mismatch",
|
|
358
|
+
f"count {subject.get('receipt_count')} != {len(receipts)} fetched",
|
|
359
|
+
)
|
|
360
|
+
# 4a. Curated-ledger stopgap: the fetched ledger MUST involve at least one
|
|
361
|
+
# known-honest anchor, else it is treated as incomplete (defeats the trivial
|
|
362
|
+
# "publish my ring with no honest anchor" laundering). Disabled when the policy
|
|
363
|
+
# carries no anchors.
|
|
364
|
+
if policy.required_anchors and not _involves_anchor(receipts, policy.required_anchors):
|
|
365
|
+
return AdmissionResult(
|
|
366
|
+
False, "anchor_absent", "fetched ledger involves no required anchor (incomplete)"
|
|
367
|
+
)
|
|
368
|
+
ledger_wide = reputation_score_v2(receipts, is_valid=is_valid)
|
|
369
|
+
if abs(ledger_wide - float(subject.get("reputation_score", 0))) > policy.score_tolerance:
|
|
370
|
+
return AdmissionResult(
|
|
371
|
+
False, "score_mismatch", "ledger-wide reputation_score does not recompute"
|
|
372
|
+
)
|
|
373
|
+
|
|
374
|
+
# 5. The gate DERIVES the subject's severed score over the full graph — an
|
|
375
|
+
# isolated ring member lands at 0 here, computed by the gate, not attested.
|
|
376
|
+
subject_did = subject.get("id")
|
|
377
|
+
if not isinstance(subject_did, str):
|
|
378
|
+
return AdmissionResult(False, "root_mismatch", "credentialSubject has no subject id")
|
|
379
|
+
severed = subject_severed_score(receipts, subject=subject_did, is_valid=is_valid)
|
|
380
|
+
if severed < policy.min_reputation_score:
|
|
381
|
+
return AdmissionResult(
|
|
382
|
+
False,
|
|
383
|
+
"below_threshold",
|
|
384
|
+
f"subject's collusion-severed score {severed:g} < {policy.min_reputation_score:g}",
|
|
385
|
+
)
|
|
386
|
+
|
|
387
|
+
return AdmissionResult.admitted()
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
__all__ = [
|
|
391
|
+
"AdmissionPolicy",
|
|
392
|
+
"AdmissionResult",
|
|
393
|
+
"admit",
|
|
394
|
+
"admit_over_published_ledger",
|
|
395
|
+
"subject_severed_score",
|
|
396
|
+
]
|
sm_parc/context.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""PARC constants — the credential @context, type, and proof identifiers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
# W3C Verifiable Credentials 2.0 base context + the PARC term context.
|
|
6
|
+
CONTEXT_V2 = "https://www.w3.org/ns/credentials/v2"
|
|
7
|
+
PARC_CONTEXT = "https://stellarminds.ai/contexts/parc/v0.1"
|
|
8
|
+
|
|
9
|
+
# Credential type. A ReputationCredential is a VC whose credentialSubject is a
|
|
10
|
+
# VRP verifiable-receipts facet (behavioral_merkle_root + nanda-rep/0.2 scores).
|
|
11
|
+
CREDENTIAL_TYPE = ["VerifiableCredential", "ReputationCredential"]
|
|
12
|
+
|
|
13
|
+
# Proof suite identifier (Ed25519 over the JCS-canonical VC sans-proof — the same
|
|
14
|
+
# canonical signing path as ARP/DAT receipts).
|
|
15
|
+
PROOF_TYPE = "Ed25519Signature2020"
|
|
16
|
+
|
|
17
|
+
__all__ = ["CONTEXT_V2", "CREDENTIAL_TYPE", "PARC_CONTEXT", "PROOF_TYPE"]
|
sm_parc/credential.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""The reputation credential — build + verify a W3C VC over a VRP ledger.
|
|
2
|
+
|
|
3
|
+
A ReputationCredential wraps a VRP Receipts Ledger's commitment + scores
|
|
4
|
+
(``behavioral_merkle_root`` + the ``nanda-rep/0.2`` ``reputation_score`` /
|
|
5
|
+
``validity_rate`` / ``corroboration_rate``) as a W3C Verifiable Credential, signed
|
|
6
|
+
by the originating chapter or a credentialed auditor. The proof is Ed25519 over the
|
|
7
|
+
JCS-canonical VC sans-``proof`` — the same canonical signing path as ARP receipts
|
|
8
|
+
and DATs, so the whole stack verifies signatures one way.
|
|
9
|
+
|
|
10
|
+
The credential is the *portable* form: an agent carries it to a new chapter, which
|
|
11
|
+
verifies the proof and recomputes the ledger (see ``sm_parc.admission``) without
|
|
12
|
+
trusting the issuing chapter's live server. The default scoring method is the
|
|
13
|
+
corroborated, collusion-resistant ``nanda-rep/0.2`` — a wash-trading ring's score
|
|
14
|
+
is severed to ~0, so it cannot buy its way past the gate.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import base64
|
|
20
|
+
from typing import Any
|
|
21
|
+
|
|
22
|
+
import base58
|
|
23
|
+
import jcs
|
|
24
|
+
from cryptography.exceptions import InvalidSignature
|
|
25
|
+
from cryptography.hazmat.primitives.asymmetric.ed25519 import (
|
|
26
|
+
Ed25519PrivateKey,
|
|
27
|
+
Ed25519PublicKey,
|
|
28
|
+
)
|
|
29
|
+
from sm_arp.vrp import SCORING_METHOD_V2
|
|
30
|
+
|
|
31
|
+
from .context import CONTEXT_V2, CREDENTIAL_TYPE, PARC_CONTEXT, PROOF_TYPE
|
|
32
|
+
|
|
33
|
+
_DID_PREFIX = b"\xed\x01"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def did_from_private_key(sk_bytes: bytes) -> str:
|
|
37
|
+
"""did:key for a 32-byte Ed25519 seed."""
|
|
38
|
+
pk = Ed25519PrivateKey.from_private_bytes(sk_bytes).public_key().public_bytes_raw()
|
|
39
|
+
return "did:key:z" + base58.b58encode(_DID_PREFIX + pk).decode("ascii")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _pubkey_from_did(did_key: str) -> Ed25519PublicKey:
|
|
43
|
+
if not did_key.startswith("did:key:z"):
|
|
44
|
+
raise ValueError(f"unsupported DID method: {did_key!r}")
|
|
45
|
+
decoded = base58.b58decode(did_key[len("did:key:z") :])
|
|
46
|
+
if len(decoded) != 34 or decoded[:2] != _DID_PREFIX:
|
|
47
|
+
raise ValueError("not a did:key Ed25519 record")
|
|
48
|
+
return Ed25519PublicKey.from_public_bytes(decoded[2:])
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _canonical_bytes_for_signing(vc: dict[str, Any]) -> bytes:
|
|
52
|
+
"""JCS-canonical bytes of the VC with the ``proof`` removed."""
|
|
53
|
+
return bytes(jcs.canonicalize({k: v for k, v in vc.items() if k != "proof"}))
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def build_reputation_credential(
|
|
57
|
+
*,
|
|
58
|
+
ledger: dict[str, Any],
|
|
59
|
+
issuer_sk: bytes,
|
|
60
|
+
issuer_did: str | None = None,
|
|
61
|
+
valid_from: str,
|
|
62
|
+
valid_until: str,
|
|
63
|
+
credential_id: str | None = None,
|
|
64
|
+
ledger_uri: str | None = None,
|
|
65
|
+
) -> dict[str, Any]:
|
|
66
|
+
"""Build + sign a ReputationCredential over ``ledger``.
|
|
67
|
+
|
|
68
|
+
Pass ``ledger_uri`` to mint a POINTER-mode credential: the facet then carries a
|
|
69
|
+
handle to a published community ledger instead of expecting the subject's receipts
|
|
70
|
+
to be presented inline. A pointer credential is what
|
|
71
|
+
:func:`sm_parc.admit_over_published_ledger` consumes — the verifier fetches that
|
|
72
|
+
ledger and re-runs collusion severance itself (see ``THREATMODEL.md``).
|
|
73
|
+
|
|
74
|
+
``credentialSubject`` IS the VRP facet: the ledger's
|
|
75
|
+
``behavioral_merkle_root`` + nanda-rep scores + subject + ledger pointer. The
|
|
76
|
+
facet mirrors the ledger's own ``scoring_method`` (defaulting to the
|
|
77
|
+
corroborated ``nanda-rep/0.2``); when the ledger is ``nanda-rep/0.2`` the facet
|
|
78
|
+
also carries its ``corroboration_rate`` (the 0.2 collusion-resistance signal).
|
|
79
|
+
The issuer (``issuer_did``, defaulting to the key's own did) is the chapter
|
|
80
|
+
(self-attested) or an auditor. ``valid_from`` / ``valid_until`` are RFC 3339
|
|
81
|
+
(caller-supplied; this module takes no wall-clock).
|
|
82
|
+
"""
|
|
83
|
+
issuer_did = issuer_did or did_from_private_key(issuer_sk)
|
|
84
|
+
subject: dict[str, Any] = {
|
|
85
|
+
"id": ledger["subject"],
|
|
86
|
+
"behavioral_merkle_root": ledger.get("behavioral_merkle_root"),
|
|
87
|
+
"scoring_method": ledger.get("scoring_method", SCORING_METHOD_V2),
|
|
88
|
+
"reputation_score": ledger.get("reputation_score"),
|
|
89
|
+
"validity_rate": ledger.get("validity_rate"),
|
|
90
|
+
"receipt_count": ledger.get("receipt_count"),
|
|
91
|
+
"as_of": ledger.get("as_of"),
|
|
92
|
+
}
|
|
93
|
+
# nanda-rep/0.2 facets carry the corroboration rate (the collusion-resistance
|
|
94
|
+
# signal); a 0.1 ledger has none, so the field is omitted there.
|
|
95
|
+
if "corroboration_rate" in ledger:
|
|
96
|
+
subject["corroboration_rate"] = ledger.get("corroboration_rate")
|
|
97
|
+
# Pointer mode: the facet names the published community ledger the verifier must
|
|
98
|
+
# fetch + re-sever, rather than carrying the subject's receipts inline.
|
|
99
|
+
if ledger_uri is not None:
|
|
100
|
+
subject["ledger_uri"] = ledger_uri
|
|
101
|
+
vc: dict[str, Any] = {
|
|
102
|
+
"@context": [CONTEXT_V2, PARC_CONTEXT],
|
|
103
|
+
"type": list(CREDENTIAL_TYPE),
|
|
104
|
+
"issuer": issuer_did,
|
|
105
|
+
"validFrom": valid_from,
|
|
106
|
+
"validUntil": valid_until,
|
|
107
|
+
"credentialSubject": subject,
|
|
108
|
+
}
|
|
109
|
+
if credential_id is not None:
|
|
110
|
+
vc["id"] = credential_id
|
|
111
|
+
sig = Ed25519PrivateKey.from_private_bytes(issuer_sk).sign(_canonical_bytes_for_signing(vc))
|
|
112
|
+
vc["proof"] = {
|
|
113
|
+
"type": PROOF_TYPE,
|
|
114
|
+
"verificationMethod": issuer_did,
|
|
115
|
+
"proofValue": base64.b64encode(sig).decode("ascii"),
|
|
116
|
+
}
|
|
117
|
+
return vc
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def verify_credential_proof(vc: dict[str, Any]) -> bool:
|
|
121
|
+
"""True iff the VC's Ed25519 proof verifies under its ``issuer`` did:key over
|
|
122
|
+
the JCS-canonical VC sans-``proof``."""
|
|
123
|
+
proof = vc.get("proof")
|
|
124
|
+
issuer = vc.get("issuer")
|
|
125
|
+
if not isinstance(proof, dict) or not isinstance(issuer, str):
|
|
126
|
+
return False
|
|
127
|
+
proof_value = proof.get("proofValue")
|
|
128
|
+
if not isinstance(proof_value, str):
|
|
129
|
+
return False
|
|
130
|
+
try:
|
|
131
|
+
pubkey = _pubkey_from_did(issuer)
|
|
132
|
+
pubkey.verify(base64.b64decode(proof_value), _canonical_bytes_for_signing(vc))
|
|
133
|
+
except (InvalidSignature, ValueError):
|
|
134
|
+
return False
|
|
135
|
+
except Exception:
|
|
136
|
+
return False
|
|
137
|
+
return True
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
__all__ = [
|
|
141
|
+
"build_reputation_credential",
|
|
142
|
+
"did_from_private_key",
|
|
143
|
+
"verify_credential_proof",
|
|
144
|
+
]
|
sm_parc/disclosure.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"""Selective disclosure — reveal a few receipts from a large ledger, provably.
|
|
2
|
+
|
|
3
|
+
As an agent transacts across trust domains its Receipts Ledger grows without bound,
|
|
4
|
+
and handing the whole history to every gate is neither private nor scalable. PARC
|
|
5
|
+
already commits the ledger as a ``behavioral_merkle_root``; this module lets a holder
|
|
6
|
+
reveal only the *receipts worth showing* plus a **Merkle inclusion proof** for each, so
|
|
7
|
+
a verifier confirms every revealed receipt sits under the signed root **without ever
|
|
8
|
+
seeing the rest of the ledger**.
|
|
9
|
+
|
|
10
|
+
The proof is the dual of recomputation: where ``sm_parc.admit`` recomputes the whole root,
|
|
11
|
+
:func:`verify_inclusion` folds one leaf up its authentication path to the same root.
|
|
12
|
+
Pure SHA-256 over the *identical* leaf/tree construction as
|
|
13
|
+
``sm_arp.vrp.behavioral_merkle_root`` (leaves sorted by ``issued_at`` then
|
|
14
|
+
``receipt_id``; node = ``SHA-256(left || right)``; an odd node is duplicated). No new
|
|
15
|
+
crypto, no zero-knowledge — just the commitment PARC already carries.
|
|
16
|
+
|
|
17
|
+
Scope: this discloses *which receipts happened* (the transactions). It does NOT
|
|
18
|
+
recompute the reputation score — that is an aggregate over the whole graph (collusion
|
|
19
|
+
severance needs every edge), so the score stays the signed value over the full root.
|
|
20
|
+
The two are deliberately separate layers; see ``THREATMODEL.md``.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from __future__ import annotations
|
|
24
|
+
|
|
25
|
+
import hashlib
|
|
26
|
+
from typing import Any, TypedDict
|
|
27
|
+
|
|
28
|
+
import jcs
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class InclusionStep(TypedDict):
|
|
32
|
+
sibling: str # hex of the sibling node hash
|
|
33
|
+
position: str # "left" if the sibling is the left input, else "right"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class InclusionProof(TypedDict):
|
|
37
|
+
leaf_index: int # index of the receipt in canonical (sorted) order
|
|
38
|
+
leaf_count: int # total leaves committed (lets a verifier sanity-check scope)
|
|
39
|
+
path: list[InclusionStep]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _leaf_hash(receipt: dict[str, Any]) -> bytes:
|
|
43
|
+
"""SHA-256 of the receipt's JCS bytes — the identical leaf as the VRP root."""
|
|
44
|
+
return hashlib.sha256(jcs.canonicalize(receipt)).digest()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _ordered_leaves(receipts: list[dict[str, Any]]) -> list[bytes]:
|
|
48
|
+
ordered = sorted(receipts, key=lambda r: (r.get("issued_at", ""), r.get("receipt_id", "")))
|
|
49
|
+
return [_leaf_hash(r) for r in ordered]
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def inclusion_proof(receipts: list[dict[str, Any]], *, receipt: dict[str, Any]) -> InclusionProof:
|
|
53
|
+
"""Build a Merkle inclusion proof that ``receipt`` is committed in ``receipts``.
|
|
54
|
+
|
|
55
|
+
Returns the leaf index, the total leaf count, and the authentication path (each
|
|
56
|
+
step is a sibling hash + which side it sits on). Raises ``ValueError`` if the
|
|
57
|
+
receipt is not in the ledger. The proof verifies against the SAME
|
|
58
|
+
``behavioral_merkle_root`` the credential signs over — see :func:`verify_inclusion`.
|
|
59
|
+
"""
|
|
60
|
+
leaves = _ordered_leaves(receipts)
|
|
61
|
+
target = _leaf_hash(receipt)
|
|
62
|
+
try:
|
|
63
|
+
index = leaves.index(target)
|
|
64
|
+
except ValueError as exc:
|
|
65
|
+
raise ValueError("receipt is not present in the ledger") from exc
|
|
66
|
+
|
|
67
|
+
path: list[InclusionStep] = []
|
|
68
|
+
level = leaves
|
|
69
|
+
i = index
|
|
70
|
+
while len(level) > 1:
|
|
71
|
+
if len(level) % 2 == 1:
|
|
72
|
+
level = [*level, level[-1]] # duplicate the final node (matches the root)
|
|
73
|
+
sibling = i ^ 1
|
|
74
|
+
# If the node is the left input (even index), its sibling is on the right.
|
|
75
|
+
position = "right" if i % 2 == 0 else "left"
|
|
76
|
+
path.append({"sibling": level[sibling].hex(), "position": position})
|
|
77
|
+
level = [hashlib.sha256(level[j] + level[j + 1]).digest() for j in range(0, len(level), 2)]
|
|
78
|
+
i //= 2
|
|
79
|
+
|
|
80
|
+
return {"leaf_index": index, "leaf_count": len(leaves), "path": path}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def verify_inclusion(receipt: dict[str, Any], proof: InclusionProof, root: str) -> bool:
|
|
84
|
+
"""True iff ``receipt`` folds up ``proof.path`` to ``root`` (a ``sha256:<hex>``).
|
|
85
|
+
|
|
86
|
+
The verifier recomputes only the revealed receipt's leaf and walks the path — it
|
|
87
|
+
never needs the other receipts. A wrong receipt, a tampered path, or a mismatched
|
|
88
|
+
root all yield False.
|
|
89
|
+
"""
|
|
90
|
+
if not isinstance(root, str) or not root.startswith("sha256:"):
|
|
91
|
+
return False
|
|
92
|
+
node = _leaf_hash(receipt)
|
|
93
|
+
for step in proof.get("path", []):
|
|
94
|
+
try:
|
|
95
|
+
sibling = bytes.fromhex(step["sibling"])
|
|
96
|
+
except (ValueError, KeyError, TypeError):
|
|
97
|
+
return False
|
|
98
|
+
if step.get("position") == "left":
|
|
99
|
+
node = hashlib.sha256(sibling + node).digest()
|
|
100
|
+
else:
|
|
101
|
+
node = hashlib.sha256(node + sibling).digest()
|
|
102
|
+
return "sha256:" + node.hex() == root
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
__all__ = ["InclusionProof", "InclusionStep", "inclusion_proof", "verify_inclusion"]
|
sm_parc/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sm-parc
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Portable Agent Reputation Credential — a verifiable, recomputable reputation VC consumed at chapter admission
|
|
5
|
+
Project-URL: Homepage, https://labs.stellarminds.ai
|
|
6
|
+
Project-URL: Repository, https://github.com/Sharathvc23/sm-parc
|
|
7
|
+
Project-URL: Documentation, https://github.com/Sharathvc23/sm-parc/blob/main/docs/WALKTHROUGH.md
|
|
8
|
+
Project-URL: Live demo, https://demoparc.stellarminds.ai
|
|
9
|
+
Author: labs.stellarminds.ai
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: agent,did,ed25519,nanda,reputation,trust,verifiable-credential
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Security :: Cryptography
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.11
|
|
22
|
+
Requires-Dist: base58>=2.1
|
|
23
|
+
Requires-Dist: cryptography>=41.0
|
|
24
|
+
Requires-Dist: jcs<0.3,>=0.2
|
|
25
|
+
Requires-Dist: sm-arp<0.3,>=0.2.3
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: coverage>=7.0; extra == 'dev'
|
|
28
|
+
Requires-Dist: mypy>=1.0; extra == 'dev'
|
|
29
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
30
|
+
Requires-Dist: ruff>=0.4; extra == 'dev'
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# PARC — Portable Agent Reputation Credential
|
|
34
|
+
|
|
35
|
+
**A verifiable, recomputable reputation Verifiable Credential consumed at chapter admission**
|
|
36
|
+
|
|
37
|
+
Agent reputation today is siloed: each platform (or chapter) holds its own scores, and
|
|
38
|
+
an agent that moves to a new trust domain starts from zero. PARC makes reputation
|
|
39
|
+
**portable**. It wraps a [Verifiable Receipts Profile (VRP)](https://github.com/Sharathvc23/sm-arp)
|
|
40
|
+
Receipts Ledger's commitment + scores — a `behavioral_merkle_root` plus the reproducible,
|
|
41
|
+
corroborated `nanda-rep/0.2` `reputation_score` / `validity_rate` / `corroboration_rate` —
|
|
42
|
+
as a W3C Verifiable Credential, signed by the originating chapter or a credentialed
|
|
43
|
+
auditor. A new chapter consumes it at admission: it verifies the signature and
|
|
44
|
+
**recomputes the ledger itself**, so it never has to trust the issuing chapter's live server.
|
|
45
|
+
|
|
46
|
+
The one thing PARC owns: the **credential envelope + the admission gate**. The commitment
|
|
47
|
+
and scoring are VRP's; PARC makes them carriable and checkable across trust domains.
|
|
48
|
+
|
|
49
|
+
## See it live
|
|
50
|
+
|
|
51
|
+
**▶︎ [demoparc.stellarminds.ai](https://demoparc.stellarminds.ai)** — an interactive viewer
|
|
52
|
+
that **re-verifies every signature and Merkle proof in your browser** (no server trusted).
|
|
53
|
+
Switch between the *Admission* and *Three-city journey* tabs; click any credential, agent,
|
|
54
|
+
or receipt to inspect it.
|
|
55
|
+
|
|
56
|
+
<p align="center">
|
|
57
|
+
<img src="docs/images/journey-workflow.gif" alt="PARC viewer walkthrough: the three-city journey, a Merkle inclusion proof, and a stop's signed credential" width="760">
|
|
58
|
+
</p>
|
|
59
|
+
|
|
60
|
+
A full picture-by-picture tour is in [`docs/WALKTHROUGH.md`](./docs/WALKTHROUGH.md).
|
|
61
|
+
|
|
62
|
+
## What this package secures (parc/0.1)
|
|
63
|
+
|
|
64
|
+
- **Verifiable.** A reputation credential is a W3C VC with an Ed25519 proof over the
|
|
65
|
+
JCS-canonical credential — the same signing path as Agency Receipt Protocol (ARP)
|
|
66
|
+
receipts and Delegated Authority Tokens (DATs).
|
|
67
|
+
- **Recomputable, not asserted.** The admission gate recomputes the
|
|
68
|
+
`behavioral_merkle_root` and the `nanda-rep/0.2` scores from the presented ledger and
|
|
69
|
+
rejects any divergence — a signed-but-inflated score cannot pass.
|
|
70
|
+
- **Collusion-resistant.** The gate admits only on the corroborated `nanda-rep/0.2`
|
|
71
|
+
score: a credential carrying the un-corroborated `nanda-rep/0.1` score is rejected, and
|
|
72
|
+
a wash-trading ring's self-dealt receipts are severed to a score of ~0, so it fails the
|
|
73
|
+
threshold.
|
|
74
|
+
- **Adversarially tested.** Hostile-path tests for: tampered credential, untrusted issuer,
|
|
75
|
+
tampered ledger (root mismatch), issuer-inflated-and-re-signed score (score mismatch),
|
|
76
|
+
0.1-facet rejection, un-corroborated/severed score, below-threshold, stale, revoked,
|
|
77
|
+
wrong-mode, and refs-only (non-recomputable).
|
|
78
|
+
|
|
79
|
+
A **valid signature is not admission** — the gate trusts the issuer's key, then recomputes
|
|
80
|
+
the score and applies its own threshold, so a genuinely-signed wash credential is still
|
|
81
|
+
rejected:
|
|
82
|
+
|
|
83
|
+
<p align="center">
|
|
84
|
+
<img src="docs/images/passport-drawer.png" alt="The passport drawer: signature verified (green) but admission rejected (red) — score 0 below threshold" width="380">
|
|
85
|
+
</p>
|
|
86
|
+
|
|
87
|
+
## Two admission modes
|
|
88
|
+
|
|
89
|
+
- **Inline** (`admit`) — the credential carries the subject's own receipts; the gate
|
|
90
|
+
recomputes them fully offline. Catches self-dealing, but a single-subject graph is a
|
|
91
|
+
star, so it *cannot* show an N-party collusion ring.
|
|
92
|
+
- **Pointer** (`admit_over_published_ledger`) — the credential names a published community
|
|
93
|
+
ledger (`credentialSubject.ledger_uri`); the gate fetches it, checks it against the
|
|
94
|
+
signed root, and **re-runs the collusion severance itself**, deriving the subject's
|
|
95
|
+
severed score over the full graph. This catches N-party Sybil rings — at the cost of
|
|
96
|
+
fetching the issuer's whole ledger (privacy + availability), and a residual that moves
|
|
97
|
+
to *ledger completeness*. See
|
|
98
|
+
[`examples/published_ledger_admission.py`](./examples/published_ledger_admission.py) and
|
|
99
|
+
[`THREATMODEL.md`](./THREATMODEL.md).
|
|
100
|
+
|
|
101
|
+
## One passport, three worlds
|
|
102
|
+
|
|
103
|
+
A single agent earns reputation transacting across a **community**, a **marketplace**, and
|
|
104
|
+
an **enterprise**, carrying one PARC. Its corroborated score **compounds** as it goes — and
|
|
105
|
+
that accumulated reputation is what opens the high-bar enterprise gate (shown cold, the
|
|
106
|
+
same agent is rejected). Transactions, portability, and security in one picture:
|
|
107
|
+
|
|
108
|
+
<p align="center">
|
|
109
|
+
<img src="docs/images/journey.png" alt="The A→B→C journey: scores compound 60→130→200 against rising admission bars, all admitted" width="820">
|
|
110
|
+
</p>
|
|
111
|
+
|
|
112
|
+
And when the ledger grows too large to hand over, the agent reveals **only the receipts
|
|
113
|
+
worth showing** plus a **Merkle inclusion proof** for each — confirmed under the signed
|
|
114
|
+
root without exposing the rest:
|
|
115
|
+
|
|
116
|
+
<p align="center">
|
|
117
|
+
<img src="docs/images/selective-disclosure.png" alt="Selective disclosure: 3 of 40 receipts revealed, each with its Merkle inclusion proof verifying to the signed root" width="820">
|
|
118
|
+
</p>
|
|
119
|
+
|
|
120
|
+
Runnable: [`examples/three_city_economy.py`](./examples/three_city_economy.py).
|
|
121
|
+
|
|
122
|
+
## What this package does NOT (yet) do
|
|
123
|
+
|
|
124
|
+
- **Run the auditor.** PARC *verifies* an auditor's attestation; who computes and signs it
|
|
125
|
+
is deployment policy — the consumer's responsibility.
|
|
126
|
+
- **Catch a *curated* ledger.** Pointer mode re-severs only what the issuer published; a
|
|
127
|
+
colluding issuer that omits the honest anchor defeats it. Closing that needs an
|
|
128
|
+
independent receipt source (cross-attestation), not the published ledger.
|
|
129
|
+
- **Unlinkable disclosure.** Selective disclosure of *which receipts happened* ships today
|
|
130
|
+
via Merkle inclusion proofs (below); an unlinkable / BBS+ variant and a zero-knowledge
|
|
131
|
+
*score* proof are future properties; see [`SPEC.md`](./SPEC.md).
|
|
132
|
+
|
|
133
|
+
## Features
|
|
134
|
+
|
|
135
|
+
- `build_reputation_credential(...)` / `verify_credential_proof(...)` — issue + verify the VC.
|
|
136
|
+
- `AdmissionPolicy` + `admit(...)` — the inline chapter-admission gate (signature →
|
|
137
|
+
trusted issuer → revocation/freshness → recompute root + scores → thresholds).
|
|
138
|
+
- `admit_over_published_ledger(...)` + `subject_severed_score(...)` — the pointer-mode
|
|
139
|
+
gate: fetch the named ledger, re-verify the root, and derive the subject's
|
|
140
|
+
collusion-severed score over the full community graph.
|
|
141
|
+
- `inclusion_proof(...)` + `verify_inclusion(...)` — **selective disclosure**: reveal a few
|
|
142
|
+
receipts of a large ledger plus a Merkle inclusion proof for each, confirmed under the
|
|
143
|
+
credential's signed root without exposing the rest.
|
|
144
|
+
|
|
145
|
+
## Installation
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
pip install sm-parc # depends on sm-arp>=0.2.3,<0.3 (carries sm_arp.vrp)
|
|
149
|
+
# working draft: pip install git+https://github.com/Sharathvc23/sm-parc.git
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Quick start
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
from sm_arp.vrp import build_ledger
|
|
156
|
+
from sm_parc import build_reputation_credential, AdmissionPolicy, admit
|
|
157
|
+
|
|
158
|
+
# Issuer (chapter or auditor) builds a corroborated nanda-rep/0.2 ledger from an
|
|
159
|
+
# agent's receipts, then a parc/0.1 VC over it.
|
|
160
|
+
ledger = build_ledger(subject=agent_did, receipts=receipts, method="nanda-rep/0.2",
|
|
161
|
+
is_valid=lambda r: verify_receipt(r).ok, as_of="2026-06-07T00:00:00Z")
|
|
162
|
+
vc = build_reputation_credential(ledger=ledger, issuer_sk=chapter_sk,
|
|
163
|
+
valid_from="2026-06-07T00:00:00Z",
|
|
164
|
+
valid_until="2026-07-07T00:00:00Z")
|
|
165
|
+
|
|
166
|
+
# A new chapter admits the agent — verifies the signature AND recomputes the ledger.
|
|
167
|
+
policy = AdmissionPolicy(trusted_issuers={chapter_did}, min_reputation_score=50.0)
|
|
168
|
+
result = admit(vc, policy=policy, ledger=ledger, is_valid=lambda r: verify_receipt(r).ok)
|
|
169
|
+
assert result.ok # admitted, without trusting the issuing chapter's server
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
See [`examples/mint_and_admit.py`](./examples/mint_and_admit.py) for a runnable end-to-end
|
|
173
|
+
demo (honest corroborated agent admitted; wash-trade agent rejected).
|
|
174
|
+
|
|
175
|
+
## Reference fixtures
|
|
176
|
+
|
|
177
|
+
The behavioural suite under `tests/` builds ledgers + credentials with deterministic test
|
|
178
|
+
keys and exercises every admission outcome. The suite IS the spec (see
|
|
179
|
+
[`GOVERNANCE.md`](./GOVERNANCE.md)); placeholder keys are test-only.
|
|
180
|
+
|
|
181
|
+
## Documentation
|
|
182
|
+
|
|
183
|
+
- [`docs/WALKTHROUGH.md`](./docs/WALKTHROUGH.md) — illustrated tour of the viewer + the mechanisms.
|
|
184
|
+
- [`SPEC.md`](./SPEC.md) — normative credential + admission profile (working draft).
|
|
185
|
+
- [`WHITEPAPER.md`](./WHITEPAPER.md) — design rationale.
|
|
186
|
+
- [`THREATMODEL.md`](./THREATMODEL.md) — what the gate does and does **not** defend.
|
|
187
|
+
- [`GLOSSARY.md`](./GLOSSARY.md) — every acronym (ARP, VRP, PARC, DAT, …) expanded.
|
|
188
|
+
|
|
189
|
+
## Related packages
|
|
190
|
+
|
|
191
|
+
| Package | Role |
|
|
192
|
+
| --- | --- |
|
|
193
|
+
| [`sm-arp`](https://github.com/Sharathvc23/sm-arp) | Agency Receipts + the VRP core (`sm_arp.vrp`) PARC composes |
|
|
194
|
+
| [`sm-conformance`](https://github.com/Sharathvc23/sm-conformance) | the signed-badge / signing infrastructure pattern |
|
|
195
|
+
|
|
196
|
+
## License
|
|
197
|
+
|
|
198
|
+
[MIT](./LICENSE)
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
Built at [labs.stellarminds.ai](https://labs.stellarminds.ai).
|
|
203
|
+
|
|
204
|
+
*First published: 2026-06-07 | Last modified: 2026-06-21*
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
sm_parc/__init__.py,sha256=U1JxzjQwYH-lWCLDrEDrN_7CGGzbeekRzoejk_Y0yCg,1300
|
|
2
|
+
sm_parc/admission.py,sha256=Z-K1LHiVZ6e1RsTJBFwgDLBL-EBReGyS0YZm2wvINzM,19068
|
|
3
|
+
sm_parc/context.py,sha256=6j8aQXnYJ4z4Rp0zpeVlnGLs4WIKbEZfbnmKNsCpY_4,770
|
|
4
|
+
sm_parc/credential.py,sha256=eVXhwhiEwsPYx5avmSDafuqkv2-pVlimFMvYyBlAGjc,5911
|
|
5
|
+
sm_parc/disclosure.py,sha256=d-yXNRjh-sXjr67A4HVOzXEFDR69HmKphaplnGSbraA,4594
|
|
6
|
+
sm_parc/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
sm_parc-0.2.0.dist-info/METADATA,sha256=C4HXpMtfYa03nDdj2Eq8vO5fUvsET9VbCrf0i6qKDow,10278
|
|
8
|
+
sm_parc-0.2.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
|
|
9
|
+
sm_parc-0.2.0.dist-info/licenses/LICENSE,sha256=mo4bEHboox6Cvk1DuRHhTstvfo1NRFPs5QqRfvrufBc,1072
|
|
10
|
+
sm_parc-0.2.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 stellarminds.ai
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|