Giter Club home page Giter Club logo

libsecp256k1's Introduction

SECP256K1 implementation in pure Rust

SECP256K1 implementation with no_std support. Currently we have implementation for:

  • Convert a private key to a public key.
  • Sign messages.
  • Signature verification.
  • Public key recovery from signed messages.
  • Shared secrets.

Feature flags

  • std: If disabled, works in no_std environment. Enabled by default.
  • hmac: Add certain features that requires the HMAC-DRBG. This includes signing. Enabled by default.
  • static-context: To speed up computation, the library uses a pre-computed table context for many ecmult operations. This feature flag puts the context directly as static variables. If disabled, the context must be created from heap manually. Increases binary size, enabled by default.
  • lazy-static-context: Instead of storing the pre-computed table context as static variables, store it as a variable that dynamically allocates the context in heap via lazy_static. It overwrites static-context. Impact bootstrap performance and only available in std, disabled by default.

Development workflow

Branch

This repository uses develop branch for development. Changes are periodically merged to master branch.

Pull request

All changes (except new releases) are handled through pull requests. Please open your PR against develop branch.

Versioning

libsecp256k1 follows Semantic Versioning. An unreleased crate in the repository will have the -dev suffix in the end, and we do rolling releases.

When you make a pull request against this repository, please also update the affected crates' versions, using the following rules. Note that the rules should be applied recursively -- if a change modifies any upper crate's dependency (even just the Cargo.toml file), then the upper crate will also need to apply those rules.

Additionally, if your change is notable, then you should also modify the corresponding CHANGELOG.md file, in the "Unreleased" section.

If the affected crate already has -dev suffix:

  • If your change is a patch, then you do not have to update any versions.
  • If your change introduces a new feature, please check if the local version already had its minor version bumped, if not, bump it.
  • If your change modifies the current interface, please check if the local version already had its major version bumped, if not, bump it.

If the affected crate does not yet have -dev suffix:

  • If your change is a patch, then bump the patch version, and add -dev suffix.
  • If your change introduces a new feature, then bump the minor version, and add -dev suffix.
  • If your change modifies the current interface, then bump the major version, and add -dev suffix.

If your pull request introduces a new crate, please set its version to 1.0.0-dev.

libsecp256k1's People

Contributors

0xdeafbeef avatar agemanning avatar andresilva avatar chiro-hiro avatar debris avatar denisp-gh-admin avatar dependabot[bot] avatar dvdplm avatar elichai avatar expenses avatar fubuloubu avatar gauthamastro avatar general-beck avatar kigawas avatar koushiro avatar nikvolf avatar romanb avatar sergejparity avatar sorpaas avatar steverusso avatar trevor-crypto avatar tripleight avatar tuxxy avatar vorot93 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

libsecp256k1's Issues

Implement useful traits

It would be nice if the major data types this crate makes available implemented certain common traits that many Rust programs leverage from the standard library or commonly used libraries, namely:

  • core::marker::Copy
  • core::fmt::Display
  • core::default::Default
  • core::cmp::{PartialOrd, Ord}
  • serde::{Deserialize, Serialize}

This would make it easier for end-users to leverage automatic derivation of these traits

What is the meaning of "group element not on the curve"

Sorry, if I misuse this for support.
I am trying to validate a signature, but I run into the error "InvalidPublicKey"
When I debug into the library, it comes from the function Affine::is_valid_var() which suggests the group element not on the curve.

Here is the code that I execute. The values are the same as when I run similar code in golang, where the signature verifies successfully.

``
extern crate libsecp256k1;
use libsecp256k1::{Message, PublicKey, PublicKeyFormat, Signature, verify};
extern crate x509_parser;
use x509_parser::pem::parse_x509_pem;
extern crate spki;
use spki::SubjectPublicKeyInfo;

