trustbeat 0.2.2

Anchor files to qualified eIDAS timestamps and verify the proofs offline.
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
//! Merkle inclusion-proof verification.
//!
//! Mirrors `MerkleVerifier.scala` and every SDK implementation (see
//! `sdk/go/verify.go`). The fold depends on the construction the proof declares
//! in `merkle_algorithm`:
//!
//! ```text
//! trustbeat-legacy-sha256   leaf   = your hash
//!                           parent = SHA-256(left || right)
//!
//! rfc6962-sha256            leaf   = SHA-256(0x00 || your hash)
//!                           parent = SHA-256(0x01 || left || right)
//! ```
//!
//! Each step's `side` gives the *sibling's* position, so:
//!
//! - `"left"`  → sibling is the left child → hash over (sibling, current)
//! - `"right"` → sibling is the right child → hash over (current, sibling)
//!
//! A proof with no `merkle_algorithm` predates the field and is legacy.

use sha2::{Digest, Sha256};

use crate::proof::ProofStep;

/// The original TrustBeat construction.
pub const LEGACY_SHA256: &str = "trustbeat-legacy-sha256";

/// RFC 6962 / RFC 9162.
pub const RFC6962_SHA256: &str = "rfc6962-sha256";

/// Leaf and node prefixes for a declared algorithm.
///
/// `None` (absent on the wire) means legacy. An unrecognised name is an error,
/// never a silent fallback: "unsupported" and "forged" must not look alike.
fn prefixes(algorithm: Option<&str>) -> Result<(&'static [u8], &'static [u8]), MerkleError> {
    match algorithm.unwrap_or(LEGACY_SHA256) {
        LEGACY_SHA256 => Ok((&[], &[])),
        RFC6962_SHA256 => Ok((&[0x00], &[0x01])),
        other => Err(MerkleError::UnsupportedAlgorithm(other.to_string())),
    }
}

#[derive(Debug, PartialEq, Eq)]
pub enum MerkleError {
    BadLeafHex(String),
    BadSiblingHex { step: usize, value: String },
    UnknownSide { step: usize, side: String },
    BadRootHex(String),
    UnsupportedAlgorithm(String),
}

impl std::fmt::Display for MerkleError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::BadLeafHex(v) => write!(f, "invalid leaf hash hex: {v:?}"),
            Self::BadSiblingHex { step, value } => {
                write!(f, "invalid sibling hex at step {step}: {value:?}")
            }
            Self::UnknownSide { step, side } => {
                write!(
                    f,
                    "unknown side {side:?} at step {step}: want \"left\" or \"right\""
                )
            }
            Self::BadRootHex(v) => write!(f, "invalid merkle_root hex: {v:?}"),
            Self::UnsupportedAlgorithm(v) => write!(
                f,
                "unsupported merkle_algorithm {v:?}: this build understands {LEGACY_SHA256:?} \
                 and {RFC6962_SHA256:?}; upgrade the CLI, or verify via the API"
            ),
        }
    }
}

impl std::error::Error for MerkleError {}

/// Re-derives the Merkle root from `leaf_hash` and the proof path.
pub fn derive_root(
    leaf_hash: &str,
    path: &[ProofStep],
    algorithm: Option<&str>,
) -> Result<Vec<u8>, MerkleError> {
    let (leaf_prefix, node_prefix) = prefixes(algorithm)?;

    let mut current =
        hex_decode(leaf_hash).ok_or_else(|| MerkleError::BadLeafHex(leaf_hash.to_string()))?;
    if !leaf_prefix.is_empty() {
        let mut hasher = Sha256::new();
        hasher.update(leaf_prefix);
        hasher.update(&current);
        current = hasher.finalize().to_vec();
    }

    for (i, step) in path.iter().enumerate() {
        let sibling = hex_decode(&step.sibling).ok_or_else(|| MerkleError::BadSiblingHex {
            step: i,
            value: step.sibling.clone(),
        })?;
        let mut hasher = Sha256::new();
        hasher.update(node_prefix);
        match step.side.as_str() {
            "left" => {
                hasher.update(&sibling);
                hasher.update(&current);
            }
            "right" => {
                hasher.update(&current);
                hasher.update(&sibling);
            }
            other => {
                return Err(MerkleError::UnknownSide {
                    step: i,
                    side: other.to_string(),
                })
            }
        }
        current = hasher.finalize().to_vec();
    }
    Ok(current)
}

