created zcash_script_verify_prehashed; compiles

This commit is contained in:
Conrado Gouvea 2024-03-06 17:47:54 -03:00
parent 6c2eb13d9f
commit 8a7245f1e5
4 changed files with 446 additions and 328 deletions

File diff suppressed because it is too large Load Diff

View File

@ -7,15 +7,15 @@
#ifndef BITCOIN_SCRIPT_INTERPRETER_H
#define BITCOIN_SCRIPT_INTERPRETER_H
#include "script_error.h"
#include "primitives/transaction.h"
#include "script_error.h"
#include <rust/transaction.h>
#include <vector>
#include <climits>
#include <stdint.h>
#include <string>
#include <climits>
#include <vector>
class CPubKey;
class CScript;
@ -26,8 +26,7 @@ class uint256;
const unsigned int NOT_AN_INPUT = UINT_MAX;
/** Signature hash types/flags */
enum
{
enum {
SIGHASH_ALL = 1,
SIGHASH_NONE = 2,
SIGHASH_SINGLE = 3,
@ -35,12 +34,11 @@ enum
};
/** Script verification flags */
enum
{
SCRIPT_VERIFY_NONE = 0,
enum {
SCRIPT_VERIFY_NONE = 0,
// Evaluate P2SH subscripts (softfork safe, BIP16).
SCRIPT_VERIFY_P2SH = (1U << 0),
SCRIPT_VERIFY_P2SH = (1U << 0),
// Passing a non-strict-DER signature or one with undefined hashtype to a checksig operation causes script failure.
// Evaluating a pubkey that is not (0x04 + 64 bytes) or (0x02 or 0x03 + 32 bytes) by checksig causes script failure.
@ -49,11 +47,11 @@ enum
// Passing a non-strict-DER signature to a checksig operation causes script failure (softfork safe, BIP62 rule 1)
// In Zcash this is required, and validation of non-strict-DER signatures is not implemented.
//SCRIPT_VERIFY_DERSIG = (1U << 2),
// SCRIPT_VERIFY_DERSIG = (1U << 2),
// Passing a non-strict-DER signature or one with S > order/2 to a checksig operation causes script failure
// (softfork safe, BIP62 rule 5).
SCRIPT_VERIFY_LOW_S = (1U << 3),
SCRIPT_VERIFY_LOW_S = (1U << 3),
// verify dummy stack item consumed by CHECKMULTISIG is of zero-length (softfork safe, BIP62 rule 7).
SCRIPT_VERIFY_NULLDUMMY = (1U << 4),
@ -76,7 +74,7 @@ enum
// discouraged NOPs fails the script. This verification flag will never be
// a mandatory flag applied to scripts in a block. NOPs that are not
// executed, e.g. within an unexecuted IF ENDIF block, are *not* rejected.
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS = (1U << 7),
SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS = (1U << 7),
// Require that only a single stack element remains after evaluation. This changes the success criterion from
// "At least one stack element must remain, and when interpreted as a boolean, it must be true" to
@ -91,10 +89,9 @@ enum
SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9),
};
bool CheckSignatureEncoding(const std::vector<unsigned char> &vchSig, unsigned int flags, ScriptError* serror);
bool CheckSignatureEncoding(const std::vector<unsigned char>& vchSig, unsigned int flags, ScriptError* serror);
struct PrecomputedTransactionData
{
struct PrecomputedTransactionData {
uint256 hashPrevouts, hashSequence, hashOutputs, hashJoinSplits, hashShieldedSpends, hashShieldedOutputs;
/** Precomputed transaction parts. */
std::unique_ptr<PrecomputedTxParts, decltype(&zcash_transaction_precomputed_free)> preTx;
@ -115,8 +112,7 @@ private:
size_t allPrevOutputsLen);
};
enum SigVersion
{
enum SigVersion {
SIGVERSION_SPROUT = 0,
SIGVERSION_OVERWINTER = 1,
SIGVERSION_SAPLING = 2,
@ -124,7 +120,7 @@ enum SigVersion
};
uint256 SignatureHash(
const CScript &scriptCode,
const CScript& scriptCode,
const CTransaction& txTo,
unsigned int nIn,
int nHashType,
@ -146,7 +142,7 @@ public:
virtual bool CheckLockTime(const CScriptNum& nLockTime) const
{
return false;
return false;
}
virtual ~BaseSignatureChecker() {}
@ -178,8 +174,28 @@ public:
MutableTransactionSignatureChecker(const CMutableTransaction* txToIn, const PrecomputedTransactionData& txdataIn, unsigned int nInIn, const CAmount& amount) : TransactionSignatureChecker(&txTo, txdataIn, nInIn, amount), txTo(*txToIn) {}
};
class PrehashedTransactionSignatureChecker : public BaseSignatureChecker
{
private:
const unsigned char* sighash;
unsigned int sighashLen;
const CScriptNum& nLockTime;
bool isFinal;
unsigned int nIn;
const CAmount amount;
protected:
virtual bool VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const;
public:
PrehashedTransactionSignatureChecker(const unsigned char* sighash, unsigned int sighashLen, const CScriptNum& nLockTime, bool isFinal, unsigned int nInIn, const CAmount& amountIn) : sighash(sighash), sighashLen(sighashLen), nLockTime(nLockTime), isFinal(isFinal), nIn(nInIn), amount(amountIn) {}
bool CheckSig(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, uint32_t consensusBranchId) const;
bool CheckLockTime(const CScriptNum& nLockTime) const;
};
bool EvalScript(
std::vector<std::vector<unsigned char> >& stack,
std::vector<std::vector<unsigned char>>& stack,
const CScript& script,
unsigned int flags,
const BaseSignatureChecker& checker,

View File

@ -12,7 +12,8 @@
#include "script/interpreter.h"
#include "version.h"
namespace {
namespace
{
inline int set_error(zcash_script_error* ret, zcash_script_error serror)
{
if (ret)
@ -25,17 +26,15 @@ inline int set_error(zcash_script_error* ret, zcash_script_error serror)
unsigned int GetLegacySigOpCount(const CTransaction& tx)
{
unsigned int nSigOps = 0;
for (const CTxIn& txin : tx.vin)
{
for (const CTxIn& txin : tx.vin) {
nSigOps += txin.scriptSig.GetSigOpCount(false);
}
for (const CTxOut& txout : tx.vout)
{
for (const CTxOut& txout : tx.vout) {
nSigOps += txout.scriptPubKey.GetSigOpCount(false);
}
return nSigOps;
}
}
} // namespace
struct PrecomputedTransaction {
const CTransaction tx;
@ -53,8 +52,8 @@ void* zcash_script_new_precomputed_tx(
zcash_script_error* err)
{
try {
const char* txToEnd = (const char *)(txTo + txToLen);
RustDataStream stream((const char *)txTo, txToEnd, SER_NETWORK, PROTOCOL_VERSION);
const char* txToEnd = (const char*)(txTo + txToLen);
RustDataStream stream((const char*)txTo, txToEnd, SER_NETWORK, PROTOCOL_VERSION);
CTransaction tx;
stream >> tx;
if (GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) != txToLen) {
@ -87,8 +86,8 @@ void* zcash_script_new_precomputed_tx_v5(
{
CTransaction tx;
try {
const char* txToEnd = (const char *)(txTo + txToLen);
RustDataStream stream((const char *)txTo, txToEnd, SER_NETWORK, PROTOCOL_VERSION);
const char* txToEnd = (const char*)(txTo + txToLen);
RustDataStream stream((const char*)txTo, txToEnd, SER_NETWORK, PROTOCOL_VERSION);
stream >> tx;
if (GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) != txToLen) {
set_error(err, zcash_script_ERR_TX_SIZE_MISMATCH);
@ -145,16 +144,19 @@ int zcash_script_verify_precomputed(
}
int zcash_script_verify(
const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen,
const unsigned char* scriptPubKey,
unsigned int scriptPubKeyLen,
int64_t amount,
const unsigned char *txTo, unsigned int txToLen,
unsigned int nIn, unsigned int flags,
const unsigned char* txTo,
unsigned int txToLen,
unsigned int nIn,
unsigned int flags,
uint32_t consensusBranchId,
zcash_script_error* err)
{
try {
const char* txToEnd = (const char *)(txTo + txToLen);
RustDataStream stream((const char *)txTo, txToEnd, SER_NETWORK, PROTOCOL_VERSION);
const char* txToEnd = (const char*)(txTo + txToLen);
RustDataStream stream((const char*)txTo, txToEnd, SER_NETWORK, PROTOCOL_VERSION);
CTransaction tx;
stream >> tx;
if (nIn >= tx.vin.size())
@ -165,7 +167,7 @@ int zcash_script_verify(
return set_error(err, zcash_script_ERR_TX_VERSION);
}
// Regardless of the verification result, the tx did not error.
// Regardless of the verification result, the tx did not error.
set_error(err, zcash_script_ERR_OK);
// This is a pre-v5 tx, so the PrecomputedTransactionData constructor
// field `allPrevOutputs` is not used.
@ -182,6 +184,35 @@ int zcash_script_verify(
}
}
int zcash_script_verify_prehashed(
const unsigned char* sighash,
unsigned int sighashLen,
int64_t nLockTime,
uint8_t isFinal,
const unsigned char* scriptPubKey,
unsigned int scriptPubKeyLen,
const unsigned char* scriptSig,
unsigned int scriptSigLen,
int64_t amount,
unsigned int nIn,
unsigned int flags,
uint32_t consensusBranchId,
zcash_script_error* err)
{
try {
CScriptNum nLockTimeNum = CScriptNum(nLockTime);
return VerifyScript(
CScript(scriptSig, scriptSig + scriptSigLen),
CScript(scriptPubKey, scriptPubKey + scriptPubKeyLen),
flags,
PrehashedTransactionSignatureChecker(sighash, sighashLen, nLockTimeNum, isFinal != 0, nIn, amount),
consensusBranchId,
NULL);
} catch (const std::exception&) {
return set_error(err, zcash_script_ERR_TX_DESERIALIZE); // Error deserializing
}
}
int zcash_script_verify_v5(
const unsigned char* txTo,
unsigned int txToLen,
@ -194,8 +225,8 @@ int zcash_script_verify_v5(
{
CTransaction tx;
try {
const char* txToEnd = (const char *)(txTo + txToLen);
RustDataStream stream((const char *)txTo, txToEnd, SER_NETWORK, PROTOCOL_VERSION);
const char* txToEnd = (const char*)(txTo + txToLen);
RustDataStream stream((const char*)txTo, txToEnd, SER_NETWORK, PROTOCOL_VERSION);
stream >> tx;
if (nIn >= tx.vin.size())
return set_error(err, zcash_script_ERR_TX_INDEX);
@ -253,13 +284,13 @@ unsigned int zcash_script_legacy_sigop_count_precomputed(
}
unsigned int zcash_script_legacy_sigop_count(
const unsigned char *txTo,
const unsigned char* txTo,
unsigned int txToLen,
zcash_script_error* err)
{
try {
const char* txToEnd = (const char *)(txTo + txToLen);
RustDataStream stream((const char *)txTo, txToEnd, SER_NETWORK, PROTOCOL_VERSION);
const char* txToEnd = (const char*)(txTo + txToLen);
RustDataStream stream((const char*)txTo, txToEnd, SER_NETWORK, PROTOCOL_VERSION);
CTransaction tx;
stream >> tx;
if (GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) != txToLen) {

View File

@ -133,6 +133,21 @@ EXPORT_SYMBOL int zcash_script_verify(
uint32_t consensusBranchId,
zcash_script_error* err);
EXPORT_SYMBOL int zcash_script_verify_prehashed(
const unsigned char* sighash,
unsigned int sighashLen,
int64_t nLockTime,
uint8_t isFinal,
const unsigned char* scriptPubKey,
unsigned int scriptPubKeyLen,
const unsigned char* scriptSig,
unsigned int scriptSigLen,
int64_t amount,
unsigned int nIn,
unsigned int flags,
uint32_t consensusBranchId,
zcash_script_error* err);
/// Returns 1 if the input nIn of the serialized transaction pointed to by
/// txTo correctly spends the matching output in allPrevOutputs under
/// the additional constraints specified by flags. Must be used for V5 transactions;