Skip to content

Commit

Permalink
new base return
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeanmichel7 committed Jan 16, 2025
1 parent a9de374 commit 401f112
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 46 deletions.
27 changes: 23 additions & 4 deletions packages/engine/src/opcodes/crypto.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ pub fn opcode_checksig<
) -> Result<(), felt252> {
let pk_bytes = engine.dstack.pop_byte_array()?;
let full_sig_bytes = engine.dstack.pop_byte_array()?;

if full_sig_bytes.len() < 1 {
if !engine.use_taproot && full_sig_bytes.len() < 1 {
engine.dstack.push_bool(false);
return Result::Ok(());
}
Expand Down Expand Up @@ -117,17 +116,37 @@ pub fn opcode_checksig<
}
} else if engine.use_taproot {
// Taproot Signature Verification
engine.taproot_context.use_ops_budget()?;
if pk_bytes.len() == 0 {

let pk_bytes_len = pk_bytes.len();
if (pk_bytes_len > 0) {
engine.taproot_context.use_ops_budget()?;
}

if pk_bytes_len == 0 {
return Result::Err(Error::TAPROOT_EMPTY_PUBKEY);
}

if (full_sig_bytes.len() == 0) {
engine.dstack.push_byte_array("");
return Result::Ok(());
}

let verifier = TaprootSigVerifierTrait::<
I, O, T,
>::new_base(@full_sig_bytes, @pk_bytes, ref engine)?;

is_valid = TaprootSigVerifierTrait::<I, O, T>::verify(verifier).is_ok();
}

// TODO finish from here for taproot impl
// txscript opcode.go opcodeCheckSig l2080
// use struct validation sig

// if vm.hasFlag(ScriptVerifyConstScriptCode) && result.sigMatch {
// str := "non-const script code"
// return scriptError(ErrNonConstScriptCode, str)
// }

if !is_valid && @engine.use_taproot == @true {
return Result::Err(Error::SIG_NULLFAIL);
}
Expand Down
64 changes: 22 additions & 42 deletions packages/engine/src/signature/taproot_signature.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn parse_taproot_sig_and_pk<
#[derive(Drop)]
pub struct TaprootSigVerifier<T> {
// public key as a point on the secp256k1 curve, used to verify the signature
pub pub_key: Secp256k1Point,
pub pub_key: Option<Secp256k1Point>,
// ECDSA signature
pub sig: Signature,
// raw byte array of the signature
Expand Down Expand Up @@ -89,15 +89,15 @@ pub trait TaprootSigVerifierTrait<
+EngineTransactionOutputTrait<O>,
+EngineTransactionTrait<T, I, O>,
> {
// fn empty() -> TaprootSigVerifier<T>;
fn empty() -> TaprootSigVerifier<T>;
fn new(
sig_bytes: @ByteArray, pk_bytes: @ByteArray, annex: @ByteArray, ref engine: Engine<T>,
) -> Result<TaprootSigVerifier<T>, felt252>;
fn new_base(
sig_bytes: @ByteArray, pk_bytes: @ByteArray, ref engine: Engine<T>,
) -> Result<TaprootSigVerifier<T>, felt252>;
fn verify(self: TaprootSigVerifier<T>) -> Result<(), felt252>;
fn verify_base(ref self: TaprootSigVerifier<T>) -> bool;
fn verify_base(self: TaprootSigVerifier<T>) -> Result<(), felt252>;
}

pub impl TaprootSigVerifierImpl<
Expand All @@ -114,20 +114,20 @@ pub impl TaprootSigVerifierImpl<
T, I, O, IEngineTransactionInputTrait, IEngineTransactionOutputTrait,
>,
> of TaprootSigVerifierTrait<I, O, T> {
// fn empty() -> TaprootSigVerifier<T> {
// TaprootSigVerifier {
// pub_key: Secp256Trait::<Secp256k1Point>::get_generator_point(),
// sig: Signature { r: 0, s: 0, y_parity: false },
// sig_bytes: @"",
// pk_bytes: @"",
// hash_type: 0,
// tx: @Default::default(),
// inputIndex: 0,
// prevOuts: Default::<EngineTransactionOutput>::default(),
// hashCache: Default::default(),
// annex: @"",
// }
// }
fn empty() -> TaprootSigVerifier<T> {
TaprootSigVerifier {
pub_key: Option::None,
sig: Signature { r: 0, s: 0, y_parity: false },
sig_bytes: @"",
pk_bytes: @"",
hash_type: 0,
tx: @Default::default(),
inputIndex: 0,
prevOuts: Default::<EngineTransactionOutput>::default(), // verify this
hashCache: Default::default(),
annex: @"",
}
}

fn new(
sig_bytes: @ByteArray, pk_bytes: @ByteArray, annex: @ByteArray, ref engine: Engine<T>,
Expand All @@ -140,7 +140,7 @@ pub impl TaprootSigVerifierImpl<

Result::Ok(
TaprootSigVerifier {
pub_key,
pub_key: Option::Some(pub_key),
sig,
sig_bytes,
pk_bytes,
Expand Down Expand Up @@ -171,31 +171,11 @@ pub impl TaprootSigVerifierImpl<
if engine.has_flag(ScriptFlags::ScriptVerifyDiscourageUpgradeablePubkeyType) {
return Result::Err(Error::DISCOURAGE_UPGRADABLE_PUBKEY_TYPE);
}
let pub_key: u256 = u256_from_byte_array_with_offset(pk_bytes, 0, 32);
let pk = Secp256Trait::<
Secp256k1Point,
>::secp256_ec_get_point_from_x_syscall(pub_key, false)
.unwrap_syscall()
.expect(Error::SECP256K1_INVALID_POINT);

return (Result::Ok(
TaprootSigVerifier {
pub_key: pk,
sig: Signature { r: 0, s: 0, y_parity: false },
sig_bytes,
pk_bytes,
hash_type: constants::SIG_HASH_DEFAULT,
tx: @Default::default(), // engine tx ?
inputIndex: 0, // engine tx idx ?
prevOuts: Default::<EngineTransactionOutput>::default(), // engine utxo ?
hashCache: Default::default(), // engine sig hash ?
annex: @"" // engine annex ?
},
));
// return Result::Ok(Self::empty());
return Result::Ok(Self::empty());
}
}


fn verify(self: TaprootSigVerifier<T>) -> Result<(), felt252> {
let mut opts = TaprootSighashOptionsTrait::new_with_annex(self.annex);
let sig_hash = sighash::calc_taproot_signature_hash::<
Expand All @@ -208,9 +188,9 @@ pub impl TaprootSigVerifierImpl<
Result::Ok(())
}

fn verify_base(ref self: TaprootSigVerifier<T>) -> bool {
fn verify_base(self: TaprootSigVerifier<T>) -> Result<(), felt252> {
// TODO: implement taproot verification
return false;
Result::Ok(())
}
}

1 change: 1 addition & 0 deletions packages/engine/src/taproot.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ pub impl TaprootContextImpl of TaprootContextTrait {
if is_valid.is_err() {
return Result::Err(Error::TAPROOT_INVALID_SIG);
}
// if verify.sigvalid Ok() else error invalid sig
Result::Ok(())
}

Expand Down

0 comments on commit 401f112

Please sign in to comment.