public static Bytes pathToBytes(final Bytes path) {
    checkArgument(!path.isEmpty(), "Path must not be empty");
    checkArgument(path.get(path.size() - 1) == LEAF_TERMINATOR, "Path must be a leaf path");
    final MutableBytes bytes = MutableBytes.create((path.size() - 1) / 2);
    int bytesPos = 0;
    for (int pathPos = 0; pathPos < path.size() - 1; pathPos += 2, bytesPos += 1) {
      final byte high = path.get(pathPos);
      final byte low = path.get(pathPos + 1);
      if ((high & 0xf0) != 0 || (low & 0xf0) != 0) {
        throw new IllegalArgumentException("Invalid path: contains elements larger than a nibble");
      }
      bytes.set(bytesPos, (byte) (high << 4 | low));
    }
    return bytes;
  }
Converts a path into a byte sequence by combining each pair of nibbles into a byte. The path
must be a leaf path, i.e., it must be terminated with a leaf terminator.

@param path the path to convert into a byte sequence
@return the resulting byte sequence
@throws IllegalArgumentException if the path is empty or not a leaf path, or if it contains
    elements larger than a nibble
ethereum/trie/src/main/java/org/hyperledger/besu/ethereum/trie/CompactEncoding.java