Skip to content

Commit

Permalink
feat: ed25519 sign
Browse files Browse the repository at this point in the history
  • Loading branch information
aya015757881 committed Dec 13, 2024
1 parent 1a767e5 commit 310327f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/anychain-kms/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "anychain-kms"
description = "A Rust library providing Key Management Schema for AnyChain. Handles general security and signature algorithms."
version = "0.1.14"
version = "0.1.15"
keywords = ["cryptography", "security", "signature", "algorithm"]
categories = ["cryptography::cryptocurrencies"]

Expand Down Expand Up @@ -32,6 +32,7 @@ ed25519-dalek = { workspace = true }
curve25519-dalek = { workspace = true }
group = "0.13.0"
encoding = "0.2.33"
anychain-core = { version = "0.1.7", path = "../anychain-core" }

[dev-dependencies]
hex-literal = "0.4"
Expand Down
25 changes: 18 additions & 7 deletions crates/anychain-kms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,26 @@ pub mod bip39;
pub mod crypto;
pub mod error;

use anychain_core::crypto::sha256;
use bip32::PrivateKey;
use curve25519_dalek::Scalar;
use ed25519_dalek::ExpandedSecretKey;
use error::Error;

pub fn ecdsa_sign(
secret_key: &libsecp256k1::SecretKey,
bytes: &[u8],
) -> Result<(Vec<u8>, u8), Error> {
let message = libsecp256k1::Message::parse_slice(bytes)?;
let (signature, recid) = libsecp256k1::sign(&message, secret_key);
Ok((signature.serialize().to_vec(), recid.into()))
pub fn secp256k1_sign(sk: &libsecp256k1::SecretKey, msg: &[u8]) -> Result<(Vec<u8>, u8), Error> {
let msg = libsecp256k1::Message::parse_slice(msg)?;
let (sig, recid) = libsecp256k1::sign(&msg, sk);
Ok((sig.serialize().to_vec(), recid.into()))
}

pub fn ed25519_sign(sk: &Scalar, msg: &[u8]) -> Result<Vec<u8>, Error> {
let sk_bytes = PrivateKey::to_bytes(sk);
let nonce = sha256(&sk_bytes).to_vec();
let xsk = [sk_bytes, nonce].concat();
let xsk = ExpandedSecretKey::from_bytes(&xsk).unwrap();
let pk = PrivateKey::public_key(sk);
let sig = xsk.sign(msg, &pk).to_bytes().to_vec();
Ok(sig)
}

#[cfg(test)]
Expand Down

0 comments on commit 310327f

Please sign in to comment.