Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeanmichel7 committed Oct 18, 2024
1 parent 8b7592e commit 3e93427
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 64 deletions.
4 changes: 3 additions & 1 deletion packages/engine/src/engine.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ pub impl EngineInternalImpl<
T, I, O, IEngineTransactionInput, IEngineTransactionOutput
>,
+Drop<T>,
+Drop<I>,
+Drop<O>,
> of EngineInternalTrait<I, O, T> {
fn pull_data(ref self: Engine<T>, len: usize) -> Result<ByteArray, felt252> {
let script = *(self.scripts[self.script_idx]);
Expand Down Expand Up @@ -642,7 +644,7 @@ pub impl EngineInternalImpl<

if witness_len == 1 {
TaprootContextImpl::verify_taproot_spend(
@self.witness_program, witness[0], @self.transaction, self.tx_idx
@self.witness_program, witness[0], self.transaction, self.tx_idx
)?;
self.taproot_context.must_succeed = true;
return Result::Ok(());
Expand Down
4 changes: 3 additions & 1 deletion packages/engine/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ pub mod signature {
pub mod sighash;
pub mod constants;
pub mod utils;
pub use signature::{BaseSigVerifier, BaseSigVerifierTrait};
pub use signature::{
BaseSigVerifier, BaseSigVerifierTrait, TaprootSigVerifier, TaprootSigVerifierTrait
};
}
pub mod transaction;
#[cfg(test)]
Expand Down
22 changes: 15 additions & 7 deletions packages/engine/src/opcodes/crypto.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::flags::ScriptFlags;
use crate::signature::signature;
use crate::signature::sighash;
use starknet::secp256_trait::{is_valid_signature};
use core::sha256::compute_sha256_byte_array;
use core::num::traits::OverflowingAdd;
use crate::signature::signature::{
BaseSigVerifierTrait, BaseSegwitSigVerifierTrait, TaprootSigVerifierTrait
Expand Down Expand Up @@ -117,8 +116,17 @@ pub fn opcode_checksig<
return Result::Err(Error::TAPROOT_EMPTY_PUBKEY);
}

let mut verifier = TaprootSigVerifierTrait::new_base(@full_sig_bytes, @pk_bytes)?;
is_valid = TaprootSigVerifierTrait::verify(ref verifier);
let mut verifier = TaprootSigVerifierTrait::<
I, O, T
>::new(@full_sig_bytes, @pk_bytes, engine.taproot_context.annex)?;
if !(TaprootSigVerifierTrait::<I, O, T>::verify(ref verifier)) {
return Result::Err(Error::TAPROOT_INVALID_SIG);
}

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

if !is_valid && @engine.use_taproot == @true {
Expand Down Expand Up @@ -413,10 +421,10 @@ pub fn opcode_checksigadd<
//
// If the constructor fails immediately, then it's because the public
// key size is zero, so we'll fail all script execution.
let mut verifier = TaprootSigVerifierTrait::new(
@sig_bytes, @pk_bytes, engine.taproot_context.annex
)?;
if !(TaprootSigVerifierTrait::verify(ref verifier)) {
let mut verifier = TaprootSigVerifierTrait::<
I, O, T
>::new(@sig_bytes, @pk_bytes, engine.taproot_context.annex)?;
if !(TaprootSigVerifierTrait::<I, O, T>::verify(ref verifier)) {
return Result::Err(Error::TAPROOT_INVALID_SIG);
}

Expand Down
44 changes: 23 additions & 21 deletions packages/engine/src/opcodes/opcodes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ pub mod Opcode {
use crate::opcodes::{
constants, flow, stack, splice, bitwise, arithmetic, crypto, locktime, utils
};
use crate::parser::data_len;

pub fn execute<
T,
Expand Down Expand Up @@ -484,40 +485,41 @@ pub mod Opcode {
// OP_UNKNOWNX
return true;
}
if opcode == OP_RESERVED ||
opcode == OP_VER ||
opcode == OP_CAT ||
opcode == OP_SUBSTR ||
opcode == OP_LEFT ||
opcode == OP_RIGHT ||
opcode == OP_INVERT ||
opcode == OP_AND ||
opcode == OP_OR ||
opcode == OP_XOR ||
opcode == OP_RESERVED1 ||
opcode == OP_RESERVED2 ||
opcode == OP_2MUL ||
opcode == OP_2DIV ||
opcode == OP_MUL ||
opcode == OP_DIV ||
opcode == OP_MOD ||
opcode == OP_LSHIFT ||
opcode == OP_RSHIFT {
if opcode == OP_RESERVED
|| opcode == OP_VER
|| opcode == OP_CAT
|| opcode == OP_SUBSTR
|| opcode == OP_LEFT
|| opcode == OP_RIGHT
|| opcode == OP_INVERT
|| opcode == OP_AND
|| opcode == OP_OR
|| opcode == OP_XOR
|| opcode == OP_RESERVED1
|| opcode == OP_RESERVED2
|| opcode == OP_2MUL
|| opcode == OP_2DIV
|| opcode == OP_MUL
|| opcode == OP_DIV
|| opcode == OP_MOD
|| opcode == OP_LSHIFT
|| opcode == OP_RSHIFT {
return true;
}
return false;
}

pub fn has_success_opcode(script: @ByteArray) -> bool {
let mut i = 0;
let mut i: usize = 0;
let mut result = false;

while i < script.len() {
let opcode = script[i];
if is_success_opcode(opcode) {
result = true;
break;
}
let data_len = data_len(i, script).unwrap();
let data_len = data_len(script, i).unwrap();
i += data_len + 1;
};
return result;
Expand Down
21 changes: 3 additions & 18 deletions packages/engine/src/signature/signature.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ pub fn parse_schnorr_pub_key(pk_bytes: @ByteArray) -> Secp256k1Point {

let mut key_compressed: ByteArray = "\02";
key_compressed.append(pk_bytes);
return parse_pub_key(@key_compressed);
return parse_pub_key(@key_compressed).unwrap();
}

// Parses a DER-encoded ECDSA signature byte array into a `Signature` struct.
Expand Down Expand Up @@ -590,21 +590,6 @@ pub struct TaprootSigVerifier {
annex: @ByteArray,
}


// pub trait BaseSigVerifierTrait<
// I,
// O,
// T,
// +EngineTransactionInputTrait<I>,
// +EngineTransactionOutputTrait<O>,
// +EngineTransactionTrait<T, I, O>
// > {
// fn new(
// ref vm: Engine<T>, sig_bytes: @ByteArray, pk_bytes: @ByteArray
// ) -> Result<BaseSigVerifier, felt252>;
// fn verify(ref self: BaseSigVerifier, ref vm: Engine<T>) -> bool;
// }

pub trait TaprootSigVerifierTrait<
I,
O,
Expand All @@ -630,9 +615,9 @@ pub impl TaprootSigVerifierImpl<
impl IEngineTransaction: EngineTransactionTrait<
T, I, O, IEngineTransactionInput, IEngineTransactionOutput
>,
+Drop<I>,
+Drop<O>,
+Drop<T>
+Drop<I>,
+Drop<T>,
> of TaprootSigVerifierTrait<I, O, T> {
fn new(
sig_bytes: @ByteArray, pk_bytes: @ByteArray, annex: @ByteArray
Expand Down
26 changes: 19 additions & 7 deletions packages/engine/src/taproot.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::errors::Error;
use crate::transaction::{Transaction, EngineTransactionTrait, EngineTransactionInputTrait};
use crate::transaction::{
EngineTransactionTrait, EngineTransactionOutputTrait, EngineTransactionInputTrait
};
use crate::signature::signature::parse_schnorr_pub_key;
use crate::signature::signature::{TaprootSigVerifierImpl};
use starknet::secp256k1::{Secp256k1Point};
Expand Down Expand Up @@ -137,19 +139,29 @@ pub impl TaprootContextImpl of TaprootContextTrait {
}
}

fn verify_taproot_spend(
witness_program: @ByteArray, raw_sig: @ByteArray, tx: @Transaction, tx_idx: u32
fn verify_taproot_spend<
T,
+Drop<T>,
I,
+Drop<I>,
impl IEngineTransactionInputTrait: EngineTransactionInputTrait<I>,
O,
+Drop<O>,
impl IEngineTransactionOutputTrait: EngineTransactionOutputTrait<O>,
impl IEngineTransactionTrait: EngineTransactionTrait<
T, I, O, IEngineTransactionInputTrait, IEngineTransactionOutputTrait
>
>(
witness_program: @ByteArray, raw_sig: @ByteArray, tx: @T, tx_idx: u32
) -> Result<(), felt252> {
let witness: Span<ByteArray> = tx.get_transaction_inputs()[tx_idx].get_witness();
let mut annex = @"";
if is_annexed_witness(witness, witness.len()) {
annex = witness[witness.len() - 1];
}

let mut verifier = TaprootSigVerifierImpl::<
Transaction
>::new(raw_sig, witness_program, annex)?;
let is_valid = TaprootSigVerifierImpl::<Transaction>::verify(ref verifier);
let mut verifier = TaprootSigVerifierImpl::<I, O, T>::new(raw_sig, witness_program, annex)?;
let is_valid = TaprootSigVerifierImpl::<I, O, T>::verify(ref verifier);
if !is_valid {
return Result::Err(Error::TAPROOT_INVALID_SIG);
}
Expand Down
9 changes: 0 additions & 9 deletions tests/script_tests_failing.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
<<<<<<< HEAD
[
["Format is: [[wit..., amount]?, scriptSig, scriptPubKey, flags, expected_scripterror, ... comments]"],
["0x01 0x00","1","MINIMALDATA","OK"],
["0x27 0x3024021077777777777777777777777777777777020a7777777777777777777777777777777701","0 CHECKSIG NOT","","OK","S with invalid S length is correctly encoded"],
["0x27 0x302402107777777777777777777777777777777702108777777777777777777777777777777701","0 CHECKSIG NOT","","OK","Negative S is correctly encoded"],
["1 0x01 0xb9","HASH160 0x14 0x15727299b05b45fdaf9ac9ecf7565cfe27c3e567 EQUAL","P2SH,DISCOURAGE_UPGRADABLE_NOPS","DISCOURAGE_UPGRADABLE_NOPS","Discouraged NOP10 in redeemScript"],
=======
>>>>>>> 926e785bfb6a6bfd23d8f1e2747d67aa757791c2
["","'dummy' 'sig1' 1 'pk1' 1 CHECKMULTISIG IF 1 ENDIF","","EVAL_FALSE","CHECKMULTISIG must push false to stack when signature is invalid when NOT in strict enc mode"],
["0 0x47 0x30440220cae00b1444babfbf6071b0ba8707f6bd373da3df494d6e74119b0430c5db810502205d5231b8c5939c8ff0c82242656d6e06edb073d42af336c99fe8837c36ea39d501 0","2 0x21 0x038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508 0x21 0x03363d90d447b00c9c99ceac05b6262ee053441c7e55552ffe526bad8f83ff4640 2 CHECKMULTISIG","DERSIG","EVAL_FALSE","BIP66 example 11, with DERSIG"],
["0 0x47 0x30440220b119d67d389315308d1745f734a51ff3ec72e06081e84e236fdf9dc2f5d2a64802204b04e3bc38674c4422ea317231d642b56dc09d214a1ecbbf16ecca01ed996e2201 0","2 0x21 0x038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508 0x21 0x03363d90d447b00c9c99ceac05b6262ee053441c7e55552ffe526bad8f83ff4640 2 CHECKMULTISIG NOT","DERSIG","OK","BIP66 example 12, with DERSIG"],
Expand Down

0 comments on commit 3e93427

Please sign in to comment.