```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 fields = mask.split(".");
  if (fields.length !== 4) {
    throw new Error("mask must have exactly four dot-separated fields");
  }
  let prefix = 0;
  for (const field of fields) {
    const value = Number(field);
    if (!Number.isInteger(value) || value < 0 || value > 255) {
      throw new Error("mask fields must be decimal digits within 0-255");
    }
    if (/^0\d/.test(field) && field !== "0") {
      throw new Error("mask fields with leading zeros are invalid except for '0'");
    }
    prefix += Math.log2(256 - value);
  }
  if (fields.some((field, i, arr) => i > 0 && (arr[i - 1] < 255 || field !== "0"))) {
    throw new Error("mask ones must not be broken by zeros");
  }
  return prefix;
}
```