From bcf7bf9b854bae73979870f1083ae1d701279d36 Mon Sep 17 00:00:00 2001 From: mohanson Date: Wed, 14 Aug 2024 15:01:26 +0800 Subject: [PATCH] Instead with secp256k1-musl --- Cargo.lock | 17 +++++++++++++++++ contracts/ccc-btc-lock/Cargo.toml | 2 ++ contracts/ccc-btc-lock/src/entry.rs | 22 ++++++++++++++++------ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d963999..1a53344 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,6 +50,7 @@ dependencies = [ "hex", "k256", "ripemd", + "secp256k1", "sha2", ] @@ -432,6 +433,22 @@ dependencies = [ "zeroize", ] +[[package]] +name = "secp256k1" +version = "0.29.0" +source = "git+https://github.com/libraries/rust-secp256k1?branch=musl#dc1497dbaaa077f434fd0e36d0add11123e21cea" +dependencies = [ + "secp256k1-sys", +] + +[[package]] +name = "secp256k1-sys" +version = "0.10.0" +source = "git+https://github.com/libraries/rust-secp256k1?branch=musl#dc1497dbaaa077f434fd0e36d0add11123e21cea" +dependencies = [ + "cc", +] + [[package]] name = "semver" version = "1.0.23" diff --git a/contracts/ccc-btc-lock/Cargo.toml b/contracts/ccc-btc-lock/Cargo.toml index 1c180bb..b5d816a 100644 --- a/contracts/ccc-btc-lock/Cargo.toml +++ b/contracts/ccc-btc-lock/Cargo.toml @@ -10,3 +10,5 @@ k256 = { version = "=0.13.1", default-features = false, features = ["arithmetic" hex = { version = "0.4", default-features = false, features = ["alloc"] } sha2 = { version = "0.10.8", default-features = false } ripemd = { version = "0.1.3", default-features = false } + +secp256k1 = { git = "https://github.com/libraries/rust-secp256k1", branch = "musl", default-features = false, features = ["recovery", "lowmemory"] } diff --git a/contracts/ccc-btc-lock/src/entry.rs b/contracts/ccc-btc-lock/src/entry.rs index e0d64f5..0f54681 100644 --- a/contracts/ccc-btc-lock/src/entry.rs +++ b/contracts/ccc-btc-lock/src/entry.rs @@ -1,12 +1,13 @@ use crate::error::Error; use alloc::vec::Vec; -use ckb_lock_helper::{generate_sighash_all, println_hex, secp256k1_patch::recover_from_prehash}; +use ckb_lock_helper::{generate_sighash_all, println_hex}; use ckb_std::{ ckb_constants::Source, high_level::{load_script, load_witness_args}, }; -use k256::ecdsa::{RecoveryId, Signature}; use ripemd::{Digest, Ripemd160}; +use secp256k1::ffi::types::AlignedType; +use secp256k1::{self, ecdsa, Message, Secp256k1}; use sha2::Sha256; fn ripemd160_sha256(msg: &[u8]) -> [u8; 20] { @@ -70,11 +71,20 @@ pub fn entry() -> Result<(), Error> { 39 | 40 | 41 | 42 => sig_raw[0] - 39, _ => sig_raw[0], }; - let rec_id = RecoveryId::try_from(rec_id).map_err(|_| Error::InvalidRecoverId)?; - let sig = Signature::from_slice(&sig_raw[1..]).map_err(|_| Error::WrongSignatureFormat)?; - let pubkey_result = recover_from_prehash(&digest_hash, &sig, rec_id) + + let mut secp_buf = [AlignedType::zeroed(); 70_000]; + let secp = Secp256k1::preallocated_new(&mut secp_buf).unwrap(); + let pubkey_result = secp + .recover_ecdsa( + &Message::from_digest_slice(&digest_hash).unwrap(), + &ecdsa::RecoverableSignature::from_compact( + &sig_raw[1..], + ecdsa::RecoveryId::from_i32(rec_id as i32).map_err(|_| Error::InvalidRecoverId)?, + ) + .unwrap(), + ) .map_err(|_| Error::CanNotRecover)? - .to_sec1_bytes(); + .serialize(); assert!(pubkey_result.len() == 33); let pubkey_hash_result = ripemd160_sha256(&pubkey_result); println_hex("pubkey_hash_result", pubkey_hash_result.as_ref());