paritytech / libsecp256k1

Pure Rust Implementation of secp256k1.
Apache License 2.0
175 stars 84 forks source link

no-std support broken in 0.5.0 #71

Closed tomaka closed 2 years ago

tomaka commented 3 years ago

This issue has the same title as https://github.com/paritytech/libsecp256k1/issues/68, but the problem isn't the same.

As of version 0.5.0/core-0.2.0, recently published on crates.io, compiling with default-feature = false leads to compilation errors in the core:

error[E0412]: cannot find type `Vec` in this scope
  --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:19:19
   |
19 |     let mut prej: Vec<Jacobian> = Vec::with_capacity(pre.len());
   |                   ^^^ not found in this scope
   |
help: consider importing one of these items
   |
1  | use alloc::vec::Vec;
   |
1  | use crate::ecmult::vec::Vec;
   |

error[E0433]: failed to resolve: use of undeclared type `Vec`
  --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:19:35
   |
19 |     let mut prej: Vec<Jacobian> = Vec::with_capacity(pre.len());
   |                                   ^^^ not found in this scope
   |
help: consider importing one of these items
   |
1  | use alloc::vec::Vec;
   |
1  | use crate::ecmult::vec::Vec;
   |

error[E0412]: cannot find type `Vec` in this scope
  --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:23:19
   |
23 |     let mut prea: Vec<Affine> = Vec::with_capacity(pre.len());
   |                   ^^^ not found in this scope
   |
help: consider importing one of these items
   |
1  | use alloc::vec::Vec;
   |
1  | use crate::ecmult::vec::Vec;
   |

error[E0433]: failed to resolve: use of undeclared type `Vec`
  --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:23:33
   |
23 |     let mut prea: Vec<Affine> = Vec::with_capacity(pre.len());
   |                                 ^^^ not found in this scope
   |
help: consider importing one of these items
   |
1  | use alloc::vec::Vec;
   |
1  | use crate::ecmult::vec::Vec;
   |

error[E0412]: cannot find type `Vec` in this scope
  --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:27:17
   |
27 |     let mut zr: Vec<Field> = Vec::with_capacity(pre.len());
   |                 ^^^ not found in this scope
   |
help: consider importing one of these items
   |
1  | use alloc::vec::Vec;
   |
1  | use crate::ecmult::vec::Vec;
   |

error[E0433]: failed to resolve: use of undeclared type `Vec`
  --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:27:30
   |
27 |     let mut zr: Vec<Field> = Vec::with_capacity(pre.len());
   |                              ^^^ not found in this scope
   |
help: consider importing one of these items
   |
1  | use alloc::vec::Vec;
   |
1  | use crate::ecmult::vec::Vec;
   |

error[E0412]: cannot find type `Box` in this scope
  --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:62:27
   |
