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

refactor: use libsecp256k1 in wasm #5910

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
100 changes: 96 additions & 4 deletions Cargo.lock

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

10 changes: 6 additions & 4 deletions stacks-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ winapi = { version = "0.3", features = ["fileapi", "processenv", "winnt"] }
version = "1.0"
features = ["arbitrary_precision", "unbounded_depth"]

[dependencies.secp256k1]
version = "0.24.3"
features = ["serde", "recovery"]

[dependencies.ed25519-dalek]
workspace = true

Expand All @@ -61,6 +57,12 @@ features = ["serde"]
version = "0.2.23"
features = ["std"]

[target.'cfg(not(target_family = "wasm"))'.dependencies]
secp256k1 = { version = "0.24.3", features = ["serde", "recovery"] }

[target.'cfg(target_family = "wasm")'.dependencies]
libsecp256k1 = { version = "0.7.0" }

[dev-dependencies]
rand_core = { workspace = true }

Expand Down
33 changes: 33 additions & 0 deletions stacks-common/src/util/secp256k1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#[cfg(not(target_family = "wasm"))]
mod native;

#[cfg(not(target_family = "wasm"))]
pub use self::native::*;

#[cfg(target_family = "wasm")]
mod wasm;

#[cfg(target_family = "wasm")]
pub use self::wasm::*;

pub const MESSAGE_SIGNATURE_ENCODED_SIZE: u32 = 65;

pub struct MessageSignature(pub [u8; 65]);
impl_array_newtype!(MessageSignature, u8, 65);
impl_array_hexstring_fmt!(MessageSignature);
impl_byte_array_newtype!(MessageSignature, u8, 65);
impl_byte_array_serde!(MessageSignature);

pub struct SchnorrSignature(pub [u8; 65]);
impl_array_newtype!(SchnorrSignature, u8, 65);
impl_array_hexstring_fmt!(SchnorrSignature);
impl_byte_array_newtype!(SchnorrSignature, u8, 65);
impl_byte_array_serde!(SchnorrSignature);
pub const SCHNORR_SIGNATURE_ENCODED_SIZE: u32 = 65;

impl Default for SchnorrSignature {
/// Creates a default Schnorr Signature. Note this is not a valid signature.
fn default() -> Self {
Self([0u8; 65])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use rand::RngCore;
use secp256k1;
use secp256k1::ecdsa::{

use ::secp256k1;
use ::secp256k1::ecdsa::{
RecoverableSignature as LibSecp256k1RecoverableSignature, RecoveryId as LibSecp256k1RecoveryID,
Signature as LibSecp256k1Signature,
};
use secp256k1::{
pub use ::secp256k1::Error;
use ::secp256k1::{
constants as LibSecp256k1Constants, Error as LibSecp256k1Error, Message as LibSecp256k1Message,
PublicKey as LibSecp256k1PublicKey, Secp256k1, SecretKey as LibSecp256k1PrivateKey,
};
use rand::RngCore;
use serde::de::{Deserialize, Error as de_Error};
use serde::Serialize;

use super::hash::Sha256Sum;
use super::MessageSignature;
use crate::types::{PrivateKey, PublicKey};
use crate::util::hash::{hex_bytes, to_hex};
use crate::util::hash::{hex_bytes, to_hex, Sha256Sum};

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

pub struct MessageSignature(pub [u8; 65]);
impl_array_newtype!(MessageSignature, u8, 65);
impl_array_hexstring_fmt!(MessageSignature);
impl_byte_array_newtype!(MessageSignature, u8, 65);
impl_byte_array_serde!(MessageSignature);
pub const MESSAGE_SIGNATURE_ENCODED_SIZE: u32 = 65;

impl MessageSignature {
pub fn empty() -> MessageSignature {
// NOTE: this cannot be a valid signature
Expand Down
Loading
Loading