```ts
export function maskFromPrefix(prefix: number): string {
  if (!Number.isInteger(prefix) || prefix < 0 || prefix > 32) {
    throw new Error("prefix must be an integer from 0 to 32");
  }
  const octets: number[] = [];
  for (let slot = 0; slot < 4; slot += 1) {
    const ones = Math.min(8, Math.max(0, prefix - slot * 8));
    octets.push(256 - 2 ** (8 - ones));
  }
  return octets.join(".");
}

export function prefixFromMask(mask: string): number {
  if (typeof mask !== "string") {
    throw new Error("mask must be a string");
  }
  const parts = mask.split(".");
  if (parts.length !== 4) {
    throw new Error("mask must have exactly four dot-separated fields");
  }
  let prefix = 0;
  let hasOnes = true;
  for (const part of parts) {
    const num = parseInt(part, 10);
    if (isNaN(num)) {
      throw new Error("each field must hold decimal digits");
    }
    if (part.length > 1 && part.startsWith("0")) {
      throw new Error("fields with leading zeros other than 0 itself are invalid");
    }
    if (num < 0 || num > 255) {
      throw new Error("fields must be within the range 0 to 255");
    }
    if (hasOnes && num !== 255) {
      hasOnes = false;
    } else if (!hasOnes && num !== 0) {
      throw new Error("ones bits are broken by a zero");
    }
    prefix += Math.floor(Math.log2(256 - num));
  }
  return prefix;
}
```