THAMER
Sovereign Bitcoin Wallet

WALLET AI BITCOIN

Your Bitcoin Wallet. Your Keys. Your Engine.

Self-hosted Bitcoin software built for developers, applications, and AI agents.

CREATE WALLET

Decoding Minimal Bitcoin Script Numbers in Go

permalink THAMER
#bitcoin#bitcoin-script#golang#opcodes#stack

Numeric operations commonly accept at most four-byte operands. Minimal encoding rules depend on the execution context and flags. Timelock operations can accept five-byte operands.

Go implementation

Go · 20 lines
41func DecodeNum(b []byte, minimal bool) (int64, error) {
42	if len(b) > 4 {
43		return 0, fmt.Errorf("numeric operand too long")
44	}
45	if len(b) == 0 {
46		return 0, nil
47	}
48	if minimal && b[len(b)-1]&127 == 0 && (len(b) == 1 || b[len(b)-2]&128 == 0) {
49		return 0, fmt.Errorf("nonminimal number")
50	}
51	var n int64
52	for i, v := range b {
53		n |= int64(v) << uint(8*i)
54	}
55	if b[len(b)-1]&128 != 0 {
56		n &^= int64(128) << uint(8*(len(b)-1))
57		n = -n
58	}
59	return n, nil
60}

Practical exercise

Decode 0100 with minimal=true and expect an error; with false expect 1.

Local verification

From the repository directory golang-bitcoin/007-script-stack, run:

go test -race -v ./...
go vet ./...
Go · 73 lines
1package scriptstack
2
3import (
4	"bytes"
5	"encoding/hex"
6	"testing"
7)
8
9func TestNumbers(t *testing.T) {
10	for _, v := range []int64{0, 1, -1, 127, 128, -128, 255, -255, 2147483647, -2147483647} {
11		b, e := EncodeNum(v)
12		if e != nil {
13			t.Fatal(e)
14		}
15		n, e := DecodeNum(b, true)
16		if e != nil || n != v {
17			t.Fatalf("%d %x %d %v", v, b, n, e)
18		}
19	}
20	if _, e := DecodeNum([]byte{0x80}, true); e == nil {
21		t.Fatal("negative zero minimal")
22	}
23	if Truth([]byte{0, 128}) || !Truth([]byte{128, 0}) {
24		t.Fatal("truth")
25	}
26}
27func TestPush(t *testing.T) {
28	for _, n := range []int{0, 1, 75, 76, 255, 256, 520} {
29		b := bytes.Repeat([]byte{42}, n)
30		s, e := PushData(b)
31		if e != nil {
32			t.Fatal(e)
33		}
34		st, e := Eval(s)
35		if e != nil || len(st) != 1 || !bytes.Equal(st[0], b) {
36			t.Fatalf("push %d: %v", n, e)
37		}
38	}
39	if _, e := PushData(make([]byte, 521)); e == nil {
40		t.Fatal("oversize")
41	}
42}
43func TestScripts(t *testing.T) {
44	for _, s := range []string{"5253935587", "517687", "51518851", "006951"} {
45		b, _ := hex.DecodeString(s)
46		e := Check(b)
47		if (s == "006951") != (e != nil) {
48			t.Fatalf("%s: %v", s, e)
49		}
50	}
51}
52func TestReject(t *testing.T) {
53	for _, s := range []string{"4c", "4d0100", "4effffffff", "76", "93", "ac", "5151", "00", "515287"} {
54		b, _ := hex.DecodeString(s)
55		if Check(b) == nil {
56			t.Fatalf("accepted %s", s)
57		}
58	}
59	if _, e := Eval(bytes.Repeat([]byte{0x51}, 1001)); e == nil {
60		t.Fatal("stack limit")
61	}
62}
63func TestHash(t *testing.T) {
64	s := []byte{3, 'a', 'b', 'c', 0xa8}
65	st, e := Eval(s)
66	if e != nil || hex.EncodeToString(st[0]) != "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" {
67		t.Fatal(st, e)
68	}
69}
70func FuzzParse(f *testing.F) {
71	f.Add([]byte{0x4c, 1, 42})
72	f.Fuzz(func(t *testing.T, b []byte) { _, _ = Parse(b) })
73}

Scope and security

This offline educational subset is not a Bitcoin consensus engine. It starts with an empty stack, does not validate transactions, signatures, witness programs or conditional branches, and uses deliberately stricter numeric and final-stack rules. Do not use it to authorize real funds. For production validation use established Bitcoin implementations.

References

https://github.com/bitcoin/bitcoin/tree/master/src/script

https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki

https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki

Questions & Answers

Explore all questions →

Latest from the Lab

THAMER illustrated developer portrait and signature

ABOUT THE DEVELOPER

Independent Software Developer specializing in Bitcoin Script, Golang, and AI Agents.

Software engineering · Independent research · Open-source projects