fn main() {
pub const PUBKEY_TEST: &str = "-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEQem4RK/YPgiT83UqNnIAx7GAFX3b
948NbKnilBKoD5t/4LSKDmP/X7qCrEIQ8TafiXB2r3mW9BPlX1mGZ0iYXw==
-----END PUBLIC KEY-----";
let (rem, pem) = parse_x509_pem(&PUBKEY_TEST.as_bytes()).unwrap();
assert!(rem.is_empty());
assert_eq!(pem.label, String::from("PUBLIC"));
let pub_key = SubjectPublicKeyInfo::try_from(pem.contents.as_slice()).unwrap();
assert_eq!(pub_key.subject_public_key.len(), 65);
assert_eq!(pub_key.subject_public_key[0], 4); // tag pubkey full
let pubkey = PublicKey::parse_slice(&pub_key.subject_public_key, Some(PublicKeyFormat::Full)).unwrap();
}
``

Update Digest dependency

Digest is stuck on version 0.9 which is outdated and breaks compatibility with crates like sha3. I noticed that there is a branch that adds the newer version of digest.

Consider exposing field, group, and scalar types as public?

Hi, this library is really one-of-a-kind in that it's a pure rust implementation of secp256k1.

Is it at all possible that the authors would consider exposing the field, group, and scalar types as public types so that we can use them for arithmetic in other libraries to build and prototype with? We have a need for this on a library-in-progress from @nucypher and these exposed types would save a ton of work.

GitHub Actions build error

Error: Unable to process command '::set-env name=CC::clang' successfully.
Error: The `set-env` command is disabled. Please upgrade to using Environment Files or opt into unsecure command execution by setting the `ACTIONS_ALLOW_UNSECURE_COMMANDS` environment variable to `true`. For more information see: https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/
Error: Unable to process command '::set-env name=CXX::clang++' successfully.
Error: The `set-env` command is disabled. Please upgrade to using Environment Files or opt into unsecure command execution by setting the `ACTIONS_ALLOW_UNSECURE_COMMANDS` environment variable to `true`. For more information see: https://github.blog/changelog/2020-10-01-github-actions-deprecating-set-env-and-add-path-commands/

https://github.com/paritytech/libsecp256k1/pull/63/checks?check_run_id=1589875269

Implement parity codec traits

To make this library more compatible with Substrate, it would be helpful if the major data types this crate makes available implemented the parity codec Encode/Decode traits.

If message is curve order, the produced signature differs from C libsecp256k1

operation name: ECDSA_Sign
ecc curve: secp256k1
private key: 56312477249014209074628570412053507700651251817507875221581725004376025072551
input: {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 
 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b, 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41} (32 bytes)
nonce source: RFC 6979
digest: NULL

Module rust_libsecp256k1 result:

X: 47388130725345365543943056156955089862855904171373701656697778116764682363258
Y: 37092251669891195025340922069241978179057338816763561493770821876984336293314
R: 6375717680451201706338283387674951504853972890504340254901358912364890170048
S: 38089468653229875417331679605347400350541399507585865787553720087037855685678


Module secp256k1 result:

X: 47388130725345365543943056156955089862855904171373701656697778116764682363258
Y: 37092251669891195025340922069241978179057338816763561493770821876984336293314
R: 33254199737740308679695132562303764730039452340150568623617514127015066954758
S: 6671420881794714356399876285623712604606322001251819062355155017162344624447

Similar bug: trezor/trezor-firmware#1374
Found with Cryptofuzz.

It can't build to wasm

When I use this dependency in my wasm project, it will report error when compiling:

error: the wasm32-unknown-unknown target is not supported by default, you may need to enable the "js" feature. For more information see: https://docs.rs/getrandom/#webassembly-support
   --> /Users/xxx/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.7/src/lib.rs:235:9
    |
235 | /         compile_error!("the wasm32-unknown-unknown target is not supported by \
236 | |                         default, you may need to enable the \"js\" feature. \
237 | |                         For more information see: \
238 | |                         https://docs.rs/getrandom/#webassembly-support");
    | |________________________________________________________________________^

error[E0433]: failed to resolve: use of undeclared crate or module `imp`
   --> /Users/xxx/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.7/src/lib.rs:262:5
    |
262 |     imp::getrandom_inner(dest)
    |     ^^^ use of undeclared crate or module `imp`

error: failed to run custom build command for `gmp-mpfr-sys v1.4.10`

