libauth icon indicating copy to clipboard operation
libauth copied to clipboard

Introspection opcode returns garbage when scenario sets version greater than int32

Open A60AB5450353F40E opened this issue 2 months ago • 0 comments

Reproduce for scenario with:

"version": 3953915070, (0xbef4abeb)

OP_TXVERSION returns -341052226 (0x420b5494) when I'd expect it to return -1806431422 (0xbef4abeb)

Found while trying to implement BTC's CTV test vector, import the below template and look at test_0 line 9:

{
  "$schema": "https://libauth.org/schemas/wallet-template-v0.schema.json",
  "description": "https://github.com/bitcoin/bips/blob/master/bip-0119.mediawiki",
  "name": "CHECKTEMPLATEVERIFY",
  "entities": {},
  "scenarios": {
    "test_0_tx": {
      "description": "",
      "name": "test 0",
      "transaction": {
        "version": 3953915070,
        "locktime": 2403554972,
        "inputs": [
          {
            "unlockingBytecode": [
              "slot"
            ],
            "sequenceNumber": 841206716
          }
        ],
        "outputs": [
          {
            "lockingBytecode": "6e238642dd5b75af59bde98b69cafe8099d052237bf841e9f921d5f9f1d116e9037f7e9ec2fcbb0969213a501077b99180f91ffcda99a3ae872ce91a86aafcf0ba5d4195743c7f1d2e37d1a8a3c68bbe9aacfcce85f186fa38c9d7365cea2081ee3ba877c794e6e9a228089d04e64fc7931f04109bb1c0c3fa8b8ddd47b6d89f311abbed387ba1ab6cb0128d5d763d03786285b0760a0abc516fbe28a8761a0fcfa6b0675c783a59ef5558c11850a2ceda1ee1009cbec3a5934217ac65a3332128312aee121fdc0e",
            "valueSatoshis": 9074527893739426000
          }
        ]
      },
      "sourceOutputs": [
        {
          "lockingBytecode": [
            "slot"
          ]
        }
      ]
    }
  },
  "scripts": {
    "def_string_serialize": {
      "name": "def_string_serialize",
      "script": "< // serialize a byte sequence to compactsize(len)+bytes\n    OP_SIZE <1> OP_INVOKE OP_SWAP OP_CAT\n> <2> OP_DEFINE"
    },
    "def_int_to_compactsize": {
      "name": "def_int_to_compactsize",
      "script": "< // Convert int to compact size\n    OP_DUP <253> OP_LESSTHAN\n    OP_IF\n        <2> OP_NUM2BIN <1> OP_SPLIT OP_DROP\n    OP_ELSE\n        OP_DUP <1> <16> OP_LSHIFTNUM OP_LESSTHAN\n        OP_IF\n            <3> OP_NUM2BIN <2> OP_SPLIT OP_DROP\n            <0xfd> OP_SWAP OP_CAT\n        OP_ELSE\n            OP_DUP <1> <32> OP_LSHIFTNUM OP_LESSTHAN\n            OP_IF\n                <5> OP_NUM2BIN <4> OP_SPLIT OP_DROP\n                <0xfe> OP_SWAP OP_CAT\n            OP_ELSE\n                <9> OP_NUM2BIN <8> OP_SPLIT OP_DROP\n                <0xff> OP_SWAP OP_CAT\n            OP_ENDIF\n        OP_ENDIF\n    OP_ENDIF\n> <1> OP_DEFINE"
    },
    "def_int_to_uint4": {
      "name": "def_int_to_uint4",
      "script": "< // Cast int to uint4\n    <5> OP_NUM2BIN <4> OP_SPLIT OP_DROP\n> <0> OP_DEFINE"
    },
    "inv_int_to_uint4": {
      "name": "inv_int_to_uint4",
      "script": "<0> OP_INVOKE"
    },
    "inv_string_serialize": {
      "name": "inv_string_serialize",
      "script": "<2> OP_INVOKE"
    },
    "checktemplateverify_sighash": {
      "name": "checktemplateverify_sighash",
      "script": "// Checktemplateverify sighash\n// https://github.com/bitcoin/bips/blob/master/bip-0119.mediawiki#user-content-Detailed_Specification\n\ndef_int_to_uint4\ndef_int_to_compactsize\ndef_string_serialize\n\n// uint4(nVersion)\nOP_TXVERSION\ninv_int_to_uint4\n<3953915070> inv_int_to_uint4\n// uint4(nLocktime)\nOP_TXLOCKTIME inv_int_to_uint4\nOP_CAT\n// scriptSigs -- must skip, BCH doesn't have SegWit\n// uint4(len(self.vin))\nOP_TXINPUTCOUNT inv_int_to_uint4\nOP_CAT\n\n// inputs loop\n<>\n<0> OP_BEGIN OP_DUP OP_TOALTSTACK\n    // uint4(nSequence)\n    OP_INPUTSEQUENCENUMBER inv_int_to_uint4\n    OP_CAT\nOP_FROMALTSTACK OP_1ADD OP_DUP OP_TXINPUTCOUNT OP_EQUAL OP_UNTIL OP_DROP\n\n// append sequences hash\nOP_SHA256 OP_CAT\n\n// uint4(len(self.vout))\nOP_TXOUTPUTCOUNT inv_int_to_uint4\nOP_CAT\n\n// outputs loop\n<>\n<0> OP_BEGIN OP_DUP OP_TOALTSTACK\n    // OP_DUP OP_OUTPUTVALUE <8> OP_NUM2BIN\n    <9074527893739425514> <8> OP_NUM2BIN\n    OP_SWAP OP_OUTPUTBYTECODE inv_string_serialize OP_CAT\n    OP_CAT\nOP_FROMALTSTACK OP_1ADD OP_DUP OP_TXOUTPUTCOUNT OP_EQUAL OP_UNTIL OP_DROP\n\n// append outputs hash\nOP_SHA256 OP_CAT\n\n// nIn\nOP_INPUTINDEX inv_int_to_uint4 OP_CAT\n\nOP_SHA256",
      "tests": {
        "test_0": {
          "passes": [
            "test_0_tx"
          ],
          "check": "<0x7fd2725ad65f970b0045073b678771bc28f6c3ffd68463a02b3a9b8f5c135dc0>\nOP_EQUAL",
          "name": "test_0",
          "setup": "// https://github.com/bitcoin/bips/blob/master/bip-0119/vectors/ctvhash.json#L1155C20-L1155C1404"
        },
        "test_1": {
          "check": "<0x4699fcfb1fc34fbea6619c9f1c07619a0d4269953d8fbd638d58492ab55e4558>\nOP_EQUAL",
          "name": "test_1",
          "setup": "// https://github.com/bitcoin/bips/blob/master/bip-0119/vectors/ctvhash.json#L1155C20-L1155C1404"
        }
      }
    }
  },
  "supported": [
    "BCH_SPEC"
  ]
}

A60AB5450353F40E avatar Nov 05 '25 15:11 A60AB5450353F40E