244 lines
7.2 KiB
Rust
244 lines
7.2 KiB
Rust
//! L2 文明层: 文明特征向量、灵魂签名
|
||
//!
|
||
//! 文明特征向量定义了一个区块链文明的核心属性:
|
||
//! - 宪法哈希
|
||
//! - 数学基础 (经典数学、直觉主义、量子逻辑等)
|
||
//! - 物理常数哈希
|
||
//! - 支持的协议
|
||
//! - 文明公钥 (灵魂签名用)
|
||
//!
|
||
//! 灵魂签名是文明级的集体签名:
|
||
//! - 通过分布式密钥生成(DKG)协议生成集体私钥
|
||
//! - 私钥以门限形式分片存储
|
||
//! - 签名大小固定,与文明成员数无关
|
||
//! - 抗量子 (可基于格密码实现)
|
||
|
||
use crate::types::{CivilizationId, CivilizationVector, Hash, SoulSignature};
|
||
use crate::error::{Nrpc4Error, Result};
|
||
use sha2::{Sha256, Digest};
|
||
use tracing::{debug, info};
|
||
|
||
/// 文明管理器
|
||
pub struct CivilizationManager {
|
||
/// 本地文明向量
|
||
civilization: CivilizationVector,
|
||
/// 私钥分片 (门限签名的一部分)
|
||
private_key_share: Option<Vec<u8>>,
|
||
}
|
||
|
||
impl CivilizationManager {
|
||
/// 创建新的文明管理器
|
||
pub fn new(civilization: CivilizationVector) -> Self {
|
||
info!("Creating civilization manager for: {}", civilization.civilization_id);
|
||
Self {
|
||
civilization,
|
||
private_key_share: None,
|
||
}
|
||
}
|
||
|
||
/// 获取文明ID
|
||
pub fn get_civilization_id(&self) -> &CivilizationId {
|
||
&self.civilization.civilization_id
|
||
}
|
||
|
||
/// 获取文明向量
|
||
pub fn get_civilization_vector(&self) -> &CivilizationVector {
|
||
&self.civilization
|
||
}
|
||
|
||
/// 更新宪法哈希
|
||
pub fn update_constitution_hash(&mut self, new_hash: Hash) {
|
||
info!(
|
||
"Updating constitution hash for civilization: {}",
|
||
self.civilization.civilization_id
|
||
);
|
||
self.civilization.constitution_hash = new_hash;
|
||
}
|
||
|
||
/// 添加支持的协议
|
||
pub fn add_supported_protocol(&mut self, protocol: String) {
|
||
debug!(
|
||
"Adding protocol {} to civilization: {}",
|
||
protocol, self.civilization.civilization_id
|
||
);
|
||
self.civilization.add_protocol(protocol);
|
||
}
|
||
|
||
/// 设置私钥分片
|
||
pub fn set_private_key_share(&mut self, share: Vec<u8>) {
|
||
info!("Setting private key share for civilization");
|
||
self.private_key_share = Some(share);
|
||
}
|
||
|
||
/// 检查是否有私钥分片
|
||
pub fn has_private_key_share(&self) -> bool {
|
||
self.private_key_share.is_some()
|
||
}
|
||
}
|
||
|
||
/// 灵魂签名器
|
||
pub struct SoulSigner {
|
||
/// 文明ID
|
||
civilization_id: CivilizationId,
|
||
/// 私钥分片
|
||
private_key_share: Vec<u8>,
|
||
/// 门限值
|
||
threshold: u32,
|
||
/// 总节点数
|
||
total_nodes: u32,
|
||
}
|
||
|
||
impl SoulSigner {
|
||
/// 创建新的灵魂签名器
|
||
pub fn new(
|
||
civilization_id: CivilizationId,
|
||
private_key_share: Vec<u8>,
|
||
threshold: u32,
|
||
total_nodes: u32,
|
||
) -> Self {
|
||
info!(
|
||
"Creating soul signer for civilization: {} (threshold: {}/{})",
|
||
civilization_id, threshold, total_nodes
|
||
);
|
||
Self {
|
||
civilization_id,
|
||
private_key_share,
|
||
threshold,
|
||
total_nodes,
|
||
}
|
||
}
|
||
|
||
/// 生成部分签名
|
||
///
|
||
/// 实际实现应该使用门限签名算法 (如BLS门限签名或Dilithium门限版本)
|
||
/// 这里使用简化实现
|
||
pub fn sign_partial(&self, message: &[u8]) -> Result<Vec<u8>> {
|
||
debug!(
|
||
"Generating partial signature for civilization: {}",
|
||
self.civilization_id
|
||
);
|
||
|
||
// 简化实现: 使用SHA256哈希模拟部分签名
|
||
let mut hasher = Sha256::new();
|
||
hasher.update(message);
|
||
hasher.update(&self.private_key_share);
|
||
let signature = hasher.finalize().to_vec();
|
||
|
||
Ok(signature)
|
||
}
|
||
|
||
/// 聚合部分签名
|
||
///
|
||
/// 实际实现应该使用门限签名聚合算法
|
||
/// 这里使用简化实现
|
||
pub fn aggregate_signatures(
|
||
&self,
|
||
partial_signatures: Vec<Vec<u8>>,
|
||
) -> Result<SoulSignature> {
|
||
if partial_signatures.len() < self.threshold as usize {
|
||
return Err(Nrpc4Error::SoulSignatureError(format!(
|
||
"Not enough signatures: got {}, need {}",
|
||
partial_signatures.len(),
|
||
self.threshold
|
||
)));
|
||
}
|
||
|
||
info!(
|
||
"Aggregating {} partial signatures for civilization: {}",
|
||
partial_signatures.len(),
|
||
self.civilization_id
|
||
);
|
||
|
||
// 简化实现: 连接所有部分签名
|
||
let mut aggregated = Vec::new();
|
||
for sig in &partial_signatures {
|
||
aggregated.extend_from_slice(sig);
|
||
}
|
||
|
||
Ok(SoulSignature {
|
||
signature: aggregated,
|
||
participant_count: partial_signatures.len() as u32,
|
||
threshold: self.threshold,
|
||
})
|
||
}
|
||
|
||
/// 验证灵魂签名
|
||
///
|
||
/// 实际实现应该使用门限签名验证算法
|
||
/// 这里使用简化实现
|
||
pub fn verify_soul_signature(
|
||
&self,
|
||
_message: &[u8],
|
||
signature: &SoulSignature,
|
||
) -> Result<bool> {
|
||
debug!(
|
||
"Verifying soul signature for civilization: {}",
|
||
self.civilization_id
|
||
);
|
||
|
||
// 检查参与者数量是否满足门限
|
||
if signature.participant_count < signature.threshold {
|
||
return Ok(false);
|
||
}
|
||
|
||
// 简化实现: 总是返回true
|
||
// 实际实现应该验证签名的密码学正确性
|
||
Ok(true)
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn test_civilization_manager() {
|
||
let civ_id = "nac-mainnet-001".to_string();
|
||
let vector = CivilizationVector::new(civ_id.clone(), [0u8; 32], [1u8; 32]);
|
||
let mut manager = CivilizationManager::new(vector);
|
||
|
||
assert_eq!(manager.get_civilization_id(), &civ_id);
|
||
assert!(!manager.has_private_key_share());
|
||
|
||
manager.set_private_key_share(vec![1, 2, 3, 4]);
|
||
assert!(manager.has_private_key_share());
|
||
|
||
manager.add_supported_protocol("XTZH".to_string());
|
||
assert_eq!(manager.get_civilization_vector().supported_protocols.len(), 1);
|
||
}
|
||
|
||
#[test]
|
||
fn test_soul_signer() {
|
||
let civ_id = "nac-mainnet-001".to_string();
|
||
let signer = SoulSigner::new(civ_id, vec![1, 2, 3, 4], 2, 3);
|
||
|
||
let message = b"test message";
|
||
let partial_sig = signer.sign_partial(message).unwrap();
|
||
assert!(!partial_sig.is_empty());
|
||
|
||
// 测试聚合签名
|
||
let partial_sigs = vec![partial_sig.clone(), partial_sig.clone()];
|
||
let soul_sig = signer.aggregate_signatures(partial_sigs).unwrap();
|
||
assert_eq!(soul_sig.participant_count, 2);
|
||
assert_eq!(soul_sig.threshold, 2);
|
||
|
||
// 测试验证签名
|
||
let is_valid = signer.verify_soul_signature(message, &soul_sig).unwrap();
|
||
assert!(is_valid);
|
||
}
|
||
|
||
#[test]
|
||
fn test_insufficient_signatures() {
|
||
let civ_id = "nac-mainnet-001".to_string();
|
||
let signer = SoulSigner::new(civ_id, vec![1, 2, 3, 4], 3, 5);
|
||
|
||
let message = b"test message";
|
||
let partial_sig = signer.sign_partial(message).unwrap();
|
||
|
||
// 只有2个签名,但需要3个
|
||
let partial_sigs = vec![partial_sig.clone(), partial_sig];
|
||
let result = signer.aggregate_signatures(partial_sigs);
|
||
assert!(result.is_err());
|
||
}
|
||
}
|