-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
374 lines (354 loc) · 30 KB
/
Copy pathindex.html
File metadata and controls
374 lines (354 loc) · 30 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>bitcoin-kernel - the rules of Bitcoin, tested live in your browser</title>
<meta name="description" content="A test runner for Bitcoin's consensus rules, running live in your browser across all of them: headers, difficulty, blocks, transactions, scripts and light-client proofs. Real test vectors, including Bitcoin Core's own script vectors, run on the page and explained.">
<meta property="og:title" content="bitcoin-kernel">
<meta property="og:description" content="The rules of Bitcoin, tested live in your browser. Headers, difficulty, blocks, transactions, scripts and proofs, all from real test vectors.">
<meta property="og:type" content="website">
<meta property="og:url" content="https://bitcoin-kernel.com/">
<meta property="og:image" content="https://bitcoin-kernel.com/og.png?v=4">
<meta property="og:image:width" content="1200">
<meta property="og:image:height" content="630">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="bitcoin-kernel">
<meta name="twitter:description" content="The rules of Bitcoin, tested live in your browser, from real test vectors.">
<meta name="twitter:image" content="https://bitcoin-kernel.com/og.png?v=4">
<style>
:root{--bg:#fff;--fg:#16181d;--mut:#5b6470;--bd:#e6e8eb;--pan:#fafbfc;--ac:#e8830c;--ac2:#0969da;--good:#1a7f37;--bad:#cf222e;--mono:ui-monospace,SFMono-Regular,Menlo,Consolas,monospace}
*{box-sizing:border-box}html{scroll-behavior:smooth}
body{margin:0;background:var(--bg);color:var(--fg);font:16px/1.6 system-ui,-apple-system,"Segoe UI",sans-serif;-webkit-font-smoothing:antialiased}
a{color:var(--ac2);text-decoration:none}a:hover{text-decoration:underline}
.wrap{max-width:940px;margin:0 auto;padding:0 1.5rem}
nav{border-bottom:1px solid var(--bd)}
nav .wrap{display:flex;align-items:center;gap:1.3rem;height:56px}
nav .brand{font-weight:700;letter-spacing:-.3px;margin-right:auto;color:var(--fg);font-size:1rem;text-decoration:none}nav .brand b{color:var(--ac)}
nav a{color:var(--mut);font-size:.9rem;font-weight:500}nav a.on{color:var(--fg)}
.note{margin:1.1rem 0 0;padding:.7rem 1rem;background:var(--pan);border:1px solid var(--bd);border-radius:10px;font-size:.9rem;color:var(--mut);line-height:1.55}
.note b{color:var(--fg)}
header.hero{padding:3.2rem 0 1.2rem;text-align:center}
h1{font-size:2.7rem;line-height:1.05;letter-spacing:-1.2px;margin:0;font-weight:800}
.sub{font-size:1.14rem;color:#37414d;margin:1rem auto 0;max-width:38rem;line-height:1.5}
.shead{text-align:center;font-size:1.9rem;font-weight:800;letter-spacing:-.6px;margin:2.6rem 0 0}
#runner{margin:1rem 0 0;border:1px solid var(--bd);border-radius:16px;overflow:hidden;box-shadow:0 8px 30px rgba(16,18,29,.06)}
.rhead{padding:1.4rem;text-align:center;border-bottom:1px solid var(--bd);background:var(--pan)}
.summary{font-size:1.05rem;color:var(--mut)}
.summary .big{font:800 1.9rem var(--mono);letter-spacing:-1px;color:var(--good);vertical-align:-2px}
.summary .big.bad{color:var(--bad)}.summary .zero{color:var(--mut);font-family:var(--mono)}
#counter{font:800 1.9rem var(--mono);color:var(--fg)}
#status{margin:.5rem 0 0;font-size:.92rem;color:var(--mut)}#status b{color:var(--fg)}
.spin{display:inline-block;width:.8em;height:.8em;border:2px solid var(--bd);border-top-color:var(--ac);border-radius:50%;animation:sp .7s linear infinite;vertical-align:-1px}
@keyframes sp{to{transform:rotate(360deg)}}
.runbody{opacity:0;transition:opacity .3s}#runner.ready .runbody{opacity:1}
.tabs{display:flex;flex-wrap:wrap;gap:.5rem;padding:1rem 1.2rem;border-bottom:1px solid var(--bd)}
.tab{display:flex;align-items:center;gap:.5rem;border:1px solid var(--bd);background:#fff;border-radius:99px;padding:.45rem .9rem;font:inherit;font-size:.9rem;color:var(--fg);cursor:pointer;transition:border-color .12s,background .12s}
.tab:hover{border-color:var(--ac)}.tab.on{border-color:var(--ac);background:#fff7ee}
.tab .tc{font:700 .8rem var(--mono);color:var(--ac)}
.suitehead{padding:1.1rem 1.4rem .2rem}
#suite-title{margin:0;font-size:1.2rem;letter-spacing:-.3px}
#suite-desc{margin:.3rem 0 0;color:var(--mut);font-size:.95rem;line-height:1.5}
.searchrow{display:flex;align-items:center;gap:.8rem;padding:.9rem 1.4rem}
#search{flex:1;border:1px solid var(--bd);border-radius:8px;padding:.5rem .7rem;font:.88rem var(--mono);color:var(--fg)}
#suite-count{font:.78rem var(--mono);color:var(--mut);white-space:nowrap}
.legend{padding:0 1.4rem .8rem;font:.82rem/1.5 var(--mono);color:var(--mut)}.legend b.g{color:var(--good)}.legend b{color:var(--fg)}
#list{max-height:34rem;overflow:auto;border-top:1px solid var(--bd)}
.row{display:flex;align-items:center;gap:.85rem;padding:.55rem 1.4rem;cursor:pointer;border-top:1px solid var(--bd);border-left:3px solid transparent}
.row:first-child{border-top:none}.row:hover{background:var(--pan)}.row.open{background:var(--pan);border-left-color:var(--ac)}
.v{color:var(--good);font:700 .95rem var(--mono);flex-shrink:0}.v.no{color:var(--bad)}
.lab{flex:1;font-size:.92rem;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.exp{font:600 .68rem var(--mono);text-transform:uppercase;border-radius:5px;padding:.13em .5em;flex-shrink:0}
.exp.pass{background:#e6f4ea;color:var(--good)}.exp.fail{background:#fce8e8;color:var(--bad)}
.detail{display:none}.detail.open{display:block;padding:.4rem 1.4rem 1.1rem 3rem;background:var(--pan)}
.card{border:1px solid var(--bd);border-radius:10px;overflow:hidden;background:#fff}
.dl{display:flex;border-top:1px solid var(--bd)}.dl:first-child{border-top:none}
.dk{width:14rem;flex-shrink:0;padding:.6rem .9rem;font:.76rem var(--mono);color:var(--mut);background:var(--pan)}
.dv{padding:.6rem .9rem;font-size:.9rem;overflow:hidden;word-break:break-word}
.dmono{font:.82rem/1.5 var(--mono);word-break:break-all}
.dasm{font:.84rem/1.5 var(--mono);color:var(--fg);word-break:break-word}
.dhex{font:.72rem/1.4 var(--mono);color:var(--mut);word-break:break-all;margin-top:.3rem}
.match{font:600 .72rem var(--mono);color:var(--good);margin-left:.5rem}.match.bad{color:var(--bad)}
.how{margin:3.4rem 0 0}
.how3{display:grid;grid-template-columns:repeat(3,1fr);gap:1.6rem;margin-top:1.6rem}
.how3 h3{margin:0 0 .35rem;font-size:1.05rem;letter-spacing:-.2px}
.how3 p{margin:0;color:var(--mut);font-size:.92rem;line-height:1.55}
@media(max-width:640px){.how3{grid-template-columns:1fr;gap:1.1rem}}
footer{margin-top:2.4rem;padding:2rem 0;border-top:1px solid var(--bd);color:var(--mut);font-size:.85rem}
footer .links{display:flex;gap:1.4rem;flex-wrap:wrap;margin-bottom:.9rem}
.fnote{font:.8rem/1.6 var(--mono);color:var(--mut);max-width:54rem}
@media(max-width:600px){h1{font-size:2.1rem}.dk{width:8rem}}
</style>
</head>
<body>
<nav><div class="wrap">
<a href="./index.html" class="brand">bitcoin<b>·</b>kernel</a>
<a href="./index.html" class="on">Test suite</a>
<a href="./random.html">Verify a block</a>
<a href="./cache.html">Your node</a>
<a href="./spec.html">Specification</a>
<a href="https://github.com/bitcoin-kernel/bitcoin-kernel.github.io">Source ↗</a>
</div></nav>
<div class="wrap"><p class="note"><b>Note:</b> An independent implementation of the Bitcoin kernel concept, a standalone consensus engine separated from wallet and networking, written in JavaScript for browsers and phones. Not affiliated with Bitcoin Core's <a href="https://thecharlatan.ch/Kernel/">libbitcoinkernel</a> and shares none of its code. For learning and light-client use, not a substitute for a full node.</p></div>
<header class="hero"><div class="wrap">
<h1>The rules of Bitcoin.</h1>
<p class="sub">Every header, block and payment has to follow them, or the network throws it out. Here is a test suite that checks those rules, all of them, running live in your browser from real test vectors.</p>
</div></header>
<div class="wrap">
<h2 class="shead">The consensus test suite</h2>
<div id="runner">
<div class="rhead">
<div id="summary" class="summary"><span id="counter">0</span> tests</div>
<p id="status"><span class="spin"></span> starting</p>
</div>
<div class="runbody">
<div class="tabs"><button class="tab" data-k="headers"><span class="tn">Headers</span><span class="tc" id="tc-headers">…</span></button><button class="tab" data-k="difficulty"><span class="tn">Difficulty</span><span class="tc" id="tc-difficulty">…</span></button><button class="tab" data-k="blocks"><span class="tn">Blocks</span><span class="tc" id="tc-blocks">…</span></button><button class="tab" data-k="money"><span class="tn">Transactions & money</span><span class="tc" id="tc-money">…</span></button><button class="tab" data-k="scripts"><span class="tn">Spending (scripts)</span><span class="tc" id="tc-scripts">…</span></button><button class="tab" data-k="spv"><span class="tn">Light clients (SPV)</span><span class="tc" id="tc-spv">…</span></button></div>
<div class="suitehead"><h2 id="suite-title">…</h2><p id="suite-desc"></p></div>
<div class="searchrow"><input id="search" placeholder="search this suite" autocomplete="off"><span id="suite-count"></span></div>
<div class="legend"><b class="g">PASS</b> means bitcoin-kernel computed the correct result for that vector: it accepted what the network accepts and rejected what the network rejects. Click any test to see the vector and the result.</div>
<div id="list"></div>
</div>
</div>
<div class="how">
<h2 class="shead">How it works</h2>
<div class="how3">
<div><h3>The rules</h3><p>Bitcoin has a fixed set of consensus rules that decide whether a header, block or payment is valid. Every node on the network enforces the same ones.</p></div>
<div><h3>The library</h3><p>bitcoin-kernel is a JavaScript library that implements those rules from scratch. The same code runs on a Node server and right here in a browser tab.</p></div>
<div><h3>The test suite</h3><p>It ships with real <a href="https://github.com/bitcoin/bitcoin/blob/master/src/test/data/script_tests.json">test vectors</a>: Bitcoin Core's script vectors, real mainnet headers, blocks and proofs. This page runs the whole suite live, above.</p></div>
</div>
</div>
</div>
<footer><div class="wrap">
<div class="links">
<a href="https://github.com/bitcoin-kernel/bitcoin-kernel.github.io">This page's source</a>
<a href="https://cdn.jsdelivr.net/gh/bitcoin-kernel/[email protected]/packages/kernel/codec/interpreter.js">The code that runs the tests</a>
<a href="https://github.com/bitcoin/bitcoin/blob/master/src/test/data/script_tests.json">Bitcoin Core's script vectors</a>
<a href="https://github.com/bitcoin-kernel/bitcoin-kernel.github.io">bitcoin-kernel</a>
</div>
<p class="fnote">Everything runs in your browser with no external calls. The vectors and the code are copied into this repository (v0.0.24); nothing is fetched at runtime.</p>
</div></footer>
<script type="module">
import { Codec } from 'https://cdn.jsdelivr.net/gh/bitcoin-kernel/[email protected]/packages/kernel/codec/codec.js';
import { ScriptEngine } from 'https://cdn.jsdelivr.net/gh/bitcoin-kernel/[email protected]/packages/kernel/codec/script.js';
import { ScriptInterpreter } from 'https://cdn.jsdelivr.net/gh/bitcoin-kernel/[email protected]/packages/kernel/codec/interpreter.js';
import { HeaderEngine } from 'https://cdn.jsdelivr.net/gh/bitcoin-kernel/[email protected]/packages/kernel/codec/headers.js';
import { BlockEngine } from 'https://cdn.jsdelivr.net/gh/bitcoin-kernel/[email protected]/packages/kernel/codec/blocks.js';
import { SpvEngine } from 'https://cdn.jsdelivr.net/gh/bitcoin-kernel/[email protected]/packages/kernel/codec/spv.js';
const $ = (id) => document.getElementById(id);
const esc = (s) => String(s).replace(/&/g, '&').replace(/</g, '<');
const j = (p) => fetch(p).then((r) => r.json());
const SUITE_NAME = { headers: 'Headers', difficulty: 'Difficulty', blocks: 'Blocks', money: 'Transactions & money', scripts: 'Spending (scripts)', spv: 'Light clients (SPV)' };
const SUITE_DESC = {
headers: 'Each block header links to the one before it, its proof of work meets the target, and its timestamp is sane. These run against real mainnet headers.',
difficulty: 'Every 2016 blocks Bitcoin retunes mining difficulty so blocks keep arriving about every ten minutes. These reproduce real retargets from the chain.',
blocks: 'A valid block has one coinbase first, a header that commits to every transaction, no duplicates, and stays within the size and signature limits.',
money: 'The block reward halves on schedule, the supply is capped at 21 million, and every transaction in a real block is well-formed.',
scripts: "How coins are locked and unlocked. Every one of Bitcoin Core's script test vectors: valid spends should pass, forged or malformed ones should be rejected.",
spv: 'A phone can prove a transaction sits inside a block from a short proof, without downloading the chain.',
};
const mono = (v, c) => '<span class="dmono"' + (c ? ' style="color:' + c + '"' : '') + '>' + esc(v) + '</span>';
const GOOD = '#1a7f37', BAD = '#cf222e';
const kvCard = (rows) => '<div class="card">' + rows.map(([k, v]) => '<div class="dl"><div class="dk">' + k + '</div><div class="dv">' + v + '</div></div>').join('') + '</div>';
const pretty = (s) => s.replace(/-/g, ' ').replace(/^./, (c) => c.toUpperCase());
// every test is { name, ok, expect: 'pass'|'reject'|null, detail: () => html }
const bySuite = { headers: [], difficulty: [], blocks: [], money: [], scripts: [], spv: [] };
const add = (suite, t) => bySuite[suite].push(t);
try {
$('status').innerHTML = '<span class="spin"></span> loading test vectors';
const [core, proof, scriptSchema, chainSchema, validate] = await Promise.all([
j('./engine/schema/core.jsonld'), j('./engine/schema/proof.jsonld'), j('./engine/schema/script.jsonld'),
j('./engine/schema/chain.jsonld'), j('./engine/schema/validate.jsonld'),
]);
const codec = new Codec(core, proof);
const he = HeaderEngine.fromSchemas(codec, chainSchema, validate);
const be = BlockEngine.fromSchemas(codec, chainSchema, validate, scriptSchema);
const spv = SpvEngine.fromSchemas(codec, validate);
const se = ScriptEngine.fromSchemas(scriptSchema, chainSchema);
const interp = new ScriptInterpreter(codec, se, scriptSchema['@graph'].find((n) => n['@id'] === 'btc:scriptLimits'));
$('status').innerHTML = '<span class="spin"></span> running the suites';
// =================== HEADERS (real mainnet headers) ===================
try {
const ch = await j('./engine/vectors/header-chain-100k.json');
const hs = ch.headers.map((h) => codec.decode('BlockHeader', h));
const start = Number(ch.startHeight) + 11;
const rows = he.validateChain(hs.slice(11), { startHeight: start });
for (const r of rows) {
const checks = (r.results || []).filter((x) => x.ok !== null);
const okk = checks.every((x) => x.ok !== false);
add('headers', {
name: 'Header at height ' + r.height.toLocaleString() + ' is valid', ok: okk, expect: null,
detail: () => kvCard([
['block hash', mono(r.hash, GOOD)],
['timestamp', new Date(r.time * 1000).toUTCString()],
['difficulty (bits)', mono('0x' + r.bits.toString(16))],
['rules checked', checks.map((x) => '<div>' + (x.ok === false ? '✗' : '✓') + ' ' + esc(pretty(x.label)) + '</div>').join('')],
]),
});
}
const g = await j('./engine/vectors/genesis-block.json');
const ghdr = codec.decode('BlockHeader', g.hex.slice(0, 160));
const ghash = codec.blockHash(ghdr);
add('headers', {
name: 'The genesis block hash is below its mining target', ok: ghash === g.expected.hash && BigInt('0x' + ghash) <= codec.expandCompact(ghdr.bits), expect: null,
detail: () => kvCard([
['the block', "Bitcoin's genesis block, 3 January 2009"],
['hash (double SHA-256 of the header)', mono(ghash, GOOD)],
['mining target', mono('0x' + codec.expandCompact(ghdr.bits).toString(16).padStart(64, '0'))],
['below target?', '<b style="color:' + GOOD + '">yes</b>, the proof of work holds. Check this hash on any explorer.'],
]),
});
} catch (e) { console.error('headers', e); }
// =================== DIFFICULTY (real retargets) ===================
for (const [file, label, height] of [['retarget-32256.json', 'The first-ever difficulty retarget (block 32,256)', 32256], ['retarget-modern.json', 'A modern difficulty retarget (block 951,552)', 951552]]) {
try {
const rt = await j('./engine/vectors/' + file);
const first = codec.decode('BlockHeader', rt.epochFirst);
const last = codec.decode('BlockHeader', rt.epochLast);
const next = codec.decode('BlockHeader', rt.next);
const bits = he.expectedBits(last, Number(rt.epochLastHeight), first);
const days = ((last.time - first.time) / 86400).toFixed(2);
add('difficulty', {
name: label + ' matches the network', ok: bits === next.bits, expect: null,
detail: () => kvCard([
['the epoch', 'blocks ' + Number(rt.epochFirstHeight).toLocaleString() + ' to ' + Number(rt.epochLastHeight).toLocaleString() + ', 2016 blocks'],
['time they took', days + ' days (target is 14.00)'],
['new difficulty we computed', mono('0x' + bits.toString(16), GOOD)],
['what block ' + height.toLocaleString() + ' actually used', mono('0x' + next.bits.toString(16), bits === next.bits ? GOOD : BAD)],
]),
});
} catch (e) { console.error('difficulty', file, e); }
}
// =================== BLOCKS (a real block, height 100000) ===================
let block100k = null;
try {
const pw = await j('./engine/vectors/pruned-window-100000.json');
const blk = codec.decode('Block', pw.blocks[0]);
block100k = blk;
const hdr = blk.header || blk;
const st = be.validateBlockStructure(blk);
const NAMES = { 'coinbase-first': 'The coinbase is the first transaction', 'coinbase-single': 'There is exactly one coinbase', 'merkle-root': 'The header commits to every transaction (merkle root)', 'no-duplicate-tx': 'No duplicate transactions', 'duplicate-txid': 'No duplicate transactions (BIP 30)', 'sigop-limit': 'Within the signature-operation limit', 'block-weight': 'Within the block weight limit', 'block-size': 'Within the block size limit' };
for (const x of (st.results || [])) {
add('blocks', {
name: NAMES[x.label] || pretty(x.label), ok: x.ok !== false, expect: null,
detail: () => kvCard([['rule', mono(x.rule || x.label)], ['block', 'a real mainnet block with ' + blk.transactions.length + ' transactions'], ['result', x.ok === false ? '<b style="color:' + BAD + '">fail</b>' : '<b style="color:' + GOOD + '">pass</b>']]),
});
}
const txids = blk.transactions.map((t) => codec.txid(t));
const root = codec.merkleRoot(txids);
add('blocks', {
name: "Rebuilding the merkle root from the block's transactions", ok: root === hdr.merkleRoot, expect: null,
detail: () => kvCard([
['transactions', blk.transactions.length + ' in the block'],
['merkle root we rebuilt', mono(root, GOOD)],
['root in the header', mono(hdr.merkleRoot, root === hdr.merkleRoot ? GOOD : BAD)],
['', 'Change one satoshi in any transaction and this root would not match.'],
]),
});
} catch (e) { console.error('blocks', e); }
// =================== TRANSACTIONS & MONEY ===================
try {
for (const [h, btc] of [[0, 50], [210000, 25], [420000, 12.5], [840000, 3.125]]) {
const sub = be.subsidy(h) / 1e8;
add('money', { name: 'Block reward at height ' + h.toLocaleString() + ' is ' + btc + ' BTC', ok: sub === btc, expect: null, detail: () => kvCard([['height', h.toLocaleString()], ['block reward', mono(sub + ' BTC', GOOD)], ['rule', 'halves every 210,000 blocks, capping the supply near 21 million']]) });
}
const g = await j('./engine/vectors/genesis-block.json');
const gblk = codec.decode('Block', g.hex);
const cb = gblk.transactions[0].outputs.reduce((s, o) => s + o.value, 0);
add('money', { name: 'The genesis coinbase paid exactly the 50 BTC reward', ok: cb === 5000000000, expect: null, detail: () => kvCard([['genesis coinbase', mono((cb / 1e8).toFixed(8) + ' BTC', GOOD)], ['', 'No transaction may ever pay out more than it takes in. This is the only way new bitcoin is created.']]) });
if (block100k) {
block100k.transactions.forEach((tx, i) => {
const v = be.validateTransaction(tx, i === 0);
const okk = !v || v.ok !== false;
add('money', { name: (i === 0 ? 'The coinbase transaction' : 'Transaction ' + (i + 1)) + ' in block 100,000 is well-formed', ok: okk, expect: null, detail: () => kvCard([['transaction id', mono(codec.txid(tx))], ['inputs / outputs', tx.inputs.length + ' in, ' + tx.outputs.length + ' out'], ['result', okk ? '<b style="color:' + GOOD + '">well-formed</b>' : '<b style="color:' + BAD + '">invalid</b>']]) });
});
}
} catch (e) { console.error('money', e); }
// =================== LIGHT CLIENTS (SPV) ===================
for (const [file, label] of [['merkleblock-block100000.json', 'A payment is proven to be in block 100,000'], ['merkleblock-first-segwit.json', 'The first-ever SegWit payment is proven in its block']]) {
try {
const mbv = await j('./engine/vectors/' + file);
const mb = codec.decode('MerkleBlock', mbv.hex);
const verdict = spv.verify(mb, { txid: mbv.txid });
add('spv', {
name: label, ok: verdict.ok === true, expect: null,
detail: () => kvCard([
['the claim', 'transaction ' + mono(mbv.txid) + ' is in this block'],
['the proof', 'a short list of hashes, not the whole block'],
['what a phone does', 'recompute the merkle root from the transaction and those hashes, and compare it to the header'],
['result', verdict.ok ? '<b style="color:' + GOOD + '">proven</b>' : '<b style="color:' + BAD + '">failed</b>'],
]),
});
} catch (e) { console.error('spv', file, e); }
}
// =================== SPENDING: SCRIPTS (Bitcoin Core's vectors) ===================
await runScripts(codec, se, interp, scriptSchema);
// ---- finish: counts + summary ----
let passed = 0, failed = 0;
for (const k of Object.keys(bySuite)) { const el = $('tc-' + k); if (el) el.textContent = bySuite[k].length.toLocaleString(); for (const t of bySuite[k]) { if (t.ok) passed++; else failed++; } }
const TOTAL = passed + failed;
$('summary').className = failed === 0 ? 'summary ok' : 'summary bad';
$('summary').innerHTML = '<span class="big">' + (failed === 0 ? '✓' : '✗') + ' ' + passed.toLocaleString() + '</span> passing' + (failed ? ' <span class="big bad">· ' + failed + ' failing</span>' : ' <span class="zero">· 0 failing</span>');
$('status').innerHTML = 'Ran <b>' + TOTAL.toLocaleString() + '</b> test vectors across <b>6</b> suites, live in your browser.';
console.log('%cbitcoin-kernel', 'color:#e8830c;font-weight:bold', passed, 'passing,', failed, 'failing.');
wireExplorer();
$('runner').classList.add('ready');
} catch (e) {
$('status').innerHTML = 'Could not run: ' + esc(e && e.message || String(e));
console.error(e);
}
// ---------- scripts suite: the differential against Core's vectors ----------
async function runScripts(codec, se, interp, scriptSchema) {
const SCAT = { logic: 'logic', signatures: 'signature', multisig: 'multisig', encoding: 'encoding', segwit: 'segwit', p2sh: 'p2sh' };
const raw = await j('./engine/vectors/script_tests.json');
const N2C = new Map();
for (const m of scriptSchema['@graph'].find((n) => n['@id'] === 'btc:Opcode').members) { N2C.set(m.name, m.code); N2C.set(m.name.replace(/^OP_/, ''), m.code); }
const hx = (b) => [...b].map((x) => x.toString(16).padStart(2, '0')).join('');
const sn = (n) => { if (n === 0n) return []; const g = n < 0n; let a = g ? -n : n; const o = []; while (a > 0n) { o.push(Number(a & 0xffn)); a >>= 8n; } if (o[o.length - 1] & 0x80) o.push(g ? 0x80 : 0); else if (g) o[o.length - 1] |= 0x80; return o; };
const pd = (b) => { const n = b.length; if (n < 76) return [n, ...b]; if (n <= 255) return [76, n, ...b]; if (n <= 65535) return [77, n & 255, n >> 8, ...b]; return [78, n & 255, (n >> 8) & 255, (n >> 16) & 255, (n >>> 24) & 255, ...b]; };
const ps = (s) => { const o = []; for (const w of s.split(/\s+/).filter(Boolean)) { if (/^-?\d+$/.test(w)) { const n = BigInt(w); if (n === 0n) o.push(0); else if (n === -1n) o.push(0x4f); else if (n >= 1n && n <= 16n) o.push(0x50 + Number(n)); else o.push(...pd(sn(n))); } else if (/^0x[0-9a-fA-F]*$/.test(w)) { const h = w.slice(2); for (let i = 0; i < h.length; i += 2) o.push(parseInt(h.slice(i, i + 2), 16)); } else if (/^'.*'$/.test(w)) o.push(...pd([...w.slice(1, -1)].map((c) => c.charCodeAt(0)))); else if (N2C.has(w)) o.push(N2C.get(w)); else throw 0; } return hx(Uint8Array.from(o)); };
const TIMELOCK = /CHECKLOCKTIMEVERIFY|CHECKSEQUENCEVERIFY/;
const STRUCT = new Set(['WITNESS_UNEXPECTED', 'WITNESS_MALLEATED', 'WITNESS_MALLEATED_P2SH', 'WITNESS_PROGRAM_WRONG_LENGTH', 'WITNESS_PROGRAM_WITNESS_EMPTY', 'WITNESS_PROGRAM_MISMATCH', 'DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM']);
const ENC_ERR = /MINIMALDATA|SCRIPTNUM|SIG_DER|PUBKEYTYPE|SIG_HIGH_S|SIG_HASHTYPE|NULLDUMMY|CLEANSTACK|SIG_PUSHONLY|NULLFAIL|MINIMALIF/;
const spend = (sig, spk, amount, wit) => { const c = { version: 1, lockTime: 0, inputs: [{ prevout: { txid: '00'.repeat(32), vout: 0xffffffff }, scriptSig: '0000', sequence: 0xffffffff }], outputs: [{ value: amount, scriptPubKey: spk }] }; const sp = { version: 1, lockTime: 0, inputs: [{ prevout: { txid: codec.txid(c), vout: 0 }, scriptSig: sig, sequence: 0xffffffff }], outputs: [{ value: amount, scriptPubKey: '' }] }; if (wit) sp.witness = [wit]; return sp; };
const asm = (h) => { try { return h ? se.asm(h) : '(empty script)'; } catch { return h; } };
const cat = (sig, spk, ph, flags, expected, wit) => { const s = sig + ' ' + spk; if (wit || /WITNESS/.test(flags)) return 'segwit'; if (/CHECKMULTISIG/.test(s)) return 'multisig'; if (/CHECKSIG/.test(s)) return 'signatures'; if (/P2SH/.test(flags) && se.classify(ph).type === 'p2sh') return 'p2sh'; if (ENC_ERR.test(expected) || /MINIMALDATA/.test(flags)) return 'encoding'; return 'logic'; };
await new Promise((done) => { let i = 0; (function chunk() { const t0 = performance.now(); while (i < raw.length && performance.now() - t0 < 14) { const t = raw[i++]; if (!t || t.length < 4) continue; let wit = null, amount = 0, sig, spk, flags, expected, comment; if (Array.isArray(t[0])) { wit = t[0].slice(0, -1); amount = Math.round(t[0][t[0].length - 1] * 1e8); [, sig, spk, flags, expected, comment] = t; } else { [sig, spk, flags, expected, comment] = t; } if (TIMELOCK.test(sig) || TIMELOCK.test(spk) || STRUCT.has(expected)) continue; let sh, ph; try { sh = ps(sig); ph = ps(spk); } catch { continue; } if (/P2SH/.test(flags) && se.classify(ph).type === 'p2sh' && /^(4c|4d|4e)/.test(ph.slice(2, 4))) continue; const fset = new Set(flags.split(/[, ]+/).filter(Boolean)); let ours; try { const r = interp.verifyInput(spend(sh, ph, amount, wit), 0, { value: amount, scriptPubKey: ph }, [{ value: amount, scriptPubKey: ph }], fset); if (r.ok === null) continue; ours = r.ok; } catch { ours = 'err'; }
const expOK = expected === 'OK'; const okk = ours === expOK;
const c = cat(sig, spk, ph, flags, expected, wit);
const rec = { sh, ph, comment: comment || '', ours };
add('scripts', { name: (comment || asm(ph) || asm(sh) || '(empty)'), ok: okk, expect: expOK ? 'pass' : 'reject',
detail: () => kvCard([
rec.comment ? ['note from Bitcoin Core', '<i>“' + esc(rec.comment) + '”</i>'] : ['type', SCAT[c]],
['the lock (on the coin)', '<div class="dasm">' + esc(asm(rec.ph)) + '</div>' + (rec.ph ? '<div class="dhex">' + esc(rec.ph) + '</div>' : '')],
['the key offered to spend', '<div class="dasm">' + esc(asm(rec.sh)) + '</div>' + (rec.sh ? '<div class="dhex">' + esc(rec.sh) + '</div>' : '')],
['expected', expOK ? '<b style="color:' + GOOD + '">accept</b>' : '<b style="color:' + BAD + '">reject</b>'],
['bitcoin-kernel did', (rec.ours === true ? '<b style="color:' + GOOD + '">accept</b>' : rec.ours === false ? '<b style="color:' + BAD + '">reject</b>' : 'error') + ' <span class="match' + (okk ? '' : ' bad') + '">' + (okk ? '✓ correct' : '✗ wrong') + '</span>'],
].filter(Boolean)) });
} if (i < raw.length) { if ($('count')) $('count').textContent = bySuite.scripts.length.toLocaleString(); requestAnimationFrame(chunk); } else done(); })(); });
}
// ---------- the explorer UI ----------
function wireExplorer() {
let active = 'scripts', query = '';
const listEl = $('list'), titleEl = $('suite-title'), descEl = $('suite-desc'), countEl = $('suite-count');
function render() {
const q = query.toLowerCase();
const tests = bySuite[active] || [];
const hits = q ? tests.filter((t) => t.name.toLowerCase().includes(q)) : tests;
titleEl.textContent = SUITE_NAME[active];
descEl.textContent = SUITE_DESC[active];
countEl.textContent = hits.length.toLocaleString() + ' tests' + (hits.length > 200 ? ', showing 200' : '');
listEl.innerHTML = hits.slice(0, 200).map((t, n) => {
const tag = '<span class="exp ' + (t.ok ? 'pass' : 'fail') + '">' + (t.ok ? 'PASS' : 'FAIL') + '</span>';
return '<div class="row" data-n="' + n + '"><span class="v ' + (t.ok ? '' : 'no') + '">' + (t.ok ? '✓' : '✗') + '</span><span class="lab">' + esc(String(t.name).slice(0, 110)) + '</span>' + tag + '</div><div class="detail" id="dt' + n + '"></div>';
}).join('');
listEl._hits = hits;
}
document.querySelectorAll('.tab').forEach((tb) => tb.addEventListener('click', () => { active = tb.dataset.k; query = ''; $('search').value = ''; document.querySelectorAll('.tab').forEach((x) => x.classList.toggle('on', x === tb)); render(); }));
$('search').oninput = (e) => { query = e.target.value; render(); };
listEl.onclick = (e) => { const row = e.target.closest('.row'); if (!row) return; const d = $('dt' + row.dataset.n); const o = d.classList.toggle('open'); row.classList.toggle('open', o); if (o && !d.innerHTML) d.innerHTML = listEl._hits[row.dataset.n].detail(); };
document.querySelector('.tab[data-k=scripts]').classList.add('on');
render();
}
</script>
</body>
</html>