Skip to content

Commit

Permalink
actor: Add sign_winternitz_signature.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Dec 6, 2024
1 parent ec074f4 commit 8b3e9f6
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions core/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ use bitcoin::{
secp256k1::{schnorr, Keypair, Message, SecretKey, XOnlyPublicKey},
Address, TapSighash, TapTweakHash,
};
use bitcoin::{TapLeafHash, TapNodeHash, TapSighashType, TxOut};
use bitvm::signatures::winternitz;
use bitcoin::{TapLeafHash, TapNodeHash, TapSighashType, TxOut, Witness};
use bitvm::signatures::winternitz::{
self, BinarysearchVerifier, StraightforwardConverter, Winternitz,
};

/// Available transaction types for [`WinternitzDerivationPath`].
#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -284,22 +286,45 @@ impl Actor {
Ok(sig_hash)
}

/// Generates a Winternitz public key for the given path.
pub fn derive_winternitz_pk(
/// Returns derivied Winternitz secret key from given path.
fn get_derived_winternitz_sk(
&self,
path: WinternitzDerivationPath,
) -> Result<winternitz::PublicKey, BridgeError> {
) -> Result<winternitz::SecretKey, BridgeError> {
let wsk = self
.winternitz_secret_key
.ok_or(BridgeError::NoWinternitzSecretKey)?;
let altered_secret_key = [wsk.as_ref().to_vec(), path.to_vec()].concat();
Ok([wsk.as_ref().to_vec(), path.to_vec()].concat())
}

/// Generates a Winternitz public key for the given path.
pub fn derive_winternitz_pk(
&self,
path: WinternitzDerivationPath,
) -> Result<winternitz::PublicKey, BridgeError> {
let winternitz_params = winternitz::Parameters::new(path.message_length, path.log_d);

let altered_secret_key = self.get_derived_winternitz_sk(path)?;
let public_key = winternitz::generate_public_key(&winternitz_params, &altered_secret_key);

Ok(public_key)
}

/// Signs given data with Winternitz signature.
pub fn sign_winternitz_signature(
&self,
path: WinternitzDerivationPath,
data: Vec<u8>,
) -> Result<Witness, BridgeError> {
let winternitz = Winternitz::<BinarysearchVerifier, StraightforwardConverter>::new();
let winternitz_params = winternitz::Parameters::new(path.message_length, path.log_d);

let altered_secret_key = self.get_derived_winternitz_sk(path)?;

let witness = winternitz.sign(&winternitz_params, &altered_secret_key, &data);

Ok(witness)
}
}

#[cfg(test)]
Expand Down

0 comments on commit 8b3e9f6

Please sign in to comment.