Caused by:
  process didn't exit successfully: `/Users/xxx/workspace/github-aewc/infinity-swap/evm-canister/target/release/build/gmp-mpfr-sys-d8a467da99dcac09/build-script-build` (exit status: 101)
  --- stderr
  thread 'main' panicked at 'Cross compilation from aarch64-apple-darwin to wasm32-unknown-unknown not supported! Use the `force-cross` feature to cross compile anyway.', /Users/xxx/.cargo/registry/src/github.com-1ecc6299db9ec823/gmp-mpfr-sys-1.4.10/build.rs:111:9
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
For more information about this error, try `rustc --explain E0433`.
error: could not compile `getrandom` due to 2 previous errors

is there any features that without using the getrandom?

Consider renaming the crate

The crate has exact same name as a very popular C library. This could cause confusion, possibly even security implications.
I suggest renaming to rsecp256k1 but any other non-conflicting name should be fine.

Jacobian point comparison doesn't seem to work

I've noticed that comparing Jacobian points doesn't seem to work very well. I discovered this after trying to compare jacobian_basepoint_g == scalar_one * jacobian_basepoint_g.

The only way to get the comparison to succeed is to convert them to Affine via Affine::from_gej and then perform the comparison. It seems the library needs a proper Jacobian point comparison function implemented.

About the function that converts a "lax DER"-encoded byte slice to a signature.

Hello, Wei Tang

I am trying to switch to this library from secp256k1, but this library don't have the function that converts a "lax DER" -encoded byte slice to a signature (in secp256k1, the function called Signature::from_der_lax(data: &[u8])).

Whether this function 'Signature::parse_der(p: &[u8])' in this library is a substitute for from_der_lax. If not, would you consider adding a "lax DER"-encoded related function?

Similarly, the "Signature::normalize_s(&mut self)" in secp256k1.

Not able to compile code with solana & exceed computation units error while calling verify function

hello,

  1. i have used libsecp256k1 in my solana program and compiled code for same.

code has been successfully compiled but with one error as stack exceeded max limit:
error message: Error: Function _ZN17libsecp256k1_core6ecmult13ECMultContext6ecmult17h70a883a093cf3492E Stack offset of -4592 exceeded max offset of -4096 by 496 bytes, please minimize large stack variables ( detailed shown in below snap )

image

  1. after successfully deploying a code with one compilation error i'm calling verify function from libsecp256k1 but is giving me an error as Program eY1fuXT4ibaEXKwxg2nCxRfhgT8whRpmDGCHRrdYGcE consumed 200000 of 200000 compute units, even-though i have left with 156539 units out of 200000 which is max limit for computation in solana.

image

i have raised same issue with solana dev support as well, and after suggesting some solutions at last he has mentioned that they can't increase computation limit you might have to contact library for same.

can you please look into this issue?

Constant time audit

  • Unless explicitly documented, all functions should be by default constant time. This includes traits operations.
  • Variable time operations should use wrapper types.

Constant macros are unusable because the field module is private.

The jacobian_const and affine_const macros are exported, but are presently unusable due to the inability to also make constant Field elements via field_const (as it's not exported).

Unfortunately, a Field cannot be instantiated because the fields n, magnitude, and normalized are only public to the crate.

Support zeroization / clearing of secrets from memory

The Anoma protocol is supporting the use of secp256k1 keys in namada and is implementing the zeroization / clearing of secret key data from memory upon drop.

We currently have a branch in our fork of this repo to provide this support. In general, our branch rolls back some changes in #56 and adds some extra functionality that we use for tests.

Our branch implements a minimal set of changes in order to accomplish our zeroization goals, notably removing the Copy trait from relevant structs. We also note that it seems that the Scalar::clear method is useless when Copy is derived for Scalar.

Ideally, we would like full zeroization support in this upstream repo, but at minimum we would like to at least have a branch with this support here.

Lack of documentation for SharedSecret generation.

I cannot figure out how to provide a type that implements Digest for creating a SharedSecret and there is no examples or documentation which show how to get a shared secret from a secret key and a public key (from a different key-pair). It would help if an example was provided as to how to generate a shared secret using ECDH with this library.

no-std support broken in 0.5.0

This issue has the same title as #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.

Add a way to parse and generate compressed public keys

Hello :)

