# MPC 分布式签名系统 - 架构设计文档 ## 1. 系统概述 本系统是一个基于多方安全计算 (MPC) 的分布式门限签名系统,支持 t-of-n 阈值签名方案。系统采用微服务架构,使用 Go 语言开发,基于 bnb-chain/tss-lib 实现 TSS (Threshold Signature Scheme) 协议。 ### 1.1 核心特性 - **门限签名**: 支持任意 t-of-n 阈值方案 (如 2-of-3, 3-of-5, 4-of-7) - **分布式密钥生成 (DKG)**: 无需可信第三方生成密钥 - **ECDSA secp256k1**: 与以太坊/比特币兼容的签名算法 - **高安全性**: 密钥分片加密存储,单点泄露不影响安全性 - **微服务架构**: 可独立扩展和部署 ### 1.2 技术栈 | 层级 | 技术选型 | |------|---------| | 语言 | Go 1.21+ | | TSS 库 | bnb-chain/tss-lib/v2 | | 通信协议 | gRPC + HTTP/REST | | 数据库 | PostgreSQL | | 缓存 | Redis | | 消息队列 | RabbitMQ | | 服务发现 | Consul | | 容器化 | Docker + Docker Compose | ## 2. 系统架构 ### 2.1 整体架构图 ``` ┌─────────────────────────────────────────────────────────────────────┐ │ Client Layer │ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │ │ iOS App │ │ Android App │ │ Web Client │ │ │ │ (MPC SDK) │ │ (MPC SDK) │ │ │ │ │ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ │ └─────────┼─────────────────┼─────────────────┼───────────────────────┘ │ │ │ ▼ ▼ ▼ ┌─────────────────────────────────────────────────────────────────────┐ │ API Gateway (HTTP/gRPC) │ └─────────────────────────────┬───────────────────────────────────────┘ │ ┌───────────────────┼───────────────────┐ ▼ ▼ ▼ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │ Account │ │ Session │ │ Message │ │ Service │ │ Coordinator │ │ Router │ │ (用户/账户管理) │ │ (会话协调) │ │ (消息路由) │ │ │ │ │ │ │ │ Port: 50054 │ │ Port: 50051 │ │ Port: 50052 │ │ HTTP: 8083 │ │ HTTP: 8080 │ │ HTTP: 8081 │ └────────┬────────┘ └────────┬────────┘ └────────┬────────┘ │ │ │ │ ▼ │ │ ┌─────────────────┐ │ │ │ Server Party │ │ │ │ Service x N │◄──────────┘ │ │ (MPC 计算节点) │ │ │ │ │ │ Party 1: 50053 │ │ │ Party 2: 50055 │ │ │ Party 3: 50056 │ │ └────────┬────────┘ │ │ ▼ ▼ ┌─────────────────────────────────────────────────────────────────────┐ │ Data Layer │ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │ │ PostgreSQL │ │ Redis │ │ RabbitMQ │ │ │ │ (持久化) │ │ (缓存/会话) │ │ (消息队列) │ │ │ └──────────────┘ └──────────────┘ └──────────────┘ │ └─────────────────────────────────────────────────────────────────────┘ ``` ### 2.2 服务职责 #### 2.2.1 Session Coordinator (会话协调器) - 创建和管理 MPC 会话 - 协调参与方加入会话 - 跟踪会话状态和进度 - 管理参与方就绪状态 #### 2.2.2 Message Router (消息路由器) - 路由 TSS 协议消息 - 支持点对点和广播消息 - 消息缓存和重传 - WebSocket 实时通信 #### 2.2.3 Server Party (服务端参与方) - 作为 MPC 协议的服务端参与方 - 执行 DKG 和签名协议 - 安全存储加密的密钥分片 - 支持多实例部署 #### 2.2.4 Account Service (账户服务) - 用户注册和认证 - 账户管理 - MPC 会话入口 API - 账户恢复流程 ## 3. 核心流程 ### 3.1 密钥生成流程 (Keygen) ``` ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ Client │ │Coordinator│ │ Router │ │ Parties │ └────┬────┘ └────┬────┘ └────┬────┘ └────┬────┘ │ │ │ │ │ CreateSession │ │ │ │──────────────>│ │ │ │ │ │ │ │ SessionID + │ │ │ │ JoinTokens │ │ │ │<──────────────│ │ │ │ │ │ │ │ JoinSession (各参与方) │ │──────────────────────────────────────────────>│ │ │ │ │ │ MarkReady (各参与方) │ │──────────────────────────────────────────────>│ │ │ │ │ │ StartSession │ │ │──────────────>│ │ │ │ │ Notify Start │ │ │ │──────────────────────────────>│ │ │ │ │ │ │ TSS Messages (多轮) │ │ │ │<─────────────>│ │ │ │ │ │ │ ReportCompletion │ │ │<──────────────────────────────│ │ │ │ │ │ Session Completed │ │ │<──────────────│ │ │ │ │ │ │ ``` ### 3.2 签名流程 (Signing) ``` ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ Client │ │Coordinator│ │ Router │ │ Parties │ └────┬────┘ └────┬────┘ └────┬────┘ └────┬────┘ │ │ │ │ │ CreateSignSession │ │ │ (messageHash) │ │ │ │──────────────>│ │ │ │ │ │ │ │ SessionID │ │ │ │<──────────────│ │ │ │ │ │ │ │ JoinSession (t+1 参与方) │ │ │──────────────────────────────────────────────>│ │ │ │ │ │ StartSession │ │ │──────────────>│ │ │ │ │ │ │ │ │ TSS Messages (多轮) │ │ │ │<─────────────>│ │ │ │ │ │ │ Signature │ │ │ │<──────────────────────────────│ │ │ │ │ │ Signature (R, S, V) │ │ │<──────────────│ │ │ │ │ │ │ ``` ## 4. 数据模型 ### 4.1 Session (会话) ```go type Session struct { ID uuid.UUID // 会话唯一标识 Type SessionType // keygen | sign Status SessionStatus // created | waiting | in_progress | completed | failed ThresholdT int // 签名阈值 (t+1 签名者) ThresholdN int // 总参与方数 MessageHash []byte // 待签名消息哈希 (签名会话) Participants []Participant // 参与方列表 CreatedAt time.Time ExpiresAt time.Time } ``` ### 4.2 Participant (参与方) ```go type Participant struct { PartyID string // 参与方标识 PartyIndex int // 协议中的索引 DeviceInfo DeviceInfo // 设备信息 Status ParticipantStatus // joined | ready | computing | completed JoinToken string // 加入令牌 } ``` ### 4.3 KeyShare (密钥分片) ```go type KeyShare struct { ID uuid.UUID AccountID uuid.UUID PartyID string EncryptedShareData []byte // AES-GCM 加密的分片数据 PublicKey []byte // 组公钥 CreatedAt time.Time } ``` ## 5. 安全设计 ### 5.1 密钥安全 - **密钥分片存储**: 使用 AES-256-GCM 加密存储 - **主密钥管理**: 从环境变量或 KMS 加载 - **无单点故障**: 任意 t 个节点被攻破不影响安全性 ### 5.2 通信安全 - **TLS 加密**: 所有 gRPC/HTTP 通信使用 TLS - **消息认证**: TSS 消息包含参与方签名 - **会话令牌**: 使用 UUID v4 生成一次性令牌 ### 5.3 安全属性 | 属性 | 描述 | |------|------| | 门限安全 | 需要至少 t+1 方参与才能签名 | | 密钥不可恢复 | 少于 t+1 个分片无法恢复私钥 | | 前向安全 | 会话密钥独立,历史泄露不影响未来 | | 抗合谋 | t 个恶意方无法伪造签名 | ## 6. 部署架构 ### 6.1 最小部署 (2-of-3) ``` ┌─────────────────────────────────────────────────────────┐ │ Server 1 (Coordinator) │ │ ┌─────────────────┐ ┌─────────────────┐ │ │ │ Session Coord. │ │ Message Router │ │ │ │ Port: 50051 │ │ Port: 50052 │ │ │ └─────────────────┘ └─────────────────┘ │ │ ┌─────────────────┐ ┌─────────────────┐ │ │ │ Account Service │ │ PostgreSQL │ │ │ │ Port: 50054 │ │ Redis/RabbitMQ │ │ │ └─────────────────┘ └─────────────────┘ │ └─────────────────────────────────────────────────────────┘ ┌──────────────────┐ ┌──────────────────┐ ┌──────────────────┐ │ Server Party 1 │ │ Server Party 2 │ │ Server Party 3 │ │ Port: 50053 │ │ Port: 50055 │ │ Port: 50056 │ └──────────────────┘ └──────────────────┘ └──────────────────┘ ``` ### 6.2 生产环境部署 - **高可用**: 每个服务至少 2 副本 - **负载均衡**: Nginx/Traefik 反向代理 - **服务发现**: Consul 集群 - **监控**: Prometheus + Grafana ## 7. 目录结构 ``` mpc-system/ ├── api/ # API 定义 │ ├── grpc/ # gRPC 生成代码 │ └── proto/ # Protobuf 定义 ├── docs/ # 文档 ├── migrations/ # 数据库迁移 ├── pkg/ # 公共包 │ ├── crypto/ # 加密工具 │ └── tss/ # TSS 封装 ├── services/ # 微服务 │ ├── account/ # 账户服务 │ ├── message-router/ # 消息路由 │ ├── server-party/ # 服务端参与方 │ └── session-coordinator/ # 会话协调 ├── tests/ # 测试 │ ├── e2e/ # 端到端测试 │ ├── integration/ # 集成测试 │ └── unit/ # 单元测试 ├── docker-compose.yml # Docker 编排 ├── Makefile # 构建脚本 └── go.mod # Go 模块 ```