RustCrypto / elliptic-curves

Collection of pure Rust elliptic curve implementations: NIST P-224, P-256, P-384, P-521, secp256k1, SM2
669 stars 185 forks source link

k256 signal: 10, SIGBUS: access to undefined memory #886

Open satoshiotomakan opened 1 year ago

satoshiotomakan commented 1 year ago

When I try to call SigningKey::sign_prehash_recoverable in wasm32-unknown-emscripten, I get the following error:

10, SIGBUS: access to undefined memory

The problems seems to be with the precomputed-tables feature enabled by default.

How to test:

# Cargo.toml

[package]
name = "k256-test"
version = "0.1.0"
edition = "2021"

[dependencies]
hex = "0.4.3"
k256 = "0.13.0"
# The fix:
# k256 = { version = "0.13.0", features = ["ecdh", "ecdsa", "schnorr", "std"], default-features = false }
// lib.rs

use k256::ecdsa::SigningKey;

#[test]
fn test_ecdsa_sign() {
  let secret = hex::decode("afeefca74d9a325cf1d6b6911d61a65c32afa8e02bd5e78e2e4ac2910bab45f5").unwrap();
  let signing = SigningKey::from_slice(&secret);
  let hash = hex::decode("1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8").unwrap();

  let (_signature, _recovery_id) = signing.sign_prehash_recoverable(&hash).unwrap();
}

To run the test (emsdk should be installed already).

source path/to/emsdk/emsdk_env.sh
CARGO_TARGET_WASM32_UNKNOWN_EMSCRIPTEN_RUNNER=node cargo test --target wasm32-unknown-emscripten --release
tarcieri commented 1 year ago

cc @fjarri

If this is true, it seems like a bug in once_cell