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

Implementing BIP340 Tagged Hashes in Go

permalink THAMER
#bitcoin#bitcoin-script#cryptography#golang

GOLANG + BITCOIN SCRIPT — Stage 002, Lesson 08 of 14.

Engineering concept

BIP340 defines tagged hashing as SHA256(SHA256(tag) || SHA256(tag) || message). Distinct tags separate cryptographic contexts. Tags are exact case-sensitive byte strings, not arbitrary labels that can be substituted in a protocol. The example computes the challenge tag on demonstration bytes; it is not a complete Schnorr challenge construction.

Source example

Go · 13 lines
1package main
2
3import (
4	"crypto/sha256"
5	"fmt"
6)
7
8func tagged(tag string, msg []byte) [32]byte {
9	h := sha256.Sum256([]byte(tag))
10	b := append(append([]byte{}, h[:]...), h[:]...)
11	return sha256.Sum256(append(b, msg...))
12}
13func main() { fmt.Printf("%x\n", tagged("BIP0340/challenge", []byte("demo"))) }

Regression test

Go · 15 lines
1package main
2
3import (
4	"crypto/sha256"
5	"testing"
6)
7
8func TestTagged(t *testing.T) {
9	h := sha256.Sum256([]byte("tag"))
10	b := append(append(h[:0:0], h[:]...), h[:]...)
11	b = append(b, 1)
12	if tagged("tag", []byte{1}) != sha256.Sum256(b) || tagged("tag", nil) == tagged("other", nil) {
13		t.Fatal("domain separation")
14	}
15}

Run locally

From the downloaded repository root, enter 002-cryptography/02-tagged-hash.

go test -race .
go run .

All examples are educational and local. Never fund the published fixture keys. No network transaction is created.

Reference

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

Previous stage

https://thamer.org/learn/bitcoin-development-libraries-go

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