I'm trying to switch to this library from secp256k1 so that I don't need to depend on the system library. Looking at the docs though I can't see any way to convert a public key to/from it's compressed 33-byte form. It would be nice if there were methods on PublicKey for doing this. Has the ground-work already been done that would make adding these methods possible?

The Compilation In Mac M1 is Failing because some missing config after updating rustler to 0.24 0r 0.25

==> ex_secp256k1
Compiling 1 file (.ex)
Compiling crate exsecp256k1 in release mode (native/exsecp256k1)

== Compilation error in file lib/ex_secp256k1.ex ==
** (RuntimeError) Compiling on macOS requires special link args in order to compile
correctly.

To remove this error, please create .cargo/config
with the following content:

   [target.'cfg(target_os = "macos")']
   rustflags = [
       "-C", "link-arg=-undefined",
       "-C", "link-arg=dynamic_lookup",
   ]

See https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/MachOTopics/1-Articles/executing_files.html
for more details.

(rustler 0.25.0) lib/rustler/compiler.ex:100: Rustler.Compiler.ensure_platform_requirements!/3
(rustler 0.25.0) lib/rustler/compiler.ex:25: Rustler.Compiler.compile_crate/2

Errors happened while compiling into wasm file with no_std environment.

Hi there,

I'm just using this crate to comile a wasm file with no_std environment, but I got errors like this.

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: `#[panic_handler]` function required, but not found

error: `#[alloc_error_handler]` function required, but not found

error: aborting due to 3 previous errors

error: could not compile `libsecp256k1`.

Is this expected?

