AxonOS-org/axonos-consent
GitHub: AxonOS-org/axonos-consent
这是一个基于 Rust 的安全关键型脑机接口确定性同意协议库,通过零分配和形式化验证的状态机,在可信执行环境中强制执行用户撤销神经刺激的权限。
Stars: 1 | Forks: 0
# axonos-consent
[](https://github.com/AxonOS-org/axonos-consent/releases)
[](https://sym.bot/spec/mmp)
[](https://sym.bot/spec/mmp-consent)
[](#guarantees)
[](src/lib.rs)
[](#guarantees)
[](#interoperability)
[](#testing)
[](#testing)
[](https://arxiv.org/abs/2604.03955)
[](#licence)
[](https://github.com/AxonOS-org/axonos-consent/actions/workflows/ci.yml)
**针对安全关键型脑机接口的确定性同意执行层。**
[MMP Consent Extension v0.1.0](https://sym.bot/spec/mmp-consent) 的参考实现,与 [Hongwei Xu](https://github.com/sym-bot) ([SYM.BOT](https://sym.bot)) 共同设计。符合 [Mesh Memory Protocol v0.2.2](https://sym.bot/spec/mmp) 第 16.4 节。
联合论文:*"Protocol-Level Consent for Cognitive Mesh Coupling"* —— 基于 [arXiv:2604.03955](https://arxiv.org/abs/2604.03955) (SVAF) 构建。
## 架构
Consent 运行于 [MMP 8 层协议栈](https://sym.bot/spec/mmp) 的**第 2 层(连接层)** —— 位于消息传递之下,[SVAF 耦合](https://arxiv.org/abs/2604.03955)(第 4 层)之下,认知层(第 5–7 层)之下。撤销同意帧会在任何更高层逻辑执行之前关闭大门。
```
┌─────────────────────────────────────────────────┐
│ Non-Secure World (A53) │
│ ┌──────────────────────────────────────────┐ │
│ │ Network Task │ │
│ │ ├─ JSON codec (relay boundary) │ │
│ │ └─ Frame parser ↔ sym relay (MMP §7) │ │
│ └──────────────┬───────────────────────────┘ │
│ │ nsc_withdraw_consent() │
├─────────────────┼───────────────────────────────┤
│ Secure World │ (TrustZone-S) │
│ ▼ │
│ ┌──────────────────────────────────────────┐ │
│ │ ConsentEngine (zero-alloc) │ │
│ │ ├─ Per-peer state machine (8 slots) │ │
│ │ ├─ CBOR codec (bounded decoder) │ │
│ │ ├─ Invariants layer (MUST/SHOULD/MAY) │ │
│ │ └─ StimGuard → Secure GPIO DAC gate │ │
│ └──────────────────────────────────────────┘ │
└─────────────────────────────────────────────────┘
```
**为什么选择第 2 层。** 如果 Consent 运行在第 4 层,[SVAF](https://arxiv.org/abs/2604.03955) 耦合引擎可能会延迟或降低撤销请求的优先级。在第 2 层执行 Consent 消除了这一类故障。在网络获知之前,大脑已处于安全状态。
## 互操作性
针对同一书面规范的两种实现 —— Rust `#![no_std]` ([axonos-consent](https://github.com/AxonOS-org/axonos-consent)) 和 Node.js ([sym](https://github.com/sym-bot/sym)) —— 第二种实现的设计基于对第一种实现的文档化审计([§6.1.2 方法论](https://sym.bot/spec/mmp-consent))。
**15/15 个标准互操作向量 —— 通过**
已在 [SYM.BOT](https://sym.bot) 生产型网格(5 个活动节点,2026 年 4 月)上验证。四种同意帧(`withdraw`、`suspend`、`resume`、`STIMGUARD_LOCKOUT`)由中继转发,根据 [MMP §7](https://sym.bot/spec/mmp) 的前向兼容性被所有生产节点静默忽略。零错误。
### 兼容性矩阵
| 规范版本 | 实现 | 向量 | 结果 |
|:---:|:---:|:---:|:---:|
| [v0.1.0](https://sym.bot/spec/mmp-consent) | Rust `no_std` ([axonos-consent](https://github.com/AxonOS-org/axonos-consent)) | 15/15 | **PASS** |
| [v0.1.0](https://sym.bot/spec/mmp-consent) | Node.js ([sym](https://github.com/sym-bot/sym)) | 15/15 | **PASS** |
| v0.2.2 | Rust `no_std` | 15/15 | **Backward-compatible** |
v0.2.2 为增量更新(审计跟踪、API 人体工程学)。状态机和线格式与 v0.1.0 保持一致。
### 完整性锁
```
SHA-256: 29a8bf9f2b4dabe5d9641a8a4c416f361c2ba9815cca9b8e9e1d222d002fa50a
```
对 [`tests/vectors/consent-interop-vectors-v0.1.0.json`](tests/vectors/consent-interop-vectors-v0.1.0.json) 的任何修改都会导致测试套件失效。[CI 在每次推送时](https://github.com/AxonOS-org/axonos-consent/actions/workflows/ci.yml) 验证此校验和。
## API
```
use axonos_consent::ConsentEngine;
let mut engine = ConsentEngine::new();
engine.register_peer(peer_id, now_us).unwrap();
// Single entry point: wire bytes → validated state transition
let result = engine.process_raw(&peer_id, cbor_bytes, now_us)?;
// result.new_state : ConsentState
// result.warnings : SHOULD-level advisories (RFC 2119)
```
完整流程 —— 无需其他函数组合:
```
process_raw → CBOR decode (bounded) → invariant check (MUST/SHOULD) → state transition (3×3) → StimGuard
```
## 状态机
依据 [Consent Extension §4](https://sym.bot/spec/mmp-consent)。对 `(ConsentState, ConsentFrame)` 进行穷举的 3×3 匹配。零通配符分支 —— 添加新的状态或帧变体会产生**编译错误**。
```
┌─────────┐ consent-suspend ┌───────────┐
│ GRANTED │ ─────────────────→ │ SUSPENDED │
│ │ ←───────────────── │ │
└────┬────┘ consent-resume └─────┬─────┘
│ │
│ consent-withdraw │ consent-withdraw
▼ ▼
┌──────────────────────────────────────┐
│ WITHDRAWN (terminal) │
└──────────────────────────────────────┘
```
| | `Withdraw` | `Suspend` | `Resume` |
|:---|:---:|:---:|:---:|
| **GRANTED** | → WITHDRAWN | → SUSPENDED | → GRANTED *(idempotent)* |
| **SUSPENDED** | → WITHDRAWN | → SUSPENDED *(idempotent)* | → GRANTED |
| **WITHDRAWN** | **REJECT** | **REJECT** | **REJECT** |
参见 [`state.rs`](src/state.rs) — `apply_frame()`。
## 保证
| 属性 | 保证 | 执行方式 |
|:---|:---|:---|
| `#![no_std]` | 默认构建,无堆 | [`Cargo.toml`](Cargo.toml) |
| 零分配 | `ReasonBuf` 固定 64B,编码器写入 `&mut [u8]` | [`frames.rs`](src/frames.rs) |
| 有界解析 | `MAX_MAP=8` · `MAX_STR=128` · `MAX_DEPTH=4` | [`cbor.rs`](src/codec/cbor.rs) |
| 无 unsafe | `#![forbid(unsafe_code)]` — 编译期 | [`lib.rs`](src/lib.rs) |
| 穷举 FSM | 3×3 表,编译器检查,无通配符 | [`state.rs`](src/state.rs) |
| 确定性 | O(1) 转换,O(n≤8) 解码 | [`engine.rs`](src/engine.rs) |
| WITHDRAWN 终态 | WITHDRAWN 后的任何帧 → REJECT | [`state.rs`](src/state.rs) |
| 第 2 层 | 位于耦合层(第 4 层)之下,[SVAF](https://arxiv.org/abs/2604.03955) 之下 | [MMP §16.4](https://sym.bot/spec/mmp) |
## 威胁模型
| 威胁 | 缓解措施 | 限制 | 来源 |
|:---|:---|:---:|:---|
| Map 炸弹 | `MAX_MAP_FIELDS` | 8 | [`cbor.rs`](src/codec/cbor.rs) |
| 字符串炸弹 | `MAX_STRING_LEN` | 128 B | [`cbor.rs`](src/codec/cbor.rs) |
| 栈溢出 | `MAX_NESTING_DEPTH` | 4 | [`cbor.rs`](src/codec/cbor.rs) |
| 类型混淆 | 位掩码重复键检测 | 7 个键 | [`cbor.rs`](src/codec/cbor.rs) |
| 不支持的 CBOR | 显式拒绝:类型 1,2,4,6,7 | [RFC 8949](https://www.rfc-editor.org/rfc/rfc8949) | [`cbor.rs`](src/codec/cbor.rs) |
| 缓冲区溢出 | `Err(BufferTooSmall)` | 256 B | [`cbor.rs`](src/codec/cbor.rs) |
| 状态违规 | `apply_frame()` REJECT | 编译器 | [`state.rs`](src/state.rs) |
## 错误分类
```
L1 (Wire) → Error::Decode — malformed CBOR, bounds, unsupported types
L2 (Struct) → Error::Invariant — MUST violations (§10)
L3 (State) → Error::Transition — WITHDRAWN→any, peer not found
L4 (System) → Error::Encode — buffer too small
```
`#[must_use]` 作用于 `Error` 枚举。每一层都有 `From` 转换。参见 [`error.rs`](src/error.rs)。
## WCET 分析
在 Cortex-M4F @ 168 MHz 上针对每个操作的最坏情况时序([`engine.rs`](src/engine.rs)):
| 操作 | WCET | 复杂度 |
|:---|:---:|:---|
| `process_raw` (完整流程,含 CBOR 解码) | <10 µs | O(n), n ≤ 8 fields |
| `process_frame` (已解码路径) | <1 µs | O(1) |
| `withdraw_all` (紧急全局终止,8 个对等节点) | <5 µs | O(MAX_PEERS) |
| §5.1 步骤 3–5 (紧急按钮 → DAC 门控) | <1 µs | O(1), 不可抢占 |
这四个数据均为特定操作的指标。联合论文采用按操作归因的方式,而非汇总的头条数据。
## MMP v0.2.2 一致性
| [MMP](https://sym.bot/spec/mmp) § | 一致性 |
|:---|:---|
| §3.5 | `consent-withdraw` 触发 CONNECTED → DISCONNECTED |
| §7 | 前向兼容:未知帧类型被静默忽略 |
| §7.2 | 错误码 `2002 CONSENT_WITHDRAWN` |
| §16 | 扩展机制:握手中的 `consent-v0.1.0` |
| §16.4 | 已发布:[`sym.bot/spec/mmp-consent`](https://sym.bot/spec/mmp-consent) |
## 原因代码
依据 [Consent Extension §3.4](https://sym.bot/spec/mmp-consent):
| 代码 | 名称 | 范围 |
|:---:|:---|:---:|
| `0x00` | `UNSPECIFIED` | spec |
| `0x01` | `USER_INITIATED` | spec |
| `0x02` | `SAFETY_VIOLATION` | spec |
| `0x03` | `HARDWARE_FAULT` | spec |
| `0x10` | `STIMGUARD_LOCKOUT` | AxonOS |
| `0x11` | `SESSION_ATTESTATION_FAILURE` | AxonOS |
| `0x12` | `EMERGENCY_BUTTON` | AxonOS |
| `0x13` | `SWARM_FAULT_DETECTED` | AxonOS |
未知代码 → `UNSPECIFIED`(根据 §3.4 前向兼容)。参见 [`reason.rs`](src/reason.rs)。
## 法规一致性
| 框架 | 相关性 | 参考 |
|:---|:---|:---|
| [IEC 62304](https://www.iso.org/standard/71604.html) Class C | 医疗器械软件生命周期 | 架构一致性 |
| [IEC 60601-1](https://www.iso.org/standard/65529.html) | 基本性能,基本安全 | StimGuard 执行 |
| [ISO 14971](https://www.iso.org/standard/72704.html) | 医疗器械风险管理 | [威胁模型](#threat-model) |
| [UNESCO 2025](https://www.unesco.org/en/articles/ethics-neurotechnology) | 神经技术伦理 —— 同意主权 | “随时”撤销权 |
| [Shannon criteria](https://doi.org/10.1109/10.126616) | 电荷密度限制 (k=1.75, ≤30 µC/cm²) | [`stim_guard.rs`](src/stim_guard.rs) |
| [FDA BCI Guidance](https://www.fda.gov/regulatory-information/search-fda-guidance-documents/implanted-brain-computer-interface-bci-devices-patients-paralysis-or-amputation-non-clinical-testing) | 植入式 BCI 非临床测试 (2021) | 网络安全 + 安全性 |
## Crate 结构
```
src/
├── lib.rs # #![forbid(unsafe_code)], spec-to-code mapping
├── state.rs # ConsentState + apply_frame (exhaustive 3×3)
├── engine.rs # ConsentEngine, process_raw, process_frame
├── frames.rs # Frame types, ReasonBuf (64B zero-alloc)
├── reason.rs # ReasonCode registry (§3.4)
├── invariants.rs # MUST/SHOULD/MAY enforcement (§10)
├── error.rs # Layered error taxonomy (L1–L4)
├── stim_guard.rs # DacGate trait, timing contract (§8)
└── codec/
├── cbor.rs # Bounded encoder/decoder, security hardened
└── json.rs # JSON codec (feature-gated)
tests/
├── consent_interop.rs
└── vectors/
└── consent-interop-vectors-v0.1.0.json
fuzz/
└── fuzz_targets/
├── fuzz_cbor_decode.rs
└── fuzz_cbor_roundtrip.rs
```
## 嵌入式
```
cargo build --release --target thumbv7em-none-eabihf --no-default-features
```
零依赖。无需操作系统。支持裸机 Cortex-M4F。
## 测试
```
cargo test # Unit + integration (no_std default)
cargo test --features json # + JSON round-trip (15 canonical vectors)
cargo +nightly fuzz run fuzz_cbor_decode # Crash resistance
cargo +nightly fuzz run fuzz_cbor_roundtrip # encode→decode invariant
```
[CI 流水线](https://github.com/AxonOS-org/axonos-consent/actions/workflows/ci.yml):`test` · `test --features json` · `build thumbv7em` · `clippy -D warnings` · `fmt --check` · SHA-256 向量完整性。
## 许可证
在以下任一许可下授权
- [Apache License, Version 2.0](LICENSE-APACHE) ([http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0))
- [MIT License](LICENSE-MIT) ([http://opensource.org/licenses/MIT](http://opensource.org/licenses/MIT))
由您选择。
**AxonOS** · [axonos.org](https://axonos.org) · [medium.com/@AxonOS](https://medium.com/@AxonOS) · [github.com/AxonOS-org](https://github.com/AxonOS-org) · axonosorg@gmail.com
**SYM.BOT** · [sym.bot](https://sym.bot) · [sym.bot/spec/mmp](https://sym.bot/spec/mmp) · [sym.bot/spec/mmp-consent](https://sym.bot/spec/mmp-consent) · [github.com/sym-bot](https://github.com/sym-bot)
**论文** · [arXiv:2604.03955](https://arxiv.org/abs/2604.03955) (SVAF) · *Protocol-Level Consent for Cognitive Mesh Coupling* (筹备中)
标签:Apache-2.0, BCI, consent-extension, Fuzzing, Mesh Memory Protocol, MMP, no_std, Rust, Safety-critical, StimGuard, SVAF, XML注入, 互操作性, 协议, 去中心化, 可视化界面, 安全性, 嵌入式, 开源, 形式化验证, 撤销授权, 状态机, 确定性, 禁止不安全代码, 网络安全, 网络流量审计, 脑机接口, 认知耦合, 通知系统, 隐私保护, 零分配