Skip to content

Commit

Permalink
Use global secp256k1 context (#1662)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaziciahmet authored Jan 3, 2025
1 parent 7c787d1 commit f27657b
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 42 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/bitcoin-da/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ metrics = { workspace = true, optional = true }
pin-project = { workspace = true, optional = true, features = [] }
rand = { workspace = true }
reqwest = { workspace = true, optional = true }
secp256k1 = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true, features = ["raw_value"] }
thiserror = { workspace = true }
Expand Down
16 changes: 8 additions & 8 deletions crates/bitcoin-da/src/helpers/builders/batch_proof_namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ use bitcoin::hashes::Hash;
use bitcoin::key::{TapTweak, TweakedPublicKey, UntweakedKeypair};
use bitcoin::opcodes::all::{OP_CHECKSIGVERIFY, OP_NIP};
use bitcoin::script::PushBytesBuf;
use bitcoin::secp256k1::{Secp256k1, SecretKey, XOnlyPublicKey};
use bitcoin::secp256k1::{SecretKey, XOnlyPublicKey};
use bitcoin::{Address, Amount, Network, Transaction};
use metrics::histogram;
use secp256k1::SECP256K1;
use serde::Serialize;
use tracing::{instrument, trace, warn};

Expand Down Expand Up @@ -97,8 +98,7 @@ pub fn create_batchproof_type_0(
"The body of a serialized sequencer commitment exceeds 520 bytes"
);
// Create reveal key
let secp256k1 = Secp256k1::new();
let key_pair = UntweakedKeypair::new(&secp256k1, &mut rand::thread_rng());
let key_pair = UntweakedKeypair::new(SECP256K1, &mut rand::thread_rng());
let (public_key, _parity) = XOnlyPublicKey::from_keypair(&key_pair);

