Skip to content

Commit

Permalink
Instead with secp256k1-musl
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Aug 14, 2024
1 parent 8c8dc2f commit bcf7bf9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions contracts/ccc-btc-lock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
22 changes: 16 additions & 6 deletions contracts/ccc-btc-lock/src/entry.rs
Original file line number Diff line number Diff line change
@@ -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] {
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit bcf7bf9

Please sign in to comment.