pub fn key_height(key: &str) -> usizeExpand description
Which layer of the tree a key belongs on.
The rule is the whole of it: hash the key with sha-256, count the leading
zero bits of the digest, and divide by two. Every part of that sentence
is a way to get it wrong — bytes instead of bits gives a height that is
almost always zero, and forgetting the divide doubles every layer — and a
tree built either way still works, which is why this is checked against
mst/key_heights.json rather than against a tree.
The key is hashed as its UTF-8 bytes. atproto record keys are ASCII by rule, so this is not a choice that shows up in the vectors, but it is the one every implementation makes and it is written down here rather than left to the caller.
The height is unbounded in principle — a digest of 256 zero bits would answer 128 — and in practice a key above layer four is already rare. There is no error case: every string has a hash and every hash has a height.
use didbot_data::mst::key_height;
assert_eq!(key_height("asdf"), 0);
assert_eq!(key_height("blue"), 1);
assert_eq!(key_height("app.bsky.feed.post/9adeb165882c"), 8);