Skip to content

Commit a9598ed

Browse files
Merge rust-bitcoin/rust-secp256k1#327: Re-arrange functionality to make ECDSA and Schnorr equal-ish citizens
d244b4d747c60822a14f07e37166c8e3f45e1cd0 Fix typo in docs (Thomas Eizinger) c5c95513f23bfbc7db29ce9baa6068117ddc301b Move helper function below usage (Thomas Eizinger) ce4427747d0b688681d0aa281cd0e2716231848c Move ECDSA functionality into ECDSA module (Thomas Eizinger) e0c3bb28c4fafb1758ccecd56ea8a7b9d9887bd4 Rename schnorr functions on `Secp256k1` to match naming of ecdsa (Thomas Eizinger) 760559c70ebea79e9140729f023760ba0c7b3ded Rename `schnorrsig` module to `schnorr` (Thomas Eizinger) d4fb819d80a3eb6dfabedf85dbb57348adf67f31 Move `XOnlyPublicKey` to `key` module (Thomas Eizinger) 87d936a765cb0b67d795fa4f84a900f7bece2a89 Rename `schnorr::PublicKey` to `schnorr::XOnlyPublicKey` (Thomas Eizinger) 2e0e731664daf956f29b55ea3511f774d7b78a82 Move `KeyPair` to `key` module (Thomas Eizinger) c47ead9967326bfaf3dc7a15bcf0b902001a1405 Move `Signature` and `SerializedSignature` to new `ecdsa` module (Thomas Eizinger) 49c7e214868fe72a3a2eaf0f17e74f95302c81e6 Prefer `use super::*` import over manually picking items (Thomas Eizinger) 52d0554423bc9f3bb95c32770c9b74d90e6087e5 Fully qualify Error to simplify imports (Thomas Eizinger) 8e96abae39281e0cccd60ac6e8cd1a0b913f9334 Make `key` module private (Thomas Eizinger) Pull request description: This patch-set tries to re-structure the library a bit. What we currently have seems to have been mostly driven by historical growth. For example, with the addition of Schnorr signatures, just exposing `secp256k1::Signature` is ambiguous. This PR only contains renames and moving around of code. I've tried to structure the patches in such a way that makes this reasonably easy to review. Feedback welcome! ACKs for top commit: sanket1729: ACK d244b4d747c60822a14f07e37166c8e3f45e1cd0 apoelstra: ACK d244b4d747c60822a14f07e37166c8e3f45e1cd0 Tree-SHA512: d40af5c56ffa500305e40eb5dbe72f2f6d6193b3a190910018d3bacdec2820ab6a59f15d47d11e0fee7ef4de6efd46d316636cd502aad5db4f314dedfff726f9
2 parents 3367b0a + 961fcd9 commit a9598ed

File tree

10 files changed

+1164
-1038
lines changed

10 files changed

+1164
-1038
lines changed

examples/sign_verify.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ extern crate bitcoin_hashes;
22
extern crate secp256k1;
33

44
use bitcoin_hashes::{sha256, Hash};
5-
use secp256k1::{Error, Message, PublicKey, Secp256k1, SecretKey, Signature, Signing, Verification};
5+
use secp256k1::{Error, Message, PublicKey, Secp256k1, SecretKey, ecdsa, Signing, Verification};
66

77
fn verify<C: Verification>(secp: &Secp256k1<C>, msg: &[u8], sig: [u8; 64], pubkey: [u8; 33]) -> Result<bool, Error> {
88
let msg = sha256::Hash::hash(msg);
99
let msg = Message::from_slice(&msg)?;
10-
let sig = Signature::from_compact(&sig)?;
10+
let sig = ecdsa::Signature::from_compact(&sig)?;
1111
let pubkey = PublicKey::from_slice(&pubkey)?;
1212

13-
Ok(secp.verify(&msg, &sig, &pubkey).is_ok())
13+
Ok(secp.verify_ecdsa(&msg, &sig, &pubkey).is_ok())
1414
}
1515

