citahub / libsm

A Rust Library of China's Standards of Encryption Algorithms (SM2/3/4)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Invalid private key length, sometimes

yejiayu opened this issue · comments

First. I created a private key and return hex.

let ctx = SigCtx::new();
let (pk, sk) = ctx.new_keypair();
format!("0x{}", hex::encode(sk.to_bytes_be()))
// 0x11a8571fe8ee79990db44379aac8d67606a783ac73f52113d563dce7f38fdf

Next, the data is signed with the private key

let ctx = SigCtx::new();
let privk = hex::decode("0x11a8571fe8ee79990db44379aac8d67606a783ac73f52113d563dce7f38fdf")
let sk = ctx.load_seckey(&privk).unwrap();
// this painic. result returns Err(true)

The length of a valid private key that doesn't include 0x should be 64, it's only 62.

version: git commit 5cc6f4d
rust: rustc 1.31.0-nightly (8c4ad4e9e 2018-10-04)

sk in pub fn new_keypair(&self) -> (Point, BigUint) is a BigUint.
BigUint in num-bigint is a dynamic length struct.

If you want to use sk directly, you should pad the bytes with leading zeros by yourself.
In the foreseeable future, no one will change this.

Or, just RTFM!