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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! `sidestr-header` — block headers for sidestr sidechains, in both families
//! the parent table allows.
//!
//! A [sidestr](https://github.com/sidestr/spec) chain is an overlay on a
//! Bitcoin-family parent, and SPEC 3 is explicit that the overlay names no
//! header format and no hash: "everything the overlay does not set is
//! inherited from the parent: header format and proof-of-work hash …
//! Nothing in the document names a header format or a hash; the parent
//! decides both." SPEC 3.2's parents table then fixes two families:
//!
//! | parent alias | chain | headers, proof of work | this crate |
//! |---|---|---|---|
//! | `btc`, `tbtc4` | Bitcoin mainnet, testnet4 | stock 80 bytes, SHA-256d | [`StockHeader`] |
//! | `xbt`, `txbt4` | Bitcoin Knots' BLAKE2b fork of each | 164-byte v2, BLAKE2b | [`Blake2bV2Header`] |
//!
//! `ltc` and `vtc` are reserved upstream and absent here. The crate is a port
//! of the reference JavaScript — the bitcoin-desktop/schema kernel's header
//! codec, Knots proof of work and Knots overlay rules, and Melvin Carvalho's
//! `siding` for how a sidestr block shapes its header and what its signature
//! covers — under the same AGPL-3.0 licence (agentbox ADR-2106). Without the
//! `core` feature it is `#![no_std]`, allocates nothing, and takes every
//! primitive from RustCrypto ([`sha2`], [`blake2`]) with no `bitcoin` crate
//! dependency (ADR-2096 D2): the header and its proof of work are the part of
//! consensus that the parent's serialisation library does not own.
//!
//! With `core` (default) the [`family`] module implements
//! `sidestr_core::HeaderFamily` for both header types, so
//! `sidestr_core::StateOf<Blake2bV2>` and `ChainOf<Blake2bV2>` validate and
//! produce a chain beside `xbt` or `txbt4` end to end — the v2 header, the
//! BLAKE2b proof of work, the Knots overlay's rules and Knots' unified sighash
//! on every spend — proven by replaying the live `sidestr:txbt4-siding` chain.
//! The edge points from this crate to `sidestr-core`, never back.
//!
//! # What it gives a validator
//!
//! Per family, through [`HeaderFamily`] and the enum [`Header`] or the two
//! structs directly:
//!
//! - the wire size and a strict `decode` / `encode` pair;
//! - `hash()`, the block id, which in both families **is** the proof-of-work
//! hash (SHA-256d of the 80 bytes; the v2 pipeline for Knots) — so there is
//! no separate `pow_hash`;
//! - [`Target`] with compact `bits` decoding, and [`check_pow`] with the
//! `powLimit` semantics siding uses — `bits` is pinned to `powLimit`'s
//! compact form and never retargets (SPEC 3, SPEC 4 step 1);
//! - the BIP-325 block-data preimage over that family's serialisation
//! ([`signet`]), so `sidestr-core` signs and verifies blocks without knowing
//! the layout;
//! - the version-bit-31 rule: bit 31 selects the family, so a stock header
//! with it set and a v2 header without it are both refused at decode;
//! - the fork activation constants of SPEC 3.2 ([`fork`]);
//! - with `core`, the two families as `sidestr-core` sees them
//! ([`Blake2bV2`], [`family::Stock`]).
//!
//! # Where this port departs from the reference
//!
//! - **The genesis is judged under the family's rules.** The reference
//! applies block 0 on its hash; through `sidestr-core`'s
//! `StateOf::from_genesis` a typed v2 genesis is held to
//! `knots:rule-header-v2-from-fork`, `-height` and `-flags-reserved`, the
//! proof of work and the signature before its hash is compared to the
//! document, so a header the decoder would refuse cannot enter as a struct
//! either (`tests/audit_regressions.rs`).
//! - **Version bit 31 is refused on every stock path.** [`StockHeader::decode`]
//! refuses the bytes; [`family::Stock`] reports the version as the kernel
//! types it (`i32le`, so bit 31 is negative) and `btc:rule-header-version`
//! refuses a typed header, as the kernel does on the same block.
//! - **Compact targets Bitcoin Core rejects** (negative, overflow) are
//! rejected by [`Target::from_compact`] where the kernel is lenient —
//! unreachable on a valid sidestr chain, where `bits` is pinned to
//! `powLimit`.
//! - **A mirror's record framing is checked** on replay (`sidestr-core`'s
//! `blockfile::read_block`): the `[u32 height][u32 size]` prefix of every
//! record must agree with the index entry, and the entry must lie within
//! the file.
//!
//! # The v2 header, field by field
//!
//! The 164-byte Knots v2 header keeps the classic 80-byte prefix (version with
//! bit 31 set, previous hash, merkle root, time on wire, bits, nonce) and
//! appends 84 bytes: two more nonces, a 128-bit extranonce, a time offset, the
//! committed transaction count, a flags byte (ASIC profile in bits 0–1, time
//! offset in bit 2, bits 6–7 reserved), the XOR-mask clear count, a 128-bit XOR
//! key, the committed height, and a 32-byte merge-mining hook. The table with
//! offsets is on [`Blake2bV2Header`]. Its hash is not a hash of the bytes but a
//! commitment tree: two BIP-340 tagged-hash rounds over the fields, then two
//! BLAKE2b-256 rounds whose second input layout the ASIC profile selects, then
//! an XOR mask derived from the key (see [`Blake2bV2Header::hash`]).
//!
//! # The signet preimage
//!
//! Siding's `blockData` hashes the first 72 header bytes — version, previous
//! hash, merkle root, time on wire — with the merkle root recomputed over the
//! coinbase stripped of its solution. Those 72 bytes have the same layout in
//! both families, so [`Header::block_data`] is one function.
//!
//! # Example
//!
//! ```
//! use sidestr_header::{Header, HeaderFamily, Target};
//!
//! // sidestr:dreamlab block 0, beside tbtc4: stock family.
//! let bytes = hex::decode(
//! "0000002000000000000000000000000000000000000000000000000000000000000000003a87d59ecf60ab58ee75948cc39d1bb44ac4285747e64b5a1e7a960d37764cb40e67b26affff7f2002000000",
//! ).unwrap();
//! let header = HeaderFamily::Stock.decode(&bytes).unwrap();
//! assert_eq!(header.hash().to_string(),
//! "4db37517728bd509c0cb96ee5a2e3e2a77f9e965a092e9f67948b413d453dbc0");
//!
//! // SPEC 4 step 1 against the chain document's powLimit.
//! let pow_limit = Target::from_hex(
//! "7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();
//! assert!(header.check_pow(&pow_limit).is_ok());
//!
//! // The same 80 bytes are not a v2 header, and 81 bytes are not a header.
//! assert!(HeaderFamily::Blake2bV2.decode(&bytes).is_err());
//! assert!(HeaderFamily::Stock.decode(&bytes[..79]).is_err());
//!
//! // Re-encoding is the identity.
//! assert_eq!(header.encode().as_ref(), bytes.as_slice());
//! # let _: Header = header;
//! ```
//!
//! # Provenance
//!
//! - `codec/pow/knots-header-v2.js`, `codec/pow/blake2b.js`, `codec/hash.js`,
//! `codec/codec.js`, `codec/headers.js`, `codec/overlays/knots-blake2b.js`
//! (the overlay's header and block checks), `schema/overlays/knots-blake2b.jsonld`
//! of [bitcoin-desktop/schema](https://github.com/bitcoin-desktop/schema)
//! (AGPL-3.0), at commit `b8cbf6337c7450fe14ddc5bce00c7280059aab5d`.
//! - `siding/lib/block.mjs`, `siding/lib/parents.mjs`, `siding/lib/chain.mjs`,
//! `siding/lib/overlay.mjs` (`blake2bHeight: 0`, `unifiedSighashParam`),
//! `SPEC.md` §3, §3.2, §4 of [sidestr/spec](https://github.com/sidestr/spec)
//! (AGPL-3.0, Melvin Carvalho), at commit
//! `2de40bdac4cba01be0864156a553d8287c22e279`.
//! - Test vectors: Knots' own `block_header_v2.json` and real fork headers
//! captured from a Knots 29.4.1 node on 2026-09-05, both carried by the
//! schema kernel; siding's `blockData` on the live `sidestr:dreamlab` block 0
//! and on kernel-hashed v2 headers, computed with the JavaScript engine as
//! the oracle; and the live `sidestr:txbt4-siding` chain (229 blocks as of
//! 2026-09-22) as the oracle for the whole BLAKE2b family through
//! `sidestr-core` (`tests/core_family.rs`).
// The crate docs link `family`, `Blake2bV2` and `family::Stock`, which exist
// only with `core`; without it those links have nowhere to point and are not
// an error.
pub use Error;
pub use Blake2bV2;
pub use StockHeader;
pub use Target;
pub use ;
use fmt;
/// Version bit 31: set on every v2 header, clear on every stock header
/// (`VERSION_HEADER_V2_FLAG` in `codec/pow/knots-header-v2.js`; the kernel's
/// `structVariants` selects the v2 struct on it).
pub const VERSION_HEADER_V2_FLAG: u32 = 0x8000_0000;
/// The kernel's name for the stock family's proof-of-work hash.
pub const SHA256D_POW_HASH_NAME: &str = "sha256d";
/// A block hash in **display order** — the byte order bitcoind prints and a
/// chain document's `genesisHash` uses; read as a big-endian number it
/// compares directly against a [`Target`].
///
/// ```
/// use sidestr_header::BlockHash;
/// let h = BlockHash::from_hex("000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f").unwrap();
/// assert_eq!(h.to_string(), "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f");
/// assert_eq!(h.to_wire()[0], 0x6f); // wire order is the reverse
/// assert_eq!(BlockHash::from_wire(h.to_wire()), h);
/// ```
;
/// The two header families of SPEC 3.2, named after the header they carry.
/// A header of either family, with the operations a sidestr validator needs
/// dispatched to the right one.
/// A header's wire bytes, sized for the larger family; `as_ref()` yields
/// exactly the family's bytes.
/// SPEC 4 step 1, "the header meets `powLimit`. No difficulty adjustment, no
/// minimum-difficulty window", as siding and the kernel enforce it together:
///
/// 1. `bits` equals `pow_limit.to_compact()` — siding writes that value into
/// every block (`siding/lib/chain.mjs`) and the kernel's difficulty rule
/// under `powNoRetargeting` requires each block's bits to equal the
/// previous block's, so the value never moves from genesis;
/// 2. `hash ≤ Target::from_compact(bits)` (`btc:rule-header-pow`).
///
/// Compact encoding keeps only three significant bytes, so the target a
/// block is actually checked against is `powLimit` truncated to its compact
/// form — for the usual `7fff…ff` limit that is `7fffff00…00`, exactly as
/// the kernel's `expandCompact(bits)` computes it.
///
/// Errors are [`Error::BitsNotPowLimit`] and [`Error::TargetNotMet`] in that
/// order; a `bits` that does not decode is reported by [`Target::from_compact`].
///
/// ```
/// use sidestr_header::{check_pow, BlockHash, Target};
/// let lim = Target::from_hex("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();
/// let easy = BlockHash::from_hex("4db37517728bd509c0cb96ee5a2e3e2a77f9e965a092e9f67948b413d453dbc0").unwrap();
/// assert!(check_pow(0x207f_ffff, easy, &lim).is_ok());
/// // Wrong bits, even an easier target, is refused before the hash is looked at.
/// assert!(check_pow(0x2100_ffff, easy, &lim).is_err());
/// // A hash above the limit is refused.
/// let high = BlockHash::from_hex("8000000000000000000000000000000000000000000000000000000000000000").unwrap();
/// assert!(check_pow(0x207f_ffff, high, &lim).is_err());
/// ```