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

Use secp256k1 musig2 #405

Merged
merged 42 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 41 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
14c208c
cargo: Add secp256k1 patch for musig.
ceyhunsen Jan 10, 2025
d8b2a52
Merge branch 'dev' into ceyhun/secp256k1_musig2
ceyhunsen Jan 10, 2025
44ef595
cargo: Revert secp to 0.29 and apply chainway patch.
ceyhunsen Jan 10, 2025
e27c567
cargo: Remove musig2.
ceyhunsen Jan 10, 2025
d390581
musig2: Update functions with new secp types.
ceyhunsen Jan 10, 2025
5abd966
errors: Remove old musig errors and add new ones.
ceyhunsen Jan 10, 2025
78c570f
secp_musig: Switch to the new types wherever possible.
ceyhunsen Jan 10, 2025
cce29a7
database: Add MusigPubNonceDB type.
ceyhunsen Jan 10, 2025
8cb65e9
database: Add new wrapper for MusigAggNonceDB and fix rest.
ceyhunsen Jan 13, 2025
b53c607
verifier-musig2: Fix copying error for nonces in deposit_sign.
ceyhunsen Jan 13, 2025
9fba2c6
musig2: Fix compilation errors caused by the switch.
ceyhunsen Jan 13, 2025
9dbe1a1
database: Add Message wrapper.
ceyhunsen Jan 13, 2025
8f72aea
core: Fix rest of the compilation errors caused by the switch.
ceyhunsen Jan 13, 2025
8551c4a
tests-musig: Fix compilation errors.
ceyhunsen Jan 13, 2025
8233b19
clippy: Apply suggestions.
ceyhunsen Jan 13, 2025
657bdc6
musig: Convert from_digest_slices to from_digest.
ceyhunsen Jan 13, 2025
7db716b
secp: Convert secp types into bitcoin::secp.
ceyhunsen Jan 14, 2025
ca1b016
musig2: Convert secp types into bitcoin::secp types.
ceyhunsen Jan 14, 2025
d0ccc06
musig2: Remove tweak_flag from from_musig2_pks.
ceyhunsen Jan 14, 2025
7ff92f3
cargo: Delete config generator.
ceyhunsen Jan 14, 2025
f16a977
Revert "musig2: Remove tweak_flag from from_musig2_pks."
ceyhunsen Jan 14, 2025
1355110
musig2: Add initial tweak support.
ceyhunsen Jan 15, 2025
07649da
musig: Add create_key_agg_cache and MusigTweak.
ceyhunsen Jan 15, 2025
6d83a3f
musig: Fix wrong params for MusigTweak and add KeyAndScriptSpend.
ceyhunsen Jan 15, 2025
4922146
Add signature verification to aggregate_partial_signatures (#417)
ozankaymak Jan 15, 2025
0f90965
database: Fix wrong encoding for pub and agg nonces.
ceyhunsen Jan 15, 2025
85092de
musig: Remove none from Musig2Mode and make them optional.
ceyhunsen Jan 15, 2025
b4d5d04
feat(musig2): add musig2 tweaking
mmtftr Jan 15, 2025
829abae
musig2: add musig2 tweaking test and update README
mmtftr Jan 16, 2025
5a64603
errors: Add Secp256k1ScalarOutOfRange and return BridgeError in musog2.
ceyhunsen Jan 16, 2025
8850158
musig2: Fix merge conflicts for key_agg_cache_tweak_checks test.
ceyhunsen Jan 16, 2025
c115abd
tests: Fix musig2 test by changing musig2 tweak modes.
ceyhunsen Jan 16, 2025
7c5da16
musig2: Remove xonly field from Musig2Mode.
ceyhunsen Jan 16, 2025
d1859c1
watchtower pk's for watchtower challenge page tx (#404)
atacann Jan 10, 2025
08c700a
Ozan/change anchor (#407)
ozankaymak Jan 10, 2025
960d8fd
Use global context for secp256k1 (#408)
ozankaymak Jan 13, 2025
78600f7
Implement kickoff timeout tx (#413)
ozankaymak Jan 14, 2025
ea7ddaa
Remove time_tx from the inputs of operator_challenge_nack_tx (#414)
ozankaymak Jan 15, 2025
64f9c29
Merge branch 'dev' into ceyhun/secp256k1_musig2
ceyhunsen Jan 16, 2025
b0905d4
merge: Fix compilation errors caused by the merge.
ceyhunsen Jan 16, 2025
bbbacb6
database: Remove redundant borsch usage for MessageDB.
ceyhunsen Jan 16, 2025
9d447be
musig2: Add comments for Musig2Mode and update key_agg_cache tests.
ceyhunsen Jan 17, 2025
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
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ serial_test = "3.2.0"
# bitcoin
bitcoin = "0.32.5"
bitcoincore-rpc = "0.18.0"
musig2 = { version = "0.0.11", features = ["serde"] }
secp256k1 = { version = "0.29.1", features = ["serde", "rand-std", "global-context"] }
secp256k1 = { version = "0.30.0", features = ["serde", "rand", "std", "global-context"] }
bitcoin-script = { git = "https://github.com/BitVM/rust-bitcoin-script", branch= "StructuredScript" }

# async + gRPC
Expand Down Expand Up @@ -59,6 +58,8 @@ ark-relations = { git = "https://github.com/arkworks-rs/snark/" }
ark-snark = { git = "https://github.com/arkworks-rs/snark/" }
ark-groth16 = { git = "https://github.com/arkworks-rs/groth16" }

secp256k1 = { git = "https://github.com/jlest01/rust-secp256k1", branch = "musig2-module" }
ceyhunsen marked this conversation as resolved.
Show resolved Hide resolved

[profile.release]
lto = true
strip = true
Expand Down
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ and configure Bitcoin Core if you haven't already.

### Preparing a Configuration File

Running a binary as a verifier, aggregator or operator requires a configuration
file. Example configuration file is located at
Running the binary as a verifier, aggregator or operator requires a configuration
file. An example configuration file is located at
[`core/tests/data/test_config.toml`](core/tests/data/test_config.toml) and can
be taken as reference. Please copy that configuration file to another location
and modify fields to your local configuration.
Expand Down Expand Up @@ -54,6 +54,30 @@ More information, use `--help` flag:

### Testing

#### Prerequisites

1. **PostgreSQL Database**

Tests require a PostgreSQL database. You can quickly set one up using Docker:

```bash
docker run --name clementine-test-db \
-e POSTGRES_USER=clementine \
-e POSTGRES_PASSWORD=clementine \
-e POSTGRES_DB=clementine \
-p 5432:5432 \
--restart always \
-d postgres:15
```

2. **RISC Zero Toolchain**

For prover tests, you'll need to install the RISC Zero toolchain:

```bash
cargo install cargo-risczero
```

#### Bitcoin Regtest Setup

To simulate deposits, withdrawals, proof generation on the Bitcoin Regtest
Expand Down
8 changes: 1 addition & 7 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ sha2 = { workspace = true }
risc0-zkvm = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
secp256k1 = { workspace = true, features = ["serde"] }
secp256k1 = { workspace = true, features = ["serde", "rand", "std"] }
crypto-bigint = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
Expand All @@ -27,7 +27,6 @@ futures = { workspace = true }
clap = { workspace = true, features = ["derive"] }
toml = { workspace = true }
sqlx = { workspace = true, features = ["runtime-tokio", "postgres", "macros"] }
musig2 = { workspace = true }
header-chain = { workspace = true }
borsh = { workspace = true}
tonic = { workspace = true}
Expand All @@ -49,8 +48,3 @@ testing = []
[[bin]]
name = "server"
path = "src/bin/server.rs"

[[bin]]
name = "config_generator"
path = "src/bin/config_generator.rs"
required-features = ["testing"]
37 changes: 19 additions & 18 deletions core/src/actor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::builder::transaction::TxHandler;
use crate::errors::BridgeError;
use crate::utils::{self, SECP};
use bitcoin::secp256k1::PublicKey;
use bitcoin::sighash::SighashCache;
use bitcoin::taproot::LeafVersion;
use bitcoin::{
Expand All @@ -11,7 +13,6 @@ use bitcoin::{TapLeafHash, TapNodeHash, TapSighashType, TxOut, Witness};
use bitvm::signatures::winternitz::{
self, BinarysearchVerifier, StraightforwardConverter, Winternitz,
};
use secp256k1::SECP256K1;

/// Available transaction types for [`WinternitzDerivationPath`].
#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -91,22 +92,22 @@ impl Default for WinternitzDerivationPath {
pub struct Actor {
pub keypair: Keypair,
_secret_key: SecretKey,
winternitz_secret_key: Option<secp256k1::SecretKey>,
winternitz_secret_key: Option<SecretKey>,
pub xonly_public_key: XOnlyPublicKey,
pub public_key: secp256k1::PublicKey,
pub public_key: PublicKey,
pub address: Address,
}

impl Actor {
#[tracing::instrument(ret(level = tracing::Level::TRACE))]
pub fn new(
sk: SecretKey,
winternitz_secret_key: Option<secp256k1::SecretKey>,
winternitz_secret_key: Option<SecretKey>,
network: bitcoin::Network,
) -> Self {
let keypair = Keypair::from_secret_key(SECP256K1, &sk);
let keypair = Keypair::from_secret_key(&SECP, &sk);
let (xonly, _parity) = XOnlyPublicKey::from_keypair(&keypair);
let address = Address::p2tr(SECP256K1, xonly, None, network);
let address = Address::p2tr(&SECP, xonly, None, network);

Actor {
keypair,
Expand All @@ -124,19 +125,19 @@ impl Actor {
sighash: TapSighash,
merkle_root: Option<TapNodeHash>,
) -> Result<schnorr::Signature, BridgeError> {
Ok(SECP256K1.sign_schnorr(
&Message::from_digest_slice(sighash.as_byte_array()).expect("should be hash"),
Ok(utils::SECP.sign_schnorr(
&Message::from_digest(*sighash.as_byte_array()),
&self.keypair.add_xonly_tweak(
SECP256K1,
&SECP,
&TapTweakHash::from_key_and_tweak(self.xonly_public_key, merkle_root).to_scalar(),
)?,
))
}

#[tracing::instrument(skip(self), ret(level = tracing::Level::TRACE))]
pub fn sign(&self, sighash: TapSighash) -> schnorr::Signature {
SECP256K1.sign_schnorr(
&Message::from_digest_slice(sighash.as_byte_array()).expect("should be hash"),
utils::SECP.sign_schnorr(
&Message::from_digest(*sighash.as_byte_array()),
&self.keypair,
)
}
Expand Down Expand Up @@ -348,11 +349,12 @@ impl Actor {
mod tests {
use super::Actor;
use crate::config::BridgeConfig;
use crate::utils::initialize_logger;
use crate::utils::{initialize_logger, SECP};
use crate::{
actor::WinternitzDerivationPath, builder::transaction::TxHandler,
create_test_config_with_thread_name, database::Database, initialize_database,
};
use bitcoin::secp256k1::SecretKey;
use bitcoin::{
absolute::Height, transaction::Version, Amount, Network, OutPoint, Transaction, TxIn, TxOut,
};
Expand All @@ -363,8 +365,7 @@ mod tests {
},
treepp::script,
};
use secp256k1::SECP256K1;
use secp256k1::{rand, SecretKey};
use secp256k1::rand;
use std::env;
use std::str::FromStr;
use std::thread;
Expand Down Expand Up @@ -435,8 +436,8 @@ mod tests {
let actor = Actor::new(sk, None, network);

assert_eq!(sk, actor._secret_key);
assert_eq!(sk.public_key(SECP256K1), actor.public_key);
assert_eq!(sk.x_only_public_key(SECP256K1).0, actor.xonly_public_key);
assert_eq!(sk.public_key(&SECP), actor.public_key);
assert_eq!(sk.x_only_public_key(&SECP).0, actor.xonly_public_key);
}

#[test]
Expand Down Expand Up @@ -555,7 +556,7 @@ mod tests {
let actor = Actor::new(
config.secret_key,
Some(
secp256k1::SecretKey::from_str(
SecretKey::from_str(
"451F451F451F451F451F451F451F451F451F451F451F451F451F451F451F451F",
)
.unwrap(),
Expand All @@ -577,7 +578,7 @@ mod tests {
let actor = Actor::new(
config.secret_key,
Some(
secp256k1::SecretKey::from_str(
SecretKey::from_str(
"451F451F451F451F451F451F451F451F451F451F451F451F451F451F451F451F",
)
.unwrap(),
Expand Down
Loading
Loading