#签名 #x25519 #ed25519 #加密 #eddsa

已删除 radicle-ed25519-compact

为 Radicle 定制的 ed25519-compact crate 的分支

2.0.3 2023年2月13日

#8#eddsa

MIT 许可证

135KB
3.5K SLoC

GitHub CI

Rust 的紧凑型 Ed25519 和 X25519 实现

  • 形式验证的 Curve25519 字段算术
  • no_std-友好
  • WebAssembly 友好
  • Compute@Edge 友好
  • 轻量级
  • 如果应用提供随机数,则无依赖项
  • 如果不,则只有一个可移植的依赖项 (getrandom)
  • 支持增量签名(流式 API)
  • 安全和简单的 Rust 接口

API 文档

示例用法

cargo.toml:

[dependencies]
ed25519-compact = "2"

示例代码

// A message to sign and verify.
let message = b"test";

// Generates a new key pair using a random seed.
// A given seed will always produce the same key pair.
let key_pair = KeyPair::from_seed(Seed::default());

// Computes a signature for this message using the secret part of the key pair.
let signature = key_pair.sk.sign(message, Some(Noise::default()));

// Verifies the signature using the public part of the key pair.
key_pair
    .pk
    .verify(message, &signature)
    .expect("Signature didn't verify");

// Verification of a different message using the same signature and public key fails.
key_pair
    .pk
    .verify(b"A different message", &signature)
    .expect_err("Signature shouldn't verify");

// All these structures can be viewed as raw bytes simply by dereferencing them:
let signature_as_bytes: &[u8] = signature.as_ref();
println!("Signature as bytes: {:?}", signature_as_bytes);

增量 API 示例用法

消息也可以作为多个部分(流式 API)提供,以便在不使用大量内存的情况下处理大型消息

/// Creates a new key pair.
let kp = KeyPair::generate();

/// Creates a state for an incremental signer.
let mut st = kp.sk.sign_incremental(Noise::default());

/// Feeds the message as any number of chunks, and sign the concatenation.
st.absorb("mes");
st.absorb("sage");
let signature = st.sign();

/// Creates a state for an incremental verifier.
let mut st = kp.pk.verify_incremental(&signature)?;

/// Feeds the message as any number of chunks, and verify the concatenation.
st.absorb("mess");
st.absorb("age");
st.verify()?;

Cargo 功能

  • self-verify:在计算新签名后,验证它是否有效。这较慢,但可提高对故障攻击的鲁棒性。在 WebAssembly 目标上默认启用。
  • std:禁用 no_std 兼容性,以便使错误实现标准 Error 特性。
  • random(默认启用):向 SeedNoise 对象添加 Default 实现,以安全地创建随机密钥和噪声。
  • traits:添加对 ed25519signature crate 的特性的支持。
  • pem:添加导入/导出密钥作为 OpenSSL 兼容 PEM 文件的支持。
  • blind-keys:添加对密钥遮蔽的支持。
  • opt_size:启用大小优化(根据基准测试,以 6.5-7% 的性能损失为代价,减少 8-15% 的大小)。
  • x25519:启用对 X25519 密钥交换系统的支持。
  • disable-signatures:禁用对签名的支持,仅编译对 X25519 的支持。

依赖关系

~140KB