Merge pull request #208 from TheBlueMatt/202-04-wasm

Re-support WASM via simple stub headers
This commit is contained in:
Andrew Poelstra 2020-04-29 19:48:03 +00:00 committed by GitHub
commit 0782872fb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 132 additions and 23 deletions

View File

@ -1,19 +1,24 @@
language: rust language: rust
# cache: cache:
# directories: directories:
# - cargo_web - wasm
rust: rust:
- stable - stable
- beta - beta
- nightly - nightly
- 1.22.0 - 1.22.0
distro: bionic
os: os:
- linux - linux
- windows - windows
addons: addons:
chrome: stable chrome: stable
apt:
packages:
- clang-9
- nodejs
matrix: matrix:
exclude: exclude:
@ -46,8 +51,10 @@ script:
cd no_std_test && cd no_std_test &&
cargo run --release | grep -q "Verified Successfully"; cargo run --release | grep -q "Verified Successfully";
fi fi
- #if [ ${TRAVIS_RUST_VERSION} == "stable" -a "$TRAVIS_OS_NAME" = "linux" ]; then - if [ ${TRAVIS_RUST_VERSION} == "stable" -a "$TRAVIS_OS_NAME" = "linux" ]; then
#CARGO_TARGET_DIR=cargo_web cargo install --verbose --force cargo-web && clang --version &&
#cargo web build --verbose --target=asmjs-unknown-emscripten && CARGO_TARGET_DIR=wasm cargo install --verbose --force wasm-pack &&
#cargo web test --verbose --target=asmjs-unknown-emscripten; sed -i 's/\[lib\]/[lib]\ncrate-type = ["cdylib", "rlib"]/' Cargo.toml &&
#fi CC=clang-9 wasm-pack build &&
CC=clang-9 wasm-pack test --node;
fi

View File

@ -46,6 +46,10 @@ rand_core = "0.4"
serde_test = "1.0" serde_test = "1.0"
bitcoin_hashes = "0.7" bitcoin_hashes = "0.7"
[target.wasm32-unknown-unknown.dev-dependencies]
wasm-bindgen-test = "0.3"
rand = { version = "0.6", features = ["wasm-bindgen"] }
[dependencies.rand] [dependencies.rand]
version = "0.6" version = "0.6"
optional = true optional = true

View File

@ -84,6 +84,10 @@ fn main() {
.define("USE_SCALAR_8X32", Some("1")); .define("USE_SCALAR_8X32", Some("1"));
} }
if env::var("TARGET").unwrap() == "wasm32-unknown-unknown" {
base_config.include("wasm-sysroot");
}
// secp256k1 // secp256k1
base_config.file("depend/secp256k1/contrib/lax_der_parsing.c") base_config.file("depend/secp256k1/contrib/lax_der_parsing.c")
.file("depend/secp256k1/src/secp256k1.c") .file("depend/secp256k1/src/secp256k1.c")

View File

