【Rust】p256でhexにencodeしたpublic keyをdecodeしてverify

hex::decode(&public_key_hex) でdecodeして、さらにfrom_sec1_bytesでpublic keyにします。

    let public_key = secret_key.public_key();
    let public_key_serialized = public_key.to_string();
    println!("Public Key: \n{}", public_key_serialized);

    let public_key_hex = hex::encode(&public_key.to_sec1_bytes());
    println!("hex Public Key: \n{}", public_key_hex);

    let public_key_decode = hex::decode(&public_key_hex).unwrap();
    println!("decode Public Key: \n{:?}", &public_key_decode);

    let public_key = p256::PublicKey::from_sec1_bytes(&public_key_decode).expect("import error");

    println!("decode Public Key: \n{:?}", &public_key);

    let signing_key: SigningKey = secret_key.into();
    let message = b"ECDSA proves knowledge of a secret number in the context of a single message";
    let signature: Signature = signing_key.sign(message);

    let verifying_key: VerifyingKey = public_key.into();
    assert!(verifying_key.verify(message, &signature).is_ok());

$ cargo run
// 省略
hex Public Key:
040b807b5e5fee8c91df858e09aa6dc085eae1b9b70783ce32ec68ad4994f9d16a991e277a1515a22e35d2d7935dec95946f31a2e3ea1e125a9067a177523063b0
decode Public Key:
[4, 11, 128, 123, 94, 95, 238, 140, 145, 223, 133, 142, 9, 170, 109, 192, 133, 234, 225, 185, 183, 7, 131, 206, 50, 236, 104, 173, 73, 148, 249, 209, 106, 153, 30, 39, 122, 21, 21, 162, 46, 53, 210, 215, 147, 93, 236, 149, 148, 111, 49, 162, 227, 234, 30, 18, 90, 144, 103, 161, 119, 82, 48, 99, 176]
decode Public Key:
PublicKey { point: AffinePoint { x: FieldElement(0x3B226D0B94EFABC8980328AB7FE8AE347D418871A770F1EFFE8A24FCAC9EE9DB), y: FieldElement(0x307BB7BAC4B807E9D59F65BEC20EC8AB54591BE07549140238927D01CA9152EF), infinity: 0 } }

おおお、このdecodeと署名の確認は本当に上手くいくのか再度トランザクションを実際に作ってテストする必要がありますね。