/// Re-derives the root and compares it to `expected_root` in constant time.
pub fn verify_inclusion(
    leaf_hash: &str,
    path: &[ProofStep],
    expected_root: &str,
    algorithm: Option<&str>,
) -> Result<bool, MerkleError> {
    let derived = derive_root(leaf_hash, path, algorithm)?;
    let expected = hex_decode(expected_root)
        .ok_or_else(|| MerkleError::BadRootHex(expected_root.to_string()))?;
    Ok(constant_time_eq(&derived, &expected))
}

pub fn hex_decode(s: &str) -> Option<Vec<u8>> {
    if s.len() % 2 != 0 {
        return None;
    }
    (0..s.len())
        .step_by(2)
        .map(|i| u8::from_str_radix(&s[i..i + 2], 16).ok())
        .collect()
}

pub fn hex_encode(bytes: &[u8]) -> String {
    use std::fmt::Write;
    bytes.iter().fold(String::new(), |mut acc, b| {
        let _ = write!(acc, "{b:02x}");
        acc
    })
}

/// Constant-time byte comparison — no early return on first mismatch.
pub fn constant_time_eq(a: &[u8], b: &[u8]) -> bool {
    if a.len() != b.len() {
        return false;
    }
    a.iter().zip(b).fold(0u8, |acc, (x, y)| acc | (x ^ y)) == 0
}

#[cfg(test)]
mod tests {
    use super::*;

    fn step(sibling: &str, side: &str) -> ProofStep {
        ProofStep {
            sibling: sibling.to_string(),
            side: side.to_string(),
        }
    }

    fn sha256_hex(parts: &[&[u8]]) -> String {
        let mut h = Sha256::new();
        for p in parts {
            h.update(p);
        }
        hex_encode(&h.finalize())
    }

    #[test]
    fn legacy_single_leaf_batch_root_is_the_leaf() {
        // A batch of one under the legacy tree: empty proof path, root == leaf
        // (matches demo-proof.json).
        let leaf = "264234150e0c34cde02b241cefb0dd13751231f0fa0e202c6eb18e79ab9321a4";
        assert!(verify_inclusion(leaf, &[], leaf, None).unwrap());
    }

    #[test]
    fn sibling_on_the_right_hashes_current_first() {
        let leaf = "aa".repeat(32);
        let sib = "bb".repeat(32);
        let expected = sha256_hex(&[&hex_decode(&leaf).unwrap(), &hex_decode(&sib).unwrap()]);
        assert!(verify_inclusion(&leaf, &[step(&sib, "right")], &expected, None).unwrap());
    }

    #[test]
    fn sibling_on_the_left_hashes_sibling_first() {
        let leaf = "aa".repeat(32);
        let sib = "bb".repeat(32);
        let expected = sha256_hex(&[&hex_decode(&sib).unwrap(), &hex_decode(&leaf).unwrap()]);
        assert!(verify_inclusion(&leaf, &[step(&sib, "left")], &expected, None).unwrap());
    }

    #[test]
    fn side_is_not_commutative() {
        // The same sibling on the other side must NOT produce the same root,
        // otherwise the proof would prove nothing about position.
        let leaf = "aa".repeat(32);
        let sib = "bb".repeat(32);
        let root_right = derive_root(&leaf, &[step(&sib, "right")], None).unwrap();
        let root_left = derive_root(&leaf, &[step(&sib, "left")], None).unwrap();
        assert_ne!(root_right, root_left);
    }

