Skip to content

Commit

Permalink
taproot::new
Browse files Browse the repository at this point in the history
  • Loading branch information
supreme2580 committed Oct 22, 2024
1 parent 34bad7b commit 5f172aa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/engine/src/signature/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,8 @@ pub const WITNESS_V0_PUB_KEY_HASH_LEN: usize = 22;

pub const MAX_U128: u128 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
pub const MAX_U32: u32 = 0xFFFFFFFF;

pub const SCHNORR_SIGNATURE_LEN: usize = 64;

pub const PUB_KEY_BYTES_LEN: usize = 32;
pub const PUB_KEY_BYTES_LEN_COMPRESSED: usize = 33;
28 changes: 26 additions & 2 deletions packages/engine/src/signature/signature.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,32 @@ pub impl TaprootSigVerifierImpl<
fn new(
sig_bytes: @ByteArray, pk_bytes: @ByteArray, annex: @ByteArray
) -> Result<TaprootSigVerifier, felt252> {
// TODO
return Result::Err('TaprootSig not implemented');
if sig_bytes.len() != 64 && sig_bytes.len() != 65 {
return Result::Err('Invalid Schnorr signature');
}

if pk_bytes.len() != 32 {
return Result::Err('Invalid public key length');
}

let hash_type = if sig_bytes.len() == 65 {
sig_bytes[64].into()
} else {
constants::SIG_HASH_DEFAULT
};

let pub_key = parse_schnorr_pub_key(pk_bytes);
let sig = Signature {
r: u256_from_byte_array_with_offset(sig_bytes, 0, 32),
s: u256_from_byte_array_with_offset(sig_bytes, 32, 32),
y_parity: false, // Schnorr signatures don't use y_parity
};

Result::Ok(
TaprootSigVerifier {
pub_key, sig, sig_bytes: sig_bytes, pk_bytes: pk_bytes, hash_type, annex,
}
)
}

fn new_base(
Expand Down

0 comments on commit 5f172aa

Please sign in to comment.