fix double-hashing bug

This commit is contained in:
Sergio Chouhy 2025-07-21 17:50:08 -03:00
parent 6d5e2a1a9f
commit 39b4c866a1

View File

@ -1,6 +1,6 @@
use k256::{ use k256::{
ecdsa::{ ecdsa::{
signature::{Signer, Verifier}, signature::hazmat::{PrehashSigner, PrehashVerifier},
Signature, SigningKey, VerifyingKey, Signature, SigningKey, VerifyingKey,
}, },
EncodedPoint, Scalar, EncodedPoint, Scalar,
@ -245,7 +245,7 @@ impl Transaction {
/// The signature is generated over the hash of the body as computed by `body.hash()` /// The signature is generated over the hash of the body as computed by `body.hash()`
pub fn new(body: TransactionBody, private_key: SigningKey) -> Transaction { pub fn new(body: TransactionBody, private_key: SigningKey) -> Transaction {
let hash = body.hash(); let hash = body.hash();
let signature: TransactionSignature = private_key.sign(&hash); let signature: TransactionSignature = private_key.sign_prehash(&hash).unwrap();
let public_key = VerifyingKey::from(&private_key); let public_key = VerifyingKey::from(&private_key);
Self { Self {
body, body,
@ -260,7 +260,7 @@ impl Transaction {
let hash = self.body.hash(); let hash = self.body.hash();
self.public_key self.public_key
.verify(&hash, &self.signature) .verify_prehash(&hash, &self.signature)
.map_err(|_| TransactionSignatureError::InvalidSignature)?; .map_err(|_| TransactionSignatureError::InvalidSignature)?;
Ok(AuthenticatedTransaction { Ok(AuthenticatedTransaction {
@ -299,7 +299,7 @@ impl AuthenticatedTransaction {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use k256::FieldBytes; use k256::{ecdsa::signature::Signer, FieldBytes};
use secp256k1_zkp::{constants::SECRET_KEY_SIZE, Tweak}; use secp256k1_zkp::{constants::SECRET_KEY_SIZE, Tweak};
use sha2::{digest::FixedOutput, Digest}; use sha2::{digest::FixedOutput, Digest};
@ -384,7 +384,7 @@ mod tests {
assert!(authenticated_tx assert!(authenticated_tx
.transaction() .transaction()
.public_key .public_key
.verify(hash, &signature) .verify_prehash(hash, &signature)
.is_ok()); .is_ok());
} }