    #[test]
    fn multi_step_path_climbs_the_tree() {
        let leaf = "11".repeat(32);
        let s1 = "22".repeat(32);
        let s2 = "33".repeat(32);
        let level1 = sha256_hex(&[&hex_decode(&leaf).unwrap(), &hex_decode(&s1).unwrap()]);
        let root = sha256_hex(&[&hex_decode(&s2).unwrap(), &hex_decode(&level1).unwrap()]);
        assert!(
            verify_inclusion(&leaf, &[step(&s1, "right"), step(&s2, "left")], &root, None).unwrap()
        );
    }

    #[test]
    fn tampered_leaf_fails() {
        let leaf = "aa".repeat(32);
        let sib = "bb".repeat(32);
        let expected = sha256_hex(&[&hex_decode(&leaf).unwrap(), &hex_decode(&sib).unwrap()]);
        let tampered = "ab".repeat(32);
        assert!(!verify_inclusion(&tampered, &[step(&sib, "right")], &expected, None).unwrap());
    }

    #[test]
    fn tampered_root_fails() {
        let leaf = "aa".repeat(32);
        assert!(!verify_inclusion(&leaf, &[], &"cc".repeat(32), None).unwrap());
    }

    #[test]
    fn malformed_input_is_an_error_not_a_false_negative() {
        assert_eq!(
            verify_inclusion("zz", &[], &"aa".repeat(32), None).unwrap_err(),
            MerkleError::BadLeafHex("zz".into())
        );
        assert_eq!(
            verify_inclusion(
                &"aa".repeat(32),
                &[step("zz", "left")],
                &"aa".repeat(32),
                None
            )
            .unwrap_err(),
            MerkleError::BadSiblingHex {
                step: 0,
                value: "zz".into()
            }
        );
        assert_eq!(
            verify_inclusion(
                &"aa".repeat(32),
                &[step(&"bb".repeat(32), "up")],
                &"aa".repeat(32),
                None
            )
            .unwrap_err(),
            MerkleError::UnknownSide {
                step: 0,
                side: "up".into()
            }
        );
    }

    #[test]
    fn odd_length_hex_is_rejected() {
        assert!(hex_decode("abc").is_none());
        assert!(hex_decode("abcd").is_some());
    }

    #[test]
    fn constant_time_eq_matches_normal_eq() {
        assert!(constant_time_eq(b"abc", b"abc"));
        assert!(!constant_time_eq(b"abc", b"abd"));
        assert!(!constant_time_eq(b"abc", b"ab"));
    }

    // ── merkle_algorithm dispatch (CLI 0.2.0) ─────────────────────────────

    #[test]
    fn absent_algorithm_is_legacy() {
        // Proofs issued before the field existed must keep verifying forever.
        let leaf = "aa".repeat(32);
        assert!(verify_inclusion(&leaf, &[], &leaf, None).unwrap());
        assert!(verify_inclusion(&leaf, &[], &leaf, Some(LEGACY_SHA256)).unwrap());
    }

    #[test]
    fn rfc6962_hashes_the_leaf() {
        let leaf = "aa".repeat(32);
        let rfc_root = sha256_hex(&[&[0x00], &hex_decode(&leaf).unwrap()]);
        assert!(verify_inclusion(&leaf, &[], &rfc_root, Some(RFC6962_SHA256)).unwrap());
        // Under rfc6962 a one-leaf root is not the leaf itself.
        assert!(!verify_inclusion(&leaf, &[], &leaf, Some(RFC6962_SHA256)).unwrap());
    }

    #[test]
    fn rfc6962_reference_vector() {
        // MTH([SHA256("a"), SHA256("b"), SHA256("c")]) per RFC 6962, leaf 0.
        let a = sha256_hex(&[b"a"]);
        let path = [
            step(
                "a0d9f0a50b35b9f7d7edc57fb64f4771ddef0fefeaca4e6f949a1514db5b136d",
                "right",
            ),
            step(
                "6a3fc11b79f836bda340e75c8906e961b8adf4d6a08a2b992e3f38cd6ff38ebf",
                "right",
            ),
        ];
        let root = "cac3d448d4e20a2ad5eae1f500e63c2a7f9217cd14572ba7fd22e26dc1ec2648";
        assert!(verify_inclusion(&a, &path, root, Some(RFC6962_SHA256)).unwrap());
    }

