mirror of
https://github.com/logos-blockchain/logos-execution-zone.git
synced 2026-03-24 03:03:09 +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
|
||||
hex.workspace = true
|
||||
k256.workspace = true
|
||||
secp256k1 = "0.31.1"
|
||||
risc0-binfmt = "3.0.2"
|
||||
log.workspace = true
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use borsh::{BorshDeserialize, BorshSerialize};
|
||||
use k256::ecdsa::signature::RandomizedSigner;
|
||||
//use k256::schnorr::signature::Verifier;
|
||||
pub use private_key::PrivateKey;
|
||||
pub use public_key::PublicKey;
|
||||
use rand::{RngCore as _, rngs::OsRng};
|
||||
@ -14,11 +14,27 @@ pub struct 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 {
|
||||
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 {
|
||||
#[must_use]
|
||||
pub fn new(key: &PrivateKey, message: &[u8]) -> Self {
|
||||
@ -27,86 +43,35 @@ impl Signature {
|
||||
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(
|
||||
key: &PrivateKey,
|
||||
message: &[u8],
|
||||
aux_random: [u8; 32],
|
||||
) -> Self {
|
||||
let value = {
|
||||
let secp = secp256k1::Secp256k1::new();
|
||||
let secret_key = secp256k1::SecretKey::from_byte_array(*key.value()).unwrap();
|
||||
let keypair = secp256k1::Keypair::from_secret_key(&secp, &secret_key);
|
||||
let signature = secp.sign_schnorr_with_aux_rand(message, &keypair, &aux_random);
|
||||
signature.to_byte_array()
|
||||
let signing_key = k256::schnorr::SigningKey::from_bytes(key.value())
|
||||
.expect("Expect valid signing key");
|
||||
signing_key
|
||||
.sign_raw(message, &aux_random)
|
||||
.expect("Expect to produce a valid signature")
|
||||
.to_bytes()
|
||||
};
|
||||
|
||||
Self { value }
|
||||
}
|
||||
*/
|
||||
|
||||
#[must_use]
|
||||
pub fn is_valid_for(&self, bytes: &[u8], public_key: &PublicKey) -> bool /*{
|
||||
// Convert signature bytes into Signature object
|
||||
let sig_slice: &[u8] = &self.value;
|
||||
let sig = match k256::schnorr::Signature::try_from(sig_slice) {
|
||||
Ok(s) => s,
|
||||
Err(_) => {panic!("TEST"); //return false
|
||||
},
|
||||
};
|
||||
pub fn is_valid_for(&self, bytes: &[u8], public_key: &PublicKey) -> bool {
|
||||
let Ok(pk) = k256::schnorr::VerifyingKey::from_bytes(public_key.value()) else {
|
||||
return false;
|
||||
};
|
||||
|
||||
// Convert x-only public key to VerifyingKey
|
||||
let vk = match k256::schnorr::VerifyingKey::from_bytes(public_key.value()) {
|
||||
Ok(vk) => vk,
|
||||
Err(_) => {panic!("TEST"); //return false
|
||||
},
|
||||
};
|
||||
let Ok(sig) = k256::schnorr::Signature::try_from(self.value.as_slice()) else {
|
||||
return false;
|
||||
};
|
||||
|
||||
// Verify the signature
|
||||
// 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()
|
||||
pk.verify_raw(bytes, &sig).is_ok()
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user