RustCrypto / elliptic-curves

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

k256 does not compile without allocator #1057

Closed StackOverflowExcept1on closed 3 months ago

StackOverflowExcept1on commented 3 months ago
error: no global memory allocator found but one is required; link to std or add `#[global_allocator]` to a static item that implements the GlobalAlloc trait

error: could not compile `app` (bin "app") due to 1 previous error
[package]
name = "app"
version = "0.1.0"
edition = "2021"

[dependencies]
k256 = { version = "0.14.0-pre.0", default-features = false, features = ["arithmetic"] }
libc = { version = "0.2.155", default-features = false }
rand_core = { version = "0.6.4", default-features = false, features = ["getrandom"] }

[profile.dev]
panic = "abort"

[profile.release]
opt-level = "z"
lto = true
codegen-units = 1
panic = "abort"
strip = true
#![no_std]
#![no_main]

extern crate libc;

use k256::SecretKey;
use rand_core::OsRng;

#[no_mangle]
extern "C" fn main() -> i32 {
    if f().to_bytes()[0] == 0xff {
        1
    } else {
        0
    }
}

#[no_mangle]
#[inline(never)]
pub fn f() -> SecretKey {
    SecretKey::random(&mut OsRng)
}

#[panic_handler]
fn panic_handler(_: &core::panic::PanicInfo) -> ! {
    loop {}
}
tarcieri commented 3 months ago

You didn't mention what target you're using and are pulling in superfluous dependencies. Can you pull in just k256 and its transitive dependencies? (e.g. no rand_core)

Which crate are you speculating is actually linking liballoc?

tarcieri commented 3 months ago

I reproduced this without the superfluous dependencies. Wonder if it's related to precomputation /cc @fjarri

StackOverflowExcept1on commented 3 months ago

You didn't mention what target you're using

rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7
StackOverflowExcept1on commented 3 months ago

This is might be related to zeroize feature "alloc"

├── hybrid-array feature "default"
│   └── hybrid-array v0.2.0-rc.8
│       ├── typenum feature "const-generics"
│       │   └── typenum v1.17.0
│       ├── typenum feature "default"
│       │   └── typenum v1.17.0
│       └── zeroize feature "default"
│           ├── zeroize v1.8.1
│           └── zeroize feature "alloc"
│               └── zeroize v1.8.1
tarcieri commented 3 months ago

Aah, good catch

Edit: opened https://github.com/RustCrypto/hybrid-array/pull/76