62 |     pub fn new_boxed() -> Box<Self> {
   |                           ^^^ not found in this scope
   |
help: consider importing one of these items
   |
1  | use alloc::prelude::v1::Box;
   |
1  | use crate::ecmult::vec::Box;
   |

error[E0433]: failed to resolve: use of undeclared type `Box`
  --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:69:28
   |
69 |             let mut this = Box::from_raw(ptr);
   |                            ^^^ not found in this scope
   |
help: consider importing one of these items
   |
1  | use alloc::prelude::v1::Box;
   |
1  | use crate::ecmult::vec::Box;
   |

error[E0412]: cannot find type `Vec` in this scope
  --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:88:43
   |
88 | pub fn set_all_gej_var(a: &[Jacobian]) -> Vec<Affine> {
   |                                           ^^^ not found in this scope
   |
help: consider importing one of these items
   |
1  | use alloc::vec::Vec;
   |
1  | use crate::ecmult::vec::Vec;
   |

error[E0412]: cannot find type `Vec` in this scope
  --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:89:17
   |
89 |     let mut az: Vec<Field> = Vec::with_capacity(a.len());
   |                 ^^^ not found in this scope
   |
help: consider importing one of these items
   |
1  | use alloc::vec::Vec;
   |
1  | use crate::ecmult::vec::Vec;
   |

error[E0433]: failed to resolve: use of undeclared type `Vec`
  --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:89:30
   |
89 |     let mut az: Vec<Field> = Vec::with_capacity(a.len());
   |                              ^^^ not found in this scope
   |
help: consider importing one of these items
   |
1  | use alloc::vec::Vec;
   |
1  | use crate::ecmult::vec::Vec;
   |

error[E0412]: cannot find type `Vec` in this scope
  --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:95:14
   |
95 |     let azi: Vec<Field> = inv_all_var(&az);
   |              ^^^ not found in this scope
   |
help: consider importing one of these items
   |
1  | use alloc::vec::Vec;
   |
1  | use crate::ecmult::vec::Vec;
   |

error[E0412]: cannot find type `Vec` in this scope
   --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:114:41
    |
114 | pub fn inv_all_var(fields: &[Field]) -> Vec<Field> {
    |                                         ^^^ not found in this scope
    |
help: consider importing one of these items
    |
1   | use alloc::vec::Vec;
    |
1   | use crate::ecmult::vec::Vec;
    |

error[E0433]: failed to resolve: use of undeclared type `Vec`
   --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:116:16
    |
116 |         return Vec::new();
    |                ^^^ not found in this scope
    |
help: consider importing one of these items
    |
1   | use alloc::vec::Vec;
    |
1   | use crate::ecmult::vec::Vec;
    |

error[E0433]: failed to resolve: use of undeclared type `Vec`
   --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:119:19
    |
119 |     let mut ret = Vec::with_capacity(fields.len());
    |                   ^^^ not found in this scope
    |
help: consider importing one of these items
    |
1   | use alloc::vec::Vec;
    |
1   | use crate::ecmult::vec::Vec;
    |

error[E0412]: cannot find type `Box` in this scope
   --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:187:27
    |
187 |     pub fn new_boxed() -> Box<Self> {
    |                           ^^^ not found in this scope
    |
help: consider importing one of these items
    |
1   | use alloc::prelude::v1::Box;
    |
1   | use crate::ecmult::vec::Box;
    |

error[E0433]: failed to resolve: use of undeclared type `Box`
   --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:194:28
    |
194 |             let mut this = Box::from_raw(ptr);
    |                            ^^^ not found in this scope
    |
help: consider importing one of these items
    |
1   | use alloc::prelude::v1::Box;
    |
1   | use crate::ecmult::vec::Box;
    |

error[E0412]: cannot find type `Vec` in this scope
   --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:226:24
    |
226 |         let mut precj: Vec<Jacobian> = Vec::with_capacity(1024);
    |                        ^^^ not found in this scope
    |
help: consider importing one of these items
    |
1   | use alloc::vec::Vec;
    |
1   | use crate::ecmult::vec::Vec;
    |

error[E0433]: failed to resolve: use of undeclared type `Vec`
   --> /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-core-0.2.0/src/ecmult.rs:226:40
    |
226 |         let mut precj: Vec<Jacobian> = Vec::with_capacity(1024);
    |                                        ^^^ not found in this scope
    |
help: consider importing one of these items
    |
1   | use alloc::vec::Vec;
    |
1   | use crate::ecmult::vec::Vec;
    |

error: aborting due to 19 previous errors

Some errors have detailed explanations: E0412, E0433.
For more information about an error, try `rustc --explain E0412`.
error: could not compile `libsecp256k1-core`

To reproduce, just add libsecp256k1 = { version = "0.5.0", default-features = false } as a dependency to a project.

fredfortier commented 3 years ago

Same issue here. Here's what cargo nono says:

libsecp256k1: FAILURE
  - Did not find a #![no_std] attribute or a simple conditional attribute like #[cfg_attr(not(feature = "std"), no_std)] in the crate source. Crate most likely doesn't support no_std without changes.
log: FAILURE
tomusdrw commented 3 years ago

bump

trevor-crypto commented 3 years ago

Doing some investigation because I would like to move this forward as well.

I believe the issue came from the changes in https://github.com/paritytech/libsecp256k1/pull/41/ which make use of allocation (Vec and Box)

Would like to know what everyone else thinks. At first glance, it does not seem possible to feature-gate behind feature = "std" because the functions are used in the generators used in build.rs

Related: https://github.com/paritytech/libsecp256k1/issues/46

trevor-crypto commented 3 years ago

75 should resolve this issue, which is in 0.6.0

samkirton commented 3 years ago

I am experiencing this issue in 0.6.0 with:

libsecp256k1 = { version="0.6.0", default-features=false, features=["static-context"] }

std duplicate method exceptions are thrown noting:

the lang item is first defined in crate `std` (which `libsecp256k1_core` depends on)
trevor-crypto commented 3 years ago

@samkirton that's strange...I just tried again with static-context and could not reproduce this. Do you have another crate in your dependencies that is possibly enabling std for libsecp256k1_core?

samkirton commented 3 years ago

The only other dependencies I have are:

rand = { version="0.7.3", optional = true }
sha2 = { version="0.9.5", default-features=false }

[features]
signing = ["rand", "libsecp256k1/hmac"]

where rand is turned on only for the signing feature

trevor-crypto commented 3 years ago

@samkirton I still can't reproduce your exact error message above, but I can reproduce a similar one when I use rand as you have in your dependencies. You might need to turn off default-features for rand if you're using no_std when the signing feature is enabled.

samkirton commented 3 years ago

Signing requires hmac, which requires std, so rand is only ever used for std builds. I will try and disable default features on rand though just incase optional = true isn't working as I expected it to

trevor-crypto commented 3 years ago

Signing requires hmac, which requires std, so rand is only ever used for std builds.

Oh right, sorry.

fredfortier commented 3 years ago

@trevor-crypto I just tried to upgrade to 0.6.0 from 0.3.5 and getting std dependencies again. I guess it's expected based on your note above:

Signing requires hmac, which requires std, so rand is only ever used for std builds.

Does this mean that this crate no longer support signing with no_std? There must be a no_std version of hmac because 0.3.5 is depending on one.

fredfortier commented 3 years ago

I tried using the blank_with_context variants thinking that the static-context feature may have been the problem but it's not.

This dependency alone pulls in the std:

libsecp256k1 = { version = "0.6.0", default-features = false, features = [
    "hmac"
] }

It would be great (or rather necessary) to have no_std signing feature like in earlier versions. My only known option is to downgrade to 0.3.5 which I'll have to patch because some of its dependencies are no longer on crates_io.

trevor-crypto commented 3 years ago

@fredfortier I submitted a PR. If you'd like you can test it out on my branch or wait for it to come into develop

fredfortier commented 3 years ago

I'm confused about this issue as well because cargo nono check gives the dependency above (with only the hmac feature) a clear report, but the compiler gives an std error when I use it. I'll try your branch.

fredfortier commented 3 years ago

You did it @trevor-crypto . Your branch referenced above is now working for me when compiling to my no_std target.

sorpaas commented 2 years ago

This should be fixed now. Let me know if the error still exists.