Reproduce:

  1. Stable rust-1.40(and I tried on 1.42-nightly, that's the same error).

  2. And just a small change in Cargo.toml file.

crate-type =["cdylib"]

Make new release on crates.io

The version on github has several changes I've been using via git dependency. It would be nice to have them on crates.io.

Changelog out of sync

Dear maintainers,

Thank you for all the work you have put into this crate.

I noticed that the changelog is a bit behind the latest published version on crates.io. Would it be possible to update the CHANGELOG.md to make it clearer what changed between 0.5.0 and 0.7.0?

no-std support broken

I think that 3aa638b made a change which causes std to be pulled in even if default-features = false.

To reproduce:

First, create a no-std project with #[panic_handler] and #[alloc_error_handler] functions. Include as a dependency

libsecp256k1 = { git = "https://github.com/paritytech/libsecp256k1", rev="df8aaf1ba395e989546da001374a08145cdce0ca", default-features = false }

and make use of it. This compiles fine.

Now try with the dependency using the next commit:

libsecp256k1 = { git = "https://github.com/paritytech/libsecp256k1", rev="3aa638bdee7fff9c11ce5f22d8cd19fa163861ac", default-features = false }

and compile again. The following errors appear

error[E0152]: found duplicate lang item `panic_impl`
  --> src/lib.rs:49:5
   |
49 |     pub unsafe fn on_panic(_info: &::core::panic::PanicInfo) -> ! {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: the lang item is first defined in crate `std` (which `libsecp256k1` depends on)
   = note: first definition in `std` loaded from /home/birchmd/.rustup/toolchains/nightly-2021-03-25-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/libstd-c154c491353fefeb.rlib
   = note: second definition in the local crate (`aurora_engine`)

error[E0152]: found duplicate lang item `oom`
  --> src/lib.rs:56:5
   |
56 |     pub unsafe fn on_alloc_error(_: core::alloc::Layout) -> ! {
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: the lang item is first defined in crate `std` (which `libsecp256k1` depends on)
   = note: first definition in `std` loaded from /home/birchmd/.rustup/toolchains/nightly-2021-03-25-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-unknown-unknown/lib/libstd-c154c491353fefeb.rlib
   = note: second definition in the local crate (`aurora_engine`)

error: aborting due to 2 previous errors

Note the error says that libsecp256k1 depends on std even though default-features = false.

Please support Scalar comparision

We are using libsecp256k1 to implement ECVRF, it's a little bit struggle to compare Scalar types. Could you guys consider it as a part of this crate?.

Maintaining a CHANGLOG file

This library is very useful. Thank you for the hard work!

Can you please keep CHANGELOG file up to date so the changes between versions can be tracked

rustc error due to arrayref dependency

Rustup seems to now throw a compile time error when trying to compile a crate with libsecp256k1 as a dependency.

This causes stable builds with libsecp256k1 to fail.

Description

During compilation, the error:

use of unstable library feature 'core_intrinsics': intrinsics are unlikely to ever be stabilized, instead they should be used through stabilized interfaces in the rest of the standard library
  --> /home/mikn/.cargo/registry/src/github.com-1ecc6299db9ec823/arrayref-0.3.6/src/lib.rs:35:5
   |
35 | use core::intrinsics::prefetch_write_data;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

occurs, which seems to originate from the arrayref transitive dependency (through libsecp256k1 0.7.0).

Reproduction

Compile a package with the libsecp256k1 dependency using cargo.

Setup

WSL2 toolchain:

stable-x86_64-unknown-linux-gnu (default)
rustc 1.56.1 (59eed8a2a 2021-11-01)

scalar multiplication is broken in --release

Running cargo test --release --no-fail-fast gives the following test failures on stable and nightly. Quick poking around seems to show it's to do with the pregen multiplication tables.

cargo test --release --no-fail-fast
    Finished release [optimized] target(s) in 0.03s
     Running target/release/deps/libsecp256k1-a03a00350fa6ef12

running 1 test
test tests::secret_key_inverse_is_sane ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/release/deps/serde-82b9567b029f1baa

running 2 tests
test test_deserialize_public_key ... FAILED
test test_serialize_public_key ... FAILED

failures:

---- test_deserialize_public_key stdout ----
thread 'test_deserialize_public_key' panicked at 'assertion failed: `(left == right)`
  left: `PublicKey(Affine { x: Field { n: [31262607, 33389173, 27261377, 13730080, 30875160, 44990809, 30666074, 26280549, 22444818, 450865], magnitude: 1, normalized: true }, y: Field { n: [27846865, 11869850, 60679442, 8560600, 48226177, 11627216, 65890916, 22041631, 59725963, 1847211], magnitude: 1, normalized: true }, infinity: false })`,
 right: `PublicKey(Affine { x: Field { n: [192306, 40206006, 26019251, 46272818, 38900612, 40079564, 65689187, 56690991, 19109204, 449584], magnitude: 1, normalized: false }, y: Field { n: [60304222, 10438730, 64516166, 22810055, 41842044, 56876599, 3603739, 59093824, 54969783, 1583710], magnitude: 1, normalized: false }, infinity: false })`', tests/serde.rs:25:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- test_serialize_public_key stdout ----
thread 'test_serialize_public_key' panicked at 'assertion failed: `(left == right)`
  left: `"\"BBtwwSOVVNhCS/6lZjmOQzJRk4SwhEyY0Fs5lfrYAu8yYKl7RsW34WzQA2/RvY943n51fFcDcf2HBGJ9ISuYK14=\""`,
 right: `"\"BBuExVZ7EmRAmV0+1aq6BWXXHhg0YEgZ/5wX9enV3QePcL6vj1iLVBUH/tamQsWrQt/fgSCn9jneUSLUemmo6NE=\""`', tests/serde.rs:19:5


failures:
    test_deserialize_public_key
    test_serialize_public_key

test result: FAILED. 0 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out

     Running target/release/deps/verify-656a8ccd0e033494

running 14 tests
test secret_clear_on_drop ... ok
test signatures::test_failing_sign_verify ... FAILED
test signatures::test_shared_secret ... ok
test signatures::test_signature_der ... ok
test test_convert_key1 ... FAILED
test test_convert_anykey ... FAILED
test test_convert_key2 ... FAILED
test test_pubkey_combine ... ok
test test_pubkey_equality ... FAILED
test test_signature_der_lax ... ok
test signatures::test_sign_verify ... FAILED
test test_recover ... ok
test test_low_s ... ok
test test_verify ... ok

failures:

---- signatures::test_failing_sign_verify stdout ----
thread 'signatures::test_failing_sign_verify' panicked at 'called `Result::unwrap()` on an `Err` value: InvalidSignature', tests/verify.rs:132:32
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- test_convert_key1 stdout ----
thread 'test_convert_key1' panicked at 'assertion failed: `(left == right)`
  left: `[4, 121, 190, 102, 126, 249, 220, 187, 172, 85, 160, 98, 149, 206, 135, 11, 7, 2, 155, 252, 219, 45, 206, 40, 217, 89, 242, 129, 91, 22, 248, 23, 152, 72, 58, 218, 119, 38, 163, 196, 101, 93, 164, 251, 252, 14, 17, 8, 168, 253, 23, 180, 72, 166, 133, 84, 25, 156, 71, 208, 143, 251, 16, 212, 184]`,
 right: `[4, 247, 78, 178, 188, 178, 252, 7, 0, 45, 237, 206, 219, 62, 162, 224, 247, 156, 230, 50, 56, 117, 134, 58, 77, 135, 57, 135, 56, 215, 179, 178, 118, 25, 149, 163, 182, 54, 140, 107, 98, 227, 81, 35, 247, 237, 110, 250, 179, 211, 231, 122, 171, 78, 176, 234, 102, 193, 169, 145, 203, 218, 49, 191, 6]`', tests/verify.rs:363:5

---- test_convert_anykey stdout ----
thread 'test_convert_anykey' panicked at 'assertion failed: `(left == right)`
  left: `[4, 240, 212, 31, 66, 132, 106, 200, 239, 174, 127, 161, 125, 177, 55, 135, 174, 237, 98, 6, 152, 49, 108, 41, 66, 9, 69, 190, 245, 179, 132, 207, 204, 155, 65, 37, 235, 101, 107, 31, 62, 31, 36, 234, 222, 234, 129, 53, 45, 86, 108, 94, 6, 20, 179, 144, 222, 125, 163, 201, 246, 80, 98, 209, 239]`,
 right: `[4, 43, 222, 83, 204, 118, 72, 73, 41, 110, 192, 182, 127, 227, 127, 0, 157, 17, 179, 123, 169, 31, 194, 50, 8, 234, 202, 177, 103, 187, 85, 165, 250, 193, 139, 250, 255, 109, 86, 70, 225, 78, 200, 173, 52, 66, 17, 167, 106, 7, 250, 109, 21, 90, 159, 111, 242, 139, 61, 247, 221, 102, 214, 85, 147]`', tests/verify.rs:421:5

---- test_convert_key2 stdout ----
thread 'test_convert_key2' panicked at 'assertion failed: `(left == right)`
  left: `[4, 63, 168, 192, 140, 101, 168, 63, 107, 78, 163, 224, 78, 28, 199, 12, 190, 60, 211, 145, 73, 158, 62, 5, 171, 125, 237, 242, 138, 255, 154, 252, 83, 130, 0, 255, 147, 227, 242, 178, 203, 80, 41, 240, 60, 126, 190, 232, 32, 214, 58, 76, 90, 149, 65, 200, 58, 206, 190, 41, 63, 84, 202, 207, 14]`,
 right: `[4, 239, 255, 184, 252, 226, 2, 236, 211, 29, 20, 159, 80, 118, 26, 152, 70, 36, 88, 80, 220, 222, 192, 103, 25, 188, 137, 120, 229, 65, 3, 85, 183, 56, 109, 246, 185, 131, 189, 190, 111, 146, 221, 194, 5, 58, 123, 133, 112, 161, 54, 46, 200, 103, 81, 67, 165, 159, 233, 230, 224, 195, 227, 82, 197]`', tests/verify.rs:384:5

---- test_pubkey_equality stdout ----
thread 'test_pubkey_equality' panicked at 'called `Result::unwrap()` on an `Err` value: InvalidPublicKey', tests/verify.rs:458:23

---- signatures::test_sign_verify stdout ----
thread 'signatures::test_sign_verify' panicked at 'assertion failed: verify(&message, &sig, &pubkey)', tests/verify.rs:87:9


failures:
    signatures::test_failing_sign_verify
    signatures::test_sign_verify
    test_convert_anykey
    test_convert_key1
    test_convert_key2
    test_pubkey_equality

test result: FAILED. 8 passed; 6 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests libsecp256k1

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

error: test failed.

I tested on:

rustc --version --verbose
rustc 1.44.0 (49cae5576 2020-06-01)
binary: rustc
commit-hash: 49cae55760da0a43428eba73abcb659bb70cf2e4
commit-date: 2020-06-01
host: x86_64-unknown-linux-gnu
release: 1.44.0
LLVM version: 9.0

Unable to build - Compile error

Compiling libsecp256k1 v0.6.0
error: could not compile libsecp256k1

Caused by:
process didn't exit successfully: rustc --crate-name libsecp256k1 --edition=2018 /home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-0.6.0/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 --cfg 'feature="default"' --cfg 'feature="hmac"' --cfg 'feature="hmac-drbg"' --cfg 'feature="sha2"' --cfg 'feature="static-context"' --cfg 'feature="std"' --cfg 'feature="typenum"' -C metadata=ff75ffe663998c68 -C extra-filename=-ff75ffe663998c68 --out-dir /home/ubuntu/solana/target/debug/deps -L dependency=/home/ubuntu/solana/target/debug/deps --extern arrayref=/home/ubuntu/solana/target/debug/deps/libarrayref-1170f8c828ab8d28.rmeta --extern base64=/home/ubuntu/solana/target/debug/deps/libbase64-3a8d5a57fd8274f7.rmeta --extern digest=/home/ubuntu/solana/target/debug/deps/libdigest-7c04696e4c42676b.rmeta --extern hmac_drbg=/home/ubuntu/solana/target/debug/deps/libhmac_drbg-4c12dff52eca0361.rmeta --extern libsecp256k1_core=/home/ubuntu/solana/target/debug/deps/liblibsecp256k1_core-5f3c6c6031e9b3bd.rmeta --extern rand=/home/ubuntu/solana/target/debug/deps/librand-df7d32eff7424b90.rmeta --extern serde=/home/ubuntu/solana/target/debug/deps/libserde-d9f9ddfe27b2d698.rmeta --extern sha2=/home/ubuntu/solana/target/debug/deps/libsha2-6bbaa12590c3611b.rmeta --extern typenum=/home/ubuntu/solana/target/debug/deps/libtypenum-17020373090ddbe4.rmeta --cap-lints allow (signal: 9, SIGKILL: kill)
warning: build failed, waiting for other jobs to finish...
{"message":"environment variable OUT_DIR not defined","code":null,"level":"error","spans":[{"file_name":"/home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-0.6.0/src/lib.rs","byte_start":1716,"byte_end":1731,"line_start":56,"line_end":56,"column_start":59,"column_end":74,"is_primary":true,"text":[{"text":" unsafe { ECMultContext::new_from_raw(include!(concat!(env!("OUT_DIR"), "/const.rs"))) };","highlight_start":59,"highlight_end":74}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":{"span":{"file_name":"/home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-0.6.0/src/lib.rs","byte_start":1716,"byte_end":1731,"line_start":56,"line_end":56,"column_start":59,"column_end":74,"is_primary":false,"text":[{"text":" unsafe { ECMultContext::new_from_raw(include!(concat!(env!("OUT_DIR"), "/const.rs"))) };","highlight_start":59,"highlight_end":74}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null},"macro_decl_name":"env!","def_site_span":{"file_name":"/rustc/c8dfcfe046a7680554bf4eb612bad840e7631c4b/library/core/src/macros/mod.rs","byte_start":30584,"byte_end":30747,"line_start":887,"line_end":890,"column_start":5,"column_end":6,"is_primary":false,"text":[],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}}}],"children":[],"rendered":"\u001b[0m\u001b[1m\u001b[38;5;9merror\u001b[0m\u001b[0m\u001b[1m: environment variable OUT_DIR not defined\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0m/home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-0.6.0/src/lib.rs:56:59\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m56\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| ...\u001b[0m\u001b[0m_from_raw(include!(concat!(env!("OUT_DIR"), "/const.rs"))) };\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9m^^^^^^^^^^^^^^^\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m= \u001b[0m\u001b[0m\u001b[1mnote\u001b[0m\u001b[0m: this error originates in the macro env (in Nightly builds, run with -Z macro-backtrace for more info)\u001b[0m\n\n"}
{"message":"environment variable OUT_DIR not defined","code":null,"level":"error","spans":[{"file_name":"/home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-0.6.0/src/lib.rs","byte_start":2029,"byte_end":2044,"line_start":62,"line_end":62,"column_start":62,"column_end":77,"is_primary":true,"text":[{"text":" unsafe { ECMultGenContext::new_from_raw(include!(concat!(env!("OUT_DIR"), "/const_gen.rs"))) };","highlight_start":62,"highlight_end":77}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":{"span":{"file_name":"/home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-0.6.0/src/lib.rs","byte_start":2029,"byte_end":2044,"line_start":62,"line_end":62,"column_start":62,"column_end":77,"is_primary":false,"text":[{"text":" unsafe { ECMultGenContext::new_from_raw(include!(concat!(env!("OUT_DIR"), "/const_gen.rs"))) };","highlight_start":62,"highlight_end":77}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null},"macro_decl_name":"env!","def_site_span":{"file_name":"/rustc/c8dfcfe046a7680554bf4eb612bad840e7631c4b/library/core/src/macros/mod.rs","byte_start":30584,"byte_end":30747,"line_start":887,"line_end":890,"column_start":5,"column_end":6,"is_primary":false,"text":[],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}}}],"children":[],"rendered":"\u001b[0m\u001b[1m\u001b[38;5;9merror\u001b[0m\u001b[0m\u001b[1m: environment variable OUT_DIR not defined\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0m/home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/libsecp256k1-0.6.0/src/lib.rs:62:62\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m62\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| ...\u001b[0m\u001b[0m_from_raw(include!(concat!(env!("OUT_DIR"), "/const_gen.rs"))) };\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;9m^^^^^^^^^^^^^^^\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m= \u001b[0m\u001b[0m\u001b[1mnote\u001b[0m\u001b[0m: this error originates in the macro env (in Nightly builds, run with -Z macro-backtrace for more info)\u001b[0m\n\n"}
{"artifact":"/home/ubuntu/solana/target/debug/deps/libsecp256k1-ff75ffe663998c68.d","emit":"dep-info"}
{"message":"aborting due to 2 previous errors","code":null,"level":"error","spans":[],"children":[],"rendered":"\u001b[0m\u001b[1m\u001b[38;5;9merror\u001b[0m\u001b[0m\u001b[1m: aborting due to 2 previous errors\u001b[0m\n\n"}

Please push release tags

The v0.3.1 release has a nice signed tag in the github repo, but no other releases do.

When auditing dependency updates, it's helpful if the upstream repo has clear tags for each published release, so simple git diff and git log can be used to review changes. It would be great if you could push tags for at least the recent releases to make this possible.

In the meantime, here are some corresponding commits according to the crates.io package metadata

version commit id
0.1.15 aba3d3f
0.2.2 22e9297
0.3.1 c05c4e0
0.3.5 d0eedf2
0.5.0 98d7025
0.6.0 e5095a8
0.7.0 34293f5

Related to #91 and #104.

Bincode Serialization Broken For PublicKey

Hi friends ๐Ÿ‘‹ When trying to serialize and deserialize a PublicKey using bincode like this:

        bincode::deserialize::<libsecp256k1::PublicKey>(
            &bincode::serialize(&libsecp256k1::PublicKey::from_secret_key(
                &libsecp256k1::SecretKey::random(&mut rand::rngs::OsRng),
            ))
            .unwrap(),
        )
        .unwrap();

I get the following runtime error:

thread 'test::repro' panicked at 'called `Result::unwrap()` on an `Err` value: Custom("invalid type: byte array, expected a bytestring of either 33 (compressed), 64 (raw), or 65 bytes in length")'

I am using the following versions of things:

bincode = "1.3.3"
rand = "0.8.4"
libsecp256k1 = "0.7.0"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.