16-
fn sign<C: Signing>(secp: &Secp256k1<C>, msg: &[u8], seckey: [u8; 32]) -> Result<Signature, Error> {
16+
fn sign<C: Signing>(secp: &Secp256k1<C>, msg: &[u8], seckey: [u8; 32]) -> Result<ecdsa::Signature, Error> {
1717
let msg = sha256::Hash::hash(msg);
1818
let msg = Message::from_slice(&msg)?;
1919
let seckey = SecretKey::from_slice(&seckey)?;
20-
Ok(secp.sign(&msg, &seckey))
20+
Ok(secp.sign_ecdsa(&msg, &seckey))
2121
}
2222

2323
fn main() {

examples/sign_verify_recovery.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@ extern crate bitcoin_hashes;
33
extern crate secp256k1;
44

55
use bitcoin_hashes::{sha256, Hash};
6-
use secp256k1::recovery::{RecoverableSignature, RecoveryId};
7-
use secp256k1::{Error, Message, PublicKey, Secp256k1, SecretKey, Signing, Verification};
6+
use secp256k1::{Error, Message, PublicKey, Secp256k1, SecretKey, Signing, Verification, ecdsa};
87

98
fn recover<C: Verification>(secp: &Secp256k1<C>,msg: &[u8],sig: [u8; 64],recovery_id: u8) -> Result<PublicKey, Error> {
109
let msg = sha256::Hash::hash(msg);
1110
let msg = Message::from_slice(&msg)?;
12-
let id = RecoveryId::from_i32(recovery_id as i32)?;
13-
let sig = RecoverableSignature::from_compact(&sig, id)?;
11+
let id = ecdsa::RecoveryId::from_i32(recovery_id as i32)?;
12+
let sig = ecdsa::RecoverableSignature::from_compact(&sig, id)?;
1413

15-
secp.recover(&msg, &sig)
14+
secp.recover_ecdsa(&msg, &sig)
1615
}
1716

18-
fn sign_recovery<C: Signing>(secp: &Secp256k1<C>, msg: &[u8], seckey: [u8; 32]) -> Result<RecoverableSignature, Error> {
17+
fn sign_recovery<C: Signing>(secp: &Secp256k1<C>, msg: &[u8], seckey: [u8; 32]) -> Result<ecdsa::RecoverableSignature, Error> {
1918
let msg = sha256::Hash::hash(msg);
2019
let msg = Message::from_slice(&msg)?;
2120
let seckey = SecretKey::from_slice(&seckey)?;
22-
Ok(secp.sign_recoverable(&msg, &seckey))
21+
Ok(secp.sign_ecdsa_recoverable(&msg, &seckey))
2322
}
2423

2524
fn main() {

no_std_test/src/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,22 @@ fn start(_argc: isize, _argv: *const *const u8) -> isize {
106106
let public_key = PublicKey::from_secret_key(&secp, &secret_key);
107107
let message = Message::from_slice(&[0xab; 32]).expect("32 bytes");
108108

109-
let sig = secp.sign(&message, &secret_key);
110-
assert!(secp.verify(&message, &sig, &public_key).is_ok());
109+
let sig = secp.sign_ecdsa(&message, &secret_key);
110+
assert!(secp.verify_ecdsa(&message, &sig, &public_key).is_ok());
111111

112-
let rec_sig = secp.sign_recoverable(&message, &secret_key);
113-
assert!(secp.verify(&message, &rec_sig.to_standard(), &public_key).is_ok());
114-
assert_eq!(public_key, secp.recover(&message, &rec_sig).unwrap());
112+
let rec_sig = secp.sign_ecdsa_recoverable(&message, &secret_key);
113+
assert!(secp.verify_ecdsa(&message, &rec_sig.to_standard(), &public_key).is_ok());
114+
assert_eq!(public_key, secp.recover_ecdsa(&message, &rec_sig).unwrap());
115115
let (rec_id, data) = rec_sig.serialize_compact();
116-
let new_rec_sig = recovery::RecoverableSignature::from_compact(&data, rec_id).unwrap();
116+
let new_rec_sig = ecdsa::RecoverableSignature::from_compact(&data, rec_id).unwrap();
117117
assert_eq!(rec_sig, new_rec_sig);
118118

119119
let mut cbor_ser = [0u8; 100];
120120
let writer = SliceWrite::new(&mut cbor_ser[..]);
121121
let mut ser = Serializer::new(writer);
122122
sig.serialize(&mut ser).unwrap();
123123
let size = ser.into_inner().bytes_written();
124-
let new_sig: Signature = de::from_mut_slice(&mut cbor_ser[..size]).unwrap();
124+
let new_sig: ecdsa::Signature = de::from_mut_slice(&mut cbor_ser[..size]).unwrap();
125125
assert_eq!(sig, new_sig);
126126

127127
let _ = SharedSecret::new(&public_key, &secret_key);

src/ecdh.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ impl SharedSecret {
169169

170170
#[cfg(test)]
171171
mod tests {
172+
use super::*;
172173
use rand::thread_rng;
173-
use super::SharedSecret;
174174
use super::super::Secp256k1;
175175

176176
#[cfg(target_arch = "wasm32")]
@@ -224,7 +224,7 @@ mod tests {
224224
let x = [5u8; 32];
225225
let y = [7u8; 32];
226226
let mut output = [0u8; 64];
227-
let res = unsafe { super::c_callback(output.as_mut_ptr(), x.as_ptr(), y.as_ptr(), ::ptr::null_mut()) };
227+
let res = unsafe { super::c_callback(output.as_mut_ptr(), x.as_ptr(), y.as_ptr(), ptr::null_mut()) };
228228
assert_eq!(res, 1);
229229
let mut new_x = [0u8; 32];
230230
let mut new_y = [0u8; 32];

0 commit comments

Comments
 (0)