mirror of
https://github.com/logos-blockchain/logos-execution-zone.git
synced 2026-08-10 23:13:18 +00:00
fix signatures to use k256
This commit is contained in:
parent
6fec57605f
commit
cfac69fffb
@ -19,7 +19,6 @@ rand.workspace = true
|
|||||||
borsh.workspace = true
|
borsh.workspace = true
|
||||||
hex.workspace = true
|
hex.workspace = true
|
||||||
k256.workspace = true
|
k256.workspace = true
|
||||||
secp256k1 = "0.31.1"
|
|
||||||
risc0-binfmt = "3.0.2"
|
risc0-binfmt = "3.0.2"
|
||||||
log.workspace = true
|
log.workspace = true
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use borsh::{BorshDeserialize, BorshSerialize};
|
use borsh::{BorshDeserialize, BorshSerialize};
|
||||||
use k256::ecdsa::signature::RandomizedSigner;
|
|
||||||
//use k256::schnorr::signature::Verifier;
|
|
||||||
pub use private_key::PrivateKey;
|
pub use private_key::PrivateKey;
|
||||||
pub use public_key::PublicKey;
|
pub use public_key::PublicKey;
|
||||||
use rand::{RngCore as _, rngs::OsRng};
|
use rand::{RngCore as _, rngs::OsRng};
|
||||||
@ -14,11 +14,27 @@ pub struct Signature {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Debug for Signature {
|
impl std::fmt::Debug for Signature {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
std::fmt::Display::fmt(self, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for Signature {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(f, "{}", hex::encode(self.value))
|
write!(f, "{}", hex::encode(self.value))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromStr for Signature {
|
||||||
|
type Err = hex::FromHexError;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
let mut bytes = [0_u8; 64];
|
||||||
|
hex::decode_to_slice(s, &mut bytes)?;
|
||||||
|
Ok(Self { value: bytes })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Signature {
|
impl Signature {
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn new(key: &PrivateKey, message: &[u8]) -> Self {
|
pub fn new(key: &PrivateKey, message: &[u8]) -> Self {
|
||||||
@ -27,86 +43,35 @@ impl Signature {
|
|||||||
Self::new_with_aux_random(key, message, aux_random)
|
Self::new_with_aux_random(key, message, aux_random)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn new_with_aux_random(
|
|
||||||
key: &PrivateKey,
|
|
||||||
message: &[u8],
|
|
||||||
mut aux_random: [u8; 32],
|
|
||||||
) -> Self {
|
|
||||||
let value = {
|
|
||||||
// Create signing key from raw bytes
|
|
||||||
let signing_key = k256::schnorr::SigningKey::from_bytes(key.value()).unwrap();
|
|
||||||
|
|
||||||
// k256 expects a 32-byte message digest for Schnorr (BIP-340)
|
|
||||||
let msg: &[u8; 32] = message.try_into().expect("message must be 32 bytes");
|
|
||||||
|
|
||||||
// Convert aux_random into the expected type
|
|
||||||
let aux: k256::elliptic_curve::FieldBytes = aux_random.into();
|
|
||||||
|
|
||||||
// Sign with auxiliary randomness
|
|
||||||
let signature: k256::schnorr::Signature = signing_key.sign_with_aux_rng(&aux, msg);
|
|
||||||
|
|
||||||
signature.to_bytes()
|
|
||||||
};
|
|
||||||
|
|
||||||
Self { value }
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
pub(crate) fn new_with_aux_random(
|
pub(crate) fn new_with_aux_random(
|
||||||
key: &PrivateKey,
|
key: &PrivateKey,
|
||||||
message: &[u8],
|
message: &[u8],
|
||||||
aux_random: [u8; 32],
|
aux_random: [u8; 32],
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let value = {
|
let value = {
|
||||||
let secp = secp256k1::Secp256k1::new();
|
let signing_key = k256::schnorr::SigningKey::from_bytes(key.value())
|
||||||
let secret_key = secp256k1::SecretKey::from_byte_array(*key.value()).unwrap();
|
.expect("Expect valid signing key");
|
||||||
let keypair = secp256k1::Keypair::from_secret_key(&secp, &secret_key);
|
signing_key
|
||||||
let signature = secp.sign_schnorr_with_aux_rand(message, &keypair, &aux_random);
|
.sign_raw(message, &aux_random)
|
||||||
signature.to_byte_array()
|
.expect("Expect to produce a valid signature")
|
||||||
|
.to_bytes()
|
||||||
};
|
};
|
||||||
|
|
||||||
Self { value }
|
Self { value }
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn is_valid_for(&self, bytes: &[u8], public_key: &PublicKey) -> bool /*{
|
pub fn is_valid_for(&self, bytes: &[u8], public_key: &PublicKey) -> bool {
|
||||||
// Convert signature bytes into Signature object
|
let Ok(pk) = k256::schnorr::VerifyingKey::from_bytes(public_key.value()) else {
|
||||||
let sig_slice: &[u8] = &self.value;
|
return false;
|
||||||
let sig = match k256::schnorr::Signature::try_from(sig_slice) {
|
};
|
||||||
Ok(s) => s,
|
|
||||||
Err(_) => {panic!("TEST"); //return false
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
// Convert x-only public key to VerifyingKey
|
let Ok(sig) = k256::schnorr::Signature::try_from(self.value.as_slice()) else {
|
||||||
let vk = match k256::schnorr::VerifyingKey::from_bytes(public_key.value()) {
|
return false;
|
||||||
Ok(vk) => vk,
|
};
|
||||||
Err(_) => {panic!("TEST"); //return false
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
// Verify the signature
|
pk.verify_raw(bytes, &sig).is_ok()
|
||||||
// vk.verify(bytes, &sig).is_ok()
|
|
||||||
|
|
||||||
// let msg = hex32(&v.message);
|
|
||||||
// let sig_bytes = hex64(&v.signature);
|
|
||||||
// let sig = Signature::try_from(&sig_bytes[..]).unwrap();
|
|
||||||
|
|
||||||
// let vk_bytes = hex32(&v.public_key);
|
|
||||||
// let vk = VerifyingKey::from_bytes(&vk_bytes).unwrap();
|
|
||||||
|
|
||||||
// --- VERIFY ---
|
|
||||||
// let verify_ok = vk.verify_prehash(&msg, &sig).is_ok();
|
|
||||||
|
|
||||||
|
|
||||||
}*/{
|
|
||||||
|
|
||||||
let pk = secp256k1::XOnlyPublicKey::from_byte_array(*public_key.value()).unwrap();
|
|
||||||
let secp = secp256k1::Secp256k1::new();
|
|
||||||
let sig = secp256k1::schnorr::Signature::from_byte_array(self.value);
|
|
||||||
secp.verify_schnorr(&sig, bytes, &pk).is_ok()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user