```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 (let slot = 0; slot < 4; slot += 1) {
    const field = fields[slot];
    const num = parseInt(field, 10);
    if (!Number.isInteger(num) || num < 0 || num > 255) {
      throw new Error("mask must have decimal numbers between 0 and 255");
    }
    if (slot === 0 && field.length > 1 && field.startsWith("0")) {
      throw new Error("leading zero not allowed for non-zero fields");
    }
    prefix += num === 255 ? 8 : Math.log2(256 - num);
    if (num !== 0 && num % 256 !== 255) {
      throw new Error("mask must have a run of leading one bits broken only by zeros");
    }
  }
  return prefix;
}
```