let kind = TransactionKindBatchProof::SequencerCommitment;
Expand Down Expand Up @@ -146,10 +146,10 @@ pub fn create_batchproof_type_0(
let reveal_script = reveal_script_builder.into_script();

let (control_block, merkle_root, tapscript_hash) =
build_taproot(&reveal_script, public_key, &secp256k1);
build_taproot(&reveal_script, public_key, SECP256K1);

// create commit tx address
let commit_tx_address = Address::p2tr(&secp256k1, public_key, merkle_root, network);
let commit_tx_address = Address::p2tr(SECP256K1, public_key, merkle_root, network);

let reveal_value = REVEAL_OUTPUT_AMOUNT;
let fee = get_size_reveal(
Expand Down Expand Up @@ -192,7 +192,7 @@ pub fn create_batchproof_type_0(
reveal_script,
control_block,
&key_pair,
&secp256k1,
SECP256K1,
);

let min_commit_value = Amount::from_sat(fee + reveal_value);
Expand All @@ -203,7 +203,7 @@ pub fn create_batchproof_type_0(
// check if first N bytes equal to the given prefix
if reveal_hash.starts_with(reveal_tx_prefix) {
// check if inscription locked to the correct address
let recovery_key_pair = key_pair.tap_tweak(&secp256k1, merkle_root);
let recovery_key_pair = key_pair.tap_tweak(SECP256K1, merkle_root);
let (x_only_pub_key, _parity) = recovery_key_pair.to_inner().x_only_public_key();
assert_eq!(
Address::p2tr_tweaked(
Expand Down Expand Up @@ -236,7 +236,7 @@ pub fn create_batchproof_type_0(
&mut reveal_tx,
tapscript_hash,
&key_pair,
&secp256k1,
SECP256K1,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use bitcoin::hashes::Hash;
use bitcoin::key::{TapTweak, TweakedPublicKey, UntweakedKeypair};
use bitcoin::opcodes::all::{OP_CHECKSIGVERIFY, OP_NIP};
use bitcoin::script::PushBytesBuf;
use bitcoin::secp256k1::{Secp256k1, SecretKey, XOnlyPublicKey};
use bitcoin::secp256k1::{SecretKey, XOnlyPublicKey};
use bitcoin::{Address, Amount, Network, Transaction};
use secp256k1::SECP256K1;
use serde::Serialize;
use sov_rollup_interface::da::DaDataLightClient;
use tracing::{instrument, trace, warn};
Expand Down Expand Up @@ -176,8 +177,7 @@ pub fn create_inscription_type_0(
reveal_tx_prefix: &[u8],
) -> Result<LightClientTxs, anyhow::Error> {
// Create reveal key
let secp256k1 = Secp256k1::new();
let key_pair = UntweakedKeypair::new(&secp256k1, &mut rand::thread_rng());
let key_pair = UntweakedKeypair::new(SECP256K1, &mut rand::thread_rng());
let (public_key, _parity) = XOnlyPublicKey::from_keypair(&key_pair);

let kind = TransactionKindLightClient::Complete;
Expand Down Expand Up @@ -231,10 +231,10 @@ pub fn create_inscription_type_0(
let reveal_script = reveal_script_builder.into_script();

let (control_block, merkle_root, tapscript_hash) =
build_taproot(&reveal_script, public_key, &secp256k1);
build_taproot(&reveal_script, public_key, SECP256K1);

// create commit tx address
let commit_tx_address = Address::p2tr(&secp256k1, public_key, merkle_root, network);
let commit_tx_address = Address::p2tr(SECP256K1, public_key, merkle_root, network);

let reveal_value = REVEAL_OUTPUT_AMOUNT;
let fee = get_size_reveal(
Expand Down Expand Up @@ -277,7 +277,7 @@ pub fn create_inscription_type_0(
reveal_script,
control_block,
&key_pair,
&secp256k1,
SECP256K1,
);

let min_commit_value = Amount::from_sat(fee + reveal_value);
Expand All @@ -287,7 +287,7 @@ pub fn create_inscription_type_0(
// check if first N bytes equal to the given prefix
if reveal_hash.starts_with(reveal_tx_prefix) {
// check if inscription locked to the correct address
let recovery_key_pair = key_pair.tap_tweak(&secp256k1, merkle_root);
let recovery_key_pair = key_pair.tap_tweak(SECP256K1, merkle_root);
let (x_only_pub_key, _parity) = recovery_key_pair.to_inner().x_only_public_key();
assert_eq!(
Address::p2tr_tweaked(
Expand All @@ -314,7 +314,7 @@ pub fn create_inscription_type_0(
&mut reveal_tx,
tapscript_hash,
&key_pair,
&secp256k1,
SECP256K1,
);
}
}
Expand All @@ -339,8 +339,7 @@ pub fn create_inscription_type_1(
reveal_tx_prefix: &[u8],
) -> Result<LightClientTxs, anyhow::Error> {
// Create reveal key
let secp256k1 = Secp256k1::new();
let key_pair = UntweakedKeypair::new(&secp256k1, &mut rand::thread_rng());
let key_pair = UntweakedKeypair::new(SECP256K1, &mut rand::thread_rng());
let (public_key, _parity) = XOnlyPublicKey::from_keypair(&key_pair);

let mut commit_chunks: Vec<Transaction> = vec![];
Expand All @@ -367,10 +366,10 @@ pub fn create_inscription_type_1(
let reveal_script = reveal_script_builder.push_opcode(OP_ENDIF).into_script();

let (control_block, merkle_root, tapscript_hash) =
build_taproot(&reveal_script, public_key, &secp256k1);
build_taproot(&reveal_script, public_key, SECP256K1);

// create commit tx address
let commit_tx_address = Address::p2tr(&secp256k1, public_key, merkle_root, network);
let commit_tx_address = Address::p2tr(SECP256K1, public_key, merkle_root, network);

let reveal_value = REVEAL_OUTPUT_AMOUNT;
let fee = get_size_reveal(
Expand Down Expand Up @@ -428,11 +427,11 @@ pub fn create_inscription_type_1(
reveal_script,
control_block,
&key_pair,
&secp256k1,
SECP256K1,
);

// check if inscription locked to the correct address
let recovery_key_pair = key_pair.tap_tweak(&secp256k1, merkle_root);
let recovery_key_pair = key_pair.tap_tweak(SECP256K1, merkle_root);
let (x_only_pub_key, _parity) = recovery_key_pair.to_inner().x_only_public_key();
assert_eq!(
Address::p2tr_tweaked(
Expand Down Expand Up @@ -525,10 +524,10 @@ pub fn create_inscription_type_1(
let reveal_script = reveal_script_builder.into_script();

let (control_block, merkle_root, tapscript_hash) =
build_taproot(&reveal_script, public_key, &secp256k1);
build_taproot(&reveal_script, public_key, SECP256K1);

// create commit tx address
let commit_tx_address = Address::p2tr(&secp256k1, public_key, merkle_root, network);
let commit_tx_address = Address::p2tr(SECP256K1, public_key, merkle_root, network);

let reveal_value = REVEAL_OUTPUT_AMOUNT;
let fee = get_size_reveal(
Expand Down Expand Up @@ -570,7 +569,7 @@ pub fn create_inscription_type_1(
reveal_script,
control_block,
&key_pair,
&secp256k1,
SECP256K1,
);

let min_commit_value = Amount::from_sat(fee + reveal_value);
Expand All @@ -581,7 +580,7 @@ pub fn create_inscription_type_1(
// check if first N bytes equal to the given prefix
if reveal_hash.starts_with(reveal_tx_prefix) {
// check if inscription locked to the correct address
let recovery_key_pair = key_pair.tap_tweak(&secp256k1, merkle_root);
let recovery_key_pair = key_pair.tap_tweak(SECP256K1, merkle_root);
let (x_only_pub_key, _parity) = recovery_key_pair.to_inner().x_only_public_key();
assert_eq!(
Address::p2tr_tweaked(
Expand Down Expand Up @@ -610,7 +609,7 @@ pub fn create_inscription_type_1(
&mut reveal_tx,
tapscript_hash,
&key_pair,
&secp256k1,
SECP256K1,
);
}
}
Expand All @@ -635,8 +634,7 @@ pub fn create_inscription_type_2(
reveal_tx_prefix: &[u8],
) -> Result<LightClientTxs, anyhow::Error> {
// Create reveal key
let secp256k1 = Secp256k1::new();
let key_pair = UntweakedKeypair::new(&secp256k1, &mut rand::thread_rng());
let key_pair = UntweakedKeypair::new(SECP256K1, &mut rand::thread_rng());
let (public_key, _parity) = XOnlyPublicKey::from_keypair(&key_pair);

let kind = TransactionKindLightClient::BatchProofMethodId;
Expand Down Expand Up @@ -690,10 +688,10 @@ pub fn create_inscription_type_2(
let reveal_script = reveal_script_builder.into_script();

let (control_block, merkle_root, tapscript_hash) =
build_taproot(&reveal_script, public_key, &secp256k1);
build_taproot(&reveal_script, public_key, SECP256K1);

// create commit tx address
let commit_tx_address = Address::p2tr(&secp256k1, public_key, merkle_root, network);
let commit_tx_address = Address::p2tr(SECP256K1, public_key, merkle_root, network);

let reveal_value = REVEAL_OUTPUT_AMOUNT;
let fee = get_size_reveal(
Expand Down Expand Up @@ -736,7 +734,7 @@ pub fn create_inscription_type_2(
reveal_script,
control_block,
&key_pair,
&secp256k1,
SECP256K1,
);

let min_commit_value = Amount::from_sat(fee + reveal_value);
Expand All @@ -746,7 +744,7 @@ pub fn create_inscription_type_2(
// check if first N bytes equal to the given prefix
if reveal_hash.starts_with(reveal_tx_prefix) {
// check if inscription locked to the correct address
let recovery_key_pair = key_pair.tap_tweak(&secp256k1, merkle_root);
let recovery_key_pair = key_pair.tap_tweak(SECP256K1, merkle_root);
let (x_only_pub_key, _parity) = recovery_key_pair.to_inner().x_only_public_key();
assert_eq!(
Address::p2tr_tweaked(
Expand All @@ -773,7 +771,7 @@ pub fn create_inscription_type_2(
&mut reveal_tx,
tapscript_hash,
&key_pair,
&secp256k1,
SECP256K1,
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bitcoin-da/src/helpers/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use bitcoin::{
Address, Amount, OutPoint, ScriptBuf, Sequence, TapLeafHash, TapNodeHash, Transaction, TxIn,
TxOut, Txid, Witness, XOnlyPublicKey,
};
use secp256k1::SECP256K1;
use serde::Serialize;
use tracing::{instrument, trace, warn};

Expand Down Expand Up @@ -464,10 +465,9 @@ fn choose_utxos(
/// Returns (signature, public_key)
pub fn sign_blob_with_private_key(blob: &[u8], private_key: &SecretKey) -> (Vec<u8>, Vec<u8>) {
let message = calculate_sha256(blob);
let secp = Secp256k1::new();
let public_key = secp256k1::PublicKey::from_secret_key(&secp, private_key);
let public_key = secp256k1::PublicKey::from_secret_key(SECP256K1, private_key);
let msg = secp256k1::Message::from_digest(message);
let sig = secp.sign_ecdsa(&msg, private_key);
let sig = SECP256K1.sign_ecdsa(&msg, private_key);
(
sig.serialize_compact().to_vec(),
public_key.serialize().to_vec(),
Expand Down
8 changes: 3 additions & 5 deletions crates/bitcoin-da/src/helpers/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use bitcoin::blockdata::script::Instruction;
use bitcoin::opcodes::all::OP_CHECKSIGVERIFY;
use bitcoin::script::Instruction::{Op, PushBytes};
use bitcoin::script::{Error as ScriptError, PushBytes as StructPushBytes};
use bitcoin::secp256k1::{ecdsa, Message, Secp256k1};
use bitcoin::{secp256k1, Opcode, Script, Transaction};
use bitcoin::{Opcode, Script, Transaction};
use secp256k1::{self, ecdsa, Message, SECP256K1};
use thiserror::Error;

use super::calculate_sha256;
Expand Down Expand Up @@ -76,11 +76,9 @@ pub trait VerifyParsed {
let hash = calculate_sha256(self.body());
let message = Message::from_digest_slice(&hash).unwrap(); // cannot fail

let secp = Secp256k1::new();

if public_key.is_ok()
&& signature.is_ok()
&& secp
&& SECP256K1
.verify_ecdsa(&message, &signature.unwrap(), &public_key.unwrap())
.is_ok()
{
Expand Down
1 change: 1 addition & 0 deletions guests/risc0/batch-proof/bitcoin/Cargo.lock

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

1 change: 1 addition & 0 deletions guests/risc0/light-client-proof/bitcoin/Cargo.lock

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

0 comments on commit f27657b

Please sign in to comment.