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

chore: use crates.io secpfun, use bytes to convert types #269

Merged
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ tiny-keccak = { version = "2", features = ["keccak"] }

bincode = { version = "1", optional = true }
curve25519-dalek = { version = "3", features = ["serde"] }
ecdsa_fun = { git = "https://github.com/farcaster-project/secp256kfun.git", branch = "secp256k1/0.22", default-features = false, features = ["all"], optional = true }
ecdsa_fun = { version = "0.7", default-features = false, features = ["all"], optional = true }
rand = { version = "0.8.4", optional = true }
rand_alt = { package = "rand", version = "0.7.3", features = ["std"] }
rand_chacha = { version = "0.3.1", optional = true }
secp256kfun = { git = "https://github.com/farcaster-project/secp256kfun.git", branch = "secp256k1/0.22", default-features = false, features = ["std", "serde", "libsecp_compat"], optional = true }
secp256kfun = { version = "0.7", default-features = false, features = ["std", "serde", "libsecp_compat"], optional = true }
sha2 = { version = "0.9", optional = true }
sha3 = "0.10"

Expand Down
71 changes: 58 additions & 13 deletions src/swap/btcxmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ use ecdsa_fun::{
fun::{Point, Scalar},
nonce, ECDSA,
};
// FIXME: when secp256kfun as new crates.io release
#[cfg(feature = "experimental")]
use rand::rngs::ThreadRng;
#[cfg(feature = "experimental")]
use rand_chacha::ChaCha20Rng;
#[cfg(feature = "experimental")]
use secp256kfun::marker::*;
#[cfg(feature = "experimental")]
use sha2::Sha256;

#[cfg(feature = "experimental")]
Expand Down Expand Up @@ -306,7 +309,12 @@ impl Sign<PublicKey, Sha256dHash, Signature> for KeyManager {
) -> Result<Signature, crypto::Error> {
let secret_key = self.get_or_derive_bitcoin_key(key)?;

let secret_key = Scalar::from(secret_key);
// FIXME: when new version is released on crates.io
// let secret_key = Scalar::from(secret_key);
let secret_key = Scalar::from_slice(&secret_key[..])
.unwrap()
.mark::<NonZero>()
.expect("SecretKey is never zero");
let message_hash: &[u8; 32] = {
use bitcoin::hashes::Hash;
msg.as_inner()
Expand All @@ -315,7 +323,12 @@ impl Sign<PublicKey, Sha256dHash, Signature> for KeyManager {
let nonce_gen = nonce::Synthetic::<Sha256, nonce::GlobalRng<ThreadRng>>::default();
let ecdsa = ECDSA::new(nonce_gen);

Ok(ecdsa.sign(&secret_key, message_hash).into())
// FIXME
// Ok(ecdsa.sign(&secret_key, message_hash).into())
Ok(
Signature::from_compact(ecdsa.sign(&secret_key, message_hash).to_bytes().as_ref())
.unwrap(),
)
}

fn verify_signature(
Expand Down Expand Up @@ -343,8 +356,15 @@ impl EncSign<PublicKey, Sha256dHash, Signature, EncryptedSignature> for KeyManag
let secret_key = self.get_or_derive_bitcoin_key(signing_key)?;

let engine = Adaptor::<Transcript, NonceGen>::default();
let secret_signing_key = Scalar::from(secret_key);
let encryption_key = Point::from(*encryption_key);
// FIXME
// let secret_signing_key = Scalar::from(secret_key);
let secret_signing_key = Scalar::from_slice(&secret_key[..])
.unwrap()
.mark::<NonZero>()
.expect("SecretKey is never zero");
// FIXME
// let encryption_key = Point::from(*encryption_key);
let encryption_key = Point::from_bytes(encryption_key.serialize()).unwrap();
let message_hash: &[u8; 32] = {
use bitcoin::hashes::Hash;
msg.as_inner()
Expand All @@ -361,8 +381,11 @@ impl EncSign<PublicKey, Sha256dHash, Signature, EncryptedSignature> for KeyManag
sig: &EncryptedSignature,
) -> Result<(), crypto::Error> {
let engine = Adaptor::<Transcript, NonceGen>::default();
let verification_key = Point::from(*signing_key);
let encryption_key = Point::from(*encryption_key);
// FIXME
// let verification_key = Point::from(*signing_key);
// let encryption_key = Point::from(*encryption_key);
let verification_key = Point::from_bytes(signing_key.serialize()).unwrap();
let encryption_key = Point::from_bytes(encryption_key.serialize()).unwrap();
let message_hash: &[u8; 32] = {
use bitcoin::hashes::Hash;
msg.as_inner()
Expand Down Expand Up @@ -394,9 +417,22 @@ impl EncSign<PublicKey, Sha256dHash, Signature, EncryptedSignature> for KeyManag
.map_err(crypto::Error::new)?;

let adaptor = Adaptor::<Transcript, NonceGen>::default();
let decryption_key = Scalar::from(secret_key);

Ok(adaptor.decrypt_signature(&decryption_key, sig).into())
// FIXME
// let decryption_key = Scalar::from(secret_key);
let decryption_key = Scalar::from_slice(&secret_key[..])
.unwrap()
.mark::<NonZero>()
.expect("SecretKey is never zero");

// FIXME
// Ok(adaptor.decrypt_signature(&decryption_key, sig).into())
Ok(Signature::from_compact(
adaptor
.decrypt_signature(&decryption_key, sig)
.to_bytes()
.as_ref(),
)
.unwrap())
}
}

Expand All @@ -410,11 +446,18 @@ impl RecoverSecret<PublicKey, SecretKey, Signature, EncryptedSignature> for KeyM
sig: Signature,
) -> SecretKey {
let adaptor = Adaptor::<Transcript, NonceGen>::default();
let encryption_key = Point::from(*encryption_key);
let signature = ecdsa_fun::Signature::from(sig);
// FIXME
// let encryption_key = Point::from(*encryption_key);
//let signature = ecdsa_fun::Signature::from(sig);
let encryption_key = Point::from_bytes(encryption_key.serialize()).unwrap();
let signature = ecdsa_fun::Signature::from_bytes(sig.serialize_compact()).unwrap();

match adaptor.recover_decryption_key(&encryption_key, &signature, &encrypted_sig) {
Some(decryption_key) => decryption_key.into(),
// FIXME
// Some(decryption_key) => decryption_key.into(),
Some(decryption_key) => {
SecretKey::from_slice(decryption_key.to_bytes().as_ref()).unwrap()
}
None => panic!("signature is not the decryption of our original encrypted signature"),
}
}
Expand Down Expand Up @@ -456,7 +499,9 @@ impl ProveCrossGroupDleq<PublicKey, monero::PublicKey, DLEQProof> for KeyManager
.point
.decompress()
.expect("Valid point to decompress"),
ecdsa_fun::fun::Point::from(*encryption_key),
// FIXME
//ecdsa_fun::fun::Point::from(*encryption_key),
Point::from_bytes(encryption_key.serialize()).unwrap(),
)
}
}
Expand Down