```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;
  let hasOnes = true;
  for (const field of fields) {
    const n = parseInt(field, 10);
    if (!Number.isInteger(n) || n < 0 || n > 255 || (field.length > 1 && field[0] === "0")) {
      throw new Error("mask field must be a decimal number from 0 to 255 with no leading zero");
    }
    if (n !== 0) {
      if (!hasOnes) {
        throw new Error("mask ones must not be broken by zeros");
      }
      prefix += n.toString(2).replace(/0/g, "").length;
    } else {
      hasOnes = false;
    }
  }
  return prefix;
}
```