簡體   English   中英

如何在 Rust 中獲取 EC 點的 X 和 Y 坐標?

[英]How to get X and Y coordinates of an EC Point in Rust?

我正在嘗試從 Rust 中的 EcKeyopenssl::pkey::Private 密鑰中提取 X 和 Y 坐標。 我設法將其轉換為點和字節,但我不知道如何獲取坐標。

pub fn exportpubkey(key: &EcKey<openssl::pkey::Private>) -> (){
    let group: EcGroup = EcGroup::from_curve_name(Nid::SECP256K1).unwrap();
    let mut ctx: BigNumContext = openssl::bn::BigNumContext::new().unwrap(); 
    let bytes = key.public_key().to_bytes(&group,openssl::ec::PointConversionForm::COMPRESSED, &mut ctx).unwrap();
    println!("{}", display_bytes(&bytes));
}

您可能想查看affine_coordinates方法

use openssl::{bn::*, ec::*, nid::Nid};

pub fn print_pub_key(key: &EcKey<openssl::pkey::Private>) -> () {
    let group: EcGroup = EcGroup::from_curve_name(Nid::SECP256K1).unwrap();
    let mut ctx: BigNumContext = BigNumContext::new().unwrap();
    let public_key = key.public_key();
    let mut x = BigNum::new().unwrap();
    let mut y = BigNum::new().unwrap();
    public_key
        .affine_coordinates_gfp(group.as_ref(), &mut x, &mut y, &mut ctx)
        .expect("extract coords");

    println!("{}, {}", x, y);
}

fn main() {
    let group: EcGroup = EcGroup::from_curve_name(Nid::SECP256K1).unwrap();
    let key = EcKey::generate(&group).unwrap();
    print_pub_key(&key);
}

請注意, affine_coordinates_gfpaffine_coordinates_gf2m似乎在您選擇的組中返回相同的坐標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM