Skip to content

An implementation of Bitcoin in zig to learn both starting from zero

Notifications You must be signed in to change notification settings

Jonatanc05/zig-bitcoin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Zig Bitcoin

A Bitcoin client with minimal dependencies written in Zig

So I'm following Jimmy Song's book to have some hands-on experience with these topics. Note that it's a learning project not meant for any real use, although I would like to use it some day.

Requirements

Currently building with Zig 0.12.0

Windows 11 and Ubuntu WSL are usually tested.

Screenshots

Currently this is the hole program:

> zig build run

------------- FiniteFields -------------
Element a: 10_F13
Element b: 5_F13
a + b: 2_F13
a - b: 5_F13
b - a: 8_F13
a * b: 11_F13
a ** 2: 9_F13
a ** 3: 12_F13
a / b: 2_F13

------------- EllipticCurves -------------
Point p1: (c0, 69)
Point p2: (11, 38)
p1 + p2: (aa, 8e)
Point p3: (2f, 47)
p3 + p3: (24, 6f)
2 p3: (24, 6f)
3 p3: (f, 89)
18 p3: (f, 56)
19 p3: (24, 70)
20 p3: (2f, 98)
21 p3: (inf, inf)
order of the group generated by (f, 56): 7

-------------- Cryptography --------------
message: "The quick brown fox jumps over the lazy dog"
h(message): d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592
Public key: (235ac91b1242539c725afc87f7fdee468111662b134bbae397b194d32a4a4d30, 85213d6fd0232b58a02588c4575fbaf5879a0c8b4e98284b8f76081827e96904)
Private key: 1ee1fcd5ec7171e113d5955357589d5893d4e9c6911a4c17b382da9abefe1c35
Signing the message...
Verifying the signature...
valid: true

-------------- Serialization --------------
p1.x: 0x8b1d28e29c07f93e00531b199c5db7e053a8be9507c35a8b0b4a3536192a281e
p1.y: 0x1fdd88952ef28c81369cf00a7204d9d08cf58d38c0f97ec124a893b8c98d3516
serialized(p1): 0x048b1d28e29c07f93e00531b199c5db7e053a8be9507c35a8b0b4a3536192a281e1fdd88952ef28c81369cf00a7204d9d08cf58d38c0f97ec124a893b8c98d3516
parsed(serialized(p1)) == p1: true
compressed(p1): 0x028b1d28e29c07f93e00531b199c5db7e053a8be9507c35a8b0b4a3536192a281e
parsed(compressed(p1)) == p1: true

u8_array: { 0, 0, 4, 9, a, f, 1a, ff }
Base58.encode(u8_array): 31Yr1PVY
Base58.decode(encoded): { 4, 9, a, f, 1a, ff }
Decoded equals original: true

------------ Generating BTC Address ------------
testnet: true
prvkey: f45e6907b16670196e487cf667e9fa510f0593276335da22311eb67c90d46421
pubkey (SEC compressed): { 2, 9a, df, 93, 12, f8, f1, 2c, 38, 66, ee, ca, 8b, 8f, 71, 61, 9c, de, cf, e7, f9, 6f, cb, f0, 85, 1c, 95, ca, 5, 79, 11, 7a, 1b }
address: muAwhTfQdaXZrTGKRqkWA28mmWQxDH23CE

------------------- Transactions -------------------

-----> Serialization
bitcoin.Tx{ .version = 1, .inputs = { bitcoin.Tx.TxInput{ .txid = 28636405658344580202443952903644473261407019314280778112116486762997828230779, .index = 0, .script_sig = { ... }, .sequence = 4294967295 } }, .outputs = { bitcoin.Tx.TxO
utput{ .amount = 4999990000, .script_pubkey = { ... } } }, .witness = null, .locktime = 0 }

-----> Serialization 2 (a real transaction on https://mempool.space/signet/tx/bd9d8ea4a30d9465159f199c48acda11441d8bcd66020ad55a1215015431bb18)
bitcoin.Tx{ .version = 2, .inputs = { bitcoin.Tx.TxInput{ .txid = 99923091109735182865009530668685512478133164262303979925212552921425228836392, .index = 1, .script_sig = { ... }, .sequence = 4294967293 } }, .outputs = { bitcoin.Tx.TxO
utput{ .amount = 250249727769, .script_pubkey = { ... } }, bitcoin.Tx.TxOutput{ .amount = 6272, .script_pubkey = { ... } } }, .witness = { { 102, 187, 255, 212, 218, 253, 1, 114, 131, 167, 19, 46, 255, 159, 112, 241, 236, 188, 92, 102,
 56, 195, 27, 117, 110, 97, 166, 166, 62, 7, 47, 230, 245, 74, 196, 253, 105, 164, 6, 16, 223, 5, 239, 191, 8, 167, 59, 181, 133, 34, 124, 215, 93, 153, 181, 168, 211, 84, 27, 193, 4, 229, 16, 80 } }, .locktime = 0 }

-----> Script
answer: "And he answering said, Thou shalt love the Lord thy God with all thy heart, and with all thy soul, and with all thy strength, and with all thy mind; and thy neighbour as thyself."
script_pub_key: OP_SHA256, <Sha256(answer)>, OP_EQUAL, OP_VERIFY
script_pub_key in hex: a82085dc4bf5d38b3435f1f4175686b193fa99b5baeb365077d12e95aeefe13573338769
script_sig: OP_PUSHDATA1, <answer>
script_sig in hex: 4cb2416e6420686520616e73776572696e6720736169642c2054686f75207368616c74206c6f766520746865204c6f72642074687920476f64207769746820616c6c207468792068656172742c20616e64207769746820616c6c2074687920736f756c2c20616e6420776974
6820616c6c2074687920737472656e6774682c20616e64207769746820616c6c20746879206d696e643b20616e6420746879206e65696768626f75722061732074687973656c662e
valid: true

-----> Signing a transaction
entire transaction: 0100000001d21f9779fe7e46a268bfd1c9a97cb521d5751df5b7f8c107ea1ba5a970740638010000006b483045022100dd55fe1b7d3d098e76c316dc37914b028c64389ce35b4ec0ee88d186ac12774e0220024869b05160f2c2b1c438e063efe1f47de29486094938ebf3b
fb0f89f9384ae0121028421e5320bb4116e380b6fc3e87b677324dffd03643099727dbb2da0a22d8f1dfdffffff0188130000000000001976a9145144cef2fbd65ccd75539e5ea6e755b66ee4d26e88ac00000000

Note that the above transaction (with a different signature, of course) was successfully broadcasted to signet with the TXID d5cf8e758abc178121736c9cbb0defe075ef50da4dfb4e736b19f2a2ff66dd14

-----> Validating transaction signature
OP_CHECKSIG result: valid

About

An implementation of Bitcoin in zig to learn both starting from zero

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published