From 3e934278d3b7a68da519c98bac8cff809f76c4fb Mon Sep 17 00:00:00 2001 From: Jeanmichel7 Date: Fri, 18 Oct 2024 19:01:00 +0200 Subject: [PATCH] merge --- packages/engine/src/engine.cairo | 4 +- packages/engine/src/lib.cairo | 4 +- packages/engine/src/opcodes/crypto.cairo | 22 +++++++--- packages/engine/src/opcodes/opcodes.cairo | 44 ++++++++++--------- packages/engine/src/signature/signature.cairo | 21 ++------- packages/engine/src/taproot.cairo | 26 ++++++++--- tests/script_tests_failing.json | 9 ---- 7 files changed, 66 insertions(+), 64 deletions(-) diff --git a/packages/engine/src/engine.cairo b/packages/engine/src/engine.cairo index 411af1d..0b0e857 100644 --- a/packages/engine/src/engine.cairo +++ b/packages/engine/src/engine.cairo @@ -477,6 +477,8 @@ pub impl EngineInternalImpl< T, I, O, IEngineTransactionInput, IEngineTransactionOutput >, +Drop, + +Drop, + +Drop, > of EngineInternalTrait { fn pull_data(ref self: Engine, len: usize) -> Result { let script = *(self.scripts[self.script_idx]); @@ -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(()); diff --git a/packages/engine/src/lib.cairo b/packages/engine/src/lib.cairo index 27da824..8ab9894 100644 --- a/packages/engine/src/lib.cairo +++ b/packages/engine/src/lib.cairo @@ -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)] diff --git a/packages/engine/src/opcodes/crypto.cairo b/packages/engine/src/opcodes/crypto.cairo index ab6ea3a..c9761aa 100644 --- a/packages/engine/src/opcodes/crypto.cairo +++ b/packages/engine/src/opcodes/crypto.cairo @@ -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 @@ -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::::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::::verify(ref verifier); } if !is_valid && @engine.use_taproot == @true { @@ -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::::verify(ref verifier)) { return Result::Err(Error::TAPROOT_INVALID_SIG); } diff --git a/packages/engine/src/opcodes/opcodes.cairo b/packages/engine/src/opcodes/opcodes.cairo index 8da84dd..8253a2a 100644 --- a/packages/engine/src/opcodes/opcodes.cairo +++ b/packages/engine/src/opcodes/opcodes.cairo @@ -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, @@ -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; diff --git a/packages/engine/src/signature/signature.cairo b/packages/engine/src/signature/signature.cairo index da3b1bb..69437eb 100644 --- a/packages/engine/src/signature/signature.cairo +++ b/packages/engine/src/signature/signature.cairo @@ -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. @@ -590,21 +590,6 @@ pub struct TaprootSigVerifier { annex: @ByteArray, } - -// pub trait BaseSigVerifierTrait< -// I, -// O, -// T, -// +EngineTransactionInputTrait, -// +EngineTransactionOutputTrait, -// +EngineTransactionTrait -// > { -// fn new( -// ref vm: Engine, sig_bytes: @ByteArray, pk_bytes: @ByteArray -// ) -> Result; -// fn verify(ref self: BaseSigVerifier, ref vm: Engine) -> bool; -// } - pub trait TaprootSigVerifierTrait< I, O, @@ -630,9 +615,9 @@ pub impl TaprootSigVerifierImpl< impl IEngineTransaction: EngineTransactionTrait< T, I, O, IEngineTransactionInput, IEngineTransactionOutput >, - +Drop, +Drop, - +Drop + +Drop, + +Drop, > of TaprootSigVerifierTrait { fn new( sig_bytes: @ByteArray, pk_bytes: @ByteArray, annex: @ByteArray diff --git a/packages/engine/src/taproot.cairo b/packages/engine/src/taproot.cairo index dd4d8be..4c8d8cf 100644 --- a/packages/engine/src/taproot.cairo +++ b/packages/engine/src/taproot.cairo @@ -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}; @@ -137,8 +139,20 @@ 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, + I, + +Drop, + impl IEngineTransactionInputTrait: EngineTransactionInputTrait, + O, + +Drop, + impl IEngineTransactionOutputTrait: EngineTransactionOutputTrait, + impl IEngineTransactionTrait: EngineTransactionTrait< + T, I, O, IEngineTransactionInputTrait, IEngineTransactionOutputTrait + > + >( + witness_program: @ByteArray, raw_sig: @ByteArray, tx: @T, tx_idx: u32 ) -> Result<(), felt252> { let witness: Span = tx.get_transaction_inputs()[tx_idx].get_witness(); let mut annex = @""; @@ -146,10 +160,8 @@ pub impl TaprootContextImpl of TaprootContextTrait { annex = witness[witness.len() - 1]; } - let mut verifier = TaprootSigVerifierImpl::< - Transaction - >::new(raw_sig, witness_program, annex)?; - let is_valid = TaprootSigVerifierImpl::::verify(ref verifier); + let mut verifier = TaprootSigVerifierImpl::::new(raw_sig, witness_program, annex)?; + let is_valid = TaprootSigVerifierImpl::::verify(ref verifier); if !is_valid { return Result::Err(Error::TAPROOT_INVALID_SIG); } diff --git a/tests/script_tests_failing.json b/tests/script_tests_failing.json index 9173aae..4cd223e 100644 --- a/tests/script_tests_failing.json +++ b/tests/script_tests_failing.json @@ -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"],