Repository: bitcoin/bitcoin (language: cpp)

Issue:
Risczero Fit
### Please describe the feature you'd like to see added.

Support the risczero platfrom by riscv32-unknown-elf-g++. which will bring more prove system for bitcoin.


### Is your feature related to a problem, if so please describe it.

when i compile by riscv32-unknown-elf-g++ compiler, get the below error:

```
blockbody-guest:   cargo:warning=In file included from depend/bitcoin/src/hash.h:14,
blockbody-guest:   cargo:warning=                 from depend/bitcoin/src/script/interpreter.h:9,
blockbody-guest:   cargo:warning=                 from depend/bitcoin/src/script/interpreter.cpp:6:
blockbody-guest:   cargo:warning=depend/bitcoin/src/serialize.h: In instantiation of 'void Serialize(Stream&, const T&) [with Stream = HashWriter; T = int]':
blockbody-guest:   cargo:warning=depend/bitcoin/src/hash.h:144:20:   required from 'HashWriter& HashWriter::operator<<(const T&) [with T = int]'
blockbody-guest:   cargo:warning=depend/bitcoin/src/script/interpreter.cpp:1613:12:   required from 'uint256 SignatureHash(const CScript&, const T&, unsigned int, int, const CAmount&, SigVersion, const PrecomputedTransactionData*) [with T = CTransaction; CAmount = long long int]'
blockbody-guest:   cargo:warning=depend/bitcoin/src/script/interpreter.cpp:1664:36:   required from 'bool GenericTransactionSignatureChecker<T>::CheckECDSASignature(const std::vector<unsigned char>&, const std::vector<unsigned char>&, const CScript&, SigVersion) const [with T = CTransaction]'
blockbody-guest:   cargo:warning=depend/bitcoin/src/script/interpreter.cpp:1785:16:   required from here
blockbody-guest:   cargo:warning=depend/bitcoin/src/serialize.h:776:7: error: request for member 'Serialize' in 'a', which is of non-class type 'const int'
blockbody-guest:   cargo:warning=  776 |     a.Serialize(os);
```

This error is caused by the logic in the SignatureHash function within src/script/interpreter.cpp. The function serializes the data to obtain a hash value, but it fails to find a serialization method for the int type.

```
// Sighash type
ss << nHashType;
```
This behavior is particularly strict in environments like RISC-V, where the ABI (Application Binary Interface) might enforce more rigid type distinctions, leading to errors during compilation.

The `-march=rv32i -mabi=ilp32` for the compiler(riscv32-unknown-elf-g++).

### Describe the solution you'd like

This issue was temporarily bypassed by adding serialization/deserialization code for the int type. Supporting R0 will allow the BTC L2 prover to extend to more proof systems.

Add methods temporarily in the PR
: https://github.com/rust-bitcoin/rust-bitcoinconsensus/pull/97/files; Maybe best solution from the commiunity.

### Describe any alternatives you've considered

_No response_

### Please leave any additional context

_No response_


Discussion / hints:
> This issue was temporarily bypassed by adding serialization/deserialization code for the int type. Supporting R0 will allow the BTC L2 prover to extend to more proof systems.

Interesting. All int widths should already be possible to serialize, according to the code:

```cpp
template<typename Stream> inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); }
template<typename Stream> inline void Serialize(Stream& s, uint16_t a) { ser_writedata16(s, a); }
template<typename Stream> inline void Serialize(Stream& s, int32_t a ) { ser_writedata32(s, a); }
template<typename Stream> inline void Serialize(Stream& s, uint32_t a) { ser_writedata32(s, a); }
template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_writedata64(s, a); }
template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
```

I guess `int` is somehow a distinct type in your environment?

Please be aware that violations in `src/compat/assumptions.h` may lead to incorrectly compiled code.

I see that https://github.com/rust-bitcoin/rust-bitcoinconsensus/blob/0fcec269c6c7fee11b163c6800c998eee9b64df6/depend/bitcoin/src/serialize.h lacks that include, whereas in current master in this repo, it is present.

Can you try to compile `static_assert(std::is_same_v<int, int32_t>);` on your system, as well as `assumptions.h`?

(Godbolt: https://godbolt.org/z/T9bY9PrPx)
Using toolchain from risc0, main.cpp that includes `assumptions.h` compiles successfully, but fails with `static_assert(std::is_same_v<int, int32_t>);`

Actually, the toolchain from risc0 **does** have different type of "int" and "int32_t", see https://github.com/risc0/toolchain/releases/tag/2024.01.05

int32_t is defined in "riscv32-unknown-elf/include/sys/_stdint.h"
```
#ifdef ___int32_t_defined
#ifndef _INT32_T_DECLARED
typedef __int32_t int32_t ;
#define _INT32_T_DECLARED
#endif
#ifndef _UINT32_T_DECLARED
typedef __uint32_t uint32_t ;
#define _UINT32_T_DECLARED
#endif
#define __int32_t_defined 1
#endif /* ___int32_t_defined */
```

Maybe, we should static_cast/reinterpret_cast to int32_t on all "int" types, such as `nHashType`.

Fix patch:
diff --git a/src/script/interpreter.cpp b/src/script/interpreter.cpp
index 9d0e9b5e3cfb2..dcdddb88e96ef 100644
--- a/src/script/interpreter.cpp
+++ b/src/script/interpreter.cpp
@@ -1303,7 +1303,7 @@ class CTransactionSignatureSerializer
         // Serialize the nSequence
         if (nInput != nIn && (fHashSingle || fHashNone))
             // let the others update at will
-            ::Serialize(s, int{0});
+            ::Serialize(s, int32_t{0});
         else
             ::Serialize(s, txTo.vin[nInput].nSequence);
     }
@@ -1565,7 +1565,7 @@ bool SignatureHashSchnorr(uint256& hash_out, ScriptExecutionData& execdata, cons
 }
 
 template <class T>
-uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn, int nHashType, const CAmount& amount, SigVersion sigversion, const PrecomputedTransactionData* cache)
+uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn, int32_t nHashType, const CAmount& amount, SigVersion sigversion, const PrecomputedTransactionData* cache)
 {
     assert(nIn < txTo.vin.size());
 


Test patch: