Class ArkadeScriptNum
Sign-magnitude little-endian encoder/decoder for ArkadeScript numeric pushes,
matching Bitcoin Script's standard CScriptNum serialization rules but
without the 4-byte (or 8-byte) range cap.
public static class ArkadeScriptNum
- Inheritance
-
ArkadeScriptNum
- Inherited Members
Remarks
ArkadeScript opcodes such as OP_ECMULSCALARVERIFY
and OP_TWEAKVERIFY push 32-byte EC scalars on the
stack — these don't fit in a 64-bit integer, so the existing internal
NArk.Core.Helpers.CScriptNum (which is long-backed) can't be
reused directly. This helper covers the wider range while staying byte-
compatible with CScriptNum for values that DO fit in a long.
Encoding rules (Bitcoin CScriptNum::serialize):
- Zero is encoded as the empty byte array.
- Non-zero values are written little-endian. The high bit of the most
significant byte is reserved for the sign —
1 = negative,0 = positive. If the value's natural top bit is already 1, an extra byte is appended to carry the sign and avoid ambiguity. - Decoding is the reverse: an empty array decodes to zero; otherwise the high bit of the last byte is the sign and the remaining bits form the magnitude.
Mirrors the ts-sdk's reliance on @scure/btc-signer's
ScriptNum() serializer, including its 520-byte cap
(MaxBytes = MaxScriptElementSize); vectors must
round-trip 1:1 across SDKs.
Fields
MaxBytes
Maximum encoded length in bytes (= Bitcoin's MaxScriptElementSize).
public const int MaxBytes = 520
Field Value
Methods
Decode(ReadOnlySpan<byte>, bool)
Decode a Bitcoin sign-magnitude LE byte array to a BigInteger.
Empty input decodes to zero. Throws if requireMinimal is
true and the encoding has a redundant zero byte / sign byte, or if the
input exceeds MaxBytes bytes.
public static BigInteger Decode(ReadOnlySpan<byte> bytes, bool requireMinimal = true)
Parameters
bytesReadOnlySpan<byte>requireMinimalbool
Returns
Exceptions
- ArgumentException
bytesexceeds MaxBytes bytes.
Encode(BigInteger)
Encode a BigInteger using Bitcoin sign-magnitude LE.
public static byte[] Encode(BigInteger value)
Parameters
valueBigInteger
Returns
- byte[]
Exceptions
- ArgumentException
The encoded value would exceed MaxBytes bytes.