sm-signing 0.3.1__tar.gz

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.
Files changed (41) hide show
  1. sm_signing-0.3.1/.github/CODEOWNERS +1 -0
  2. sm_signing-0.3.1/.github/workflows/ci.yml +59 -0
  3. sm_signing-0.3.1/.github/workflows/release.yml +52 -0
  4. sm_signing-0.3.1/.gitignore +14 -0
  5. sm_signing-0.3.1/CHANGELOG.md +163 -0
  6. sm_signing-0.3.1/CONTRIBUTING.md +31 -0
  7. sm_signing-0.3.1/GOVERNANCE.md +34 -0
  8. sm_signing-0.3.1/LICENSE +21 -0
  9. sm_signing-0.3.1/Makefile +11 -0
  10. sm_signing-0.3.1/PKG-INFO +253 -0
  11. sm_signing-0.3.1/PUBLISHING.md +42 -0
  12. sm_signing-0.3.1/README.md +224 -0
  13. sm_signing-0.3.1/SPEC.md +456 -0
  14. sm_signing-0.3.1/WHITEPAPER.md +96 -0
  15. sm_signing-0.3.1/examples/.gitkeep +0 -0
  16. sm_signing-0.3.1/examples/did_key.py +21 -0
  17. sm_signing-0.3.1/examples/hybrid.py +143 -0
  18. sm_signing-0.3.1/examples/sign_verify.py +29 -0
  19. sm_signing-0.3.1/pyproject.toml +55 -0
  20. sm_signing-0.3.1/sm_signing/__init__.py +219 -0
  21. sm_signing-0.3.1/sm_signing/backends/__init__.py +7 -0
  22. sm_signing-0.3.1/sm_signing/backends/ed25519.py +64 -0
  23. sm_signing-0.3.1/sm_signing/backends/mldsa.py +106 -0
  24. sm_signing-0.3.1/sm_signing/base.py +102 -0
  25. sm_signing-0.3.1/sm_signing/hybrid.py +1457 -0
  26. sm_signing-0.3.1/sm_signing/multicodec.py +251 -0
  27. sm_signing-0.3.1/sm_signing/py.typed +0 -0
  28. sm_signing-0.3.1/tests/conftest.py +62 -0
  29. sm_signing-0.3.1/tests/hybrid_support.py +141 -0
  30. sm_signing-0.3.1/tests/test_examples.py +54 -0
  31. sm_signing-0.3.1/tests/test_hybrid.py +693 -0
  32. sm_signing-0.3.1/tests/test_hybrid_binding.py +718 -0
  33. sm_signing-0.3.1/tests/test_hybrid_hostile.py +844 -0
  34. sm_signing-0.3.1/tests/test_hybrid_trust.py +704 -0
  35. sm_signing-0.3.1/tests/test_hybrid_vectors.py +90 -0
  36. sm_signing-0.3.1/tests/test_multicodec.py +329 -0
  37. sm_signing-0.3.1/tests/test_no_pq_backend.py +231 -0
  38. sm_signing-0.3.1/tests/test_signing.py +252 -0
  39. sm_signing-0.3.1/tests/test_zero_dep.py +92 -0
  40. sm_signing-0.3.1/tests/vectors/hybrid_v1.json +54 -0
  41. sm_signing-0.3.1/uv.lock +481 -0
@@ -0,0 +1 @@
1
+ * @stellarminds-ai
@@ -0,0 +1,59 @@
1
+ name: ci
2
+
3
+ on:
4
+ push:
5
+ branches: [main, master]
6
+ pull_request:
7
+
8
+ jobs:
9
+ ci-local:
10
+ runs-on: ubuntu-latest
11
+ strategy:
12
+ matrix:
13
+ python-version: ["3.11", "3.12"]
14
+ steps:
15
+ - uses: actions/checkout@v4
16
+ - uses: astral-sh/setup-uv@v5
17
+ - name: Pin Python
18
+ run: uv python pin ${{ matrix.python-version }}
19
+ # The same gate as `make ci-local` — green locally == green here.
20
+ - run: uv sync --extra dev
21
+ - run: uv run ruff check .
22
+ - run: uv run ruff format --check .
23
+ - run: uv run mypy sm_signing
24
+ # Both backends must be present here: a missing one is an error, not a skip.
25
+ - run: SM_SIGNING_REQUIRE_BACKENDS=1 uv run pytest -v
26
+ - name: Examples run
27
+ run: |
28
+ uv run python examples/sign_verify.py
29
+ uv run python examples/did_key.py
30
+ uv run python examples/hybrid.py
31
+
32
+ # The same suite where ML-DSA cannot be verified, for real rather than by
33
+ # patching: `cryptography` 46.x has no ML-DSA module, 47.0 has the module but
34
+ # cannot run it, and "none" removes the package. Tests that need a missing
35
+ # backend skip; tests/test_no_pq_backend.py runs only here and checks that the
36
+ # hybrid profile reports "not checked" under `auto` and rejects under
37
+ # `pq-required`.
38
+ no-pq-backend:
39
+ runs-on: ubuntu-latest
40
+ strategy:
41
+ fail-fast: false
42
+ matrix:
43
+ cryptography: ["46.0.7", "47.0.0", "none"]
44
+ steps:
45
+ - uses: actions/checkout@v4
46
+ - uses: astral-sh/setup-uv@v5
47
+ - name: Pin Python
48
+ run: uv python pin 3.12
49
+ - run: uv sync --extra dev
50
+ - name: Replace cryptography (${{ matrix.cryptography }})
51
+ run: |
52
+ if [ "${{ matrix.cryptography }}" = "none" ]; then
53
+ uv pip uninstall cryptography
54
+ else
55
+ uv pip install "cryptography==${{ matrix.cryptography }}"
56
+ fi
57
+ # --no-sync: keep the environment as just arranged. SM_SIGNING_EXPECT_NO_PQ
58
+ # makes the run fail if an ML-DSA backend turns out to be usable after all.
59
+ - run: SM_SIGNING_EXPECT_NO_PQ=1 uv run --no-sync pytest -v
@@ -0,0 +1,52 @@
1
+ # Publish sm-signing to PyPI on a version tag, via PyPI Trusted Publishing (OIDC).
2
+ #
3
+ # No API token anywhere: PyPI is told (once, in its web UI) to trust THIS repo +
4
+ # THIS workflow file, and the `id-token: write` permission below lets the job
5
+ # mint a short-lived OIDC token PyPI accepts. See PUBLISHING.md for the one-time
6
+ # setup and the exact values to type into PyPI's "Add a publisher" form.
7
+ #
8
+ # Fires only when a tag like `v0.1.0` is pushed — never on normal commits.
9
+ name: release
10
+
11
+ on:
12
+ push:
13
+ tags:
14
+ - "v*"
15
+
16
+ permissions:
17
+ contents: read
18
+
19
+ jobs:
20
+ publish:
21
+ name: Build + publish to PyPI
22
+ runs-on: ubuntu-latest
23
+ permissions:
24
+ # A job-level permissions block REPLACES the workflow-level one (it does not
25
+ # merge), so contents:read must be restated here or actions/checkout 404s on
26
+ # its own repo. Both lines are required.
27
+ contents: read # for actions/checkout
28
+ id-token: write # REQUIRED for Trusted Publishing — this is what replaces the token.
29
+ steps:
30
+ - uses: actions/checkout@v4
31
+
32
+ - uses: actions/setup-python@v5
33
+ with:
34
+ python-version: "3.12"
35
+
36
+ - name: Build sdist + wheel
37
+ run: |
38
+ python -m pip install --upgrade build
39
+ python -m build
40
+
41
+ - name: Check the built distributions
42
+ run: |
43
+ python -m pip install --upgrade twine
44
+ python -m twine check dist/*
45
+
46
+ - name: Publish to PyPI
47
+ # No `with: password:` — the action uses the OIDC token automatically.
48
+ uses: pypa/gh-action-pypi-publish@release/v1
49
+ with:
50
+ # A re-pushed tag (e.g. a message reword) must no-op, not red-fail,
51
+ # when the version is already on PyPI.
52
+ skip-existing: true
@@ -0,0 +1,14 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.egg-info/
4
+ .pytest_cache/
5
+ .mypy_cache/
6
+ .ruff_cache/
7
+ .venv/
8
+ dist/
9
+ build/
10
+ uv.lock.tmp
11
+
12
+ # Development & reconnaissance notes — design scoping, competitive research,
13
+ # decision records. Kept local, never published.
14
+ dev/
@@ -0,0 +1,163 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/) and this project adheres to
5
+ [Semantic Versioning](https://semver.org/).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.3.1] — 2026-12-04
10
+
11
+ Hardening of the hybrid profile after a security review of 0.3.0. **No bytes on
12
+ the wire change**; the stored vector is unchanged. What changes is what a
13
+ verifier accepts and what it reports. 0.3.0 was never published.
14
+
15
+ ### Changed — behaviour a caller of 0.3.0 must know about
16
+ - **`pq-required` needs a key the verifier trusts.** A signature that verifies
17
+ only under the object's own `pub` no longer satisfies `pq-required`: the entry
18
+ is `not-checked` with the new reason `key-untrusted`, and the object is
19
+ rejected unless another entry verifies under a key from the `resolver` or the
20
+ key pinned for `subject`. An attacker who can forge the classical signature
21
+ can put any key in `pub`, and that is the attacker `pq-required` is for.
22
+ Opt in to the old behaviour with `trust_inline_pub=True`.
23
+ - **The resolver is always consulted** and its answer wins. In 0.3.0 it was
24
+ skipped when the entry carried `pub`. If both are present and differ, the
25
+ entry fails with the new reason `key-conflict`.
26
+ - **Verification no longer pins.** `verify_hybrid` and `verify_key_binding`
27
+ only read the binding store unless `pin_new=True` is passed. With it,
28
+ `verify_key_binding` enrols an accepted binding (recording `pub` too), and
29
+ `verify_hybrid` pins only when the object is accepted and every entry
30
+ verified, choosing the strongest suite instead of the first entry.
31
+ - **A pin is a commitment under `auto`.** When the store holds a pin for the
32
+ subject, `auto` is as strict as `pq-required` for it: no container, a stripped
33
+ container or an uncheckable entry rejects, and the key must be the pinned one.
34
+ The pinned key is also a key source for an entry without `pub`.
35
+ - **With a binding store, `subject` must be a canonical Ed25519 `did:key`.**
36
+ Anything else (a fragment, another case, another encoding of the same key) is
37
+ rejected with the new reason `invalid-subject` instead of getting a pin of
38
+ its own.
39
+ - **Limits, checked before decoding:** at most 8 entries; `pub` 4,096, `sig`
40
+ 8,192, `subject` 64 characters; `kid` exactly 71; nesting 32 levels and 1,024
41
+ nested values per container. `prepare_container` refuses more than 8 signers.
42
+ - **Verification does not raise on hostile data.** Deep nesting gives
43
+ `malformed-container` / `malformed-binding` instead of `RecursionError`. A
44
+ resolver or canonicalizer that raises, or returns the wrong type, gives a
45
+ rejecting result (new reasons `resolver-error`, `canonicalizer-error`); a
46
+ resolver returning a non-bytes value used to raise `TypeError`, and so did a
47
+ canonical-bytes function returning non-bytes.
48
+ - **Key binding:** a top-level member the v1 object does not define is rejected;
49
+ a small-order Ed25519 `subject` is rejected; `issued_at` digits must be ASCII.
50
+ - **`parse_did_key` / `multikey_decode` are strict and bounded:** non-minimal
51
+ multicodec varints and keys of the wrong length for their suite are rejected,
52
+ non-string input raises `ValueError`, and text longer than 4,104 / 4,096
53
+ characters is rejected without decoding. `base58btc_decode` and
54
+ `multibase_decode` have the same 4,096-character default limit; all four take
55
+ `max_len` to change it.
56
+ - **The Ed25519 backend refuses small-order public keys** (`ValueError` from
57
+ `verifier_for`): one constant signature verifies every message under them.
58
+ - `canonical_binding_bytes` rejects subclasses of `str` and nesting deeper than
59
+ 32 levels.
60
+ - A backend module that fails to import with `AttributeError` (a `cryptography`
61
+ too old to have the ML-DSA classes) surfaces as `BackendNotInstalled`.
62
+
63
+ ### Added
64
+ - `verify_hybrid(..., trust_inline_pub=False, pin_new=False)` and
65
+ `verify_key_binding(..., pin_new=False)`.
66
+ - `KeySource` (`resolver` / `pinned` / `inline`); `EntryResult.key_source`,
67
+ `HybridResult.key_source` and `HybridResult.pin_required`.
68
+ - `Reason.KEY_UNTRUSTED`, `KEY_CONFLICT`, `INVALID_SUBJECT`, `RESOLVER_ERROR`,
69
+ `CANONICALIZER_ERROR`.
70
+ - Limit constants `MAX_CONTAINER_ENTRIES`, `MAX_PUB_CHARS`, `MAX_SIG_CHARS`,
71
+ `MAX_KID_CHARS`, `MAX_SUBJECT_CHARS`, `MAX_NESTING_DEPTH`,
72
+ `MAX_CONTAINER_NODES`, `MAX_MULTIBASE_CHARS`, `MAX_DID_KEY_CHARS`; and
73
+ `PUBLIC_KEY_SIZES`, `is_small_order_ed25519_key`.
74
+ - CI job `no-pq-backend`: the suite with `cryptography` 46.0.7, 47.0.0 and
75
+ absent. The main job fails, instead of skipping, if a backend is missing.
76
+
77
+ ### Performance
78
+ - base58 encoding and decoding work ten digits at a time and one redundant
79
+ re-encoding per entry is gone: verifying one ML-DSA-65 entry takes about a
80
+ sixth of the time it did.
81
+
82
+ ### Documentation
83
+ - SPEC: §3.1 strict and bounded parsing; §4.3 key source and the trust rule of
84
+ `pq-required`; §4.4 result fields and reasons; §4.5; new §4.7 (limits) and
85
+ §4.8 (verification does not raise); §5.2 and §5.3 rewritten for explicit
86
+ enrolment and "a pin is a commitment".
87
+ - README: the `pq-required` quick start now shows a resolver and a pinned
88
+ binding, and says plainly that a key from `pub` alone is not enough.
89
+
90
+ ## [0.3.0] — 2026-12-04
91
+
92
+ ### Added
93
+ - **Hybrid profile `sm-signing/hybrid/v1`** (`sm_signing.hybrid`, pure stdlib; SPEC §4).
94
+ A signed object keeps its Ed25519 signature and carries ML-DSA (FIPS 204)
95
+ signatures in a container inside its signed body.
96
+ - `prepare_container` / `complete_container` build the container in two steps
97
+ around the caller's canonicalization; the ML-DSA key signs
98
+ `b"sm-signing/hybrid/v1\x00" + canonical_bytes`. `prepare_entry`, `key_id`,
99
+ `hybrid_signing_input` and `strip_signatures` are the pieces.
100
+ - `verify_hybrid` with policies `classical`, `auto` (default) and `pq-required`.
101
+ It returns a `HybridResult` that states `accepted`, `classical_ok`,
102
+ `pq_present`, and `pq_status` (`verified` / `failed` / `not-checked`) with a
103
+ `reason`, per entry as well. "Not checked" is never reported as verified;
104
+ `pq-required` fails closed on a missing container, backend or key.
105
+ - **Key binding** (SPEC §5): `build_key_binding` / `verify_key_binding` for the
106
+ standalone `sm-signing/key-binding/v1` object signed by both keys;
107
+ `canonical_binding_bytes`, a small serializer for that object only (its output
108
+ equals RFC 8785 for the object's restricted content); the `BindingStore`
109
+ protocol and `InMemoryBindingStore` (pin on first use, first-seen time).
110
+ - `multikey_encode` / `multikey_decode`: multibase(base58btc) of
111
+ `multicodec || raw public key` — the part of a `did:key` after `did:key:` and
112
+ the form of the container's `pub` member. `multicodec_for` is exported from the
113
+ package root.
114
+ - `tests/vectors/hybrid_v1.json`: a stored hybrid object and key binding for
115
+ other implementations; `examples/hybrid.py`.
116
+
117
+ ### Changed
118
+ - **ML-DSA `did:key` code points are now the values listed in the multicodec
119
+ table**: `mldsa-44-pub` `0x1210`, `mldsa-65-pub` `0x1211`, `mldsa-87-pub`
120
+ `0x1212` (constants `ML_DSA_44_MULTICODEC` / `ML_DSA_65_MULTICODEC` /
121
+ `ML_DSA_87_MULTICODEC`). This changes the ML-DSA `did:key` wire form, which
122
+ 0.2.0 documented as not stable. **Ed25519 is unchanged** (`0xed`).
123
+ - The placeholder values `0x1207`/`0x1208`/`0x1209` used up to 0.2.0 belong to
124
+ other entries in the multicodec table. They are retired: never produced, and
125
+ `parse_did_key` / `multikey_decode` reject a key that carries one
126
+ (`UnsupportedAlgorithm`). An ML-DSA `did:key` string made by 0.2.0 or earlier
127
+ no longer parses.
128
+ - **`[pq]` now requires `cryptography>=48.0`** (was `>=45.0`). Checked against
129
+ the published wheels: the `mldsa` module does not exist up to 46.0.7; 47.0.0
130
+ ships it but every ML-DSA call raises "not supported by this backend"; 48.0.0
131
+ is the first that signs and verifies ML-DSA-44/65/87.
132
+ - An installed `cryptography` that has the `mldsa` module but cannot run it now
133
+ surfaces as `BackendNotInstalled` instead of a `cryptography` exception.
134
+
135
+ ### Removed
136
+ - `ML_DSA_MULTICODEC_OVERRIDE`, `ML_DSA_*_MULTICODEC_PROVISIONAL`,
137
+ `PROVISIONAL_MULTICODEC_ALGS` and `is_provisional` from
138
+ `sm_signing.multicodec`. They existed to swap in the table values later; the
139
+ table values are now the constants. None of them was in a tagged release or
140
+ exported from the package root.
141
+
142
+ ### Documentation
143
+ - SPEC: §3.1 rewritten for the table values and the retired placeholders; new §4
144
+ (hybrid profile) and §5 (key binding, including its stated limitation: once
145
+ Ed25519 is breakable, a binding is only as good as the record of it made
146
+ before that point). Wire version `0.2`.
147
+ - README: what the package owns, hybrid quick start, policy table.
148
+ - WHITEPAPER: §4 on the hybrid profile's rationale.
149
+
150
+ ## [0.2.0] — 2026-07-12
151
+
152
+ ### Added
153
+ - ML-DSA (FIPS-204) backend behind the `[pq]` extra — suites `ml-dsa-44/65/87` via
154
+ `cryptography`'s native `mldsa` module (OpenSSL >= 3.5).
155
+ - Parametrized suite tests: round-trip, tamper, wrong-key, did:key, and protocol
156
+ conformance run identically across Ed25519 and all three ML-DSA suites.
157
+
158
+ ## [0.1.0] — 2026-07-12
159
+
160
+ ### Added
161
+ - Initial working-draft release: zero-dep `Signer`/`Verifier` contract, `SigAlg`
162
+ registry, lazy backend dispatch, pure-stdlib `did:key`/multibase, Ed25519 backend
163
+ behind `[ed25519]`, and the mechanical zero-runtime-dependency gate.
@@ -0,0 +1,31 @@
1
+ # Contributing to sm-signing
2
+
3
+ Contributions are accepted under the Developer Certificate of Origin (DCO)
4
+ sign-off model. Add `Signed-off-by: Your Name <you@example.com>` to every commit
5
+ (`git commit -s`).
6
+
7
+ ## Change process
8
+
9
+ 1. Spec-affecting changes open a PR updating the **spec (`SPEC.md`), the tests,
10
+ and the code together**. The `tests/` suite is the authoritative behavioural
11
+ specification of the package.
12
+ 2. The gate is `make ci-local` (uv: `ruff` → `ruff format` → `mypy --strict` →
13
+ `pytest`). CI runs the same on Python 3.11 and 3.12. Push only when green.
14
+ 3. CI also runs the suite with no usable ML-DSA backend (`cryptography` 46.0.7,
15
+ 47.0.0 and none; job `no-pq-backend`). A test that needs a backend skips where
16
+ it is missing (`tests/conftest.py`), so write tests through `fresh()` /
17
+ `need()` rather than importing `cryptography` directly.
18
+
19
+ ## House rules
20
+
21
+ - **The core stays zero-dep.** did:key / multibase / dispatch MUST import and
22
+ work with `cryptography` absent (mechanically gated by
23
+ `tests/test_zero_dep.py`). Crypto lives only behind the `[ed25519]` / `[pq]`
24
+ extras.
25
+ - **One seam.** A new algorithm enters only as: a `SigAlg` member + a backend
26
+ behind an extra + a multicodec entry — spec'd in `SPEC.md` first
27
+ (RFC-style PR). No side doors.
28
+ - **Multicodec honesty.** Unregistered code points stay marked **PROVISIONAL**
29
+ in SPEC and source until actually registered; never present them as
30
+ interop-stable.
31
+ - **Signature/wire format changes are semver-major.**
@@ -0,0 +1,34 @@
1
+ # Governance
2
+
3
+ ## Scope
4
+
5
+ | In scope | Out of scope |
6
+ | --- | --- |
7
+ | The signing contract (`Signer`/`Verifier`, `SigAlg`), suite dispatch, `did:key`/multibase, alg-agility field, the hybrid profile `sm-signing/hybrid/v1` and key binding | Canonicalization, key custody/rotation/transport, COSE/JWS framing, receipt schemas |
8
+
9
+ The primitive owns one thing. Anything outside the table belongs to a companion
10
+ package or the consumer's stack.
11
+
12
+ ## Versioning
13
+
14
+ Semantic Versioning 2.0.0. The protocol surface (wire shape / algorithm) is
15
+ frozen within a major. A change to it requires an RFC-style PR to `SPEC.md`
16
+ before code.
17
+
18
+ ## Conformance
19
+
20
+ The test suite under `tests/` (and any vectors it loads) is the authoritative
21
+ behavioural specification. A change in behaviour without a corresponding test
22
+ change is a bug.
23
+
24
+ ## Contributions
25
+
26
+ - PRs must include tests and pass `ruff` + `mypy --strict` + `pytest`.
27
+ - No expansion of the protocol surface without an accepted RFC.
28
+ - No domain-specific or deployment-specific content — this is a generic
29
+ primitive (see the neutrality checklist in the extraction playbook).
30
+ - Sign off with the Developer Certificate of Origin (DCO), not a CLA.
31
+
32
+ ## Attribution
33
+
34
+ Built by Stellarminds (stellarminds.ai). MIT-licensed.
@@ -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.
@@ -0,0 +1,11 @@
1
+ .PHONY: ci-local
2
+ # One-command pre-push gate — the same steps CI runs, hard-failing on first red.
3
+ ci-local:
4
+ uv sync --extra dev
5
+ uv run ruff check .
6
+ uv run ruff format --check .
7
+ uv run mypy sm_signing
8
+ SM_SIGNING_REQUIRE_BACKENDS=1 uv run pytest -v
9
+ uv run python examples/sign_verify.py
10
+ uv run python examples/did_key.py
11
+ uv run python examples/hybrid.py
@@ -0,0 +1,253 @@
1
+ Metadata-Version: 2.5
2
+ Name: sm-signing
3
+ Version: 0.3.1
4
+ Summary: Algorithm-agile signing for agent attestations: Ed25519 and ML-DSA (FIPS 204) behind one seam, plus a hybrid profile carrying both. Zero runtime dependencies.
5
+ Project-URL: Homepage, https://github.com/Sharathvc23/sm-signing
6
+ Project-URL: Spec, https://github.com/Sharathvc23/sm-signing/blob/main/SPEC.md
7
+ Project-URL: Whitepaper, https://github.com/Sharathvc23/sm-signing/blob/main/WHITEPAPER.md
8
+ Author: Sharath (Stellarminds)
9
+ License: MIT
10
+ License-File: LICENSE
11
+ Keywords: ai-agents,algorithm-agility,did-key,ed25519,hybrid-signatures,ml-dsa,post-quantum,signing
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Typing :: Typed
18
+ Requires-Python: >=3.11
19
+ Provides-Extra: dev
20
+ Requires-Dist: cryptography>=48.0; extra == 'dev'
21
+ Requires-Dist: mypy>=1.10; extra == 'dev'
22
+ Requires-Dist: pytest>=8; extra == 'dev'
23
+ Requires-Dist: ruff>=0.6; extra == 'dev'
24
+ Provides-Extra: ed25519
25
+ Requires-Dist: cryptography>=41.0; extra == 'ed25519'
26
+ Provides-Extra: pq
27
+ Requires-Dist: cryptography>=48.0; extra == 'pq'
28
+ Description-Content-Type: text/markdown
29
+
30
+ # sm-signing
31
+
32
+ **Algorithm-agile signing for agent attestations: Ed25519 and ML-DSA (FIPS 204) behind one seam,
33
+ and a hybrid profile that carries both signatures on one object.**
34
+
35
+ Agent attestation code hardcodes Ed25519. `sm-signing` is the single seam that makes *"ML-DSA
36
+ (FIPS 204) everywhere"* a configuration change instead of a breaking refactor at every call site.
37
+ The core — the `Signer`/`Verifier` protocols, algorithm dispatch, `did:key`/multibase encoding and
38
+ the hybrid profile — is **pure standard library, zero runtime dependencies**. Actual cryptography
39
+ lives in backends imported lazily behind install extras.
40
+
41
+ ## What this package owns (0.3.1)
42
+
43
+ - One **algorithm-agile** `Signer`/`Verifier` contract: choose a suite by `SigAlg`, not by rewriting.
44
+ - A **zero-dependency core** — a hard, tested invariant (`tests/test_zero_dep.py`), so the abstraction
45
+ can be embedded in the leanest kernels; crypto backends are opt-in extras.
46
+ - An **Ed25519 backend** (`[ed25519]`) and an **ML-DSA-44/65/87 backend** (`[pq]`, FIPS 204 via
47
+ `cryptography`'s native ML-DSA) — real signatures at FIPS 204 sizes, covered by the same
48
+ parametrized suite tests.
49
+ - **`did:key` + multibase(base58btc)** encode/parse implemented in pure stdlib. Ed25519 and ML-DSA
50
+ public keys use the code points listed in the multicodec table (`0xed`; `0x1210`/`0x1211`/`0x1212`).
51
+ Parsing is strict (one key, one string) and bounded (length checked before decoding).
52
+ - The **algorithm-agility envelope field** (`suite_field`) so signed statements carry their suite and a
53
+ migration marker.
54
+ - The **hybrid profile `sm-signing/hybrid/v1`** (`sm_signing.hybrid`): a long-lived signed object
55
+ keeps its Ed25519 signature and carries an ML-DSA signature next to it, inside its signed body.
56
+ - a small **container** (`alg`, `kid`, `sig`, optional `pub`) and the **nested signing order**
57
+ that makes removing the post-quantum signature break the classical one;
58
+ - one **verification function with three policies** — `classical`, `auto` (default) and
59
+ `pq-required` — whose result reports the post-quantum signature as *verified*, *failed* or
60
+ *not checked*, never reports "not checked" as "ok", and says **where the key came from**
61
+ (the verifier's resolver, a pinned binding, or only the object's own `pub`);
62
+ - a **key binding** between the Ed25519 identity and the ML-DSA key: a standalone object signed
63
+ by both keys, and a `BindingStore` of pinned keys. A pin is a commitment: once an identity has
64
+ one, an object from it without a valid signature by that key is rejected, under `auto` too;
65
+ - **bounded, non-raising verification**: sizes, entry count and nesting depth are checked before
66
+ anything is decoded, and hostile input gets a rejecting result, never an exception.
67
+
68
+ ## What it does NOT do (by design or yet)
69
+
70
+ - **Canonicalization** — a `Signer` signs exactly the bytes it is given; JCS/RFC 8785 or COSE
71
+ `Sig_structure` framing is the caller's responsibility. The hybrid functions take the object's
72
+ canonical bytes from the caller. The single exception is the key-binding object, which has its
73
+ own small, fully specified serializer (`canonical_binding_bytes`).
74
+ - **Replace Ed25519** — identities stay short Ed25519 `did:key` values. An ML-DSA `did:key` is
75
+ about 2,700 characters; it encodes and parses, but it is not meant to be an identity in a header.
76
+ - **Protect an unpinned identity after Ed25519 breaks** — a key binding is only as good as the
77
+ record of it made before that point. Pin or log bindings early ([`SPEC.md`](./SPEC.md) §5.4).
78
+ - **Decide which ML-DSA keys you trust** — `pq-required` accepts only a key your resolver returns
79
+ or one pinned for the identity. The package checks the key against that; supplying the resolver
80
+ or enrolling the pin is the verifier's job.
81
+
82
+ ## Install
83
+
84
+ ```bash
85
+ pip install "sm-signing[ed25519]" # core + the Ed25519 backend
86
+ pip install "sm-signing[pq]" # + ML-DSA/FIPS 204 (needs cryptography >= 48)
87
+ ```
88
+
89
+ The bare `sm-signing` (no extra) installs the zero-dep core: protocols, dispatch, did:key and the
90
+ hybrid profile's parsing and policy logic.
91
+
92
+ `cryptography` 48.0 is the first release whose wheels can sign and verify ML-DSA: the module is
93
+ absent before 47.0, and the 47.0 wheels ship it but cannot run it. On an older install the ML-DSA
94
+ suites raise `BackendNotInstalled`.
95
+
96
+ ## Quick start
97
+
98
+ ```python
99
+ from sm_signing import SigAlg, generate_secret, load_signer, verifier_for, suite_field
100
+
101
+ secret = generate_secret(SigAlg.ED25519) # needs [ed25519]
102
+ signer = load_signer(SigAlg.ED25519, secret)
103
+
104
+ payload = b"the canonical bytes a caller already produced"
105
+ sig = signer.sign(payload)
106
+
107
+ verifier = verifier_for(SigAlg.ED25519, signer.public_key_multibase())
108
+ assert verifier.verify(payload, sig)
109
+ print(suite_field(SigAlg.ED25519)) # {'signature_suite': 'ed25519', 'alg_agility': 'v1'}
110
+ ```
111
+
112
+ Swap `SigAlg.ED25519` for `SigAlg.ML_DSA_65` (with `[pq]` installed) and nothing else changes —
113
+ that is the point of the seam.
114
+
115
+ ## Quick start — hybrid (Ed25519 + ML-DSA-65)
116
+
117
+ Needs `sm-signing[ed25519,pq]`. The object below stands in for any signed JSON object; `canonical`
118
+ is whatever canonicalization that object already uses.
119
+
120
+ ```python
121
+ import base64, copy, json
122
+ from sm_signing import (
123
+ SigAlg, generate_secret, load_signer, verifier_for,
124
+ prepare_container, complete_container, verify_hybrid,
125
+ )
126
+
127
+ def canonical(obj):
128
+ return json.dumps(obj, sort_keys=True, separators=(",", ":")).encode("utf-8")
129
+
130
+ ed = load_signer(SigAlg.ED25519, generate_secret(SigAlg.ED25519))
131
+ pq = load_signer(SigAlg.ML_DSA_65, generate_secret(SigAlg.ML_DSA_65))
132
+
133
+ # Sign — nested.
134
+ obj = {"claim": "example", "ext": {}}
135
+ obj["ext"]["pq"] = prepare_container(pq) # 1. entries without `sig`
136
+ c = canonical(obj) # 2. no classical signature, no `sig`
137
+ obj["ext"]["pq"] = complete_container(obj["ext"]["pq"], pq, c) # 3. ML-DSA signs prefix + c
138
+ obj["signature"] = base64.b64encode(ed.sign(canonical(obj))).decode() # 4. Ed25519, as always
139
+
140
+ # Verify.
141
+ unsigned = {k: v for k, v in obj.items() if k != "signature"}
142
+ classical_ok = verifier_for(SigAlg.ED25519, ed.public_key_multibase()).verify(
143
+ canonical(unsigned), base64.b64decode(obj["signature"])
144
+ )
145
+
146
+ def pq_canonical(stripped_container): # container with every `sig` removed
147
+ rebuilt = copy.deepcopy(unsigned)
148
+ rebuilt["ext"]["pq"] = stripped_container
149
+ return canonical(rebuilt)
150
+
151
+ result = verify_hybrid(obj["ext"].get("pq"), pq_canonical, classical_ok=classical_ok, policy="auto")
152
+ assert result.accepted
153
+ print(result.classical_ok, result.pq_present, result.pq_status, result.reason, result.key_source)
154
+ # True True verified verified inline <- "inline": only the object's own `pub` vouches for the key
155
+ ```
156
+
157
+ | Policy | Accepts when |
158
+ |---|---|
159
+ | `classical` | the Ed25519 signature verifies; the container is ignored |
160
+ | `auto` (default) | the Ed25519 signature verifies, and no post-quantum entry that can be checked fails. An entry that cannot be checked (no `[pq]` backend, or no key) leaves `pq_status` at `not-checked`. If the identity has a pinned key, `auto` is as strict as `pq-required` |
161
+ | `pq-required` | the Ed25519 signature verifies, a container is present, no entry fails and at least one verifies **under a key the verifier trusts** (from its resolver, or pinned for the identity). Missing container, backend or key all reject |
162
+
163
+ `result.accepted` is the decision; `result.pq_status` says what was actually established, and
164
+ `result.key_source` says who vouched for the key. An object accepted under `auto` with
165
+ `pq_status == "not-checked"` rests on its Ed25519 signature only.
166
+
167
+ ### `pq-required` needs a key you trust
168
+
169
+ `pq-required` is for the day an Ed25519 signature can be forged. Whoever can forge it can also
170
+ replace the container and put a key of their own in `pub`, so **a key that only the object vouches
171
+ for does not satisfy `pq-required`**: on its own, with nothing but the object, `pq-required`
172
+ rejects (`reason == "key-untrusted"`). Give the verifier a source of keys it trusts.
173
+
174
+ A key resolver, your own lookup by `(alg, kid)`. It is always consulted, and its answer wins over
175
+ the object's `pub`:
176
+
177
+ ```python
178
+ from sm_signing import key_id
179
+
180
+ TRUSTED = {key_id(pq.public_key_bytes()): pq.public_key_bytes()} # however you came to trust them
181
+
182
+ result = verify_hybrid(
183
+ obj["ext"].get("pq"), pq_canonical, classical_ok=classical_ok,
184
+ policy="pq-required", resolver=lambda alg, kid: TRUSTED.get(kid),
185
+ )
186
+ assert result.accepted and result.key_source == "resolver"
187
+ ```
188
+
189
+ Or a pinned binding: the ML-DSA key bound to the Ed25519 identity by a key-binding object signed
190
+ with both keys, enrolled once in a `BindingStore`:
191
+
192
+ ```python
193
+ from sm_signing import InMemoryBindingStore, build_key_binding, did_key, verify_key_binding
194
+
195
+ store = InMemoryBindingStore() # use a durable BindingStore in production
196
+ binding = build_key_binding(ed, pq) # signed by both keys
197
+ enrolled = verify_key_binding(binding, binding_store=store, pin_new=True) # explicit: this pins
198
+ assert enrolled.accepted
199
+
200
+ issuer = did_key(SigAlg.ED25519, ed.public_key_bytes())
201
+ result = verify_hybrid(
202
+ obj["ext"].get("pq"), pq_canonical, classical_ok=classical_ok,
203
+ policy="pq-required", binding_store=store, subject=issuer,
204
+ )
205
+ assert result.accepted and result.key_source == "pinned"
206
+ ```
207
+
208
+ Verification never writes to the store unless you pass `pin_new=True`. Once an identity is pinned,
209
+ the pin is a commitment under `auto` as well: an object from that identity with no container, a
210
+ stripped container, an uncheckable entry or a different ML-DSA key is rejected. Enrolling the
211
+ first binding you are shown is trust on first use, so do it early, while Ed25519 signatures still
212
+ mean something ([`SPEC.md`](./SPEC.md) §5.4).
213
+
214
+ `trust_inline_pub=True` makes `pq-required` count a key from `pub` alone. It then requires *a*
215
+ post-quantum signature and adds nothing against an attacker who can forge the classical one; the
216
+ result still reports `key_source == "inline"`.
217
+
218
+ ### Untrusted input
219
+
220
+ `verify_hybrid` and `verify_key_binding` are meant to be called on objects from the network. They
221
+ check sizes, entry count and nesting depth before decoding anything (`MAX_PUB_CHARS`,
222
+ `MAX_SIG_CHARS`, `MAX_CONTAINER_ENTRIES`, `MAX_NESTING_DEPTH`, ...; [`SPEC.md`](./SPEC.md) §4.7), and
223
+ they return a rejecting result instead of raising, also when your resolver or canonicalizer raises
224
+ on the object. `parse_did_key` and `multikey_decode` raise `ValueError` on anything that is not
225
+ the one canonical, correctly sized encoding of a key, and refuse text over 4,104 / 4,096 characters
226
+ without decoding it.
227
+
228
+ See [`examples/`](./examples) for runnable sign/verify, did:key and hybrid round-trips (exercised by CI).
229
+
230
+ ## Status — 0.x working draft
231
+
232
+ [`SPEC.md`](./SPEC.md) is reviewable but **not yet frozen**, and wire/API details may change in 0.x.
233
+
234
+ - ML-DSA `did:key` strings produced by 0.2.0 or earlier used placeholder code points and are
235
+ rejected from 0.3.0 on. Ed25519 `did:key` strings are unchanged.
236
+ - 0.3.1 changes no bytes on the wire. It changes what a verifier accepts: see
237
+ [`CHANGELOG.md`](./CHANGELOG.md) for the list, in particular `pq-required` and pinning.
238
+ - Open question: whether COSE `Sig_structure` helpers belong here or in a separate COSE package
239
+ (leaning separate) — see [`WHITEPAPER.md`](./WHITEPAPER.md) §6.
240
+
241
+ ## Specification
242
+
243
+ - [`SPEC.md`](./SPEC.md) — the signer contract, did:key encoding, the hybrid profile and key
244
+ binding (working draft).
245
+ - [`WHITEPAPER.md`](./WHITEPAPER.md) — design rationale + open questions.
246
+
247
+ ## License
248
+
249
+ [MIT](./LICENSE)
250
+
251
+ ---
252
+
253
+ *First published: 2026-07-12 · Built by Stellarminds (stellarminds.ai).*