```python
def mask_from_prefix(prefix: int) -> str:
    if isinstance(prefix, bool) or not isinstance(prefix, int) or not 0 <= prefix <= 32:
        raise ValueError("prefix must be an integer from 0 to 32")
    octets = []
    for slot in range(4):
        ones = min(8, max(0, prefix - slot * 8))
        octets.append(str(256 - 2 ** (8 - ones)))
    return ".".join(octets)


def prefix_from_mask(mask: str) -> int:
    if not isinstance(mask, str):
        raise ValueError("mask must be a string")
    
    try:
        octets = mask.split('.')
        if len(octets) != 4:
            raise ValueError("mask must have exactly four dot-separated fields")
        
        prefix = 0
        for octet in octets:
            if not octet.isdigit() or (len(octet) > 1 and octet[0] == '0'):
                raise ValueError("each field must be a decimal number without leading zeros except 0 itself")
            octet_value = int(octet)
            if octet_value > 255:
                raise ValueError("each field must hold a value between 0 and 255 inclusive")
            
            prefix_bits = bin(octet_value)[2:].zfill(8)
            if '1' in prefix_bits[1:]:
                raise ValueError("mask must have an unbroken run of one bits from the top followed by only zeros")
            prefix += prefix_bits.count('1')
    
    except (ValueError, AttributeError) as e:
        raise ValueError(f"invalid mask: {e}")
    
    return prefix
```