@ -152,7 +152,7 @@ extern "C" {
// Contexts // Contexts
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_context_preallocated_size")] #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_context_preallocated_size")]
pub fn secp256k1_context_preallocated_size(flags: c_uint) -> usize; pub fn secp256k1_context_preallocated_size(flags: c_uint) -> size_t;
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_context_preallocated_create")] #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_context_preallocated_create")]
pub fn secp256k1_context_preallocated_create(prealloc: *mut c_void, flags: c_uint) -> *mut Context; pub fn secp256k1_context_preallocated_create(prealloc: *mut c_void, flags: c_uint) -> *mut Context;
@ -161,7 +161,7 @@ extern "C" {
pub fn secp256k1_context_preallocated_destroy(cx: *mut Context); pub fn secp256k1_context_preallocated_destroy(cx: *mut Context);
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_context_preallocated_clone_size")] #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_context_preallocated_clone_size")]
pub fn secp256k1_context_preallocated_clone_size(cx: *const Context) -> usize; pub fn secp256k1_context_preallocated_clone_size(cx: *const Context) -> size_t;
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_context_preallocated_clone")] #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_context_preallocated_clone")]
pub fn secp256k1_context_preallocated_clone(cx: *const Context, prealloc: *mut c_void) -> *mut Context; pub fn secp256k1_context_preallocated_clone(cx: *const Context, prealloc: *mut c_void) -> *mut Context;
@ -174,19 +174,19 @@ extern "C" {
// Pubkeys // Pubkeys
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ec_pubkey_parse")] #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ec_pubkey_parse")]
pub fn secp256k1_ec_pubkey_parse(cx: *const Context, pk: *mut PublicKey, pub fn secp256k1_ec_pubkey_parse(cx: *const Context, pk: *mut PublicKey,
input: *const c_uchar, in_len: usize) input: *const c_uchar, in_len: size_t)
-> c_int; -> c_int;
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ec_pubkey_serialize")] #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ec_pubkey_serialize")]
pub fn secp256k1_ec_pubkey_serialize(cx: *const Context, output: *mut c_uchar, pub fn secp256k1_ec_pubkey_serialize(cx: *const Context, output: *mut c_uchar,
out_len: *mut usize, pk: *const PublicKey, out_len: *mut size_t, pk: *const PublicKey,
compressed: c_uint) compressed: c_uint)
-> c_int; -> c_int;
// Signatures // Signatures
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ecdsa_signature_parse_der")] #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ecdsa_signature_parse_der")]
pub fn secp256k1_ecdsa_signature_parse_der(cx: *const Context, sig: *mut Signature, pub fn secp256k1_ecdsa_signature_parse_der(cx: *const Context, sig: *mut Signature,
input: *const c_uchar, in_len: usize) input: *const c_uchar, in_len: size_t)
-> c_int; -> c_int;
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ecdsa_signature_parse_compact")] #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ecdsa_signature_parse_compact")]
@ -196,12 +196,12 @@ extern "C" {
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ecdsa_signature_parse_der_lax")] #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ecdsa_signature_parse_der_lax")]
pub fn ecdsa_signature_parse_der_lax(cx: *const Context, sig: *mut Signature, pub fn ecdsa_signature_parse_der_lax(cx: *const Context, sig: *mut Signature,
input: *const c_uchar, in_len: usize) input: *const c_uchar, in_len: size_t)
-> c_int; -> c_int;
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ecdsa_signature_serialize_der")] #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ecdsa_signature_serialize_der")]
pub fn secp256k1_ecdsa_signature_serialize_der(cx: *const Context, output: *mut c_uchar, pub fn secp256k1_ecdsa_signature_serialize_der(cx: *const Context, output: *mut c_uchar,
out_len: *mut usize, sig: *const Signature) out_len: *mut size_t, sig: *const Signature)
-> c_int; -> c_int;
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ecdsa_signature_serialize_compact")] #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ecdsa_signature_serialize_compact")]
@ -462,12 +462,12 @@ mod fuzz_dummy {
} }
/// Return dummy size of context struct. /// Return dummy size of context struct.
pub unsafe fn secp256k1_context_preallocated_size(_flags: c_uint) -> usize { pub unsafe fn secp256k1_context_preallocated_size(_flags: c_uint) -> size_t {
mem::size_of::<Context>() mem::size_of::<Context>()
} }
/// Return dummy size of context struct. /// Return dummy size of context struct.
pub unsafe fn secp256k1_context_preallocated_clone_size(_cx: *mut Context) -> usize { pub unsafe fn secp256k1_context_preallocated_clone_size(_cx: *mut Context) -> size_t {
mem::size_of::<Context>() mem::size_of::<Context>()
} }
@ -494,7 +494,7 @@ mod fuzz_dummy {
// Pubkeys // Pubkeys
/// Parse 33/65 byte pubkey into PublicKey, losing compressed information /// Parse 33/65 byte pubkey into PublicKey, losing compressed information
pub unsafe fn secp256k1_ec_pubkey_parse(cx: *const Context, pk: *mut PublicKey, pub unsafe fn secp256k1_ec_pubkey_parse(cx: *const Context, pk: *mut PublicKey,
input: *const c_uchar, in_len: usize) input: *const c_uchar, in_len: size_t)
-> c_int { -> c_int {
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0); assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
match in_len { match in_len {
@ -521,7 +521,7 @@ mod fuzz_dummy {
/// Serialize PublicKey back to 33/65 byte pubkey /// Serialize PublicKey back to 33/65 byte pubkey
pub unsafe fn secp256k1_ec_pubkey_serialize(cx: *const Context, output: *mut c_uchar, pub unsafe fn secp256k1_ec_pubkey_serialize(cx: *const Context, output: *mut c_uchar,
out_len: *mut usize, pk: *const PublicKey, out_len: *mut size_t, pk: *const PublicKey,
compressed: c_uint) compressed: c_uint)
-> c_int { -> c_int {
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0); assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
@ -546,7 +546,7 @@ mod fuzz_dummy {
// Signatures // Signatures
pub unsafe fn secp256k1_ecdsa_signature_parse_der(_cx: *const Context, _sig: *mut Signature, pub unsafe fn secp256k1_ecdsa_signature_parse_der(_cx: *const Context, _sig: *mut Signature,
_input: *const c_uchar, _in_len: usize) _input: *const c_uchar, _in_len: size_t)
-> c_int { -> c_int {
unimplemented!(); unimplemented!();
} }
@ -562,14 +562,14 @@ mod fuzz_dummy {
} }
pub unsafe fn ecdsa_signature_parse_der_lax(_cx: *const Context, _sig: *mut Signature, pub unsafe fn ecdsa_signature_parse_der_lax(_cx: *const Context, _sig: *mut Signature,
_input: *const c_uchar, _in_len: usize) _input: *const c_uchar, _in_len: size_t)
-> c_int { -> c_int {
unimplemented!(); unimplemented!();
} }
/// Copies up to 72 bytes into output from sig /// Copies up to 72 bytes into output from sig
pub unsafe fn secp256k1_ecdsa_signature_serialize_der(cx: *const Context, output: *mut c_uchar, pub unsafe fn secp256k1_ecdsa_signature_serialize_der(cx: *const Context, output: *mut c_uchar,
out_len: *mut usize, sig: *const Signature) out_len: *mut size_t, sig: *const Signature)
-> c_int { -> c_int {
assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0); assert!(!cx.is_null() && (*cx).0 as u32 & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);

View File

@ -4,6 +4,7 @@ use core::fmt;
pub type c_int = i32; pub type c_int = i32;
pub type c_uchar = u8; pub type c_uchar = u8;
pub type c_uint = u32; pub type c_uint = u32;
pub type size_t = usize;
/// This might not match C's `c_char` exactly. /// This might not match C's `c_char` exactly.
/// The way we use it makes it fine either way but this type shouldn't be used outside of the library. /// The way we use it makes it fine either way but this type shouldn't be used outside of the library.
@ -39,3 +40,42 @@ mod tests {
assert_eq!(TypeId::of::<types::c_char>(), TypeId::of::<raw::c_char>()); assert_eq!(TypeId::of::<types::c_char>(), TypeId::of::<raw::c_char>());
} }
} }
#[doc(hidden)]
#[cfg(target_arch = "wasm32")]
pub fn sanity_checks_for_wasm() {
use std::mem::{size_of, align_of};
extern "C" {
pub static WASM32_INT_SIZE: c_uchar;
pub static WASM32_INT_ALIGN: c_uchar;
pub static WASM32_UNSIGNED_INT_SIZE: c_uchar;
pub static WASM32_UNSIGNED_INT_ALIGN: c_uchar;
pub static WASM32_SIZE_T_SIZE: c_uchar;
pub static WASM32_SIZE_T_ALIGN: c_uchar;
pub static WASM32_UNSIGNED_CHAR_SIZE: c_uchar;
pub static WASM32_UNSIGNED_CHAR_ALIGN: c_uchar;
pub static WASM32_PTR_SIZE: c_uchar;
pub static WASM32_PTR_ALIGN: c_uchar;
}
unsafe {
assert_eq!(size_of::<c_int>(), WASM32_INT_SIZE as usize);
assert_eq!(align_of::<c_int>(), WASM32_INT_ALIGN as usize);
assert_eq!(size_of::<c_uint>(), WASM32_UNSIGNED_INT_SIZE as usize);
assert_eq!(align_of::<c_uint>(), WASM32_UNSIGNED_INT_ALIGN as usize);
assert_eq!(size_of::<size_t>(), WASM32_SIZE_T_SIZE as usize);
assert_eq!(align_of::<size_t>(), WASM32_SIZE_T_ALIGN as usize);
assert_eq!(size_of::<c_uchar>(), WASM32_UNSIGNED_CHAR_SIZE as usize);
assert_eq!(align_of::<c_uchar>(), WASM32_UNSIGNED_CHAR_ALIGN as usize);
assert_eq!(size_of::<*const ()>(), WASM32_PTR_SIZE as usize);
assert_eq!(align_of::<*const ()>(), WASM32_PTR_ALIGN as usize);
}
}

View File

@ -0,0 +1,17 @@
#include <stddef.h>
#define alignof(type) offsetof (struct { char c; type member; }, member)
extern const unsigned char WASM32_INT_SIZE = sizeof(int);
extern const unsigned char WASM32_INT_ALIGN = alignof(int);
extern const unsigned char WASM32_UNSIGNED_INT_SIZE = sizeof(unsigned int);
extern const unsigned char WASM32_UNSIGNED_INT_ALIGN = alignof(unsigned int);
extern const unsigned char WASM32_SIZE_T_SIZE = sizeof(size_t);
extern const unsigned char WASM32_SIZE_T_ALIGN = alignof(size_t);
extern const unsigned char WASM32_UNSIGNED_CHAR_SIZE = sizeof(unsigned char);
extern const unsigned char WASM32_UNSIGNED_CHAR_ALIGN = alignof(unsigned char);
extern const unsigned char WASM32_PTR_SIZE = sizeof(void*);
extern const unsigned char WASM32_PTR_ALIGN = alignof(void*);

View File

View File

@ -0,0 +1,4 @@
#include <stddef.h>
void *memset(void *s, int c, size_t n);
void *memcpy(void *dest, const void *src, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);

View File

@ -106,6 +106,9 @@ mod std_only {
impl<C: Context> Secp256k1<C> { impl<C: Context> Secp256k1<C> {
/// Lets you create a context in a generic manner(sign/verify/all) /// Lets you create a context in a generic manner(sign/verify/all)
pub fn gen_new() -> Secp256k1<C> { pub fn gen_new() -> Secp256k1<C> {
#[cfg(target_arch = "wasm32")]
ffi::types::sanity_checks_for_wasm();
let buf = vec![0u8; Self::preallocate_size_gen()].into_boxed_slice(); let buf = vec![0u8; Self::preallocate_size_gen()].into_boxed_slice();
let ptr = Box::into_raw(buf); let ptr = Box::into_raw(buf);
Secp256k1 { Secp256k1 {
@ -192,6 +195,9 @@ unsafe impl<'buf> Context for AllPreallocated<'buf> {
impl<'buf, C: Context + 'buf> Secp256k1<C> { impl<'buf, C: Context + 'buf> Secp256k1<C> {
/// Lets you create a context with preallocated buffer in a generic manner(sign/verify/all) /// Lets you create a context with preallocated buffer in a generic manner(sign/verify/all)
pub fn preallocated_gen_new(buf: &'buf mut [u8]) -> Result<Secp256k1<C>, Error> { pub fn preallocated_gen_new(buf: &'buf mut [u8]) -> Result<Secp256k1<C>, Error> {
#[cfg(target_arch = "wasm32")]
ffi::types::sanity_checks_for_wasm();
if buf.len() < Self::preallocate_size_gen() { if buf.len() < Self::preallocate_size_gen() {
return Err(Error::NotEnoughMemory); return Err(Error::NotEnoughMemory);
} }

View File

@ -1060,6 +1060,33 @@ mod tests {
assert_tokens(&sig.compact(), &[Token::BorrowedBytes(&SIG_BYTES[..])]); assert_tokens(&sig.compact(), &[Token::BorrowedBytes(&SIG_BYTES[..])]);
assert_tokens(&sig.readable(), &[Token::BorrowedStr(SIG_STR)]); assert_tokens(&sig.readable(), &[Token::BorrowedStr(SIG_STR)]);
} }
// For WASM, just run through our general tests in this file all at once.
#[cfg(target_arch = "wasm32")]
extern crate wasm_bindgen_test;
#[cfg(target_arch = "wasm32")]
use self::wasm_bindgen_test::*;
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test]
fn stuff() {
test_manual_create_destroy();
test_raw_ctx();
// Note that, sadly, WASM doesn't currently properly unwind panics, so use of the library
// via unsafe primitives may cause abort() instead of catch-able panics.
/*assert!(std::panic::catch_unwind(|| {
test_panic_raw_ctx();
}).is_err());*/
test_preallocation();
capabilities();
signature_serialize_roundtrip();
signature_display();
signature_lax_der();
sign_and_verify();
sign_and_verify_extreme();
sign_and_verify_fail();
test_bad_slice();
test_low_s();
}
} }
#[cfg(all(test, feature = "unstable"))] #[cfg(all(test, feature = "unstable"))]