paritytech / libsecp256k1

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

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

Closed ulrichard closed 1 year ago

ulrichard commented 2 years ago

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(); } ``

JoseMoranUrena523 commented 1 year ago

The reason you are getting that error is because it is not a secp256k1 key, it is a secp256r1 key. Try using this: extern crate libsecp256r1;
use libsecp256r1::{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();