    // Vectors below are taken verbatim from Google's transparency-dev/merkle
    // (rfc6962_test.go) — a third-party implementation. Our own arithmetic only
    // proves self-consistency; these prove conformance.
    const UPSTREAM_ENTRY: &str = "4c313233343536"; // hex of "L123456"
    const UPSTREAM_LEAF: &str = "395aa064aa4c29f7010acfe3f25db9485bbd4b91897b6ad7ad547639252b4d56";
    const UPSTREAM_EMPTY_LEAF: &str =
        "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d";
    const UPSTREAM_ROOT_2: &str =
        "bf9ae70442844df993ca0001a7c8a095c5f145857960b1ee389df6cbe84b5bf3";

    #[test]
    fn leaf_hash_matches_upstream_vector() {
        // SHA-256(0x00 || "L123456") per transparency-dev/merkle.
        assert!(
            verify_inclusion(UPSTREAM_ENTRY, &[], UPSTREAM_LEAF, Some(RFC6962_SHA256)).unwrap()
        );
    }

    #[test]
    fn rfc6962_left_sibling_applies_the_node_prefix() {
        // Two-leaf tree whose BOTH leaf hashes are upstream vectors.
        // Exercises side="left", which no other rfc6962 test reaches.
        let path = [step(UPSTREAM_EMPTY_LEAF, "left")];
        assert!(
            verify_inclusion(UPSTREAM_ENTRY, &path, UPSTREAM_ROOT_2, Some(RFC6962_SHA256)).unwrap()
        );
    }

    // ── Shared RFC 6962 fixture ────────────────────────────────────────────
    //
    // tests/fixtures/rfc6962-proofs.json is checked by the Scala engine and by
    // every SDK, so this pins cross-implementation agreement rather than
    // self-consistency.

    fn fixture() -> serde_json::Value {
        let mut dir = std::env::current_dir().expect("cwd");
        for _ in 0..8 {
            let f = dir.join("tests/fixtures/rfc6962-proofs.json");
            if f.exists() {
                return serde_json::from_str(&std::fs::read_to_string(f).unwrap()).unwrap();
            }
            if !dir.pop() {
                break;
            }
        }
        panic!("rfc6962-proofs.json not found");
    }

    fn fixture_steps(p: &serde_json::Value) -> Vec<ProofStep> {
        p["proof_path"]
            .as_array()
            .unwrap()
            .iter()
            .map(|s| step(s["sibling"].as_str().unwrap(), s["side"].as_str().unwrap()))
            .collect()
    }

    #[test]
    fn every_fixture_proof_verifies() {
        let doc = fixture();
        let proofs = doc["proofs"].as_array().unwrap();
        assert_eq!(proofs.len(), 7, "fixture should hold 7 proofs");
        for p in proofs {
            assert!(
                verify_inclusion(
                    p["hash"].as_str().unwrap(),
                    &fixture_steps(p),
                    p["merkle_root"].as_str().unwrap(),
                    Some(p["merkle_algorithm"].as_str().unwrap()),
                )
                .unwrap(),
                "leaf {} failed",
                p["leaf_index"]
            );
        }
    }

    #[test]
    fn a_tampered_fixture_proof_fails() {
        // Guards against the suite passing because verification is a no-op.
        let doc = fixture();
        let p = &doc["proofs"][0];
        assert!(!verify_inclusion(
            &"00".repeat(32),
            &fixture_steps(p),
            p["merkle_root"].as_str().unwrap(),
            Some(RFC6962_SHA256),
        )
        .unwrap());
    }

    #[test]
    fn unknown_algorithm_is_an_error_not_a_false_negative() {
        // "I cannot check this" must not look like "this proof is forged".
        let leaf = "aa".repeat(32);
        assert_eq!(
            verify_inclusion(&leaf, &[], &leaf, Some("sha3-512-tree")).unwrap_err(),
            MerkleError::UnsupportedAlgorithm("sha3-512-tree".into())
        );
    }
}