Skip to content

Commit be3bcae

Browse files
authored
Merge pull request #5910 from hugocaillard/refactor/add-libsecp259k1-for-wasm
refactor: use libsecp256k1 in wasm
2 parents 0d016a9 + 51169a6 commit be3bcae

File tree

5 files changed

+466
-21
lines changed

5 files changed

+466
-21
lines changed

Cargo.lock

Lines changed: 96 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stacks-common/Cargo.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ winapi = { version = "0.3", features = ["fileapi", "processenv", "winnt"] }
4646
version = "1.0"
4747
features = ["arbitrary_precision", "unbounded_depth"]
4848

49-
[dependencies.secp256k1]
50-
version = "0.24.3"
51-
features = ["serde", "recovery"]
52-
5349
[dependencies.ed25519-dalek]
5450
workspace = true
5551

@@ -61,6 +57,12 @@ features = ["serde"]
6157
version = "0.2.23"
6258
features = ["std"]
6359

60+
[target.'cfg(not(target_family = "wasm"))'.dependencies]
61+
secp256k1 = { version = "0.24.3", features = ["serde", "recovery"] }
62+
63+
[target.'cfg(target_family = "wasm")'.dependencies]
64+
libsecp256k1 = { version = "0.7.0" }
65+
6466
[dev-dependencies]
6567
rand_core = { workspace = true }
6668

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#[cfg(not(target_family = "wasm"))]
2+
mod native;
3+
4+
#[cfg(not(target_family = "wasm"))]
5+
pub use self::native::*;
6+
7+
#[cfg(target_family = "wasm")]
8+
mod wasm;
9+
10+
#[cfg(target_family = "wasm")]
11+
pub use self::wasm::*;
12+
13+
pub const MESSAGE_SIGNATURE_ENCODED_SIZE: u32 = 65;
14+
15+
pub struct MessageSignature(pub [u8; 65]);
16+
impl_array_newtype!(MessageSignature, u8, 65);
17+
impl_array_hexstring_fmt!(MessageSignature);
18+
impl_byte_array_newtype!(MessageSignature, u8, 65);
19+
impl_byte_array_serde!(MessageSignature);
20+
21+
pub struct SchnorrSignature(pub [u8; 65]);
22+
impl_array_newtype!(SchnorrSignature, u8, 65);
23+
impl_array_hexstring_fmt!(SchnorrSignature);
24+
impl_byte_array_newtype!(SchnorrSignature, u8, 65);
25+
impl_byte_array_serde!(SchnorrSignature);
26+
pub const SCHNORR_SIGNATURE_ENCODED_SIZE: u32 = 65;
27+
28+
impl Default for SchnorrSignature {
29+
/// Creates a default Schnorr Signature. Note this is not a valid signature.
30+
fn default() -> Self {
31+
Self([0u8; 65])
32+
}
33+
}

stacks-common/src/util/secp256k1.rs renamed to stacks-common/src/util/secp256k1/native.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,24 @@
1313
//
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16-
use rand::RngCore;
17-
use secp256k1;
18-
use secp256k1::ecdsa::{
16+
17+
use ::secp256k1;
18+
use ::secp256k1::ecdsa::{
1919
RecoverableSignature as LibSecp256k1RecoverableSignature, RecoveryId as LibSecp256k1RecoveryID,
2020
Signature as LibSecp256k1Signature,
2121
};
22-
use secp256k1::{
22+
pub use ::secp256k1::Error;
23+
use ::secp256k1::{
2324
constants as LibSecp256k1Constants, Error as LibSecp256k1Error, Message as LibSecp256k1Message,
2425
PublicKey as LibSecp256k1PublicKey, Secp256k1, SecretKey as LibSecp256k1PrivateKey,
2526
};
27+
use rand::RngCore;
2628
use serde::de::{Deserialize, Error as de_Error};
2729
use serde::Serialize;
2830

29-
use super::hash::Sha256Sum;
31+
use super::MessageSignature;
3032
use crate::types::{PrivateKey, PublicKey};
31-
use crate::util::hash::{hex_bytes, to_hex};
33+
use crate::util::hash::{hex_bytes, to_hex, Sha256Sum};
3234

3335
// per-thread Secp256k1 context
3436
thread_local!(static _secp256k1: Secp256k1<secp256k1::All> = Secp256k1::new());
@@ -55,13 +57,6 @@ pub struct Secp256k1PrivateKey {
5557
compress_public: bool,
5658
}
5759

58-
pub struct MessageSignature(pub [u8; 65]);
59-
impl_array_newtype!(MessageSignature, u8, 65);
60-
impl_array_hexstring_fmt!(MessageSignature);
61-
impl_byte_array_newtype!(MessageSignature, u8, 65);
62-
impl_byte_array_serde!(MessageSignature);
63-
pub const MESSAGE_SIGNATURE_ENCODED_SIZE: u32 = 65;
64-
6560
impl MessageSignature {
6661
pub fn empty() -> MessageSignature {
6762
// NOTE: this cannot be a valid signature

0 commit comments

Comments
 (0)