Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ed25519 sign #304

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading